View Javadoc

1   /*
2    * $Id: LoanBrokerSyncTestCase.java 19577 2010-09-10 16:57:16Z aperepel $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.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  
27  public class LoanBrokerSyncTestCase extends FunctionalTestCase
28  {
29      
30      @Override
31      protected String getConfigResources()
32      {
33          return "mule-config.xml";
34      }
35  
36      public void testLoanBrokerMuleClient() throws Exception
37      {
38          muleContext.getRegistry().registerObject("streamToObjectTransformer", new ByteArrayToObject());
39          MuleClient client = new MuleClient(muleContext);
40          Customer c = new Customer("Ross Mason", 1234);
41          CustomerQuoteRequest request = new CustomerQuoteRequest(c, 100000, 48);
42          MuleMessage result = client.send("http://localhost:11080?responseTransformers=streamToObjectTransformer", request, null);
43          assertNotNull("Result is null", result);
44          assertFalse("Result is null", result.getPayload() instanceof NullPayload);
45          assertTrue("Result should be LoanQuote but is " + result.getPayload().getClass().getName(), 
46                      result.getPayload(Object.class) instanceof LoanQuote);
47          LoanQuote quote = (LoanQuote)result.getPayload();
48          assertTrue(quote.getInterestRate() > 0);
49      }
50  
51      public void testLoanBrokerHttpUrlWithDefaults() throws Exception
52      {
53          muleContext.getRegistry().registerObject("streamToObjectTransformer", new ByteArrayToObject());
54          MuleClient client = new MuleClient(muleContext);
55          // there are some default built into the http url service (note a different port)
56          @SuppressWarnings("unchecked")
57          Map<String, String> props = new SingletonMap("http.method", HttpConstants.METHOD_GET);
58          MuleMessage result = client.send("http://localhost:11081", null, props);
59          assertNotNull("Result is null", result);
60          assertFalse("Result is null", result.getPayload() instanceof NullPayload);
61          assertNull(result.getExceptionPayload());
62  
63          assertTrue("Unexpected response string", result.getPayloadAsString().matches("Bank #\\d, rate: \\d\\.(\\d)*$"));
64      }
65  
66  
67      
68  }
69  
70