diff options
author | Alexey Sandler <alexey.sandler@intl.att.com> | 2020-01-28 16:57:39 +0200 |
---|---|---|
committer | Alexey Sandler <alexey.sandler@intl.att.com> | 2020-01-28 16:57:39 +0200 |
commit | 35a9debcc64b05163612165043690ddb128b1293 (patch) | |
tree | 00c66d6ed8c6d512b729d872f154b10951799291 | |
parent | 22fb6d661b72bcb99e467c2289257d360d5e13a9 (diff) |
create RoleValidatorFactory component.
Issue-ID: VID-758
Signed-off-by: Alexey Sandler <alexey.sandler@intl.att.com>
Change-Id: Id444ddbe74b6d28d697e130caa73bd7bfae9ce52
10 files changed, 123 insertions, 213 deletions
diff --git a/vid-app-common/src/main/java/org/onap/vid/controller/AaiController.java b/vid-app-common/src/main/java/org/onap/vid/controller/AaiController.java index 563c9ff20..a9ce40bba 100644 --- a/vid-app-common/src/main/java/org/onap/vid/controller/AaiController.java +++ b/vid-app-common/src/main/java/org/onap/vid/controller/AaiController.java @@ -49,7 +49,6 @@ import org.onap.vid.aai.model.AaiGetTenatns.GetTenantsResponse; import org.onap.vid.aai.util.AAIRestInterface; import org.onap.vid.model.VersionByInvariantIdsRequest; import org.onap.vid.properties.Features; -import org.onap.vid.roles.Role; import org.onap.vid.roles.RoleProvider; import org.onap.vid.roles.RoleValidator; import org.onap.vid.services.AaiService; @@ -137,7 +136,7 @@ public class AaiController extends RestrictedBaseController { @RequestMapping(value = "/aai_get_services", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity<String> doGetServices(HttpServletRequest request) throws IOException { - RoleValidator roleValidator = RoleValidator.by(roleProvider.getUserRoles(request)); + RoleValidator roleValidator = roleProvider.getUserRolesValidator(request); AaiResponse subscriberList = aaiService.getServices(roleValidator); return aaiResponseToResponseEntity(subscriberList); @@ -225,7 +224,7 @@ public class AaiController extends RestrictedBaseController { @RequestMapping(value = "/aai_get_full_subscribers", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity<String> getFullSubscriberList(HttpServletRequest request) throws IOException { ResponseEntity<String> responseEntity; - RoleValidator roleValidator = RoleValidator.by(roleProvider.getUserRoles(request)); + RoleValidator roleValidator = roleProvider.getUserRolesValidator(request); SubscriberFilteredResults subscriberList = aaiService.getFullSubscriberList(roleValidator); if (subscriberList.getHttpCode() == 200) { responseEntity = new ResponseEntity<>(objectMapper.writeValueAsString(subscriberList.getSubscriberList()), @@ -256,8 +255,7 @@ public class AaiController extends RestrictedBaseController { @RequestMapping(value = "/aai_sub_details/{subscriberId}", method = RequestMethod.GET) public ResponseEntity<String> getSubscriberDetails(HttpServletRequest request, @PathVariable("subscriberId") String subscriberId, @RequestParam(value="omitServiceInstances", required = false, defaultValue = "false") boolean omitServiceInstances) throws IOException { - List<Role> roles = roleProvider.getUserRoles(request); - RoleValidator roleValidator = RoleValidator.by(roles); + RoleValidator roleValidator = roleProvider.getUserRolesValidator(request); AaiResponse subscriberData = aaiService.getSubscriberData(subscriberId, roleValidator, featureManager.isActive(Features.FLAG_1906_AAI_SUB_DETAILS_REDUCE_DEPTH) && omitServiceInstances); String httpMessage = subscriberData.getT() != null ? objectMapper.writeValueAsString(subscriberData.getT()) : subscriberData.getErrorMessage(); @@ -274,8 +272,7 @@ public class AaiController extends RestrictedBaseController { @RequestParam(value = "owningEntity", required = false) List<String> owningEntities) throws IOException { ResponseEntity responseEntity; - List<Role> roles = roleProvider.getUserRoles(request); - RoleValidator roleValidator = RoleValidator.by(roles); + RoleValidator roleValidator = roleProvider.getUserRolesValidator(request); AaiResponse<ServiceInstancesSearchResults> searchResult = aaiService .getServiceInstanceSearchResults(subscriberId, instanceIdentifier, roleValidator, owningEntities, projects); @@ -404,8 +401,7 @@ public class AaiController extends RestrictedBaseController { ResponseEntity responseEntity; try { - List<Role> roles = roleProvider.getUserRoles(request); - RoleValidator roleValidator = RoleValidator.by(roles); + RoleValidator roleValidator = roleProvider.getUserRolesValidator(request); AaiResponse<GetTenantsResponse[]> response = aaiService .getTenants(globalCustomerId, serviceType, roleValidator); if (response.getHttpCode() == 200) { diff --git a/vid-app-common/src/main/java/org/onap/vid/roles/RoleProvider.java b/vid-app-common/src/main/java/org/onap/vid/roles/RoleProvider.java index 898db332c..d9f2fdedf 100644 --- a/vid-app-common/src/main/java/org/onap/vid/roles/RoleProvider.java +++ b/vid-app-common/src/main/java/org/onap/vid/roles/RoleProvider.java @@ -58,16 +58,20 @@ public class RoleProvider { private Function<HttpServletRequest, Integer> getUserIdFunction; private Function<HttpServletRequest, Map> getRolesFunction; + private final RoleValidatorFactory roleValidatorFactory; @Autowired - public RoleProvider(AaiService aaiService) { + public RoleProvider(AaiService aaiService, RoleValidatorFactory roleValidatorFactory) { this.aaiService=aaiService; + this.roleValidatorFactory = roleValidatorFactory; getUserIdFunction = UserUtils::getUserId; getRolesFunction = UserUtils::getRoles; } - RoleProvider(AaiService aaiService, Function<HttpServletRequest, Integer> getUserIdFunction, Function<HttpServletRequest, Map> getRolesFunction) { + RoleProvider(AaiService aaiService, RoleValidatorFactory roleValidatorFactory, + Function<HttpServletRequest, Integer> getUserIdFunction, Function<HttpServletRequest, Map> getRolesFunction) { this.aaiService = aaiService; + this.roleValidatorFactory = roleValidatorFactory; this.getRolesFunction = getRolesFunction; this.getUserIdFunction = getUserIdFunction; } @@ -162,7 +166,7 @@ public class RoleProvider { } public RoleValidator getUserRolesValidator(HttpServletRequest request) { - return RoleValidator.by(getUserRoles(request)); + return roleValidatorFactory.by(getUserRoles(request)); } } diff --git a/vid-app-common/src/main/java/org/onap/vid/roles/RoleValidator.java b/vid-app-common/src/main/java/org/onap/vid/roles/RoleValidator.java index ed280c8b8..7b7401a01 100644 --- a/vid-app-common/src/main/java/org/onap/vid/roles/RoleValidator.java +++ b/vid-app-common/src/main/java/org/onap/vid/roles/RoleValidator.java @@ -27,16 +27,6 @@ import org.onap.portalsdk.core.util.SystemProperties; public interface RoleValidator { - static RoleValidator by(List<Role> roles) { - final boolean disableRoles = StringUtils.equals(SystemProperties.getProperty("role_management_activated"), "false"); - return by(roles, disableRoles); - } - - static RoleValidator by(List<Role> roles, boolean disableRoles) { - return disableRoles - ? new AlwaysValidRoleValidator() - : new RoleValidatorBySubscriberAndServiceType(roles); - } boolean isSubscriberPermitted(String subscriberId); diff --git a/vid-app-common/src/main/java/org/onap/vid/roles/RoleValidatorFactory.java b/vid-app-common/src/main/java/org/onap/vid/roles/RoleValidatorFactory.java new file mode 100644 index 000000000..12865401d --- /dev/null +++ b/vid-app-common/src/main/java/org/onap/vid/roles/RoleValidatorFactory.java @@ -0,0 +1,53 @@ +/*- + * ============LICENSE_START======================================================= + * VID + * ================================================================================ + * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2018 - 2019 Nokia. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.vid.roles; + + +import java.util.List; +import org.apache.commons.lang3.StringUtils; +import org.onap.portalsdk.core.util.SystemProperties; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import org.togglz.core.manager.FeatureManager; + +@Component +public class RoleValidatorFactory { + private final FeatureManager featureManager; + + @Autowired + public RoleValidatorFactory(FeatureManager featureManager) { + this.featureManager = featureManager; + } + + + public RoleValidator by(List<Role> roles) { + final boolean disableRoles = StringUtils + .equals(SystemProperties.getProperty("role_management_activated"), "false"); + return by(roles, disableRoles); + } + + public RoleValidator by(List<Role> roles, boolean disableRoles) { + return disableRoles + ? new AlwaysValidRoleValidator() + : new RoleValidatorBySubscriberAndServiceType(roles); + } +} diff --git a/vid-app-common/src/test/java/org/onap/vid/aai/SubscriberFilteredResultsTest.java b/vid-app-common/src/test/java/org/onap/vid/aai/SubscriberFilteredResultsTest.java index f9668c960..06ef5d586 100644 --- a/vid-app-common/src/test/java/org/onap/vid/aai/SubscriberFilteredResultsTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/aai/SubscriberFilteredResultsTest.java @@ -33,8 +33,10 @@ import org.onap.vid.model.SubscriberList; import org.onap.vid.roles.EcompRole; import org.onap.vid.roles.Role; import org.onap.vid.roles.RoleValidator; +import org.onap.vid.roles.RoleValidatorFactory; import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; public class SubscriberFilteredResultsTest { @@ -89,9 +91,7 @@ public class SubscriberFilteredResultsTest { } private void prepareRoleValidator() { - ArrayList<Role> list = new ArrayList<>(); - list.add(new Role(EcompRole.READ, "a", "a", "a")); - roleValidator = RoleValidator.by(list); + roleValidator = mock(RoleValidator.class); } private void prepareSubscriberList() throws IOException { diff --git a/vid-app-common/src/test/java/org/onap/vid/bl/AaiServiceTest.java b/vid-app-common/src/test/java/org/onap/vid/bl/AaiServiceTest.java deleted file mode 100644 index 1d4556535..000000000 --- a/vid-app-common/src/test/java/org/onap/vid/bl/AaiServiceTest.java +++ /dev/null @@ -1,177 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * VID - * ================================================================================ - * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.vid.bl; - -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.arrayWithSize; -import static org.hamcrest.Matchers.equalTo; -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertNotNull; - -import java.util.Arrays; -import java.util.Collections; -import java.util.List; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.Mockito; -import org.mockito.MockitoAnnotations; -import org.onap.vid.aai.AaiClientInterface; -import org.onap.vid.aai.AaiResponse; -import org.onap.vid.aai.model.AaiGetPnfResponse; -import org.onap.vid.aai.model.AaiGetPnfs.Pnf; -import org.onap.vid.aai.model.AaiGetTenatns.GetTenantsResponse; -import org.onap.vid.aai.model.LogicalLinkResponse; -import org.onap.vid.aai.model.Relationship; -import org.onap.vid.aai.model.RelationshipData; -import org.onap.vid.aai.model.RelationshipList; -import org.onap.vid.aai.model.ServiceRelationships; -import org.onap.vid.roles.Role; -import org.onap.vid.roles.RoleValidator; -import org.onap.vid.services.AaiServiceImpl; -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.DataProvider; -import org.testng.annotations.Test; - -public class AaiServiceTest { - - @InjectMocks - private AaiServiceImpl aaiService; - - @Mock - private AaiClientInterface aaiClientInterface; - - - - @BeforeMethod - public void initMocks(){ - MockitoAnnotations.initMocks(this); - } - - @Test - public void testGetSpecificPnf(){ - Pnf pnf = Pnf.builder().withPnfId("11111").build(); - AaiResponse<Pnf> aaiResponse = new AaiResponse<>(pnf, "aaaa", 200); - Mockito.doReturn(aaiResponse).when(aaiClientInterface).getSpecificPnf(Mockito.anyString()); - AaiResponse<Pnf> specificPnf = aaiService.getSpecificPnf("1345667"); - assertNotNull(specificPnf); - pnf = specificPnf.getT(); - assertNotNull(pnf); - assertEquals("11111",pnf.getPnfId()); - assertEquals("aaaa",specificPnf.getErrorMessage()); - assertEquals(200,specificPnf.getHttpCode()); - } - - @Test - public void testPnfByRegion(){ - AaiGetPnfResponse aaiGetPnfResponse = new AaiGetPnfResponse(); - AaiResponse<AaiGetPnfResponse> aaiResponse = new AaiResponse<>(aaiGetPnfResponse, "", 200); - Mockito.doReturn(aaiResponse).when(aaiClientInterface).getPNFData(Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString()); - AaiResponse<AaiGetPnfResponse> aaiGetPnfResponseWrapper = aaiService.getPNFData("1345667", "1345667", "1345667", "1345667", "1345667", "1345667", "1345667"); - assertNotNull(aaiGetPnfResponseWrapper); - aaiGetPnfResponse = aaiGetPnfResponseWrapper.getT(); - assertNotNull(aaiGetPnfResponse); - } - - @Test - public void testGetAssociatedPnfs(){ - ServiceRelationships serviceRelationships = createServiceRelationships(); - AaiResponse<ServiceRelationships> aaiResponse = new AaiResponse<>(serviceRelationships, null, 200); - Mockito.doReturn(aaiResponse).when(aaiClientInterface).getServiceInstance(Mockito.anyString(), Mockito.anyString(), Mockito.anyString()); - - LogicalLinkResponse logicalLinkResponse = createLogicalLinkResponse(); - AaiResponse<LogicalLinkResponse> aaiResponse1 = new AaiResponse<>(logicalLinkResponse, null, 200); - Mockito.doReturn(aaiResponse1).when(aaiClientInterface).getLogicalLink("SANITY6758cce9%3ALAG1992%7CSANITY6785cce9%3ALAG1961"); - - List<String> pnfList = aaiService.getServiceInstanceAssociatedPnfs("123", "456", "789"); - assertNotNull(pnfList); - assertEquals(1, pnfList.size()); - assertEquals("SANITY6785cce9", pnfList.get(0)); - } - - private ServiceRelationships createServiceRelationships() { - ServiceRelationships serviceRelationships = new ServiceRelationships(); - serviceRelationships.setServiceInstanceName("test service"); - - RelationshipData logicalLinksRelationshipData = new RelationshipData(); - logicalLinksRelationshipData.setRelationshipKey("logical-link.link-name"); - logicalLinksRelationshipData.setRelationshipValue("SANITY6758cce9:LAG1992|SANITY6785cce9:LAG1961"); - - Relationship logicalLinksRelationship = new Relationship(); - logicalLinksRelationship.setRelatedTo("logical-link"); - logicalLinksRelationship.setRelationDataList(Arrays.asList(logicalLinksRelationshipData)); - - RelationshipList logicalLinksRelationshipsList = new RelationshipList(); - logicalLinksRelationshipsList.setRelationship(Arrays.asList(logicalLinksRelationship)); - - serviceRelationships.setRelationshipList(logicalLinksRelationshipsList); - return serviceRelationships; - } - - private LogicalLinkResponse createLogicalLinkResponse() { - LogicalLinkResponse logicalLinkResponse = new LogicalLinkResponse(); - logicalLinkResponse.setLinkName("SANITY6758cce9:LAG1992|SANITY6785cce9:LAG1961"); - - RelationshipData lagInterfaceRelationshipData = new RelationshipData(); - lagInterfaceRelationshipData.setRelationshipKey("pnf.pnf-name"); - lagInterfaceRelationshipData.setRelationshipValue("SANITY6785cce9"); - - Relationship lagInterfaceRelationship = new Relationship(); - lagInterfaceRelationship.setRelatedTo("lag-interface"); - lagInterfaceRelationship.setRelationDataList(Arrays.asList(lagInterfaceRelationshipData)); - - RelationshipList lagInterfaceRelationshipsList = new RelationshipList(); - lagInterfaceRelationshipsList.setRelationship(Arrays.asList(lagInterfaceRelationship)); - - logicalLinkResponse.setRelationshipList(lagInterfaceRelationshipsList); - - return logicalLinkResponse; - } - - @DataProvider - public static Object[][] getTenantsData() { - return new Object[][] { - {"customer1", "serviceType1", "tenant1", "customer1", "serviceType1", "tenant1", "id-1", true}, - {"customer1", "serviceType1", "TeNant1", "customer1", "serviceType1", "tenant1", "id-1", true}, - {"customer1", "serviceType1", "TENANT1", "customer1", "serviceType1", "tenant1", "id-1", true}, - {"customer1", "serviceType1", "tenant2", "customer1", "serviceType1", "tenant1", "tenant2", false}, - {"customer1", "serviceType1", null, "customer1", "serviceType1", "tenant1", "tenant2", true}, - {"customer2", "serviceType1", "tenant1", "customer1", "serviceType1", "tenant1", "id-1", false}, - {"customer1", "serviceType2", "tenant1", "customer1", "serviceType1", "tenant1", "id-1", false}, - {"customer2", "serviceType1", null, "customer1", "serviceType1", "tenant1", "id-1", false}, - {"customer1", "serviceType2", null, "customer1", "serviceType1", "tenant1", "id-1", false}, - }; - } - - @Test(dataProvider = "getTenantsData") - public void testGetTenants(String userGlobalCustomerId, String userServiceType, String userTenantName, String serviceGlobalCustomerId, - String serviceServiceType, String serviceTenantName, String serviceTenantId, boolean expectedIsPermitted) { - GetTenantsResponse[] getTenantsResponses = new GetTenantsResponse[] {new GetTenantsResponse(null, null, serviceTenantName, serviceTenantId, expectedIsPermitted)}; - AaiResponse<GetTenantsResponse[]> aaiResponse = new AaiResponse<>(getTenantsResponses, null, 200); - Mockito.doReturn(aaiResponse).when(aaiClientInterface).getTenants(serviceGlobalCustomerId, serviceServiceType); - Role role = new Role(null, userGlobalCustomerId, userServiceType, userTenantName); - RoleValidator roleValidator = RoleValidator.by(Collections.singletonList(role)); - AaiResponse<GetTenantsResponse[]> actualTenants = aaiService.getTenants(serviceGlobalCustomerId, serviceServiceType, roleValidator); - - assertThat(actualTenants.getT(), arrayWithSize(1)); - assertThat(actualTenants.getT()[0].tenantName, equalTo(serviceTenantName)); - //assertThat(actualTenants.getT()[0].isPermitted, equalTo(expectedIsPermitted)); - } -} diff --git a/vid-app-common/src/test/java/org/onap/vid/controller/AaiControllerTest.java b/vid-app-common/src/test/java/org/onap/vid/controller/AaiControllerTest.java index 400926fbd..202263c41 100644 --- a/vid-app-common/src/test/java/org/onap/vid/controller/AaiControllerTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/controller/AaiControllerTest.java @@ -23,11 +23,13 @@ package org.onap.vid.controller; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.core.Is.is; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.isA; import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; @@ -68,8 +70,11 @@ import org.onap.vid.aai.model.PortDetailsTranslator.PortDetailsOk; import org.onap.vid.aai.util.AAIRestInterface; import org.onap.vid.model.VersionByInvariantIdsRequest; import org.onap.vid.properties.Features; +import org.onap.vid.roles.AlwaysValidRoleValidator; import org.onap.vid.roles.RoleProvider; +import org.onap.vid.roles.RoleValidator; import org.onap.vid.roles.RoleValidatorBySubscriberAndServiceType; +import org.onap.vid.roles.RoleValidatorFactory; import org.onap.vid.services.AaiService; import org.onap.vid.utils.SystemPropertiesWrapper; import org.onap.vid.utils.Unchecked; @@ -92,6 +97,8 @@ public class AaiControllerTest { @Mock private RoleProvider roleProvider; @Mock + private RoleValidator roleValidator; + @Mock private SystemPropertiesWrapper systemPropertiesWrapper; @Mock private FeatureManager featureManager; @@ -103,6 +110,7 @@ public class AaiControllerTest { public void setUp() { aaiController = new AaiController(aaiService, aaiRestInterface, roleProvider, systemPropertiesWrapper, featureManager); + when(roleProvider.getUserRolesValidator(any())).thenReturn(roleValidator); mockMvc = MockMvcBuilders.standaloneSetup(aaiController).build(); } @@ -408,7 +416,7 @@ public class AaiControllerTest { String okResponseBody = "OK_RESPONSE"; AaiResponse<String> aaiResponse = new AaiResponse<>(okResponseBody, "", HttpStatus.OK.value()); given(featureManager.isActive(Features.FLAG_1906_AAI_SUB_DETAILS_REDUCE_DEPTH)).willReturn(isFeatureActive); - given(aaiService.getSubscriberData(eq(subscriberId), isA(RoleValidatorBySubscriberAndServiceType.class), + given(aaiService.getSubscriberData(eq(subscriberId), isA(RoleValidator.class), eq(isFeatureActive && omitServiceInstances))) .willReturn(aaiResponse); @@ -479,7 +487,7 @@ public class AaiControllerTest { String okResponseBody = "OK_RESPONSE"; AaiResponse<String> aaiResponse = new AaiResponse<>(okResponseBody, "", HttpStatus.OK.value()); given(featureManager.isActive(Features.FLAG_1906_AAI_SUB_DETAILS_REDUCE_DEPTH)).willReturn(isFeatureActive); - given(aaiService.getSubscriberData(eq(subscriberId), isA(RoleValidatorBySubscriberAndServiceType.class), + given(aaiService.getSubscriberData(eq(subscriberId), isA(RoleValidator.class), eq(isFeatureActive && omitServiceInstances))) .willReturn(aaiResponse); diff --git a/vid-app-common/src/test/java/org/onap/vid/roles/RoleProviderTest.java b/vid-app-common/src/test/java/org/onap/vid/roles/RoleProviderTest.java index 452ae52fe..3935349c0 100644 --- a/vid-app-common/src/test/java/org/onap/vid/roles/RoleProviderTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/roles/RoleProviderTest.java @@ -22,6 +22,8 @@ package org.onap.vid.roles; import static org.assertj.core.api.Assertions.assertThat; +import static org.hamcrest.CoreMatchers.is; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.when; import static org.mockito.MockitoAnnotations.initMocks; @@ -30,6 +32,7 @@ import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import org.assertj.core.util.Lists; +import org.hamcrest.CoreMatchers; import org.mockito.Mock; import org.onap.vid.aai.AaiResponse; import org.onap.vid.aai.exceptions.RoleParsingException; @@ -58,13 +61,16 @@ public class RoleProviderTest { @Mock private AaiResponse<SubscriberList> subscriberListResponse; + @Mock + private RoleValidatorFactory roleValidatorFactory; + private RoleProvider roleProvider; @BeforeMethod public void setUp() { initMocks(this); - roleProvider = new RoleProvider(aaiService, httpServletRequest -> 5, httpServletRequest -> createRoles()); + roleProvider = new RoleProvider(aaiService, roleValidatorFactory, httpServletRequest -> 5, httpServletRequest -> createRoles()); } @Test @@ -143,6 +149,16 @@ public class RoleProviderTest { assertThat(roleProvider.userPermissionIsReadLogs(Lists.list(withoutPermission, withPermission))).isTrue(); } + @Test + public void getUserRolesValidator_shouldReturnValidatorFromFactory() { + RoleValidator expectedRoleValidator = new AlwaysValidRoleValidator(); + when(roleValidatorFactory.by(any())).thenReturn(expectedRoleValidator); + + RoleValidator result = roleProvider.getUserRolesValidator(request); + + assertThat(result).isEqualTo(expectedRoleValidator); + } + private void setSubscribers() { Subscriber subscriber = new Subscriber(); subscriber.subscriberName = SAMPLE_SUBSCRIBER; diff --git a/vid-app-common/src/test/java/org/onap/vid/roles/RoleValidatorBySubscriberAndServiceTypeTest.java b/vid-app-common/src/test/java/org/onap/vid/roles/RoleValidatorBySubscriberAndServiceTypeTest.java index 39928ef9d..d90ea51a9 100644 --- a/vid-app-common/src/test/java/org/onap/vid/roles/RoleValidatorBySubscriberAndServiceTypeTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/roles/RoleValidatorBySubscriberAndServiceTypeTest.java @@ -87,6 +87,12 @@ public class RoleValidatorBySubscriberAndServiceTypeTest { .isTenantPermitted(SAMPLE_SUBSCRIBER, SAMPLE_SERVICE_TYPE, SAMPLE_TENANT)).isTrue(); } + @Test + public void shouldPermitTenantWhenNameMatchesCaseInsensitive() { + assertThat(roleValidatorBySubscriberAndServiceType + .isTenantPermitted(SAMPLE_SUBSCRIBER, SAMPLE_SERVICE_TYPE, SAMPLE_TENANT.toUpperCase())).isTrue(); + } + @Test public void shouldNotPermitTenantWhenNameNotMatches() { diff --git a/vid-app-common/src/test/java/org/onap/vid/services/AaiServiceTest.java b/vid-app-common/src/test/java/org/onap/vid/services/AaiServiceTest.java index 6aa67051e..338657b51 100644 --- a/vid-app-common/src/test/java/org/onap/vid/services/AaiServiceTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/services/AaiServiceTest.java @@ -23,6 +23,12 @@ package org.onap.vid.services; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.arrayWithSize; import static org.hamcrest.Matchers.equalTo; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.mockito.Mockito.withSettings; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertNotNull; @@ -49,6 +55,7 @@ import org.onap.vid.aai.model.ServiceRelationships; import org.onap.vid.model.aaiTree.AAITreeNode; import org.onap.vid.roles.Role; import org.onap.vid.roles.RoleValidator; +import org.onap.vid.roles.RoleValidatorFactory; import org.testng.annotations.BeforeMethod; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; @@ -61,6 +68,9 @@ public class AaiServiceTest { @Mock private AaiClientInterface aaiClientInterface; + @Mock + private RoleValidatorFactory roleValidatorFactory; + @BeforeMethod public void initMocks(){ MockitoAnnotations.initMocks(this); @@ -68,7 +78,7 @@ public class AaiServiceTest { @Test public void testGetSpecificPnf(){ - Pnf pnf = new Pnf("11111", null, null, null, null, null, null); + Pnf pnf = Pnf.builder().withPnfId("11111").build(); AaiResponse<Pnf> aaiResponse = new AaiResponse<>(pnf, "aaaa", 200); Mockito.doReturn(aaiResponse).when(aaiClientInterface).getSpecificPnf(Mockito.anyString()); AaiResponse<Pnf> specificPnf = aaiService.getSpecificPnf("1345667"); @@ -150,8 +160,6 @@ public class AaiServiceTest { public static Object[][] getTenantsData() { return new Object[][] { {"customer1", "serviceType1", "tenant1", "customer1", "serviceType1", "tenant1", "id-1", true}, - {"customer1", "serviceType1", "TeNant1", "customer1", "serviceType1", "tenant1", "id-1", true}, - {"customer1", "serviceType1", "TENANT1", "customer1", "serviceType1", "tenant1", "id-1", true}, {"customer1", "serviceType1", "tenant2", "customer1", "serviceType1", "tenant1", "tenant2", false}, {"customer1", "serviceType1", null, "customer1", "serviceType1", "tenant1", "tenant2", true}, {"customer2", "serviceType1", "tenant1", "customer1", "serviceType1", "tenant1", "id-1", false}, @@ -162,14 +170,20 @@ public class AaiServiceTest { } @Test(dataProvider = "getTenantsData") - public void testGetTenants(String userGlobalCustomerId, String userServiceType, String userTenantName, String serviceGlobalCustomerId, - String serviceServiceType, String serviceTenantName, String serviceTenantId, boolean expectedIsPermitted) { + public void testGetTenants(String userGlobalCustomerId, String userServiceType, String userTenantName, + String serviceGlobalCustomerId, String serviceServiceType, String serviceTenantName, + String serviceTenantId, boolean expectedIsPermitted) { GetTenantsResponse[] getTenantsResponses = new GetTenantsResponse[] {new GetTenantsResponse(null, null, serviceTenantName, serviceTenantId, false)}; AaiResponse<GetTenantsResponse[]> aaiResponse = new AaiResponse<>(getTenantsResponses, null, 200); Mockito.doReturn(aaiResponse).when(aaiClientInterface).getTenants(serviceGlobalCustomerId, serviceServiceType); - Role role = new Role(null, userGlobalCustomerId, userServiceType, userTenantName); - RoleValidator roleValidator = RoleValidator.by(Collections.singletonList(role), false); - AaiResponse<GetTenantsResponse[]> actualTenants = aaiService.getTenants(serviceGlobalCustomerId, serviceServiceType, roleValidator); + + RoleValidator roleValidatorMock = mock(RoleValidator.class); + when(roleValidatorMock.isTenantPermitted( + eq(userGlobalCustomerId), eq(userServiceType), + (userTenantName == null) ? anyString() : eq(userTenantName)) + ).thenReturn(true); + + AaiResponse<GetTenantsResponse[]> actualTenants = aaiService.getTenants(serviceGlobalCustomerId, serviceServiceType, roleValidatorMock); assertThat(actualTenants.getT(), arrayWithSize(1)); assertThat(actualTenants.getT()[0].tenantName, equalTo(serviceTenantName)); |