From d0099f039b0dbf8f14ec2d273290a7c8ff58cb1c Mon Sep 17 00:00:00 2001 From: Temoc Rodriguez Date: Mon, 4 Dec 2017 11:39:54 -0800 Subject: Fix bug where paramsAreBad doesn't update string Replaced the string parameter with a StringBuilder so that the modified version can be accessed outside the method, since strings are immutable. Added null check for properties. Removed trim on properties in case the property is null. Added junit to test that the modified StringBuilder can be read outside the paramsAreBad method. Issue-ID: POLICY-492 Change-Id: I0550e9d639cbbcc876e6aafb84f6e9a363b653ff Signed-off-by: Temoc Rodriguez --- .../main/java/org/onap/policy/common/ia/DbDAO.java | 2 +- .../org/onap/policy/common/ia/IntegrityAudit.java | 109 +++++++++++---------- .../policy/common/ia/test/IntegrityAuditTest.java | 65 ++++++++++++ 3 files changed, 124 insertions(+), 52 deletions(-) create mode 100644 integrity-audit/src/test/java/org/onap/policy/common/ia/test/IntegrityAuditTest.java (limited to 'integrity-audit') diff --git a/integrity-audit/src/main/java/org/onap/policy/common/ia/DbDAO.java b/integrity-audit/src/main/java/org/onap/policy/common/ia/DbDAO.java index db633400..99503854 100644 --- a/integrity-audit/src/main/java/org/onap/policy/common/ia/DbDAO.java +++ b/integrity-audit/src/main/java/org/onap/policy/common/ia/DbDAO.java @@ -95,7 +95,7 @@ public class DbDAO { * @throws IntegrityAuditPropertiesException */ private void validateProperties(String resourceName, String persistenceUnit, Properties properties) throws IntegrityAuditPropertiesException{ - String badparams=""; + StringBuilder badparams= new StringBuilder(); if(IntegrityAudit.parmsAreBad(resourceName, persistenceUnit, properties, badparams)){ String msg = "DbDAO: Bad parameters: badparams" + badparams; throw new IntegrityAuditPropertiesException(msg); diff --git a/integrity-audit/src/main/java/org/onap/policy/common/ia/IntegrityAudit.java b/integrity-audit/src/main/java/org/onap/policy/common/ia/IntegrityAudit.java index 50fc6932..424e603d 100644 --- a/integrity-audit/src/main/java/org/onap/policy/common/ia/IntegrityAudit.java +++ b/integrity-audit/src/main/java/org/onap/policy/common/ia/IntegrityAudit.java @@ -67,7 +67,7 @@ public class IntegrityAudit { public IntegrityAudit(String resourceName, String persistenceUnit, Properties properties) throws IntegrityAuditException { logger.info("Constructor: Entering and checking for nulls"); - String parmList = ""; + StringBuilder parmList = new StringBuilder(); if (parmsAreBad(resourceName, persistenceUnit, properties, parmList)) { logger.error("Constructor: Parms contain nulls; cannot run audit for resourceName=" + resourceName + ", persistenceUnit=" + persistenceUnit @@ -117,73 +117,80 @@ public class IntegrityAudit { * Makes sure we don't try to run the audit with bad parameters. */ public static boolean parmsAreBad(String resourceName, String persistenceUnit, - Properties properties, String badparams) { + Properties properties, StringBuilder badparams) { boolean parmsAreBad = false; if(resourceName == null || resourceName.isEmpty()){ - badparams = badparams.concat("resourceName "); + badparams = badparams.append("resourceName "); parmsAreBad = true; } if(persistenceUnit == null || persistenceUnit.isEmpty()){ - badparams = badparams.concat("persistenceUnit "); + badparams = badparams.append("persistenceUnit "); parmsAreBad = true; } - String dbDriver = properties.getProperty(IntegrityAuditProperties.DB_DRIVER).trim(); - if(dbDriver == null || dbDriver.isEmpty()){ - badparams = badparams.concat("dbDriver "); + if(properties == null || properties.isEmpty()){ + badparams = badparams.append("properties "); parmsAreBad = true; } - - String dbUrl = properties.getProperty(IntegrityAuditProperties.DB_URL).trim(); - if(dbUrl == null || dbUrl.isEmpty()){ - badparams = badparams.concat("dbUrl "); - parmsAreBad = true; - } - - String dbUser = properties.getProperty(IntegrityAuditProperties.DB_USER).trim(); - if(dbUser == null || dbUser.isEmpty()){ - badparams = badparams.concat("dbUser "); - parmsAreBad = true; - } - - String dbPwd = properties.getProperty(IntegrityAuditProperties.DB_PWD).trim(); - if(dbPwd == null){ //may be empty - badparams = badparams.concat("dbPwd "); - parmsAreBad = true; - } - - String siteName = properties.getProperty(IntegrityAuditProperties.SITE_NAME).trim(); - if(siteName == null || siteName.isEmpty()){ - badparams = badparams.concat("siteName "); - parmsAreBad = true; - } - - String nodeType = properties.getProperty(IntegrityAuditProperties.NODE_TYPE).trim(); - if(nodeType == null || nodeType.isEmpty()){ - badparams = badparams.concat("nodeType "); - parmsAreBad = true; - } else { - if (!isNodeTypeEnum(nodeType)) { - String nodetypes = "nodeType must be one of["; - for (NodeTypeEnum n : NodeTypeEnum.values()) { - nodetypes = nodetypes.concat(n.toString() + " "); - } - badparams = badparams.concat(nodetypes + "] "); + else{ + String dbDriver = properties.getProperty(IntegrityAuditProperties.DB_DRIVER); + if(dbDriver == null || dbDriver.isEmpty()){ + badparams = badparams.append("dbDriver "); parmsAreBad = true; } - } - if(properties.getProperty(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS) != null){ //It is allowed to be null - try{ - Integer.parseInt(properties.getProperty(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS).trim()); - }catch(NumberFormatException nfe){ - badparams = badparams.concat(", auditPeriodSeconds=" - + properties.getProperty(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS).trim()); + + String dbUrl = properties.getProperty(IntegrityAuditProperties.DB_URL); + if(dbUrl == null || dbUrl.isEmpty()){ + badparams = badparams.append("dbUrl "); parmsAreBad = true; } - } + + String dbUser = properties.getProperty(IntegrityAuditProperties.DB_USER); + if(dbUser == null || dbUser.isEmpty()){ + badparams = badparams.append("dbUser "); + parmsAreBad = true; + } + + String dbPwd = properties.getProperty(IntegrityAuditProperties.DB_PWD); + if(dbPwd == null){ //may be empty + badparams = badparams.append("dbPwd "); + parmsAreBad = true; + } + + String siteName = properties.getProperty(IntegrityAuditProperties.SITE_NAME); + if(siteName == null || siteName.isEmpty()){ + badparams = badparams.append("siteName "); + parmsAreBad = true; + } + + String nodeType = properties.getProperty(IntegrityAuditProperties.NODE_TYPE); + if(nodeType == null || nodeType.isEmpty()){ + badparams = badparams.append("nodeType "); + parmsAreBad = true; + } else { + nodeType = nodeType.trim(); + if (!isNodeTypeEnum(nodeType)) { + String nodetypes = "nodeType must be one of["; + for (NodeTypeEnum n : NodeTypeEnum.values()) { + nodetypes = nodetypes.concat(n.toString() + " "); + } + badparams = badparams.append(nodetypes + "] "); + parmsAreBad = true; + } + } + if(properties.getProperty(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS) != null){ //It is allowed to be null + try{ + Integer.parseInt(properties.getProperty(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS).trim()); + }catch(NumberFormatException nfe){ + badparams = badparams.append(", auditPeriodSeconds=" + + properties.getProperty(IntegrityAuditProperties.AUDIT_PERIOD_SECONDS).trim()); + parmsAreBad = true; + } + } + } // End else logger.debug("parmsAreBad: exit:" + "\nresourceName: " + resourceName + "\npersistenceUnit: " + persistenceUnit diff --git a/integrity-audit/src/test/java/org/onap/policy/common/ia/test/IntegrityAuditTest.java b/integrity-audit/src/test/java/org/onap/policy/common/ia/test/IntegrityAuditTest.java new file mode 100644 index 00000000..5f19e2b9 --- /dev/null +++ b/integrity-audit/src/test/java/org/onap/policy/common/ia/test/IntegrityAuditTest.java @@ -0,0 +1,65 @@ +/*- + * ============LICENSE_START======================================================= + * Integrity Audit + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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.policy.common.ia.test; + +import static org.junit.Assert.*; + +import java.util.Properties; + +import org.junit.Test; +import org.onap.policy.common.ia.IntegrityAudit; +import org.onap.policy.common.ia.IntegrityAuditProperties; + +public class IntegrityAuditTest { + + @Test + /** + * Test if we can access the updated bad params outside of the parmsAreBad method + */ + public void parmsAreBadTest() { + // Try with 2 null params + StringBuilder badParams = new StringBuilder(); + IntegrityAudit.parmsAreBad(null, "something", null, badParams); + + assertFalse("".equals(badParams.toString())); + assertTrue(badParams.toString().contains("resourceName")); + assertTrue(badParams.toString().contains("properties")); + + // Try with 1 null params + badParams = new StringBuilder(); + Properties props = new Properties(); + props.put(IntegrityAuditProperties.DB_DRIVER, "test_db_driver"); + IntegrityAudit.parmsAreBad(null, "something", props, badParams); + + assertFalse("".equals(badParams.toString())); + assertTrue(badParams.toString().contains("resourceName")); + assertFalse(badParams.toString().contains("properties")); + + // Try with 0 null params + badParams = new StringBuilder(); + IntegrityAudit.parmsAreBad("someting", "something", props, badParams); + assertFalse("".equals(badParams.toString())); + assertFalse(badParams.toString().contains("resourceName")); + assertFalse(badParams.toString().contains("properties")); + + } + +} -- cgit 1.2.3-korg