Update license header in appc client-lib files
[appc.git] / appc-client / client-lib / src / test / java / org / onap / appc / client / impl / core / SyncFlowTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 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  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.client.impl.core;
25
26 import org.onap.appc.client.impl.core.CoreException;
27 import org.onap.appc.client.impl.core.CoreManager;
28 import org.onap.appc.client.impl.core.ICoreSyncResponseHandler;
29 import org.onap.appc.client.impl.core.MessageContext;
30 import org.onap.appc.client.impl.core.SyncRequestResponseHandler;
31 import org.onap.appc.client.impl.protocol.AsyncProtocol;
32 import org.onap.appc.client.impl.protocol.ProtocolException;
33 import org.onap.appc.client.impl.protocol.RetrieveMessageCallback;
34 import org.junit.Assert;
35 import org.junit.Before;
36
37 import java.util.Properties;
38 import java.util.concurrent.ExecutorService;
39 import java.util.concurrent.Executors;
40 import java.util.concurrent.TimeoutException;
41
42 import static org.mockito.Mockito.mock;
43
44 public class SyncFlowTest {
45     CoreManager coreManager = null;
46
47     public void initialize() throws CoreException {
48         Properties prop = new Properties();
49         prop.setProperty("client.pool.size", "10");
50         prop.setProperty("client.response.timeout", "7000");
51         coreManager = new CoreManagerTest(prop);
52     }
53
54     <T> T syncRequest(String request, ICoreSyncResponseHandler businessCallback, String correlationId, String rpcName ) throws CoreException, TimeoutException {
55         SyncRequestResponseHandler requestResponseHandler = new SyncRequestResponseHandler(correlationId, businessCallback, coreManager);
56         requestResponseHandler.sendRequest(request, correlationId, rpcName);
57         T responseObject = (T) requestResponseHandler.getResponse();
58         return responseObject;
59     }
60
61     public void blockRequestTest(){
62         ICoreSyncResponseHandler handler = new ICoreSyncResponseHandlerImpl1();
63         try {
64             syncRequest("request 1", handler, "vasia1", "test");
65         }catch (Throwable e){
66             e.printStackTrace();
67             Assert.assertTrue(e != null);
68         }
69
70     }
71
72     public <T> void blockRequestSucceedTest() throws InterruptedException {
73         ExecutorService executorService = Executors.newFixedThreadPool(2);
74         final ICoreSyncResponseHandler handler = new ICoreSyncResponseHandlerImpl1();
75         try {
76             executorService.submit(new Runnable() {
77                 public void run() {
78                     System.out.println("Send request");
79                     T response;
80                     try {
81                         response = syncRequest("request 1", handler, "vasia1", "test");
82                         System.out.println("=======" + response.toString());
83                     } catch (CoreException e) {
84                         e.printStackTrace();
85                     } catch (TimeoutException e) {
86                         e.printStackTrace();
87                     }
88                 }
89             });
90         }catch (Throwable e){
91             Assert.assertTrue((RuntimeException)e != null);
92         }
93         Thread.sleep(2000);
94         executorService.submit(new Runnable() {
95             public void run() {
96                 MessageContext ctx = new MessageContext();
97                 ctx.setCorrelationID("vasia1");
98                 ctx.setType("response");
99                 try {
100                     System.out.println("Send response 1");
101                     coreManager.getProtocolCallback().onResponse("response for request 1", ctx);
102                 } catch (Exception e) {
103                     e.printStackTrace();
104                 }
105             }
106         });
107
108         Thread.sleep(2000);
109         executorService.submit(new Runnable() {
110             public void run() {
111                 MessageContext ctx = new MessageContext();
112                 ctx.setCorrelationID("vasia1");
113                 ctx.setType("response");
114                 try {
115                     System.out.println("Send response 2");
116                     coreManager.getProtocolCallback().onResponse("response for request 1 final", ctx);
117                 } catch (Exception e) {
118                     e.printStackTrace();
119                 }
120             }
121         });
122         Thread.sleep(1000);
123
124     }
125
126     class ICoreSyncResponseHandlerImpl1 implements ICoreSyncResponseHandler{
127
128
129         public <T> T onResponse(String message, String type) {
130             System.out.println("Received message = " + message) ;
131             if(message.contains("final")){
132                 return (T) new String(message);
133             }
134             return null;
135         }
136     }
137
138     class CoreManagerTest extends CoreManager{
139         CoreManagerTest(Properties properties) throws CoreException {
140             super(properties);
141             protocol = mock(AsyncProtocol.class);
142         }
143         protected void sendRequest2Protocol(String request, String corrId, String rpcName) throws CoreException {
144         }
145
146         protected void initProtocol(Properties properties, RetrieveMessageCallback protocolCallback) throws ProtocolException{
147
148         }
149     }
150 }