2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.openecomp.appc.client.impl.core;
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;
37 import java.util.Properties;
38 import java.util.concurrent.ExecutorService;
39 import java.util.concurrent.Executors;
41 import static org.mockito.Mockito.mock;
43 public class ResponseManagerTest {
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;
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);
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);
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);
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);
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);
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);
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);
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);
111 coreManager.getProtocolCallback().onResponse("three2", msgCtx3);
113 coreManager.getProtocolCallback().onResponse("first1", msgCtx1);
116 coreManager.getProtocolCallback().onResponse("first2", msgCtx1);
120 private class ListenerImpl implements ICoreAsyncResponseHandler{
122 public boolean onResponse(String message, String type) {
123 System.out.println("callback " + message);
124 return message != null;
128 public void onException(Exception e) {
133 private class SleeepListenerImpl implements ICoreAsyncResponseHandler{
135 public boolean onResponse(String message, String type) {
138 System.out.println("sleep callback " + message);
139 } catch (InterruptedException e) {
142 return message != null;
146 public void onException(Exception e) {
151 class CoreManagerTest extends CoreManager{
152 CoreManagerTest(Properties properties) throws CoreException {
154 protocol = mock(AsyncProtocol.class);
156 protected void sendRequest2Protocol(String request, String corrId, String rpcName) throws CoreException {
159 protected void initProtocol(Properties properties, RetrieveMessageCallback protocolCallback) throws ProtocolException {