View Javadoc

1   /*
2    * $Id: LoanBrokerSyncTestCase.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.loanbroker;
12  
13  import org.mule.api.MuleMessage;
14  import org.mule.example.loanbroker.message.CustomerQuoteRequest;
15  import org.mule.example.loanbroker.model.Customer;
16  import org.mule.example.loanbroker.model.LoanQuote;
17  import org.mule.module.client.MuleClient;
18  import org.mule.tck.junit4.FunctionalTestCase;
19  import org.mule.transformer.simple.ByteArrayToObject;
20  import org.mule.transport.NullPayload;
21  import org.mule.transport.http.HttpConstants;
22  
23  import java.util.Map;
24  
25  import org.apache.commons.collections.map.SingletonMap;
26  import org.junit.Test;
27  
28  import static org.junit.Assert.assertFalse;
29  import static org.junit.Assert.assertNotNull;
30  import static org.junit.Assert.assertNull;
31  import static org.junit.Assert.assertTrue;
32  
33  public class LoanBrokerSyncTestCase extends FunctionalTestCase
34  {
35      
36      @Override
37      protected String getConfigResources()
38      {
39          return "mule-config.xml";
40      }
41  
42      @Test
43      public void testLoanBrokerMuleClient() throws Exception
44      {
45          muleContext.getRegistry().registerObject("streamToObjectTransformer", new ByteArrayToObject());
46          MuleClient client = new MuleClient(muleContext);
47          Customer c = new Customer("Ross Mason", 1234);
48          CustomerQuoteRequest request = new CustomerQuoteRequest(c, 100000, 48);
49          MuleMessage result = client.send("http://localhost:11080?responseTransformers=streamToObjectTransformer", request, null);
50          assertNotNull("Result is null", result);
51          assertFalse("Result is null", result.getPayload() instanceof NullPayload);
52          assertTrue("Result should be LoanQuote but is " + result.getPayload().getClass().getName(), 
53                      result.getPayload(Object.class) instanceof LoanQuote);
54          LoanQuote quote = (LoanQuote)result.getPayload();
55          assertTrue(quote.getInterestRate() > 0);
56      }
57  
58      @Test
59      public void testLoanBrokerHttpUrlWithDefaults() throws Exception
60      {
61          muleContext.getRegistry().registerObject("streamToObjectTransformer", new ByteArrayToObject());
62          MuleClient client = new MuleClient(muleContext);
63          // there are some default built into the http url service (note a different port)
64          @SuppressWarnings("unchecked")
65          Map<String, String> props = new SingletonMap("http.method", HttpConstants.METHOD_GET);
66          MuleMessage result = client.send("http://localhost:11081", null, props);
67          assertNotNull("Result is null", result);
68          assertFalse("Result is null", result.getPayload() instanceof NullPayload);
69          assertNull(result.getExceptionPayload());
70  
71          assertTrue("Unexpected response string", result.getPayloadAsString().matches("Bank #\\d, rate: \\d\\.(\\d)*$"));
72      }
73      
74  }
75  
76