Coverage Report - org.mule.module.ibeans.spi.MuleMockCallAnnotationHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
MuleMockCallAnnotationHandler
0%
0/34
0%
0/12
0
 
 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  0
         super(muleContext);
 47  0
         this.mock = mock;
 48  0
         this.plugin = plugin;
 49  0
     }
 50  
 
 51  
     @Override
 52  
     public Response invoke(InvocationContext ctx) throws Exception
 53  
     {
 54  0
         this.ctx = ctx;
 55  0
         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  0
         if (method.getName().startsWith("ibean"))
 60  
         {
 61  0
             result = method.invoke(this, ctx.getArgs());
 62  
         }
 63  
         else
 64  
         {
 65  0
             result = ctx.getMethod().invoke(mock, ctx.getArgs());
 66  
         }
 67  
 
 68  0
         Map<String, Object> props = new HashMap<String, Object>();
 69  0
         props.put(HttpConstants.HEADER_CONTENT_TYPE, (ctx.getInvocationReturnType()==null ?
 70  
                     ctx.getReturnType().getMimeType() : ctx.getInvocationReturnType().getMimeType()));
 71  0
         Response response = (result instanceof Response ? (Response)result : plugin.createResponse(result, props, null));
 72  
 
 73  
         //Now handled in the test case
 74  0
         if (callback != null)
 75  
         {
 76  
             try
 77  
             {
 78  0
                 callback.onMessage(response);
 79  
             }
 80  
             finally
 81  
             {
 82  
                 //Only run it once
 83  0
                 callback = null;
 84  0
                 this.ctx = null;
 85  0
             }
 86  
 
 87  
         }
 88  0
         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  0
         return "http";
 95  
     }
 96  
 
 97  
     public void ibeanSetMimeType(MimeType mime) throws MimeTypeParseException
 98  
     {
 99  0
         if (ctx.getIBeanConfig().getReturnType() != null)
 100  
         {
 101  0
             ctx.setInvocationReturnType(DataTypeFactory.create(ctx.getIBeanConfig().getReturnType().getType(), mime));
 102  
         }
 103  0
     }
 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  0
             DataType type = ctx.getIBeanConfig().getReturnType();
 114  0
             if(type==null)
 115  
             {
 116  0
                 type = ctx.getIBeanDefaultConfig().getReturnType();
 117  
             }
 118  0
             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  0
             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  0
             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  0
             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  0
             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  0
             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  0
             return ctx.getIBeanConfig().getAttachments();
 191  
         }
 192  
     }
 193  
 
 194  
 
 195  
     public void ibeanSetMessageCallback(MockMessageCallback callback)
 196  
     {
 197  0
         this.callback = callback;
 198  0
     }
 199  
 
 200  
 }