You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by an...@apache.org on 2012/08/16 16:20:47 UTC

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

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("<return>") + "<return>".length();
+        int y = xml.indexOf("</return>");
+        return xml.substring(x, y);
+    }
+
+    private org.w3c.dom.Node getRequestDOM(String name) {
+        try {
+
+            String xml = "<ns2:sayHello xmlns:ns2=\"http://sample/\"><arg0>"+ name + "</arg0></ns2:sayHello>";
+            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<PortType> 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 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<wsdl:definitions name="HelloworldService" targetNamespace="http://sample/" xmlns="http://sample/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:SOAP="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:SOAP11="http://schemas.xmlsoap.org/wsdl/soap/">
+  <wsdl:types>
+    <xs:schema targetNamespace="http://sample/" version="1.0" xmlns:tns="http://sample/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
+<xs:element name="sayHello" type="tns:sayHello"/>
+<xs:element name="sayHelloResponse" type="tns:sayHelloResponse"/>
+<xs:complexType name="sayHelloResponse"><xs:sequence><xs:element minOccurs="0" name="return" type="xs:string"/></xs:sequence></xs:complexType>
+<xs:complexType name="sayHello"><xs:sequence><xs:element minOccurs="0" name="arg0" type="xs:string"/></xs:sequence></xs:complexType>
+</xs:schema>
+  </wsdl:types>
+  <wsdl:message name="sayHelloResponse">
+    <wsdl:part name="sayHelloResponse" element="sayHelloResponse">
+    </wsdl:part>
+  </wsdl:message>
+  <wsdl:message name="sayHello">
+    <wsdl:part name="sayHello" element="sayHello">
+    </wsdl:part>
+  </wsdl:message>
+  <wsdl:portType name="Helloworld">
+    <wsdl:operation name="sayHello">
+      <wsdl:input message="sayHello">
+    </wsdl:input>
+      <wsdl:output message="sayHelloResponse">
+    </wsdl:output>
+    </wsdl:operation>
+  </wsdl:portType>
+  <wsdl:binding name="HelloworldBinding" type="Helloworld">
+    <SOAP:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
+    <wsdl:operation name="sayHello">
+      <SOAP:operation soapAction=""/>
+      <wsdl:input>
+        <SOAP:body use="literal"/>
+      </wsdl:input>
+      <wsdl:output>
+        <SOAP:body use="literal"/>
+      </wsdl:output>
+    </wsdl:operation>
+  </wsdl:binding>
+  <wsdl:service name="HelloworldComponent_Helloworld">
+    <wsdl:port name="HelloworldSOAP11Port" binding="HelloworldBinding">
+      <SOAP:address location="http://9.167.203.149:8080/HelloworldComponent/Helloworld"/>
+    </wsdl:port>
+  </wsdl:service>
+</wsdl:definitions>
\ No newline at end of file