View Javadoc

1   /*
2    * $Id: MuleMockCallAnnotationHandler.java 19453 2010-09-08 15:07:22Z rossmason $
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.spi;
11  
12  import org.mule.api.MuleContext;
13  import org.mule.transport.http.HttpConstants;
14  
15  import java.lang.reflect.Method;
16  import java.util.HashMap;
17  import java.util.List;
18  import java.util.Map;
19  import java.util.Set;
20  
21  import javax.activation.DataSource;
22  import javax.activation.MimeTypeParseException;
23  
24  import org.ibeans.api.DataType;
25  import org.ibeans.api.InvocationContext;
26  import org.ibeans.api.Response;
27  import org.ibeans.api.channel.MimeType;
28  import org.ibeans.impl.support.datatype.DataTypeFactory;
29  import org.ibeans.impl.test.MockIBean;
30  import org.ibeans.impl.test.MockMessageCallback;
31  import org.ibeans.spi.IBeansPlugin;
32  
33  /**
34   * A handler for supporting the {@link org.ibeans.annotation.MockIntegrationBean} annotation. This implementation uses
35   * Mockito to mock out the call itself. Mockito can be used in tests to supply response data.
36   */
37  public class MuleMockCallAnnotationHandler extends MuleCallAnnotationHandler implements MockIBean
38  {
39      private Object mock;
40      private InvocationContext ctx;
41      private MockMessageCallback callback;
42      private IBeansPlugin plugin;
43  
44      public MuleMockCallAnnotationHandler(MuleContext muleContext, Object mock, IBeansPlugin plugin)
45      {
46          super(muleContext);
47          this.mock = mock;
48          this.plugin = plugin;
49      }
50  
51      @Override
52      public Response invoke(InvocationContext ctx) throws Exception
53      {
54          this.ctx = ctx;
55          Method method = ctx.getMethod();
56          //Special handling of methods with an ibean prefix, these are called by the the IBeansTestSupport
57          //To pass in additional information from the testcase
58          Object result;
59          if (method.getName().startsWith("ibean"))
60          {
61              result = method.invoke(this, ctx.getArgs());
62          }
63          else
64          {
65              result = ctx.getMethod().invoke(mock, ctx.getArgs());
66          }
67  
68          Map<String, Object> props = new HashMap<String, Object>();
69          props.put(HttpConstants.HEADER_CONTENT_TYPE, (ctx.getInvocationReturnType()==null ?
70                      ctx.getReturnType().getMimeType() : ctx.getInvocationReturnType().getMimeType()));
71          Response response = (result instanceof Response ? (Response)result : plugin.createResponse(result, props, null));
72  
73          //Now handled in the test case
74          if (callback != null)
75          {
76              try
77              {
78                  callback.onMessage(response);
79              }
80              finally
81              {
82                  //Only run it once
83                  callback = null;
84                  this.ctx = null;
85              }
86  
87          }
88          return response;
89      }
90  
91      public String getScheme(Method method)
92      {
93          //Default to http, should not make a difference for a Mock iBean
94          return "http";
95      }
96  
97      public void ibeanSetMimeType(MimeType mime) throws MimeTypeParseException
98      {
99          if (ctx.getIBeanConfig().getReturnType() != null)
100         {
101             ctx.setInvocationReturnType(DataTypeFactory.create(ctx.getIBeanConfig().getReturnType().getType(), mime));
102         }
103     }
104 
105     public DataType ibeanReturnType()
106     {
107 //        if (invocationContext == null || invocationContext.getReturnType().getType().getName().equals("void"))
108 //        {
109 //            return helper.getReturnType();
110 //        }
111 //        else
112         {
113             DataType type = ctx.getIBeanConfig().getReturnType();
114             if(type==null)
115             {
116                 type = ctx.getIBeanDefaultConfig().getReturnType();
117             }
118             return type;
119         }
120     }
121 
122     public Object ibeanUriParam(String name)
123     {
124 //        if (invocationContext == null)
125 //        {
126 //            return helper.getDefaultUriParams().get(name);
127 //        }
128 //        else
129         {
130             return ctx.getIBeanConfig().getUriParams().get(name);
131         }
132     }
133 
134     public Object ibeanHeaderParam(String name)
135     {
136 //        if (invocationContext == null)
137 //        {
138 //            return helper.getDefaultHeaderParams().get(name);
139 //        }
140 //        else
141         {
142             return ctx.getIBeanConfig().getHeaderParams().get(name);
143         }
144     }
145 
146     public Object ibeanPropertyParam(String name)
147     {
148 //        if (invocationContext == null)
149 //        {
150 //            return helper.getDefaultPropertyParams().get(name);
151 //        }
152 //        else
153         {
154             return ctx.getIBeanConfig().getPropertyParams().get(name);
155         }
156     }
157 
158     public Object ibeanPayloadParam(String name)
159     {
160 //        if (invocationContext == null)
161 //        {
162 //            return helper.getDefaultPayloadParams().get(name);
163 //        }
164 //        else
165         {
166             return ctx.getIBeanConfig().getPayloadParams().get(name);
167         }
168     }
169 
170     public List<Object> ibeanPayloads()
171     {
172 //        if (invocationContext == null)
173 //        {
174 //            return null;
175 //        }
176 //        else
177         {
178             return ctx.getIBeanConfig().getPayloads();
179         }
180     }
181 
182     public Set<DataSource> ibeanAttachments()
183     {
184 //        if (invocationContext == null)
185 //        {
186 //            return null;
187 //        }
188 //        else
189         {
190             return ctx.getIBeanConfig().getAttachments();
191         }
192     }
193 
194 
195     public void ibeanSetMessageCallback(MockMessageCallback callback)
196     {
197         this.callback = callback;
198     }
199 
200 }