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