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.annotations;
8   
9   
10  import org.mule.module.ibeans.HostIpIBean;
11  import org.mule.module.xml.util.XMLUtils;
12  
13  import org.ibeans.annotation.MockIntegrationBean;
14  import org.ibeans.api.CallException;
15  import org.junit.Test;
16  import org.w3c.dom.Document;
17  
18  import static org.junit.Assert.assertEquals;
19  import static org.junit.Assert.fail;
20  import static org.mockito.Mockito.when;
21  
22  public class MockIBeanTestCase extends AbstractIBeansTestCase
23  {
24  
25      public static final String GOOD_IP = "12.215.42.19";
26      public static final String BAD_IP = "12.215.42.";
27  
28      @SuppressWarnings("unused")
29      @MockIntegrationBean
30      private HostIpIBean hostip;
31  
32      @Test
33      public void testSuccessfulHostipLookup() throws Exception
34      {
35          hostip.init(Document.class);
36          when(hostip.getHostInfo(GOOD_IP)).thenAnswer(withXmlData("mock/hostip-found-response.xml", hostip));
37  
38          Document result = hostip.getHostInfo(GOOD_IP);
39          String loc = XMLUtils.selectValue("//*[local-name()='coordinates']", result);
40          assertEquals("-88.4588,41.7696", loc);
41      }
42  
43      //    @Test
44      //    public void testSuccessfulHostipLookupWithReturn() throws Exception
45      //    {
46      //        hostip.init(Document.class);
47      //        when(hostip.hasIp(GOOD_IP)).thenAnswer(withXmlData("mock/hostip-found-response.xml", hostip));
48      //        when(hostip.hasIp(GOOD_IP)).thenAnswer(withXmlData("mock/hostip-found-response.xml", hostip));
49      //
50      //        assertTrue(hostip.hasIp(GOOD_IP));
51      //    }
52  
53      @Test(expected = CallException.class)
54      public void testUnsuccessfulHostipLookup() throws Exception
55      {
56          //Because we are testing this in the core module we cannot import the xml module, so
57          //we set the return type to sting and define a RegEx error filter on the iBean
58          hostip.init(String.class);
59          when(hostip.getHostInfo(BAD_IP)).thenAnswer(withXmlData("mock/hostip-not-found-response.xml", hostip));
60  
61          hostip.getHostInfo(BAD_IP);
62      }
63  
64      @Test
65      public void testTemplateMethod() throws Exception
66      {
67          String result = hostip.dummyTemplateMethod("three");
68          assertEquals("one two three", result);
69      }
70  }