From 70770572844f95a11206ae008dd62e42aedfe04d Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Mon, 26 Jul 2021 11:11:09 -0400 Subject: Add "configure" operation to xacml Added "configure" operation to xacml simulator and actor.xacml. Issue-ID: POLICY-3502 Change-Id: Ia206303c65ce4e54187d818da9253dabfe864d62 Signed-off-by: Jim Hahn --- .../onap/policy/simulators/XacmlSimulatorTest.java | 92 ++++++++++++++++++---- 1 file changed, 78 insertions(+), 14 deletions(-) (limited to 'models-interactions/model-simulators/src/test/java') diff --git a/models-interactions/model-simulators/src/test/java/org/onap/policy/simulators/XacmlSimulatorTest.java b/models-interactions/model-simulators/src/test/java/org/onap/policy/simulators/XacmlSimulatorTest.java index 53b476fb2..e188edc77 100644 --- a/models-interactions/model-simulators/src/test/java/org/onap/policy/simulators/XacmlSimulatorTest.java +++ b/models-interactions/model-simulators/src/test/java/org/onap/policy/simulators/XacmlSimulatorTest.java @@ -21,6 +21,7 @@ package org.onap.policy.simulators; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.fail; @@ -60,35 +61,98 @@ public class XacmlSimulatorTest { @Test public void testGuard() throws CoderException { - String request = makeRequest("test_actor_id", "test_op_id", "test_target", "test_clName"); + String request = makeGuardRequest("test_actor_id", "test_op_id", "test_target", "test_clName"); + DecisionResponse decision = sendRequest(request); + assertEquals("Permit", decision.getStatus()); + + request = makeGuardRequest("test_actor_id", "test_op_id", "test_target", "denyGuard"); + decision = sendRequest(request); + assertEquals("Deny", decision.getStatus()); + } + + @Test + public void testConfigure() throws CoderException { + // test retrieving a policy + String request = makeConfigureRequest("policy-id", "test-policy"); + DecisionResponse decision = sendRequest(request); + assertNotNull(decision.getPolicies()); + assertThat(decision.getPolicies()).containsKey("test-policy"); + + // test no policy found + request = makeConfigureRequest("policy-id", "nonexistent"); + decision = sendRequest(request); + assertNotNull(decision.getPolicies()); + assertThat(decision.getPolicies()).doesNotContainKey("nonexistent"); + + // test unsupported operation + request = makeConfigureRequest("policy-type", "test"); + decision = sendRequest(request); + assertEquals("resource must contain policy-id key", decision.getMessage()); + } + + @Test + public void testConfigureMissingFile() throws CoderException { + // test retrieving a policy + String request = makeConfigureRequest("policy-id", "bogus-policy"); + DecisionResponse decision = sendRequest(request); + assertNotNull(decision.getPolicies()); + assertEquals("cannot read policy simulator file", decision.getMessage()); + } + + @Test + public void testConfigureInvalidJson() throws CoderException { + // test retrieving a policy + String request = makeConfigureRequest("policy-id", "invalid-policy"); + DecisionResponse decision = sendRequest(request); + assertNotNull(decision.getPolicies()); + assertEquals("cannot decode policy", decision.getMessage()); + } + + @Test + public void testUnknownAction() throws CoderException { + String request = makeGuardRequest("test_actor_id", "test_op_id", "test_target", "test_clName"); + request = request.replace("guard", "bogus-action"); + DecisionResponse decision = sendRequest(request); + assertThat(decision.getStatus()).isNull(); + assertThat(decision.getMessage()).isEqualTo("unsupported action: bogus-action"); + } + + private DecisionResponse sendRequest(String request) throws CoderException { String url = "http://localhost:" + Util.XACMLSIM_SERVER_PORT + "/policy/pdpx/v1/decision"; Pair response = new RestManager().post(url, "testUname", "testPass", null, "application/json", request); - assertNotNull(response); - assertNotNull(response.getLeft()); - assertNotNull(response.getRight()); - DecisionResponse decision = coder.decode(response.getRight(), DecisionResponse.class); - assertEquals("Permit", decision.getStatus()); - - request = makeRequest("test_actor_id", "test_op_id", "test_target", "denyGuard"); - response = new RestManager().post(url, "testUname", "testPass", null, "application/json", request); + // verify the response isn't null assertNotNull(response); assertNotNull(response.getLeft()); assertNotNull(response.getRight()); - decision = coder.decode(response.getRight(), DecisionResponse.class); - assertEquals("Deny", decision.getStatus()); + + return coder.decode(response.getRight(), DecisionResponse.class); } - private static String makeRequest(String actor, String recipe, String target, String clName) throws CoderException { - Map guard = new HashMap(); + private String makeGuardRequest(String actor, String recipe, String target, String clName) throws CoderException { + Map guard = new HashMap<>(); guard.put("actor", actor); guard.put("recipe", recipe); guard.put("target", target); guard.put("clname", clName); - Map resource = new HashMap(); + + Map resource = new HashMap<>(); resource.put("guard", guard); + + DecisionRequest request = new DecisionRequest(); + request.setAction("guard"); + request.setResource(resource); + + return coder.encode(request); + } + + private String makeConfigureRequest(String key, String val) throws CoderException { + Map resource = new HashMap<>(); + resource.put(key, val); + DecisionRequest request = new DecisionRequest(); + request.setAction("configure"); request.setResource(resource); return coder.encode(request); -- cgit 1.2.3-korg