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