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>
private final UserValidations userValidations;
private final ComponentValidations componentValidations;
-
public AccessValidations(UserValidations userValidations, ComponentValidations componentValidations) {
this.userValidations = userValidations;
this.componentValidations = componentValidations;
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) {
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);
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);
- }
}
urlBuilder = urlBuilder.append(servletContext).append("/");
urlBuilder.append("resources/")
// .append(artifactData.getResourceId()).append("/")
-
- .append("/artifacts/");
+ .append("artifacts/");
this.url = urlBuilder.toString();
-
}
private String name;
--- /dev/null
+/*-
+ * ============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
+/*-
+ * ============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
+/*-
+ * ============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