aboutsummaryrefslogtreecommitdiffstats
path: root/sdc-distribution-client/src/test/java/org/onap/sdc/http/SdcConnectorClientTest.java
blob: 20228e0043547ef452186cbb0d008351cf15e4dd (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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
 * Modifications copyright (C) 2019 Nokia. All rights reserved.
 * ================================================================================
 * 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.sdc.http;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import com.google.common.hash.Hashing;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.mockito.Matchers;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.onap.sdc.api.notification.IArtifactInfo;
import org.onap.sdc.api.results.IDistributionClientResult;
import org.onap.sdc.impl.DistributionClientResultImpl;
import org.onap.sdc.api.asdc.RegistrationRequest;
import org.onap.sdc.api.consumer.IConfiguration;
import org.onap.sdc.utils.DistributionActionResultEnum;
import org.onap.sdc.utils.Pair;

import com.att.nsa.apiClient.credentials.ApiCredential;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import fj.data.Either;

public class SdcConnectorClientTest {

    private static Gson gson = new GsonBuilder().create();
    private static final String MOCK_ENV = "MockEnv";
    private static final String MOCK_API_KEY = "MockApikey";
    private static HttpAsdcClient httpClient = mock(HttpAsdcClient.class);
    private static IConfiguration configuration = mock(IConfiguration.class);
    private static ApiCredential apiCredential = mock(ApiCredential.class);
    private static HttpAsdcResponse httpAsdcResponse = mock(HttpAsdcResponse.class);
    @SuppressWarnings("unchecked")
    private static Either<TopicRegistrationResponse, DistributionClientResultImpl> mockResponse =
            Mockito.mock(Either.class);
    private static Map<String, String> mockHeaders = new HashMap<>();
    Pair<HttpAsdcResponse, CloseableHttpResponse> mockPair = new Pair<>(httpAsdcResponse, null);
    private HttpEntity lastHttpEntity = null;

    private static SdcConnectorClient asdcClient = Mockito.spy(new SdcConnectorClient());

    private static final String ARTIFACT_URL = "http://127.0.0.1/artifact/url";
    private static final String IT_JUST_DIDN_T_WORK = "It just didn't work";
    private static final List<String> ARTIFACT_TYPES = Arrays.asList("Service", "Resource", "VF", "VFC");
    private static final String VALID_JSON_PAYLOAD = gson.toJson(ARTIFACT_TYPES);
    private static final int PORT = 49512;
    private static final byte[] BYTES = new byte[] {0xA, 0xB, 0xC, 0xD};


    @BeforeClass
    public static void beforeClass() {
        asdcClient.setConfiguration(configuration);
        asdcClient.setHttpClient(httpClient);
        when(apiCredential.getApiKey()).thenReturn(MOCK_API_KEY);
        when(httpAsdcResponse.getStatus()).thenReturn(HttpStatus.SC_OK);

        doReturn(mockHeaders).when(asdcClient).addHeadersToHttpRequest(Mockito.anyString());
        doReturn(mockResponse).when(asdcClient).parseRegistrationResponse(httpAsdcResponse);
    }

    @Before
    public void beforeMethod() {
        Mockito.reset(configuration, httpClient);
        lastHttpEntity = null;
        when(configuration.getEnvironmentName()).thenReturn(MOCK_ENV);


        doAnswer(new Answer<Pair<HttpAsdcResponse, CloseableHttpResponse>>() {
            @Override
            public Pair<HttpAsdcResponse, CloseableHttpResponse> answer(InvocationOnMock invocation) throws Throwable {
                lastHttpEntity = invocation.getArgumentAt(1, HttpEntity.class);
                return mockPair;
            }
        }).when(httpClient).postRequest(Mockito.eq(AsdcUrls.POST_FOR_TOPIC_REGISTRATION), Mockito.any(HttpEntity.class),
                Mockito.eq(mockHeaders), Mockito.eq(false));
    }

    @Test(expected = IllegalStateException.class)
    public void initAndCloseTest() {
        IConfiguration conf = Mockito.mock(IConfiguration.class);
        when(conf.getUser()).thenReturn("user");
        when(conf.getPassword()).thenReturn("password");

        when(conf.activateServerTLSAuth()).thenReturn(false);
        SdcConnectorClient client = new SdcConnectorClient();
        client.init(conf);
        assertNotNull(client.getHttpClient());
        client.close();

        //check if client is really closed
        client.getHttpClient().getRequest(AsdcUrls.POST_FOR_TOPIC_REGISTRATION, new HashMap<>());
    }


    @Test
    public void getConfigurationTest() {
        IConfiguration conf = asdcClient.getConfiguration();
        assertEquals(configuration, conf);
    }

    @Test
    public void getHttpClientTest() {
        HttpAsdcClient httpAsdcClient = asdcClient.getHttpClient();
        assertEquals(httpClient, httpAsdcClient);
    }

    @Test
    public void testConsumeProduceStatusTopicFalse() throws UnsupportedOperationException, IOException {

        testConsumeProduceStatusTopic(false);

    }

    @Test
    public void testConsumeProduceStatusTopicTrue() throws UnsupportedOperationException, IOException {

        testConsumeProduceStatusTopic(true);

    }

    private void testConsumeProduceStatusTopic(final boolean isConsumeProduceStatusFlag) throws IOException {
        when(configuration.isConsumeProduceStatusTopic()).thenReturn(isConsumeProduceStatusFlag);
        asdcClient.registerAsdcTopics(apiCredential);
        verify(httpClient, times(1))
                .postRequest(Mockito.eq(AsdcUrls.POST_FOR_TOPIC_REGISTRATION), any(HttpEntity.class),
                        Mockito.eq(mockHeaders), Mockito.eq(false));
        assertNotNull(lastHttpEntity);
        RegistrationRequest actualRegRequest =
                gson.fromJson(IOUtils.toString(lastHttpEntity.getContent(), StandardCharsets.UTF_8),
                        RegistrationRequest.class);
        RegistrationRequest expectedRegRequest =
                gson.fromJson(excpectedStringBody(isConsumeProduceStatusFlag), RegistrationRequest.class);

        assertTrue(actualRegRequest.getApiPublicKey().equals(expectedRegRequest.getApiPublicKey()));
        assertTrue(actualRegRequest.getDistrEnvName().equals(expectedRegRequest.getDistrEnvName()));
        assertTrue(actualRegRequest.getIsConsumerToSdcDistrStatusTopic()
                           .equals(expectedRegRequest.getIsConsumerToSdcDistrStatusTopic()));
    }

    @Test
    public void getValidArtifactTypesListHappyScenarioTest() throws IOException {
        HttpAsdcResponse responseMock = mock(HttpAsdcResponse.class);
        CloseableHttpResponse closeableHttpResponseMock = mock(CloseableHttpResponse.class);
        HttpEntity messageMock = mock(HttpEntity.class);
        Pair<HttpAsdcResponse, CloseableHttpResponse> responsePair =
                new Pair<>(responseMock, closeableHttpResponseMock);

        when(responseMock.getStatus()).thenReturn(HttpStatus.SC_OK);
        when(responseMock.getMessage()).thenReturn(messageMock);
        when(messageMock.getContent()).thenReturn(new ByteArrayInputStream(VALID_JSON_PAYLOAD.getBytes()));
        when(httpClient.getRequest(eq(AsdcUrls.GET_VALID_ARTIFACT_TYPES), Matchers.any(), eq(false)))
                .thenReturn(responsePair);

        Either<List<String>, IDistributionClientResult> result = asdcClient.getValidArtifactTypesList();
        assertTrue(result.isLeft());
        List<String> list = result.left().value();
        assertEquals(ARTIFACT_TYPES, list);
    }

    @Test
    public void getValidArtifactTypesListErrorResponseScenarioTest() throws IOException {
        HttpAsdcResponse responseMock = mock(HttpAsdcResponse.class);
        HttpEntity messageMock = mock(HttpEntity.class);
        Pair<HttpAsdcResponse, CloseableHttpResponse> responsePair = new Pair<>(responseMock, null);

        when(responseMock.getStatus()).thenReturn(HttpStatus.SC_GATEWAY_TIMEOUT);
        when(responseMock.getMessage()).thenReturn(messageMock);
        when(messageMock.getContent()).thenReturn(new ByteArrayInputStream(IT_JUST_DIDN_T_WORK.getBytes()));
        when(httpClient.getRequest(eq(AsdcUrls.GET_VALID_ARTIFACT_TYPES), Matchers.any(), eq(false)))
                .thenReturn(responsePair);

        Either<List<String>, IDistributionClientResult> result = asdcClient.getValidArtifactTypesList();
        assertTrue(result.isRight());
        IDistributionClientResult distributionClientResult = result.right().value();
        assertEquals(DistributionActionResultEnum.ASDC_SERVER_TIMEOUT,
                distributionClientResult.getDistributionActionResult());
    }


    @Test
    public void getValidArtifactTypesListExceptionDuringConnectionClosingTest() throws IOException {
        HttpAsdcResponse responseMock = mock(HttpAsdcResponse.class);
        CloseableHttpResponse closeableHttpResponseMock = mock(CloseableHttpResponse.class);
        HttpEntity messageMock = mock(HttpEntity.class);
        Pair<HttpAsdcResponse, CloseableHttpResponse> responsePair =
                new Pair<>(responseMock, closeableHttpResponseMock);

        when(responseMock.getStatus()).thenReturn(HttpStatus.SC_GATEWAY_TIMEOUT);
        when(responseMock.getMessage()).thenReturn(messageMock);
        when(messageMock.getContent()).thenReturn(new ByteArrayInputStream(VALID_JSON_PAYLOAD.getBytes()));
        when(httpClient.getRequest(eq(AsdcUrls.GET_VALID_ARTIFACT_TYPES), Matchers.any(), eq(false)))
                .thenReturn(responsePair);

        doThrow(new IOException("Test exception")).when(closeableHttpResponseMock).close();

        Either<List<String>, IDistributionClientResult> result = asdcClient.getValidArtifactTypesList();
        assertTrue(result.isRight());
        IDistributionClientResult distributionClientResult = result.right().value();
        assertEquals(DistributionActionResultEnum.ASDC_SERVER_TIMEOUT,
                distributionClientResult.getDistributionActionResult());
    }

    @Test
    public void getValidArtifactTypesListParsingExceptionHandlingTest() throws IOException {
        HttpAsdcResponse responseMock = mock(HttpAsdcResponse.class);
        CloseableHttpResponse closeableHttpResponseMock = mock(CloseableHttpResponse.class);
        HttpEntity messageMock = mock(HttpEntity.class);
        Pair<HttpAsdcResponse, CloseableHttpResponse> responsePair =
                new Pair<>(responseMock, closeableHttpResponseMock);

        when(responseMock.getStatus()).thenReturn(HttpStatus.SC_OK);
        when(responseMock.getMessage()).thenReturn(messageMock);
        when(messageMock.getContent()).thenReturn(new ThrowingInputStreamForTesting());
        when(httpClient.getRequest(eq(AsdcUrls.GET_VALID_ARTIFACT_TYPES), Matchers.any(), eq(false)))
                .thenReturn(responsePair);

        Either<List<String>, IDistributionClientResult> result = asdcClient.getValidArtifactTypesList();
        assertTrue(result.isRight());
        IDistributionClientResult distributionClientResult = result.right().value();
        assertEquals(DistributionActionResultEnum.GENERAL_ERROR,
                distributionClientResult.getDistributionActionResult());
    }

    @Test
    public void unregisterTopicsErrorDuringProcessingTest() throws IOException {
        when(configuration.getAsdcAddress()).thenReturn("127.0.0.1" + PORT);
        when(configuration.isConsumeProduceStatusTopic()).thenReturn(false);
        when(configuration.getMsgBusAddress())
                .thenReturn(Arrays.asList("http://127.0.0.1:45321/dmaap", "http://127.0.0.1:45321/dmaap"));

        doReturn(httpClient).when(asdcClient).createNewHttpClient();

        String failMessage = "It just didn't work";
        HttpAsdcResponse responseMock = mock(HttpAsdcResponse.class);
        HttpEntity messageMock = mock(HttpEntity.class);
        Pair<HttpAsdcResponse, CloseableHttpResponse> responsePair = new Pair<>(responseMock, null);

        when(responseMock.getStatus()).thenReturn(HttpStatus.SC_BAD_GATEWAY);
        when(responseMock.getMessage()).thenReturn(messageMock);
        when(messageMock.getContent()).thenReturn(new ByteArrayInputStream(failMessage.getBytes()));
        doReturn(responsePair).when(httpClient)
                .postRequest(eq(AsdcUrls.POST_FOR_UNREGISTER), any(HttpEntity.class), any(), eq(false));

        IDistributionClientResult result = asdcClient.unregisterTopics(apiCredential);
        assertEquals(DistributionActionResultEnum.ASDC_CONNECTION_FAILED, result.getDistributionActionResult());
    }

    @Test
    public void unregisterTopicsHappyScenarioTest() throws IOException {
        when(configuration.getAsdcAddress()).thenReturn("127.0.0.1" + PORT);
        when(configuration.isConsumeProduceStatusTopic()).thenReturn(false);

        doReturn(httpClient).when(asdcClient).createNewHttpClient();

        String failMessage = "";
        HttpAsdcResponse responseMock = mock(HttpAsdcResponse.class);
        HttpEntity messageMock = mock(HttpEntity.class);
        Pair<HttpAsdcResponse, CloseableHttpResponse> responsePair = new Pair<>(responseMock, null);

        when(responseMock.getStatus()).thenReturn(HttpStatus.SC_NO_CONTENT);
        when(responseMock.getMessage()).thenReturn(messageMock);
        when(messageMock.getContent()).thenReturn(new ByteArrayInputStream(failMessage.getBytes()));
        doReturn(responsePair).when(httpClient)
                .postRequest(eq(AsdcUrls.POST_FOR_UNREGISTER), any(HttpEntity.class), any(), eq(false));

        IDistributionClientResult result = asdcClient.unregisterTopics(apiCredential);
        assertEquals(DistributionActionResultEnum.SUCCESS, result.getDistributionActionResult());
    }

    @Test
    public void downloadArtifactHappyScenarioTest() throws IOException {
        Map<String, String> headers = new HashMap<>();
        headers.put(asdcClient.contentDispositionHeader, "SomeHeader");

        IArtifactInfo artifactInfo = mock(IArtifactInfo.class);
        when(artifactInfo.getArtifactURL()).thenReturn(ARTIFACT_URL);
        when(artifactInfo.getArtifactChecksum()).thenReturn(Hashing.md5().hashBytes(BYTES).toString());

        HttpAsdcResponse responseMock = mock(HttpAsdcResponse.class);
        HttpEntity messageMock = mock(HttpEntity.class);
        Pair<HttpAsdcResponse, CloseableHttpResponse> responsePair = new Pair<>(responseMock, null);

        when(responseMock.getStatus()).thenReturn(HttpStatus.SC_OK);
        when(responseMock.getMessage()).thenReturn(messageMock);
        when(responseMock.getHeadersMap()).thenReturn(headers);
        when(messageMock.getContent()).thenReturn(new ByteArrayInputStream(BYTES));
        doReturn(responsePair).when(httpClient).getRequest(eq(ARTIFACT_URL), any(), eq(false));

        IDistributionClientResult result = asdcClient.dowloadArtifact(artifactInfo);
        assertEquals(DistributionActionResultEnum.SUCCESS, result.getDistributionActionResult());
    }

    @Test
    public void downloadArtifactDataIntegrityProblemTest() throws IOException {
        IArtifactInfo artifactInfo = mock(IArtifactInfo.class);
        when(artifactInfo.getArtifactURL()).thenReturn(ARTIFACT_URL);

        HttpAsdcResponse responseMock = mock(HttpAsdcResponse.class);
        HttpEntity messageMock = mock(HttpEntity.class);
        Pair<HttpAsdcResponse, CloseableHttpResponse> responsePair = new Pair<>(responseMock, null);

        when(responseMock.getStatus()).thenReturn(HttpStatus.SC_OK);
        when(responseMock.getMessage()).thenReturn(messageMock);
        when(messageMock.getContent()).thenReturn(new ByteArrayInputStream(BYTES));
        doReturn(responsePair).when(httpClient).getRequest(eq(ARTIFACT_URL), any(), eq(false));

        IDistributionClientResult result = asdcClient.dowloadArtifact(artifactInfo);
        assertEquals(DistributionActionResultEnum.DATA_INTEGRITY_PROBLEM, result.getDistributionActionResult());
    }

    @Test
    public void downloadArtifactExceptionDuringDownloadHandlingTest() throws IOException {
        IArtifactInfo artifactInfo = mock(IArtifactInfo.class);
        when(artifactInfo.getArtifactURL()).thenReturn(ARTIFACT_URL);

        HttpAsdcResponse responseMock = mock(HttpAsdcResponse.class);
        HttpEntity messageMock = mock(HttpEntity.class);
        Pair<HttpAsdcResponse, CloseableHttpResponse> responsePair = new Pair<>(responseMock, null);

        when(responseMock.getStatus()).thenReturn(HttpStatus.SC_OK);
        when(responseMock.getMessage()).thenReturn(messageMock);
        when(messageMock.getContent()).thenReturn(new ThrowingInputStreamForTesting());
        doReturn(responsePair).when(httpClient).getRequest(eq(ARTIFACT_URL), any(), eq(false));

        IDistributionClientResult result = asdcClient.dowloadArtifact(artifactInfo);
        assertEquals(DistributionActionResultEnum.GENERAL_ERROR, result.getDistributionActionResult());
    }

    @Test
    public void downloadArtifactHandleDownloadErrorTest() throws IOException {
        IArtifactInfo artifactInfo = mock(IArtifactInfo.class);
        when(artifactInfo.getArtifactURL()).thenReturn(ARTIFACT_URL);

        HttpAsdcResponse responseMock = mock(HttpAsdcResponse.class);
        HttpEntity messageMock = mock(HttpEntity.class);
        Pair<HttpAsdcResponse, CloseableHttpResponse> responsePair = new Pair<>(responseMock, null);

        when(responseMock.getStatus()).thenReturn(HttpStatus.SC_INTERNAL_SERVER_ERROR);
        when(responseMock.getMessage()).thenReturn(messageMock);
        when(messageMock.getContent()).thenReturn(new ThrowingInputStreamForTesting());
        doReturn(responsePair).when(httpClient).getRequest(eq(ARTIFACT_URL), any(), eq(false));

        IDistributionClientResult result = asdcClient.dowloadArtifact(artifactInfo);
        assertEquals(DistributionActionResultEnum.ASDC_SERVER_PROBLEM, result.getDistributionActionResult());
    }

    private String excpectedStringBody(boolean isConsumeProduceStatusTopic) {
        String stringBodyTemplate =
                "{\r\n" + "  \"apiPublicKey\": \"MockApikey\",\r\n" + "  \"distrEnvName\": \"MockEnv\",\r\n"
                        + "  \"isConsumerToSdcDistrStatusTopic\": %s\r\n" + "}";
        return String.format(stringBodyTemplate, isConsumeProduceStatusTopic);

    }

    static class ThrowingInputStreamForTesting extends InputStream {

        @Override
        public int read() throws IOException {
            throw new IOException("Not implemented. This is expected as the implementation is for unit tests only.");
        }
    }
}