1
2
3
4
5
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
34
35 return false;
36 }
37
38
39
40
41
42
43
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
84
85 if (responseCode == HttpConstants.SC_OK)
86 {
87 assertTrue("Stock quote should contain \"CISCO\": " + text, StringUtils.containsIgnoreCase(text, "CISCO"));
88
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
97 logger.warn("web service appears to be down again, so not failing the test");
98 }
99 }
100
101 }