View Javadoc

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