1   /*
2    * $Id: BexeeProviderTest.java,v 1.1 2004/12/15 14:18:22 patforna Exp $
3    *
4    * Copyright (c) 2004 Patric Fornasier, Pawel Kowalski
5    * Berne University of Applied Sciences
6    * School of Engineering and Information Technology
7    * All rights reserved.
8    */
9   package bexee.axis;
10  
11  import java.io.File;
12  import java.io.FileOutputStream;
13  import java.io.PrintWriter;
14  import java.net.URL;
15  import java.util.Vector;
16  
17  import javax.xml.rpc.ParameterMode;
18  
19  import junit.framework.TestCase;
20  
21  import org.apache.axis.AxisFault;
22  import org.apache.axis.EngineConfiguration;
23  import org.apache.axis.Handler;
24  import org.apache.axis.client.Call;
25  import org.apache.axis.client.Service;
26  import org.apache.axis.configuration.XMLStringProvider;
27  import org.apache.axis.deployment.wsdd.WSDDService;
28  import org.apache.axis.encoding.XMLType;
29  import org.apache.axis.server.AxisServer;
30  import org.apache.axis.transport.local.LocalTransport;
31  
32  import bexee.core.BexeeMessage;
33  
34  /***
35   * @version $Revision: 1.1 $, $Date: 2004/12/15 14:18:22 $
36   * @author Patric Fornasier
37   */
38  public class BexeeProviderTest extends TestCase {
39  
40      // the web service deployment descriptor for axis
41      protected static final String DEPLOY_DOC = ""
42              + "<deployment xmlns=\"http://xml.apache.org/axis/wsdd/\" "
43              + "    xmlns:java=\"http://xml.apache.org/axis/wsdd/providers/java\""
44              + "    xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">"
45              + "  <service name=\"ServiceOne\" provider=\"java:BexeeProvider\">"
46              + "    <operation name=\"myOperation\" returnQName=\"value\" returnType=\"xsd:string\">"
47              + "      <parameter name=\"myParam\" type=\"xsd:string\" mode=\"IN\" />"
48              + "    </operation>"
49              + "  </service>"
50              + "  <service name=\"ServiceTwo\" provider=\"java:BexeeProvider\" xmlns:bexee=\"http://bexee.sf.net\">"
51              + "    <operation name=\"myOperation\" returnQName=\"value\" returnType=\"xsd:string\">"
52              + "      <parameter name=\"name\" type=\"xsd:string\" mode=\"IN\" />"
53              + "      <parameter name=\"street\" type=\"xsd:string\" mode=\"IN\" />"
54              + "      <parameter name=\"zip\" type=\"xsd:double\" mode=\"IN\" />"
55              + "      <parameter name=\"city\" type=\"xsd:string\" mode=\"IN\" />"
56              + "    </operation>"
57              + "  </service>"
58              + "  <service name=\"TravelProcess\" provider=\"java:BexeeProvider\">"
59              + "    <operation returnQName=\"ns1:value\" returnType=\"xsd:string\" name=\"initiate\" xmlns:ns1=\"http://bexee.sourceforge.net/Travel\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">"
60              + "      <parameter qname=\"ns1:value\" type=\"xsd:string\"/>"
61              + "    </operation>" + "  </service>" + "</deployment>";
62  
63      protected Call call;
64  
65      protected static BexeeMessage message;
66  
67      private LocalTransport transport;
68  
69      private File file;
70  
71      protected static String DUMMY_RESULT = "I'm just a dummy result :(";
72  
73      // TODO: MORE TESTING NEEDED
74  
75      protected void setUp() throws Exception {
76          super.setUp();
77  
78          /*
79           * this is a bit of a hack, but we need to get the discovery file into
80           * the classpath, so that we can use our own WSDDProvider
81           */
82          URL url = this.getClass().getClassLoader().getResource("");
83          File classpath = new File(url.getFile());
84  
85          // create the discovery folders if necessary and then the file
86          File folder = new File(classpath, "META-INF/services/");
87          if (!folder.exists()) {
88              folder.mkdirs();
89          }
90          file = new File(folder, "org.apache.axis.deployment.wsdd.Provider");
91          // insert our mock wsdd provider class name
92          PrintWriter pw = new PrintWriter(new FileOutputStream(file));
93          pw.println("bexee.axis.BexeeProviderTest$MockBexeeWSDDProvider");
94          pw.close();
95  
96          // create axis server and local transport
97          XMLStringProvider provider = new XMLStringProvider(DEPLOY_DOC);
98          AxisServer axis = new AxisServer(provider);
99          transport = new LocalTransport(axis);
100 
101         // create service call and set local transport
102         Service service = new Service();
103         call = (Call) service.createCall();
104         call.setTransport(transport);
105     }
106 
107     protected void tearDown() throws Exception {
108         super.tearDown();
109 
110         // try to delete temporary file on exiting VM
111         //file.deleteOnExit();
112     }
113 
114     /***
115      * Test if we can invoke a given service
116      */
117     public void testInvoke() throws Exception {
118         transport.setRemoteService("TravelProcess");
119 
120         call.setOperation("initiate");
121         call.addParameter("x", XMLType.XSD_STRING, ParameterMode.IN);
122         call.setReturnType(XMLType.XSD_STRING);
123 
124         String result = (String) call.invoke(new Object[] { "anything" });
125 
126         assertEquals(DUMMY_RESULT, result);
127     }
128 
129     /***
130      * Test if a call to a operation with too many parameters throws an
131      * exception.
132      */
133     public void testInvokeTooManyParameters() throws Exception {
134 
135         transport.setRemoteService("ServiceOne");
136 
137         call.setOperation("myOperation");
138         call.addParameter("value", XMLType.XSD_STRING, ParameterMode.IN);
139         call.addParameter("valueTwo", XMLType.XSD_STRING, ParameterMode.IN);
140         call.setReturnType(XMLType.XSD_STRING);
141 
142         try {
143             call.invoke(new Object[] { "blub", "blob" });
144             // if no exception is thrown, this test fails
145             fail("Call with too many params must throw an Exception");
146         } catch (AxisFault e) {
147             // if we get to here, the test succeeded
148         }
149     }
150 
151     /***
152      * Test if a call to a operation with too little parameters throws an
153      * exception.
154      */
155     public void testInvokeTooLittleParameters() throws Exception {
156 
157         transport.setRemoteService("ServiceOne");
158 
159         call.setOperation("myOperation");
160         call.setReturnType(XMLType.XSD_STRING);
161 
162         try {
163             call.invoke(new Object[] {});
164             // if no exception is thrown, this test fails
165             fail("Call with too little params must throw an Exception");
166         } catch (AxisFault e) {
167             // if we get to here, the test succeeded
168         }
169     }
170 
171     /***
172      * Test if a call to a non-existing operation results in an Exception.
173      */
174     public void testInvokeNonExistingOperation() throws Exception {
175 
176         transport.setRemoteService("ServiceOne");
177 
178         call.setOperation("nonExistingOperation");
179         call.addParameter("value", XMLType.XSD_STRING, ParameterMode.IN);
180         call.setReturnType(XMLType.XSD_STRING);
181 
182         try {
183             call.invoke(new Object[] { "blub" });
184             // if no exception is thrown, this test fails
185             fail("Call to a non-existing operation must throw an Exception");
186         } catch (AxisFault e) {
187             // if we get to here, the test succeeded
188         }
189     }
190 
191     /***
192      * Test if the <code>BexeeMessage</code> gets created properly from the
193      * input parameters.
194      */
195     public void testCreateBexeeMessage() throws Exception {
196 
197         transport.setRemoteService("ServiceTwo");
198 
199         // a rather useless call, but that's not the point here
200         call.setOperation("myOperation");
201         call.addParameter("name", XMLType.XSD_STRING, ParameterMode.IN);
202         call.addParameter("street", XMLType.XSD_STRING, ParameterMode.IN);
203         call.addParameter("zip", XMLType.XSD_DOUBLE, ParameterMode.IN);
204         call.addParameter("city", XMLType.XSD_STRING, ParameterMode.IN);
205         call.setReturnType(XMLType.XSD_STRING);
206 
207         call.invoke(new Object[] { "n", "s", new Double(90210), "c" });
208 
209         // compare fields
210         assertEquals("myOperation", message.getOperation());
211         assertEquals("ServiceTwo", message.getService());
212 
213         // compare part values
214         assertEquals("n", message.getPart("name"));
215         assertEquals("s", message.getPart("street"));
216         assertEquals(new Double(90210), message.getPart("zip"));
217         assertEquals("c", message.getPart("city"));
218     }
219 
220     /***
221      * Test if the <code>BexeeMessage</code> gets created properly from the
222      * input parameters, even if we use incorrect parameter names
223      */
224     public void testCreateBexeeMessageIncorrectParamNames() throws Exception {
225 
226         transport.setRemoteService("ServiceTwo");
227 
228         // a rather useless call, but that's not the point here
229         call.setOperation("myOperation");
230         call.addParameter("wr", XMLType.XSD_STRING, ParameterMode.IN);
231         call.addParameter("wro", XMLType.XSD_STRING, ParameterMode.IN);
232         call.addParameter("wron", XMLType.XSD_DOUBLE, ParameterMode.IN);
233         call.addParameter("wrong", XMLType.XSD_STRING, ParameterMode.IN);
234         call.setReturnType(XMLType.XSD_STRING);
235 
236         call.invoke(new Object[] { "n", "s", new Double(90210), "c" });
237 
238         // compare fields
239         assertEquals("myOperation", message.getOperation());
240         assertEquals("ServiceTwo", message.getService());
241 
242         // compare part values
243         assertEquals("n", message.getPart("name"));
244         assertEquals("s", message.getPart("street"));
245         assertEquals(new Double(90210), message.getPart("zip"));
246         assertEquals("c", message.getPart("city"));
247     }
248 
249     /***
250      * 
251      * @version $Revision: 1.1 $, $Date: 2004/12/15 14:18:22 $
252      * @author Patric Fornasier
253      * @author Pawel Kowalski
254      */
255     public static class MockBexeeWSDDProvider extends BexeeWSDDProvider {
256 
257         public Handler newProviderInstance(WSDDService service,
258                 EngineConfiguration registry) throws Exception {
259             return new MockBexeeProvider();
260         }
261     }
262 
263     /***
264      * 
265      * @version $Revision: 1.1 $, $Date: 2004/12/15 14:18:22 $
266      * @author Patric Fornasier
267      * @author Pawel Kowalski
268      */
269     private static class MockBexeeProvider extends BexeeProvider {
270 
271         /***
272          * Create a dummy result element
273          */
274         protected Object dispatch(BexeeMessage message) {
275 
276             return DUMMY_RESULT;
277         }
278 
279         /***
280          * Create message for testing class
281          */
282         protected BexeeMessage createBexeeMessage(String service,
283                 String operation, Vector params) throws AxisFault {
284 
285             // set message variable in testing class
286             message = super.createBexeeMessage(service, operation, params);
287             return message;
288         }
289 
290     }
291 }