719418fb87a476dec9525e963762291b889a4dad
[dcaegen2/collectors/datafile.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * Datafile Collector Service
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.collectors.datafile.service;
22
23 import static org.mockito.ArgumentMatchers.any;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.when;
26
27 import java.io.IOException;
28 import java.lang.reflect.Field;
29 import java.util.HashMap;
30 import java.util.Map;
31 import java.util.Optional;
32
33 import org.apache.http.client.ResponseHandler;
34 import org.apache.http.client.methods.HttpGet;
35 import org.apache.http.impl.client.CloseableHttpClient;
36 import org.junit.jupiter.api.Assertions;
37 import org.junit.jupiter.api.BeforeAll;
38 import org.junit.jupiter.api.Test;
39 import org.onap.dcaegen2.collectors.datafile.config.AaiClientConfiguration;
40 import org.onap.dcaegen2.collectors.datafile.model.ConsumerDmaapModel;
41 import org.onap.dcaegen2.collectors.datafile.service.AaiConsumerClient;
42
43 class AaiConsumerClientTest {
44
45     private static AaiConsumerClient testedObject;
46     private static AaiClientConfiguration aaiHttpClientConfigurationMock = mock(AaiClientConfiguration.class);
47     private static CloseableHttpClient closeableHttpClientMock = mock(CloseableHttpClient.class);
48     private static final String JSON_MESSAGE = "{ \"pnf-id\": \"example-pnf-id-val-22343\", "
49         + "\"regional-resource-zone\":null, \"ipaddress-v4-oam\": \"11.22.33.44\" }";
50     private static ConsumerDmaapModel consumerDmaapModelMock = mock(ConsumerDmaapModel.class);
51     private static final String PNF_NAME = "nokia-pnf-nhfsadhff";
52
53     @BeforeAll
54     static void setup() throws NoSuchFieldException, IllegalAccessException {
55
56         Map<String, String> aaiHeaders = new HashMap<>();
57         aaiHeaders.put("X-FromAppId", "datafile");
58         aaiHeaders.put("X-TransactionId", "9999");
59         aaiHeaders.put("Accept", "application/json");
60         aaiHeaders.put("Authorization", "Basic QUFJOkFBSQ==");
61         aaiHeaders.put("Real-Time", "true");
62         aaiHeaders.put("Content-Type", "application/json");
63
64         when(aaiHttpClientConfigurationMock.aaiHost()).thenReturn("54.45.33.2");
65         when(aaiHttpClientConfigurationMock.aaiProtocol()).thenReturn("https");
66         when(aaiHttpClientConfigurationMock.aaiPort()).thenReturn(1234);
67         when(aaiHttpClientConfigurationMock.aaiUserName()).thenReturn("Datafile");
68         when(aaiHttpClientConfigurationMock.aaiUserPassword()).thenReturn("Datafile");
69         when(aaiHttpClientConfigurationMock.aaiBasePath()).thenReturn("/aai/v11");
70         when(aaiHttpClientConfigurationMock.aaiPnfPath()).thenReturn("/network/pnfs/pnf");
71         when(aaiHttpClientConfigurationMock.aaiHeaders()).thenReturn(aaiHeaders);
72
73         when(consumerDmaapModelMock.getPnfName()).thenReturn(PNF_NAME);
74
75         testedObject = new AaiConsumerClient(aaiHttpClientConfigurationMock);
76         setField();
77     }
78
79
80     @Test
81     void getExtendedDetails_returnsSuccess() throws IOException {
82
83         when(closeableHttpClientMock.execute(any(HttpGet.class), any(ResponseHandler.class)))
84             .thenReturn(Optional.of(JSON_MESSAGE));
85         Optional<String> actualResult = testedObject.getHttpResponse(consumerDmaapModelMock);
86         Assertions.assertEquals(Optional.of(JSON_MESSAGE), actualResult);
87     }
88
89
90     private static void setField() throws NoSuchFieldException, IllegalAccessException {
91         Field field = testedObject.getClass().getDeclaredField("closeableHttpClient");
92         field.setAccessible(true);
93         field.set(testedObject, closeableHttpClientMock);
94     }
95 }