2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.so.bpmn.infrastructure.pnf.dmaap;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.mockito.Matchers.any;
25 import static org.mockito.Matchers.eq;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.verify;
28 import static org.mockito.Mockito.verifyZeroInteractions;
29 import static org.mockito.Mockito.when;
31 import java.io.IOException;
32 import java.io.UnsupportedEncodingException;
33 import java.lang.reflect.Field;
35 import java.util.concurrent.ConcurrentHashMap;
36 import java.util.concurrent.ScheduledExecutorService;
37 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.StringEntity;
45 import org.apache.http.message.BasicHttpResponse;
46 import org.junit.Before;
47 import org.junit.Test;
48 import org.junit.runner.RunWith;
49 import org.mockito.ArgumentCaptor;
50 import org.mockito.InjectMocks;
51 import org.mockito.Mock;
52 import org.mockito.runners.MockitoJUnitRunner;
53 import org.onap.so.bpmn.infrastructure.pnf.dmaap.PnfEventReadyDmaapClient.DmaapTopicListenerThread;
54 import org.springframework.core.env.Environment;
55 @RunWith(MockitoJUnitRunner.class)
56 public class PnfEventReadyDmaapClientTest {
58 private static final String CORRELATION_ID = "corrTestId";
59 private static final String CORRELATION_ID_NOT_FOUND_IN_MAP = "otherCorrId";
60 private static final String JSON_EXAMPLE_WITH_CORRELATION_ID = "[\n"
62 + " \"pnfRegistrationFields\" : {\n"
63 + " \"correlationId\" : \"%s\",\n"
64 + " \"value\" : \"value1\"\n"
68 + " \"pnfRegistrationFields\" : {\n"
69 + " \"correlationId\" : \"corr\",\n"
70 + " \"value\" : \"value2\"\n"
74 private static final String JSON_EXAMPLE_WITH_NO_CORRELATION_ID =
75 "{\"pnfRegistrationFields\":{\"field\":\"value\"}}";
77 private static final String HOST = "hostTest";
78 private static final int PORT = 1234;
79 private static final String PROTOCOL = "http";
80 private static final String URI_PATH_PREFIX = "eventsForTesting";
81 private static final String EVENT_TOPIC_TEST = "eventTopicTest";
82 private static final String CONSUMER_ID = "consumerTestId";
83 private static final String CONSUMER_GROUP = "consumerGroupTest";
85 private Environment env;
86 private PnfEventReadyDmaapClient testedObject;
88 private DmaapTopicListenerThread testedObjectInnerClassThread;
89 private HttpClient httpClientMock;
90 private Runnable threadMockToNotifyCamundaFlow;
91 private ScheduledThreadPoolExecutor executorMock;
94 public void init() throws NoSuchFieldException, IllegalAccessException {
95 testedObject = new PnfEventReadyDmaapClient(env);
96 when(env.getProperty(eq("pnf.dmaap.port"), eq(Integer.class))).thenReturn(PORT);
97 when(env.getProperty(eq("pnf.dmaap.host"))).thenReturn(HOST);
98 testedObject.setDmaapProtocol(PROTOCOL);
99 testedObject.setDmaapUriPathPrefix(URI_PATH_PREFIX);
100 testedObject.setDmaapTopicName(EVENT_TOPIC_TEST);
101 testedObject.setConsumerId(CONSUMER_ID);
102 testedObject.setConsumerGroup(CONSUMER_GROUP);
103 testedObject.setDmaapClientDelayInSeconds(1);
105 testedObjectInnerClassThread = testedObject.new DmaapTopicListenerThread();
106 httpClientMock = mock(HttpClient.class);
107 threadMockToNotifyCamundaFlow = mock(Runnable.class);
108 executorMock = mock(ScheduledThreadPoolExecutor.class);
113 * Test run method, where the are following conditions:
114 * <p> - DmaapThreadListener is running, flag is set to true
115 * <p> - map is filled with one entry with the key that we get from response
116 * <p> run method should invoke thread from map to notify camunda process, remove element from the map (map is
117 * empty) and shutdown the executor because of empty map
120 public void correlationIdIsFoundInHttpResponse_notifyAboutPnfReady()
122 when(httpClientMock.execute(any(HttpGet.class))).
123 thenReturn(createResponse(String.format(JSON_EXAMPLE_WITH_CORRELATION_ID, CORRELATION_ID)));
124 testedObjectInnerClassThread.run();
125 ArgumentCaptor<HttpGet> captor1 = ArgumentCaptor.forClass(HttpGet.class);
126 verify(httpClientMock).execute(captor1.capture());
127 assertThat(captor1.getValue().getURI()).hasHost(HOST).hasPort(PORT).hasScheme(PROTOCOL)
129 "/" + URI_PATH_PREFIX + "/" + EVENT_TOPIC_TEST + "/" + CONSUMER_GROUP + "/" + CONSUMER_ID + "");
130 verify(threadMockToNotifyCamundaFlow).run();
131 verify(executorMock).shutdown();
135 * Test run method, where the are following conditions:
136 * <p> - DmaapThreadListener is running, flag is set to true
137 * <p> - map is filled with one entry with the correlationId that does not match to correlationId
138 * taken from http response. run method should not do anything with the map not run any thread to notify camunda
142 public void correlationIdIsFoundInHttpResponse_NotFoundInMap()
144 when(httpClientMock.execute(any(HttpGet.class))).
145 thenReturn(createResponse(
146 String.format(JSON_EXAMPLE_WITH_CORRELATION_ID, CORRELATION_ID_NOT_FOUND_IN_MAP)));
147 testedObjectInnerClassThread.run();
148 verifyZeroInteractions(threadMockToNotifyCamundaFlow, executorMock);
152 * Test run method, where the are following conditions:
153 * <p> - DmaapThreadListener is running, flag is set to true
154 * <p> - map is filled with one entry with the correlationId but no correlation id is taken from HttpResponse
155 * run method should not do anything with the map and not run any thread to notify camunda process
158 public void correlationIdIsNotFoundInHttpResponse() throws IOException {
159 when(httpClientMock.execute(any(HttpGet.class))).
160 thenReturn(createResponse(JSON_EXAMPLE_WITH_NO_CORRELATION_ID));
161 testedObjectInnerClassThread.run();
162 verifyZeroInteractions(threadMockToNotifyCamundaFlow, executorMock);
165 private void setPrivateField() throws NoSuchFieldException, IllegalAccessException {
166 Field httpClientField = testedObject.getClass().getDeclaredField("httpClient");
167 httpClientField.setAccessible(true);
168 httpClientField.set(testedObject, httpClientMock);
169 httpClientField.setAccessible(false);
171 Field executorField = testedObject.getClass().getDeclaredField("executor");
172 executorField.setAccessible(true);
173 executorField.set(testedObject, executorMock);
174 executorField.setAccessible(false);
176 Field pnfCorrelationToThreadMapField = testedObject.getClass()
177 .getDeclaredField("pnfCorrelationIdToThreadMap");
178 pnfCorrelationToThreadMapField.setAccessible(true);
179 Map<String, Runnable> pnfCorrelationToThreadMap = new ConcurrentHashMap<>();
180 pnfCorrelationToThreadMap.put(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) throws UnsupportedEncodingException {
190 HttpEntity entity = new StringEntity(json);
191 ProtocolVersion protocolVersion = new ProtocolVersion("", 1, 1);
192 HttpResponse response = new BasicHttpResponse(protocolVersion, 1, "");
193 response.setEntity(entity);
194 response.setStatusCode(200);