diff options
Diffstat (limited to 'vid-app-common/src/test/java/org/onap')
15 files changed, 490 insertions, 484 deletions
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 521102383..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.RoleValidatorByRoles; +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(RoleValidatorByRoles.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(RoleValidatorByRoles.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/controller/LoggerControllerTest.java b/vid-app-common/src/test/java/org/onap/vid/controller/LoggerControllerTest.java index f0d840929..6f584ed3d 100644 --- a/vid-app-common/src/test/java/org/onap/vid/controller/LoggerControllerTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/controller/LoggerControllerTest.java @@ -66,7 +66,7 @@ public class LoggerControllerTest { @Test public void shouldThrowNotAuthorizedException_whenUserIsNotAuthorizedToGetLogs() throws Exception { - List<Role> list = ImmutableList.of(new Role(EcompRole.READ, "subName1", "servType1", "tenant1")); + List<Role> list = ImmutableList.of(new Role(EcompRole.READ, "subName1", "servType1", "tenant1", "owningEntityId")); given(provider.getUserRoles(argThat(req -> req.getRequestedSessionId().equals("id1")))).willReturn(list); given(provider.userPermissionIsReadLogs(list)).willReturn(false); @@ -80,7 +80,7 @@ public class LoggerControllerTest { @Test public void shouldReturnLastAndOneBeforeLogLines_whenLimitIs2() throws Exception { - List<Role> list = ImmutableList.of(new Role(EcompRole.READ, "subName1", "servType1", "tenant1")); + List<Role> list = ImmutableList.of(new Role(EcompRole.READ, "subName1", "servType1", "tenant1", "owningEntityId")); given(provider.getUserRoles(argThat(req -> req.getRequestedSessionId().equals("id1")))).willReturn(list); given(provider.userPermissionIsReadLogs(list)).willReturn(true); @@ -96,7 +96,7 @@ public class LoggerControllerTest { @Test public void shouldReturnEmptyString_whenLogFileIsEmpty() throws Exception { - List<Role> list = ImmutableList.of(new Role(EcompRole.READ, "subName1", "servType1", "tenant1")); + List<Role> list = ImmutableList.of(new Role(EcompRole.READ, "subName1", "servType1", "tenant1", "owningEntityId")); given(provider.getUserRoles(argThat(req -> req.getRequestedSessionId().equals("id1")))).willReturn(list); given(provider.userPermissionIsReadLogs(list)).willReturn(true); @@ -111,7 +111,7 @@ public class LoggerControllerTest { @Test public void shouldReturnEmptyString_whenDebugLogFileIsEmpty() throws Exception { - List<Role> list = ImmutableList.of(new Role(EcompRole.READ, "subName1", "servType1", "tenant1")); + List<Role> list = ImmutableList.of(new Role(EcompRole.READ, "subName1", "servType1", "tenant1", "owningEntityId")); given(provider.getUserRoles(argThat(req -> req.getRequestedSessionId().equals("id1")))).willReturn(list); given(provider.userPermissionIsReadLogs(list)).willReturn(true); diff --git a/vid-app-common/src/test/java/org/onap/vid/controller/ServicePermissionsTest.java b/vid-app-common/src/test/java/org/onap/vid/controller/ServicePermissionsTest.java index ac3da50ab..3b7dbfbb8 100644 --- a/vid-app-common/src/test/java/org/onap/vid/controller/ServicePermissionsTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/controller/ServicePermissionsTest.java @@ -31,6 +31,7 @@ import static org.mockito.Mockito.when; import org.jetbrains.annotations.NotNull; import org.onap.vid.aai.model.Permissions; +import org.onap.vid.roles.PermissionPropertiesSubscriberAndServiceType; import org.onap.vid.roles.RoleProvider; import org.onap.vid.roles.RoleValidator; import org.springframework.mock.web.MockHttpServletRequest; @@ -53,7 +54,7 @@ public class ServicePermissionsTest { RoleProvider roleProvider = mock(RoleProvider.class); RoleValidator roleValidator = mock(RoleValidator.class); when(roleProvider.getUserRolesValidator(any())).thenReturn(roleValidator); - when(roleValidator.isServicePermitted(subscriberId, serviceType)).thenReturn(expected); + when(roleValidator.isServicePermitted(new PermissionPropertiesSubscriberAndServiceType(subscriberId, serviceType))).thenReturn(expected); AaiController2 aaiController2 = new AaiController2(null, roleProvider, null, null); diff --git a/vid-app-common/src/test/java/org/onap/vid/model/ServiceInstanceSearchResultTest.java b/vid-app-common/src/test/java/org/onap/vid/model/ServiceInstanceSearchResultTest.java index 5168a5c63..b777375ef 100644 --- a/vid-app-common/src/test/java/org/onap/vid/model/ServiceInstanceSearchResultTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/model/ServiceInstanceSearchResultTest.java @@ -20,183 +20,47 @@ package org.onap.vid.model; -import org.junit.Test; +import static com.google.code.beanmatchers.BeanMatchers.hasValidBeanConstructor; +import static com.google.code.beanmatchers.BeanMatchers.hasValidBeanEqualsFor; +import static com.google.code.beanmatchers.BeanMatchers.hasValidBeanHashCodeFor; +import static com.google.code.beanmatchers.BeanMatchers.hasValidGettersAndSetters; +import static net.javacrumbs.jsonunit.JsonMatchers.jsonNodeAbsent; +import static net.javacrumbs.jsonunit.JsonMatchers.jsonPartEquals; +import static org.apache.commons.lang3.ArrayUtils.toArray; +import static org.hamcrest.CoreMatchers.allOf; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.testng.annotations.Test; public class ServiceInstanceSearchResultTest { - private ServiceInstanceSearchResult createTestSubject() { - return new ServiceInstanceSearchResult(); - } - - @Test - public void testGetServiceInstanceId() throws Exception { - ServiceInstanceSearchResult testSubject; - String result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getServiceInstanceId(); - } - - @Test - public void testSetServiceInstanceId() throws Exception { - ServiceInstanceSearchResult testSubject; - String serviceInstanceId = ""; - - // default test - testSubject = createTestSubject(); - testSubject.setServiceInstanceId(serviceInstanceId); - } - - @Test - public void testGetGlobalCustomerId() throws Exception { - ServiceInstanceSearchResult testSubject; - String result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getGlobalCustomerId(); - } - - @Test - public void testSetGlobalCustomerId() throws Exception { - ServiceInstanceSearchResult testSubject; - String globalCustomerId = ""; - - // default test - testSubject = createTestSubject(); - testSubject.setGlobalCustomerId(globalCustomerId); - } - - @Test - public void testGetServiceType() throws Exception { - ServiceInstanceSearchResult testSubject; - String result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getServiceType(); - } - - @Test - public void testSetServiceType() throws Exception { - ServiceInstanceSearchResult testSubject; - String serviceType = ""; - - // default test - testSubject = createTestSubject(); - testSubject.setServiceType(serviceType); - } - - @Test - public void testGetServiceInstanceName() throws Exception { - ServiceInstanceSearchResult testSubject; - String result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getServiceInstanceName(); - } - - @Test - public void testSetServiceInstanceName() throws Exception { - ServiceInstanceSearchResult testSubject; - String serviceInstanceName = ""; - - // default test - testSubject = createTestSubject(); - testSubject.setServiceInstanceName(serviceInstanceName); - } - - @Test - public void testGetSubscriberName() throws Exception { - ServiceInstanceSearchResult testSubject; - String result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getSubscriberName(); - } - - @Test - public void testSetSubscriberName() throws Exception { - ServiceInstanceSearchResult testSubject; - String subscriberName = ""; - - // default test - testSubject = createTestSubject(); - testSubject.setSubscriberName(subscriberName); - } - @Test - public void testGetAaiModelInvariantId() throws Exception { - ServiceInstanceSearchResult testSubject; - String result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getAaiModelInvariantId(); - } - - @Test - public void testSetAaiModelInvariantId() throws Exception { - ServiceInstanceSearchResult testSubject; - String aaiModelInvariantId = ""; - - // default test - testSubject = createTestSubject(); - testSubject.setAaiModelInvariantId(aaiModelInvariantId); - } - - @Test - public void testGetAaiModelVersionId() throws Exception { - ServiceInstanceSearchResult testSubject; - String result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getAaiModelVersionId(); + public void shouldHaveValidGettersAndSetters() { + assertThat(ServiceInstanceSearchResult.class, hasValidGettersAndSetters()); } @Test - public void testSetAaiModelVersionId() throws Exception { - ServiceInstanceSearchResult testSubject; - String aaiModelVersionId = ""; - - // default test - testSubject = createTestSubject(); - testSubject.setAaiModelVersionId(aaiModelVersionId); - } - - @Test - public void testGetIsPermitted() throws Exception { - ServiceInstanceSearchResult testSubject; - boolean result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getIsPermitted(); + public void shouldHaveValidConstructor() { + assertThat(ServiceInstanceSearchResult.class, hasValidBeanConstructor()); } @Test - public void testSetIsPermitted() throws Exception { - ServiceInstanceSearchResult testSubject; - boolean isPermitted = false; + public void shouldHaveValidEqualsAndHashCode() { + String[] propertiesToEqualBy = toArray("serviceInstanceId"); - // default test - testSubject = createTestSubject(); - testSubject.setIsPermitted(isPermitted); + assertThat(ServiceInstanceSearchResult.class, allOf( + hasValidBeanHashCodeFor(propertiesToEqualBy), + hasValidBeanEqualsFor(propertiesToEqualBy)) + ); } @Test - public void testEquals() throws Exception { - ServiceInstanceSearchResult testSubject; - Object other = null; - boolean result; + public void subscriberId_shouldBeSerializedAsGlobalCustomerId() { + ServiceInstanceSearchResult underTest = new ServiceInstanceSearchResult(); + underTest.setSubscriberId("example"); - // default test - testSubject = createTestSubject(); - result = testSubject.equals(other); + assertThat(underTest, jsonPartEquals("globalCustomerId", "example")); + assertThat(underTest, jsonNodeAbsent("subscriberId")); } } diff --git a/vid-app-common/src/test/java/org/onap/vid/roles/AlwaysValidRoleValidatorTest.java b/vid-app-common/src/test/java/org/onap/vid/roles/AlwaysValidRoleValidatorTest.java index 363c6ff76..6826b1e4a 100644 --- a/vid-app-common/src/test/java/org/onap/vid/roles/AlwaysValidRoleValidatorTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/roles/AlwaysValidRoleValidatorTest.java @@ -33,7 +33,8 @@ public class AlwaysValidRoleValidatorTest { @Test public void testIsServicePermitted() { - assertTrue(new AlwaysValidRoleValidator().isServicePermitted("any", "any")); + WithPermissionProperties emptyPermissionProperties = new WithPermissionProperties() {}; + assertTrue(new AlwaysValidRoleValidator().isServicePermitted(emptyPermissionProperties)); } @Test 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 c1033d2d0..8d81c929c 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,7 @@ package org.onap.vid.roles; import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.when; import static org.mockito.MockitoAnnotations.initMocks; @@ -42,7 +43,7 @@ import org.testng.annotations.Test; public class RoleProviderTest { private static final String SAMPLE_SUBSCRIBER = "sampleSubscriber"; - private static final String SAMPLE_CUSTOMER_ID = "sampleCustomerId"; + private static final String SAMPLE_SUBSCRIBER_ID = "subscriberId"; private static final String SERVICE_TYPE_LOGS = "LOGS"; private static final String TENANT_PERMITTED = "PERMITTED"; private static final String SAMPLE_SERVICE = "sampleService"; @@ -58,13 +59,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 @@ -83,7 +87,7 @@ public class RoleProviderTest { Role role = roleProvider.createRoleFromStringArr(roleParts, SAMPLE_ROLE_PREFIX); assertThat(role.getEcompRole()).isEqualTo(EcompRole.READ); - assertThat(role.getSubscribeName()).isEqualTo(SAMPLE_CUSTOMER_ID); + assertThat(role.getSubscriberId()).isEqualTo(SAMPLE_SUBSCRIBER_ID); assertThat(role.getTenant()).isEqualTo(SAMPLE_TENANT); assertThat(role.getServiceType()).isEqualTo(SAMPLE_SERVICE); } @@ -97,7 +101,7 @@ public class RoleProviderTest { Role role = roleProvider.createRoleFromStringArr(roleParts, SAMPLE_ROLE_PREFIX); assertThat(role.getEcompRole()).isEqualTo(EcompRole.READ); - assertThat(role.getSubscribeName()).isEqualTo(SAMPLE_CUSTOMER_ID); + assertThat(role.getSubscriberId()).isEqualTo(SAMPLE_SUBSCRIBER_ID); assertThat(role.getServiceType()).isEqualTo(SAMPLE_SERVICE); assertThat(role.getTenant()).isNullOrEmpty(); } @@ -111,7 +115,7 @@ public class RoleProviderTest { @Test public void shouldProperlyRetrieveUserRolesWhenPermissionIsDifferentThanRead() { - Role expectedRole = new Role(EcompRole.READ, SAMPLE_CUSTOMER_ID, SAMPLE_SERVICE, SAMPLE_TENANT); + Role expectedRole = new Role(EcompRole.READ, SAMPLE_SUBSCRIBER_ID, SAMPLE_SERVICE, SAMPLE_TENANT, owningEntityId()); setSubscribers(); List<Role> userRoles = roleProvider.getUserRoles(request); @@ -121,7 +125,7 @@ public class RoleProviderTest { Role actualRole = userRoles.get(0); assertThat(actualRole.getTenant()).isEqualTo(expectedRole.getTenant()); - assertThat(actualRole.getSubscribeName()).isEqualTo(expectedRole.getSubscribeName()); + assertThat(actualRole.getSubscriberId()).isEqualTo(expectedRole.getSubscriberId()); assertThat(actualRole.getServiceType()).isEqualTo(expectedRole.getServiceType()); } @@ -132,21 +136,38 @@ public class RoleProviderTest { @Test public void shouldReturnNotReadOnlyPermissionWhenRolesArePresent() { - assertThat(roleProvider.userPermissionIsReadOnly(Lists.list(new Role(EcompRole.READ, SAMPLE_SUBSCRIBER, SAMPLE_SERVICE, SAMPLE_TENANT)))).isFalse(); + assertThat(roleProvider.userPermissionIsReadOnly(Lists.list(new Role( + EcompRole.READ, SAMPLE_SUBSCRIBER, SAMPLE_SERVICE, SAMPLE_TENANT, owningEntityId())))).isFalse(); } @Test public void userShouldHavePermissionToReadLogsWhenServiceAndTenantAreCorrect() { - Role withoutPermission = new Role(EcompRole.READ, SAMPLE_SUBSCRIBER, SAMPLE_SERVICE, SAMPLE_TENANT); - Role withPermission = new Role(EcompRole.READ, SAMPLE_SUBSCRIBER, SERVICE_TYPE_LOGS, TENANT_PERMITTED); + Role withoutPermission = new Role(EcompRole.READ, SAMPLE_SUBSCRIBER, SAMPLE_SERVICE, SAMPLE_TENANT, owningEntityId()); + Role withPermission = new Role(EcompRole.READ, SAMPLE_SUBSCRIBER, SERVICE_TYPE_LOGS, TENANT_PERMITTED, owningEntityId()); 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 String owningEntityId() { + // while translateOwningEntityNameToOwningEntityId does nothing, no translation happens. + // this will be changed later. + return SAMPLE_SUBSCRIBER; + } + private void setSubscribers() { Subscriber subscriber = new Subscriber(); subscriber.subscriberName = SAMPLE_SUBSCRIBER; - subscriber.globalCustomerId = SAMPLE_CUSTOMER_ID; + subscriber.globalCustomerId = SAMPLE_SUBSCRIBER_ID; SubscriberList subscriberList = new SubscriberList(Lists.list(subscriber)); when(aaiService.getFullSubscriberList()).thenReturn(subscriberListResponse); when(subscriberListResponse.getT()).thenReturn(subscriberList); diff --git a/vid-app-common/src/test/java/org/onap/vid/roles/RoleValidatorByOwningEntityTest.java b/vid-app-common/src/test/java/org/onap/vid/roles/RoleValidatorByOwningEntityTest.java new file mode 100644 index 000000000..d84ac6edd --- /dev/null +++ b/vid-app-common/src/test/java/org/onap/vid/roles/RoleValidatorByOwningEntityTest.java @@ -0,0 +1,90 @@ +/*- + * ============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.roles; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.withSettings; +import static org.testng.Assert.assertFalse; + +import com.google.common.collect.ImmutableList; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; + +public class RoleValidatorByOwningEntityTest { + + private static final String OWNING_ENTITY_ID = "owningEntityId"; + private static final String SUBSCRIBER_NAME = "subscriber_name"; + private static final String NOT_MATCHING_OWNING_ENTITY_ID = "notMatchingOwningEntityId"; + + private static final String SERVICE_TYPE = "serviceType"; + private static final String GLOBAL_CUSTOMER_ID = "globalCustomerId"; + private static final String TENANT_NAME = "tenantName"; + + + private RoleValidatorByOwningEntity roleValidatorByOwningEntity; + + @BeforeMethod + public void setup() { + final Role SAMPLE_ROLE = new Role(EcompRole.READ, "", "", "", OWNING_ENTITY_ID); + roleValidatorByOwningEntity = new RoleValidatorByOwningEntity(ImmutableList.of(SAMPLE_ROLE)); + } + + @Test + public void testIsSubscriberPermitted() { + assertFalse(roleValidatorByOwningEntity.isSubscriberPermitted(SUBSCRIBER_NAME)); + } + + @Test + public void isServicePermitted_owningEntityMatch_returnTrue() { + PermissionPropertiesOwningEntity permittedOwningEntity = + new PermissionPropertiesOwningEntity(OWNING_ENTITY_ID); + + assertThat(roleValidatorByOwningEntity.isServicePermitted(permittedOwningEntity), is(true)); + } + + @DataProvider + public static Object[][] nonMatchingPermissionProperties() { + return new Object[][]{ + {new PermissionPropertiesOwningEntity(NOT_MATCHING_OWNING_ENTITY_ID)}, + {new PermissionPropertiesOwningEntity("")}, + {new WithPermissionProperties() {}}, + {mock(PermissionPropertiesOwningEntity.class, + withSettings().name("PermissionPropertiesOwningEntity with null owningEntityId"))}, + {new PermissionPropertiesSubscriberAndServiceType(OWNING_ENTITY_ID, OWNING_ENTITY_ID)}, + }; + } + + @Test(dataProvider = "nonMatchingPermissionProperties") + public void isServicePermitted_nonMatchingPermissionProperties_returnFalse(WithPermissionProperties permissionProperties) { + assertThat(permissionProperties.toString(), roleValidatorByOwningEntity.isServicePermitted( + permissionProperties + ), is(false)); + } + + @Test + public void testIsTenantPermitted() { + assertFalse(roleValidatorByOwningEntity.isTenantPermitted(GLOBAL_CUSTOMER_ID, SERVICE_TYPE, TENANT_NAME)); + } + +} diff --git a/vid-app-common/src/test/java/org/onap/vid/roles/RoleValidatorByRolesTest.java b/vid-app-common/src/test/java/org/onap/vid/roles/RoleValidatorByRolesTest.java deleted file mode 100644 index 9362ec9d7..000000000 --- a/vid-app-common/src/test/java/org/onap/vid/roles/RoleValidatorByRolesTest.java +++ /dev/null @@ -1,114 +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.roles; - - -import static org.assertj.core.api.Assertions.assertThat; - -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; -import java.util.List; -import java.util.Map; -import org.onap.vid.mso.rest.RequestDetails; -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.Test; - -public class RoleValidatorByRolesTest { - - private static final String SAMPLE_SUBSCRIBER = "sampleSubscriber"; - private static final String NOT_MATCHING_SUBSCRIBER = "notMatchingSubscriber"; - private static final String SAMPLE_SERVICE_TYPE = "sampleServiceType"; - private static final String NOT_MATCHING_TENANT = "notMatchingTenant"; - private static final String SAMPLE_TENANT = "sampleTenant"; - - private static final Role SAMPLE_ROLE = new Role(EcompRole.READ, SAMPLE_SUBSCRIBER, SAMPLE_SERVICE_TYPE, SAMPLE_TENANT); - - private List<Role> roles = ImmutableList.of(SAMPLE_ROLE); - private Map<String, Object> subscriberInfo = ImmutableMap.of("globalSubscriberId", SAMPLE_SUBSCRIBER); - private Map<String, Object> requestParameters = ImmutableMap.of("subscriptionServiceType", SAMPLE_SERVICE_TYPE); - private Map<String, Object> requestDetailsProperties = ImmutableMap.of("subscriberInfo", subscriberInfo, "requestParameters", requestParameters); - private RequestDetails requestDetails; - private RoleValidatorByRoles roleValidator; - - @BeforeMethod - public void setUp() { - roleValidator = new RoleValidatorByRoles(roles); - requestDetails = new RequestDetails(); - } - - @Test - public void shouldPermitSubscriberWhenNameMatchesAndRolesAreEnabled() { - assertThat(roleValidator.isSubscriberPermitted(SAMPLE_SUBSCRIBER)).isTrue(); - } - - @Test - public void shouldNotPermitSubscriberWhenNameNotMatches() { - assertThat(roleValidator.isSubscriberPermitted(NOT_MATCHING_SUBSCRIBER)).isFalse(); - } - - @Test - public void shouldPermitServiceWhenNamesMatches() { - assertThat(roleValidator.isServicePermitted(SAMPLE_SUBSCRIBER, SAMPLE_SERVICE_TYPE)).isTrue(); - } - - - @Test - public void shouldNotPermitServiceWhenSubscriberNameNotMatches() { - assertThat(roleValidator.isServicePermitted(NOT_MATCHING_SUBSCRIBER, SAMPLE_SERVICE_TYPE)).isFalse(); - } - - @Test - public void shouldNotPermitServiceWhenServiceTypeNotMatches() { - assertThat(roleValidator.isServicePermitted(SAMPLE_SUBSCRIBER, NOT_MATCHING_SUBSCRIBER)).isFalse(); - } - - @Test - public void shouldPermitTenantWhenNameMatches() { - assertThat(roleValidator.isTenantPermitted(SAMPLE_SUBSCRIBER, SAMPLE_SERVICE_TYPE, SAMPLE_TENANT)).isTrue(); - } - - - @Test - public void shouldNotPermitTenantWhenNameNotMatches() { - assertThat(roleValidator.isTenantPermitted(SAMPLE_SUBSCRIBER, SAMPLE_SERVICE_TYPE, NOT_MATCHING_TENANT)).isFalse(); - } - - @Test - public void shouldValidateProperlySORequest() { - requestDetails.setAdditionalProperty("requestDetails", requestDetailsProperties); - - assertThat(roleValidator.isMsoRequestValid(requestDetails)).isTrue(); - } - - @Test - public void shouldValidateUnknownSORequest() { - assertThat(roleValidator.isMsoRequestValid(new RequestDetails())).isTrue(); - } - - @Test - public void shouldRejectSORequestWhenSubscriberNotMatches() { - Map<String, Object> subscriberInfo = ImmutableMap.of("globalSubscriberId", "sample"); - Map<String, Object> requestDetailsProperties = ImmutableMap.of("subscriberInfo", subscriberInfo, "requestParameters", requestParameters); - requestDetails.setAdditionalProperty("requestDetails", requestDetailsProperties); - - assertThat(roleValidator.isMsoRequestValid(requestDetails)).isFalse(); - } -} 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 new file mode 100644 index 000000000..b6958cd67 --- /dev/null +++ b/vid-app-common/src/test/java/org/onap/vid/roles/RoleValidatorBySubscriberAndServiceTypeTest.java @@ -0,0 +1,111 @@ +/*- + * ============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.roles; + + +import static org.assertj.core.api.Assertions.assertThat; + +import com.google.common.collect.ImmutableList; +import java.util.List; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +public class RoleValidatorBySubscriberAndServiceTypeTest { + + private static final String SAMPLE_SUBSCRIBER = "sampleSubscriber"; + private static final String NOT_MATCHING_SUBSCRIBER = "notMatchingSubscriber"; + private static final String SAMPLE_SERVICE_TYPE = "sampleServiceType"; + private static final String NOT_MATCHING_TENANT = "notMatchingTenant"; + private static final String SAMPLE_TENANT = "sampleTenant"; + private static final String SOME_OWNING_ENTITY_ID = "someOwningEntityId"; + + private static final Role SAMPLE_ROLE = new Role( + EcompRole.READ, SAMPLE_SUBSCRIBER, SAMPLE_SERVICE_TYPE, SAMPLE_TENANT, SOME_OWNING_ENTITY_ID); + + private List<Role> roles = ImmutableList.of(SAMPLE_ROLE); + private RoleValidatorBySubscriberAndServiceType roleValidatorBySubscriberAndServiceType; + + @BeforeMethod + public void setUp() { + roleValidatorBySubscriberAndServiceType = new RoleValidatorBySubscriberAndServiceType(roles); + } + + @Test + public void shouldPermitSubscriberWhenNameMatchesAndRolesAreEnabled() { + assertThat(roleValidatorBySubscriberAndServiceType.isSubscriberPermitted(SAMPLE_SUBSCRIBER)).isTrue(); + } + + @Test + public void shouldNotPermitSubscriberWhenNameNotMatches() { + assertThat(roleValidatorBySubscriberAndServiceType.isSubscriberPermitted(NOT_MATCHING_SUBSCRIBER)).isFalse(); + } + + @Test + public void shouldPermitServiceWhenNamesMatches() { + assertThat(roleValidatorBySubscriberAndServiceType.isServicePermitted( + new PermissionPropertiesSubscriberAndServiceType(SAMPLE_SUBSCRIBER, SAMPLE_SERVICE_TYPE))).isTrue(); + } + + @Test + public void isServicePermitted_serviceWithAllPermissionProperties_isPermitted() { + assertThat(roleValidatorBySubscriberAndServiceType.isServicePermitted( + new AllPermissionProperties(SAMPLE_SUBSCRIBER, SAMPLE_SERVICE_TYPE, SOME_OWNING_ENTITY_ID))).isTrue(); + } + + @Test + public void shouldNotPermitServiceWhenSubscriberNameNotMatches() { + assertThat( + roleValidatorBySubscriberAndServiceType.isServicePermitted( + new PermissionPropertiesSubscriberAndServiceType(NOT_MATCHING_SUBSCRIBER, SAMPLE_SERVICE_TYPE))).isFalse(); + } + + @Test + public void shouldNotPermitServiceWhenServiceTypeNotMatches() { + assertThat(roleValidatorBySubscriberAndServiceType.isServicePermitted( + new PermissionPropertiesSubscriberAndServiceType(SAMPLE_SUBSCRIBER, NOT_MATCHING_SUBSCRIBER))).isFalse(); + } + + @Test + public void isServicePermitted_owningEntityPermissionProperties_isNotPermitted() { + assertThat(roleValidatorBySubscriberAndServiceType.isServicePermitted( + new PermissionPropertiesOwningEntity(SAMPLE_SUBSCRIBER))).isFalse(); + } + + @Test + public void shouldPermitTenantWhenNameMatches() { + assertThat(roleValidatorBySubscriberAndServiceType + .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() { + assertThat(roleValidatorBySubscriberAndServiceType + .isTenantPermitted(SAMPLE_SUBSCRIBER, SAMPLE_SERVICE_TYPE, NOT_MATCHING_TENANT)).isFalse(); + } + +} diff --git a/vid-app-common/src/test/java/org/onap/vid/roles/RoleValidatorFactoryTest.java b/vid-app-common/src/test/java/org/onap/vid/roles/RoleValidatorFactoryTest.java new file mode 100644 index 000000000..b3c71c547 --- /dev/null +++ b/vid-app-common/src/test/java/org/onap/vid/roles/RoleValidatorFactoryTest.java @@ -0,0 +1,77 @@ +/*- + * ============LICENSE_START======================================================= + * VID + * ================================================================================ + * Copyright (C) 2017 - 2020 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.roles; + +import static java.util.Collections.emptyList; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.instanceOf; +import static org.mockito.Mockito.when; + +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.properties.Features; +import org.testng.annotations.AfterMethod; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; +import org.togglz.core.manager.FeatureManager; + +public class RoleValidatorFactoryTest { + + @InjectMocks + private RoleValidatorFactory roleValidatorFactory; + + @Mock + private FeatureManager featureManager; + + @BeforeClass + public void initMocks() { + MockitoAnnotations.initMocks(this); + } + + @AfterMethod + public void reset() { + Mockito.reset(featureManager); + } + + @Test (dataProvider = "presetRoleValidatorClass") + public void returnRoleValidatorByGivenClass_And_RoleManagementActivated_And_FeatureFlag(Class expectedClass,boolean isDisabledRoles, boolean flagActive ) { + + when(featureManager.isActive(Features.FLAG_2006_USER_PERMISSIONS_BY_OWNING_ENTITY)).thenReturn(flagActive); + RoleValidator roleValidator = roleValidatorFactory.by(emptyList(), isDisabledRoles); + assertThat(roleValidator, instanceOf(expectedClass)); + } + + @DataProvider + public static Object[][] presetRoleValidatorClass() { + return new Object[][] { + {RoleValidatorsComposer.class, false, true}, + {AlwaysValidRoleValidator.class, true, true}, + {RoleValidatorBySubscriberAndServiceType.class, false, false}, + {AlwaysValidRoleValidator.class, true, false} + }; + } + +}
\ No newline at end of file diff --git a/vid-app-common/src/test/java/org/onap/vid/roles/RoleValidatorsComposerTest.java b/vid-app-common/src/test/java/org/onap/vid/roles/RoleValidatorsComposerTest.java new file mode 100644 index 000000000..c0892dafb --- /dev/null +++ b/vid-app-common/src/test/java/org/onap/vid/roles/RoleValidatorsComposerTest.java @@ -0,0 +1,110 @@ +/*- + * ============LICENSE_START======================================================= + * VID + * ================================================================================ + * Copyright (C) 2017 - 2020 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.roles; + +import static java.util.stream.Collectors.toList; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verifyZeroInteractions; +import static org.mockito.Mockito.withSettings; + +import com.google.common.collect.ImmutableSet; +import java.util.function.Function; +import java.util.stream.Stream; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; + +public class RoleValidatorsComposerTest { + + private RoleValidator alwaysTrueRoles; + private RoleValidator alwaysFalseRoles; + + @DataProvider + public static Object[][] allInterfaceFunctions() { + return Stream.<Function<RoleValidator, Boolean>>of( + (RoleValidator o) -> o.isSubscriberPermitted("subscriberId"), + (RoleValidator o) -> o.isServicePermitted(new WithPermissionProperties() {}), + (RoleValidator o) -> o.isTenantPermitted("subscriberId", "serviceType", "tenantName") + ).map(it -> new Object[]{it}).collect(toList()).toArray(new Object[][]{}); + } + + @BeforeMethod + public void setUp() { + alwaysTrueRoles = mock(RoleValidator.class, withSettings().defaultAnswer(o -> true)); + alwaysFalseRoles = mock(RoleValidator.class); + } + + @Test(dataProvider = "allInterfaceFunctions") + public void emptyComposite_returnsFalse(Function<RoleValidator, Boolean> interfaceFunction) { + RoleValidatorsComposer underTest = new RoleValidatorsComposer(); + + assertThat( + interfaceFunction.apply(underTest), + is(false) + ); + + } + + @Test(dataProvider = "allInterfaceFunctions") + public void falseAndTrueComposite_returnsTrue(Function<RoleValidator, Boolean> interfaceFunction) { + RoleValidatorsComposer underTest = + new RoleValidatorsComposer(alwaysFalseRoles, alwaysFalseRoles, alwaysTrueRoles); + + assertThat( + interfaceFunction.apply(underTest), + is(true) + ); + } + + @Test(dataProvider = "allInterfaceFunctions") + public void trueAndFalseComposite_returnsTrueAndShortCircuits(Function<RoleValidator, Boolean> interfaceFunction) { + RoleValidatorsComposer underTest = new RoleValidatorsComposer(alwaysTrueRoles, alwaysFalseRoles); + + assertThat( + interfaceFunction.apply(underTest), + is(true) + ); + + verifyZeroInteractions(alwaysFalseRoles); + } + + @Test(dataProvider = "allInterfaceFunctions") + public void falseAndFalseComposite_returnsFalse(Function<RoleValidator, Boolean> interfaceFunction) { + RoleValidatorsComposer underTest = new RoleValidatorsComposer(alwaysFalseRoles, alwaysFalseRoles); + + assertThat( + interfaceFunction.apply(underTest), + is(false) + ); + } + + @Test + public void secondaryConstructor_givenSetIfValidators_returnsTrue() { + RoleValidatorsComposer underTest = new RoleValidatorsComposer( + ImmutableSet.of(alwaysTrueRoles) + ); + + assertThat(underTest.isSubscriberPermitted("anything"), is(true)); + } + +} diff --git a/vid-app-common/src/test/java/org/onap/vid/services/AaiServiceImplTest.java b/vid-app-common/src/test/java/org/onap/vid/services/AaiServiceImplTest.java index d3b2a48b1..a37482624 100644 --- a/vid-app-common/src/test/java/org/onap/vid/services/AaiServiceImplTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/services/AaiServiceImplTest.java @@ -183,7 +183,7 @@ public class AaiServiceImplTest { AaiResponse<Services> aaiResponseServices = new AaiResponse<>(services, null, HttpStatus.SC_OK); when(aaiClient.getSubscriberData(SUBSCRIBER_ID, false)).thenReturn(aaiResponseServices); - when(roleValidator.isServicePermitted(eq(GLOBAL_CUSTOMER_ID), anyString())).thenReturn(Boolean.TRUE); + when(roleValidator.isServicePermitted(any())).thenReturn(Boolean.TRUE); AaiResponse actualResponse = aaiService.getSubscriberData(SUBSCRIBER_ID, roleValidator, false); List<ServiceSubscription> actualServiceSubscriptions = ((AaiResponse<Services>) actualResponse) 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)); |