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.openecomp.mso.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.Mockito.mock;
26 import static org.mockito.Mockito.verify;
27 import static org.mockito.Mockito.verifyZeroInteractions;
28 import static org.mockito.Mockito.when;
30 import java.io.IOException;
31 import java.io.UnsupportedEncodingException;
32 import java.lang.reflect.Field;
33 import java.util.HashMap;
35 import java.util.concurrent.ConcurrentHashMap;
36 import java.util.concurrent.ScheduledExecutorService;
37 import org.apache.http.HttpEntity;
38 import org.apache.http.HttpResponse;
39 import org.apache.http.ProtocolVersion;
40 import org.apache.http.client.HttpClient;
41 import org.apache.http.client.methods.HttpGet;
42 import org.apache.http.entity.StringEntity;
43 import org.apache.http.message.BasicHttpResponse;
44 import org.junit.Before;
45 import org.junit.Test;
46 import org.junit.runner.RunWith;
47 import org.mockito.ArgumentCaptor;
48 import org.openecomp.mso.bpmn.core.PropertyConfiguration;
49 import org.openecomp.mso.bpmn.infrastructure.pnf.dmaap.PnfEventReadyDmaapClient.DmaapTopicListenerThread;
50 import org.powermock.api.mockito.PowerMockito;
51 import org.powermock.core.classloader.annotations.PowerMockIgnore;
52 import org.powermock.core.classloader.annotations.PrepareForTest;
53 import org.powermock.modules.junit4.PowerMockRunner;
55 @RunWith(PowerMockRunner.class)
56 @PrepareForTest({PropertyConfiguration.class})
57 @PowerMockIgnore("javax.net.ssl.*")
58 public class PnfEventReadyDmaapClientTest {
60 private static final String CORRELATION_ID = "corrTestId";
61 private static final String CORRELATION_ID_NOT_FOUND_IN_MAP = "otherCorrId";
62 private static final String JSON_EXAMPLE_WITH_CORRELATION_ID = "[\n"
64 + " \"pnfRegistrationFields\" : {\n"
65 + " \"correlationId\" : \"%s\",\n"
66 + " \"value\" : \"value1\"\n"
70 + " \"pnfRegistrationFields\" : {\n"
71 + " \"correlationId\" : \"corr\",\n"
72 + " \"value\" : \"value2\"\n"
76 private static final String JSON_EXAMPLE_WITH_NO_CORRELATION_ID =
77 "{\"pnfRegistrationFields\":{\"field\":\"value\"}}";
79 private static final String HOST = "hostTest";
80 private static final int PORT = 1234;
81 private static final String PROTOCOL = "http";
82 private static final String URI_PATH_PREFIX = "eventsForTesting";
83 private static final String EVENT_TOPIC_TEST = "eventTopicTest";
84 private static final String CONSUMER_ID = "consumerTestId";
85 private static final String CONSUMER_GROUP = "consumerGroupTest";
87 private PnfEventReadyDmaapClient testedObject;
88 private DmaapTopicListenerThread testedObjectInnerClassThread;
89 private HttpClient httpClientMock;
90 private Runnable threadMockToNotifyCamundaFlow;
91 private ScheduledExecutorService executorMock;
94 public void init() throws NoSuchFieldException, IllegalAccessException {
95 PowerMockito.mockStatic(PropertyConfiguration.class);
96 PropertyConfiguration propertyConfigurationMock = mock(PropertyConfiguration.class);
97 PowerMockito.when(PropertyConfiguration.getInstance()).thenReturn(propertyConfigurationMock);
98 when(propertyConfigurationMock.getProperties(PropertyConfiguration.MSO_BPMN_URN_PROPERTIES)).thenReturn(
100 testedObject = new PnfEventReadyDmaapClient();
101 testedObject.setDmaapProtocol(PROTOCOL);
102 testedObject.setDmaapUriPathPrefix(URI_PATH_PREFIX);
103 testedObject.setDmaapTopicName(EVENT_TOPIC_TEST);
104 testedObject.setConsumerId(CONSUMER_ID);
105 testedObject.setConsumerGroup(CONSUMER_GROUP);
106 testedObject.setDmaapClientDelayInSeconds(1);
108 testedObjectInnerClassThread = testedObject.new DmaapTopicListenerThread();
109 httpClientMock = mock(HttpClient.class);
110 threadMockToNotifyCamundaFlow = mock(Runnable.class);
111 executorMock = mock(ScheduledExecutorService.class);
116 * Test run method, where the are following conditions:
117 * <p> - DmaapThreadListener is running, flag is set to true
118 * <p> - map is filled with one entry with the key that we get from response
119 * <p> run method should invoke thread from map to notify camunda process, remove element from the map (map is
120 * empty) and shutdown the executor because of empty map
123 public void correlationIdIsFoundInHttpResponse_notifyAboutPnfReady()
125 when(httpClientMock.execute(any(HttpGet.class))).
126 thenReturn(createResponse(String.format(JSON_EXAMPLE_WITH_CORRELATION_ID, CORRELATION_ID)));
127 testedObjectInnerClassThread.run();
128 ArgumentCaptor<HttpGet> captor1 = ArgumentCaptor.forClass(HttpGet.class);
129 verify(httpClientMock).execute(captor1.capture());
130 assertThat(captor1.getValue().getURI()).hasHost(HOST).hasPort(PORT).hasScheme(PROTOCOL)
132 "/" + URI_PATH_PREFIX + "/" + EVENT_TOPIC_TEST + "/" + CONSUMER_GROUP + "/" + CONSUMER_ID + "");
133 verify(threadMockToNotifyCamundaFlow).run();
134 verify(executorMock).shutdownNow();
138 * Test run method, where the are following conditions:
139 * <p> - DmaapThreadListener is running, flag is set to true
140 * <p> - map is filled with one entry with the correlationId that does not match to correlationId
141 * taken from http response. run method should not do anything with the map not run any thread to notify camunda
145 public void correlationIdIsFoundInHttpResponse_NotFoundInMap()
147 when(httpClientMock.execute(any(HttpGet.class))).
148 thenReturn(createResponse(
149 String.format(JSON_EXAMPLE_WITH_CORRELATION_ID, CORRELATION_ID_NOT_FOUND_IN_MAP)));
150 testedObjectInnerClassThread.run();
151 verifyZeroInteractions(threadMockToNotifyCamundaFlow, executorMock);
155 * Test run method, where the are following conditions:
156 * <p> - DmaapThreadListener is running, flag is set to true
157 * <p> - map is filled with one entry with the correlationId but no correlation id is taken from HttpResponse
158 * run method should not do anything with the map and not run any thread to notify camunda process
161 public void correlationIdIsNotFoundInHttpResponse() throws IOException {
162 when(httpClientMock.execute(any(HttpGet.class))).
163 thenReturn(createResponse(JSON_EXAMPLE_WITH_NO_CORRELATION_ID));
164 testedObjectInnerClassThread.run();
165 verifyZeroInteractions(threadMockToNotifyCamundaFlow, executorMock);
168 private void setPrivateField() throws NoSuchFieldException, IllegalAccessException {
169 Field httpClientField = testedObject.getClass().getDeclaredField("httpClient");
170 httpClientField.setAccessible(true);
171 httpClientField.set(testedObject, httpClientMock);
172 httpClientField.setAccessible(false);
174 Field executorField = testedObject.getClass().getDeclaredField("executor");
175 executorField.setAccessible(true);
176 executorField.set(testedObject, executorMock);
177 executorField.setAccessible(false);
179 Field pnfCorrelationToThreadMapField = testedObject.getClass()
180 .getDeclaredField("pnfCorrelationIdToThreadMap");
181 pnfCorrelationToThreadMapField.setAccessible(true);
182 Map<String, Runnable> pnfCorrelationToThreadMap = new ConcurrentHashMap<>();
183 pnfCorrelationToThreadMap.put(CORRELATION_ID, threadMockToNotifyCamundaFlow);
184 pnfCorrelationToThreadMapField.set(testedObject, pnfCorrelationToThreadMap);
186 Field threadRunFlag = testedObject.getClass().getDeclaredField("dmaapThreadListenerIsRunning");
187 threadRunFlag.setAccessible(true);
188 threadRunFlag.set(testedObject, true);
189 threadRunFlag.setAccessible(false);
192 private Map<String, String> createProperties() {
193 Map<String, String> map = new HashMap<>();
194 map.put("mso.dmaap.host", HOST);
195 map.put("mso.dmaap.port", String.valueOf(PORT));
199 private HttpResponse createResponse(String json) throws UnsupportedEncodingException {
200 HttpEntity entity = new StringEntity(json);
201 ProtocolVersion protocolVersion = new ProtocolVersion("", 1, 1);
202 HttpResponse response = new BasicHttpResponse(protocolVersion, 1, "");
203 response.setEntity(entity);
204 response.setStatusCode(200);