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