Improved unit tests for ComponentValidationUtils and Sonar fixes. 81/90781/2
authork.kedron <k.kedron@partner.samsung.com>
Tue, 2 Jul 2019 10:55:35 +0000 (12:55 +0200)
committerOren Kleks <orenkle@amdocs.com>
Sun, 7 Jul 2019 13:05:01 +0000 (13:05 +0000)
Improved unit tests for ComponentValidationUtils.
Checkstyle for ComponentValidationUtils.
Add a private constructor to hide the implicit public one
in the ComponentValidationUtils.

Issue-ID: SDC-2327
Signed-off-by: Krystian Kedron <k.kedron@partner.samsung.com>
Change-Id: I8a2f2f7b9a5e6475a4e28d13f3acab333b4af6da

catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/utils/ComponentValidationUtils.java
catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/utils/ComponentValidationUtilsTest.java

index 5c22120..9621f9e 100644 (file)
@@ -3,6 +3,7 @@
  * SDC
  * ================================================================================
  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * Modifications Copyright (c) 2019 Samsung
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -20,7 +21,6 @@
 
 package org.openecomp.sdc.be.model.operations.utils;
 
-import fj.data.Either;
 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
 import org.openecomp.sdc.be.model.Component;
 import org.openecomp.sdc.be.model.LifecycleStateEnum;
@@ -29,10 +29,15 @@ import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade
 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
 import org.openecomp.sdc.common.log.wrappers.Logger;
 
