View Javadoc

1   /*
2    * $Id: RESTTestCase.java 23027 2011-09-26 13:19:28Z 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.transport.servlet;
12  
13  import org.mule.DefaultMuleMessage;
14  import org.mule.api.MuleMessage;
15  import org.mule.module.client.MuleClient;
16  import org.mule.tck.junit4.FunctionalTestCase;
17  import org.mule.tck.util.WebServiceOnlineCheck;
18  
19  import org.junit.Test;
20  
21  import static org.junit.Assert.assertNotNull;
22  import static org.junit.Assert.assertTrue;
23  
24  public class RESTTestCase extends FunctionalTestCase
25  {
26      @Override
27      protected String getConfigResources()
28      {
29          return "rest-functional-test.xml";
30      }
31  
32      @Override
33      protected boolean isFailOnTimeout()
34      {
35          // Do not fail test case upon timeout because this probably just means
36          // that the 3rd-party web service is off-line.
37          return false;
38      }
39  
40      /**
41       * If a simple call to the web service indicates that it is not responding properly,
42       * we disable the test case so as to not report a test failure which has nothing to do
43       * with Mule.
44       *
45       * see EE-947
46       */
47      @Override
48      protected boolean isDisabledInThisEnvironment()
49      {
50          return (WebServiceOnlineCheck.isWebServiceOnline() == false);
51      }
52  
53      @Test
54      public void testRest1ParamPost() throws Exception
55      {
56          MuleClient client = new MuleClient(muleContext);
57          MuleMessage reply  = client.send("vm://in1", new DefaultMuleMessage("IBM", muleContext));
58  
59          assertNotNull(reply);
60          assertNotNull(reply.getPayloadAsString());
61          assertTrue(reply.getPayloadAsString().indexOf("Symbol>IBM<") > -1);
62      }
63  
64      @Test
65      public void testRest2ParamsPost() throws Exception
66      {
67          MuleClient client = new MuleClient(muleContext);
68          MuleMessage reply  = client.send("vm://in2", new DefaultMuleMessage(new Object[]{"ARS","ARS"}, muleContext));
69  
70          assertNotNull(reply.getPayloadAsString());
71          assertTrue(reply.getPayloadAsString().indexOf(">0</double>") > -1);
72      }
73  
74      @Test
75      public void testRest1ParamGet() throws Exception
76      {
77          MuleClient client = new MuleClient(muleContext);
78          MuleMessage reply  = client.send("vm://in3", new DefaultMuleMessage(new Object[]{"IBM"}, muleContext));
79  
80          assertNotNull(reply);
81          String replyStr = reply.getPayloadAsString();
82          assertNotNull(replyStr);
83          assertTrue("'Symbol&gt;IBM&lt;' not found in reply: " + replyStr, replyStr.indexOf("Symbol&gt;IBM&lt;") > -1);
84      }
85  
86      @Test
87      public void testRest2ParamsGet() throws Exception
88      {
89          MuleClient client = new MuleClient(muleContext);
90          MuleMessage reply  = client.send("vm://in4", new DefaultMuleMessage(new Object[]{"ARS","ARS"}, muleContext));
91  
92          String replyStr = reply.getPayloadAsString();
93          System.out.println(replyStr);
94          assertTrue("'>0</double>' not found in reply: " + replyStr, replyStr.indexOf(">0</double>") > -1);
95      }
96  }