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.mockito.InjectMocks;
47 import org.mockito.Mock;
48 import org.mockito.Mockito;
49 import org.mockito.MockitoAnnotations;
50 import org.onap.vid.aai.AaiClientInterface;
51 import org.onap.vid.aai.AaiResponse;
52 import org.onap.vid.aai.ServiceInstance;
53 import org.onap.vid.aai.model.AaiGetPnfResponse;
54 import org.onap.vid.aai.model.AaiGetPnfs.Pnf;
55 import org.onap.vid.aai.model.AaiGetTenatns.GetTenantsResponse;
56 import org.onap.vid.aai.model.LogicalLinkResponse;
57 import org.onap.vid.aai.model.OwningEntityResponse;
58 import org.onap.vid.aai.model.Relationship;
59 import org.onap.vid.aai.model.RelationshipData;
60 import org.onap.vid.aai.model.RelationshipList;
61 import org.onap.vid.aai.model.ServiceRelationships;
62 import org.onap.vid.model.ServiceInstanceSearchResult;
63 import org.onap.vid.model.aaiTree.AAITreeNode;
64 import org.onap.vid.roles.RoleValidator;
65 import org.onap.vid.roles.RoleValidatorFactory;
66 import org.onap.vid.roles.WithPermissionProperties;
67 import org.onap.vid.testUtils.TestUtils;
68 import org.testng.annotations.BeforeMethod;
69 import org.testng.annotations.DataProvider;
70 import org.testng.annotations.Test;
72 public class AaiServiceTest {
75 private AaiServiceImpl aaiService;
78 private AaiClientInterface aaiClientInterface;
81 private RoleValidatorFactory roleValidatorFactory;
84 public void initMocks(){
85 MockitoAnnotations.initMocks(this);
89 public void testGetSpecificPnf(){
90 Pnf pnf = Pnf.builder().withPnfId("11111").build();
91 AaiResponse<Pnf> aaiResponse = new AaiResponse<>(pnf, "aaaa", 200);
92 Mockito.doReturn(aaiResponse).when(aaiClientInterface).getSpecificPnf(Mockito.anyString());
93 AaiResponse<Pnf> specificPnf = aaiService.getSpecificPnf("1345667");
94 assertNotNull(specificPnf);
95 pnf = specificPnf.getT();
97 assertEquals("11111",pnf.getPnfId());
98 assertEquals("aaaa",specificPnf.getErrorMessage());
99 assertEquals(200,specificPnf.getHttpCode());
103 public void testPnfByRegion(){
104 AaiGetPnfResponse aaiGetPnfResponse = new AaiGetPnfResponse();
105 AaiResponse<AaiGetPnfResponse> aaiResponse = new AaiResponse<>(aaiGetPnfResponse, "", 200);
106 Mockito.doReturn(aaiResponse).when(aaiClientInterface).getPNFData(Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString());
107 AaiResponse<AaiGetPnfResponse> aaiGetPnfResponseWrapper = aaiService.getPNFData("1345667", "1345667", "1345667", "1345667", "1345667", "1345667", "1345667");
108 assertNotNull(aaiGetPnfResponseWrapper);
109 aaiGetPnfResponse = aaiGetPnfResponseWrapper.getT();
110 assertNotNull(aaiGetPnfResponse);
114 public void testGetAssociatedPnfs(){
115 ServiceRelationships serviceRelationships = createServiceRelationships();
116 AaiResponse<ServiceRelationships> aaiResponse = new AaiResponse<>(serviceRelationships, null, 200);
117 Mockito.doReturn(aaiResponse).when(aaiClientInterface).getServiceInstance(Mockito.anyString(), Mockito.anyString(), Mockito.anyString());
119 LogicalLinkResponse logicalLinkResponse = createLogicalLinkResponse();
120 AaiResponse<LogicalLinkResponse> aaiResponse1 = new AaiResponse<>(logicalLinkResponse, null, 200);
121 Mockito.doReturn(aaiResponse1).when(aaiClientInterface).getLogicalLink("SANITY6758cce9%3ALAG1992%7CSANITY6785cce9%3ALAG1961");
123 List<String> pnfList = aaiService.getServiceInstanceAssociatedPnfs("123", "456", "789");
124 assertNotNull(pnfList);
125 assertEquals(1, pnfList.size());
126 assertEquals("SANITY6785cce9", pnfList.get(0));
129 private ServiceRelationships createServiceRelationships() {
130 ServiceRelationships serviceRelationships = new ServiceRelationships();
131 serviceRelationships.setServiceInstanceName("test service");
133 RelationshipData logicalLinksRelationshipData = new RelationshipData();
134 logicalLinksRelationshipData.setRelationshipKey("logical-link.link-name");
135 logicalLinksRelationshipData.setRelationshipValue("SANITY6758cce9:LAG1992|SANITY6785cce9:LAG1961");
137 Relationship logicalLinksRelationship = new Relationship();
138 logicalLinksRelationship.setRelatedTo("logical-link");
139 logicalLinksRelationship.setRelationDataList(Arrays.asList(logicalLinksRelationshipData));
141 RelationshipList logicalLinksRelationshipsList = new RelationshipList();
142 logicalLinksRelationshipsList.setRelationship(Arrays.asList(logicalLinksRelationship));
144 serviceRelationships.setRelationshipList(logicalLinksRelationshipsList);
145 return serviceRelationships;
148 private LogicalLinkResponse createLogicalLinkResponse() {
149 LogicalLinkResponse logicalLinkResponse = new LogicalLinkResponse();
150 logicalLinkResponse.setLinkName("SANITY6758cce9:LAG1992|SANITY6785cce9:LAG1961");
152 RelationshipData lagInterfaceRelationshipData = new RelationshipData();
153 lagInterfaceRelationshipData.setRelationshipKey("pnf.pnf-name");
154 lagInterfaceRelationshipData.setRelationshipValue("SANITY6785cce9");
156 Relationship lagInterfaceRelationship = new Relationship();
157 lagInterfaceRelationship.setRelatedTo("lag-interface");
158 lagInterfaceRelationship.setRelationDataList(Arrays.asList(lagInterfaceRelationshipData));
160 RelationshipList lagInterfaceRelationshipsList = new RelationshipList();
161 lagInterfaceRelationshipsList.setRelationship(Arrays.asList(lagInterfaceRelationship));
163 logicalLinkResponse.setRelationshipList(lagInterfaceRelationshipsList);
165 return logicalLinkResponse;
169 public static Object[][] getTenantsData() {
170 return new Object[][] {
171 {"customer1", "serviceType1", "tenant1", "customer1", "serviceType1", "tenant1", "id-1", true},
172 {"customer1", "serviceType1", "tenant2", "customer1", "serviceType1", "tenant1", "tenant2", false},
173 {"customer1", "serviceType1", null, "customer1", "serviceType1", "tenant1", "tenant2", true},
174 {"customer2", "serviceType1", "tenant1", "customer1", "serviceType1", "tenant1", "id-1", false},
175 {"customer1", "serviceType2", "tenant1", "customer1", "serviceType1", "tenant1", "id-1", false},
176 {"customer2", "serviceType1", null, "customer1", "serviceType1", "tenant1", "id-1", false},
177 {"customer1", "serviceType2", null, "customer1", "serviceType1", "tenant1", "id-1", false},
181 @Test(dataProvider = "getTenantsData")
182 public void testGetTenants(String userGlobalCustomerId, String userServiceType, String userTenantName,
183 String serviceGlobalCustomerId, String serviceServiceType, String serviceTenantName,
184 String serviceTenantId, boolean expectedIsPermitted) {
185 GetTenantsResponse[] getTenantsResponses = new GetTenantsResponse[] {new GetTenantsResponse(null, null, serviceTenantName, serviceTenantId, false)};
186 AaiResponse<GetTenantsResponse[]> aaiResponse = new AaiResponse<>(getTenantsResponses, null, 200);
187 Mockito.doReturn(aaiResponse).when(aaiClientInterface).getTenants(serviceGlobalCustomerId, serviceServiceType);
189 RoleValidator roleValidatorMock = mock(RoleValidator.class);
190 when(roleValidatorMock.isTenantPermitted(
191 eq(userGlobalCustomerId), eq(userServiceType),
192 (userTenantName == null) ? anyString() : eq(userTenantName))
195 AaiResponse<GetTenantsResponse[]> actualTenants = aaiService.getTenants(serviceGlobalCustomerId, serviceServiceType, roleValidatorMock);
197 assertThat(actualTenants.getT(), arrayWithSize(1));
198 assertThat(actualTenants.getT()[0].tenantName, equalTo(serviceTenantName));
199 assertThat(actualTenants.getT()[0].isPermitted, equalTo(expectedIsPermitted));
203 public static Object[][] instanceGroupsRoleAndType() {
204 return new Object[][]{
205 {"group role & type are same as requested", ImmutableMap.of("SERVICE-ACCESS", "LOAD-GROUP"), false},
206 {"1 group properties is same as requested, 1 is not same", ImmutableMap.of("SERVICE-ACCESS", "LOAD-GROUP",
207 "dummy", "dummy"), false},
208 {"only group type is same as requested", ImmutableMap.of("dummy", "LOAD-GROUP"), true},
209 {"only group role is same as requested", ImmutableMap.of("SERVICE-ACCESS", "dummy"), true},
210 {"group role & type are not same as requested", ImmutableMap.of("dummy", "dummy"), true},
211 {"vnf is not related to instance group", ImmutableMap.of(), true},
215 @Test(dataProvider = "instanceGroupsRoleAndType")
216 public void testFilterInstanceGroupByRoleAndType(String description, Map<String, String> instanceGroupsProperties, boolean expectedMatch) {
217 List<AAITreeNode> instanceGroups = new ArrayList<>();
219 for (Map.Entry<String, String> props: instanceGroupsProperties.entrySet()) {
220 AAITreeNode instanceGroup = new AAITreeNode();
221 Map<String, Object> additionalProperties = ImmutableMap.of(
222 "instance-group-role", props.getKey(),
223 "instance-group-type", props.getValue());
224 instanceGroup.setAdditionalProperties(additionalProperties);
225 instanceGroups.add(instanceGroup);
228 boolean anyMatch = aaiService.isInstanceGroupsNotMatchRoleAndType(instanceGroups, "SERVICE-ACCESS", "LOAD-GROUP");
229 assertThat(anyMatch, equalTo(expectedMatch));
233 public static Object[][] dataToDestroy() {
234 return new Object[][]{
235 {"nothing"}, {"relationship-list"}, {"relationship"}, {"relationship-data"} ,{"owning-entity-id"}
240 @Test(dataProvider = "dataToDestroy")
241 public void relatedOwningEntityId_givenInstanceAndOptionalError_extractCorrectlyOrReturnNull(String dataToDestroy) throws JsonProcessingException {
242 ServiceInstance serviceInstance = new ObjectMapper().readValue((""
244 + " \"service-instance-id\": \"5d521981-33be-4bb5-bb20-5616a9c52a5a\", "
245 + " \"service-instance-name\": \"dfgh\", "
246 + " \"service-type\": \"\", "
247 + " \"service-role\": \"\", "
248 + " \"environment-context\": \"null\", "
249 + " \"workload-context\": \"null\", "
250 + " \"model-invariant-id\": \"331a194d-9248-4533-88bc-62c812ccb5c1\", "
251 + " \"model-version-id\": \"171b3887-e73e-479d-8ef8-2690bf74f2aa\", "
252 + " \"resource-version\": \"1508832105498\", "
253 + " \"orchestration-status\": \"Active\", "
254 + " \"relationship-list\": { "
255 + " \"relationship\": [ "
257 + " \"related-to\": \"project\", "
258 + " \"related-link\": \"/aai/v11/business/projects/project/Kennedy\", "
259 + " \"relationship-data\": [ "
261 + " \"relationship-key\": \"project.project-name\", "
262 + " \"relationship-value\": \"Kennedy\" "
267 + " \"related-to\": \"owning-entity\", "
268 + " \"related-link\": \"/aai/v11/business/owning-entities/owning-entity/4d4ecf59-41f1-40d4-818d-885234680a42\", "
269 + " \"relationship-data\": [ "
271 + " \"relationship-key\": \"owning-entity.owning-entity-id\", "
272 + " \"relationship-value\": \"4d4ecf59-41f1-40d4-818d-885234680a42\" "
278 + "}").replace(dataToDestroy, "omitted"), ServiceInstance.class);
280 if (dataToDestroy.equals("nothing")) {
281 assertThat(aaiService.relatedOwningEntityId(serviceInstance), is("4d4ecf59-41f1-40d4-818d-885234680a42"));
283 assertThat(aaiService.relatedOwningEntityId(serviceInstance), is(nullValue()));
288 public void testGetServicesByOwningEntityId() {
289 List<String> owningEntityIds = ImmutableList.of("43b8a85a-0421-4265-9069-117dd6526b8a", "26dcc4aa-725a-447d-8346-aa26dfaa4eb7");
290 RoleValidator roleValidator = mock(RoleValidator.class);
292 OwningEntityResponse owningEntityResponse = TestUtils.readJsonResourceFileAsObject("/responses/aai/listServicesByOwningEntity.json", OwningEntityResponse.class);
293 when(roleValidator.isServicePermitted(any(WithPermissionProperties.class))).thenReturn(true);
294 when(aaiClientInterface.getServicesByOwningEntityId(owningEntityIds)).thenReturn(new AaiResponse<>(owningEntityResponse, "", 200));
295 List<ServiceInstanceSearchResult> result = aaiService.getServicesByOwningEntityId(owningEntityIds, roleValidator);
298 ServiceInstanceSearchResult expected1 = new ServiceInstanceSearchResult(
299 "af9d52f9-13b2-4657-a198-463677f82dc0", "256cddb4-3aa1-43cc-a08f-315bb50b275e", "MSO-dev-service-type", "xbghrftgr_shani", null, null, null, null, true);
300 ServiceInstanceSearchResult expected2 = new ServiceInstanceSearchResult(
301 "49769492-5def-4c89-8e73-b236f958fa40", "e02fd6f2-7fc2-434b-a92d-15abdb24b68d", "JUST-another-service-type", "fghghfhgf", null, null, null, null, true);
302 ServiceInstanceSearchResult expected3 = new ServiceInstanceSearchResult(
303 "1d8fd482-2f53-4d62-a7bd-20e4bab14c45", "256cddb4-3aa1-43cc-a08f-315bb50b275e", "MSO-dev-service-type", "Bryant", null, null, null, null, true);
305 assertThat(result, jsonEquals(ImmutableList.of(expected1, expected2, expected3)).when(IGNORING_ARRAY_ORDER).whenIgnoringPaths("[*].subscriberName")); //ignore in array