3f64fd5f8b7971aef466b0c5e0f30531cba0d2f0
[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.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.config.DmaapPublisherConfiguration;
30
31 import java.io.IOException;
32 import java.lang.reflect.Field;
33 import java.util.Optional;
34
35 import static org.mockito.ArgumentMatchers.any;
36 import static org.mockito.Mockito.mock;
37 import static org.mockito.Mockito.when;
38
39
40 public class ExtendedDmaapProducerHttpClientImplTest {
41
42     private static ExtendedDmaapProducerHttpClientImpl objectUnderTest;
43
44     private static DmaapPublisherConfiguration configurationMock = mock(DmaapPublisherConfiguration.class);
45     private static CloseableHttpClient closeableHttpClientMock = mock(CloseableHttpClient.class);
46     private static DmaapPublisherRequestDetails requestDetailsMock = mock(DmaapPublisherRequestDetails.class);
47
48     private static Optional<String> expectedResult = Optional.empty();
49     private static final String JSON_MESSAGE = "{ \"ipaddress-v4-oam\": \"11.22.33.44\" }";
50     private static final String RESPONSE_SUCCESS = "200";
51     private static final String RESPONSE_FAILURE = "404";
52
53     @BeforeAll
54     public static void init() throws NoSuchFieldException, IllegalAccessException {
55
56         when(configurationMock.dmaapHostName()).thenReturn("54.45.33.2");
57         when(configurationMock.dmaapProtocol()).thenReturn("https");
58         when(configurationMock.dmaapPortNumber()).thenReturn(1234);
59         when(configurationMock.dmaapUserName()).thenReturn("PRH");
60         when(configurationMock.dmaapUserPassword()).thenReturn("PRH");
61         when(configurationMock.dmaapContentType()).thenReturn("application/json");
62         when(configurationMock.dmaapTopicName()).thenReturn("pnfReady");
63
64         when(requestDetailsMock.dmaapAPIPath()).thenReturn("events");
65         when(requestDetailsMock.jsonBody()).thenReturn(JSON_MESSAGE);
66
67         objectUnderTest = new ExtendedDmaapProducerHttpClientImpl(configurationMock);
68
69         setField();
70     }
71
72
73     @Test
74     public void getHttpResponsePost_success() throws IOException {
75         expectedResult = Optional.of(RESPONSE_SUCCESS);
76
77         when(closeableHttpClientMock.execute(any(HttpPost.class), any(ResponseHandler.class)))
78             .thenReturn(expectedResult);
79
80         Optional<String> actualResult = objectUnderTest.getHttpProducerResponse(requestDetailsMock);
81
82         Assertions.assertEquals(expectedResult.get(), actualResult.get());
83     }
84
85     @Test
86     public void getExtendedDetails_returnsFailure() throws IOException {
87         expectedResult = Optional.of(RESPONSE_FAILURE);
88         when(closeableHttpClientMock.execute(any(HttpPost.class), any(ResponseHandler.class)))
89             .thenReturn(Optional.empty());
90         Optional<String> actualResult = objectUnderTest.getHttpProducerResponse(requestDetailsMock);
91         Assertions.assertEquals(Optional.empty(), actualResult);
92     }
93
94
95     private static void setField() throws NoSuchFieldException, IllegalAccessException {
96         Field field = objectUnderTest.getClass().getDeclaredField("closeableHttpClient");
97         field.setAccessible(true);
98         field.set(objectUnderTest, closeableHttpClientMock);
99     }
100 }