2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.vid.services;
23 import static net.javacrumbs.jsonunit.JsonMatchers.jsonEquals;
24 import static net.javacrumbs.jsonunit.core.Option.IGNORING_ARRAY_ORDER;
25 import static org.hamcrest.CoreMatchers.is;
26 import static org.hamcrest.MatcherAssert.assertThat;
27 import static org.hamcrest.Matchers.arrayWithSize;
28 import static org.hamcrest.Matchers.equalTo;
29 import static org.hamcrest.Matchers.nullValue;
30 import static org.mockito.ArgumentMatchers.any;
31 import static org.mockito.ArgumentMatchers.anyString;
32 import static org.mockito.ArgumentMatchers.eq;
33 import static org.mockito.Mockito.mock;
34 import static org.mockito.Mockito.when;
35 import static org.testng.Assert.assertEquals;
36 import static org.testng.Assert.assertNotNull;
38 import com.fasterxml.jackson.core.JsonProcessingException;
39 import com.fasterxml.jackson.databind.ObjectMapper;
40 import com.google.common.collect.ImmutableList;
41 import com.google.common.collect.ImmutableMap;
42 import java.util.ArrayList;
43 import java.util.Arrays;
44 import java.util.List;
46 import org.jetbrains.annotations.NotNull;
47 import org.mockito.InjectMocks;
48 import org.mockito.Mock;
49 import org.mockito.Mockito;
50 import org.mockito.MockitoAnnotations;
51 import org.onap.vid.aai.AaiClientInterface;
52 import org.onap.vid.aai.AaiResponse;
53 import org.onap.vid.aai.ServiceInstance;
54 import org.onap.vid.aai.model.AaiGetPnfResponse;
55 import org.onap.vid.aai.model.AaiGetPnfs.Pnf;
56 import org.onap.vid.aai.model.AaiGetTenatns.GetTenantsResponse;
57 import org.onap.vid.aai.model.LogicalLinkResponse;
58 import org.onap.vid.aai.model.OwningEntityResponse;
59 import org.onap.vid.aai.model.ProjectResponse;
60 import org.onap.vid.aai.model.Relationship;
61 import org.onap.vid.aai.model.RelationshipData;
62 import org.onap.vid.aai.model.RelationshipList;
63 import org.onap.vid.aai.model.ServiceRelationships;
64 import org.onap.vid.model.ServiceInstanceSearchResult;
65 import org.onap.vid.model.aaiTree.AAITreeNode;
66 import org.onap.vid.roles.RoleValidator;
67 import org.onap.vid.roles.RoleValidatorFactory;
68 import org.onap.vid.roles.WithPermissionProperties;
69 import org.onap.vid.testUtils.TestUtils;
70 import org.testng.annotations.BeforeMethod;
71 import org.testng.annotations.DataProvider;
72 import org.testng.annotations.Test;
74 public class AaiServiceTest {
77 private AaiServiceImpl aaiService;
80 private AaiClientInterface aaiClientInterface;
83 private RoleValidatorFactory roleValidatorFactory;
86 public void initMocks(){
87 MockitoAnnotations.initMocks(this);
91 public void testGetSpecificPnf(){
92 Pnf pnf = Pnf.builder().withPnfId("11111").build();
93 AaiResponse<Pnf> aaiResponse = new AaiResponse<>(pnf, "aaaa", 200);
94 Mockito.doReturn(aaiResponse).when(aaiClientInterface).getSpecificPnf(Mockito.anyString());
95 AaiResponse<Pnf> specificPnf = aaiService.getSpecificPnf("1345667");
96 assertNotNull(specificPnf);
97 pnf = specificPnf.getT();
99 assertEquals("11111",pnf.getPnfId());
100 assertEquals("aaaa",specificPnf.getErrorMessage());
101 assertEquals(200,specificPnf.getHttpCode());
105 public void testPnfByRegion(){
106 AaiGetPnfResponse aaiGetPnfResponse = new AaiGetPnfResponse();
107 AaiResponse<AaiGetPnfResponse> aaiResponse = new AaiResponse<>(aaiGetPnfResponse, "", 200);
108 Mockito.doReturn(aaiResponse).when(aaiClientInterface).getPNFData(Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString());
109 AaiResponse<AaiGetPnfResponse> aaiGetPnfResponseWrapper = aaiService.getPNFData("1345667", "1345667", "1345667", "1345667", "1345667", "1345667", "1345667");
110 assertNotNull(aaiGetPnfResponseWrapper);
111 aaiGetPnfResponse = aaiGetPnfResponseWrapper.getT();
112 assertNotNull(aaiGetPnfResponse);
116 public void testGetAssociatedPnfs(){
117 ServiceRelationships serviceRelationships = createServiceRelationships();
118 AaiResponse<ServiceRelationships> aaiResponse = new AaiResponse<>(serviceRelationships, null, 200);
119 Mockito.doReturn(aaiResponse).when(aaiClientInterface).getServiceInstance(Mockito.anyString(), Mockito.anyString(), Mockito.anyString());
121 LogicalLinkResponse logicalLinkResponse = createLogicalLinkResponse();
122 AaiResponse<LogicalLinkResponse> aaiResponse1 = new AaiResponse<>(logicalLinkResponse, null, 200);
123 Mockito.doReturn(aaiResponse1).when(aaiClientInterface).getLogicalLink("SANITY6758cce9%3ALAG1992%7CSANITY6785cce9%3ALAG1961");
125 List<String> pnfList = aaiService.getServiceInstanceAssociatedPnfs("123", "456", "789");
126 assertNotNull(pnfList);
127 assertEquals(1, pnfList.size());
128 assertEquals("SANITY6785cce9", pnfList.get(0));
131 private ServiceRelationships createServiceRelationships() {
132 ServiceRelationships serviceRelationships = new ServiceRelationships();
133 serviceRelationships.setServiceInstanceName("test service");
135 RelationshipData logicalLinksRelationshipData = new RelationshipData();
136 logicalLinksRelationshipData.setRelationshipKey("logical-link.link-name");
137 logicalLinksRelationshipData.setRelationshipValue("SANITY6758cce9:LAG1992|SANITY6785cce9:LAG1961");
139 Relationship logicalLinksRelationship = new Relationship();
140 logicalLinksRelationship.setRelatedTo("logical-link");
141 logicalLinksRelationship.setRelationDataList(Arrays.asList(logicalLinksRelationshipData));
143 RelationshipList logicalLinksRelationshipsList = new RelationshipList();
144 logicalLinksRelationshipsList.setRelationship(Arrays.asList(logicalLinksRelationship));
146 serviceRelationships.setRelationshipList(logicalLinksRelationshipsList);
147 return serviceRelationships;
150 private LogicalLinkResponse createLogicalLinkResponse() {
151 LogicalLinkResponse logicalLinkResponse = new LogicalLinkResponse();
152 logicalLinkResponse.setLinkName("SANITY6758cce9:LAG1992|SANITY6785cce9:LAG1961");
154 RelationshipData lagInterfaceRelationshipData = new RelationshipData();
155 lagInterfaceRelationshipData.setRelationshipKey("pnf.pnf-name");
156 lagInterfaceRelationshipData.setRelationshipValue("SANITY6785cce9");
158 Relationship lagInterfaceRelationship = new Relationship();
159 lagInterfaceRelationship.setRelatedTo("lag-interface");
160 lagInterfaceRelationship.setRelationDataList(Arrays.asList(lagInterfaceRelationshipData));
162 RelationshipList lagInterfaceRelationshipsList = new RelationshipList();
163 lagInterfaceRelationshipsList.setRelationship(Arrays.asList(lagInterfaceRelationship));
165 logicalLinkResponse.setRelationshipList(lagInterfaceRelationshipsList);
167 return logicalLinkResponse;
171 public static Object[][] getTenantsData() {
172 return new Object[][] {
173 {"customer1", "serviceType1", "tenant1", "customer1", "serviceType1", "tenant1", "id-1", true},
174 {"customer1", "serviceType1", "tenant2", "customer1", "serviceType1", "tenant1", "tenant2", false},
175 {"customer1", "serviceType1", null, "customer1", "serviceType1", "tenant1", "tenant2", true},
176 {"customer2", "serviceType1", "tenant1", "customer1", "serviceType1", "tenant1", "id-1", false},
177 {"customer1", "serviceType2", "tenant1", "customer1", "serviceType1", "tenant1", "id-1", false},
178 {"customer2", "serviceType1", null, "customer1", "serviceType1", "tenant1", "id-1", false},
179 {"customer1", "serviceType2", null, "customer1", "serviceType1", "tenant1", "id-1", false},
183 @Test(dataProvider = "getTenantsData")
184 public void testGetTenants(String userGlobalCustomerId, String userServiceType, String userTenantName,
185 String serviceGlobalCustomerId, String serviceServiceType, String serviceTenantName,
186 String serviceTenantId, boolean expectedIsPermitted) {
187 GetTenantsResponse[] getTenantsResponses = new GetTenantsResponse[] {new GetTenantsResponse(null, null, serviceTenantName, serviceTenantId, false)};
188 AaiResponse<GetTenantsResponse[]> aaiResponse = new AaiResponse<>(getTenantsResponses, null, 200);
189 Mockito.doReturn(aaiResponse).when(aaiClientInterface).getTenants(serviceGlobalCustomerId, serviceServiceType);
191 RoleValidator roleValidatorMock = mock(RoleValidator.class);
192 when(roleValidatorMock.isTenantPermitted(
193 eq(userGlobalCustomerId), eq(userServiceType),
194 (userTenantName == null) ? anyString() : eq(userTenantName))
197 AaiResponse<GetTenantsResponse[]> actualTenants = aaiService.getTenants(serviceGlobalCustomerId, serviceServiceType, roleValidatorMock);
199 assertThat(actualTenants.getT(), arrayWithSize(1));
200 assertThat(actualTenants.getT()[0].tenantName, equalTo(serviceTenantName));
201 assertThat(actualTenants.getT()[0].isPermitted, equalTo(expectedIsPermitted));
205 public static Object[][] instanceGroupsRoleAndType() {
206 return new Object[][]{
207 {"group role & type are same as requested", ImmutableMap.of("SERVICE-ACCESS", "LOAD-GROUP"), false},
208 {"1 group properties is same as requested, 1 is not same", ImmutableMap.of("SERVICE-ACCESS", "LOAD-GROUP",
209 "dummy", "dummy"), false},
210 {"only group type is same as requested", ImmutableMap.of("dummy", "LOAD-GROUP"), true},
211 {"only group role is same as requested", ImmutableMap.of("SERVICE-ACCESS", "dummy"), true},
212 {"group role & type are not same as requested", ImmutableMap.of("dummy", "dummy"), true},
213 {"vnf is not related to instance group", ImmutableMap.of(), true},
217 @Test(dataProvider = "instanceGroupsRoleAndType")
218 public void testFilterInstanceGroupByRoleAndType(String description, Map<String, String> instanceGroupsProperties, boolean expectedMatch) {
219 List<AAITreeNode> instanceGroups = new ArrayList<>();
221 for (Map.Entry<String, String> props: instanceGroupsProperties.entrySet()) {
222 AAITreeNode instanceGroup = new AAITreeNode();
223 Map<String, Object> additionalProperties = ImmutableMap.of(
224 "instance-group-role", props.getKey(),
225 "instance-group-type", props.getValue());
226 instanceGroup.setAdditionalProperties(additionalProperties);
227 instanceGroups.add(instanceGroup);
230 boolean anyMatch = aaiService.isInstanceGroupsNotMatchRoleAndType(instanceGroups, "SERVICE-ACCESS", "LOAD-GROUP");
231 assertThat(anyMatch, equalTo(expectedMatch));
235 public static Object[][] dataToDestroy() {
236 return new Object[][]{
237 {"nothing"}, {"relationship-list"}, {"relationship"}, {"relationship-data"} ,{"owning-entity-id"}
242 @Test(dataProvider = "dataToDestroy")
243 public void relatedOwningEntityId_givenInstanceAndOptionalError_extractCorrectlyOrReturnNull(String dataToDestroy) throws JsonProcessingException {
244 ServiceInstance serviceInstance = new ObjectMapper().readValue((""
246 + " \"service-instance-id\": \"5d521981-33be-4bb5-bb20-5616a9c52a5a\", "
247 + " \"service-instance-name\": \"dfgh\", "
248 + " \"service-type\": \"\", "
249 + " \"service-role\": \"\", "
250 + " \"environment-context\": \"null\", "
251 + " \"workload-context\": \"null\", "
252 + " \"model-invariant-id\": \"331a194d-9248-4533-88bc-62c812ccb5c1\", "
253 + " \"model-version-id\": \"171b3887-e73e-479d-8ef8-2690bf74f2aa\", "
254 + " \"resource-version\": \"1508832105498\", "
255 + " \"orchestration-status\": \"Active\", "
256 + " \"relationship-list\": { "
257 + " \"relationship\": [ "
259 + " \"related-to\": \"project\", "
260 + " \"related-link\": \"/aai/v11/business/projects/project/Kennedy\", "
261 + " \"relationship-data\": [ "
263 + " \"relationship-key\": \"project.project-name\", "
264 + " \"relationship-value\": \"Kennedy\" "
269 + " \"related-to\": \"owning-entity\", "
270 + " \"related-link\": \"/aai/v11/business/owning-entities/owning-entity/4d4ecf59-41f1-40d4-818d-885234680a42\", "
271 + " \"relationship-data\": [ "
273 + " \"relationship-key\": \"owning-entity.owning-entity-id\", "
274 + " \"relationship-value\": \"4d4ecf59-41f1-40d4-818d-885234680a42\" "
280 + "}").replace(dataToDestroy, "omitted"), ServiceInstance.class);
282 if (dataToDestroy.equals("nothing")) {
283 assertThat(aaiService.relatedOwningEntityId(serviceInstance), is("4d4ecf59-41f1-40d4-818d-885234680a42"));
285 assertThat(aaiService.relatedOwningEntityId(serviceInstance), is(nullValue()));
290 public void testGetServicesByOwningEntityId() {
293 List<String> owningEntityIds = ImmutableList.of("43b8a85a-0421-4265-9069-117dd6526b8a", "26dcc4aa-725a-447d-8346-aa26dfaa4eb7");
294 OwningEntityResponse owningEntityResponse = TestUtils.readJsonResourceFileAsObject("/responses/aai/listServicesByOwningEntity.json", OwningEntityResponse.class);
295 when(aaiClientInterface.getServicesByOwningEntityId(owningEntityIds)).thenReturn(new AaiResponse<>(owningEntityResponse, "", 200));
296 RoleValidator roleValidator = createAlwaysTrueRoleValidator();
299 List<ServiceInstanceSearchResult> result = aaiService.getServicesByOwningEntityId(owningEntityIds, roleValidator);
302 ServiceInstanceSearchResult expected1 = new ServiceInstanceSearchResult(
303 "af9d52f9-13b2-4657-a198-463677f82dc0", "256cddb4-3aa1-43cc-a08f-315bb50b275e", "MSO-dev-service-type", "xbghrftgr_shani", null, null, null, null, true);
304 ServiceInstanceSearchResult expected2 = new ServiceInstanceSearchResult(
305 "49769492-5def-4c89-8e73-b236f958fa40", "e02fd6f2-7fc2-434b-a92d-15abdb24b68d", "JUST-another-service-type", "fghghfhgf", null, null, null, null, true);
306 ServiceInstanceSearchResult expected3 = new ServiceInstanceSearchResult(
307 "1d8fd482-2f53-4d62-a7bd-20e4bab14c45", "256cddb4-3aa1-43cc-a08f-315bb50b275e", "MSO-dev-service-type", "Bryant", null, null, null, null, true);
309 assertThat(result, jsonEquals(ImmutableList.of(expected1, expected2, expected3)).when(IGNORING_ARRAY_ORDER).whenIgnoringPaths("[*].subscriberName")); //ignore in array
313 private RoleValidator createAlwaysTrueRoleValidator() {
314 RoleValidator roleValidator = mock(RoleValidator.class);
315 when(roleValidator.isServicePermitted(any(WithPermissionProperties.class))).thenReturn(true);
316 return roleValidator;
320 public void testGetServicesByProjectNames() {
323 List<String> projectNames = ImmutableList.of("x1", "y2");
324 ProjectResponse projectResponse = TestUtils.readJsonResourceFileAsObject("/responses/aai/listServicesByProject.json", ProjectResponse.class);
325 when(aaiClientInterface.getServicesByProjectNames(projectNames)).thenReturn(new AaiResponse<>(projectResponse, "", 200));
326 RoleValidator roleValidator = createAlwaysTrueRoleValidator();
329 List<ServiceInstanceSearchResult> result = aaiService.getServicesByProjectNames(projectNames, roleValidator);
332 ServiceInstanceSearchResult expected1 = new ServiceInstanceSearchResult(
333 "3f826016-3ac9-4928-9561-beee75fd91d5", "a9a77d5a-123e-4ca2-9eb9-0b015d2ee0fb", "Emanuel", "Lital_SRIOV2_001", null, null, null, null, true);
334 ServiceInstanceSearchResult expected2 = new ServiceInstanceSearchResult(
335 "7e4f8130-5dee-47c4-8770-1abc5f5ded83", "3d15d7ea-4174-49b6-89ec-e569381f7231", "vMOG", "justAname", null, null, null, null, true);
336 ServiceInstanceSearchResult expected3 = new ServiceInstanceSearchResult(
337 "ff2d9326-1ef5-4760-aba0-0eaf372ae675", "a9a77d5a-123e-4ca2-9eb9-0b015d2ee0fb", "Yoda", "anotherName", null, null, null, null, true);
339 assertThat(result, jsonEquals(ImmutableList.of(expected1, expected2, expected3)).when(IGNORING_ARRAY_ORDER).whenIgnoringPaths("[*].subscriberName")); //ignore in array