View Javadoc

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