2 * ============LICENSE_START=======================================================
3 * PNF-REGISTRATION-HANDLER
4 * ================================================================================
5 * Copyright (C) 2018 NOKIA 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.dcaegen2.services.prh.service.producer;
23 import org.apache.http.HttpEntity;
24 import org.apache.http.HttpResponse;
25 import org.apache.http.HttpStatus;
26 import org.apache.http.StatusLine;
27 import org.apache.http.client.ResponseHandler;
28 import org.apache.http.client.methods.HttpPost;
29 import org.apache.http.impl.client.CloseableHttpClient;
30 import org.junit.jupiter.api.Assertions;
31 import org.junit.jupiter.api.BeforeAll;
32 import org.junit.jupiter.api.Test;
33 import org.onap.dcaegen2.services.prh.config.DmaapPublisherConfiguration;
34 import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel;
35 import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModelForUnitTest;
37 import java.io.IOException;
38 import java.lang.reflect.Field;
39 import java.util.Optional;
41 import static org.junit.jupiter.api.Assertions.assertEquals;
42 import static org.mockito.ArgumentMatchers.any;
43 import static org.mockito.Mockito.mock;
44 import static org.mockito.Mockito.when;
47 class ExtendedDmaapProducerHttpClientImplTest {
49 private static ExtendedDmaapProducerHttpClientImpl objectUnderTest;
50 private static DmaapPublisherConfiguration configurationMock = mock(DmaapPublisherConfiguration.class);
51 private static CloseableHttpClient closeableHttpClientMock = mock(CloseableHttpClient.class);
52 private static ConsumerDmaapModel consumerDmaapModel = new ConsumerDmaapModelForUnitTest();
53 private static Integer expectedResult;
54 private static final Integer RESPONSE_SUCCESS = 200;
55 private static final Integer RESPONSE_FAILURE = 404;
56 private final static HttpResponse httpResponseMock = mock(HttpResponse.class);
57 private final static HttpEntity httpEntityMock = mock(HttpEntity.class);
58 private final static StatusLine statusLineMock = mock(StatusLine.class);
62 static void init() throws NoSuchFieldException, IllegalAccessException {
63 when(configurationMock.dmaapHostName()).thenReturn("54.45.33.2");
64 when(configurationMock.dmaapProtocol()).thenReturn("https");
65 when(configurationMock.dmaapPortNumber()).thenReturn(1234);
66 when(configurationMock.dmaapUserName()).thenReturn("PRH");
67 when(configurationMock.dmaapUserPassword()).thenReturn("PRH");
68 when(configurationMock.dmaapContentType()).thenReturn("application/json");
69 when(configurationMock.dmaapTopicName()).thenReturn("pnfReady");
70 objectUnderTest = new ExtendedDmaapProducerHttpClientImpl(configurationMock);
76 void getHttpResponsePost_success() throws IOException {
77 expectedResult = RESPONSE_SUCCESS;
78 when(closeableHttpClientMock.execute(any(HttpPost.class), any(ResponseHandler.class)))
79 .thenReturn(Optional.of(expectedResult));
80 Optional<Integer> actualResult = objectUnderTest.getHttpProducerResponse(consumerDmaapModel);
81 Assertions.assertEquals(expectedResult, actualResult.get());
85 void getExtendedDetails_returnsFailure() throws IOException {
86 expectedResult = RESPONSE_FAILURE;
87 when(closeableHttpClientMock.execute(any(HttpPost.class), any(ResponseHandler.class)))
88 .thenReturn(Optional.of(expectedResult));
89 Optional<Integer> actualResult = objectUnderTest.getHttpProducerResponse(consumerDmaapModel);
90 Assertions.assertEquals(expectedResult, actualResult.get());
94 void handleResponse_shouldReturn200() throws IOException {
96 when(httpResponseMock.getEntity()).thenReturn(httpEntityMock);
97 when(httpResponseMock.getStatusLine()).thenReturn(statusLineMock);
98 when(httpResponseMock.getStatusLine().getStatusCode()).thenReturn(HttpStatus.SC_OK);
100 assertEquals(Optional.of(HttpStatus.SC_OK), objectUnderTest.handleResponse(httpResponseMock));
104 void handleResponse_shouldReturn300() throws IOException {
106 when(httpResponseMock.getEntity()).thenReturn(httpEntityMock);
107 when(httpResponseMock.getStatusLine()).thenReturn(statusLineMock);
108 when(httpResponseMock.getStatusLine().getStatusCode()).thenReturn(HttpStatus.SC_BAD_REQUEST);
110 assertEquals(Optional.of(HttpStatus.SC_BAD_REQUEST), objectUnderTest.handleResponse(httpResponseMock));
113 private static void setField() throws NoSuchFieldException, IllegalAccessException {
114 Field field = objectUnderTest.getClass().getDeclaredField("closeableHttpClient");
115 field.setAccessible(true);
116 field.set(objectUnderTest, closeableHttpClientMock);