1   /*******************************************************************************
2    *  Copyright (c) 2005, 2006 Imola Informatica.
3    *  All rights reserved. This program and the accompanying materials
4    *  are made available under the terms of the LGPL License v2.1
5    *  which accompanies this distribution, and is available at
6    *  http://www.gnu.org/licenses/lgpl.html
7    *******************************************************************************/
8   package it.imolinfo.jbi4cics.test.webservices.runtime;
9   
10  import it.imolinfo.jbi4cics.jbi.BCELClassLoader;
11  import it.imolinfo.jbi4cics.locator.ServiceLocation;
12  import it.imolinfo.jbi4cics.locator.SimpleLocation;
13  import it.imolinfo.jbi4cics.messageformat.NoOpMappingDescriptor;
14  import it.imolinfo.jbi4cics.test.BaseCommareaTest;
15  import it.imolinfo.jbi4cics.webservices.descriptor.ServiceDescriptor;
16  import it.imolinfo.jbi4cics.webservices.runtime.ServiceCreator;
17  import it.imolinfo.jbi4cics.webservices.utils.generators.ServiceBeanGenerator;
18  import it.imolinfo.jbi4cics.webservices.utils.generators.ServiceInterfaceGenerator;
19  
20  import java.io.FileOutputStream;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.codehaus.xfire.XFire;
25  import org.codehaus.xfire.service.Service;
26  import org.jdom.Document;
27  
28  public class ServiceInvocationTest extends BaseCommareaTest {
29  
30    private static Log log = LogFactory.getLog(ServiceInvocationTest.class);
31  
32    public static final String DeafultHostCodePage="CP037";
33  
34    public ServiceInvocationTest() {
35    }
36  
37    public void testEchoServiceInvocation(){
38      try{
39        // creo il service desciptor
40        NoOpMappingDescriptor noOpMappingDescriptor=new NoOpMappingDescriptor();
41        noOpMappingDescriptor.setBeanClass(StringBean.class);
42        ServiceDescriptor serviceDescriptor=new ServiceDescriptor();
43        serviceDescriptor.setOperationName("provaEchoOperation");
44        serviceDescriptor.setServiceName("ProvaEcho");
45        serviceDescriptor.setServiceInterfacePackageName("it.imolinfo.jbi4cics.test.webservices.utils.generators");
46        serviceDescriptor.setServiceInterfaceName("ProvaServiceBeanInterface");
47        serviceDescriptor.setServiceNameSpace("urn:it.imolinfo.jbi4cics.test.webservices.utils.generators");
48        serviceDescriptor.setInputBeanClassName("ProvaEchoBean");
49        serviceDescriptor.setOutputBeanClassName("ProvaEchoBean");
50        serviceDescriptor.setInputMappingDescriptor(noOpMappingDescriptor);
51        serviceDescriptor.setOutputMappingDescriptor(noOpMappingDescriptor);
52        SimpleLocation serviceLocation=new SimpleLocation();
53        serviceLocation.setConnectionType(ServiceLocation.DUMMY);
54        serviceDescriptor.setServiceLocation(serviceLocation);
55  
56        // creo il service bean, basta quello di input tanto in questo caso sono uguali
57        BCELClassLoader bcelClassLoader = new BCELClassLoader(Thread.currentThread().getContextClassLoader());
58        ServiceBeanGenerator serviceBeanGenerator=new ServiceBeanGenerator(serviceDescriptor,true);
59        serviceBeanGenerator.generateBeanClass(bcelClassLoader);
60  
61  
62        serviceBeanGenerator=new ServiceBeanGenerator(serviceDescriptor,false);
63        serviceBeanGenerator.generateBeanClass(bcelClassLoader);
64  
65  
66        // creo la service interface
67        ServiceInterfaceGenerator serviceInterfaceGenerator=new ServiceInterfaceGenerator(serviceDescriptor);
68        serviceInterfaceGenerator.generateServiceInterface(bcelClassLoader);
69  
70  
71        //creo il servizio xfire
72        ServiceCreator serviceCreator=new ServiceCreator();
73        XFire xfire = getXFire();
74        Service service=serviceCreator.createService(serviceDescriptor,xfire);
75        service.getWSDLWriter().write(new FileOutputStream("target/test-wsdl-ext/"+service.getSimpleName()+".wsdl"));
76        log.debug("created service: "+service);
77  
78        xfire.getServiceRegistry().register(service);
79        log.debug("registry: "+xfire.getServiceRegistry());
80  
81        // invoco il servizio
82        Document response = invokeService(service.getName().getLocalPart(), "/xmlmessages/testEchoMessage2.xml" );
83        printNode(response);
84  
85        addNamespace( "test", "urn:it.imolinfo.jbi4cics.test.webservices.utils.generators" );
86        assertValid( "//test:provaEchoOperationResponse", response );
87        assertValid( "string(test:provaEchoOperationResponse/out/string)='Hello World'", response );
88        log.info("response document: "+response);
89      } catch (Throwable e) {
90        log.error("errore in test testServiceCreator",e);
91        fail(e.getMessage());
92      }
93  
94    }
95  
96    public static class StringBean {
97      private String string;
98  
99      /**
100      *
101      */
102     public StringBean() {
103       // TODO Auto-generated constructor stub
104     }
105 
106     /**
107      * @return Returns the string.
108      */
109     public String getString() {
110       return string;
111     }
112 
113     /**
114      * @param string The string to set.
115      */
116     public void setString(String string) {
117       this.string = string;
118     }
119 
120   }
121 
122 }