Sonar Critical Fix
[dcaegen2/analytics/tca.git] / dcae-analytics-aai / src / test / java / org / openecomp / dcae / apod / analytics / aai / service / AAIEnrichmentClientImplTest.java
1 /*
2  * ===============================LICENSE_START======================================
3  *  dcae-analytics
4  * ================================================================================
5  *    Copyright © 2017 AT&T 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.openecomp.dcae.apod.analytics.aai.service;
22
23 import com.google.common.base.Optional;
24 import com.google.common.collect.ImmutableMap;
25 import org.apache.http.HttpEntity;
26 import org.apache.http.HttpResponse;
27 import org.apache.http.StatusLine;
28 import org.apache.http.client.ResponseHandler;
29 import org.apache.http.client.methods.HttpGet;
30 import org.apache.http.impl.client.CloseableHttpClient;
31 import org.junit.Assert;
32 import org.junit.Test;
33 import org.openecomp.dcae.apod.analytics.aai.BaseAnalyticsAAIUnitTest;
34 import org.openecomp.dcae.apod.analytics.aai.domain.config.AAIHttpClientConfig;
35
36 import java.io.BufferedInputStream;
37 import java.io.ByteArrayInputStream;
38 import java.io.InputStream;
39 import java.util.Map;
40
41 import static org.hamcrest.CoreMatchers.is;
42 import static org.junit.Assert.assertThat;
43 import static org.mockito.ArgumentMatchers.any;
44 import static org.mockito.ArgumentMatchers.anyInt;
45 import static org.mockito.ArgumentMatchers.eq;
46 import static org.mockito.Mockito.mock;
47 import static org.mockito.Mockito.when;
48
49 /**
50  * @author Rajiv Singla . Creation Date: 9/25/2017.
51  */
52 public class AAIEnrichmentClientImplTest extends BaseAnalyticsAAIUnitTest {
53
54
55     @Test
56     public void testGetEnrichmentDetails() throws Exception {
57
58         final String enrichmentResponseJson = "{}";
59
60         final String vnfName = "vCPEInfraVNF13";
61         final AAIHttpClientConfig aaiHttpClientTestConfig = getAAIHttpClientTestConfig(true, PROXY_URL);
62         Map<String, String> queryParams = ImmutableMap.of("vnf-name", vnfName);
63         final AAIHttpClientFactory aaiHttpClientFactory = mock(AAIHttpClientFactory.class);
64         final CloseableHttpClient closeableHttpClient = mock(CloseableHttpClient.class);
65         final AAIHttpClient aaiHttpClient = mock(AAIHttpClient.class);
66         when(aaiHttpClientFactory.create(any(AAIHttpClientConfig.class))).thenReturn(aaiHttpClient);
67         when(aaiHttpClient.getAAIHttpClient()).thenReturn(closeableHttpClient);
68         when(closeableHttpClient.execute(any(HttpGet.class), any(ResponseHandler.class)))
69                 .thenReturn(Optional.of(enrichmentResponseJson));
70         final AAIEnrichmentClientImpl aaiEnrichmentClient = new AAIEnrichmentClientImpl(
71                 aaiHttpClientTestConfig, aaiHttpClientFactory);
72
73         final String enrichmentDetails = aaiEnrichmentClient.getEnrichmentDetails(AAI_VNF_ENRICHMENT_PATH,
74                 queryParams, AAI_HEADERS);
75         assertThat("Enrichment response is same", enrichmentDetails, is(enrichmentResponseJson));
76
77     }
78
79     @Test
80     public void testAAiResponseHandler() throws Exception {
81         final ResponseHandler<Optional<String>> aaiResponseHandler =
82                 AAIEnrichmentClientImpl.aaiResponseHandler();
83         final HttpResponse httpResponse = mock(HttpResponse.class);
84         final HttpEntity httpEntity = mock(HttpEntity.class);
85         final String response = "{}";
86         InputStream inputStream = new ByteArrayInputStream(response.getBytes());
87         when(httpEntity.getContent()).thenReturn(inputStream);
88         when(httpResponse.getEntity()).thenReturn(httpEntity);
89         final StatusLine statusLine = mock(StatusLine.class);
90         when(httpResponse.getStatusLine()).thenReturn(statusLine);
91         when(statusLine.getStatusCode()).thenReturn(200);
92         final Optional<String> result = aaiResponseHandler.handleResponse(httpResponse);
93         assertThat("Response must match", result.get(), is(response));
94     }
95
96 }