From 8fa77ad4d4c2498e40a4c23e6826d9ad73a1dab8 Mon Sep 17 00:00:00 2001 From: Tomasz Golabek Date: Thu, 4 Apr 2019 17:52:26 +0200 Subject: Some unit tests for catalog-be Code coverage for some classes from catalog-be increased. Some refactor made if needed. Change-Id: I5cd63fe61425f5eb05336545d714cbe2df83e116 Issue-ID: SDC-2220 Signed-off-by: Tomasz Golabek --- .../validation/AccessValidationsTest.java | 121 ++++++++++++++++++++ .../sdc/be/info/ArtifactAccessInfoTest.java | 35 +++++- .../sdc/be/user/UserAdminValidatorTest.java | 124 ++++++++++++++++----- 3 files changed, 250 insertions(+), 30 deletions(-) create mode 100644 catalog-be/src/test/java/org/openecomp/sdc/be/components/validation/AccessValidationsTest.java (limited to 'catalog-be/src/test/java/org') diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/components/validation/AccessValidationsTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/components/validation/AccessValidationsTest.java new file mode 100644 index 0000000000..f04d71097e --- /dev/null +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/components/validation/AccessValidationsTest.java @@ -0,0 +1,121 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2019 Nokia 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.openecomp.sdc.be.components.validation; + +import static org.mockito.Mockito.atLeast; + +import java.util.ArrayList; +import java.util.List; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.MockitoJUnitRunner; +import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum; +import org.openecomp.sdc.be.model.Component; +import org.openecomp.sdc.be.model.User; +import org.openecomp.sdc.be.user.Role; + +@RunWith(MockitoJUnitRunner.class) +public class AccessValidationsTest { + + private static final String ANY_CONTEXT = "anyContext"; + private static final String RESOURCES = "resources"; + private static final String COMPONENT_ID = "1"; + private static final String USER_ID = "2"; + private AccessValidations accessValidations; + + @Mock + private UserValidations userValidations; + @Mock + private ComponentValidations componentValidations; + + @Before + public void setUp() throws Exception { + accessValidations = new AccessValidations(userValidations, componentValidations); + } + + @Test + public void testValidateUserCanRetrieveComponentData() { + accessValidations.validateUserCanRetrieveComponentData(COMPONENT_ID, RESOURCES, USER_ID, ANY_CONTEXT); + + Mockito.verify(userValidations).validateUserExists(USER_ID, ANY_CONTEXT, true); + Mockito.verify(componentValidations).getComponent(COMPONENT_ID, ComponentTypeEnum.RESOURCE); + } + + @Test + public void testValidateUserCanWorkOnComponent() { + User user = new User(); + List adminRoles = new ArrayList<>(); + adminRoles.add(Role.ADMIN); + adminRoles.add(Role.DESIGNER); + Mockito.when(userValidations.validateUserExists(USER_ID, ANY_CONTEXT, true)).thenReturn(user); + + accessValidations.validateUserCanWorkOnComponent(COMPONENT_ID, ComponentTypeEnum.RESOURCE, USER_ID, ANY_CONTEXT); + + Mockito.verify(userValidations).validateUserExists(USER_ID, ANY_CONTEXT, true); + Mockito.verify(userValidations).validateUserRole(user, adminRoles); + Mockito.verify(componentValidations).validateComponentIsCheckedOutByUser(COMPONENT_ID, ComponentTypeEnum.RESOURCE, + USER_ID); + } + + @Test + public void testValidateUserCanWorkOnComponentGivingComponent() { + User user = new User(); + Component component = Mockito.mock(Component.class); + List adminRoles = new ArrayList<>(); + adminRoles.add(Role.ADMIN); + adminRoles.add(Role.DESIGNER); + Mockito.when(userValidations.validateUserExists(USER_ID, ANY_CONTEXT, true)).thenReturn(user); + + accessValidations.validateUserCanWorkOnComponent(component, USER_ID, ANY_CONTEXT); + + Mockito.verify(userValidations, atLeast(1)).validateUserExists(USER_ID, ANY_CONTEXT, true); + Mockito.verify(userValidations).validateUserRole(user, adminRoles); + Mockito.verify(componentValidations).validateComponentIsCheckedOutByUser(component, USER_ID); + } + + @Test + public void testValidateUserExists() { + accessValidations.validateUserExists(COMPONENT_ID, ANY_CONTEXT); + Mockito.verify(userValidations).validateUserExists(COMPONENT_ID, ANY_CONTEXT, true); + } + + @Test + public void validateUserExist() { + accessValidations.validateUserExist(COMPONENT_ID, ANY_CONTEXT); + Mockito.verify(userValidations).validateUserExists(COMPONENT_ID, ANY_CONTEXT, false); + } + + @Test + public void userIsAdminOrDesigner() { + User user = new User(); + List adminRoles = new ArrayList<>(); + adminRoles.add(Role.ADMIN); + adminRoles.add(Role.DESIGNER); + Mockito.when(userValidations.validateUserExists(COMPONENT_ID, ANY_CONTEXT, true)).thenReturn(user); + + accessValidations.userIsAdminOrDesigner(COMPONENT_ID, ANY_CONTEXT); + + Mockito.verify(userValidations).validateUserRole(user, adminRoles); + } +} \ No newline at end of file diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/info/ArtifactAccessInfoTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/info/ArtifactAccessInfoTest.java index 5828f71983..03d305b6fe 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/info/ArtifactAccessInfoTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/info/ArtifactAccessInfoTest.java @@ -1,23 +1,50 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 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========================================================= + * Modifications copyright (c) 2019 Nokia + * ================================================================================ + */ package org.openecomp.sdc.be.info; import org.junit.Test; import org.openecomp.sdc.be.resources.data.ESArtifactData; import static com.google.code.beanmatchers.BeanMatchers.hasValidGettersAndSetters; +import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; - public class ArtifactAccessInfoTest { - @Test public void shouldHaveValidGettersAndSetters() { assertThat(ArtifactAccessInfo.class, hasValidGettersAndSetters()); } @Test - public void testCtor() throws Exception { - new ArtifactAccessInfo(new ESArtifactData()); + public void testArtifactAccessInfoConstructorUsingESArtifactData() { + ArtifactAccessInfo artifactAccessInfo = new ArtifactAccessInfo(new ESArtifactData("anyId")); + assertThat(artifactAccessInfo.getId(), is("anyId")); + } + @Test + public void testArtifactAccessInfoConstructorUsingServletContext() { + ArtifactAccessInfo artifactAccessInfo = new ArtifactAccessInfo("http://localhost/test"); + assertThat(artifactAccessInfo.getUrl(), is("http://localhost/test/resources/artifacts/")); } + } \ No newline at end of file diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/user/UserAdminValidatorTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/user/UserAdminValidatorTest.java index 23e619d764..2198c4b18f 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/user/UserAdminValidatorTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/user/UserAdminValidatorTest.java @@ -1,51 +1,123 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2018 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========================================================= + * Modifications copyright (c) 2019 Nokia + * ================================================================================ + */ + package org.openecomp.sdc.be.user; +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertThat; + +import org.hamcrest.core.IsNull; import org.junit.Test; public class UserAdminValidatorTest { - private UserAdminValidator createTestSubject() { - return UserAdminValidator.getInstance(); + @Test + public void testGetInstance() { + UserAdminValidator result = createTestSubject(); + assertThat(result, is(IsNull.notNullValue())); + } + + @Test + public void testShouldValidateCorrectEmail() { + UserAdminValidator validator = createTestSubject(); + String email = "test@test.com"; + boolean result = validator.validateEmail(email); + assertThat(result, is(true)); + } + + @Test + public void testShouldNotValidateEmailWithoutAt() { + UserAdminValidator validator = createTestSubject(); + String email = "test#test.com"; + boolean result = validator.validateEmail(email); + assertThat(result, is(false)); + } + + @Test + public void testShouldNotValidateEmailWithoutDomainSuffix() { + UserAdminValidator validator = createTestSubject(); + String email = "test@test"; + boolean result = validator.validateEmail(email); + assertThat(result, is(false)); } @Test - public void testGetInstance() throws Exception { - UserAdminValidator result; + public void testShouldNotValidateEmailWithoutPrefix() { + UserAdminValidator validator = createTestSubject(); + String email = "@test.com"; + boolean result = validator.validateEmail(email); + assertThat(result, is(false)); + } - // default test - result = UserAdminValidator.getInstance(); + @Test + public void testShouldValidateUserId() { + UserAdminValidator testSubject = createTestSubject(); + String userId = "User"; + boolean result = testSubject.validateUserId(userId); + assertThat(result, is(true)); } @Test - public void testValidateEmail() throws Exception { - UserAdminValidator testSubject; - String hex = ""; - boolean result; + public void testShouldNotValidateUserIdLongerThan25Characters() { + UserAdminValidator testSubject = createTestSubject(); + String userId = "User1user2user3user4user5toLong"; + boolean result = testSubject.validateUserId(userId); + assertThat(result, is(false)); + } - // default test - testSubject = createTestSubject(); - result = testSubject.validateEmail(hex); + @Test + public void testShouldNotValidateUserIdWithMulipleWords() { + UserAdminValidator testSubject = createTestSubject(); + String userId = "User 1"; + boolean result = testSubject.validateUserId(userId); + assertThat(result, is(false)); } @Test - public void testValidateUserId() throws Exception { - UserAdminValidator testSubject; + public void testShouldNotValidateEmptyUserId() { + UserAdminValidator testSubject = createTestSubject(); String userId = ""; - boolean result; + boolean result = testSubject.validateUserId(userId); + assertThat(result, is(false)); + } - // default test - testSubject = createTestSubject(); - result = testSubject.validateUserId(userId); + @Test + public void testValidateCorrectRole() { + UserAdminValidator testSubject = createTestSubject(); + String role = "ADMIN"; + boolean result = testSubject.validateRole(role); + assertThat(result, is(true)); } @Test - public void testValidateRole() throws Exception { - UserAdminValidator testSubject; - String role = ""; - boolean result; + public void testValidateIncorrectRole() { + UserAdminValidator testSubject = createTestSubject(); + String role = "DEVELOPER"; + boolean result = testSubject.validateRole(role); + assertThat(result, is(false)); + } - // default test - testSubject = createTestSubject(); - result = testSubject.validateRole(role); + private UserAdminValidator createTestSubject() { + return UserAdminValidator.getInstance(); } + } \ No newline at end of file -- cgit 1.2.3-korg