View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.module.ibeans;
8   
9   import org.mule.api.MuleMessage;
10  import org.mule.api.client.MuleClient;
11  import org.mule.api.config.ConfigurationBuilder;
12  import org.mule.api.transport.DispatchException;
13  import org.mule.module.ibeans.config.IBeanHolderConfigurationBuilder;
14  import org.mule.module.xml.util.XMLUtils;
15  import org.mule.tck.junit4.FunctionalTestCase;
16  
17  import java.util.List;
18  
19  import org.junit.Test;
20  import org.w3c.dom.Document;
21  
22  import static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertNotNull;
24  import static org.junit.Assert.assertTrue;
25  import static org.junit.Assert.fail;
26  
27  public class IBeansHostIpFunctionalTestCase extends FunctionalTestCase
28  {
29  
30      public IBeansHostIpFunctionalTestCase()
31      {
32          setDisposeContextPerClass(true);
33      }
34  
35      @Override
36      protected String getConfigResources()
37      {
38          return "hostip-functional-test.xml";
39      }
40  
41      @Override
42      protected void addBuilders(List<ConfigurationBuilder> builders)
43      {
44          IBeanHolderConfigurationBuilder builder = new IBeanHolderConfigurationBuilder("org.mule");
45          builders.add(0, builder);
46      }
47  
48      @Test
49      public void testHostIp() throws Exception
50      {
51          if (isOffline(getClass().getName() + ".testHostIp"))
52          {
53              return;
54          }
55  
56          String ip = "192.215.42.198";
57          MuleClient client = muleContext.getClient();
58          MuleMessage response = client.send("vm://in", ip, null);
59          assertNotNull(response.getPayload());
60          Document result = response.getPayload(Document.class);
61          assertEquals(ip, XMLUtils.selectValue("//ip", result));
62          assertEquals("-117.136,32.8149", XMLUtils.selectValue("//gml:coordinates", result));
63      }
64  
65      @Test
66      public void testHostIpFromClient() throws Exception
67      {
68          if (isOffline(getClass().getName() + ".testHostIpFromClient"))
69          {
70              return;
71          }
72  
73          String ip = "192.215.42.198";
74          MuleClient client = muleContext.getClient();
75          MuleMessage response = client.send("ibean://hostip.getHostInfo", ip, null);
76          assertNotNull(response.getPayload());
77          Document result = response.getPayload(Document.class);
78          assertEquals(ip, XMLUtils.selectValue("//ip", result));
79          assertEquals("-117.136,32.8149", XMLUtils.selectValue("//gml:coordinates", result));
80      }
81  
82      @Test
83      public void testHostIpWrongNumberOfArguments() throws Exception
84      {
85          Object[] params = new Object[]{"192.215.42.198", new Integer(12)};
86          MuleClient client = muleContext.getClient();
87          MuleMessage response = client.send("vm://in", params, null);
88          assertNotNull(response.getExceptionPayload());
89          assertTrue(response.getExceptionPayload().getException() instanceof DispatchException);
90          assertTrue(response.getExceptionPayload().getRootException() instanceof NoSuchMethodException);
91      }
92  
93      @Test
94      public void testHostIpBadArgumentType() throws Exception
95      {
96          MuleClient client = muleContext.getClient();
97          MuleMessage response = client.send("vm://in", new StringBuffer(), null);
98          assertNotNull(response.getExceptionPayload());
99          assertTrue(response.getExceptionPayload().getException() instanceof DispatchException);
100         assertTrue(response.getExceptionPayload().getRootException() instanceof NoSuchMethodException);
101     }
102 
103     @Test
104     public void testHostIpWrongNumberOfArgumentsDirectClient() throws Exception
105     {
106         Object[] params = new Object[]{"192.215.42.198", new Integer(12)};
107 
108         try
109         {
110             muleContext.getClient().send("ibean://hostip.getHostInfo", params, null);
111             fail("Local call should throw exception since there will be no matching method called getHostInfo(String, Integer)");
112         }
113         catch (DispatchException e)
114         {
115             assertTrue(e.getCause() instanceof NoSuchMethodException);
116         }
117     }
118 
119     @Test
120     public void testHostIpBadArgumentTypeDirectClient() throws Exception
121     {
122         try
123         {
124             muleContext.getClient().send("ibean://hostip.getHostInfo", new StringBuffer(), null);
125             fail("Local call should throw exception since there will be no matching method called getHostInfo(StringBuffer)");
126         }
127         catch (DispatchException e)
128         {
129             assertTrue(e.getCause() instanceof NoSuchMethodException);
130         }
131     }
132 }