1 | package gov.va.med.edp.rpc;
|
---|
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.RpcRequest;
|
---|
8 | import gov.va.med.vistalink.rpc.RpcRequestFactory;
|
---|
9 | import gov.va.med.vistalink.rpc.RpcResponse;
|
---|
10 | import gov.va.med.vistalink.rpc.RpcRequestParams;
|
---|
11 | import junit.framework.TestCase;
|
---|
12 | import org.easymock.AbstractMatcher;
|
---|
13 | import org.easymock.ArgumentsMatcher;
|
---|
14 | import org.easymock.MockControl;
|
---|
15 | import org.easymock.internal.EqualsMatcher;
|
---|
16 |
|
---|
17 | import javax.resource.cci.ConnectionFactory;
|
---|
18 | import javax.resource.ResourceException;
|
---|
19 | import java.util.List;
|
---|
20 |
|
---|
21 | public abstract class AbstractVistaLinkConnectionTest extends TestCase {
|
---|
22 |
|
---|
23 | protected MockControl mockConnectionFactoryControl;
|
---|
24 | protected ConnectionFactory mockConnectionFactory;
|
---|
25 | protected MockControl mockVistaLinkConnectionControl;
|
---|
26 | protected VistaLinkConnection mockVistaLinkConnection;
|
---|
27 | protected MockConnectionFactoryLocator mockConnectionFactoryLocator;
|
---|
28 |
|
---|
29 | static ArgumentsMatcher RPC_REQUEST_MATCHER = new RpcRequestMatcher();
|
---|
30 |
|
---|
31 | protected void setUp() throws Exception {
|
---|
32 | mockConnectionFactoryControl = MockControl.createControl(ConnectionFactory.class);
|
---|
33 | mockConnectionFactory = (ConnectionFactory) mockConnectionFactoryControl.getMock();
|
---|
34 |
|
---|
35 | mockVistaLinkConnectionControl = MockControl.createControl(VistaLinkConnection.class);
|
---|
36 | mockVistaLinkConnection = (VistaLinkConnection) mockVistaLinkConnectionControl.getMock();
|
---|
37 |
|
---|
38 | mockVistaLinkConnection.setTimeOut(VistaLinkRpcTemplate.DEFAULT_TIMEOUT);
|
---|
39 | mockVistaLinkConnectionControl.setDefaultVoidCallable();
|
---|
40 |
|
---|
41 | mockConnectionFactoryLocator = new MockConnectionFactoryLocator();
|
---|
42 | mockConnectionFactoryLocator.put(getStationNumber(), mockConnectionFactory);
|
---|
43 | }
|
---|
44 |
|
---|
45 | protected abstract String getStationNumber();
|
---|
46 |
|
---|
47 | protected void expectVistaLinkDuzConnection(String duz) {
|
---|
48 | try {
|
---|
49 | mockConnectionFactoryControl.expectAndDefaultReturn(mockConnectionFactory.getConnection(new VistaLinkDuzConnectionSpec(getStationNumber(), duz)), mockVistaLinkConnection);
|
---|
50 | } catch (ResourceException e) {
|
---|
51 | fail("unexpected exception: " + e.getMessage());
|
---|
52 | }
|
---|
53 | }
|
---|
54 |
|
---|
55 | protected void expectVistaLinkAppProxyConnection(String appProxyName) {
|
---|
56 | try {
|
---|
57 | mockConnectionFactoryControl.expectAndDefaultReturn(mockConnectionFactory.getConnection(new VistaLinkAppProxyConnectionSpec(getStationNumber(), appProxyName)), mockVistaLinkConnection);
|
---|
58 | } catch (ResourceException e) {
|
---|
59 | fail("unexpected exception: " + e.getMessage());
|
---|
60 | }
|
---|
61 | }
|
---|
62 |
|
---|
63 | protected void expectRpcAndReturn(String rpcContext, String rpc, List params, RpcResponse response) {
|
---|
64 | try {
|
---|
65 | RpcRequest request = RpcRequestFactory.getRpcRequest(rpcContext, rpc);
|
---|
66 | if (params != null) {
|
---|
67 | request.setParams(params);
|
---|
68 | }
|
---|
69 | mockVistaLinkConnection.executeRPC(request);
|
---|
70 | mockVistaLinkConnectionControl.setMatcher(RPC_REQUEST_MATCHER);
|
---|
71 | mockVistaLinkConnectionControl.setReturnValue(response);
|
---|
72 | } catch (FoundationsException e) {
|
---|
73 | throw new IllegalArgumentException(e.toString());
|
---|
74 | }
|
---|
75 | }
|
---|
76 |
|
---|
77 | protected void expectRpcAndReturn(String rpcContext, String rpc, String response) {
|
---|
78 | expectRpcAndReturn(rpcContext, rpc, null, new TestRpcResponse(response));
|
---|
79 | }
|
---|
80 |
|
---|
81 | protected void expectRpcAndReturn(String rpcContext, String rpc, List params, String response) {
|
---|
82 | expectRpcAndReturn(rpcContext, rpc, params, new TestRpcResponse(response));
|
---|
83 | }
|
---|
84 |
|
---|
85 | protected void expectRpcAndDefaultThrow(String rpcContext, String rpc, Throwable t) {
|
---|
86 | try {
|
---|
87 | mockVistaLinkConnection.executeRPC(RpcRequestFactory.getRpcRequest(rpcContext, rpc));
|
---|
88 | mockVistaLinkConnectionControl.setMatcher(RPC_REQUEST_MATCHER);
|
---|
89 | mockVistaLinkConnectionControl.setDefaultThrowable(t);
|
---|
90 | } catch (FoundationsException e) {
|
---|
91 | throw new IllegalArgumentException(e.toString());
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | protected void replay() {
|
---|
96 | mockConnectionFactoryControl.replay();
|
---|
97 | mockVistaLinkConnectionControl.replay();
|
---|
98 | }
|
---|
99 |
|
---|
100 | protected void verify() {
|
---|
101 | mockConnectionFactoryControl.verify();
|
---|
102 | mockVistaLinkConnectionControl.verify();
|
---|
103 | }
|
---|
104 |
|
---|
105 | static String buildXmlResponse(String rpcResult) {
|
---|
106 | StringBuffer buf = new StringBuffer();
|
---|
107 |
|
---|
108 | return buf.toString();
|
---|
109 | }
|
---|
110 |
|
---|
111 |
|
---|
112 | static class TestRpcResponse extends RpcResponse {
|
---|
113 | protected TestRpcResponse(String rpcResult) {
|
---|
114 | super(buildXmlResponse(rpcResult), buildXmlResponse(rpcResult), null, "gov.va.med.foundations.rpc.response", rpcResult, "flee");
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | static class RpcRequestMatcher extends AbstractMatcher {
|
---|
119 |
|
---|
120 | protected boolean argumentMatches(Object o1, Object o2) {
|
---|
121 | RpcRequest r1 = (RpcRequest) o1;
|
---|
122 | RpcRequest r2 = (RpcRequest) o2;
|
---|
123 |
|
---|
124 | return r1.getRpcName().equals(r2.getRpcName()) &&
|
---|
125 | r1.getRpcContext().equals(r2.getRpcContext()) &&
|
---|
126 | r1.getRpcClientTimeOut() == r2.getRpcClientTimeOut() &&
|
---|
127 | r1.getTimeOut() == r2.getTimeOut() &&
|
---|
128 | parametersMatch(r1.getParams(), r2.getParams());
|
---|
129 | }
|
---|
130 |
|
---|
131 | private boolean parametersMatch(RpcRequestParams p1, RpcRequestParams p2) {
|
---|
132 | if (p1 == null && p2 == null) return true;
|
---|
133 | if (p1 != null && p2 == null) return false;
|
---|
134 | if (p1 == null && p2 != null) return false;
|
---|
135 | for (int pos = 1; true; pos++) {
|
---|
136 | Object v1 = p1.getParam(pos);
|
---|
137 | Object v2 = p2.getParam(pos);
|
---|
138 | if (v1 == null && v2 == null) return true;
|
---|
139 | if (v1 != null && v2 == null) return false;
|
---|
140 | if (v1 == null && v2 != null) return false;
|
---|
141 | return MockControl.EQUALS_MATCHER.matches(new Object[] {v1}, new Object[] {v2});
|
---|
142 | }
|
---|
143 | }
|
---|
144 | }
|
---|
145 | }
|
---|