Some unit tests for catalog-be 51/84251/3
authorTomasz Golabek <tomasz.golabek@nokia.com>
Thu, 4 Apr 2019 15:52:26 +0000 (17:52 +0200)
committerOren Kleks <orenkle@amdocs.com>
Wed, 17 Apr 2019 06:49:39 +0000 (06:49 +0000)
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>
catalog-be/src/main/java/org/openecomp/sdc/be/components/validation/AccessValidations.java
catalog-be/src/main/java/org/openecomp/sdc/be/info/ArtifactAccessInfo.java
catalog-be/src/test/java/org/openecomp/sdc/be/components/validation/AccessValidationsTest.java [new file with mode: 0644]
catalog-be/src/test/java/org/openecomp/sdc/be/info/ArtifactAccessInfoTest.java
catalog-be/src/test/java/org/openecomp/sdc/be/user/UserAdminValidatorTest.java

index 335a939..28723f6 100644 (file)
@@ -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);
-    }
 }
index bedbff4..490684e 100644 (file)
@@ -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 (file)
index 0000000..f04d710
--- /dev/null
@@ -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
index 5828f71..03d305b 100644 (file)
@@ -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
index 23e619d..2198c4b 100644 (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