Extend probe mechanism
[vid.git] / vid-app-common / src / test / java / org / onap / vid / aai / AaiOverTLSClientTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
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
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.vid.aai;
22
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;
30
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;
39
40 import java.io.InputStream;
41 import java.util.Collections;
42 import java.util.List;
43 import java.util.Map;
44
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;
51
52 public class AaiOverTLSClientTest {
53
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;
57
58     @Mock(answer = Answers.RETURNS_MOCKS)
59     private SyncRestClient syncRestClient;
60     @Mock
61     private AaiOverTLSPropertySupplier propertySupplier;
62
63     @Mock
64     private HttpResponse<SubscriberList> response;
65
66     @BeforeMethod
67     public void setUp() {
68         initMocks(this);
69         aaiRestClient = new AaiOverTLSClient(syncRestClient,  propertySupplier);
70     }
71
72     @Test
73     public void testIsNodeTypeExistsByName() {
74         mockPropertyReader();
75
76         aaiRestClient.isNodeTypeExistsByName("name", ResourceType.GENERIC_VNF);
77         verify(syncRestClient).get(contains(SEARCH_NODES_QUERY_SEARCH_NODE_TYPE),
78             eq(getHeaders()), eq(Collections.emptyMap()));
79     }
80
81     @Test
82     public void  testGetAllSubscribers(){
83         mockPropertyReader();
84
85         aaiRestClient.getAllSubscribers();
86         verify(syncRestClient).get(contains(SUBSCRIBERS),
87             eq(getHeaders()), eq(Collections.emptyMap()), eq(SubscriberList.class));
88     }
89
90
91     @Test
92     public void probeMechanismShouldReturnAllSubscribers() {
93         mockPropertyReader();
94         List<Subscriber> subscribers = Lists.newArrayList(new Subscriber());
95
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);
103
104
105         ExternalComponentStatus externalComponentStatus = aaiRestClient.probeGetAllSubscribers();
106
107         assertThat(externalComponentStatus.isAvailable()).isTrue();
108         assertThat(externalComponentStatus.getComponent()).isEqualTo(ExternalComponentStatus.Component.AAI);
109     }
110
111     @Test
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"));
116
117         ExternalComponentStatus externalComponentStatus = aaiRestClient.probeGetAllSubscribers();
118
119         assertThat(externalComponentStatus.isAvailable()).isFalse();
120         assertThat(externalComponentStatus.getComponent()).isEqualTo(ExternalComponentStatus.Component.AAI);
121         assertThat(externalComponentStatus.getMetadata().getDescription()).containsSequence("call failed");
122     }
123
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");
129     }
130
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();
135     }
136 }