Test coverage for RequestValidationPolicy 12/79012/4
authorJoss Armstrong <joss.armstrong@ericsson.com>
Fri, 22 Feb 2019 12:36:50 +0000 (12:36 +0000)
committerTakamune Cho <takamune.cho@att.com>
Fri, 22 Feb 2019 17:05:33 +0000 (17:05 +0000)
Added test cases for untested parts of code

Issue-ID: APPC-1482
Change-Id: I3dfa64b4eb1f69a08ca3346d75ae362affd55e0d
Signed-off-by: Joss Armstrong <joss.armstrong@ericsson.com>
appc-dispatcher/appc-request-handler/appc-request-handler-core/src/main/java/org/onap/appc/validationpolicy/RequestValidationPolicy.java
appc-dispatcher/appc-request-handler/appc-request-handler-core/src/test/java/org/onap/appc/validationpolicy/RequestValidationPolicyTest.java [new file with mode: 0644]

index 5d67665..af6dc31 100644 (file)
@@ -5,6 +5,8 @@
  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Copyright (C) 2017 Amdocs
+ * ================================================================================
+ * Modifications Copyright (C) 2019 Ericsson
  * =============================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -85,12 +87,12 @@ public class RequestValidationPolicy {
                     Set<VNFOperation> exclusionSet = null;
                     if (ruleDTO.getInclusionList() != null && !ruleDTO.getInclusionList().isEmpty()) {
                         inclusionSet = ruleDTO.getInclusionList().stream()
-                                .map(VNFOperation::findByString).filter(operation -> operation!=null)
+                                .map(VNFOperation::findByString).filter(operation -> operation != null)
                                 .collect(Collectors.toSet());
                     }
                     if (ruleDTO.getExclusionList() != null && !ruleDTO.getExclusionList().isEmpty()) {
                         exclusionSet = ruleDTO.getExclusionList().stream()
-                                .map(VNFOperation::findByString).filter(operation -> operation!=null)
+                                .map(VNFOperation::findByString).filter(operation -> operation != null)
                                 .collect(Collectors.toSet());
                     }
                     org.onap.appc.validationpolicy.rules.Rule rule = RuleFactory
@@ -100,7 +102,7 @@ public class RequestValidationPolicy {
                 actionInProgressRuleExecutor = new ActionInProgressRuleExecutor(Collections.unmodifiableMap(rules));
             });
         } catch (Exception e) {
-            logger.error("Error reading request validation policies",e);
+            logger.error("Error reading request validation policies", e);
         }
     }
 
@@ -112,9 +114,9 @@ public class RequestValidationPolicy {
                        "GROUP BY ARTIFACT_NAME";
         ArrayList<String> arguments = new ArrayList<>();
         arguments.add("request_validation_policy");
-        String jsonContent =null;
+        String jsonContent = null;
         try{
-            CachedRowSet rowSet = dbLibService.getData(query,arguments,schema);
+            CachedRowSet rowSet = dbLibService.getData(query, arguments, schema);
             if(rowSet.next()){
                 jsonContent = rowSet.getString("ARTIFACT_CONTENT");
             }
@@ -126,14 +128,14 @@ public class RequestValidationPolicy {
             }
         }
         catch(SQLException e){
-            logger.error("Error accessing database",e);
+            logger.error("Error accessing database", e);
             throw new RuntimeException(e);
         }
         return jsonContent;
     }
 
     public RuleExecutor getInProgressRuleExecutor(){
-        if(actionInProgressRuleExecutor ==null){
+        if(actionInProgressRuleExecutor == null){
             throw new RuntimeException("Rule executor not available, initialization of RequestValidationPolicy failed");
         }
         return actionInProgressRuleExecutor;
diff --git a/appc-dispatcher/appc-request-handler/appc-request-handler-core/src/test/java/org/onap/appc/validationpolicy/RequestValidationPolicyTest.java b/appc-dispatcher/appc-request-handler/appc-request-handler-core/src/test/java/org/onap/appc/validationpolicy/RequestValidationPolicyTest.java
new file mode 100644 (file)
index 0000000..b8e4da3
--- /dev/null
@@ -0,0 +1,81 @@
+/*
+ * ============LICENSE_START=======================================================
+ * ONAP : APPC
+ * ================================================================================
+ * Copyright (C) 2019 Ericsson
+ * ================================================================================
+ * 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.onap.appc.validationpolicy;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import java.sql.SQLException;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.mockito.Mockito;
+import org.onap.ccsdk.sli.core.dblib.DbLibService;
+import com.sun.rowset.CachedRowSetImpl;
+
+public class RequestValidationPolicyTest {
+
+    private final String TEST_CONTENT = "TEST_CONTENT";
+    private final String ARTIFACT_CONTENT = "ARTIFACT_CONTENT";
+    private DbLibService db;
+    private RequestValidationPolicy policy;
+    private CachedRowSetImpl rowset;
+
+    @Rule
+    public ExpectedException expectedEx = ExpectedException.none();
+
+    @Before
+    public void setup() throws SQLException {
+        db = Mockito.mock(DbLibService.class);
+        rowset = Mockito.mock(CachedRowSetImpl.class);
+        Mockito.when(rowset.next()).thenReturn(true);
+        Mockito.when(db.getData(Mockito.anyString(), Mockito.any(), Mockito.anyString())).thenReturn(rowset);
+        policy = new RequestValidationPolicy();
+        policy.setDbLibService(db);
+    }
+
+    @Test
+    public void testGetPolicyJson() throws SQLException {
+        Mockito.when(rowset.getString(ARTIFACT_CONTENT)).thenReturn(TEST_CONTENT);
+        assertEquals(TEST_CONTENT, policy.getPolicyJson());
+    }
+
+    @Test
+    public void testGetPolicyJsonBlank() throws SQLException {
+        Mockito.when(rowset.next()).thenThrow(new SQLException());
+        expectedEx.expect(RuntimeException.class);
+        policy.getPolicyJson();
+    }
+
+    @Test
+    public void testGetPolicyJsonError() throws SQLException {
+        Mockito.when(rowset.getString(ARTIFACT_CONTENT)).thenReturn("");
+        assertEquals("", policy.getPolicyJson());
+    }
+
+    @Test
+    public void testGetInProgressRuleExecutor() {
+        expectedEx.expect(RuntimeException.class);
+        expectedEx.expectMessage("Rule executor not available, initialization of RequestValidationPolicy failed");
+        policy.getInProgressRuleExecutor();
+    }
+}