1
2
3
4
5
6
7
8
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.junit4.AbstractMuleContextTestCase;
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 import static org.junit.Assert.assertEquals;
33 import static org.junit.Assert.assertFalse;
34 import static org.junit.Assert.assertNotNull;
35 import static org.junit.Assert.assertNull;
36 import static org.junit.Assert.fail;
37
38 public abstract class AbstractAnnotatedEntrypointResolverTestCase extends AbstractMuleContextTestCase
39 {
40 protected MuleEventContext eventContext;
41 protected boolean inboundScope = true;
42
43 @Override
44 public void doSetUp() throws Exception
45 {
46 super.doSetUp();
47
48 try
49 {
50 eventContext = createEventContext(null, null);
51 }
52 catch (Exception e)
53 {
54 fail(e.getMessage());
55 }
56 }
57
58 protected MuleEventContext createEventContext(Map<String, Object> headers, Map<String, DataHandler> attachments) throws Exception
59 {
60 if(headers==null)
61 {
62 headers = new HashMap<String, Object>();
63 headers.put("foo", "fooValue");
64 headers.put("bar", "barValue");
65 headers.put("baz", "bazValue");
66 }
67
68 if(attachments==null)
69 {
70 attachments = new HashMap<String, DataHandler>();
71 attachments.put("foo", new DataHandler(new StringDataSource("fooValue")));
72 attachments.put("bar", new DataHandler(new StringDataSource("barValue")));
73 attachments.put("baz", new DataHandler(new StringDataSource("bazValue")));
74 }
75 DefaultMuleMessage message;
76 if(inboundScope)
77 {
78 message = new DefaultMuleMessage("test", null, attachments, muleContext);
79 for (String s : headers.keySet())
80 {
81 message.setInboundProperty(s, headers.get(s));
82 }
83 }
84 else
85 {
86 message = new DefaultMuleMessage("test", headers, muleContext);
87 for (String s : attachments.keySet())
88 {
89 message.addOutboundAttachment(s, attachments.get(s));
90 }
91 }
92 return new DefaultMuleEventContext(new DefaultMuleEvent(message, getTestInboundEndpoint("null"), new DefaultMuleSession(muleContext)));
93 }
94
95 protected InvocationResult invokeResolver(String methodName, MuleEventContext eventContext) throws Exception
96 {
97 EntryPointResolver resolver = getResolver();
98 eventContext.getMessage().setInvocationProperty(MuleProperties.MULE_METHOD_PROPERTY, methodName);
99 InvocationResult result = resolver.invoke(getComponent(), eventContext);
100 if (InvocationResult.State.SUCCESSFUL == result.getState())
101 {
102 assertNotNull("The result of invoking the component should not be null", result.getResult());
103 assertNull(result.getErrorMessage());
104 assertFalse(result.hasError());
105 assertEquals(methodName, result.getMethodCalled());
106 }
107 return result;
108 }
109
110 protected EntryPointResolver getResolver() throws Exception
111 {
112 return createObject(AnnotatedEntryPointResolver.class);
113 }
114
115 protected abstract Object getComponent();
116
117 protected String readAttachment(DataHandler handler) throws IOException
118 {
119 return IOUtils.toString((InputStream) handler.getContent());
120 }
121 }