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