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