1
2
3
4
5
6
7
8
9
10 package org.mule.module.ibeans.annotations;
11
12 import org.mule.api.config.ConfigurationBuilder;
13 import org.mule.api.config.MuleProperties;
14 import org.mule.api.transformer.Transformer;
15 import org.mule.api.transformer.TransformerException;
16 import org.mule.module.ibeans.config.IBeanHolderConfigurationBuilder;
17 import org.mule.module.ibeans.spi.MuleIBeansPlugin;
18 import org.mule.tck.junit4.AbstractMuleContextTestCase;
19 import org.mule.util.IOUtils;
20
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26
27 import org.ibeans.annotation.Return;
28 import org.ibeans.api.DataType;
29 import org.ibeans.api.Response;
30 import org.ibeans.api.channel.MimeType;
31 import org.ibeans.api.channel.MimeTypes;
32 import org.ibeans.impl.support.datatype.CollectionDataType;
33 import org.ibeans.impl.support.datatype.DataTypeFactory;
34 import org.ibeans.impl.test.MockIBean;
35 import org.ibeans.impl.test.MockMessageCallback;
36 import org.ibeans.spi.IBeansPlugin;
37 import org.mockito.invocation.InvocationOnMock;
38 import org.mockito.stubbing.Answer;
39
40 import static org.junit.Assert.assertNotNull;
41
42 public abstract class AbstractIBeansTestCase extends AbstractMuleContextTestCase
43 {
44 protected IBeansPlugin plugin;
45
46 protected AbstractIBeansTestCase()
47 {
48 setStartContext(true);
49 setDisposeContextPerClass(true);
50 }
51
52 protected IBeansPlugin createPlugin()
53 {
54 return new MuleIBeansPlugin(muleContext);
55 }
56
57 @Override
58 protected void doSetUp() throws Exception
59 {
60 plugin = createPlugin();
61
62 muleContext.getRegistry().registerObject("testcase", this);
63 }
64
65 @Override
66 protected void addBuilders(List<ConfigurationBuilder> builders)
67 {
68 IBeanHolderConfigurationBuilder builder = new IBeanHolderConfigurationBuilder("org.mule");
69 builders.add(builder);
70 }
71
72 protected Answer withXmlData(final String resource, final Object ibean)
73 {
74 return withData(resource, MimeTypes.XML, null, ibean);
75 }
76
77 protected Answer withRssData(final String resource, final Object ibean)
78 {
79 return withData(resource, MimeTypes.RSS, null, ibean);
80 }
81
82 protected Answer withAtomData(final String resource, final Object ibean)
83 {
84 return withData(resource, MimeTypes.ATOM, null, ibean);
85 }
86
87 protected Answer withJsonData(final String resource, final Object ibean)
88 {
89 return withData(resource, MimeTypes.JSON, null, ibean);
90 }
91
92 protected Answer withTextData(final String resource, final Object ibean)
93 {
94 return withData(resource, MimeTypes.TEXT, null, ibean);
95 }
96
97
98
99
100
101
102
103
104 protected Answer withData(final String resource, final Class returnType)
105 {
106 return new Answer()
107 {
108 public Object answer(InvocationOnMock
109 invocation) throws Throwable
110 {
111 return loadData(resource, DataTypeFactory.create(returnType));
112 }
113 };
114 }
115
116
117
118
119
120
121
122
123
124
125 protected Answer withData(final String resource, final MimeType mimeType, final MockMessageCallback callback, final Object ibean)
126 {
127 return new Answer()
128 {
129 public Object answer(InvocationOnMock invocation) throws Throwable
130 {
131 MimeType mime = mimeType;
132 DataType ret = ((MockIBean)ibean).ibeanReturnType();
133 if(ret!=null) ret.setMimeType(mime.toString());
134 Object data;
135
136
137
138
139
140
141
142 if (ret == null || invocation.getMethod().isAnnotationPresent(Return.class) ||
143 !invocation.getMethod().getReturnType().isAssignableFrom(ret.getType()))
144 {
145 ret = DataTypeFactory.createFromReturnType(invocation.getMethod());
146 mime = null;
147 }
148
149 data = loadData(resource, ret);
150 ((MockIBean)ibean).ibeanSetMimeType(mime);
151 ((MockIBean)ibean).ibeanSetMessageCallback(callback);
152
153 Response response;
154 Map<String, Object> headers = null;
155 if(mime!=null)
156 {
157 headers = new HashMap<String, Object>();
158 headers.put(MuleProperties.CONTENT_TYPE_PROPERTY, mime.toString());
159 }
160 response = plugin.createResponse(data, headers, null);
161 if(callback!=null)
162 {
163 callback.onMessage(response);
164 }
165 return response;
166 }
167 };
168 }
169
170 protected <T> T loadData(String resource, DataType<T> type) throws IOException, TransformerException
171 {
172 InputStream in = IOUtils.getResourceAsStream(resource, getClass());
173 assertNotNull("Resource stream for: " + resource + " must not be null", in);
174 return getDataAs(in, type);
175 }
176
177 protected <T> T getDataAs(InputStream data, DataType<T> as) throws TransformerException
178 {
179 org.mule.api.transformer.DataType muleDT;
180 if (as instanceof CollectionDataType)
181 {
182 muleDT = new org.mule.transformer.types.CollectionDataType(
183 as.getType(),
184 ((CollectionDataType) as).getItemType(),
185 as.getMimeType());
186 }
187 else
188 {
189 muleDT = new org.mule.transformer.types.SimpleDataType(
190 as.getType(),
191 as.getMimeType());
192 }
193 Transformer t = muleContext.getRegistry().lookupTransformer(org.mule.transformer.types.DataTypeFactory.create(data.getClass()), muleDT);
194 return (T) t.transform(data);
195 }
196
197 }