1
2
3
4
5
6
7
8
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
38
39 return false;
40 }
41
42
43
44
45
46
47
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
88
89 if (responseCode == HttpConstants.SC_OK)
90 {
91 assertTrue("Stock quote should contain \"CISCO\": " + text, StringUtils.containsIgnoreCase(text, "CISCO"));
92
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
101 logger.warn("web service appears to be down again, so not failing the test");
102 }
103 }
104
105 }