338657b515a5aff0ba4f16a970d20c6d2d48ad41
[vid.git] / vid-app-common / src / test / java / org / onap / vid / services / AaiServiceTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 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.onap.vid.services;
22
23 import static org.hamcrest.MatcherAssert.assertThat;
24 import static org.hamcrest.Matchers.arrayWithSize;
25 import static org.hamcrest.Matchers.equalTo;
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.ArgumentMatchers.anyString;
28 import static org.mockito.ArgumentMatchers.eq;
29 import static org.mockito.Mockito.mock;
30 import static org.mockito.Mockito.when;
31 import static org.mockito.Mockito.withSettings;
32 import static org.testng.Assert.assertEquals;
33 import static org.testng.Assert.assertNotNull;
34
35 import com.google.common.collect.ImmutableMap;
36 import java.util.ArrayList;
37 import java.util.Arrays;
38 import java.util.Collections;
39 import java.util.List;
40 import java.util.Map;
41 import org.mockito.InjectMocks;
42 import org.mockito.Mock;
43 import org.mockito.Mockito;
44 import org.mockito.MockitoAnnotations;
45 import org.onap.vid.aai.AaiClientInterface;
46 import org.onap.vid.aai.AaiResponse;
47 import org.onap.vid.aai.model.AaiGetPnfResponse;
48 import org.onap.vid.aai.model.AaiGetPnfs.Pnf;
49 import org.onap.vid.aai.model.AaiGetTenatns.GetTenantsResponse;
50 import org.onap.vid.aai.model.LogicalLinkResponse;
51 import org.onap.vid.aai.model.Relationship;
52 import org.onap.vid.aai.model.RelationshipData;
53 import org.onap.vid.aai.model.RelationshipList;
54 import org.onap.vid.aai.model.ServiceRelationships;
55 import org.onap.vid.model.aaiTree.AAITreeNode;
56 import org.onap.vid.roles.Role;
57 import org.onap.vid.roles.RoleValidator;
58 import org.onap.vid.roles.RoleValidatorFactory;
59 import org.testng.annotations.BeforeMethod;
60 import org.testng.annotations.DataProvider;
61 import org.testng.annotations.Test;
62
63 public class AaiServiceTest {
64
65     @InjectMocks
66     private AaiServiceImpl aaiService;
67
68     @Mock
69     private AaiClientInterface aaiClientInterface;
70
71     @Mock
72     private RoleValidatorFactory roleValidatorFactory;
73
74     @BeforeMethod
75     public void initMocks(){
76         MockitoAnnotations.initMocks(this);
77     }
78
79     @Test
80     public void testGetSpecificPnf(){
81         Pnf pnf = Pnf.builder().withPnfId("11111").build();
82         AaiResponse<Pnf> aaiResponse = new AaiResponse<>(pnf, "aaaa", 200);
83         Mockito.doReturn(aaiResponse).when(aaiClientInterface).getSpecificPnf(Mockito.anyString());
84         AaiResponse<Pnf> specificPnf = aaiService.getSpecificPnf("1345667");
85         assertNotNull(specificPnf);
86         pnf = specificPnf.getT();
87         assertNotNull(pnf);
88         assertEquals("11111",pnf.getPnfId());
89         assertEquals("aaaa",specificPnf.getErrorMessage());
90         assertEquals(200,specificPnf.getHttpCode());
91     }
92
93     @Test
94     public void testPnfByRegion(){
95         AaiGetPnfResponse aaiGetPnfResponse = new AaiGetPnfResponse();
96         AaiResponse<AaiGetPnfResponse> aaiResponse = new AaiResponse<>(aaiGetPnfResponse, "", 200);
97         Mockito.doReturn(aaiResponse).when(aaiClientInterface).getPNFData(Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString());
98         AaiResponse<AaiGetPnfResponse> aaiGetPnfResponseWrapper = aaiService.getPNFData("1345667", "1345667", "1345667", "1345667", "1345667", "1345667", "1345667");
99         assertNotNull(aaiGetPnfResponseWrapper);
100         aaiGetPnfResponse = aaiGetPnfResponseWrapper.getT();
101         assertNotNull(aaiGetPnfResponse);
102     }
103
104     @Test
105     public void testGetAssociatedPnfs(){
106         ServiceRelationships serviceRelationships = createServiceRelationships();
107         AaiResponse<ServiceRelationships> aaiResponse = new AaiResponse<>(serviceRelationships, null, 200);
108         Mockito.doReturn(aaiResponse).when(aaiClientInterface).getServiceInstance(Mockito.anyString(), Mockito.anyString(), Mockito.anyString());
109
110         LogicalLinkResponse logicalLinkResponse = createLogicalLinkResponse();
111         AaiResponse<LogicalLinkResponse> aaiResponse1 = new AaiResponse<>(logicalLinkResponse, null, 200);
112         Mockito.doReturn(aaiResponse1).when(aaiClientInterface).getLogicalLink("SANITY6758cce9%3ALAG1992%7CSANITY6785cce9%3ALAG1961");
113
114         List<String> pnfList = aaiService.getServiceInstanceAssociatedPnfs("123", "456", "789");
115         assertNotNull(pnfList);
116         assertEquals(1, pnfList.size());
117         assertEquals("SANITY6785cce9", pnfList.get(0));
118     }
119
120     private ServiceRelationships createServiceRelationships() {
121         ServiceRelationships serviceRelationships = new ServiceRelationships();
122         serviceRelationships.setServiceInstanceName("test service");
123
124         RelationshipData logicalLinksRelationshipData = new RelationshipData();
125         logicalLinksRelationshipData.setRelationshipKey("logical-link.link-name");
126         logicalLinksRelationshipData.setRelationshipValue("SANITY6758cce9:LAG1992|SANITY6785cce9:LAG1961");
127
128         Relationship logicalLinksRelationship = new Relationship();
129         logicalLinksRelationship.setRelatedTo("logical-link");
130         logicalLinksRelationship.setRelationDataList(Arrays.asList(logicalLinksRelationshipData));
131
132         RelationshipList logicalLinksRelationshipsList = new RelationshipList();
133         logicalLinksRelationshipsList.setRelationship(Arrays.asList(logicalLinksRelationship));
134
135         serviceRelationships.setRelationshipList(logicalLinksRelationshipsList);
136         return serviceRelationships;
137     }
138
139     private LogicalLinkResponse createLogicalLinkResponse() {
140         LogicalLinkResponse logicalLinkResponse = new LogicalLinkResponse();
141         logicalLinkResponse.setLinkName("SANITY6758cce9:LAG1992|SANITY6785cce9:LAG1961");
142
143         RelationshipData lagInterfaceRelationshipData = new RelationshipData();
144         lagInterfaceRelationshipData.setRelationshipKey("pnf.pnf-name");
145         lagInterfaceRelationshipData.setRelationshipValue("SANITY6785cce9");
146
147         Relationship lagInterfaceRelationship = new Relationship();
148         lagInterfaceRelationship.setRelatedTo("lag-interface");
149         lagInterfaceRelationship.setRelationDataList(Arrays.asList(lagInterfaceRelationshipData));
150
151         RelationshipList lagInterfaceRelationshipsList = new RelationshipList();
152         lagInterfaceRelationshipsList.setRelationship(Arrays.asList(lagInterfaceRelationship));
153
154         logicalLinkResponse.setRelationshipList(lagInterfaceRelationshipsList);
155
156         return logicalLinkResponse;
157     }
158
159     @DataProvider
160     public static Object[][] getTenantsData() {
161         return new Object[][] {
162                 {"customer1", "serviceType1", "tenant1", "customer1", "serviceType1", "tenant1", "id-1", true},
163                 {"customer1", "serviceType1", "tenant2", "customer1", "serviceType1", "tenant1", "tenant2", false},
164                 {"customer1", "serviceType1", null, "customer1", "serviceType1", "tenant1", "tenant2", true},
165                 {"customer2", "serviceType1", "tenant1", "customer1", "serviceType1", "tenant1", "id-1", false},
166                 {"customer1", "serviceType2", "tenant1", "customer1", "serviceType1", "tenant1", "id-1", false},
167                 {"customer2", "serviceType1", null, "customer1", "serviceType1", "tenant1", "id-1", false},
168                 {"customer1", "serviceType2", null, "customer1", "serviceType1", "tenant1", "id-1", false},
169         };
170     }
171
172     @Test(dataProvider = "getTenantsData")
173     public void testGetTenants(String userGlobalCustomerId, String userServiceType, String userTenantName,
174                                 String serviceGlobalCustomerId, String serviceServiceType, String serviceTenantName,
175                                 String serviceTenantId, boolean expectedIsPermitted) {
176         GetTenantsResponse[] getTenantsResponses = new GetTenantsResponse[] {new GetTenantsResponse(null, null, serviceTenantName, serviceTenantId, false)};
177         AaiResponse<GetTenantsResponse[]> aaiResponse = new AaiResponse<>(getTenantsResponses, null, 200);
178         Mockito.doReturn(aaiResponse).when(aaiClientInterface).getTenants(serviceGlobalCustomerId, serviceServiceType);
179
180         RoleValidator roleValidatorMock = mock(RoleValidator.class);
181         when(roleValidatorMock.isTenantPermitted(
182             eq(userGlobalCustomerId), eq(userServiceType),
183             (userTenantName == null) ? anyString() : eq(userTenantName))
184         ).thenReturn(true);
185
186         AaiResponse<GetTenantsResponse[]> actualTenants = aaiService.getTenants(serviceGlobalCustomerId, serviceServiceType, roleValidatorMock);
187
188         assertThat(actualTenants.getT(), arrayWithSize(1));
189         assertThat(actualTenants.getT()[0].tenantName, equalTo(serviceTenantName));
190         assertThat(actualTenants.getT()[0].isPermitted, equalTo(expectedIsPermitted));
191     }
192
193     @DataProvider
194     public static Object[][] instanceGroupsRoleAndType() {
195         return new Object[][]{
196                 {"group role & type are same as requested", ImmutableMap.of("SERVICE-ACCESS", "LOAD-GROUP"), false},
197                 {"1 group properties is same as requested, 1 is not same", ImmutableMap.of("SERVICE-ACCESS", "LOAD-GROUP",
198                                                                     "dummy", "dummy"), false},
199                 {"only group type is same as requested", ImmutableMap.of("dummy", "LOAD-GROUP"), true},
200                 {"only group role is same as requested", ImmutableMap.of("SERVICE-ACCESS", "dummy"), true},
201                 {"group role & type are not same as requested", ImmutableMap.of("dummy", "dummy"), true},
202                 {"vnf is not related to instance group", ImmutableMap.of(), true},
203         };
204     }
205
206     @Test(dataProvider = "instanceGroupsRoleAndType")
207     public void testFilterInstanceGroupByRoleAndType(String description, Map<String, String> instanceGroupsProperties, boolean expectedMatch) {
208         List<AAITreeNode> instanceGroups = new ArrayList<>();
209
210         for (Map.Entry<String, String> props: instanceGroupsProperties.entrySet()) {
211             AAITreeNode instanceGroup = new AAITreeNode();
212             Map<String, Object> additionalProperties = ImmutableMap.of(
213                     "instance-group-role", props.getKey(),
214                     "instance-group-type", props.getValue());
215             instanceGroup.setAdditionalProperties(additionalProperties);
216             instanceGroups.add(instanceGroup);
217         }
218
219         boolean anyMatch = aaiService.isInstanceGroupsNotMatchRoleAndType(instanceGroups, "SERVICE-ACCESS", "LOAD-GROUP");
220         assertThat(anyMatch, equalTo(expectedMatch));
221     }
222
223 }