From 63b928252c2d5ef905f5e074fa8bbcfbf460d780 Mon Sep 17 00:00:00 2001 From: Joss Armstrong Date: Mon, 21 Jan 2019 13:02:33 +0000 Subject: [PATCH] Correct unit tests for DBService class Increased test coverage from 74% to 92% in DBService class Issue-ID: APPC-1335 Change-Id: Iabd792eb104429d5090e0099dba463e8ad6d499a Signed-off-by: Joss Armstrong --- .../artifact/handler/dbservices/DBService.java | 6 +- .../artifact/handler/dbservices/DBServiceTest.java | 255 ++++++++++++--------- .../artifact/handler/dbservices/MockDBService.java | 18 +- .../handler/dbservices/MockSvcLogicResource.java | 4 +- .../dbservices/MockSvcLogicResourceFailure.java | 51 +++++ 5 files changed, 221 insertions(+), 113 deletions(-) create mode 100644 appc-inbound/appc-artifact-handler/provider/src/test/java/org/onap/appc/artifact/handler/dbservices/MockSvcLogicResourceFailure.java diff --git a/appc-inbound/appc-artifact-handler/provider/src/main/java/org/onap/appc/artifact/handler/dbservices/DBService.java b/appc-inbound/appc-artifact-handler/provider/src/main/java/org/onap/appc/artifact/handler/dbservices/DBService.java index 24dd4f477..cf45bc32e 100644 --- a/appc-inbound/appc-artifact-handler/provider/src/main/java/org/onap/appc/artifact/handler/dbservices/DBService.java +++ b/appc-inbound/appc-artifact-handler/provider/src/main/java/org/onap/appc/artifact/handler/dbservices/DBService.java @@ -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"); } } diff --git a/appc-inbound/appc-artifact-handler/provider/src/test/java/org/onap/appc/artifact/handler/dbservices/DBServiceTest.java b/appc-inbound/appc-artifact-handler/provider/src/test/java/org/onap/appc/artifact/handler/dbservices/DBServiceTest.java index aac7cbe00..0676d59fd 100644 --- a/appc-inbound/appc-artifact-handler/provider/src/test/java/org/onap/appc/artifact/handler/dbservices/DBServiceTest.java +++ b/appc-inbound/appc-artifact-handler/provider/src/test/java/org/onap/appc/artifact/handler/dbservices/DBServiceTest.java @@ -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 @@ -25,25 +27,23 @@ 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)); } } diff --git a/appc-inbound/appc-artifact-handler/provider/src/test/java/org/onap/appc/artifact/handler/dbservices/MockDBService.java b/appc-inbound/appc-artifact-handler/provider/src/test/java/org/onap/appc/artifact/handler/dbservices/MockDBService.java index 57115d821..374f6b16e 100644 --- a/appc-inbound/appc-artifact-handler/provider/src/test/java/org/onap/appc/artifact/handler/dbservices/MockDBService.java +++ b/appc-inbound/appc-artifact-handler/provider/src/test/java/org/onap/appc/artifact/handler/dbservices/MockDBService.java @@ -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; + } } diff --git a/appc-inbound/appc-artifact-handler/provider/src/test/java/org/onap/appc/artifact/handler/dbservices/MockSvcLogicResource.java b/appc-inbound/appc-artifact-handler/provider/src/test/java/org/onap/appc/artifact/handler/dbservices/MockSvcLogicResource.java index 6513e6ab1..d516c4359 100644 --- a/appc-inbound/appc-artifact-handler/provider/src/test/java/org/onap/appc/artifact/handler/dbservices/MockSvcLogicResource.java +++ b/appc-inbound/appc-artifact-handler/provider/src/test/java/org/onap/appc/artifact/handler/dbservices/MockSvcLogicResource.java @@ -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 index 000000000..221511416 --- /dev/null +++ b/appc-inbound/appc-artifact-handler/provider/src/test/java/org/onap/appc/artifact/handler/dbservices/MockSvcLogicResourceFailure.java @@ -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 parms, + String prefix, SvcLogicContext ctx) throws SvcLogicException { + ctx.setAttribute("keys", key); + return QueryStatus.FAILURE; + } +} -- 2.16.6