View Javadoc

1   /*
2    * $Id:  $
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.api.annotations.param;
11  
12  import org.mule.DefaultMuleEvent;
13  import org.mule.DefaultMuleEventContext;
14  import org.mule.DefaultMuleMessage;
15  import org.mule.api.MuleEventContext;
16  import org.mule.api.MuleMessage;
17  import org.mule.api.config.MuleProperties;
18  import org.mule.api.model.EntryPointResolver;
19  import org.mule.api.model.InvocationResult;
20  import org.mule.impl.model.resolvers.AnnotatedEntryPointResolver;
21  import org.mule.session.DefaultMuleSession;
22  import org.mule.tck.AbstractMuleTestCase;
23  import org.mule.util.IOUtils;
24  import org.mule.util.StringDataSource;
25  
26  import java.io.IOException;
27  import java.io.InputStream;
28  import java.util.HashMap;
29  import java.util.Map;
30  
31  import javax.activation.DataHandler;
32  
33  public abstract class AbstractAnnotatedEntrypointResolverTestCase extends AbstractMuleTestCase
34  {
35      protected MuleEventContext eventContext;
36      protected boolean inboundScope = true;
37  
38      @Override
39      public void doSetUp() throws Exception
40      {
41          super.doSetUp();
42  
43          try
44          {
45              eventContext = createEventContext(null, null);
46          }
47          catch (Exception e)
48          {
49              fail(e.getMessage());
50          }
51      }
52  
53      protected MuleEventContext createEventContext(Map<String, Object> headers, Map<String, DataHandler> attachments) throws Exception
54      {
55          if(headers==null)
56          {
57              headers = new HashMap<String, Object>();
58              headers.put("foo", "fooValue");
59              headers.put("bar", "barValue");
60              headers.put("baz", "bazValue");
61          }
62  
63          if(attachments==null)
64          {
65              attachments = new HashMap<String, DataHandler>();
66              attachments.put("foo", new DataHandler(new StringDataSource("fooValue")));
67              attachments.put("bar", new DataHandler(new StringDataSource("barValue")));
68              attachments.put("baz", new DataHandler(new StringDataSource("bazValue")));
69          }
70          DefaultMuleMessage message;
71          if(inboundScope)
72          {
73              message = new DefaultMuleMessage("test", null, attachments, muleContext);
74              for (String s : headers.keySet())
75              {
76                  message.setInboundProperty(s, headers.get(s));
77              }
78          }
79          else
80          {
81              message = new DefaultMuleMessage("test", headers, muleContext);
82              for (String s : attachments.keySet())
83              {
84                  message.addOutboundAttachment(s, attachments.get(s));
85              }
86          }
87          return new DefaultMuleEventContext(new DefaultMuleEvent(message, getTestInboundEndpoint("null"), new DefaultMuleSession(muleContext)));
88      }
89  
90      protected InvocationResult invokeResolver(String methodName, MuleEventContext eventContext) throws Exception
91      {
92          EntryPointResolver resolver = getResolver();
93          eventContext.getMessage().setInvocationProperty(MuleProperties.MULE_METHOD_PROPERTY, methodName);
94          InvocationResult result = resolver.invoke(getComponent(), eventContext);
95          if (InvocationResult.State.SUCCESSFUL == result.getState())
96          {
97              assertNotNull("The result of invoking the component should not be null", result.getResult());
98              assertNull(result.getErrorMessage());
99              assertFalse(result.hasError());
100             assertEquals(methodName, result.getMethodCalled());
101         }
102         return result;
103     }
104 
105     protected EntryPointResolver getResolver() throws Exception
106     {
107         return createObject(AnnotatedEntryPointResolver.class);
108     }
109 
110     protected abstract Object getComponent();
111 
112     protected String readAttachment(DataHandler handler) throws IOException
113     {
114         return IOUtils.toString((InputStream) handler.getContent());
115     }
116 }