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