View Javadoc

1   /*
2    * $Id: StockQuoteFunctionalTestCase.java 20057 2010-11-02 23:32:11Z dzapata $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.example.stockquote;
12  
13  import org.mule.tck.FunctionalTestCase;
14  import org.mule.tck.util.WebServiceOnlineCheck;
15  import org.mule.util.StringUtils;
16  
17  import java.util.Locale;
18  
19  import org.apache.commons.httpclient.HttpClient;
20  import org.apache.commons.httpclient.methods.GetMethod;
21  
22  
23  public class StockQuoteFunctionalTestCase extends FunctionalTestCase
24  {
25      public StockQuoteFunctionalTestCase()
26      {
27          // Do not fail test case upon timeout because this probably just means
28          // that the 3rd-party web service is off-line.
29          setFailOnTimeout(false);
30      }
31      
32      /**
33       * If a simple call to the web service indicates that it is not responding properly,
34       * we disable the test case so as to not report a test failure which has nothing to do
35       * with Mule.
36       *
37       * see EE-947
38       */
39      @Override
40      protected boolean isDisabledInThisEnvironment()
41      {
42          return (WebServiceOnlineCheck.isWebServiceOnline() == false);
43      }
44  
45      @Override
46      protected String getConfigResources()
47      {
48          return "mule-config.xml";
49      }
50  
51      public void testREST() throws Exception
52      {
53          runTest("REST");
54      }
55      
56      public void testSOAP() throws Exception
57      {
58          runTest("SOAP");
59      }
60  
61      public void testWSDL() throws Exception
62      {
63          runTest("WSDL");
64      }
65  
66      private void runTest(String method) throws Exception
67      {
68          String url = String.format("http://localhost:48309/stockquote?symbol=CSCO&method=%1s", method);
69          GetMethod request = new GetMethod(url);
70          int responseCode = new HttpClient().executeMethod(request);
71          
72          String text = request.getResponseBodyAsString();
73  
74          // FIXME : there is still a chance this test will fail when the webservice
75          // goes down in between tests
76          if(responseCode == 200) // we should have a proper quote to check if the return code is 200
77          {
78              assertTrue("Stock quote should contain \"CISCO\": " + text, StringUtils.containsIgnoreCase(text, "CISCO"));
79              //  the stockquote message is localized ...
80              if (Locale.getDefault().getISO3Language().equalsIgnoreCase("eng"))
81              {
82                  assertTrue("Stock quote should start with \"StockQuote[\":" + text, text.startsWith("StockQuote["));
83              }
84          }
85          // otherwise don't do anything else
86      }
87  }