From 8ed27a86af9811816db99195f65fb2a50e4f37b2 Mon Sep 17 00:00:00 2001 From: Joss Armstrong Date: Wed, 27 Feb 2019 09:59:54 +0000 Subject: [PATCH] Test coverage in DBService Increase coverage from 71% to 98% Fix for Sonar major issue on branch coverage Issue-ID: APPC-1499 Change-Id: I74261af92b3263a03d29d476ad097182e6112ad9 Signed-off-by: Joss Armstrong --- .../controller/dbervices/FlowControlDBService.java | 17 +- .../dbervices/FlowControlDBServiceTest.java | 248 +++++++++++++++++++++ 2 files changed, 260 insertions(+), 5 deletions(-) create mode 100644 appc-config/appc-flow-controller/provider/src/test/java/org/onap/appc/flow/controller/dbervices/FlowControlDBServiceTest.java diff --git a/appc-config/appc-flow-controller/provider/src/main/java/org/onap/appc/flow/controller/dbervices/FlowControlDBService.java b/appc-config/appc-flow-controller/provider/src/main/java/org/onap/appc/flow/controller/dbervices/FlowControlDBService.java index 10c456426..946df7ce2 100644 --- a/appc-config/appc-flow-controller/provider/src/main/java/org/onap/appc/flow/controller/dbervices/FlowControlDBService.java +++ b/appc-config/appc-flow-controller/provider/src/main/java/org/onap/appc/flow/controller/dbervices/FlowControlDBService.java @@ -3,6 +3,8 @@ * ONAP : APPC * ================================================================================ * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. @@ -38,14 +40,14 @@ public class FlowControlDBService { private static final EELFLogger log = EELFManager.getInstance().getLogger(FlowControlDBService.class); private static final String QUERY_STR = "Query String : "; private static final String FAILURE_PARAM = "FAILURE"; - private static final String GET_FLOW_REF_DATA_ERROR = "Error - while getting FlowReferenceData "; + protected static final String GET_FLOW_REF_DATA_ERROR = "Error - while getting FlowReferenceData "; private static final String SELECT_AS_QUERY_STR = "select max(internal_version) as maxInternalVersion, artifact_name as artifactName from "; private static final String WHERE_ART_NAME_QUERY_STR = " where artifact_name in (select artifact_name from "; private static final String WHERE_VNF_TYPE_QUERY_STR = " where vnf_type= $"; private static final String SELECT_ART_CONTENT_QUERY_STR = "select artifact_content from "; private static final String WHERE_ARTIFACT_NAME_QUERY_STR = " where artifact_name = $artifactName and internal_version = $maxInternalVersion "; private static final String ARTIFACT_CONTENT_PARAM = "artifact-content"; - private static final String COUNT_PROTOCOL_PARAM = "count(protocol)"; + protected static final String COUNT_PROTOCOL_PARAM = "count(protocol)"; private static final String WHERE_ACTION_QUERY_STR = " where action = '"; private static final String AND_ACTION_LEVEL_QUERY_STR = " and action_level = '"; @@ -165,7 +167,7 @@ public class FlowControlDBService { public void populateModuleAndRPC(Transaction transaction, String vnfType) throws SvcLogicException { String fn = "FlowControlDBService.populateModuleAndRPC "; QueryStatus status; - SvcLogicContext context = new SvcLogicContext(); + SvcLogicContext context = getSvcLogicContext(); String protocolType = getProtocolType(transaction, vnfType, fn, context); String key = "select execution_type, execution_module, execution_rpc from " @@ -200,8 +202,9 @@ public class FlowControlDBService { throw new SvcLogicException(GET_FLOW_REF_DATA_ERROR); } - log.debug(" Protocol Count " + context.getAttribute(COUNT_PROTOCOL_PARAM)); - protocolCount = Integer.parseInt(context.getAttribute(COUNT_PROTOCOL_PARAM)); + String countProtocolParam = context.getAttribute(COUNT_PROTOCOL_PARAM); + log.debug(" Protocol Count " + countProtocolParam); + protocolCount = Integer.parseInt(countProtocolParam); if (protocolCount == 1) { protocolQuery = "select protocol from " + FlowControllerConstants.DB_PROTOCOL_REFERENCE @@ -316,4 +319,8 @@ public class FlowControlDBService { } return localContext != null ? localContext.getAttribute(ARTIFACT_CONTENT_PARAM) : null; } + + protected SvcLogicContext getSvcLogicContext() { + return new SvcLogicContext(); + } } diff --git a/appc-config/appc-flow-controller/provider/src/test/java/org/onap/appc/flow/controller/dbervices/FlowControlDBServiceTest.java b/appc-config/appc-flow-controller/provider/src/test/java/org/onap/appc/flow/controller/dbervices/FlowControlDBServiceTest.java new file mode 100644 index 000000000..25c8ee500 --- /dev/null +++ b/appc-config/appc-flow-controller/provider/src/test/java/org/onap/appc/flow/controller/dbervices/FlowControlDBServiceTest.java @@ -0,0 +1,248 @@ +/* + * ============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.flow.controller.dbervices; + +import static org.junit.Assert.assertNull; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.mockito.Mockito; +import org.onap.appc.flow.controller.data.Transaction; +import org.onap.appc.flow.controller.utils.FlowControllerConstants; +import org.onap.ccsdk.sli.adaptors.resource.sql.SqlResource; +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; + +public class FlowControlDBServiceTest { + + private SqlResource sqlResource = Mockito.mock(SqlResource.class); + private FlowControlDBService dbService; + + @Rule + public ExpectedException expectedEx = ExpectedException.none(); + + @Test + public void testGetFlowReferenceData() throws SvcLogicException { + dbService = new FlowControlDBService(sqlResource); + SvcLogicContext ctx = new SvcLogicContext(); + ctx.setAttribute(FlowControllerConstants.ACTION_LEVEL, "action_level"); + Mockito.when(sqlResource.query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(), + Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), + Mockito.any(SvcLogicContext.class))).thenReturn(QueryStatus.FAILURE); + expectedEx.expect(SvcLogicException.class); + expectedEx.expectMessage(FlowControlDBService.GET_FLOW_REF_DATA_ERROR); + dbService.getFlowReferenceData(ctx, null, new SvcLogicContext()); + } + + @Test + public void testGetEndpointByAction() { + dbService = new FlowControlDBService(sqlResource); + assertNull(dbService.getEndPointByAction(null)); + } + + @Test + public void testGetDesignTimeFlowModelFirstQueryException() throws SvcLogicException { + dbService = new FlowControlDBService(sqlResource); + SvcLogicContext ctx = new SvcLogicContext(); + ctx.setAttribute(FlowControllerConstants.ACTION_LEVEL, "action_level"); + Mockito.when(sqlResource.query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(), + Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), + Mockito.any(SvcLogicContext.class))).thenReturn(QueryStatus.FAILURE); + expectedEx.expect(SvcLogicException.class); + expectedEx.expectMessage(FlowControlDBService.GET_FLOW_REF_DATA_ERROR); + dbService.getDesignTimeFlowModel(ctx); + } + + @Test + public void testGetDesignTimeFlowModelSecondQueryException() throws SvcLogicException { + dbService = new FlowControlDBService(sqlResource); + SvcLogicContext ctx = new SvcLogicContext(); + ctx.setAttribute(FlowControllerConstants.ACTION_LEVEL, "action_level"); + Mockito.when(sqlResource.query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(), + Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), + Mockito.any(SvcLogicContext.class))).thenReturn(QueryStatus.SUCCESS).thenReturn(QueryStatus.FAILURE); + expectedEx.expect(SvcLogicException.class); + expectedEx.expectMessage(FlowControlDBService.GET_FLOW_REF_DATA_ERROR); + dbService.getDesignTimeFlowModel(ctx); + } + + @Test + public void testGetDesignTimeFlowModelNullLocalContext() throws SvcLogicException { + dbService = new FlowControlDBService(sqlResource); + assertNull(dbService.getDesignTimeFlowModel(null)); + } + + @Test + public void testLoadSequenceIntoDb() throws SvcLogicException { + dbService = new FlowControlDBService(sqlResource); + SvcLogicContext ctx = new SvcLogicContext(); + ctx.setAttribute(FlowControllerConstants.ACTION_LEVEL, "action_level"); + Mockito.when(sqlResource.save(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyBoolean(), Mockito.anyString(), + Mockito.any(), Mockito.anyString(), Mockito.any(SvcLogicContext.class))) + .thenReturn(QueryStatus.FAILURE); + expectedEx.expect(SvcLogicException.class); + expectedEx.expectMessage("Error While processing storing Artifact: "); + dbService.loadSequenceIntoDB(ctx); + } + + @Test + public void testGetProtocolTypeFirstException() throws SvcLogicException { + dbService = new FlowControlDBService(sqlResource); + SvcLogicContext ctx = new SvcLogicContext(); + ctx.setAttribute(FlowControllerConstants.ACTION_LEVEL, "action_level"); + Mockito.when(sqlResource.query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(), + Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), + Mockito.any(SvcLogicContext.class))).thenReturn(QueryStatus.FAILURE); + expectedEx.expect(SvcLogicException.class); + expectedEx.expectMessage(FlowControlDBService.GET_FLOW_REF_DATA_ERROR); + dbService.populateModuleAndRPC(new Transaction(), "vnf_type"); + } + + @Test + public void testGetProtocolTypeSecondException() throws SvcLogicException { + dbService = Mockito.spy(new FlowControlDBService(sqlResource)); + SvcLogicContext ctx = new SvcLogicContext(); + ctx.setAttribute(FlowControlDBService.COUNT_PROTOCOL_PARAM, "1"); + Mockito.when(sqlResource.query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(), + Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), + Mockito.any(SvcLogicContext.class))).thenReturn(QueryStatus.SUCCESS).thenReturn(QueryStatus.FAILURE); + Mockito.when(dbService.getSvcLogicContext()).thenReturn(ctx); + expectedEx.expect(SvcLogicException.class); + expectedEx.expectMessage(FlowControlDBService.GET_FLOW_REF_DATA_ERROR); + dbService.populateModuleAndRPC(new Transaction(), "vnf_type"); + } + + @Test + public void testHasSingleProtocolFirstException() throws SvcLogicException { + dbService = Mockito.spy(new FlowControlDBService(sqlResource)); + SvcLogicContext ctx = new SvcLogicContext(); + ctx.setAttribute(FlowControlDBService.COUNT_PROTOCOL_PARAM, "2"); + Mockito.when(sqlResource.query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(), + Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), + Mockito.any(SvcLogicContext.class))).thenReturn(QueryStatus.SUCCESS).thenReturn(QueryStatus.FAILURE); + Mockito.when(dbService.getSvcLogicContext()).thenReturn(ctx); + expectedEx.expect(SvcLogicException.class); + expectedEx.expectMessage(FlowControlDBService.GET_FLOW_REF_DATA_ERROR); + dbService.populateModuleAndRPC(new Transaction(), "vnf_type"); + Mockito.verify(dbService).getSvcLogicContext(); + } + + @Test + public void testHasSingleProtocolSecondException() throws SvcLogicException { + dbService = Mockito.spy(new FlowControlDBService(sqlResource)); + SvcLogicContext ctx = new SvcLogicContext(); + ctx.setAttribute(FlowControlDBService.COUNT_PROTOCOL_PARAM, "2"); + Mockito.when(sqlResource.query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(), + Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), + Mockito.any(SvcLogicContext.class))).thenReturn(QueryStatus.SUCCESS).thenReturn(QueryStatus.SUCCESS) + .thenReturn(QueryStatus.FAILURE); + Mockito.when(dbService.getSvcLogicContext()).thenReturn(ctx); + expectedEx.expect(SvcLogicException.class); + expectedEx.expectMessage("Got more than 2 values.."); + dbService.populateModuleAndRPC(new Transaction(), "vnf_type"); + Mockito.verify(dbService).getSvcLogicContext(); + } + + @Test + public void testHasSingleProtocolThirdException() throws SvcLogicException { + dbService = Mockito.spy(new FlowControlDBService(sqlResource)); + SvcLogicContext ctx = Mockito.spy(new SvcLogicContext()); + Mockito.when(ctx.getAttribute(FlowControlDBService.COUNT_PROTOCOL_PARAM)).thenReturn("2").thenReturn("1"); + Mockito.when(sqlResource.query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(), + Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), + Mockito.any(SvcLogicContext.class))).thenReturn(QueryStatus.SUCCESS).thenReturn(QueryStatus.SUCCESS) + .thenReturn(QueryStatus.FAILURE); + Mockito.when(dbService.getSvcLogicContext()).thenReturn(ctx); + expectedEx.expect(SvcLogicException.class); + expectedEx.expectMessage(FlowControlDBService.GET_FLOW_REF_DATA_ERROR); + dbService.populateModuleAndRPC(new Transaction(), "vnf_type"); + Mockito.verify(dbService).getSvcLogicContext(); + } + + @Test + public void testHasSingleProtocolSuccessFlow() throws SvcLogicException { + dbService = Mockito.spy(new FlowControlDBService(sqlResource)); + SvcLogicContext ctx = Mockito.spy(new SvcLogicContext()); + Mockito.when(ctx.getAttribute(FlowControlDBService.COUNT_PROTOCOL_PARAM)).thenReturn("2").thenReturn("1"); + Mockito.when(sqlResource.query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(), + Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), + Mockito.any(SvcLogicContext.class))).thenReturn(QueryStatus.SUCCESS).thenReturn(QueryStatus.SUCCESS) + .thenReturn(QueryStatus.SUCCESS); + Mockito.when(dbService.getSvcLogicContext()).thenReturn(ctx); + Transaction transaction = Mockito.spy(new Transaction()); + dbService.populateModuleAndRPC(transaction, "vnf_type"); + Mockito.verify(transaction).setExecutionRPC(null); + } + + @Test + public void testGetDependencyInfoFirstException() throws SvcLogicException { + dbService = new FlowControlDBService(sqlResource); + SvcLogicContext ctx = new SvcLogicContext(); + ctx.setAttribute(FlowControllerConstants.ACTION_LEVEL, "action_level"); + Mockito.when(sqlResource.query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(), + Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), + Mockito.any(SvcLogicContext.class))).thenReturn(QueryStatus.FAILURE); + expectedEx.expect(SvcLogicException.class); + expectedEx.expectMessage("Error - while getting dependencydata "); + dbService.getDependencyInfo(ctx); + } + + @Test + public void testGetDependencyInfoSecondException() throws SvcLogicException { + dbService = new FlowControlDBService(sqlResource); + SvcLogicContext ctx = new SvcLogicContext(); + ctx.setAttribute(FlowControllerConstants.ACTION_LEVEL, "action_level"); + Mockito.when(sqlResource.query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(), + Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), + Mockito.any(SvcLogicContext.class))).thenReturn(QueryStatus.SUCCESS).thenReturn(QueryStatus.FAILURE); + expectedEx.expect(SvcLogicException.class); + expectedEx.expectMessage("Error - while getting dependencyData "); + dbService.getDependencyInfo(ctx); + } + + @Test + public void testGetCapabilitiesDataFirstException() throws SvcLogicException { + dbService = new FlowControlDBService(sqlResource); + SvcLogicContext ctx = new SvcLogicContext(); + ctx.setAttribute(FlowControllerConstants.ACTION_LEVEL, "action_level"); + Mockito.when(sqlResource.query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(), + Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), + Mockito.any(SvcLogicContext.class))).thenReturn(QueryStatus.FAILURE); + expectedEx.expect(SvcLogicException.class); + expectedEx.expectMessage("Error - while getting capabilitiesData "); + dbService.getCapabilitiesData(ctx); + } + + @Test + public void testGetCapabilitiesDataSecondException() throws SvcLogicException { + dbService = new FlowControlDBService(sqlResource); + SvcLogicContext ctx = new SvcLogicContext(); + ctx.setAttribute(FlowControllerConstants.ACTION_LEVEL, "action_level"); + Mockito.when(sqlResource.query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(), + Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), + Mockito.any(SvcLogicContext.class))).thenReturn(QueryStatus.SUCCESS).thenReturn(QueryStatus.FAILURE); + expectedEx.expect(SvcLogicException.class); + expectedEx.expectMessage("Error - while getting capabilitiesData "); + dbService.getCapabilitiesData(ctx); + } +} -- 2.16.6