Correct unit tests for DBService class 65/76065/3
authorJoss Armstrong <joss.armstrong@ericsson.com>
Mon, 21 Jan 2019 13:02:33 +0000 (13:02 +0000)
committerTakamune Cho <takamune.cho@att.com>
Mon, 21 Jan 2019 21:22:51 +0000 (21:22 +0000)
Increased test coverage from 74% to 92%
in DBService class

Issue-ID: APPC-1335
Change-Id: Iabd792eb104429d5090e0099dba463e8ad6d499a
Signed-off-by: Joss Armstrong <joss.armstrong@ericsson.com>
appc-inbound/appc-artifact-handler/provider/src/main/java/org/onap/appc/artifact/handler/dbservices/DBService.java
appc-inbound/appc-artifact-handler/provider/src/test/java/org/onap/appc/artifact/handler/dbservices/DBServiceTest.java
appc-inbound/appc-artifact-handler/provider/src/test/java/org/onap/appc/artifact/handler/dbservices/MockDBService.java
appc-inbound/appc-artifact-handler/provider/src/test/java/org/onap/appc/artifact/handler/dbservices/MockSvcLogicResource.java
appc-inbound/appc-artifact-handler/provider/src/test/java/org/onap/appc/artifact/handler/dbservices/MockSvcLogicResourceFailure.java [new file with mode: 0644]

