393730ed4368dc9b5881c6c8d137fea70c0f96d8
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.openecomp.mso.bpmn.infrastructure.pnf.dmaap;
22
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;
29
30 import java.io.IOException;
31 import java.io.UnsupportedEncodingException;
32 import java.lang.reflect.Field;
33 import java.util.Map;
34 import java.util.concurrent.ConcurrentHashMap;
35 import java.util.concurrent.ScheduledExecutorService;
36 import org.apache.http.HttpEntity;
37 import org.apache.http.HttpResponse;
38 import org.apache.http.ProtocolVersion;
39 import org.apache.http.client.HttpClient;
40 import org.apache.http.client.methods.HttpGet;
41 import org.apache.http.entity.StringEntity;
42 import org.apache.http.message.BasicHttpResponse;
43 import org.junit.Before;
44 import org.junit.Test;
45 import org.mockito.ArgumentCaptor;
46 import org.openecomp.mso.bpmn.infrastructure.pnf.dmaap.PnfEventReadyDmaapClient.DmaapTopicListenerThread;
47
48 public class PnfEventReadyDmaapClientTest {
49
50     private static final String CORRELATION_ID = "corrTestId";
51     private static final String CORRELATION_ID_NOT_FOUND_IN_MAP = "otherCorrId";
52     private static final String JSON_EXAMPLE_WITH_CORRELATION_ID =
53             "{\"pnfRegistrationFields\":{\"correlationId\":\"%s\"}}";
54     private static final String JSON_EXAMPLE_WITH_NO_CORRELATION_ID =
55             "{\"pnfRegistrationFields\":{\"field\":\"value\"}}";
56
57     private static final String HOST = "hostTest";
58     private static final int PORT = 1234;
59     private static final String PROTOCOL = "http";
60     private static final String URI_PATH_PREFIX = "eventsForTesting";
61     private static final String EVENT_TOPIC_TEST = "eventTopicTest";
62     private static final String CONSUMER_ID = "consumerTestId";
63     private static final String CONSUMER_GROUP = "consumerGroupTest";
64
65     private PnfEventReadyDmaapClient testedObject;
66     private DmaapTopicListenerThread testedObjectInnerClassThread;
67     private HttpClient httpClientMock;
68     private Runnable threadMockToNotifyCamundaFlow;
69     private ScheduledExecutorService executorMock;
70
71     @Before
72     public void init() throws NoSuchFieldException, IllegalAccessException {
73         testedObject = new PnfEventReadyDmaapClient();
74         testedObject.setDmaapHost(HOST);
75         testedObject.setDmaapPort(PORT);
76         testedObject.setDmaapProtocol(PROTOCOL);
77         testedObject.setDmaapUriPathPrefix(URI_PATH_PREFIX);
78         testedObject.setDmaapTopicName(EVENT_TOPIC_TEST);
79         testedObject.setConsumerId(CONSUMER_ID);
80         testedObject.setConsumerGroup(CONSUMER_GROUP);
81         testedObject.setDmaapClientDelayInSeconds(1);
82         testedObject.init();
83         testedObjectInnerClassThread = testedObject.new DmaapTopicListenerThread();
84         httpClientMock = mock(HttpClient.class);
85         threadMockToNotifyCamundaFlow = mock(Runnable.class);
86         executorMock = mock(ScheduledExecutorService.class);
87         setPrivateField();
88     }
89
90     /**
91      * Test run method, where the are following conditions:
92      * <p> - DmaapThreadListener is running, flag is set to true
93      * <p> - map is filled with one entry with the key that we get from response
94      * <p> run method should invoke thread from map to notify camunda process, remove element from the map (map is empty)
95      * and shutdown the executor because of empty map
96      */
97     @Test
98     public void correlationIdIsFoundInHttpResponse_notifyAboutPnfReady()
99             throws IOException {
100         when(httpClientMock.execute(any(HttpGet.class))).
101                 thenReturn(createResponse(String.format(JSON_EXAMPLE_WITH_CORRELATION_ID, CORRELATION_ID)));
102         testedObjectInnerClassThread.run();
103         ArgumentCaptor<HttpGet> captor1 = ArgumentCaptor.forClass(HttpGet.class);
104         verify(httpClientMock).execute(captor1.capture());
105         assertThat(captor1.getValue().getURI()).hasHost(HOST).hasPort(PORT).hasScheme(PROTOCOL)
106                 .hasPath(
107                         "/" + URI_PATH_PREFIX + "/" + EVENT_TOPIC_TEST + "/" + CONSUMER_GROUP + "/" + CONSUMER_ID + "");
108         verify(threadMockToNotifyCamundaFlow).run();
109         verify(executorMock).shutdownNow();
110     }
111
112     /**
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 correlationId that does not match to correlationId
116      * taken from http response. run method should not do anything with the map not run any thread to
117      * notify camunda process
118      */
119     @Test
120     public void correlationIdIsFoundInHttpResponse_NotFoundInMap()
121             throws IOException {
122         when(httpClientMock.execute(any(HttpGet.class))).
123                 thenReturn(createResponse(
124                         String.format(JSON_EXAMPLE_WITH_CORRELATION_ID, CORRELATION_ID_NOT_FOUND_IN_MAP)));
125         testedObjectInnerClassThread.run();
126         verifyZeroInteractions(threadMockToNotifyCamundaFlow, executorMock);
127     }
128
129     /**
130      * Test run method, where the are following conditions:
131      * <p> - DmaapThreadListener is running, flag is set to true
132      * <p> - map is filled with one entry with the correlationId but no correlation id is taken from HttpResponse
133      * run method should not do anything with the map and not run any thread to notify camunda process
134      */
135     @Test
136     public void correlationIdIsNotFoundInHttpResponse() throws IOException {
137         when(httpClientMock.execute(any(HttpGet.class))).
138                 thenReturn(createResponse(JSON_EXAMPLE_WITH_NO_CORRELATION_ID));
139         testedObjectInnerClassThread.run();
140         verifyZeroInteractions(threadMockToNotifyCamundaFlow, executorMock);
141     }
142
143     private void setPrivateField() throws NoSuchFieldException, IllegalAccessException {
144         Field httpClientField = testedObject.getClass().getDeclaredField("httpClient");
145         httpClientField.setAccessible(true);
146         httpClientField.set(testedObject, httpClientMock);
147         httpClientField.setAccessible(false);
148
149         Field executorField = testedObject.getClass().getDeclaredField("executor");
150         executorField.setAccessible(true);
151         executorField.set(testedObject, executorMock);
152         executorField.setAccessible(false);
153
154         Field pnfCorrelationToThreadMapField = testedObject.getClass()
155                 .getDeclaredField("pnfCorrelationIdToThreadMap");
156         pnfCorrelationToThreadMapField.setAccessible(true);
157         Map<String, Runnable> pnfCorrelationToThreadMap = new ConcurrentHashMap<>();
158         pnfCorrelationToThreadMap.put(CORRELATION_ID, threadMockToNotifyCamundaFlow);
159         pnfCorrelationToThreadMapField.set(testedObject, pnfCorrelationToThreadMap);
160
161         Field threadRunFlag = testedObject.getClass().getDeclaredField("dmaapThreadListenerIsRunning");
162         threadRunFlag.setAccessible(true);
163         threadRunFlag.set(testedObject, true);
164         threadRunFlag.setAccessible(false);
165     }
166
167     private HttpResponse createResponse(String json) throws UnsupportedEncodingException {
168         HttpEntity entity = new StringEntity(json);
169         ProtocolVersion protocolVersion = new ProtocolVersion("", 1, 1);
170         HttpResponse response = new BasicHttpResponse(protocolVersion, 1, "");
171         response.setEntity(entity);
172         response.setStatusCode(200);
173         return response;
174     }
175
176 }