+import fj.data.Either;
+
 public class ComponentValidationUtils {
 
     private static final Logger log = Logger.getLogger(ComponentValidationUtils.class.getName());
 
+    private ComponentValidationUtils() {
+    }
+
     public static boolean canWorkOnResource(Resource resource, String userId) {
         // verify resource is checked-out
         if (resource.getLifecycleState() != LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT) {
@@ -52,9 +57,11 @@ public class ComponentValidationUtils {
         return true;
     }
 
-    public static boolean canWorkOnComponent(String componentId, ToscaOperationFacade toscaOperationFacade, String userId) {
+    public static boolean canWorkOnComponent(String componentId,
+        ToscaOperationFacade toscaOperationFacade, String userId) {
 
-        Either<Component, StorageOperationStatus> getResourceResult = toscaOperationFacade.getToscaElement(componentId, JsonParseFlagEnum.ParseMetadata);
+        Either<Component, StorageOperationStatus> getResourceResult =
+            toscaOperationFacade.getToscaElement(componentId, JsonParseFlagEnum.ParseMetadata);
 
         if (getResourceResult.isRight()) {
             log.debug("Failed to retrieve component, component id {}", componentId);
@@ -69,7 +76,8 @@ public class ComponentValidationUtils {
         return canWorkOnComponent(component.getLifecycleState(), component.getLastUpdaterUserId(), userId);
     }
 
-    private static boolean canWorkOnComponent(LifecycleStateEnum lifecycleState, String lastUpdaterUserId, String userId) {
+    private static boolean canWorkOnComponent(LifecycleStateEnum lifecycleState,
+        String lastUpdaterUserId, String userId) {
         // verify resource is checked-out
         if (lifecycleState != LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT) {
             log.debug("resource is not checked-out");
index b2e7c60..1b5b9cb 100644 (file)
@@ -1,16 +1,57 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * SDC
+ * ================================================================================
+ * Modifications Copyright (c) 2019 Samsung
+ * ================================================================================
+ * 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.model.operations.utils;
 
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.when;
+
 import org.junit.Before;
 import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
+import org.openecomp.sdc.be.model.Component;
 import org.openecomp.sdc.be.model.LifecycleStateEnum;
 import org.openecomp.sdc.be.model.Resource;
+import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
 
-import static org.junit.Assert.assertTrue;
+import fj.data.Either;
 
+@RunWith(MockitoJUnitRunner.class)
 public class ComponentValidationUtilsTest {
 
+    private static final String USER_ID = "jh003";
+    private static final String BAD_USER_ID = "badID";
+    private static final String COMPONENT_ID = "componentID";
+
     private Resource resource;
 
+    @Mock
+    ToscaOperationFacade toscaOperationFacade;
+    @Mock
+    Component component;
+
     @Before
     public void setup() {
         resource = new Resource();
@@ -21,11 +62,81 @@ public class ComponentValidationUtilsTest {
 
     @Test
     public void testCanWorkOnResource() {
-        assertTrue (ComponentValidationUtils.canWorkOnResource(resource, "jh003"));
+        assertTrue(ComponentValidationUtils.canWorkOnResource(resource, USER_ID));
+    }
+
+    @Test
+    public void testCanWorkOnBadResourceAndBadUser() {
+        assertFalse(ComponentValidationUtils.canWorkOnResource(resource, BAD_USER_ID));
+
+        resource.setLifecycleState(LifecycleStateEnum.CERTIFICATION_IN_PROGRESS);
+        assertFalse(ComponentValidationUtils.canWorkOnResource(resource, USER_ID));
+
+        resource.setIsDeleted(true);
+        assertFalse(ComponentValidationUtils.canWorkOnResource(resource, USER_ID));
     }
 
     @Test
     public void testCanWorkOnComponent() {
-        assertTrue (ComponentValidationUtils.canWorkOnComponent(resource, "jh003"));
+        // given
+        when(component.getLifecycleState()).thenReturn(resource.getLifecycleState());
+        when(component.getLastUpdaterUserId()).thenReturn(resource.getLastUpdaterUserId());
+
+        when(toscaOperationFacade.getToscaElement(eq(COMPONENT_ID),
+            eq(JsonParseFlagEnum.ParseMetadata))).thenReturn(Either.left(component));
+
+        // when
+        boolean resultFromExtractComponent = ComponentValidationUtils
+            .canWorkOnComponent(COMPONENT_ID, toscaOperationFacade, USER_ID);
+        boolean resultFromComponent =
+            ComponentValidationUtils.canWorkOnComponent(component, USER_ID);
+
+        // then
+        assertTrue(resultFromExtractComponent);
+        assertTrue(resultFromComponent);
+    }
+
+    @Test
+    public void testCanWorkOnBadComponent() {
+        // given
+        when(component.getLifecycleState())
+            .thenReturn(LifecycleStateEnum.CERTIFICATION_IN_PROGRESS);
+        when(component.getLastUpdaterUserId()).thenReturn(resource.getLastUpdaterUserId());
+
+        when(toscaOperationFacade.getToscaElement(eq(COMPONENT_ID),
+            eq(JsonParseFlagEnum.ParseMetadata))).thenReturn(Either.left(component));
+
+        // when
+        boolean resultFromExtractComponent = ComponentValidationUtils
+            .canWorkOnComponent(COMPONENT_ID, toscaOperationFacade, USER_ID);
+        boolean resultFromComponent =
+            ComponentValidationUtils.canWorkOnComponent(component, USER_ID);
+
+        // then
+        assertFalse(resultFromExtractComponent);
+        assertFalse(resultFromComponent);
+    }
+
+    @Test
+    public void testCanWorkOnComponentWithBadUser() {
+        // given
+        when(component.getLifecycleState()).thenReturn(resource.getLifecycleState());
+        when(component.getLastUpdaterUserId()).thenReturn(resource.getLastUpdaterUserId());
+
+        when(toscaOperationFacade.getToscaElement(eq(COMPONENT_ID),
+            eq(JsonParseFlagEnum.ParseMetadata))).thenReturn(Either.left(component));
+
+        // when
+        boolean resultFromExtractComponent = ComponentValidationUtils
+            .canWorkOnComponent(COMPONENT_ID, toscaOperationFacade, BAD_USER_ID);
+        boolean resultFromComponent =
+            ComponentValidationUtils.canWorkOnComponent(component, BAD_USER_ID);
+        boolean resultFromComponentWithNullUser =
+            ComponentValidationUtils.canWorkOnComponent(component, null);
+
+        // then
+        assertFalse(resultFromExtractComponent);
+        assertFalse(resultFromComponent);
+        assertFalse(resultFromComponentWithNullUser);
     }
-}
\ No newline at end of file
+}