2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2018 Nokia.
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.
20 * ============LICENSE_END=========================================================
23 package org.onap.so.bpmn.infrastructure.pnf.dmaap;
26 import static org.junit.Assert.assertEquals;
27 import static org.mockito.ArgumentMatchers.any;
28 import static org.mockito.ArgumentMatchers.eq;
29 import static org.mockito.Mockito.mock;
30 import static org.mockito.Mockito.verify;
31 import static org.mockito.Mockito.verifyZeroInteractions;
32 import static org.mockito.Mockito.when;
33 import java.io.ByteArrayInputStream;
34 import java.io.IOException;
35 import java.lang.reflect.Field;
37 import java.util.concurrent.ConcurrentHashMap;
38 import java.util.concurrent.ScheduledThreadPoolExecutor;
39 import org.apache.http.HttpEntity;
40 import org.apache.http.HttpResponse;
41 import org.apache.http.ProtocolVersion;
42 import org.apache.http.client.HttpClient;
43 import org.apache.http.client.methods.HttpGet;
44 import org.apache.http.entity.InputStreamEntity;
45 import org.apache.http.message.BasicHttpResponse;
46 import org.junit.Before;
47 import org.junit.Ignore;
48 import org.junit.Test;
49 import org.junit.runner.RunWith;
50 import org.mockito.ArgumentCaptor;
51 import org.mockito.Mock;
52 import org.mockito.junit.MockitoJUnitRunner;
53 import org.onap.so.bpmn.infrastructure.pnf.dmaap.PnfEventReadyDmaapClient.DmaapTopicListenerThread;
54 import org.springframework.core.env.Environment;
56 @RunWith(MockitoJUnitRunner.class)
57 public class PnfEventReadyDmaapClientTest {
59 private static final String PNF_CORRELATION_ID = "corrTestId";
60 private static final String PNF_CORRELATION_ID_NOT_FOUND_IN_MAP = "otherCorrId";
61 private static final String JSON_EXAMPLE_WITH_PNF_CORRELATION_ID = "[{\"correlationId\": \"%s\","
62 + "\"value\":\"value1\"},{\"correlationId\": \"corr\",\"value\":\"value2\"}]";
64 private static final String JSON_EXAMPLE_WITH_NO_PNF_CORRELATION_ID = "[{\"key1\":\"value1\"}]";
66 private static final String HOST = "hostTest";
67 private static final int PORT = 1234;
68 private static final String PROTOCOL = "http";
69 private static final String URI_PATH_PREFIX = "eventsForTesting";
70 private static final String EVENT_TOPIC_TEST = "eventTopicTest";
71 private static final String CONSUMER_ID = "consumerTestId";
72 private static final String CONSUMER_GROUP = "consumerGroupTest";
73 private static final int TOPIC_LISTENER_DELAY_IN_SECONDS = 5;
76 private Environment env;
77 private PnfEventReadyDmaapClient testedObject;
79 private DmaapTopicListenerThread testedObjectInnerClassThread;
80 private HttpClient httpClientMock;
81 private Runnable threadMockToNotifyCamundaFlow;
82 private ScheduledThreadPoolExecutor executorMock;
85 public void init() throws NoSuchFieldException, IllegalAccessException {
86 when(env.getProperty(eq("pnf.dmaap.port"), eq(Integer.class))).thenReturn(PORT);
87 when(env.getProperty(eq("pnf.dmaap.host"))).thenReturn(HOST);
88 when(env.getProperty(eq("pnf.dmaap.protocol"))).thenReturn(PROTOCOL);
89 when(env.getProperty(eq("pnf.dmaap.uriPathPrefix"))).thenReturn(URI_PATH_PREFIX);
90 when(env.getProperty(eq("pnf.dmaap.topicName"))).thenReturn(EVENT_TOPIC_TEST);
91 when(env.getProperty(eq("pnf.dmaap.consumerId"))).thenReturn(CONSUMER_ID);
92 when(env.getProperty(eq("pnf.dmaap.consumerGroup"))).thenReturn(CONSUMER_GROUP);
93 when(env.getProperty(eq("pnf.dmaap.topicListenerDelayInSeconds"), eq(Integer.class)))
94 .thenReturn(TOPIC_LISTENER_DELAY_IN_SECONDS);
95 testedObject = new PnfEventReadyDmaapClient(env);
96 testedObjectInnerClassThread = testedObject.new DmaapTopicListenerThread();
97 httpClientMock = mock(HttpClient.class);
98 threadMockToNotifyCamundaFlow = mock(Runnable.class);
99 executorMock = mock(ScheduledThreadPoolExecutor.class);
104 * Test run method, where the are following conditions:
106 * - DmaapThreadListener is running, flag is set to true
108 * - map is filled with one entry with the key that we get from response
110 * run method should invoke thread from map to notify camunda process, remove element from the map (map is empty)
111 * and shutdown the executor because of empty map
115 public void pnfCorrelationIdIsFoundInHttpResponse_notifyAboutPnfReady() throws IOException {
116 when(httpClientMock.execute(any(HttpGet.class)))
117 .thenReturn(createResponse(String.format(JSON_EXAMPLE_WITH_PNF_CORRELATION_ID, PNF_CORRELATION_ID)));
118 testedObjectInnerClassThread.run();
119 ArgumentCaptor<HttpGet> captor1 = ArgumentCaptor.forClass(HttpGet.class);
120 verify(httpClientMock).execute(captor1.capture());
122 assertEquals(captor1.getValue().getURI().getHost(), HOST);
123 assertEquals(captor1.getValue().getURI().getPort(), PORT);
124 assertEquals(captor1.getValue().getURI().getScheme(), PROTOCOL);
125 assertEquals(captor1.getValue().getURI().getPath(),
126 "/" + URI_PATH_PREFIX + "/" + EVENT_TOPIC_TEST + "/" + CONSUMER_GROUP + "/" + CONSUMER_ID + "");
128 verify(threadMockToNotifyCamundaFlow).run();
129 verify(executorMock).shutdown();
133 * Test run method, where the are following conditions:
135 * - DmaapThreadListener is running, flag is set to true
137 * - map is filled with one entry with the pnfCorrelationId that does not match to pnfCorrelationId taken from http
138 * response. run method should not do anything with the map not run any thread to notify camunda process
142 public void pnfCorrelationIdIsFoundInHttpResponse_NotFoundInMap() throws IOException {
143 when(httpClientMock.execute(any(HttpGet.class))).thenReturn(createResponse(
144 String.format(JSON_EXAMPLE_WITH_PNF_CORRELATION_ID, PNF_CORRELATION_ID_NOT_FOUND_IN_MAP)));
145 testedObjectInnerClassThread.run();
146 verifyZeroInteractions(threadMockToNotifyCamundaFlow, executorMock);
150 * Test run method, where the are following conditions:
152 * - DmaapThreadListener is running, flag is set to true
154 * - map is filled with one entry with the pnfCorrelationId but no correlation id is taken from HttpResponse run
155 * method should not do anything with the map and not run any thread to notify camunda process
159 public void pnfCorrelationIdIsNotFoundInHttpResponse() throws IOException {
160 when(httpClientMock.execute(any(HttpGet.class)))
161 .thenReturn(createResponse(JSON_EXAMPLE_WITH_NO_PNF_CORRELATION_ID));
162 testedObjectInnerClassThread.run();
163 verifyZeroInteractions(threadMockToNotifyCamundaFlow, executorMock);
166 private void setPrivateField() throws NoSuchFieldException, IllegalAccessException {
167 Field httpClientField = testedObject.getClass().getDeclaredField("httpClient");
168 httpClientField.setAccessible(true);
169 httpClientField.set(testedObject, httpClientMock);
170 httpClientField.setAccessible(false);
172 Field executorField = testedObject.getClass().getDeclaredField("executor");
173 executorField.setAccessible(true);
174 executorField.set(testedObject, executorMock);
175 executorField.setAccessible(false);
177 Field pnfCorrelationToThreadMapField = testedObject.getClass().getDeclaredField("pnfCorrelationIdToThreadMap");
178 pnfCorrelationToThreadMapField.setAccessible(true);
179 Map<String, Runnable> pnfCorrelationToThreadMap = new ConcurrentHashMap<>();
180 pnfCorrelationToThreadMap.put(PNF_CORRELATION_ID, threadMockToNotifyCamundaFlow);
181 pnfCorrelationToThreadMapField.set(testedObject, pnfCorrelationToThreadMap);
183 Field threadRunFlag = testedObject.getClass().getDeclaredField("dmaapThreadListenerIsRunning");
184 threadRunFlag.setAccessible(true);
185 threadRunFlag.set(testedObject, true);
186 threadRunFlag.setAccessible(false);
189 private HttpResponse createResponse(String json) {
190 HttpEntity entity = new InputStreamEntity(new ByteArrayInputStream(json.getBytes()));
191 ProtocolVersion protocolVersion = new ProtocolVersion("", 1, 1);
192 HttpResponse response = new BasicHttpResponse(protocolVersion, 1, "");
193 response.setEntity(entity);
194 response.setStatusCode(200);