View Javadoc

1   /*
2    * $Id: StockQuoteFunctionalTestCase.java 20438 2010-12-02 12:24:57Z dirk.olmes $
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.transport.http.HttpConstants;
16  import org.mule.util.StringUtils;
17  
18  import java.util.Locale;
19  
20  import org.apache.commons.httpclient.HttpClient;
21  import org.apache.commons.httpclient.methods.GetMethod;
22  
23  
24  public class StockQuoteFunctionalTestCase extends FunctionalTestCase
25  {
26      public StockQuoteFunctionalTestCase()
27      {
28          // Do not fail test case upon timeout because this probably just means
29          // that the 3rd-party web service is off-line.
30          setFailOnTimeout(false);
31      }
32      
33      /**
34       * If a simple call to the web service indicates that it is not responding properly,
35       * we disable the test case so as to not report a test failure which has nothing to do
36       * with Mule.
37       *
38       * see EE-947
39       */
40      @Override
41      protected boolean isDisabledInThisEnvironment()
42      {
43          return (WebServiceOnlineCheck.isWebServiceOnline() == false);
44      }
45  
46      @Override
47      protected String getConfigResources()
48      {
49          return "mule-config.xml";
50      }
51  
52      public void testREST() throws Exception
53      {
54          runTest("REST");
55      }
56      
57      public void testSOAP() throws Exception
58      {
59          runTest("SOAP");
60      }
61  
62      public void testWSDL() throws Exception
63      {
64          runTest("WSDL");
65      }
66  
67      private void runTest(String method) throws Exception
68      {
69          String url = String.format("http://localhost:48309/stockquote?symbol=CSCO&method=%1s", method);
70          GetMethod request = new GetMethod(url);
71          int responseCode = new HttpClient().executeMethod(request);
72          
73          String text = request.getResponseBodyAsString();
74  
75          // FIXME : there is still a chance this test will fail when the webservice
76          // goes down in between tests
77          if (responseCode == HttpConstants.SC_OK)
78          {
79              assertTrue("Stock quote should contain \"CISCO\": " + text, StringUtils.containsIgnoreCase(text, "CISCO"));
80              //  the stockquote message is localized ...
81              if (Locale.getDefault().getISO3Language().equalsIgnoreCase("eng"))
82              {
83                  assertTrue("Stock quote should start with \"StockQuote[\":" + text, text.startsWith("StockQuote["));
84              }
85          }
86          else
87          {
88              fail();
89          }
90      }
91  }