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   import org.mule.transformer.types.MimeTypes;
10  
11  import org.ibeans.annotation.IntegrationBean;
12  import org.ibeans.api.CallException;
13  import org.junit.Test;
14  
15  import static org.junit.Assert.assertEquals;
16  import static org.junit.Assert.assertTrue;
17  import static org.junit.Assert.fail;
18  
19  public class ErrorFiltersTestCase extends AbstractIBeansTestCase
20  {
21      @SuppressWarnings("unused")
22      @IntegrationBean
23      private ErrorFilterIBean errorFilter;
24  
25  
26      @Test
27      public void testJsonFilter() throws Exception
28      {
29          //No error here
30          errorFilter.jsonErrorFilter("{\"message\" : \"hello\"}", MimeTypes.JSON);
31  
32          try
33          {
34              errorFilter.jsonErrorFilter("{\"message\" : {\"error\" : 1234, \"errorMsg\" : \"it didnt work dude\"}}", MimeTypes.JSON);
35              fail("Error should have been caught");
36          }
37          catch (CallException e)
38          {
39              //expected
40              assertEquals("1234", e.getErrorCode());
41              assertTrue(e.getMessage().contains("it didnt work dude"));
42          }
43  
44          try
45          {
46              errorFilter.jsonErrorFilter("{\"message\" : {\"errorMsg\" : \"it didnt work dude\"}}", MimeTypes.JSON);
47              fail("Error should have been caught");            
48          }
49          catch (CallException e)
50          {
51              //expected
52              assertTrue(e.getMessage().contains("it didnt work dude"));            
53          }
54      }
55  
56      @Test
57      public void testXmlFilter() throws Exception
58      {
59          //No error here
60          errorFilter.jsonErrorFilter("<message>hello</message>", MimeTypes.XML);
61  
62          try
63          {
64              errorFilter.jsonErrorFilter("<message><error>1234</error><errorMsg>it didnt work dude</errorMsg></message>", MimeTypes.XML);
65              fail("Error should have been caught");
66          }
67          catch (CallException e)
68          {
69              //expected
70              assertEquals("1234", e.getErrorCode());
71              assertTrue(e.getMessage().contains("it didnt work dude"));
72          }
73  
74          try
75          {
76              errorFilter.jsonErrorFilter("<message><errorMsg>it didnt work dude</errorMsg></message>", MimeTypes.XML);
77              fail("Error should have been caught");
78          }
79          catch (CallException e)
80          {
81              //expected
82              assertTrue(e.getMessage().contains("it didnt work dude"));
83          }
84      }
85  
86      @Test
87      public void testRegexFilter() throws Exception
88      {
89          //No error here
90          errorFilter.jsonErrorFilter("<message>hello</message>", MimeTypes.TEXT);
91  
92          try
93          {
94              errorFilter.jsonErrorFilter("<message><error>1234</error><errorMsg>it didnt work dude</errorMsg></message>", MimeTypes.TEXT);
95              fail("Error should have been caught");
96          }
97          catch (CallException e)
98          {
99              //expected
100             assertTrue(e.getMessage().contains("it didnt work dude"));
101         }
102 
103         try
104         {
105             errorFilter.jsonErrorFilter("<message><errorMsg>it didnt work dude</errorMsg></message>", MimeTypes.TEXT);
106             fail("Error should have been caught");
107         }
108         catch (CallException e)
109         {
110             //expected
111             assertTrue(e.getMessage().contains("it didnt work dude"));
112         }
113     }
114 
115 }