d1720f03b8b15b79f4eb8d35a4934f90ee0ff382
[appc.git] / appc-client / client-lib / src / test / java / org / openecomp / appc / client / impl / core / ResponseManagerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.openecomp.appc.client.impl.core;
26
27 import org.openecomp.appc.client.impl.core.AsyncRequestResponseHandler;
28 import org.openecomp.appc.client.impl.core.CoreException;
29 import org.openecomp.appc.client.impl.core.CoreManager;
30 import org.openecomp.appc.client.impl.core.ICoreAsyncResponseHandler;
31 import org.openecomp.appc.client.impl.core.MessageContext;
32 import org.openecomp.appc.client.impl.protocol.AsyncProtocol;
33 import org.openecomp.appc.client.impl.protocol.ProtocolException;
34 import org.openecomp.appc.client.impl.protocol.RetrieveMessageCallback;
35 import org.junit.Before;
36
37 import java.util.Properties;
38 import java.util.concurrent.ExecutorService;
39 import java.util.concurrent.Executors;
40
41 import static org.mockito.Mockito.mock;
42
43 public class ResponseManagerTest {
44
45     private final ExecutorService executorService = Executors.newFixedThreadPool(10);
46     ICoreAsyncResponseHandler listener1 = new ListenerImpl();
47     ICoreAsyncResponseHandler listener2 = new SleeepListenerImpl();
48     ICoreAsyncResponseHandler listener3 = new ListenerImpl();
49     CoreManager coreManager = null;
50
51     public void initialize() throws CoreException {
52         Properties prop = new Properties();
53         prop.setProperty("client.pool.size", "10");
54         prop.setProperty("client.response.timeout", "7000");
55         coreManager = new ResponseManagerTest.CoreManagerTest(prop);
56     }
57
58     void asyncRequest(String request, ICoreAsyncResponseHandler businessCallback, String correlationId, String rpcName) throws CoreException {
59         AsyncRequestResponseHandler requestResponseHandler = new AsyncRequestResponseHandler(correlationId, businessCallback, coreManager);
60         requestResponseHandler.sendRequest(request, correlationId, rpcName);
61     }
62
63     public void simpleResponseTest() throws Exception {
64         System.out.println("simpleResponseTest");
65         asyncRequest("request 1", listener1,"vasia1", "test");
66         MessageContext msgCtx = new MessageContext();
67         msgCtx.setCorrelationID("vasia1");
68         msgCtx.setType("response");
69         coreManager.getProtocolCallback().onResponse("vasia1 response",msgCtx);
70         coreManager.getProtocolCallback().onResponse("vasia2 response",msgCtx);
71         Thread.sleep(10);
72     }
73
74     public void twoResponseTest() throws Exception {
75         System.out.println("twoResponseTest");
76         asyncRequest("twoResponseTest request 1", listener2,"vasia2", "test");
77         MessageContext msgCtx = new MessageContext();
78         msgCtx.setCorrelationID("vasia2");
79         msgCtx.setType("response");
80         coreManager.getProtocolCallback().onResponse("second of vasia2",msgCtx);
81         Thread.sleep(100);
82         asyncRequest("twoResponseTest request 2", listener1,"vasia1", "test");
83         MessageContext msgCtx2 = new MessageContext();
84         msgCtx2.setCorrelationID("vasia1");
85         msgCtx2.setType("response");
86         coreManager.getProtocolCallback().onResponse("first of vasia1",msgCtx2);
87         Thread.sleep(150);
88     }
89
90     public void threeResponseTest() throws Exception {
91         System.out.println("treeResponseTest");
92         asyncRequest("threeResponseTest request 2", listener1,"vasia4", "test");
93         asyncRequest("threeResponseTest request 1", listener2,"vasia2", "test");
94         MessageContext msgCtx2 = new MessageContext();
95         msgCtx2.setCorrelationID("vasia2");
96         msgCtx2.setType("response");
97         coreManager.getProtocolCallback().onResponse("second of vasia2",msgCtx2);
98
99         asyncRequest("threeResponseTest request 2", listener1,"vasia1", "test");
100         MessageContext msgCtx1 = new MessageContext();
101         msgCtx1.setCorrelationID("vasia1");
102         msgCtx1.setType("response");
103         coreManager.getProtocolCallback().onResponse("vasia1",msgCtx1);
104
105         asyncRequest("threeResponseTest request 3", listener3,"vasia3", "test");
106         MessageContext msgCtx3 = new MessageContext();
107         msgCtx3.setCorrelationID("vasia3");
108         msgCtx3.setType("response");
109         coreManager.getProtocolCallback().onResponse("three1",msgCtx3);
110
111         coreManager.getProtocolCallback().onResponse("three2", msgCtx3);
112
113         coreManager.getProtocolCallback().onResponse("first1", msgCtx1);
114         Thread.sleep(250);
115
116         coreManager.getProtocolCallback().onResponse("first2", msgCtx1);
117         Thread.sleep(10000);
118     }
119
120     private class ListenerImpl implements ICoreAsyncResponseHandler{
121
122         public boolean onResponse(String message, String type) {
123             System.out.println("callback " + message);
124             return message != null;
125         }
126
127         @Override
128         public void onException(Exception e) {
129             e.printStackTrace();
130         }
131     }
132
133     private class SleeepListenerImpl implements ICoreAsyncResponseHandler{
134
135         public boolean onResponse(String message, String type) {
136             try {
137                 Thread.sleep(150);
138                 System.out.println("sleep callback " + message);
139             } catch (InterruptedException e) {
140                 e.printStackTrace();
141             }
142             return message != null;
143         }
144
145         @Override
146         public void onException(Exception e) {
147             e.printStackTrace();
148         }
149     }
150
151     class CoreManagerTest extends CoreManager{
152         CoreManagerTest(Properties properties) throws CoreException {
153             super(properties);
154             protocol = mock(AsyncProtocol.class);
155         }
156         protected void sendRequest2Protocol(String request, String corrId, String rpcName) throws CoreException {
157         }
158
159         protected void initProtocol(Properties properties, RetrieveMessageCallback protocolCallback) throws ProtocolException {
160
161         }
162     }
163 }