diff options
Diffstat (limited to 'controlloop/common/guard')
11 files changed, 242 insertions, 260 deletions
diff --git a/controlloop/common/guard/src/main/java/org/onap/policy/guard/impl/PnfTargetLock.java b/controlloop/common/guard/src/main/java/org/onap/policy/guard/impl/PnfTargetLock.java index c1ad34a22..8ddb5ffcf 100644 --- a/controlloop/common/guard/src/main/java/org/onap/policy/guard/impl/PnfTargetLock.java +++ b/controlloop/common/guard/src/main/java/org/onap/policy/guard/impl/PnfTargetLock.java @@ -7,9 +7,9 @@ * 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. @@ -20,70 +20,28 @@ package org.onap.policy.guard.impl; -import java.io.Serializable; import java.util.UUID; - import org.onap.policy.controlloop.policy.TargetType; import org.onap.policy.guard.LockCallback; -import org.onap.policy.guard.TargetLock; -public class PnfTargetLock implements TargetLock, Serializable { +public class PnfTargetLock extends TargetLockImpl { private static final long serialVersionUID = 2335897394577202732L; - private final UUID lockId; - private final TargetType targetType; - private final String target; - private final UUID requestId; - private final transient LockCallback callback; - /** * Construct an instance. - * + * * @param type the target type * @param target the target * @param requestId the request Id * @param callback the callback */ public PnfTargetLock(TargetType type, String target, UUID requestId, LockCallback callback) { - this.lockId = UUID.randomUUID(); - this.targetType = type; - this.target = target; - this.requestId = requestId; - this.callback = callback; - } - - @Override - public UUID getLockId() { - return this.lockId; - } - - - @Override - public TargetType getTargetType() { - return targetType; - } - - @Override - public String getTargetInstance() { - return target; - } - - @Override - public UUID getRequestId() { - return this.requestId; - } - - public LockCallback getCallback() { - return this.callback; + super(type, target, requestId, callback); } @Override public String toString() { - return "PnfTargetLock [lockId=" + lockId + ", targetType=" + targetType + ", target=" + target + ", requestId=" - + requestId + "]"; + return "PnfTargetLock [" + super.toString() + "]"; } - - - } diff --git a/controlloop/common/guard/src/main/java/org/onap/policy/guard/impl/TargetLockImpl.java b/controlloop/common/guard/src/main/java/org/onap/policy/guard/impl/TargetLockImpl.java new file mode 100644 index 000000000..d406999f4 --- /dev/null +++ b/controlloop/common/guard/src/main/java/org/onap/policy/guard/impl/TargetLockImpl.java @@ -0,0 +1,86 @@ +/*- + * ============LICENSE_START======================================================= + * guard + * ================================================================================ + * 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. + * 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.guard.impl; + +import java.io.Serializable; +import java.util.UUID; + +import org.onap.policy.controlloop.policy.TargetType; +import org.onap.policy.guard.LockCallback; +import org.onap.policy.guard.TargetLock; + +public class TargetLockImpl implements TargetLock, Serializable { + + private static final long serialVersionUID = 2335897394577202732L; + + private final UUID lockId; + private final TargetType targetType; + private final String target; + private final UUID requestId; + private final transient LockCallback callback; + + /** + * Construct an instance. + * + * @param type the target type + * @param target the target + * @param requestId the request Id + * @param callback the callback + */ + public TargetLockImpl(TargetType type, String target, UUID requestId, LockCallback callback) { + this.lockId = UUID.randomUUID(); + this.targetType = type; + this.target = target; + this.requestId = requestId; + this.callback = callback; + } + + @Override + public UUID getLockId() { + return this.lockId; + } + + + @Override + public TargetType getTargetType() { + return targetType; + } + + @Override + public String getTargetInstance() { + return target; + } + + @Override + public UUID getRequestId() { + return this.requestId; + } + + public LockCallback getCallback() { + return this.callback; + } + + @Override + public String toString() { + return "lockId=" + lockId + ", targetType=" + targetType + ", target=" + target + ", requestId=" + + requestId; + } +} diff --git a/controlloop/common/guard/src/main/java/org/onap/policy/guard/impl/VmTargetLock.java b/controlloop/common/guard/src/main/java/org/onap/policy/guard/impl/VmTargetLock.java index 301c6ec7c..2e612a03a 100644 --- a/controlloop/common/guard/src/main/java/org/onap/policy/guard/impl/VmTargetLock.java +++ b/controlloop/common/guard/src/main/java/org/onap/policy/guard/impl/VmTargetLock.java @@ -7,9 +7,9 @@ * 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. @@ -20,66 +20,28 @@ package org.onap.policy.guard.impl; -import java.io.Serializable; import java.util.UUID; - import org.onap.policy.controlloop.policy.TargetType; import org.onap.policy.guard.LockCallback; -import org.onap.policy.guard.TargetLock; -public class VmTargetLock implements TargetLock, Serializable { +public class VmTargetLock extends TargetLockImpl { - private static final long serialVersionUID = -8795145054334409724L; - private final UUID lockId; - private final TargetType targetType; - private final String target; - private final UUID requestId; - private final transient LockCallback callback; + private static final long serialVersionUID = 2335897394577202732L; /** - * Create an instance. - * - * @param targetType the target type + * Construct an instance. + * + * @param type the target type * @param target the target * @param requestId the request Id * @param callback the callback */ - public VmTargetLock(TargetType targetType, String target, UUID requestId, LockCallback callback) { - this.lockId = UUID.randomUUID(); - this.targetType = targetType; - this.target = target; - this.requestId = requestId; - this.callback = callback; - } - - @Override - public UUID getLockId() { - return this.lockId; - } - - @Override - public TargetType getTargetType() { - return targetType; - } - - @Override - public String getTargetInstance() { - return target; - } - - @Override - public UUID getRequestId() { - return this.requestId; - } - - public LockCallback getCallback() { - return this.callback; + public VmTargetLock(TargetType type, String target, UUID requestId, LockCallback callback) { + super(type, target, requestId, callback); } @Override public String toString() { - return "VmTargetLock [lockId=" + lockId + ", targetType=" + targetType + ", target=" + target + ", requestId=" - + requestId + "]"; + return "VmTargetLock [" + super.toString() + "]"; } - } diff --git a/controlloop/common/guard/src/main/java/org/onap/policy/guard/impl/VnfTargetLock.java b/controlloop/common/guard/src/main/java/org/onap/policy/guard/impl/VnfTargetLock.java index 2e3fc28b1..2912c7ad4 100644 --- a/controlloop/common/guard/src/main/java/org/onap/policy/guard/impl/VnfTargetLock.java +++ b/controlloop/common/guard/src/main/java/org/onap/policy/guard/impl/VnfTargetLock.java @@ -7,9 +7,9 @@ * 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. @@ -20,70 +20,28 @@ package org.onap.policy.guard.impl; -import java.io.Serializable; import java.util.UUID; - import org.onap.policy.controlloop.policy.TargetType; import org.onap.policy.guard.LockCallback; -import org.onap.policy.guard.TargetLock; -public class VnfTargetLock implements TargetLock, Serializable { +public class VnfTargetLock extends TargetLockImpl { private static final long serialVersionUID = 2335897394577202732L; - private final UUID lockId; - private final TargetType targetType; - private final String target; - private final UUID requestId; - private final transient LockCallback callback; - /** - * Create an instance. - * - * @param type the type + * Construct an instance. + * + * @param type the target type * @param target the target * @param requestId the request Id * @param callback the callback */ public VnfTargetLock(TargetType type, String target, UUID requestId, LockCallback callback) { - this.lockId = UUID.randomUUID(); - this.targetType = type; - this.target = target; - this.requestId = requestId; - this.callback = callback; - } - - @Override - public UUID getLockId() { - return this.lockId; - } - - - @Override - public TargetType getTargetType() { - return targetType; - } - - @Override - public String getTargetInstance() { - return target; - } - - @Override - public UUID getRequestId() { - return this.requestId; - } - - public LockCallback getCallback() { - return this.callback; + super(type, target, requestId, callback); } @Override public String toString() { - return "VnfTargetLock [lockId=" + lockId + ", targetType=" + targetType + ", target=" + target + ", requestId=" - + requestId + "]"; + return "VnfTargetLock [" + super.toString() + "]"; } - - - } 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<GuardResult, TargetLock> result; @@ -299,7 +299,7 @@ public class PolicyGuardTest { } @Test - public void testLockTargetTargetLockInt() throws Exception { + public void testLockTargetTargetLockInt() { TargetType type = TargetType.VM; LockResult<GuardResult, TargetLock> 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<String> 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<String, String> timeWindow = new HashMap<>(); timeWindow.put("value", "10"); - timeWindow.put("units", "hours"); + timeWindow.put("units", HOURS); limitConstraint.setTime_window(timeWindow); Map<String, String> 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<Constraint> 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<String> 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<String> 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)); + } } } |