From d0932a1a2339a02dab04eedefa0480535e68d31c Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Thu, 27 Jun 2019 10:52:06 -0400 Subject: Fix some sonar issues in drools-applications Added coverage to: - feature-controlloop-management Fixed sonar issues, but didn't add coverage to: - feature-controlloop-trans - eventmanager - guard Change-Id: I12f09c4a533e838c6fb9762ba56194e51ce864eb Issue-ID: POLICY-1791 Signed-off-by: Jim Hahn --- .../java/org/onap/policy/guard/GuardUtilTest.java | 12 +-- .../onap/policy/guard/PolicyGuardRequestTest.java | 18 ++-- .../onap/policy/guard/PolicyGuardResponseTest.java | 13 ++- .../org/onap/policy/guard/PolicyGuardTest.java | 30 +++--- .../policy/guard/PolicyGuardXacmlHelperTest.java | 37 +++---- .../policy/guard/PolicyGuardYamlToXacmlTest.java | 110 ++++++++++++--------- .../onap/policy/guard/SupportTextFileUtils.java | 26 ++--- 7 files changed, 132 insertions(+), 114 deletions(-) (limited to 'controlloop/common/guard/src/test') diff --git a/controlloop/common/guard/src/test/java/org/onap/policy/guard/GuardUtilTest.java b/controlloop/common/guard/src/test/java/org/onap/policy/guard/GuardUtilTest.java index 0738ad180..e17207634 100644 --- a/controlloop/common/guard/src/test/java/org/onap/policy/guard/GuardUtilTest.java +++ b/controlloop/common/guard/src/test/java/org/onap/policy/guard/GuardUtilTest.java @@ -38,6 +38,7 @@ public class GuardUtilTest { @Test public void testLoadYamlOk() throws IOException { File tempYamlFile = File.createTempFile("ONAPPF", "yaml"); + tempYamlFile.deleteOnExit(); ControlLoopPolicy clPolicy = new ControlLoopPolicy(); @@ -50,23 +51,21 @@ public class GuardUtilTest { assertEquals(clPolicy, result.parameterA); assertEquals(clYamlString, result.parameterB); - - tempYamlFile.delete(); } @Test public void testLoadYamlError() throws IOException { File tempDir = Files.createTempDir(); + tempDir.deleteOnExit(); // Read from a directory forces an IO exception assertNull(Util.loadYaml(tempDir.getCanonicalPath())); - - tempDir.delete(); } @Test public void testLoadGuardYamlOk() throws IOException { File tempYamlFile = File.createTempFile("ONAPPF", "yaml"); + tempYamlFile.deleteOnExit(); ControlLoopGuard clGuardPolicy = new ControlLoopGuard(); @@ -78,18 +77,15 @@ public class GuardUtilTest { ControlLoopGuard result = Util.loadYamlGuard(tempYamlFile.getCanonicalPath()); assertEquals(clGuardPolicy, result); - - tempYamlFile.delete(); } @Test public void testLoadGuardYamlError() throws IOException { File tempDir = Files.createTempDir(); + tempDir.deleteOnExit(); // Read from a directory forces an IO exception assertNull(Util.loadYamlGuard(tempDir.getCanonicalPath())); - - tempDir.delete(); } @Test diff --git a/controlloop/common/guard/src/test/java/org/onap/policy/guard/PolicyGuardRequestTest.java b/controlloop/common/guard/src/test/java/org/onap/policy/guard/PolicyGuardRequestTest.java index 3b0441fc2..2be0e06ec 100644 --- a/controlloop/common/guard/src/test/java/org/onap/policy/guard/PolicyGuardRequestTest.java +++ b/controlloop/common/guard/src/test/java/org/onap/policy/guard/PolicyGuardRequestTest.java @@ -31,25 +31,29 @@ import org.junit.Test; public class PolicyGuardRequestTest { + private static final String KANSAS = "Kansas"; + private static final String GET_BACK_HOME = "GetBackHome"; + private static final String DOROTHY = "Dorothy"; + @Test public void policyGuardRequestTest() { UUID requestId = UUID.randomUUID(); assertNotNull(new PolicyGuardRequest(null, null, null, null)); - PolicyGuardRequest request = new PolicyGuardRequest("Dorothy", "Kansas", requestId, "GetBackHome"); + PolicyGuardRequest request = new PolicyGuardRequest(DOROTHY, KANSAS, requestId, GET_BACK_HOME); request.setRequestId(requestId); assertEquals(requestId, request.getRequestId()); - request.setActor("Dorothy"); - assertEquals("Dorothy", request.getActor()); + request.setActor(DOROTHY); + assertEquals(DOROTHY, request.getActor()); - request.setTarget("Kansas"); - assertEquals("Kansas", request.getTarget()); + request.setTarget(KANSAS); + assertEquals(KANSAS, request.getTarget()); - request.setOperation("GetBackHome"); - assertEquals("GetBackHome", request.getOperation()); + request.setOperation(GET_BACK_HOME); + assertEquals(GET_BACK_HOME, request.getOperation()); assertEquals("PolicyGuardRequest [actor=Dorothy", request.toString().substring(0, 33)); } diff --git a/controlloop/common/guard/src/test/java/org/onap/policy/guard/PolicyGuardResponseTest.java b/controlloop/common/guard/src/test/java/org/onap/policy/guard/PolicyGuardResponseTest.java index b30ae0210..14e7a3ad0 100644 --- a/controlloop/common/guard/src/test/java/org/onap/policy/guard/PolicyGuardResponseTest.java +++ b/controlloop/common/guard/src/test/java/org/onap/policy/guard/PolicyGuardResponseTest.java @@ -31,22 +31,25 @@ import org.junit.Test; public class PolicyGuardResponseTest { + private static final String GET_BACK_HOME = "GetBackHome"; + private static final String BACK_HOME = "BackHome"; + @Test public void policyGuardResponseTest() { UUID requestId = UUID.randomUUID(); assertNotNull(new PolicyGuardResponse(null, null, null)); - PolicyGuardResponse response = new PolicyGuardResponse("BackHome", requestId, "GetBackHome"); + PolicyGuardResponse response = new PolicyGuardResponse(BACK_HOME, requestId, GET_BACK_HOME); response.setRequestId(requestId); assertEquals(requestId, response.getRequestId()); - response.setResult("BackHome"); - assertEquals("BackHome", response.getResult()); + response.setResult(BACK_HOME); + assertEquals(BACK_HOME, response.getResult()); - response.setOperation("GetBackHome"); - assertEquals("GetBackHome", response.getOperation()); + response.setOperation(GET_BACK_HOME); + assertEquals(GET_BACK_HOME, response.getOperation()); assertEquals("PolicyGuardResponse [requestId=", response.toString().substring(0, 31)); } diff --git a/controlloop/common/guard/src/test/java/org/onap/policy/guard/PolicyGuardTest.java b/controlloop/common/guard/src/test/java/org/onap/policy/guard/PolicyGuardTest.java index 87303623c..5f77d0331 100644 --- a/controlloop/common/guard/src/test/java/org/onap/policy/guard/PolicyGuardTest.java +++ b/controlloop/common/guard/src/test/java/org/onap/policy/guard/PolicyGuardTest.java @@ -46,9 +46,9 @@ import org.onap.policy.guard.impl.VnfTargetLock; public class PolicyGuardTest { private static final String INSTANCENAME = "targetInstance"; private static final int LOCK_SEC = 10; - + private static Factory saveFactory; - + private Factory factory; private PolicyResourceLockManager mgr; private UUID uuid; @@ -69,12 +69,12 @@ public class PolicyGuardTest { private class DummyTargetLock implements TargetLock { private TargetType type; private UUID reqid; - + public DummyTargetLock(TargetType type, UUID reqid) { this.type = type; this.reqid = reqid; } - + @Override public UUID getLockId() { return null; @@ -95,17 +95,17 @@ public class PolicyGuardTest { return reqid; } } - + @BeforeClass public static void setUpBeforeClass() { saveFactory = PolicyGuard.getFactory(); } - + @AfterClass public static void tearDownAfterClass() { PolicyGuard.setFactory(saveFactory); } - + /** * Setup method. */ @@ -117,10 +117,10 @@ public class PolicyGuardTest { * manager is protected; this gets around that */ }); - + factory = mock(Factory.class); when(factory.getManager()).thenReturn(mgr); - + uuid = UUID.randomUUID(); dlcb = new DummyLockCallback(); @@ -279,7 +279,7 @@ public class PolicyGuardTest { } @Test - public void testLockTargetTargetTypeStringUuidLockCallbackInt() throws Exception { + public void testLockTargetTargetTypeStringUuidLockCallbackInt() { TargetType type = TargetType.VM; LockResult result; @@ -299,7 +299,7 @@ public class PolicyGuardTest { } @Test - public void testLockTargetTargetLockInt() throws Exception { + public void testLockTargetTargetLockInt() { TargetType type = TargetType.VM; LockResult result; @@ -309,16 +309,16 @@ public class PolicyGuardTest { verify(mgr).lock(INSTANCENAME, type + ":" + uuid, LOCK_SEC); assertEquals(GuardResult.LOCK_ACQUIRED, result.getA()); assertEquals(VmTargetLock.class, result.getB().getClass()); - + TargetLock lock = result.getB(); - + // refresh - re-acquired assertEquals(GuardResult.LOCK_ACQUIRED, PolicyGuard.lockTarget(lock, LOCK_SEC + 1)); verify(mgr).refresh(INSTANCENAME, type + ":" + uuid, LOCK_SEC + 1); - + // unlock PolicyGuard.unlockTarget(lock); - + // refresh - denied, as we no longer own the lock assertEquals(GuardResult.LOCK_DENIED, PolicyGuard.lockTarget(lock, LOCK_SEC + 2)); } diff --git a/controlloop/common/guard/src/test/java/org/onap/policy/guard/PolicyGuardXacmlHelperTest.java b/controlloop/common/guard/src/test/java/org/onap/policy/guard/PolicyGuardXacmlHelperTest.java index 2418ac663..19fed30a0 100644 --- a/controlloop/common/guard/src/test/java/org/onap/policy/guard/PolicyGuardXacmlHelperTest.java +++ b/controlloop/common/guard/src/test/java/org/onap/policy/guard/PolicyGuardXacmlHelperTest.java @@ -22,10 +22,8 @@ package org.onap.policy.guard; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.fail; import java.util.Properties; - import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; @@ -35,21 +33,24 @@ import org.onap.policy.drools.utils.logging.LoggerUtil; public class PolicyGuardXacmlHelperTest { + private static final String TARGET = "target"; + private static final String REQUEST_ID = "requestId"; + private static final String RECIPE = "recipe"; + private static final String GUARD_URL = "guard.url"; + private static final String ACTOR = "actor"; private static final Integer VF_COUNT = 100; /** * Set up test class. */ @BeforeClass - public static void setupSimulator() { + public static void setupSimulator() throws Exception { LoggerUtil.setLevel("ROOT", "INFO"); LoggerUtil.setLevel("org.eclipse.jetty", "WARN"); - try { - HttpServletServer.factory.destroy(); - org.onap.policy.simulators.Util.buildGuardSim(); - } catch (Exception e) { - fail(e.getMessage()); - } + + HttpServletServer.factory.destroy(); + org.onap.policy.simulators.Util.buildGuardSim(); + // // Set guard properties // @@ -65,8 +66,8 @@ public class PolicyGuardXacmlHelperTest { // Null/ Bad Connection Case PolicyGuardXacmlRequestAttributes xacmlReq = new PolicyGuardXacmlRequestAttributes( - org.onap.policy.simulators.GuardSimulatorJaxRs.DENY_CLNAME, "actor", "recipe", "target", - "requestId", VF_COUNT); + org.onap.policy.simulators.GuardSimulatorJaxRs.DENY_CLNAME, ACTOR, RECIPE, TARGET, + REQUEST_ID, VF_COUNT); String rawDecision = new PolicyGuardXacmlHelper().callPdp(xacmlReq); assertNotNull(rawDecision); assertEquals(Util.DENY, rawDecision); @@ -88,14 +89,14 @@ public class PolicyGuardXacmlHelperTest { public void testCallPdp() { // Deny Case PolicyGuardXacmlRequestAttributes xacmlReq = new PolicyGuardXacmlRequestAttributes( - org.onap.policy.simulators.GuardSimulatorJaxRs.DENY_CLNAME, "actor", "recipe", "target", - "requestId", VF_COUNT); + org.onap.policy.simulators.GuardSimulatorJaxRs.DENY_CLNAME, ACTOR, RECIPE, TARGET, + REQUEST_ID, VF_COUNT); String rawDecision = new PolicyGuardXacmlHelper().callPdp(xacmlReq); assertNotNull(rawDecision); assertEquals(Util.DENY, rawDecision); // Permit Case - xacmlReq = new PolicyGuardXacmlRequestAttributes("clname", "actor", "recipe", "target", "requestId", VF_COUNT); + xacmlReq = new PolicyGuardXacmlRequestAttributes("clname", ACTOR, RECIPE, TARGET, REQUEST_ID, VF_COUNT); rawDecision = new PolicyGuardXacmlHelper().callPdp(xacmlReq); assertNotNull(rawDecision); assertEquals(Util.PERMIT, rawDecision); @@ -109,15 +110,15 @@ public class PolicyGuardXacmlHelperTest { assertNotNull(new PolicyGuardXacmlHelper()); - PolicyEngine.manager.getEnvironment().setProperty("guard.url", + PolicyEngine.manager.getEnvironment().setProperty(GUARD_URL, "http://localhost:6669/pdp/api/getDecision,Dorothy"); assertNotNull(new PolicyGuardXacmlHelper()); - PolicyEngine.manager.getEnvironment().setProperty("guard.url", + PolicyEngine.manager.getEnvironment().setProperty(GUARD_URL, "http://localhost:6669/pdp/api/getDecision,Dorothy,Toto"); assertNotNull(new PolicyGuardXacmlHelper()); - PolicyEngine.manager.getEnvironment().setProperty("guard.url", + PolicyEngine.manager.getEnvironment().setProperty(GUARD_URL, "http://localhost:6969/policy/pdpx/v1/decision"); PolicyEngine.manager.getEnvironment().setProperty("pdpx.timeout", "thisIsNotANumber"); @@ -132,7 +133,7 @@ public class PolicyGuardXacmlHelperTest { PolicyEngine.manager.getEnvironment().setProperty("pdpx.username", "python"); assertNotNull(new PolicyGuardXacmlHelper()); - PolicyEngine.manager.getEnvironment().setProperty("guard.url", "///"); + PolicyEngine.manager.getEnvironment().setProperty(GUARD_URL, "///"); assertNotNull(new PolicyGuardXacmlHelper()); PolicyEngine.manager.getEnvironment().setProperty("guard.disabled", ""); diff --git a/controlloop/common/guard/src/test/java/org/onap/policy/guard/PolicyGuardYamlToXacmlTest.java b/controlloop/common/guard/src/test/java/org/onap/policy/guard/PolicyGuardYamlToXacmlTest.java index a35696dec..75dc73c27 100644 --- a/controlloop/common/guard/src/test/java/org/onap/policy/guard/PolicyGuardYamlToXacmlTest.java +++ b/controlloop/common/guard/src/test/java/org/onap/policy/guard/PolicyGuardYamlToXacmlTest.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * guard * ================================================================================ - * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2019 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. @@ -41,6 +41,16 @@ import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.constructor.Constructor; public class PolicyGuardYamlToXacmlTest { + private static final String SOME_START_TIME = "someStartTime"; + private static final String SOME_END_TIME = "someEndTime"; + private static final String HOURS = "hours"; + private static final String TARGET2 = "WickedWitchOfTheWest"; + private static final String TARGET1 = "Wizard"; + private static final String ONAPPF_FILE = "ONAPPF"; + private static final String RECIPE = "GoToOz"; + private static final String TEXT1 = "WestWitches"; + private static final String TEXT2 = "EastWitches"; + private static final String OUT_XACML = ".out.xacml"; private ControlLoopGuard clGuard; /** @@ -52,10 +62,10 @@ public class PolicyGuardYamlToXacmlTest { MatchParameters matchParameters = new MatchParameters(); matchParameters.setControlLoopName("WizardOfOz"); matchParameters.setActor("Dorothy"); - matchParameters.setRecipe("GoToOz"); + matchParameters.setRecipe(RECIPE); List targets = new ArrayList<>(); - targets.add("Wizard"); - targets.add("WickedWitchOfTheWest"); + targets.add(TARGET1); + targets.add(TARGET2); matchParameters.setTargets(targets); GuardPolicy guardPolicy = new GuardPolicy(); guardPolicy.setMatch_parameters(matchParameters); @@ -63,11 +73,11 @@ public class PolicyGuardYamlToXacmlTest { limitConstraint.setFreq_limit_per_target(5); Map timeWindow = new HashMap<>(); timeWindow.put("value", "10"); - timeWindow.put("units", "hours"); + timeWindow.put("units", HOURS); limitConstraint.setTime_window(timeWindow); Map activeTimeRange = new HashMap<>(); - activeTimeRange.put("start", "someStartTime"); - activeTimeRange.put("end", "someEndTime"); + activeTimeRange.put("start", SOME_START_TIME); + activeTimeRange.put("end", SOME_END_TIME); limitConstraint.setActive_time_range(activeTimeRange); LinkedList limitConstraints = new LinkedList<>(); limitConstraints.add(limitConstraint); @@ -79,9 +89,13 @@ public class PolicyGuardYamlToXacmlTest { @Test public void testGenerateXacmlGuardFull() throws IOException { - File tempYamlFile = File.createTempFile("ONAPPF", "yaml"); + File tempYamlFile = File.createTempFile(ONAPPF_FILE, "yaml"); + tempYamlFile.deleteOnExit(); + File tempXacmlTemplateFile = new File("src/test/resources/frequency_limiter_template.xml"); - File tempXacmlOutputFile = File.createTempFile("ONAPPF", ".out.xacml"); + + File tempXacmlOutputFile = File.createTempFile(ONAPPF_FILE, OUT_XACML); + tempXacmlOutputFile.deleteOnExit(); Yaml clYaml = new Yaml(new Constructor(ControlLoopGuard.class)); String clYamlString = clYaml.dump(clGuard); @@ -98,23 +112,24 @@ public class PolicyGuardYamlToXacmlTest { // Assert all substitutions are made assertTrue(result.contains("cl")); assertTrue(result.contains("actor")); - assertTrue(result.contains("GoToOz")); - assertTrue(result.contains("Wizard")); - assertTrue(result.contains("WickedWitchOfTheWest")); + assertTrue(result.contains(RECIPE)); + assertTrue(result.contains(TARGET1)); + assertTrue(result.contains(TARGET2)); assertTrue(result.contains("10")); - assertTrue(result.contains("hours")); - assertTrue(result.contains("someStartTime")); - assertTrue(result.contains("someEndTime")); - - tempYamlFile.delete(); - tempXacmlOutputFile.delete(); + assertTrue(result.contains(HOURS)); + assertTrue(result.contains(SOME_START_TIME)); + assertTrue(result.contains(SOME_END_TIME)); } @Test public void testGenerateXacmlGuardPartial() throws IOException { - final File tempYamlFile = File.createTempFile("ONAPPF", "yaml"); + final File tempYamlFile = File.createTempFile(ONAPPF_FILE, "yaml"); + tempYamlFile.deleteOnExit(); + final File tempXacmlTemplateFile = new File("src/test/resources/frequency_limiter_template.xml"); - final File tempXacmlOutputFile = File.createTempFile("ONAPPF", ".out.xacml"); + + final File tempXacmlOutputFile = File.createTempFile(ONAPPF_FILE, OUT_XACML); + tempXacmlOutputFile.deleteOnExit(); MatchParameters matchParameters = clGuard.getGuards().get(0).getMatch_parameters(); matchParameters.setControlLoopName(null); @@ -137,16 +152,13 @@ public class PolicyGuardYamlToXacmlTest { // Assert all substitutions are made assertTrue(result.contains("cl")); assertTrue(result.contains("actor")); - assertFalse(result.contains("GoToOz")); - assertFalse(result.contains("Wizard")); - assertFalse(result.contains("WickedWitchOfTheWest")); + assertFalse(result.contains(RECIPE)); + assertFalse(result.contains(TARGET1)); + assertFalse(result.contains(TARGET2)); assertTrue(result.contains("10")); - assertTrue(result.contains("hours")); - assertTrue(result.contains("someStartTime")); - assertTrue(result.contains("someEndTime")); - - tempYamlFile.delete(); - tempXacmlOutputFile.delete(); + assertTrue(result.contains(HOURS)); + assertTrue(result.contains(SOME_START_TIME)); + assertTrue(result.contains(SOME_END_TIME)); } @Test @@ -173,13 +185,17 @@ public class PolicyGuardYamlToXacmlTest { @Test public void testGenerateXacmlGuardBlacklist() throws IOException { - final File tempYamlFile = File.createTempFile("ONAPPF", "yaml"); + final File tempYamlFile = File.createTempFile(ONAPPF_FILE, "yaml"); + tempYamlFile.deleteOnExit(); + final File tempXacmlTemplateFile = new File("src/test/resources/blacklist_template.xml"); - final File tempXacmlOutputFile = File.createTempFile("ONAPPF", ".out.xacml"); + + final File tempXacmlOutputFile = File.createTempFile(ONAPPF_FILE, OUT_XACML); + tempXacmlOutputFile.deleteOnExit(); List blacklist = new ArrayList<>(); - blacklist.add("WestWitches"); - blacklist.add("EastWitches"); + blacklist.add(TEXT1); + blacklist.add(TEXT2); clGuard.getGuards().get(0).getLimit_constraints().get(0).setBlacklist(blacklist); Yaml clYaml = new Yaml(new Constructor(ControlLoopGuard.class)); @@ -190,27 +206,27 @@ public class PolicyGuardYamlToXacmlTest { tempXacmlTemplateFile.getCanonicalPath(), tempXacmlOutputFile.getCanonicalPath()); String result = SupportTextFileUtils.getTextFileAsString(tempXacmlOutputFile.getCanonicalPath()); - System.err.println(result); // Assert no mote "${}" are left assertFalse(result.contains("${")); assertFalse(result.contains("}")); // Assert all substitutions are made - assertTrue(result.contains("WestWitches")); - assertTrue(result.contains("EastWitches")); - - tempYamlFile.delete(); - tempXacmlOutputFile.delete(); + assertTrue(result.contains(TEXT1)); + assertTrue(result.contains(TEXT2)); } @Test public void testGenerateXacmlGuardBlacklistPartial() throws IOException { - final File tempYamlFile = File.createTempFile("ONAPPF", "yaml"); + final File tempYamlFile = File.createTempFile(ONAPPF_FILE, "yaml"); + tempYamlFile.deleteOnExit(); + final File tempXacmlTemplateFile = new File("src/test/resources/blacklist_template.xml"); - final File tempXacmlOutputFile = File.createTempFile("ONAPPF", ".out.xacml"); + + final File tempXacmlOutputFile = File.createTempFile(ONAPPF_FILE, OUT_XACML); + tempXacmlOutputFile.deleteOnExit(); List blacklist = new ArrayList<>(); - blacklist.add("WestWitches"); - blacklist.add("EastWitches"); + blacklist.add(TEXT1); + blacklist.add(TEXT2); GuardPolicy guardPolicy = clGuard.getGuards().get(0); guardPolicy.getLimit_constraints().get(0).setBlacklist(blacklist); @@ -228,15 +244,11 @@ public class PolicyGuardYamlToXacmlTest { tempXacmlTemplateFile.getCanonicalPath(), tempXacmlOutputFile.getCanonicalPath()); String result = SupportTextFileUtils.getTextFileAsString(tempXacmlOutputFile.getCanonicalPath()); - System.err.println(result); // Assert no mote "${}" are left assertFalse(result.contains("${")); assertFalse(result.contains("}")); // Assert all substitutions are made - assertTrue(result.contains("WestWitches")); - assertTrue(result.contains("EastWitches")); - - tempYamlFile.delete(); - tempXacmlOutputFile.delete(); + assertTrue(result.contains(TEXT1)); + assertTrue(result.contains(TEXT2)); } } diff --git a/controlloop/common/guard/src/test/java/org/onap/policy/guard/SupportTextFileUtils.java b/controlloop/common/guard/src/test/java/org/onap/policy/guard/SupportTextFileUtils.java index 03260ada9..98c33c761 100644 --- a/controlloop/common/guard/src/test/java/org/onap/policy/guard/SupportTextFileUtils.java +++ b/controlloop/common/guard/src/test/java/org/onap/policy/guard/SupportTextFileUtils.java @@ -3,6 +3,7 @@ * guard * ================================================================================ * Copyright (C) 2018 Ericsson. All rights reserved. + * Modifications Copyright (C) 2019 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. @@ -21,9 +22,10 @@ package org.onap.policy.guard; import java.io.File; -import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; +import java.nio.charset.StandardCharsets; +import org.drools.core.util.IoUtils; /** * The Class TextFileUtils is class that provides useful functions for handling text files. @@ -31,7 +33,12 @@ import java.io.IOException; * * @author Liam Fallon (liam.fallon@ericsson.com) */ -public abstract class SupportTextFileUtils { +public class SupportTextFileUtils { + + private SupportTextFileUtils() { + // do nothing + } + /** * Method to return the contents of a text file as a string. * @@ -39,13 +46,8 @@ public abstract class SupportTextFileUtils { * @return A string containing the contents of the file * @throws IOException on errors reading text from the file */ - public static String getTextFileAsString(final String textFilePath) throws IOException { - final File textFile = new File(textFilePath); - final FileInputStream textFileInputStream = new FileInputStream(textFile); - final byte[] textData = new byte[(int) textFile.length()]; - textFileInputStream.read(textData); - textFileInputStream.close(); - return new String(textData); + public static String getTextFileAsString(final String textFilePath) { + return IoUtils.readFileAsString(new File(textFilePath)); } /** @@ -56,8 +58,8 @@ public abstract class SupportTextFileUtils { * @throws IOException on errors reading text from the file */ public static void putStringAsFile(final String outString, final File textFile) throws IOException { - final FileOutputStream textFileOutputStream = new FileOutputStream(textFile); - textFileOutputStream.write(outString.getBytes()); - textFileOutputStream.close(); + try (final FileOutputStream textFileOutputStream = new FileOutputStream(textFile)) { + textFileOutputStream.write(outString.getBytes(StandardCharsets.UTF_8)); + } } } -- cgit 1.2.3-korg