aboutsummaryrefslogtreecommitdiffstats
path: root/bpmn/so-bpmn-infrastructure-common/src/test/java/org/onap/so/bpmn/infrastructure/pnf/dmaap/PnfEventReadyDmaapClientTest.java
blob: bbb6aad49b1aa781af367c038a34c45760a8701f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*-
 * ============LICENSE_START=======================================================
 * ONAP - SO
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Copyright (C) 2018 Nokia.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.so.bpmn.infrastructure.pnf.dmaap;


import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.ProtocolVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.message.BasicHttpResponse;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.onap.so.bpmn.infrastructure.pnf.dmaap.PnfEventReadyDmaapClient.DmaapTopicListenerThread;
import org.springframework.core.env.Environment;

@RunWith(MockitoJUnitRunner.class)
public class PnfEventReadyDmaapClientTest {

    private static final String PNF_CORRELATION_ID = "corrTestId";
    private static final String PNF_CORRELATION_ID_NOT_FOUND_IN_MAP = "otherCorrId";
    private static final String JSON_EXAMPLE_WITH_PNF_CORRELATION_ID = "[{\"correlationId\": \"%s\","
            + "\"value\":\"value1\"},{\"correlationId\": \"corr\",\"value\":\"value2\"}]";

    private static final String JSON_EXAMPLE_WITH_NO_PNF_CORRELATION_ID = "[{\"key1\":\"value1\"}]";

    private static final String HOST = "hostTest";
    private static final int PORT = 1234;
    private static final String PROTOCOL = "http";
    private static final String URI_PATH_PREFIX = "eventsForTesting";
    private static final String TOPIC_NAME = "unauthenticated.PNF_READY";
    private static final String TOPIC_NAME_UPDATE = "unauthenticated.PNF_UPDATE";
    private static final String CONSUMER_ID = "so-bpmn-infra-pnfready";
    private static final String CONSUMER_ID_UPDATE = "so-bpmn-infra-pnfupdate";
    private static final String CONSUMER_GROUP = "so-consumer";
    private static final int TOPIC_LISTENER_DELAY_IN_SECONDS = 5;

    @Mock
    private Environment env;
    private PnfEventReadyDmaapClient testedObject;

    private DmaapTopicListenerThread testedObjectInnerClassThread;
    private HttpClient httpClientMock;
    private Runnable threadMockToNotifyCamundaFlow;
    private ScheduledThreadPoolExecutor executorMock;

    @Before
    public void init() throws NoSuchFieldException, IllegalAccessException {
        when(env.getProperty(eq("pnf.dmaap.port"), eq(Integer.class))).thenReturn(PORT);
        when(env.getProperty(eq("pnf.dmaap.host"))).thenReturn(HOST);
        when(env.getProperty(eq("pnf.dmaap.protocol"))).thenReturn(PROTOCOL);
        when(env.getProperty(eq("pnf.dmaap.uriPathPrefix"))).thenReturn(URI_PATH_PREFIX);
        when(env.getProperty(eq("pnf.dmaap.pnfReadyTopicName"))).thenReturn(TOPIC_NAME);
        when(env.getProperty(eq("pnf.dmaap.pnfUpdateTopicName"))).thenReturn(TOPIC_NAME_UPDATE);
        when(env.getProperty(eq("pnf.dmaap.consumerId"))).thenReturn(CONSUMER_ID);
        when(env.getProperty(eq("pnf.dmaap.consumerIdUpdate"))).thenReturn(CONSUMER_ID_UPDATE);
        when(env.getProperty(eq("pnf.dmaap.consumerGroup"))).thenReturn(CONSUMER_GROUP);
        when(env.getProperty(eq("pnf.dmaap.topicListenerDelayInSeconds"), eq(Integer.class)))
                .thenReturn(TOPIC_LISTENER_DELAY_IN_SECONDS);
        testedObject = new PnfEventReadyDmaapClient(env);
        testedObjectInnerClassThread = testedObject.new DmaapTopicListenerThread();
        httpClientMock = mock(HttpClient.class);
        threadMockToNotifyCamundaFlow = mock(Runnable.class);
        executorMock = mock(ScheduledThreadPoolExecutor.class);
        setPrivateField();
    }

    /**
     * Test run method, where the are following conditions:
     * <p>
     * - DmaapThreadListener is running, flag is set to true
     * <p>
     * - map is filled with one entry with the key that we get from response
     * <p>
     * run method should invoke thread from map to notify camunda process, remove element from the map (map is empty)
     * and shutdown the executor because of empty map
     */
    @Test
    public void pnfCorrelationIdIsFoundInHttpResponse_notifyAboutPnfUpdate() throws IOException {
        when(httpClientMock.execute(any(HttpGet.class)))
                .thenReturn(createResponse(String.format(JSON_EXAMPLE_WITH_PNF_CORRELATION_ID, PNF_CORRELATION_ID)));
        testedObjectInnerClassThread.run();
        ArgumentCaptor<HttpGet> captor1 = ArgumentCaptor.forClass(HttpGet.class);
        verify(httpClientMock).execute(captor1.capture());
        assertEquals(captor1.getValue().getURI().getHost(), HOST);
        assertEquals(captor1.getValue().getURI().getPort(), PORT);
        assertEquals(captor1.getValue().getURI().getScheme(), PROTOCOL);
        assertEquals(captor1.getValue().getURI().getPath(),
                "/" + URI_PATH_PREFIX + "/" + TOPIC_NAME_UPDATE + "/" + CONSUMER_GROUP + "/" + CONSUMER_ID_UPDATE + "");

        verify(threadMockToNotifyCamundaFlow).run();
        verify(executorMock).shutdown();
    }


