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.onap.appc.client.impl.core;
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;
38 import java.util.Properties;
39 import java.util.concurrent.ExecutorService;
40 import java.util.concurrent.Executors;
41 import java.util.concurrent.TimeoutException;
43 import static org.mockito.Mockito.mock;
45 public class SyncFlowTest {
46 CoreManager coreManager = null;
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);
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;
62 public void blockRequestTest(){
63 ICoreSyncResponseHandler handler = new ICoreSyncResponseHandlerImpl1();
65 syncRequest("request 1", handler, "vasia1", "test");
68 Assert.assertTrue(e != null);
73 public <T> void blockRequestSucceedTest() throws InterruptedException {
74 ExecutorService executorService = Executors.newFixedThreadPool(2);
75 final ICoreSyncResponseHandler handler = new ICoreSyncResponseHandlerImpl1();
77 executorService.submit(new Runnable() {
79 System.out.println("Send request");
82 response = syncRequest("request 1", handler, "vasia1", "test");
83 System.out.println("=======" + response.toString());
84 } catch (CoreException e) {
86 } catch (TimeoutException e) {
92 Assert.assertTrue((RuntimeException)e != null);
95 executorService.submit(new Runnable() {
97 MessageContext ctx = new MessageContext();
98 ctx.setCorrelationID("vasia1");
99 ctx.setType("response");
101 System.out.println("Send response 1");
102 coreManager.getProtocolCallback().onResponse("response for request 1", ctx);
103 } catch (Exception e) {
110 executorService.submit(new Runnable() {
112 MessageContext ctx = new MessageContext();
113 ctx.setCorrelationID("vasia1");
114 ctx.setType("response");
116 System.out.println("Send response 2");
117 coreManager.getProtocolCallback().onResponse("response for request 1 final", ctx);
118 } catch (Exception e) {
127 class ICoreSyncResponseHandlerImpl1 implements ICoreSyncResponseHandler{
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);
139 class CoreManagerTest extends CoreManager{
140 CoreManagerTest(Properties properties) throws CoreException {
142 protocol = mock(AsyncProtocol.class);
144 protected void sendRequest2Protocol(String request, String corrId, String rpcName) throws CoreException {
147 protected void initProtocol(Properties properties, RetrieveMessageCallback protocolCallback) throws ProtocolException{