1   /*$Id: HttpApplicationProviderTest.java,v 1.10 2004/11/27 13:20:02 pah Exp $
2    * Created on 30-Jul-2004
3    *
4    * Copyright (C) AstroGrid. All rights reserved.
5    *
6    * This software is published under the terms of the AstroGrid 
7    * Software License version 1.2, a copy of which has been included 
8    * with this distribution in the LICENSE.txt file.  
9    *
10  **/
11  package org.astrogrid.applications.http;
12  
13  import java.net.URL;
14  
15  import junit.framework.TestCase;
16  
17  import org.apache.commons.logging.Log;
18  import org.apache.commons.logging.LogFactory;
19  import org.astrogrid.applications.Application;
20  import org.astrogrid.applications.MockMonitor;
21  import org.astrogrid.applications.Status;
22  import org.astrogrid.applications.beans.v1.cea.castor.ResultListType;
23  import org.astrogrid.applications.beans.v1.parameters.ParameterValue;
24  import org.astrogrid.applications.description.ApplicationDescription;
25  import org.astrogrid.applications.description.ApplicationDescriptionLibrary;
26  import org.astrogrid.applications.description.ApplicationInterface;
27  import org.astrogrid.applications.description.base.ApplicationDescriptionEnvironment;
28  import org.astrogrid.applications.description.exception.ApplicationDescriptionNotFoundException;
29  import org.astrogrid.applications.http.registry.RegistryQuerier;
30  import org.astrogrid.applications.http.test.FileUnmarshaller;
31  import org.astrogrid.applications.http.test.TestRegistryQuerier;
32  import org.astrogrid.applications.http.test.TestWebServer;
33  import org.astrogrid.applications.manager.idgen.IdGen;
34  import org.astrogrid.applications.manager.idgen.InMemoryIdGen;
35  import org.astrogrid.applications.parameter.protocol.DefaultProtocolLibrary;
36  import org.astrogrid.applications.parameter.protocol.FileProtocol;
37  import org.astrogrid.community.User;
38  import org.astrogrid.workflow.beans.v1.Tool;
39  
40  /*** 
41   * Test of the HttpApplication backend to CEA
42   * @author JDT/Noel Winstanley nw@jb.man.ac.uk 08-Jun-2004
43   *
44   */
45  public class HttpApplicationProviderTest extends TestCase {
46      /***
47       * Logger for this class
48       */
49      private static final Log log = LogFactory.getLog(HttpApplicationProviderTest.class);
50  
51      private static final String COMMUNITY = "org.astrogrid.test";
52      private static final int PORT = 8078;
53      /***
54       * Constructor 
55       * @param arg0
56       */
57      public HttpApplicationProviderTest(String arg0) {
58          super(arg0);
59      }
60      /*
61       * @see TestCase#setUp()
62       */
63      protected void setUp() throws Exception {
64          super.setUp();
65          //Fire up an in process server
66          TestWebServer.getServer(PORT);
67          IdGen idgen = new InMemoryIdGen();
68          DefaultProtocolLibrary protocolLib = new DefaultProtocolLibrary();
69          protocolLib.addProtocol(new FileProtocol()); //this is done by the component manager normally
70          monitor = new MockMonitor();
71          HttpApplicationDescriptionLibrary.AppAuthorityIDResolver aresolver = new TestAuthority();
72          ApplicationDescriptionEnvironment env = new ApplicationDescriptionEnvironment(idgen,protocolLib, aresolver);
73          RegistryQuerier querier = new TestRegistryQuerier();
74          numberOfApps = querier.getHttpApplications().size();
75          applicationDescriptionLibrary = new HttpApplicationDescriptionLibrary(querier, env);
76          assertNotNull(applicationDescriptionLibrary);
77      }
78      protected ApplicationDescriptionLibrary applicationDescriptionLibrary;
79      protected MockMonitor monitor;
80      
81      protected final User user = new User();
82      private int numberOfApps;
83      private FileUnmarshaller toolUnmarshaller = new FileUnmarshaller(Tool.class);
84      public void testLibrary() throws Exception {
85          String[] names = applicationDescriptionLibrary.getApplicationNames();
86          assertNotNull(names);
87          assertEquals(numberOfApps,names.length);        
88      }
89      
90      public void testNoDescriptionFound() {
91          try {
92              ApplicationDescription hw = applicationDescriptionLibrary.getDescription(COMMUNITY+"/dadsfadgadfgadgd");
93          } catch (ApplicationDescriptionNotFoundException e) {
94              return; //expected
95          }
96          fail("Expected an ApplicationDescriptionNotFoundException");
97      }
98   
99      public void testHelloWorld() throws Exception {
100         if (log.isTraceEnabled()) {
101             log.trace("testHelloWorld() - start");
102         }
103 
104         ApplicationDescription hw = getApplicationDescription("/HelloWorld");
105         
106         Tool tool  = (Tool) toolUnmarshaller.unmarshallFromFile("tool-helloWorld1.xml");
107         
108         Application app = hw.initializeApplication("testrun",user,tool);
109         assertNotNull(app);
110         app.addObserver(monitor);
111         app.execute();
112         monitor.waitFor(10);
113         assertTrue(monitor.sawExit);
114         ResultListType results= app.getResult();
115         assertNotNull(results);
116         assertEquals(1,results.getResultCount());
117         String resultName=results.getResult(0).getName();
118         assertEquals("Result", resultName);
119         
120         String resultVal = results.getResult(0).getValue();
121         assertNotNull(resultVal);
122         log.debug("testHelloWorld()" + resultVal);
123         assertEquals("HelloWorld",resultVal);
124 
125         if (log.isTraceEnabled()) {
126             log.trace("testHelloWorld() - end");
127         }
128     }
129     
130     /***
131      * get an application library and run a few basic checks
132      * @param appName
133      * @return
134      * @throws ApplicationDescriptionNotFoundException
135      */
136     private ApplicationDescription getApplicationDescription(final String appName) throws ApplicationDescriptionNotFoundException {
137         ApplicationDescription hw = applicationDescriptionLibrary.getDescription(COMMUNITY+appName);
138         assertNotNull(hw);
139         assertEquals(COMMUNITY+appName,hw.getName());
140         ApplicationInterface iface = hw.getInterfaces()[0];
141         assertNotNull(iface);
142         return hw;
143     }
144     public void testHelloYou() throws Exception {
145         ApplicationDescription hw = getApplicationDescription("/HelloYou");
146         
147         Tool tool  = (Tool) toolUnmarshaller.unmarshallFromFile("tool-helloYou1.xml");
148         
149         Application app = hw.initializeApplication("testrun",user,tool);
150         assertNotNull(app);
151         app.addObserver(monitor);
152         app.execute();
153         monitor.waitFor(10);
154         assertTrue(monitor.sawExit);
155         ResultListType results= app.getResult();
156         assertNotNull(results);
157         assertEquals(1,results.getResultCount());
158         String resultName=results.getResult(0).getName();
159         assertEquals("result", resultName);
160         
161         String resultVal = results.getResult(0).getValue();
162         assertNotNull(resultVal);
163         log.debug("testHelloYou()" + resultVal);
164         assertEquals("Hello Old King Cole was a Merry Old Soul",resultVal);
165     }
166     
167     public void testEcho1() throws Exception {
168         ApplicationDescription hw = getApplicationDescription("/Echoer");
169        
170         Tool tool  = (Tool) toolUnmarshaller.unmarshallFromFile("tool-echo1.xml");
171         
172         Application app = hw.initializeApplication("testrun",user,tool);
173         assertNotNull(app);
174         app.addObserver(monitor);
175         app.execute();
176         monitor.waitFor(10);
177         assertTrue(monitor.sawExit);
178         ResultListType results= app.getResult();
179         assertNotNull(results);
180         assertEquals(1,results.getResultCount());
181         String resultName=results.getResult(0).getName();
182         assertEquals("Reply", resultName);
183         
184         String resultVal = results.getResult(0).getValue();
185         assertNotNull(resultVal);
186         log.debug("testEcho1()" + resultVal);
187         assertEquals("{x=kerry4potus}",resultVal);
188     }
189     
190     public void testEcho2() throws Exception {
191         ApplicationDescription hw = getApplicationDescription("/Echoer");
192         
193         Tool tool  = (Tool) toolUnmarshaller.unmarshallFromFile("tool-echo2.xml");
194         
195         Application app = hw.initializeApplication("testrun",user,tool);
196         assertNotNull(app);
197         app.addObserver(monitor);
198         app.execute();
199         monitor.waitFor(10);
200         assertTrue(monitor.sawExit);
201         ResultListType results= app.getResult();
202         assertNotNull(results);
203         assertEquals(1,results.getResultCount());
204         String resultName=results.getResult(0).getName();
205         assertEquals("Reply", resultName);
206         
207         String resultVal = results.getResult(0).getValue();
208         assertNotNull(resultVal);
209         log.debug("testEcho2()" + resultVal);
210         assertEquals("{y=bush4theHague}",resultVal); 
211     }
212     
213     public void testAdd() throws Exception {
214         ApplicationDescription hw = getApplicationDescription("/Adder");
215         
216         Tool tool  = (Tool) toolUnmarshaller.unmarshallFromFile("tool-Adder1.xml");
217         
218         Application app = hw.initializeApplication("testrun",user,tool);
219         assertNotNull(app);
220         app.addObserver(monitor);
221         app.execute();
222         monitor.waitFor(10);
223         assertTrue(monitor.sawExit);
224         ResultListType results= app.getResult();
225         assertNotNull(results);
226         assertEquals(1,results.getResultCount());
227         String resultName=results.getResult(0).getName();
228         assertEquals("sum", resultName);
229         
230         String resultVal = results.getResult(0).getValue();
231         assertNotNull(resultVal);
232         log.debug("testAdd()" + resultVal);
233         assertEquals("8",resultVal);
234     }
235     
236     /***
237      * As testAdd, but using a file:// param
238      * @throws Exception
239      */
240     public void testAddIndirectParameter() throws Exception {
241         ApplicationDescription hw = getApplicationDescription("/Adder");
242         
243         
244         
245         Tool tool  = (Tool) toolUnmarshaller.unmarshallFromFile("tool-Adder1.xml");
246         // Replace a parameter by an indirect value.  Can't do this in the xml file
247         // since we don't know where your local tmp directory is going to be.
248         URL url_x = this.getClass().getResource("test/xinput.txt");
249         log.debug("Located x input file at "+url_x);
250         ParameterValue pval_x = (ParameterValue)tool.findXPathValue("input/parameter[name='x']");
251         log.debug("Changing x value in tools document from "+pval_x.getValue());
252         pval_x.setValue(url_x.toString());
253         log.debug("to " + pval_x.getValue());
254         pval_x.setIndirect(true);
255         Application app = hw.initializeApplication("testrun",user,tool);
256         assertNotNull(app);
257         app.addObserver(monitor);
258         app.execute();
259         monitor.waitFor(10);
260         assertTrue(monitor.sawExit);
261         ResultListType results= app.getResult();
262         assertNotNull(results);
263         assertEquals(1,results.getResultCount());
264         String resultName=results.getResult(0).getName();
265         assertEquals("sum", resultName);
266         
267         String resultVal = results.getResult(0).getValue();
268         assertNotNull(resultVal);
269         log.debug("testAdd()" + resultVal);
270         assertEquals("8",resultVal);
271     }
272     
273     /***
274      * Post isn't supported yet
275      * @throws Exception
276      */
277     public void testPost() throws Exception {
278     		fail("This test is not ready yet, since the in-process webserver doesn't seem very happy with post");
279             ApplicationDescription hw = getApplicationDescription("/AdderPost");
280             
281             Tool tool  = (Tool) toolUnmarshaller.unmarshallFromFile("tool-Adder1.xml");
282             
283             Application app = hw.initializeApplication("testrun",user,tool);
284             assertNotNull(app);
285             app.addObserver(monitor);
286             app.execute();
287             monitor.waitFor(30);
288             assertTrue(monitor.sawExit);
289     }
290     /***
291      * What happens if the URL returns a 404?
292      * @throws Exception
293      */
294     public void test404() throws Exception {
295             ApplicationDescription hw = getApplicationDescription("/Bad");
296             
297             Tool tool  = (Tool) toolUnmarshaller.unmarshallFromFile("tool-Adder1.xml");
298             
299             Application app = hw.initializeApplication("testrun",user,tool);
300             assertNotNull(app);
301             app.addObserver(monitor);
302             app.execute();
303             monitor.waitFor(10);
304             assertTrue(monitor.sawError);
305             Status status = app.getStatus();
306             assert(status.equals(Status.ERROR));
307     }
308     
309     /***
310      * What happens if the URL timesout?
311      * @throws Exception
312      */
313     public void testTimeOut() throws Exception {
314             ApplicationDescription hw = getApplicationDescription("/Badder");
315             
316             Tool tool  = (Tool) toolUnmarshaller.unmarshallFromFile("tool-Adder1.xml");
317             
318             Application app = hw.initializeApplication("testrun",user,tool);
319             assertNotNull(app);
320             app.addObserver(monitor);
321             app.execute();
322             monitor.waitFor(10);
323             assertTrue(monitor.sawError);
324             Status status = app.getStatus();
325             assert(status.equals(Status.ERROR));
326     }
327     
328     /***
329      * What happens if the URL is malformed?
330      * @throws Exception
331      */
332     public void testMalformedURL() throws Exception {
333             ApplicationDescription hw = getApplicationDescription("/Baddest");
334             
335             Tool tool  = (Tool) toolUnmarshaller.unmarshallFromFile("tool-Adder1.xml");
336             
337             Application app = hw.initializeApplication("testrun",user,tool);
338             assertNotNull(app);
339             app.addObserver(monitor);
340             app.execute();
341             monitor.waitFor(10);
342             assertTrue(monitor.sawError);
343             Status status = app.getStatus();
344             assert(status.equals(Status.ERROR));
345     }
346     
347     /***
348      * Apps containing preprocessing scripts should fail at the moment.
349      * @throws Exception
350      * @todo NWW: this isn't observable anymore. 
351      */
352     public void testPreProcess() throws Exception {
353 
354             ApplicationDescription hw = getApplicationDescription("/AdderPreProcess");
355             
356             Tool tool  = (Tool) toolUnmarshaller.unmarshallFromFile("tool-Adder1.xml");
357             
358             Application app = hw.initializeApplication("testrun",user,tool);
359             assertNotNull(app);
360             app.addObserver(monitor);
361             app.execute();
362             monitor.waitFor(10);
363             //Should fail internally with an UnsupportedOperationException
364             assertTrue(monitor.sawError);
365     }
366     
367     //@TODO what if the registry entry is garbage?
368 
369 }
370 
371 
372 /* 
373 $Log: HttpApplicationProviderTest.java,v $
374 Revision 1.10  2004/11/27 13:20:02  pah
375 result of merge of pah_cea_bz561 branch
376 
377 Revision 1.9  2004/11/08 17:59:21  jdt
378 Fixed a unit test.
379 
380 Revision 1.8  2004/09/26 23:34:47  jdt
381 Added a unit test that checks that indirect input params are handled right.
382 
383 Revision 1.7  2004/09/17 01:23:09  nw
384 updated tests
385 
386 Revision 1.6  2004/09/14 16:26:26  jdt
387 Attempt to get the http-post working.  Upgraded http-client, to no avail.  Either http client
388 or the embedded test webserver isn't handling post correctly.  Flagged tests
389 to ensure this is dealt with.
390 
391 Revision 1.5  2004/09/01 16:17:07  jdt
392 fixed unit test - file name was wrong case you great pillock.
393 
394 Revision 1.4  2004/09/01 15:42:26  jdt
395 Merged in Case 3
396 
397 Revision 1.1.2.8  2004/08/15 10:58:19  jdt
398 more test updates
399 
400 Revision 1.1.2.7  2004/08/12 13:16:58  jdt
401 fixed some tests
402 
403 Revision 1.1.2.6  2004/08/11 22:55:35  jdt
404 Refactoring, and a lot of new unit tests.
405 
406 Revision 1.1.2.5  2004/08/06 13:31:52  jdt
407 Registry interface stuff
408 
409 Revision 1.1.2.4  2004/08/02 18:05:28  jdt
410 Added more tests and refactored the test apps to be set up
411 from xml.
412 
413 Revision 1.1.2.3  2004/07/30 17:03:27  jdt
414 renamed the test web server class
415 
416 Revision 1.1.2.2  2004/07/30 16:59:50  jdt
417 limping along.
418 
419 Revision 1.1.2.1  2004/07/30 11:02:30  jdt
420 Added unit tests, refactored the RegistryQuerier anf finished off
421 HttpApplicationCEAComponentManager.
422 
423 
424  
425 */