aa6810e3d7327f4c1d41bec098d5e3003c5f18b5
[dcaegen2/services/prh.git] /
1 /*-
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
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.onap.dcaegen2.services.prh.service.producer;
22
23 import org.apache.http.client.ResponseHandler;
24 import org.apache.http.client.methods.HttpPost;
25 import org.apache.http.impl.client.CloseableHttpClient;
26 import org.junit.jupiter.api.Assertions;
27 import org.junit.jupiter.api.BeforeAll;
28 import org.junit.jupiter.api.Test;
29 import org.onap.dcaegen2.services.prh.config.DmaapPublisherConfiguration;
30 import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel;
31 import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModelForUnitTest;
32
33 import java.io.IOException;
34 import java.lang.reflect.Field;
35 import java.util.Optional;
36
37 import static org.mockito.ArgumentMatchers.any;
38 import static org.mockito.Mockito.mock;
39 import static org.mockito.Mockito.when;
40
41
42 public class ExtendedDmaapProducerHttpClientImplTest {
43
44     private static ExtendedDmaapProducerHttpClientImpl objectUnderTest;
45     private static DmaapPublisherConfiguration configurationMock = mock(DmaapPublisherConfiguration.class);
46     private static CloseableHttpClient closeableHttpClientMock = mock(CloseableHttpClient.class);
47     private static ConsumerDmaapModel consumerDmaapModel = new ConsumerDmaapModelForUnitTest();
48     private static Integer expectedResult;
49     private static final Integer RESPONSE_SUCCESS = 200;
50     private static final Integer RESPONSE_FAILURE = 404;
51
52     @BeforeAll
53     public static void init() throws NoSuchFieldException, IllegalAccessException {
54         when(configurationMock.dmaapHostName()).thenReturn("54.45.33.2");
55         when(configurationMock.dmaapProtocol()).thenReturn("https");
56         when(configurationMock.dmaapPortNumber()).thenReturn(1234);
57         when(configurationMock.dmaapUserName()).thenReturn("PRH");
58         when(configurationMock.dmaapUserPassword()).thenReturn("PRH");
59         when(configurationMock.dmaapContentType()).thenReturn("application/json");
60         when(configurationMock.dmaapTopicName()).thenReturn("pnfReady");
61         objectUnderTest = new ExtendedDmaapProducerHttpClientImpl(configurationMock);
62         setField();
63     }
64
65
66     @Test
67     public void getHttpResponsePost_success() throws IOException {
68         expectedResult = RESPONSE_SUCCESS;
69         when(closeableHttpClientMock.execute(any(HttpPost.class), any(ResponseHandler.class)))
70             .thenReturn(Optional.of(expectedResult));
71         Optional<Integer> actualResult = objectUnderTest.getHttpProducerResponse(consumerDmaapModel);
72         Assertions.assertEquals(expectedResult, actualResult.get());
73     }
74
75     @Test
76     public void getExtendedDetails_returnsFailure() throws IOException {
77         expectedResult = RESPONSE_FAILURE;
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());
82     }
83
84     private static void setField() throws NoSuchFieldException, IllegalAccessException {
85         Field field = objectUnderTest.getClass().getDeclaredField("closeableHttpClient");
86         field.setAccessible(true);
87         field.set(objectUnderTest, closeableHttpClientMock);
88     }
89 }