index 24dd4f4..cf45bc3 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.
@@ -80,7 +82,7 @@ public class DBService {
         if (serviceLogic != null && ctx != null) {
             String key = "select max(internal_version) as maximum from ASDC_ARTIFACTS  WHERE ARTIFACT_NAME = '"
                 + artifactName + "'";
-            log.info("Getting internal Versoin :" + key);
+            log.info("Getting internal Version :" + key);
             status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
             if (status.toString().equals(FAILURE_PARAM)) {
                 throw new SvcLogicException("Error - getting internal Artifact Number");
@@ -141,7 +143,7 @@ public class DBService {
                 + " message_type = $log-message-type , " + " message = $log-message ;";
             status = serviceLogic.save("SQL", false, false, key, null, prefix, ctx);
             if (status.toString().equals(FAILURE_PARAM)) {
-                throw new SvcLogicException("Error while loging data");
+                throw new SvcLogicException("Error while logging data");
             }
 
         }
index aac7cbe..0676d59 100644 (file)
@@ -8,6 +8,8 @@
  * =============================================================================
  * Modifications Copyright (C) 2018 IBM
  * ================================================================================
+ * 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.
  * You may obtain a copy of the License at
 
 package org.onap.appc.artifact.handler.dbservices;
 
-import org.junit.Ignore;
+import org.junit.Rule;
 import org.junit.Test;
+import org.junit.rules.ExpectedException;
 import org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants;
 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
+import org.onap.ccsdk.sli.core.sli.SvcLogicException;
+import org.onap.ccsdk.sli.core.sli.SvcLogicResource.QueryStatus;
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
 
 public class DBServiceTest {
 
-    @Test
-    public void testSaveArtifacts() throws Exception {
-        MockDBService dbService = MockDBService.initialise();
-        SvcLogicContext ctx = new SvcLogicContext();
-        ctx.setAttribute("test", "test");
-        int internalVersion = 1;
-        dbService.saveArtifacts(ctx, internalVersion);
-    }
+    @Rule
+    public ExpectedException expectedEx = ExpectedException.none();
 
     @Test
-    public void testSaveArtifactsException() throws Exception {
+    public void testSaveArtifacts() throws Exception {
         MockDBService dbService = MockDBService.initialise();
         SvcLogicContext ctx = new SvcLogicContext();
         ctx.setAttribute("test", "test");
@@ -57,17 +57,7 @@ public class DBServiceTest {
         SvcLogicContext ctx = new SvcLogicContext();
         ctx.setAttribute("test", "test");
         String prefix = "test";
-        dbService.logData(ctx, prefix);
-    }
-
-
-    @Test
-    public void testLogDataException() throws Exception {
-        MockDBService dbService = MockDBService.initialise();
-        SvcLogicContext ctx = new SvcLogicContext();
-        ctx.setAttribute("test", "test");
-        String prefix = "test";
-        dbService.logData(ctx, prefix);
+        assertEquals(QueryStatus.SUCCESS, dbService.logData(ctx, prefix));
     }
 
     @Test
@@ -80,15 +70,6 @@ public class DBServiceTest {
         dbService.processConfigActionDg(ctx, isUpdate);
     }
 
-    @Test
-    public void testProcessConfigActionDgException() throws Exception {
-        MockDBService dbService = MockDBService.initialise();
-        SvcLogicContext ctx = new SvcLogicContext();
-        ctx.setAttribute("test", "test");
-        boolean isUpdate = true;
-        ctx.setAttribute(SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE, "Reference");
-        dbService.processConfigActionDg(ctx, isUpdate);
-    }
 
     @Test
     public void testGetModelDataInformationbyArtifactName() throws Exception {
@@ -96,16 +77,7 @@ public class DBServiceTest {
         SvcLogicContext ctx = new SvcLogicContext();
         ctx.setAttribute("test", "test");
         String artifactName = "test";
-        dbService.getModelDataInformationbyArtifactName(artifactName);
-    }
-
-    @Test
-    public void testGetModelDataInformationbyArtifactNameException() throws Exception {
-        MockDBService dbService = MockDBService.initialise();
-        SvcLogicContext ctx = new SvcLogicContext();
-        ctx.setAttribute("test", "test");
-        String artifactName = "test";
-        dbService.getModelDataInformationbyArtifactName(artifactName);
+        assertNotNull(dbService.getModelDataInformationbyArtifactName(artifactName));
     }
 
     @Test
@@ -113,18 +85,6 @@ public class DBServiceTest {
         MockDBService dbService = MockDBService.initialise();
         SvcLogicContext ctx = new SvcLogicContext();
         ctx.setAttribute("test", "test");
-        String artifactName = "test";
-        String artifactId = "TestArtifact";
-        String yangContents = "TestYangContents";
-        dbService.updateYangContents(ctx, artifactId, yangContents);
-    }
-
-    @Test
-    public void testUpdateYangContentsException() throws Exception {
-        MockDBService dbService = MockDBService.initialise();
-        SvcLogicContext ctx = new SvcLogicContext();
-        ctx.setAttribute("test", "test");
-        String artifactName = "test";
         String artifactId = "TestArtifact";
         String yangContents = "TestYangContents";
         dbService.updateYangContents(ctx, artifactId, yangContents);
@@ -158,16 +118,7 @@ public class DBServiceTest {
     }
 
     @Test
-    public void testprocessDpwnloadDGReference() throws Exception {
-        MockDBService dbService = MockDBService.initialise();
-        SvcLogicContext ctx = new SvcLogicContext();
-        ctx.setAttribute("test", "test");
-        boolean isUpdate = true;
-        dbService.processDownloadDgReference(ctx, isUpdate);
-    }
-
-    @Test
-    public void testprocessDpwnloadDGReferenceException() throws Exception {
+    public void testProcessDownloadDGReference() throws Exception {
         MockDBService dbService = MockDBService.initialise();
         SvcLogicContext ctx = new SvcLogicContext();
         ctx.setAttribute("test", "test");
@@ -184,15 +135,6 @@ public class DBServiceTest {
         dbService.processVnfcReference(ctx, isUpdate);
     }
 
-    @Test
-    public void testProcessVnfcReferenceException() throws Exception {
-        MockDBService dbService = MockDBService.initialise();
-        SvcLogicContext ctx = new SvcLogicContext();
-        ctx.setAttribute("test", "test");
-        boolean isUpdate = true;
-        dbService.processVnfcReference(ctx, isUpdate);
-    }
-
     @Test
     public void testProcessDeviceAuthentication() throws Exception {
         MockDBService dbService = MockDBService.initialise();
@@ -216,15 +158,6 @@ public class DBServiceTest {
         assertEquals(true,ctx.getAttribute("keys").contains("DEVICE_AUTHENTICATION"));
     }
 
-    //@Test
-    public void testProcessDeviceAuthenticationException() throws Exception {
-        MockDBService dbService = MockDBService.initialise();
-        SvcLogicContext ctx = new SvcLogicContext();
-        ctx.setAttribute("test", "test");
-        boolean isUpdate = true;
-        dbService.processDeviceAuthentication(ctx, isUpdate);
-    }
-
     @Test
     public void testProcessDeviceInterfaceProtocol() throws Exception {
         MockDBService dbService = MockDBService.initialise();
@@ -245,75 +178,183 @@ public class DBServiceTest {
     }
 
     @Test
-    public void testProcessDeviceInterfaceProtocolException() throws Exception {
+    public void testProcessSdcReferences() throws Exception {
         MockDBService dbService = MockDBService.initialise();
         SvcLogicContext ctx = new SvcLogicContext();
         ctx.setAttribute("test", "test");
+        ctx.setAttribute(SdcArtifactHandlerConstants.FILE_CATEGORY, "testCategory");
         boolean isUpdate = true;
-        dbService.processDeviceInterfaceProtocol(ctx, isUpdate);
+        dbService.processSdcReferences(ctx, isUpdate);
     }
 
     @Test
-    public void testProcessSdcReferences() throws Exception {
+    public void testIsArtifactUpdateRequired() throws Exception {
         MockDBService dbService = MockDBService.initialise();
         SvcLogicContext ctx = new SvcLogicContext();
         ctx.setAttribute("test", "test");
-        ctx.setAttribute(SdcArtifactHandlerConstants.FILE_CATEGORY, "testCategory");
-        boolean isUpdate = true;
-        dbService.processSdcReferences(ctx, isUpdate);
+        String db = "db";
+        dbService.isArtifactUpdateRequired(ctx, db);
     }
 
-    @Ignore
-    public void testProcessSdcReferencesException() throws Exception {
+    @Test
+    public void testgetArtifactID() throws Exception {
         MockDBService dbService = MockDBService.initialise();
         SvcLogicContext ctx = new SvcLogicContext();
         ctx.setAttribute("test", "test");
-        boolean isUpdate = true;
-        dbService.processSdcReferences(ctx, isUpdate);
+        String db = "db";
+        dbService.getArtifactID(ctx, db);
     }
 
     @Test
-    public void testIsArtifactUpdateRequired() throws Exception {
+    public void testGetDownLoadDGReference() throws Exception {
         MockDBService dbService = MockDBService.initialise();
         SvcLogicContext ctx = new SvcLogicContext();
         ctx.setAttribute("test", "test");
-        String db = "db";
-        dbService.isArtifactUpdateRequired(ctx, db);
+        ctx.setAttribute(SdcArtifactHandlerConstants.DEVICE_PROTOCOL, "CLI");
+        assertEquals("TestDG", dbService.getDownLoadDGReference(ctx));
     }
 
     @Test
-    public void testIsArtifactUpdateRequiredExcetion() throws Exception {
-        MockDBService dbService = MockDBService.initialise();
+    public void testInitialise() {
+        DBService dbService = DBService.initialise();
+        assertNotNull(dbService);
+    }
+
+    @Test
+    public void testGetInternalVersionNumberException() throws SvcLogicException {
+        MockDBService dbService = MockDBService.initialise(true);
+        SvcLogicContext ctx = new SvcLogicContext();
+        expectedEx.expect(SvcLogicException.class);
+        expectedEx.expectMessage("Error - getting internal Artifact Number");
+        dbService.getInternalVersionNumber(ctx, "artifactName", "prefix");
+    }
+
+    @Test
+    public void testGetArtifactIDException() throws SvcLogicException {
+        MockDBService dbService = MockDBService.initialise(true);
+        SvcLogicContext ctx = new SvcLogicContext();
+        expectedEx.expect(SvcLogicException.class);
+        expectedEx.expectMessage("Error - getting  Artifact ID from database");
+        dbService.getArtifactID(ctx, "artifactName");
+    }
+
+    @Test
+    public void testSaveArtifactsException() throws Exception {
+        MockDBService dbService = MockDBService.initialise(true);
+        SvcLogicContext ctx = new SvcLogicContext();
+        expectedEx.expect(SvcLogicException.class);
+        expectedEx.expectMessage("Error While processing storing Artifact: null");
+        dbService.saveArtifacts(ctx, -1);
+    }
+
+    @Test
+    public void testLogDataException() throws Exception {
+        MockDBService dbService = MockDBService.initialise(true);
+        SvcLogicContext ctx = new SvcLogicContext();
+        expectedEx.expect(SvcLogicException.class);
+        expectedEx.expectMessage("Error while logging data");
+        dbService.logData(ctx, null);
+    }
+
+    @Test
+    public void testProcessSdcReferencesException() throws Exception {
+        MockDBService dbService = MockDBService.initialise(true);
+        SvcLogicContext ctx = new SvcLogicContext();
+        ctx.setAttribute("file-category", "test");
+        expectedEx.expect(SvcLogicException.class);
+        expectedEx.expectMessage("Error While processing sdc_reference table ");
+        dbService.processSdcReferences(ctx, false);
+    }
+
+    @Test
+    public void testIsArtifactUpdateRequiredException() throws Exception {
+        MockDBService dbService = MockDBService.initialise(true);
+        SvcLogicContext ctx = new SvcLogicContext();
+        expectedEx.expect(DBException.class);
+        expectedEx.expectMessage("An error occurred while checking for artifact update");
+        dbService.isArtifactUpdateRequired(ctx, "db");
+    }
+
+    @Test
+    public void testProcessDeviceInterfaceProtocolException() throws Exception {
+        MockDBService dbService = MockDBService.initialise(true);
+        SvcLogicContext ctx = new SvcLogicContext();
+        expectedEx.expect(SvcLogicException.class);
+        expectedEx.expectMessage("Error While processing DEVICE_INTERFACE_PROTOCOL table ");
+        dbService.processDeviceInterfaceProtocol(ctx, false);
+    }
+
+    @Test
+    public void testProcessDeviceAuthenticationException() throws Exception {
+        MockDBService dbService = MockDBService.initialise(true);
+        SvcLogicContext ctx = new SvcLogicContext();
+        boolean isUpdate = true;
+        expectedEx.expect(DBException.class);
+        expectedEx.expectMessage("An error occurred when processing device authentication");
+        dbService.processDeviceAuthentication(ctx, isUpdate);
+    }
+
+    @Test
+    public void testProcessVnfcReferenceException() throws Exception {
+        MockDBService dbService = MockDBService.initialise(true);
         SvcLogicContext ctx = new SvcLogicContext();
         ctx.setAttribute("test", "test");
-        String db = "db";
-        dbService.isArtifactUpdateRequired(ctx, db);
+        expectedEx.expect(SvcLogicException.class);
+        expectedEx.expectMessage("Error While processing VNFC_REFERENCE table ");
+        dbService.processVnfcReference(ctx, true);
     }
 
+    @Test
+    public void testProcessDownloadDGReferenceException() throws Exception {
+        MockDBService dbService = MockDBService.initialise(true);
+        SvcLogicContext ctx = new SvcLogicContext();
+        ctx.setAttribute("test", "test");
+        expectedEx.expect(SvcLogicException.class);
+        expectedEx.expectMessage("Error While processing DOWNLOAD_DG_REFERENCE table ");
+        dbService.processDownloadDgReference(ctx, false);
+    }
 
     @Test
-    public void testgetArtifactID() throws Exception {
-        MockDBService dbService = MockDBService.initialise();
+    public void testProcessConfigActionDgException() throws Exception {
+        MockDBService dbService = MockDBService.initialise(true);
         SvcLogicContext ctx = new SvcLogicContext();
         ctx.setAttribute("test", "test");
-        String db = "db";
-        dbService.getArtifactID(ctx, db);
+        ctx.setAttribute(SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE, "Reference");
+        expectedEx.expect(SvcLogicException.class);
+        expectedEx.expectMessage("Error While processing Configure DG Action table ");
+        dbService.processConfigActionDg(ctx, true);
     }
 
     @Test
-    public void testgetArtifactIDException() throws Exception {
-        MockDBService dbService = MockDBService.initialise();
+    public void testGetModelDataInformationbyArtifactNameException() throws Exception {
+        MockDBService dbService = MockDBService.initialise(true);
         SvcLogicContext ctx = new SvcLogicContext();
         ctx.setAttribute("test", "test");
-        String db = "db";
-        dbService.getArtifactID(ctx, db);
+        expectedEx.expect(SvcLogicException.class);
+        expectedEx.expectMessage("Error While processing is ArtifactUpdateRequiredforPD table ");
+        dbService.getModelDataInformationbyArtifactName("test");
     }
+
     @Test
-    public void testGetDownLoadDGReference() throws Exception {
-        MockDBService dbService = MockDBService.initialise();
+    public void testUpdateYangContentsException() throws Exception {
+        MockDBService dbService = MockDBService.initialise(true);
+        SvcLogicContext ctx = new SvcLogicContext();
+        ctx.setAttribute("test", "test");
+        String artifactId = "TestArtifact";
+        String yangContents = "TestYangContents";
+        expectedEx.expect(SvcLogicException.class);
+        expectedEx.expectMessage("Error While processing Configure DG Action table ");
+        dbService.updateYangContents(ctx, artifactId, yangContents);
+    }
+
+    @Test
+    public void testGetDownLoadDGReferenceException() throws Exception {
+        MockDBService dbService = MockDBService.initialise(true);
         SvcLogicContext ctx = new SvcLogicContext();
         ctx.setAttribute("test", "test");
         ctx.setAttribute(SdcArtifactHandlerConstants.DEVICE_PROTOCOL, "CLI");
+        expectedEx.expect(DBException.class);
+        expectedEx.expectMessage("An error occurred when getting DG reference");
         assertEquals("TestDG", dbService.getDownLoadDGReference(ctx));
     }
 }
index 57115d8..374f6b1 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.
@@ -25,20 +27,26 @@ package org.onap.appc.artifact.handler.dbservices;
 
 public class MockDBService extends DBService {
     private static MockDBService mockDgGeneralDBService = null;
-    private static MockSvcLogicResource serviceLogic = new MockSvcLogicResource();;
+    private static MockDBService mockDgGeneralDBServiceFailure = null;
+    private static MockSvcLogicResource serviceLogic = new MockSvcLogicResource();
+    private static MockSvcLogicResourceFailure serviceLogicFailure = new MockSvcLogicResourceFailure();
+
 
     public MockDBService() {
         super(serviceLogic);
         if (mockDgGeneralDBService != null) {
             mockDgGeneralDBService = new MockDBService(serviceLogic);
         }
-
     }
 
     public MockDBService(MockSvcLogicResource serviceLogic2) {
         super(serviceLogic);
     }
 
+    public MockDBService(MockSvcLogicResourceFailure serviceLogic2) {
+        super(serviceLogicFailure);
+    }
+
     public static MockDBService initialise() {
         if (mockDgGeneralDBService == null) {
             mockDgGeneralDBService = new MockDBService(serviceLogic);
@@ -46,4 +54,10 @@ public class MockDBService extends DBService {
         return mockDgGeneralDBService;
     }
 
+    public static MockDBService initialise(boolean failure) {
+        if(failure == true) {
+            mockDgGeneralDBServiceFailure = new MockDBService(serviceLogicFailure);
+        }
+        return mockDgGeneralDBServiceFailure;
+    }
 }
index 6513e6a..d516c43 100644 (file)
@@ -8,6 +8,8 @@
  * =============================================================================
  * Modifications Copyright (C) 2018 IBM
  * ================================================================================
+ * 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.
  * You may obtain a copy of the License at
@@ -27,8 +29,6 @@ package org.onap.appc.artifact.handler.dbservices;
 
 import java.util.Map;
 
-import com.att.eelf.configuration.EELFLogger;
-import com.att.eelf.configuration.EELFManager;
 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
 import org.onap.ccsdk.sli.adaptors.resource.sql.SqlResource;
diff --git a/appc-inbound/appc-artifact-handler/provider/src/test/java/org/onap/appc/artifact/handler/dbservices/MockSvcLogicResourceFailure.java b/appc-inbound/appc-artifact-handler/provider/src/test/java/org/onap/appc/artifact/handler/dbservices/MockSvcLogicResourceFailure.java
new file mode 100644 (file)
index 0000000..2215114
--- /dev/null
@@ -0,0 +1,51 @@
+/*-
+ * ============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.artifact.handler.dbservices;
+
+import java.util.Map;
+
+import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
+import org.onap.ccsdk.sli.core.sli.SvcLogicException;
+import org.onap.ccsdk.sli.adaptors.resource.sql.SqlResource;
+
+public class MockSvcLogicResourceFailure extends SqlResource {
+
+    @Override
+    public QueryStatus query(String resource, boolean localOnly, String select, String key, String prefix,
+            String orderBy, SvcLogicContext ctx) throws SvcLogicException {
+        QueryStatus status = QueryStatus.FAILURE;
+        ctx.setAttribute("keys",key);
+        ctx.setAttribute("id", "testId");
+        ctx.setAttribute("VNF_TYPE", "testvnf");
+        ctx.setAttribute("maximum", "1");
+        ctx.setAttribute("COUNT(*)", "1");
+        ctx.setAttribute("download-config-dg", "TestDG");
+        return status;
+    }
+
+    @Override
+    public QueryStatus save(String resource, boolean force, boolean localOnly, String key, Map<String, String> parms,
+            String prefix, SvcLogicContext ctx) throws SvcLogicException {
+        ctx.setAttribute("keys", key);
+        return QueryStatus.FAILURE;
+    }
+}