2 * ============LICENSE_START=======================================================
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
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 * ============LICENSE_END=========================================================
24 package org.onap.appc.client.impl.core;
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;
37 import java.util.Properties;
38 import java.util.concurrent.ExecutorService;
39 import java.util.concurrent.Executors;
40 import java.util.concurrent.TimeoutException;
42 import static org.mockito.Mockito.mock;
44 public class SyncFlowTest {
45 CoreManager coreManager = null;
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);
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;
61 public void blockRequestTest(){
62 ICoreSyncResponseHandler handler = new ICoreSyncResponseHandlerImpl1();
64 syncRequest("request 1", handler, "vasia1", "test");
67 Assert.assertTrue(e != null);
72 public <T> void blockRequestSucceedTest() throws InterruptedException {
73 ExecutorService executorService = Executors.newFixedThreadPool(2);
74 final ICoreSyncResponseHandler handler = new ICoreSyncResponseHandlerImpl1();
76 executorService.submit(new Runnable() {
78 System.out.println("Send request");
81 response = syncRequest("request 1", handler, "vasia1", "test");
82 System.out.println("=======" + response.toString());
83 } catch (CoreException e) {
85 } catch (TimeoutException e) {
91 Assert.assertTrue((RuntimeException)e != null);
94 executorService.submit(new Runnable() {
96 MessageContext ctx = new MessageContext();
97 ctx.setCorrelationID("vasia1");
98 ctx.setType("response");
100 System.out.println("Send response 1");
101 coreManager.getProtocolCallback().onResponse("response for request 1", ctx);
102 } catch (Exception e) {
109 executorService.submit(new Runnable() {
111 MessageContext ctx = new MessageContext();
112 ctx.setCorrelationID("vasia1");
113 ctx.setType("response");
115 System.out.println("Send response 2");
116 coreManager.getProtocolCallback().onResponse("response for request 1 final", ctx);
117 } catch (Exception e) {
126 class ICoreSyncResponseHandlerImpl1 implements ICoreSyncResponseHandler{
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);
138 class CoreManagerTest extends CoreManager{
139 CoreManagerTest(Properties properties) throws CoreException {
141 protocol = mock(AsyncProtocol.class);
143 protected void sendRequest2Protocol(String request, String corrId, String rpcName) throws CoreException {
146 protected void initProtocol(Properties properties, RetrieveMessageCallback protocolCallback) throws ProtocolException{