Merge "PombaReqest and ServiceInstance improvements"
[vid.git] / vid-app-common / src / test / java / org / onap / vid / services / AaiServiceImplTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2018 Nokia. All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.vid.services;
23
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.when;
27
28 import java.util.ArrayList;
29 import java.util.Arrays;
30 import java.util.List;
31 import javax.ws.rs.core.Response;
32 import org.junit.Test;
33 import org.onap.vid.aai.AaiClientInterface;
34 import org.onap.vid.aai.AaiGetVnfResponse;
35 import org.onap.vid.aai.AaiOverTLSClientInterface;
36 import org.onap.vid.aai.AaiResponse;
37 import org.onap.vid.aai.AaiResponseTranslator;
38 import org.onap.vid.aai.model.AaiGetTenatns.GetTenantsResponse;
39 import org.onap.vid.aai.model.VnfResult;
40 import org.onap.vid.roles.RoleValidator;
41
42 public class AaiServiceImplTest {
43
44     private AaiClientInterface aaiClient = mock(AaiClientInterface.class);
45     private AaiOverTLSClientInterface aaiSslClient = mock(AaiOverTLSClientInterface.class);
46     private AaiResponseTranslator aaiResponseTranslator = mock(AaiResponseTranslator.class);
47     private AAITreeNodeBuilder aaiTreeNode = mock(AAITreeNodeBuilder.class);
48     private AAIServiceTree aaiServiceTree = mock(AAIServiceTree.class);
49
50     private AaiServiceImpl aaiService = new AaiServiceImpl(
51         aaiClient, aaiSslClient, aaiResponseTranslator, aaiTreeNode, aaiServiceTree
52     );
53
54     @Test
55     public void shouldRetrievePnf() {
56         // given
57         String globalCustomerId = "global_customer";
58         String serviceType = "service_type";
59         String modelVersionId = "model_version";
60         String modelInvariantId = "model_invariant_id";
61         String cloudRegion = "cloud_region";
62         String equipVendor = "equip_vendor";
63         String equipModel = "equip_model";
64
65         AaiResponse response = mock(AaiResponse.class);
66         when(aaiClient.getPNFData(
67             globalCustomerId, serviceType, modelVersionId, modelInvariantId, cloudRegion, equipVendor, equipModel
68         )).thenReturn(response);
69
70         // when
71         AaiResponse actual = aaiService.getPNFData(
72             globalCustomerId, serviceType, modelVersionId, modelInvariantId, cloudRegion, equipVendor, equipModel
73         );
74
75         // then
76         assertThat(response).isEqualTo(actual);
77     }
78
79     @Test
80     @SuppressWarnings("unchecked")
81     public void shouldRetrieveSpecificPnf() {
82         // given
83         String pnfId = "some_pnf_id";
84
85         AaiResponse response = mock(AaiResponse.class);
86         when(aaiClient.getSpecificPnf(pnfId)).thenReturn(response);
87
88         // when
89         AaiResponse actual = aaiService.getSpecificPnf(pnfId);
90
91         // then
92         assertThat(response).isEqualTo(actual);
93     }
94
95     @Test
96     public void shouldRetrieveTenantsByInvariantId() {
97         // given
98         List<String> modelInvariantId = new ArrayList<>();
99
100         Response response = mock(Response.class);
101         when(aaiClient.getVersionByInvariantId(modelInvariantId)).thenReturn(response);
102
103         // when
104         Response actual = aaiService.getVersionByInvariantId(modelInvariantId);
105
106         // then
107         assertThat(response).isEqualTo(actual);
108     }
109
110     @Test
111     @SuppressWarnings("unchecked")
112     public void shouldRetrieveTenants() {
113         // given
114         String globalCustomerId = "global_customer";
115         String serviceType = "service_type";
116
117         GetTenantsResponse permittedTenant = new GetTenantsResponse(
118             "cloud_region", "cloud_owner", "permitted_tenant", "tenant_id", false
119         );
120         GetTenantsResponse unpermittedTenant = new GetTenantsResponse(
121             "cloud_region", "cloud_owner", "unpermitted_tenant", "tenant_id", false
122         );
123
124         AaiResponse<GetTenantsResponse[]> response = mock(AaiResponse.class);
125         when(response.getT()).thenReturn(new GetTenantsResponse[]{ permittedTenant, unpermittedTenant });
126         when(aaiClient.getTenants(globalCustomerId, serviceType)).thenReturn(response);
127
128         RoleValidator roleValidator = mock(RoleValidator.class);
129         when(roleValidator.isTenantPermitted(globalCustomerId, serviceType, "permitted_tenant")).thenReturn(true);
130         when(roleValidator.isTenantPermitted(globalCustomerId, serviceType, "unpermitted_tenant")).thenReturn(false);
131
132         // when
133         AaiResponse actual = aaiService.getTenants(globalCustomerId, serviceType, roleValidator);
134
135         // then
136         assertThat(response).isEqualTo(actual);
137         assertThat(permittedTenant.isPermitted).isTrue();
138         assertThat(unpermittedTenant.isPermitted).isFalse();
139     }
140
141     @Test
142     public void shouldRetrieveVNFs() {
143         // given
144         String globalSubscriber = "global_subscriber";
145         String serviceType = "service_type";
146         String serviceInstanceId = "service_instance";
147
148         AaiResponse response = mock(AaiResponse.class);
149         when(aaiClient.getVNFData(globalSubscriber, serviceType, serviceInstanceId)).thenReturn(response);
150
151         // when
152         AaiResponse actual = aaiService.getVNFData(globalSubscriber, serviceType, serviceInstanceId);
153
154         // then
155         assertThat(response).isEqualTo(actual);
156     }
157
158     @Test
159     @SuppressWarnings("unchecked")
160     public void shouldRetrieveAndFilterVNFsBySubscriberAndServiceType() {
161         // given
162         String globalSubscriber = "global_subscriber";
163         String serviceType = "service_type";
164
165         VnfResult genericVnf = new VnfResult();
166         genericVnf.nodeType = "generic-vnf";
167
168         VnfResult serviceInstance = new VnfResult();
169         serviceInstance.nodeType = "service-instance";
170
171         VnfResult someVnf = new VnfResult();
172         someVnf.nodeType = "some-vnf";
173
174         AaiResponse<AaiGetVnfResponse> response = mock(AaiResponse.class);
175         AaiGetVnfResponse vnfs = new AaiGetVnfResponse();
176         vnfs.results = Arrays.asList(genericVnf, serviceInstance, someVnf);
177         when(response.getT()).thenReturn(vnfs);
178
179         when(aaiClient.getVNFData(globalSubscriber, serviceType)).thenReturn(response);
180
181         // when
182         AaiResponse actual = aaiService.getVNFData(globalSubscriber, serviceType);
183
184         // then
185         assertThat(response).isEqualTo(actual);
186         assertThat(response.getT().results).containsOnly(genericVnf, serviceInstance);
187     }
188 }