    @Test
    public void pnfCorrelationIdIsFoundInHttpResponse_notifyAboutPnfReady() throws IOException {
        ArgumentCaptor<HttpGet> captor1 = ArgumentCaptor.forClass(HttpGet.class);
        when(httpClientMock.execute(any(HttpGet.class)))
                .thenReturn(createResponse_forReady(
                        String.format(JSON_EXAMPLE_WITH_PNF_CORRELATION_ID, PNF_CORRELATION_ID)))
                .thenReturn(createResponse(String.format(JSON_EXAMPLE_WITH_PNF_CORRELATION_ID, PNF_CORRELATION_ID)));
        testedObjectInnerClassThread.run();
        verify(httpClientMock, times(2)).execute(captor1.capture());
        assertEquals(captor1.getValue().getURI().getHost(), HOST);
        assertEquals(captor1.getValue().getURI().getPort(), PORT);
        assertEquals(captor1.getValue().getURI().getScheme(), PROTOCOL);

        assertEquals(captor1.getValue().getURI().getPath(),
                "/" + URI_PATH_PREFIX + "/" + TOPIC_NAME + "/" + CONSUMER_GROUP + "/" + CONSUMER_ID + "");


        verify(threadMockToNotifyCamundaFlow).run();
        verify(executorMock).shutdown();
    }


    /**
     * Test run method, where the are following conditions:
     * <p>
     * - DmaapThreadListener is running, flag is set to true
     * <p>
     * - map is filled with one entry with the pnfCorrelationId that does not match to pnfCorrelationId taken from http
     * response. run method should not do anything with the map not run any thread to notify camunda process
     */
    @Test
    public void pnfCorrelationIdIsFoundInHttpResponse_NotFoundInMap() throws IOException {
        when(httpClientMock.execute(any(HttpGet.class))).thenReturn(createResponse(
                String.format(JSON_EXAMPLE_WITH_PNF_CORRELATION_ID, PNF_CORRELATION_ID_NOT_FOUND_IN_MAP)));
        testedObjectInnerClassThread.run();
        verifyZeroInteractions(threadMockToNotifyCamundaFlow, executorMock);
    }

    /**
     * Test run method, where the are following conditions:
     * <p>
     * - DmaapThreadListener is running, flag is set to true
     * <p>
     * - map is filled with one entry with the pnfCorrelationId but no correlation id is taken from HttpResponse run
     * method should not do anything with the map and not run any thread to notify camunda process
     */
    @Test
    public void pnfCorrelationIdIsNotFoundInHttpResponse() throws IOException {
        when(httpClientMock.execute(any(HttpGet.class)))
                .thenReturn(createResponse(JSON_EXAMPLE_WITH_NO_PNF_CORRELATION_ID));
        testedObjectInnerClassThread.run();
        verifyZeroInteractions(threadMockToNotifyCamundaFlow, executorMock);
    }

    private void setPrivateField() throws NoSuchFieldException, IllegalAccessException {
        Field httpClientField = testedObject.getClass().getDeclaredField("httpClient");
        httpClientField.setAccessible(true);
        httpClientField.set(testedObject, httpClientMock);
        httpClientField.setAccessible(false);

        Field executorField = testedObject.getClass().getDeclaredField("executor");
        executorField.setAccessible(true);
        executorField.set(testedObject, executorMock);
        executorField.setAccessible(false);

        Field pnfCorrelationToThreadMapField = testedObject.getClass().getDeclaredField("pnfCorrelationIdToThreadMap");
        pnfCorrelationToThreadMapField.setAccessible(true);
        Map<String, Runnable> pnfCorrelationToThreadMap = new ConcurrentHashMap<>();
        pnfCorrelationToThreadMap.put(PNF_CORRELATION_ID, threadMockToNotifyCamundaFlow);
        pnfCorrelationToThreadMapField.set(testedObject, pnfCorrelationToThreadMap);

        Field threadRunFlag = testedObject.getClass().getDeclaredField("dmaapThreadListenerIsRunning");
        threadRunFlag.setAccessible(true);
        threadRunFlag.set(testedObject, true);
        threadRunFlag.setAccessible(false);
    }

    private HttpResponse createResponse(String json) {
        HttpEntity entity = new InputStreamEntity(new ByteArrayInputStream(json.getBytes()));
        ProtocolVersion protocolVersion = new ProtocolVersion("", 1, 1);
        HttpResponse response = new BasicHttpResponse(protocolVersion, 1, "");
        response.setEntity(entity);
        response.setStatusCode(200);
        return response;
    }

    private HttpResponse createResponse_forReady(String json) {
        HttpEntity entity = new InputStreamEntity(new ByteArrayInputStream(json.getBytes()));
        ProtocolVersion protocolVersion = new ProtocolVersion("", 1, 1);
        HttpResponse response = new BasicHttpResponse(protocolVersion, 1, "");
        response.setEntity(entity);
        response.setStatusCode(500);
        return response;
    }

}