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