1 | package gov.va.med.edp.vistalink;
|
---|
2 |
|
---|
3 | import gov.va.med.exception.FoundationsException;
|
---|
4 | import gov.va.med.vistalink.adapter.cci.VistaLinkConnection;
|
---|
5 | import gov.va.med.vistalink.adapter.cci.VistaLinkAppProxyConnectionSpec;
|
---|
6 | import gov.va.med.vistalink.adapter.cci.VistaLinkDuzConnectionSpec;
|
---|
7 | import gov.va.med.vistalink.rpc.*;
|
---|
8 | import junit.framework.TestCase;
|
---|
9 | import org.easymock.AbstractMatcher;
|
---|
10 | import org.easymock.ArgumentsMatcher;
|
---|
11 | import org.easymock.MockControl;
|
---|
12 | import org.springframework.util.FileCopyUtils;
|
---|
13 |
|
---|
14 | import javax.resource.cci.ConnectionFactory;
|
---|
15 | import javax.resource.ResourceException;
|
---|
16 | import java.util.List;
|
---|
17 | import java.util.ArrayList;
|
---|
18 | import java.io.IOException;
|
---|
19 | import java.io.InputStreamReader;
|
---|
20 | import java.io.StringWriter;
|
---|
21 |
|
---|
22 | public abstract class AbstractVistaLinkConnectionTest extends TestCase {
|
---|
23 |
|
---|
24 | protected MockControl mockConnectionFactoryControl;
|
---|
25 | protected ConnectionFactory mockConnectionFactory;
|
---|
26 | protected MockControl mockVistaLinkConnectionControl;
|
---|
27 | protected VistaLinkConnection mockVistaLinkConnection;
|
---|
28 | protected MockConnectionFactoryLocator mockConnectionFactoryLocator;
|
---|
29 |
|
---|
30 | static ArgumentsMatcher RPC_REQUEST_MATCHER = new RpcRequestMatcher();
|
---|
31 |
|
---|
32 | private int expectedTimeOut = VistaLinkTemplate.DEFAULT_TIMEOUT;
|
---|
33 |
|
---|
34 | protected String getResourceAsString(String resource) throws IOException {
|
---|
35 | StringWriter w = new StringWriter();
|
---|
36 | InputStreamReader r = new InputStreamReader(getClass().getResourceAsStream(resource));
|
---|
37 | FileCopyUtils.copy(r, w);
|
---|
38 | return w.toString();
|
---|
39 | }
|
---|
40 |
|
---|
41 | protected void setUp() throws Exception {
|
---|
42 | mockConnectionFactoryControl = MockControl.createControl(ConnectionFactory.class);
|
---|
43 | mockConnectionFactory = (ConnectionFactory) mockConnectionFactoryControl.getMock();
|
---|
44 |
|
---|
45 | mockVistaLinkConnectionControl = MockControl.createControl(VistaLinkConnection.class);
|
---|
46 | mockVistaLinkConnection = (VistaLinkConnection) mockVistaLinkConnectionControl.getMock();
|
---|
47 |
|
---|
48 | mockVistaLinkConnection.setTimeOut(getExpectedTimeOut());
|
---|
49 | mockVistaLinkConnectionControl.setDefaultVoidCallable();
|
---|
50 |
|
---|
51 | mockConnectionFactoryLocator = new MockConnectionFactoryLocator();
|
---|
52 | mockConnectionFactoryLocator.put(getStationNumber(), mockConnectionFactory);
|
---|
53 | }
|
---|
54 |
|
---|
55 | protected int getExpectedTimeOut() {
|
---|
56 | return expectedTimeOut;
|
---|
57 | }
|
---|
58 |
|
---|
59 | protected void setExpectedTimeOut(int expectedTimeOut) {
|
---|
60 | this.expectedTimeOut = expectedTimeOut;
|
---|
61 | }
|
---|
62 |
|
---|
63 |
|
---|
64 | protected abstract String getStationNumber();
|
---|
65 |
|
---|
66 | protected void expectVistaLinkDuzConnection(String duz) {
|
---|
67 | try {
|
---|
68 | mockConnectionFactoryControl.expectAndDefaultReturn(mockConnectionFactory.getConnection(new VistaLinkDuzConnectionSpec(getStationNumber(), duz)), mockVistaLinkConnection);
|
---|
69 | } catch (ResourceException e) {
|
---|
70 | fail("unexpected exception: " + e.getMessage());
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | protected void expectVistaLinkAppProxyConnection(String appProxyName) {
|
---|
75 | try {
|
---|
76 | mockConnectionFactoryControl.expectAndDefaultReturn(mockConnectionFactory.getConnection(new VistaLinkAppProxyConnectionSpec(getStationNumber(), appProxyName)), mockVistaLinkConnection);
|
---|
77 | } catch (ResourceException e) {
|
---|
78 | fail("unexpected exception: " + e.getMessage());
|
---|
79 | }
|
---|
80 | }
|
---|
81 |
|
---|
82 | protected void expectRpcAndReturn(String rpcContext, String rpc, List params, String response) {
|
---|
83 | try {
|
---|
84 | RpcRequest request = RpcRequestFactory.getRpcRequest(rpcContext, rpc);
|
---|
85 | if (params != null) request.setParams(params);
|
---|
86 | expectRpcAndReturn(request, new TestRpcResponse(response));
|
---|
87 | } catch (FoundationsException e) {
|
---|
88 | throw new IllegalArgumentException(e.toString());
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
92 | protected void expectRpcAndReturn(RpcRequest request, RpcResponse response) throws FoundationsException {
|
---|
93 | mockVistaLinkConnection.setTimeOut(getExpectedTimeOut());
|
---|
94 | mockVistaLinkConnection.executeRPC(request);
|
---|
95 | mockVistaLinkConnectionControl.setMatcher(RPC_REQUEST_MATCHER);
|
---|
96 | mockVistaLinkConnectionControl.setReturnValue(response);
|
---|
97 | }
|
---|
98 |
|
---|
99 | protected void expectRpcAndReturn(String rpcContext, String rpc, String response) {
|
---|
100 | expectRpcAndReturn(rpcContext, rpc, null, response);
|
---|
101 | }
|
---|
102 |
|
---|
103 | protected void expectRpcAndDefaultThrow(String rpcContext, String rpc, List params, Throwable t) {
|
---|
104 | mockVistaLinkConnection.setTimeOut(getExpectedTimeOut());
|
---|
105 | try {
|
---|
106 | RpcRequest request = RpcRequestFactory.getRpcRequest(rpcContext, rpc);
|
---|
107 | if (params != null) request.setParams(params);
|
---|
108 | mockVistaLinkConnection.executeRPC(request);
|
---|
109 | mockVistaLinkConnectionControl.setMatcher(RPC_REQUEST_MATCHER);
|
---|
110 | mockVistaLinkConnectionControl.setDefaultThrowable(t);
|
---|
111 | } catch (FoundationsException e) {
|
---|
112 | throw new IllegalArgumentException(e.toString());
|
---|
113 | }
|
---|
114 | }
|
---|
115 |
|
---|
116 |
|
---|
117 | protected void expectRpcAndDefaultThrow(String rpcContext, String rpc, Throwable t) {
|
---|
118 | try {
|
---|
119 | mockVistaLinkConnection.executeRPC(RpcRequestFactory.getRpcRequest(rpcContext, rpc));
|
---|
120 | mockVistaLinkConnectionControl.setMatcher(RPC_REQUEST_MATCHER);
|
---|
121 | mockVistaLinkConnectionControl.setDefaultThrowable(t);
|
---|
122 | } catch (FoundationsException e) {
|
---|
123 | throw new IllegalArgumentException(e.toString());
|
---|
124 | }
|
---|
125 | }
|
---|
126 |
|
---|
127 | protected void expectRpcAndReturnXmlResource(String rpcContext, String rpc, List params, String resource) throws IOException {
|
---|
128 | try {
|
---|
129 | RpcRequest request = RpcRequestFactory.getRpcRequest(rpcContext, rpc);
|
---|
130 | if (params != null) request.setParams(params);
|
---|
131 | RpcResponseFactory responseFactory = new RpcResponseFactory();
|
---|
132 | RpcResponse response = (RpcResponse) responseFactory.handleResponse(getResourceAsString(resource), request);
|
---|
133 | expectRpcAndReturn(request, response);
|
---|
134 | } catch (FoundationsException e) {
|
---|
135 | throw new IllegalArgumentException(e.toString());
|
---|
136 | }
|
---|
137 | }
|
---|
138 |
|
---|
139 | protected void replay() {
|
---|
140 | mockConnectionFactoryControl.replay();
|
---|
141 | mockVistaLinkConnectionControl.replay();
|
---|
142 | }
|
---|
143 |
|
---|
144 | protected void verify() {
|
---|
145 | mockConnectionFactoryControl.verify();
|
---|
146 | mockVistaLinkConnectionControl.verify();
|
---|
147 | }
|
---|
148 |
|
---|
149 | protected List createParams(Object arg1) {
|
---|
150 | List l = new ArrayList();
|
---|
151 | l.add(arg1);
|
---|
152 | return l;
|
---|
153 | }
|
---|
154 |
|
---|
155 | protected List createParams(Object arg1, Object arg2) {
|
---|
156 | List l = new ArrayList();
|
---|
157 | l.add(arg1);
|
---|
158 | l.add(arg2);
|
---|
159 | return l;
|
---|
160 | }
|
---|
161 |
|
---|
162 | protected List createParams(Object arg1, Object arg2, Object arg3) {
|
---|
163 | List l = new ArrayList();
|
---|
164 | l.add(arg1);
|
---|
165 | l.add(arg2);
|
---|
166 | l.add(arg3);
|
---|
167 | return l;
|
---|
168 | }
|
---|
169 |
|
---|
170 | static String buildXmlResponse(String rpcResult) {
|
---|
171 | StringBuffer buf = new StringBuffer();
|
---|
172 |
|
---|
173 | return buf.toString();
|
---|
174 | }
|
---|
175 |
|
---|
176 |
|
---|
177 | static class TestRpcResponse extends RpcResponse {
|
---|
178 | protected TestRpcResponse(String rpcResult) {
|
---|
179 | super(buildXmlResponse(rpcResult), buildXmlResponse(rpcResult), null, "gov.va.med.foundations.vistalink.response", rpcResult, "flee");
|
---|
180 | }
|
---|
181 | }
|
---|
182 |
|
---|
183 | static class RpcRequestMatcher extends AbstractMatcher {
|
---|
184 |
|
---|
185 | protected boolean argumentMatches(Object o1, Object o2) {
|
---|
186 | RpcRequest r1 = (RpcRequest) o1;
|
---|
187 | RpcRequest r2 = (RpcRequest) o2;
|
---|
188 |
|
---|
189 | return r1.getRpcName().equals(r2.getRpcName()) &&
|
---|
190 | r1.getRpcContext().equals(r2.getRpcContext()) &&
|
---|
191 | r1.getRpcClientTimeOut() == r2.getRpcClientTimeOut() &&
|
---|
192 | r1.getTimeOut() == r2.getTimeOut() &&
|
---|
193 | parametersMatch(r1.getParams(), r2.getParams());
|
---|
194 | }
|
---|
195 |
|
---|
196 | private boolean parametersMatch(RpcRequestParams p1, RpcRequestParams p2) {
|
---|
197 | if (p1 == null && p2 == null) return true;
|
---|
198 | if (p1 != null && p2 == null) return false;
|
---|
199 | if (p1 == null && p2 != null) return false;
|
---|
200 | for (int pos = 1; true; pos++) {
|
---|
201 | Object v1 = p1.getParam(pos);
|
---|
202 | Object v2 = p2.getParam(pos);
|
---|
203 | if (v1 == null && v2 == null) return true;
|
---|
204 | if (v1 != null && v2 == null) return false;
|
---|
205 | if (v1 == null && v2 != null) return false;
|
---|
206 | return MockControl.EQUALS_MATCHER.matches(new Object[]{v1}, new Object[]{v2});
|
---|
207 | }
|
---|
208 | }
|
---|
209 | }
|
---|
210 | }
|
---|
211 |
|
---|
212 | /* this is the source once we switch to java 5 and later EasyMock
|
---|
213 | import gov.va.med.exception.FoundationsException;
|
---|
214 | import gov.va.med.vistalink.adapter.cci.VistaLinkAppProxyConnectionSpec;
|
---|
215 | import gov.va.med.vistalink.adapter.cci.VistaLinkConnection;
|
---|
216 | import gov.va.med.vistalink.adapter.cci.VistaLinkDuzConnectionSpec;
|
---|
217 | import gov.va.med.vistalink.vistalink.*;
|
---|
218 | import gov.va.med.edp.vistalink.VistaLinkTemplate;
|
---|
219 | import junit.framework.TestCase;
|
---|
220 | import org.easymock.EasyMock;
|
---|
221 | import org.easymock.IArgumentMatcher;
|
---|
222 | import org.easymock.internal.matchers.Equals;
|
---|
223 | import org.springframework.util.FileCopyUtils;
|
---|
224 |
|
---|
225 | import javax.resource.ResourceException;
|
---|
226 | import javax.resource.cci.ConnectionFactory;
|
---|
227 | import java.io.IOException;
|
---|
228 | import java.io.InputStreamReader;
|
---|
229 | import java.io.StringWriter;
|
---|
230 | import java.util.ArrayList;
|
---|
231 | import java.util.List;
|
---|
232 |
|
---|
233 | public abstract class AbstractVistaLinkConnectionTest extends TestCase {
|
---|
234 |
|
---|
235 | protected ConnectionFactory mockConnectionFactory;
|
---|
236 | protected VistaLinkConnection mockVistaLinkConnection;
|
---|
237 | protected MockConnectionFactoryLocator mockConnectionFactoryLocator;
|
---|
238 |
|
---|
239 | private int expectedTimeOut = VistaLinkTemplate.DEFAULT_TIMEOUT;
|
---|
240 |
|
---|
241 | protected void setUp() throws Exception {
|
---|
242 | mockConnectionFactory = EasyMock.createMock(ConnectionFactory.class);
|
---|
243 |
|
---|
244 | mockVistaLinkConnection = EasyMock.createMock(VistaLinkConnection.class);
|
---|
245 |
|
---|
246 | mockConnectionFactoryLocator = new MockConnectionFactoryLocator();
|
---|
247 | mockConnectionFactoryLocator.put(getStationNumber(), mockConnectionFactory);
|
---|
248 | }
|
---|
249 |
|
---|
250 | protected abstract String getStationNumber();
|
---|
251 |
|
---|
252 | protected String getResourceAsString(String resource) throws IOException {
|
---|
253 | StringWriter w = new StringWriter();
|
---|
254 | InputStreamReader r = new InputStreamReader(getClass().getResourceAsStream(resource));
|
---|
255 | FileCopyUtils.copy(r, w);
|
---|
256 | return w.toString();
|
---|
257 | }
|
---|
258 |
|
---|
259 | protected int getExpectedTimeOut() {
|
---|
260 | return expectedTimeOut;
|
---|
261 | }
|
---|
262 |
|
---|
263 | protected void setExpectedTimeOut(int expectedTimeOut) {
|
---|
264 | this.expectedTimeOut = expectedTimeOut;
|
---|
265 | }
|
---|
266 |
|
---|
267 | protected void expectVistaLinkDuzConnection(String duz) {
|
---|
268 | try {
|
---|
269 | EasyMock.expect(mockConnectionFactory.getConnection(new VistaLinkDuzConnectionSpec(getStationNumber(), duz))).andReturn(mockVistaLinkConnection);
|
---|
270 | } catch (ResourceException e) {
|
---|
271 | fail("unexpected exception: " + e.getMessage());
|
---|
272 | }
|
---|
273 | }
|
---|
274 |
|
---|
275 | protected void expectVistaLinkAppProxyConnection(String appProxyName) {
|
---|
276 | try {
|
---|
277 | EasyMock.expect(mockConnectionFactory.getConnection(new VistaLinkAppProxyConnectionSpec(getStationNumber(), appProxyName))).andReturn(mockVistaLinkConnection);
|
---|
278 | } catch (ResourceException e) {
|
---|
279 | fail("unexpected exception: " + e.getMessage());
|
---|
280 | }
|
---|
281 | }
|
---|
282 |
|
---|
283 | protected void expectRpcAndReturn(String rpcContext, String vistalink, List params, String response) {
|
---|
284 | try {
|
---|
285 | RpcRequest request = RpcRequestFactory.getRpcRequest(rpcContext, vistalink);
|
---|
286 | if (params != null) request.setParams(params);
|
---|
287 | expectRpcAndReturn(request, new TestRpcResponse(response));
|
---|
288 | } catch (FoundationsException e) {
|
---|
289 | throw new IllegalArgumentException(e);
|
---|
290 | }
|
---|
291 | }
|
---|
292 |
|
---|
293 | protected void expectRpcAndReturn(RpcRequest request, RpcResponse response) throws FoundationsException {
|
---|
294 | mockVistaLinkConnection.setTimeOut(getExpectedTimeOut());
|
---|
295 | EasyMock.expect(mockVistaLinkConnection.executeRPC(eqRpcRequest(request))).andReturn(response);
|
---|
296 | }
|
---|
297 |
|
---|
298 | protected void expectRpcAndReturnXmlResource(String rpcContext, String vistalink, List params, String resource) throws IOException {
|
---|
299 | try {
|
---|
300 | RpcRequest request = RpcRequestFactory.getRpcRequest(rpcContext, vistalink);
|
---|
301 | if (params != null) request.setParams(params);
|
---|
302 | RpcResponseFactory responseFactory = new RpcResponseFactory();
|
---|
303 | RpcResponse response = (RpcResponse) responseFactory.handleResponse(getResourceAsString(resource), request);
|
---|
304 | expectRpcAndReturn(request, response);
|
---|
305 | } catch (FoundationsException e) {
|
---|
306 | throw new IllegalArgumentException(e);
|
---|
307 | }
|
---|
308 | }
|
---|
309 |
|
---|
310 | protected void expectRpcAndDefaultThrow(String rpcContext, String vistalink, List params, Throwable t) {
|
---|
311 | mockVistaLinkConnection.setTimeOut(getExpectedTimeOut());
|
---|
312 | try {
|
---|
313 | RpcRequest request = RpcRequestFactory.getRpcRequest(rpcContext, vistalink);
|
---|
314 | if (params != null) request.setParams(params);
|
---|
315 | EasyMock.expect(mockVistaLinkConnection.executeRPC(eqRpcRequest(request))).andThrow(t);
|
---|
316 | } catch (FoundationsException e) {
|
---|
317 | throw new IllegalArgumentException(e);
|
---|
318 | }
|
---|
319 | }
|
---|
320 |
|
---|
321 | protected void replay() {
|
---|
322 | EasyMock.replay(mockConnectionFactory, mockVistaLinkConnection);
|
---|
323 | }
|
---|
324 |
|
---|
325 | protected void verify() {
|
---|
326 | EasyMock.verify(mockConnectionFactory, mockVistaLinkConnection);
|
---|
327 | }
|
---|
328 |
|
---|
329 | static String buildXmlResponse(String rpcResult) {
|
---|
330 | StringBuffer buf = new StringBuffer();
|
---|
331 |
|
---|
332 | return buf.toString();
|
---|
333 | }
|
---|
334 |
|
---|
335 | protected List createParams(Object... args) {
|
---|
336 | List l = new ArrayList();
|
---|
337 | for (Object arg : args) {
|
---|
338 | l.add(arg);
|
---|
339 | }
|
---|
340 | return l;
|
---|
341 | }
|
---|
342 |
|
---|
343 | static class TestRpcResponse extends RpcResponse {
|
---|
344 | protected TestRpcResponse(String rpcResult) {
|
---|
345 | super(buildXmlResponse(rpcResult), buildXmlResponse(rpcResult), null, "gov.va.med.foundations.vistalink.response", rpcResult, "flee");
|
---|
346 | }
|
---|
347 | }
|
---|
348 |
|
---|
349 | static class RpcRequestEquals implements IArgumentMatcher {
|
---|
350 | private RpcRequest expected;
|
---|
351 | private RpcRequestParamsEquals paramMatcher;
|
---|
352 |
|
---|
353 | public RpcRequestEquals(RpcRequest expected) {
|
---|
354 | this.expected = expected;
|
---|
355 | this.paramMatcher = new RpcRequestParamsEquals(expected.getParams());
|
---|
356 | }
|
---|
357 |
|
---|
358 | public boolean matches(Object request) {
|
---|
359 | if (!(request instanceof RpcRequest)) {
|
---|
360 | return false;
|
---|
361 | }
|
---|
362 |
|
---|
363 | RpcRequest actual = (RpcRequest) request;
|
---|
364 | return expected.getRpcName().equals(actual.getRpcName()) &&
|
---|
365 | expected.getRpcContext().equals(actual.getRpcContext()) &&
|
---|
366 | expected.getRpcClientTimeOut() == actual.getRpcClientTimeOut() &&
|
---|
367 | expected.getTimeOut() == actual.getTimeOut() &&
|
---|
368 | expected.isXmlResponse() == actual.isXmlResponse() &&
|
---|
369 | paramMatcher.matches(actual.getParams());
|
---|
370 | }
|
---|
371 |
|
---|
372 | public void appendTo(StringBuffer buffer) {
|
---|
373 | buffer.append("eqRpcRequest(");
|
---|
374 | buffer.append(expected.toString());
|
---|
375 | buffer.append("\")");
|
---|
376 | }
|
---|
377 | }
|
---|
378 |
|
---|
379 | static class RpcRequestParamsEquals implements IArgumentMatcher {
|
---|
380 | private RpcRequestParams expected;
|
---|
381 |
|
---|
382 | public RpcRequestParamsEquals(RpcRequestParams expected) {
|
---|
383 | this.expected = expected;
|
---|
384 | }
|
---|
385 |
|
---|
386 | public boolean matches(Object request) {
|
---|
387 | if (!(request instanceof RpcRequestParams)) {
|
---|
388 | return false;
|
---|
389 | }
|
---|
390 |
|
---|
391 | RpcRequestParams actual = (RpcRequestParams) request;
|
---|
392 |
|
---|
393 | int position = 1;
|
---|
394 | Object expectedValue = expected.getParam(position);
|
---|
395 | Object actualValue = expected.getParam(position);
|
---|
396 | if (expectedValue == null && actualValue == null) return true;
|
---|
397 |
|
---|
398 | while (expectedValue != null && actualValue != null) {
|
---|
399 | if (!(new Equals(expectedValue).matches(actualValue))) {
|
---|
400 | return false;
|
---|
401 | }
|
---|
402 | position++;
|
---|
403 | expectedValue = expected.getParam(position);
|
---|
404 | actualValue = actual.getParam(position);
|
---|
405 | }
|
---|
406 | if ((expectedValue == null && actualValue != null) || (expectedValue != null && actualValue == null))
|
---|
407 | return false;
|
---|
408 | return true;
|
---|
409 | }
|
---|
410 |
|
---|
411 | public void appendTo(StringBuffer buffer) {
|
---|
412 | buffer.append("eqRpcRequest(");
|
---|
413 | buffer.append(expected.toString());
|
---|
414 | buffer.append("\")");
|
---|
415 | }
|
---|
416 | }
|
---|
417 |
|
---|
418 | public static <T extends RpcRequest> T eqRpcRequest(T in) {
|
---|
419 | EasyMock.reportMatcher(new RpcRequestEquals(in));
|
---|
420 | return null;
|
---|
421 | }
|
---|
422 | }
|
---|
423 | */
|
---|