View Javadoc

1   /*
2    * $Id: MockIBeanTestCase.java 19026 2010-08-16 07:30:47Z 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  package org.mule.module.ibeans.annotations;
11  
12  
13  import org.mule.module.ibeans.HostIpIBean;
14  import org.mule.module.xml.util.XMLUtils;
15  
16  import org.ibeans.annotation.MockIntegrationBean;
17  import org.ibeans.api.CallException;
18  import org.junit.Test;
19  import org.w3c.dom.Document;
20  
21  import static org.mockito.Mockito.when;
22  
23  public class MockIBeanTestCase extends AbstractIBeansTestCase
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          try
57          {
58              //Because we are testing this in the core module we cannot import the xml module, so
59              //we set the return type to sting and define a RegEx error filter on the iBean
60              hostip.init(String.class);
61              when(hostip.getHostInfo(BAD_IP)).thenAnswer(withXmlData("mock/hostip-not-found-response.xml", hostip));
62  
63              hostip.getHostInfo(BAD_IP);
64              fail("The iBean should have recognised a Bad ip");
65          }
66          catch (CallException e)
67          {
68              //expected
69          }
70      }
71  
72      @Test
73      public void testTemplateMethod() throws Exception
74      {
75          String result = hostip.dummyTemplateMethod("three");
76          assertEquals("one two three", result);
77      }
78  }