Respect owning-entity-id when searching instances by Subscriber
[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.CoreMatchers.is;
24 import static org.hamcrest.MatcherAssert.assertThat;
25 import static org.hamcrest.Matchers.arrayWithSize;
26 import static org.hamcrest.Matchers.equalTo;
27 import static org.hamcrest.Matchers.nullValue;
28 import static org.mockito.ArgumentMatchers.anyString;
29 import static org.mockito.ArgumentMatchers.eq;
30 import static org.mockito.Mockito.mock;
31 import static org.mockito.Mockito.when;
32 import static org.testng.Assert.assertEquals;
33 import static org.testng.Assert.assertNotNull;
34
35 import com.fasterxml.jackson.core.JsonProcessingException;
36 import com.fasterxml.jackson.databind.ObjectMapper;
37 import com.google.common.collect.ImmutableMap;
38 import java.util.ArrayList;
39 import java.util.Arrays;
40 import java.util.List;
41 import java.util.Map;
42 import org.mockito.InjectMocks;
43 import org.mockito.Mock;
44 import org.mockito.Mockito;
45 import org.mockito.MockitoAnnotations;
46 import org.onap.vid.aai.AaiClientInterface;
47 import org.onap.vid.aai.AaiResponse;
48 import org.onap.vid.aai.ServiceInstance;
49 import org.onap.vid.aai.model.AaiGetPnfResponse;
50 import org.onap.vid.aai.model.AaiGetPnfs.Pnf;
51 import org.onap.vid.aai.model.AaiGetTenatns.GetTenantsResponse;
52 import org.onap.vid.aai.model.LogicalLinkResponse;
53 import org.onap.vid.aai.model.Relationship;
54 import org.onap.vid.aai.model.RelationshipData;
55 import org.onap.vid.aai.model.RelationshipList;
56 import org.onap.vid.aai.model.ServiceRelationships;
57 import org.onap.vid.model.aaiTree.AAITreeNode;
58 import org.onap.vid.roles.RoleValidator;
59 import org.onap.vid.roles.RoleValidatorFactory;
60 import org.testng.annotations.BeforeMethod;
61 import org.testng.annotations.DataProvider;
62 import org.testng.annotations.Test;
63
64 public class AaiServiceTest {
65
66     @InjectMocks
67     private AaiServiceImpl aaiService;
68
69     @Mock
70     private AaiClientInterface aaiClientInterface;
71
72     @Mock
73     private RoleValidatorFactory roleValidatorFactory;
74
75     @BeforeMethod
76     public void initMocks(){
77         MockitoAnnotations.initMocks(this);
78     }
79
80     @Test
81     public void testGetSpecificPnf(){
82         Pnf pnf = Pnf.builder().withPnfId("11111").build();
83         AaiResponse<Pnf> aaiResponse = new AaiResponse<>(pnf, "aaaa", 200);
84         Mockito.doReturn(aaiResponse).when(aaiClientInterface).getSpecificPnf(Mockito.anyString());
85         AaiResponse<Pnf> specificPnf = aaiService.getSpecificPnf("1345667");
86         assertNotNull(specificPnf);
87         pnf = specificPnf.getT();
88         assertNotNull(pnf);
89         assertEquals("11111",pnf.getPnfId());
90         assertEquals("aaaa",specificPnf.getErrorMessage());
91         assertEquals(200,specificPnf.getHttpCode());
92     }
93
94     @Test
95     public void testPnfByRegion(){
96         AaiGetPnfResponse aaiGetPnfResponse = new AaiGetPnfResponse();
97         AaiResponse<AaiGetPnfResponse> aaiResponse = new AaiResponse<>(aaiGetPnfResponse, "", 200);
98         Mockito.doReturn(aaiResponse).when(aaiClientInterface).getPNFData(Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString());
99         AaiResponse<AaiGetPnfResponse> aaiGetPnfResponseWrapper = aaiService.getPNFData("1345667", "1345667", "1345667", "1345667", "1345667", "1345667", "1345667");
100         assertNotNull(aaiGetPnfResponseWrapper);
101         aaiGetPnfResponse = aaiGetPnfResponseWrapper.getT();
102         assertNotNull(aaiGetPnfResponse);
103     }
104
105     @Test
106     public void testGetAssociatedPnfs(){
107         ServiceRelationships serviceRelationships = createServiceRelationships();
108         AaiResponse<ServiceRelationships> aaiResponse = new AaiResponse<>(serviceRelationships, null, 200);
109         Mockito.doReturn(aaiResponse).when(aaiClientInterface).getServiceInstance(Mockito.anyString(), Mockito.anyString(), Mockito.anyString());
110
111         LogicalLinkResponse logicalLinkResponse = createLogicalLinkResponse();
112         AaiResponse<LogicalLinkResponse> aaiResponse1 = new AaiResponse<>(logicalLinkResponse, null, 200);
113         Mockito.doReturn(aaiResponse1).when(aaiClientInterface).getLogicalLink("SANITY6758cce9%3ALAG1992%7CSANITY6785cce9%3ALAG1961");
114
115         List<String> pnfList = aaiService.getServiceInstanceAssociatedPnfs("123", "456", "789");
116         assertNotNull(pnfList);
117         assertEquals(1, pnfList.size());
118         assertEquals("SANITY6785cce9", pnfList.get(0));
119     }
120
121     private ServiceRelationships createServiceRelationships() {
122         ServiceRelationships serviceRelationships = new ServiceRelationships();
123         serviceRelationships.setServiceInstanceName("test service");
124
125         RelationshipData logicalLinksRelationshipData = new RelationshipData();
126         logicalLinksRelationshipData.setRelationshipKey("logical-link.link-name");
127         logicalLinksRelationshipData.setRelationshipValue("SANITY6758cce9:LAG1992|SANITY6785cce9:LAG1961");
128
129         Relationship logicalLinksRelationship = new Relationship();
130         logicalLinksRelationship.setRelatedTo("logical-link");
131         logicalLinksRelationship.setRelationDataList(Arrays.asList(logicalLinksRelationshipData));
132
133         RelationshipList logicalLinksRelationshipsList = new RelationshipList();
134         logicalLinksRelationshipsList.setRelationship(Arrays.asList(logicalLinksRelationship));
135
136         serviceRelationships.setRelationshipList(logicalLinksRelationshipsList);
137         return serviceRelationships;
138     }
139
140     private LogicalLinkResponse createLogicalLinkResponse() {
141         LogicalLinkResponse logicalLinkResponse = new LogicalLinkResponse();
142         logicalLinkResponse.setLinkName("SANITY6758cce9:LAG1992|SANITY6785cce9:LAG1961");
143
144         RelationshipData lagInterfaceRelationshipData = new RelationshipData();
145         lagInterfaceRelationshipData.setRelationshipKey("pnf.pnf-name");
146         lagInterfaceRelationshipData.setRelationshipValue("SANITY6785cce9");
147
148         Relationship lagInterfaceRelationship = new Relationship();
149         lagInterfaceRelationship.setRelatedTo("lag-interface");
150         lagInterfaceRelationship.setRelationDataList(Arrays.asList(lagInterfaceRelationshipData));
151
152         RelationshipList lagInterfaceRelationshipsList = new RelationshipList();
153         lagInterfaceRelationshipsList.setRelationship(Arrays.asList(lagInterfaceRelationship));
154
155         logicalLinkResponse.setRelationshipList(lagInterfaceRelationshipsList);
156
157         return logicalLinkResponse;
158     }
159
160     @DataProvider
161     public static Object[][] getTenantsData() {
162         return new Object[][] {
163                 {"customer1", "serviceType1", "tenant1", "customer1", "serviceType1", "tenant1", "id-1", true},
164                 {"customer1", "serviceType1", "tenant2", "customer1", "serviceType1", "tenant1", "tenant2", false},
165                 {"customer1", "serviceType1", null, "customer1", "serviceType1", "tenant1", "tenant2", true},
166                 {"customer2", "serviceType1", "tenant1", "customer1", "serviceType1", "tenant1", "id-1", false},
167                 {"customer1", "serviceType2", "tenant1", "customer1", "serviceType1", "tenant1", "id-1", false},
168                 {"customer2", "serviceType1", null, "customer1", "serviceType1", "tenant1", "id-1", false},
169                 {"customer1", "serviceType2", null, "customer1", "serviceType1", "tenant1", "id-1", false},
170         };
171     }
172
173     @Test(dataProvider = "getTenantsData")
174     public void testGetTenants(String userGlobalCustomerId, String userServiceType, String userTenantName,
175                                 String serviceGlobalCustomerId, String serviceServiceType, String serviceTenantName,
176                                 String serviceTenantId, boolean expectedIsPermitted) {
177         GetTenantsResponse[] getTenantsResponses = new GetTenantsResponse[] {new GetTenantsResponse(null, null, serviceTenantName, serviceTenantId, false)};
178         AaiResponse<GetTenantsResponse[]> aaiResponse = new AaiResponse<>(getTenantsResponses, null, 200);
179         Mockito.doReturn(aaiResponse).when(aaiClientInterface).getTenants(serviceGlobalCustomerId, serviceServiceType);
180
181         RoleValidator roleValidatorMock = mock(RoleValidator.class);
182         when(roleValidatorMock.isTenantPermitted(
183             eq(userGlobalCustomerId), eq(userServiceType),
184             (userTenantName == null) ? anyString() : eq(userTenantName))
185         ).thenReturn(true);
186
187         AaiResponse<GetTenantsResponse[]> actualTenants = aaiService.getTenants(serviceGlobalCustomerId, serviceServiceType, roleValidatorMock);
188
189         assertThat(actualTenants.getT(), arrayWithSize(1));
190         assertThat(actualTenants.getT()[0].tenantName, equalTo(serviceTenantName));
191         assertThat(actualTenants.getT()[0].isPermitted, equalTo(expectedIsPermitted));
192     }
193
194     @DataProvider
195     public static Object[][] instanceGroupsRoleAndType() {
196         return new Object[][]{
197                 {"group role & type are same as requested", ImmutableMap.of("SERVICE-ACCESS", "LOAD-GROUP"), false},
198                 {"1 group properties is same as requested, 1 is not same", ImmutableMap.of("SERVICE-ACCESS", "LOAD-GROUP",
199                                                                     "dummy", "dummy"), false},
200                 {"only group type is same as requested", ImmutableMap.of("dummy", "LOAD-GROUP"), true},
201                 {"only group role is same as requested", ImmutableMap.of("SERVICE-ACCESS", "dummy"), true},
202                 {"group role & type are not same as requested", ImmutableMap.of("dummy", "dummy"), true},
203                 {"vnf is not related to instance group", ImmutableMap.of(), true},
204         };
205     }
206
207     @Test(dataProvider = "instanceGroupsRoleAndType")
208     public void testFilterInstanceGroupByRoleAndType(String description, Map<String, String> instanceGroupsProperties, boolean expectedMatch) {
209         List<AAITreeNode> instanceGroups = new ArrayList<>();
210
211         for (Map.Entry<String, String> props: instanceGroupsProperties.entrySet()) {
212             AAITreeNode instanceGroup = new AAITreeNode();
213             Map<String, Object> additionalProperties = ImmutableMap.of(
214                     "instance-group-role", props.getKey(),
215                     "instance-group-type", props.getValue());
216             instanceGroup.setAdditionalProperties(additionalProperties);
217             instanceGroups.add(instanceGroup);
218         }
219
220         boolean anyMatch = aaiService.isInstanceGroupsNotMatchRoleAndType(instanceGroups, "SERVICE-ACCESS", "LOAD-GROUP");
221         assertThat(anyMatch, equalTo(expectedMatch));
222     }
223
224     @DataProvider
225     public static Object[][] dataToDestroy() {
226         return new Object[][]{
227             {"nothing"}, {"relationship-list"}, {"relationship"}, {"relationship-data"} ,{"owning-entity-id"}
228         };
229     }
230
231
232     @Test(dataProvider = "dataToDestroy")
233     public void relatedOwningEntityId_givenInstanceAndOptionalError_extractCorrectlyOrReturnNull(String dataToDestroy) throws JsonProcessingException {
234         ServiceInstance serviceInstance = new ObjectMapper().readValue((""
235             + "{ "
236             + "  \"service-instance-id\": \"5d521981-33be-4bb5-bb20-5616a9c52a5a\", "
237             + "  \"service-instance-name\": \"dfgh\", "
238             + "  \"service-type\": \"\", "
239             + "  \"service-role\": \"\", "
240             + "  \"environment-context\": \"null\", "
241             + "  \"workload-context\": \"null\", "
242             + "  \"model-invariant-id\": \"331a194d-9248-4533-88bc-62c812ccb5c1\", "
243             + "  \"model-version-id\": \"171b3887-e73e-479d-8ef8-2690bf74f2aa\", "
244             + "  \"resource-version\": \"1508832105498\", "
245             + "  \"orchestration-status\": \"Active\", "
246             + "  \"relationship-list\": { "
247             + "    \"relationship\": [ "
248             + "      { "
249             + "        \"related-to\": \"project\", "
250             + "        \"related-link\": \"/aai/v11/business/projects/project/Kennedy\", "
251             + "        \"relationship-data\": [ "
252             + "          { "
253             + "            \"relationship-key\": \"project.project-name\", "
254             + "            \"relationship-value\": \"Kennedy\" "
255             + "          } "
256             + "        ] "
257             + "      }, "
258             + "      { "
259             + "        \"related-to\": \"owning-entity\", "
260             + "        \"related-link\": \"/aai/v11/business/owning-entities/owning-entity/4d4ecf59-41f1-40d4-818d-885234680a42\", "
261             + "        \"relationship-data\": [ "
262             + "          { "
263             + "            \"relationship-key\": \"owning-entity.owning-entity-id\", "
264             + "            \"relationship-value\": \"4d4ecf59-41f1-40d4-818d-885234680a42\" "
265             + "          } "
266             + "        ] "
267             + "      } "
268             + "    ] "
269             + "  } "
270             + "}").replace(dataToDestroy, "omitted"), ServiceInstance.class);
271
272         if (dataToDestroy.equals("nothing")) {
273             assertThat(aaiService.relatedOwningEntityId(serviceInstance), is("4d4ecf59-41f1-40d4-818d-885234680a42"));
274         } else {
275             assertThat(aaiService.relatedOwningEntityId(serviceInstance), is(nullValue()));
276         }
277     }
278
279 }