diff options
author | Tomasz Golabek <tomasz.golabek@nokia.com> | 2019-04-04 17:52:26 +0200 |
---|---|---|
committer | Oren Kleks <orenkle@amdocs.com> | 2019-04-17 06:49:39 +0000 |
commit | 8fa77ad4d4c2498e40a4c23e6826d9ad73a1dab8 (patch) | |
tree | 2974630bcf758e8e3247a5f58e15c836a043d108 /catalog-be/src | |
parent | 6df542cf030b3f22111f7f091f43c28fcd395bb1 (diff) |
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 <tomasz.golabek@nokia.com>
Diffstat (limited to 'catalog-be/src')
5 files changed, 265 insertions, 45 deletions
diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/validation/AccessValidations.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/validation/AccessValidations.java index 335a9398d7..28723f6c29 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/validation/AccessValidations.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/validation/AccessValidations.java @@ -14,7 +14,6 @@ public class AccessValidations { private final UserValidations userValidations; private final ComponentValidations componentValidations; - public AccessValidations(UserValidations userValidations, ComponentValidations componentValidations) { this.userValidations = userValidations; this.componentValidations = componentValidations; @@ -31,8 +30,16 @@ public class AccessValidations { validateUserIsAdminOrDesigner(user); return componentValidations.validateComponentIsCheckedOutByUser(componentId, componentType, userId); } - private User retrieveUser(String userId, String actionContext) { - return userValidations.validateUserExists(userId, actionContext, true); + + + public void validateUserCanWorkOnComponent(Component component, String userId, String actionContext) { + User user = retrieveUser(userId, actionContext); + validateUserIsAdminOrDesigner(user); + componentValidations.validateComponentIsCheckedOutByUser(component, userId); + } + + public void validateUserExists(String userId, String context) { + retrieveUser(userId, context); } public void validateUserExist(String userId, String actionContext) { @@ -45,6 +52,10 @@ public class AccessValidations { return user; } + private User retrieveUser(String userId, String actionContext) { + return userValidations.validateUserExists(userId, actionContext, true); + } + private void validateUserIsAdminOrDesigner(User user) { List<Role> roles = new ArrayList<>(2); roles.add(Role.ADMIN); @@ -52,13 +63,4 @@ public class AccessValidations { userValidations.validateUserRole(user, roles); } - public void validateUserCanWorkOnComponent(Component component, String userId, String actionContext) { - User user = retrieveUser(userId, actionContext); - validateUserIsAdminOrDesigner(user); - componentValidations.validateComponentIsCheckedOutByUser(component, userId); - } - - public void validateUserExists(String userId, String context) { - retrieveUser(userId, context); - } } diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/info/ArtifactAccessInfo.java b/catalog-be/src/main/java/org/openecomp/sdc/be/info/ArtifactAccessInfo.java index bedbff46b1..490684e20d 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/info/ArtifactAccessInfo.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/info/ArtifactAccessInfo.java @@ -35,10 +35,8 @@ public class ArtifactAccessInfo { urlBuilder = urlBuilder.append(servletContext).append("/"); urlBuilder.append("resources/") // .append(artifactData.getResourceId()).append("/") - - .append("/artifacts/"); + .append("artifacts/"); this.url = urlBuilder.toString(); - } private String name; 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<Role> 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<Role> 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<Role> 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 |