15c06d06940e96c7d5b23279ddd76587f10c47f7
[so.git] / bpmn / so-bpmn-infrastructure-common / src / test / java / org / onap / so / bpmn / infrastructure / pnf / dmaap / PnfEventReadyDmaapClientTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
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=========================================================
21  */
22
23 package org.onap.so.bpmn.infrastructure.pnf.dmaap;
24
25
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;
36 import java.util.Map;
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;
55
56 @RunWith(MockitoJUnitRunner.class)
57 public class PnfEventReadyDmaapClientTest {
58
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\"}]";
63
64     private static final String JSON_EXAMPLE_WITH_NO_PNF_CORRELATION_ID = "[{\"key1\":\"value1\"}]";
65
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;
74
75     @Mock
76     private Environment env;
77     private PnfEventReadyDmaapClient testedObject;
78
79     private DmaapTopicListenerThread testedObjectInnerClassThread;
80     private HttpClient httpClientMock;
81     private Runnable threadMockToNotifyCamundaFlow;
82     private ScheduledThreadPoolExecutor executorMock;
83
84     @Before
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);
100         setPrivateField();
101     }
102
103     /**
104      * Test run method, where the are following conditions:
105      * <p>
106      * - DmaapThreadListener is running, flag is set to true
107      * <p>
108      * - map is filled with one entry with the key that we get from response
109      * <p>
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
112      */
113     @Ignore
114     @Test
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());
121
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 + "");
127
128         verify(threadMockToNotifyCamundaFlow).run();
129         verify(executorMock).shutdown();
130     }
131
132     /**
133      * Test run method, where the are following conditions:
134      * <p>
135      * - DmaapThreadListener is running, flag is set to true
136      * <p>
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
139      */
140     @Ignore
141     @Test
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);
147     }
148
149     /**
150      * Test run method, where the are following conditions:
151      * <p>
152      * - DmaapThreadListener is running, flag is set to true
153      * <p>
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
156      */
157     @Ignore
158     @Test
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);
164     }
165
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);
171
172         Field executorField = testedObject.getClass().getDeclaredField("executor");
173         executorField.setAccessible(true);
174         executorField.set(testedObject, executorMock);
175         executorField.setAccessible(false);
176
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);
182
183         Field threadRunFlag = testedObject.getClass().getDeclaredField("dmaapThreadListenerIsRunning");
184         threadRunFlag.setAccessible(true);
185         threadRunFlag.set(testedObject, true);
186         threadRunFlag.setAccessible(false);
187     }
188
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);
195         return response;
196     }
197
198 }