2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 - 2019 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.vid.aai;
23 import com.google.common.collect.ImmutableMap;
24 import com.google.common.collect.Lists;
25 import com.google.gson.Gson;
26 import io.joshworks.restclient.http.HttpResponse;
27 import org.apache.commons.io.IOUtils;
28 import org.mockito.Answers;
29 import org.mockito.Mock;
31 import org.onap.vid.aai.model.ResourceType;
32 import org.onap.vid.client.SyncRestClient;
33 import org.onap.vid.model.Subscriber;
34 import org.onap.vid.model.SubscriberList;
35 import org.onap.vid.model.probes.ExternalComponentStatus;
36 import org.springframework.http.HttpStatus;
37 import org.testng.annotations.BeforeMethod;
38 import org.testng.annotations.Test;
40 import java.io.InputStream;
41 import java.util.Collections;
42 import java.util.List;
45 import static org.assertj.core.api.Assertions.assertThat;
46 import static org.mockito.ArgumentMatchers.contains;
47 import static org.mockito.ArgumentMatchers.eq;
48 import static org.mockito.Mockito.verify;
49 import static org.mockito.Mockito.when;
50 import static org.mockito.MockitoAnnotations.initMocks;
52 public class AaiOverTLSClientTest {
54 private static final String SEARCH_NODES_QUERY_SEARCH_NODE_TYPE = "nodes/generic-vnfs?vnf-name=name";
55 private static final String SUBSCRIBERS = "business/customers?subscriber-type=INFRA&depth=0";
56 private AaiOverTLSClient aaiRestClient;
58 @Mock(answer = Answers.RETURNS_MOCKS)
59 private SyncRestClient syncRestClient;
61 private AaiOverTLSPropertySupplier propertySupplier;
64 private HttpResponse<SubscriberList> response;
69 aaiRestClient = new AaiOverTLSClient(syncRestClient, propertySupplier);
73 public void testIsNodeTypeExistsByName() {
76 aaiRestClient.isNodeTypeExistsByName("name", ResourceType.GENERIC_VNF);
77 verify(syncRestClient).get(contains(SEARCH_NODES_QUERY_SEARCH_NODE_TYPE),
78 eq(getHeaders()), eq(Collections.emptyMap()));
82 public void testGetAllSubscribers(){
85 aaiRestClient.getAllSubscribers();
86 verify(syncRestClient).get(contains(SUBSCRIBERS),
87 eq(getHeaders()), eq(Collections.emptyMap()), eq(SubscriberList.class));
92 public void probeMechanismShouldReturnAllSubscribers() {
94 List<Subscriber> subscribers = Lists.newArrayList(new Subscriber());
96 SubscriberList subscriberList = new SubscriberList(subscribers);
97 InputStream json = IOUtils.toInputStream(new Gson().toJson(subscriberList));
98 when(syncRestClient.get(contains(SUBSCRIBERS), eq(getHeaders()), eq(Collections.emptyMap()),
99 eq(SubscriberList.class))).thenReturn(response);
100 when(response.getStatus()).thenReturn(HttpStatus.OK.value());
101 when(response.getRawBody()).thenReturn(json);
102 when(response.isSuccessful()).thenReturn(true);
105 ExternalComponentStatus externalComponentStatus = aaiRestClient.probeGetAllSubscribers();
107 assertThat(externalComponentStatus.isAvailable()).isTrue();
108 assertThat(externalComponentStatus.getComponent()).isEqualTo(ExternalComponentStatus.Component.AAI);
112 public void probeMechanismShouldHandleExceptionProperly(){
113 mockPropertyReader();
114 when(syncRestClient.get(contains(SUBSCRIBERS), eq(getHeaders()), eq(Collections.emptyMap()),
115 eq(SubscriberList.class))).thenThrow(new RuntimeException("call failed"));
117 ExternalComponentStatus externalComponentStatus = aaiRestClient.probeGetAllSubscribers();
119 assertThat(externalComponentStatus.isAvailable()).isFalse();
120 assertThat(externalComponentStatus.getComponent()).isEqualTo(ExternalComponentStatus.Component.AAI);
121 assertThat(externalComponentStatus.getMetadata().getDescription()).containsSequence("call failed");
124 private void mockPropertyReader() {
125 when(propertySupplier.getPassword()).thenReturn("Pass");
126 when(propertySupplier.getUsername()).thenReturn("User");
127 when(propertySupplier.getRequestId()).thenReturn("1");
128 when(propertySupplier.getRandomUUID()).thenReturn("2");
131 private Map<String,String> getHeaders(){
132 return ImmutableMap.<String, String>builder().put("Authorization", "Basic VXNlcjpQYXNz").
133 put("X-FromAppId", "VidAaiController").put("Accept", "application/json").put("X-ECOMP-RequestID", "1").
134 put("X-TransactionId", "2").put("Content-Type", "application/json").build();