Return-Path: X-Original-To: apmail-tuscany-commits-archive@www.apache.org Delivered-To: apmail-tuscany-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 0FF329F9D for ; Thu, 16 Aug 2012 14:21:33 +0000 (UTC) Received: (qmail 88102 invoked by uid 500); 16 Aug 2012 14:21:33 -0000 Delivered-To: apmail-tuscany-commits-archive@tuscany.apache.org Received: (qmail 88078 invoked by uid 500); 16 Aug 2012 14:21:32 -0000 Mailing-List: contact commits-help@tuscany.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@tuscany.apache.org Delivered-To: mailing list commits@tuscany.apache.org Received: (qmail 88071 invoked by uid 99); 16 Aug 2012 14:21:32 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 16 Aug 2012 14:21:32 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED,NORMAL_HTTP_TO_IP X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 16 Aug 2012 14:21:31 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 0CA982388AC8 for ; Thu, 16 Aug 2012 14:20:48 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1373857 - in /tuscany/sca-java-2.x/trunk/testing/itest/dynamic/src/test: java/test/DynamicEndpointTestCase.java java/test/EndpointHelper.java resources/helloworld.wsdl Date: Thu, 16 Aug 2012 14:20:47 -0000 To: commits@tuscany.apache.org From: antelder@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20120816142048.0CA982388AC8@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: antelder Date: Thu Aug 16 14:20:47 2012 New Revision: 1373857 URL: http://svn.apache.org/viewvc?rev=1373857&view=rev Log: Add a test that shows programatically adding a new remote endpoint and invoking that with DOM objects Added: tuscany/sca-java-2.x/trunk/testing/itest/dynamic/src/test/java/test/DynamicEndpointTestCase.java tuscany/sca-java-2.x/trunk/testing/itest/dynamic/src/test/java/test/EndpointHelper.java tuscany/sca-java-2.x/trunk/testing/itest/dynamic/src/test/resources/helloworld.wsdl Added: tuscany/sca-java-2.x/trunk/testing/itest/dynamic/src/test/java/test/DynamicEndpointTestCase.java URL: http://svn.apache.org/viewvc/tuscany/sca-java-2.x/trunk/testing/itest/dynamic/src/test/java/test/DynamicEndpointTestCase.java?rev=1373857&view=auto ============================================================================== --- tuscany/sca-java-2.x/trunk/testing/itest/dynamic/src/test/java/test/DynamicEndpointTestCase.java (added) +++ tuscany/sca-java-2.x/trunk/testing/itest/dynamic/src/test/java/test/DynamicEndpointTestCase.java Thu Aug 16 14:20:47 2012 @@ -0,0 +1,102 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package test; + +import java.io.File; +import java.io.IOException; + +import javax.xml.namespace.QName; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import org.apache.tuscany.sca.Node; +import org.apache.tuscany.sca.TuscanyRuntime; +import org.apache.tuscany.sca.common.xml.dom.DOMHelper; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.runtime.DOMInvoker; +import org.junit.Test; +import org.xml.sax.SAXException; + +/** + * Create an Endpoint programatically which uses binding.ws and get + * that calls the remote web service exposed by the WSServiceTestCase. + */ +public class DynamicEndpointTestCase extends TestCase { + + private TuscanyRuntime tuscanyRuntime; + private ExtensionPointRegistry extensionPoints; + private Node node; + private WSServiceTestCase wsService; + private DOMHelper domHelper; + + @Override + protected void setUp() throws Exception { + tuscanyRuntime = TuscanyRuntime.newInstance(); + extensionPoints = tuscanyRuntime.getExtensionPointRegistry(); + node = tuscanyRuntime.createNode(); + domHelper = DOMHelper.getInstance(extensionPoints); + + // start the WSServicetestCase service to use as a test target service + wsService = new WSServiceTestCase(); + wsService.setUp(); + wsService.testInvoke(); + + + } + + @Test + public void testInvoke() throws Exception { + + EndpointHelper.addWSEndpoint(node, "SomeEndpointName", new File("src/test/resources/helloworld.wsdl").toURI().toURL(), new QName("http://sample/", "Helloworld"), "http://localhost:8089/testComponent/Helloworld"); + + DOMInvoker domInvoker = node.getDOMInvoker("SomeEndpointName"); + + org.w3c.dom.Node arg = getRequestDOM("petra"); + org.w3c.dom.Node response = domInvoker.invoke("sayHello", arg); + Assert.assertEquals("Hello petra", getResponseString(response)); + } + + private String getResponseString(org.w3c.dom.Node responseDOM) { + String xml = domHelper.saveAsString(responseDOM); + int x = xml.indexOf("") + "".length(); + int y = xml.indexOf(""); + return xml.substring(x, y); + } + + private org.w3c.dom.Node getRequestDOM(String name) { + try { + + String xml = ""+ name + ""; + return domHelper.load(xml); + + } catch (IOException e) { + throw new RuntimeException(e); + } catch (SAXException e) { + throw new RuntimeException(e); + } + } + + @Override + protected void tearDown() throws Exception { + node.stop(); + tuscanyRuntime.stop(); + wsService.tearDown(); + } +} Added: tuscany/sca-java-2.x/trunk/testing/itest/dynamic/src/test/java/test/EndpointHelper.java URL: http://svn.apache.org/viewvc/tuscany/sca-java-2.x/trunk/testing/itest/dynamic/src/test/java/test/EndpointHelper.java?rev=1373857&view=auto ============================================================================== --- tuscany/sca-java-2.x/trunk/testing/itest/dynamic/src/test/java/test/EndpointHelper.java (added) +++ tuscany/sca-java-2.x/trunk/testing/itest/dynamic/src/test/java/test/EndpointHelper.java Thu Aug 16 14:20:47 2012 @@ -0,0 +1,100 @@ +package test; + +import java.net.URI; +import java.net.URL; + +import javax.wsdl.PortType; +import javax.wsdl.Service; +import javax.xml.namespace.QName; + +import org.apache.tuscany.sca.Node; +import org.apache.tuscany.sca.assembly.AssemblyFactory; +import org.apache.tuscany.sca.assembly.Component; +import org.apache.tuscany.sca.assembly.ComponentService; +import org.apache.tuscany.sca.assembly.Endpoint; +import org.apache.tuscany.sca.binding.ws.WebServiceBinding; +import org.apache.tuscany.sca.binding.ws.WebServiceBindingFactory; +import org.apache.tuscany.sca.contribution.Contribution; +import org.apache.tuscany.sca.contribution.ContributionFactory; +import org.apache.tuscany.sca.contribution.processor.ContributionResolveException; +import org.apache.tuscany.sca.contribution.processor.ExtensibleURLArtifactProcessor; +import org.apache.tuscany.sca.contribution.processor.ProcessorContext; +import org.apache.tuscany.sca.contribution.processor.URLArtifactProcessorExtensionPoint; +import org.apache.tuscany.sca.contribution.resolver.ExtensibleModelResolver; +import org.apache.tuscany.sca.contribution.resolver.ModelResolver; +import org.apache.tuscany.sca.contribution.resolver.ModelResolverExtensionPoint; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.FactoryExtensionPoint; +import org.apache.tuscany.sca.impl.NodeImpl; +import org.apache.tuscany.sca.interfacedef.wsdl.WSDLDefinition; +import org.apache.tuscany.sca.interfacedef.wsdl.WSDLFactory; +import org.apache.tuscany.sca.interfacedef.wsdl.WSDLInterface; +import org.apache.tuscany.sca.interfacedef.wsdl.WSDLInterfaceContract; +import org.apache.tuscany.sca.interfacedef.wsdl.WSDLObject; + +public class EndpointHelper { + + public static void addWSEndpoint(Node node, String endpointName, URL wsdlURL, QName portTypeQN, String targetURL) { + ExtensionPointRegistry extensionPoints = ((NodeImpl)node).getExtensionPointRegistry(); + FactoryExtensionPoint modelFactories = extensionPoints.getExtensionPoint(FactoryExtensionPoint.class); + AssemblyFactory assemblyFactory = modelFactories.getFactory(AssemblyFactory.class); + + Component component = assemblyFactory.createComponent(); + component.setName("foo"); + + WebServiceBindingFactory webServiceBindingFactory = modelFactories.getFactory(WebServiceBindingFactory.class); + WebServiceBinding wsBinding = webServiceBindingFactory.createWebServiceBinding(); + wsBinding.setURI(targetURL); + + WSDLInterfaceContract wsdlIC = getWSDLInterfaceContract(wsdlURL, portTypeQN, modelFactories, extensionPoints); + wsBinding.setBindingInterfaceContract(wsdlIC); + + wsBinding.setGeneratedWSDLDocument(((WSDLInterface)wsdlIC.getInterface()).getWsdlDefinition().getDefinition()); + wsBinding.setService((Service)wsBinding.getGeneratedWSDLDocument().getServices().values().iterator().next()); + + Endpoint newEndpoint = assemblyFactory.createEndpoint(); + newEndpoint.setComponent(component); + ComponentService cs = assemblyFactory.createComponentService(); + cs.setName("baa"); + newEndpoint.setService(cs); + newEndpoint.setBinding(wsBinding); + newEndpoint.setURI(endpointName); + newEndpoint.setRemote(true); + + ((NodeImpl)node).getEndpointRegistry().addEndpoint(newEndpoint); + } + + private static WSDLInterfaceContract getWSDLInterfaceContract(URL wsdlURL, QName portTypeQN, FactoryExtensionPoint modelFactories, ExtensionPointRegistry extensionPoints) { + try { + + ContributionFactory contributionFactory = modelFactories.getFactory(ContributionFactory.class); + Contribution contribution = contributionFactory.createContribution(); + ModelResolverExtensionPoint modelResolvers = extensionPoints.getExtensionPoint(ModelResolverExtensionPoint.class); + ModelResolver modelResolver = new ExtensibleModelResolver(contribution, modelResolvers, modelFactories); + contribution.setModelResolver(modelResolver); + + ExtensibleURLArtifactProcessor aproc = new ExtensibleURLArtifactProcessor(extensionPoints.getExtensionPoint(URLArtifactProcessorExtensionPoint.class)); + + ProcessorContext ctx = new ProcessorContext(); + WSDLDefinition wd = aproc.read(null, new URI("anything.wsdl"), wsdlURL, ctx, WSDLDefinition.class); + modelResolver.addModel(wd, ctx); + modelResolver.resolveModel(WSDLDefinition.class, wd, ctx); + final WSDLObject pt = wd.getWSDLObject(PortType.class, portTypeQN); + if(pt == null) + throw new ContributionResolveException("Couldn't find " + portTypeQN); + + WSDLFactory wif = modelFactories.getFactory(WSDLFactory.class); + final WSDLInterface nwi = wif.createWSDLInterface(pt.getElement(), wd, modelResolver, null); + nwi.setWsdlDefinition(wd); + + WSDLInterfaceContract wsdlIC = wif.createWSDLInterfaceContract(); + wsdlIC.setInterface(nwi); + + return wsdlIC; + + } catch(Exception e) { + throw new RuntimeException(e); + } + } + +} Added: tuscany/sca-java-2.x/trunk/testing/itest/dynamic/src/test/resources/helloworld.wsdl URL: http://svn.apache.org/viewvc/tuscany/sca-java-2.x/trunk/testing/itest/dynamic/src/test/resources/helloworld.wsdl?rev=1373857&view=auto ============================================================================== --- tuscany/sca-java-2.x/trunk/testing/itest/dynamic/src/test/resources/helloworld.wsdl (added) +++ tuscany/sca-java-2.x/trunk/testing/itest/dynamic/src/test/resources/helloworld.wsdl Thu Aug 16 14:20:47 2012 @@ -0,0 +1,44 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file