39c10de53bec3f8b702f3a53d60b7308aeebd1f5
[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.consumer;
22
23 import org.apache.http.client.ResponseHandler;
24 import org.apache.http.client.methods.HttpGet;
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.DmaapConsumerConfiguration;
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 ExtendedDmaapConsumerHttpClientImplTest {
41
42     private static ExtendedDmaapConsumerHttpClientImpl objectUnderTest;
43
44     private static DmaapConsumerConfiguration configurationMock = mock(DmaapConsumerConfiguration.class);
45     private static CloseableHttpClient closeableHttpClientMock = mock(CloseableHttpClient.class);
46
47     private static final String JSON_MESSAGE = "{ \"responseFromDmaap\": \"Success\" }";
48
49     private static Optional<String> expectedResult = Optional.empty();
50
51     @BeforeAll
52     public static void init() throws NoSuchFieldException, IllegalAccessException {
53
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("unauthenticated.SEC_OTHER_OUTPUT");
61         when(configurationMock.consumerGroup()).thenReturn("OpenDCAE-c12");
62         when(configurationMock.consumerId()).thenReturn("c12");
63
64         objectUnderTest = new ExtendedDmaapConsumerHttpClientImpl(configurationMock);
65
66         setField();
67     }
68
69
70     @Test
71     public void getHttpResponseGet_success() throws IOException {
72         expectedResult = Optional.of(JSON_MESSAGE);
73
74         when(closeableHttpClientMock.execute(any(HttpGet.class), any(ResponseHandler.class)))
75                 .thenReturn(expectedResult);
76
77         Optional<String> actualResult = objectUnderTest.getHttpConsumerResponse();
78
79         Assertions.assertEquals(expectedResult.get(), actualResult.get());
80     }
81
82     @Test
83     public void getExtendedDetails_returnsNull() throws IOException {
84         when(closeableHttpClientMock.execute(any(HttpGet.class), any(ResponseHandler.class))).
85                 thenReturn(Optional.empty());
86         Optional<String>  actualResult = objectUnderTest.getHttpConsumerResponse();
87         Assertions.assertEquals(Optional.empty(),actualResult);
88     }
89
90
91     private static void setField() throws NoSuchFieldException, IllegalAccessException {
92         Field field = objectUnderTest.getClass().getDeclaredField("closeableHttpClient");
93         field.setAccessible(true);
94         field.set(objectUnderTest, closeableHttpClientMock);
95     }
96 }