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.bl;
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;
29 import java.util.Arrays;
30 import java.util.Collections;
31 import java.util.List;
32 import org.mockito.InjectMocks;
33 import org.mockito.Mock;
34 import org.mockito.Mockito;
35 import org.mockito.MockitoAnnotations;
36 import org.onap.vid.aai.AaiClientInterface;
37 import org.onap.vid.aai.AaiResponse;
38 import org.onap.vid.aai.model.AaiGetPnfResponse;
39 import org.onap.vid.aai.model.AaiGetPnfs.Pnf;
40 import org.onap.vid.aai.model.AaiGetTenatns.GetTenantsResponse;
41 import org.onap.vid.aai.model.LogicalLinkResponse;
42 import org.onap.vid.aai.model.Relationship;
43 import org.onap.vid.aai.model.RelationshipData;
44 import org.onap.vid.aai.model.RelationshipList;
45 import org.onap.vid.aai.model.ServiceRelationships;
46 import org.onap.vid.roles.Role;
47 import org.onap.vid.roles.RoleValidator;
48 import org.onap.vid.services.AaiServiceImpl;
49 import org.testng.annotations.BeforeMethod;
50 import org.testng.annotations.DataProvider;
51 import org.testng.annotations.Test;
53 public class AaiServiceTest {
56 private AaiServiceImpl aaiService;
59 private AaiClientInterface aaiClientInterface;
64 public void initMocks(){
65 MockitoAnnotations.initMocks(this);
69 public void testGetSpecificPnf(){
71 pnf.setPnfId("11111");
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();
78 assertEquals("11111",pnf.getPnfId());
79 assertEquals("aaaa",specificPnf.getErrorMessage());
80 assertEquals(200,specificPnf.getHttpCode());
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);
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());
100 LogicalLinkResponse logicalLinkResponse = createLogicalLinkResponse();
101 AaiResponse<LogicalLinkResponse> aaiResponse1 = new AaiResponse<>(logicalLinkResponse, null, 200);
102 Mockito.doReturn(aaiResponse1).when(aaiClientInterface).getLogicalLink("SANITY6758cce9%3ALAG1992%7CSANITY6785cce9%3ALAG1961");
104 List<String> pnfList = aaiService.getServiceInstanceAssociatedPnfs("123", "456", "789");
105 assertNotNull(pnfList);
106 assertEquals(1, pnfList.size());
107 assertEquals("SANITY6785cce9", pnfList.get(0));
110 private ServiceRelationships createServiceRelationships() {
111 ServiceRelationships serviceRelationships = new ServiceRelationships();
112 serviceRelationships.setServiceInstanceName("test service");
114 RelationshipData logicalLinksRelationshipData = new RelationshipData();
115 logicalLinksRelationshipData.setRelationshipKey("logical-link.link-name");
116 logicalLinksRelationshipData.setRelationshipValue("SANITY6758cce9:LAG1992|SANITY6785cce9:LAG1961");
118 Relationship logicalLinksRelationship = new Relationship();
119 logicalLinksRelationship.setRelatedTo("logical-link");
120 logicalLinksRelationship.setRelationDataList(Arrays.asList(logicalLinksRelationshipData));
122 RelationshipList logicalLinksRelationshipsList = new RelationshipList();
123 logicalLinksRelationshipsList.setRelationship(Arrays.asList(logicalLinksRelationship));
125 serviceRelationships.setRelationshipList(logicalLinksRelationshipsList);
126 return serviceRelationships;
129 private LogicalLinkResponse createLogicalLinkResponse() {
130 LogicalLinkResponse logicalLinkResponse = new LogicalLinkResponse();
131 logicalLinkResponse.setLinkName("SANITY6758cce9:LAG1992|SANITY6785cce9:LAG1961");
133 RelationshipData lagInterfaceRelationshipData = new RelationshipData();
134 lagInterfaceRelationshipData.setRelationshipKey("pnf.pnf-name");
135 lagInterfaceRelationshipData.setRelationshipValue("SANITY6785cce9");
137 Relationship lagInterfaceRelationship = new Relationship();
138 lagInterfaceRelationship.setRelatedTo("lag-interface");
139 lagInterfaceRelationship.setRelationDataList(Arrays.asList(lagInterfaceRelationshipData));
141 RelationshipList lagInterfaceRelationshipsList = new RelationshipList();
142 lagInterfaceRelationshipsList.setRelationship(Arrays.asList(lagInterfaceRelationship));
144 logicalLinkResponse.setRelationshipList(lagInterfaceRelationshipsList);
146 return logicalLinkResponse;
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},
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, expectedIsPermitted)};
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));
172 AaiResponse<GetTenantsResponse[]> actualTenants = aaiService.getTenants(serviceGlobalCustomerId, serviceServiceType, roleValidator);
174 assertThat(actualTenants.getT(), arrayWithSize(1));
175 assertThat(actualTenants.getT()[0].tenantName, equalTo(serviceTenantName));
176 //assertThat(actualTenants.getT()[0].isPermitted, equalTo(expectedIsPermitted));