X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=ONAP-PDP%2Fsrc%2Ftest%2Fjava%2Forg%2Fonap%2Fpolicy%2Fxacml%2Faction%2FFindActionTest.java;h=f8a5e4b4a01597059e30f631024349e36e3695fe;hb=da8ae32083194628e581c1ddeb5423fec4ff7dc0;hp=2a36fa280218cd00be2d62a867a1e9c2ae159321;hpb=b6abd7445f80d1d165877f394555f8844fe7ac63;p=policy%2Fengine.git diff --git a/ONAP-PDP/src/test/java/org/onap/policy/xacml/action/FindActionTest.java b/ONAP-PDP/src/test/java/org/onap/policy/xacml/action/FindActionTest.java index 2a36fa280..f8a5e4b4a 100644 --- a/ONAP-PDP/src/test/java/org/onap/policy/xacml/action/FindActionTest.java +++ b/ONAP-PDP/src/test/java/org/onap/policy/xacml/action/FindActionTest.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. @@ -17,16 +17,12 @@ * limitations under the License. * ============LICENSE_END========================================================= */ + package org.onap.policy.xacml.action; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; -import org.onap.policy.xacml.custom.OnapFunctionDefinitionFactory; import com.att.research.xacml.api.Decision; import com.att.research.xacml.api.Request; import com.att.research.xacml.api.XACML3; @@ -45,229 +41,196 @@ import com.att.research.xacml.std.datatypes.DataTypes; import com.att.research.xacml.std.json.JSONRequest; import com.att.research.xacml.util.XACMLProperties; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; import org.onap.policy.drools.http.server.HttpServletServer; import org.onap.policy.drools.utils.NetworkUtil; +import org.onap.policy.xacml.custom.OnapFunctionDefinitionFactory; public class FindActionTest { - - - String xPathExampleFromSpec = "{ " + - "\"Request\" : { " + - "\"Resource\" : { " + - "\"Attribute\": [ " + - "{ " + - "\"Id\" : \"urn:oasis:names:tc:xacml:3.0:content-selector\", " + - "\"DataType\" : \"xpathExpression\", " + - "\"Value\" : { " + - "\"XPathCategory\" : \"urn:oasis:names:tc:xacml:3.0:attribute-category:resource\", " + - "\"Namespaces\" : [{ " + - "\"Namespace\" : \"urn:oasis:names:tc:xacml:3.0:core:schema:wd-17\" " + - "}, " + - "{ " + - "\"Prefix\" : \"md\", " + - "\"Namespace\" : \"urn:example:med:schemas:record\" " + - "} " + - "], " + - "\"XPath\" : \"md:record/md:patient/md:patientDoB\" " + - "} " + - "} " + - "] " + - "} " + - "} " + - "} "; - - String jsonResponse; - Request request; - private static final int MOCK_SERVER_PORT = 6670; - - @BeforeClass - public static void setUpServer() { + + String xpathexamplefromspec = "{ " + "\"Request\" : { " + "\"Resource\" : { " + "\"Attribute\": [ " + "{ " + + "\"Id\" : \"urn:oasis:names:tc:xacml:3.0:content-selector\", " + "\"DataType\" : \"xpathExpression\", " + + "\"Value\" : { " + "\"XPathCategory\" : \"urn:oasis:names:tc:xacml:3.0:attribute-category:resource\", " + + "\"Namespaces\" : [{ " + "\"Namespace\" : \"urn:oasis:names:tc:xacml:3.0:core:schema:wd-17\" " + "}, " + + "{ " + "\"Prefix\" : \"md\", " + "\"Namespace\" : \"urn:example:med:schemas:record\" " + "} " + "], " + + "\"XPath\" : \"md:record/md:patient/md:patientDoB\" " + "} " + "} " + "] " + "} " + "} " + "} "; + + String jsonResponse; + Request request; + private static final int MOCK_SERVER_PORT = 6670; + + /** + * Setup server before test class. + */ + @BeforeClass + public static void setUpServer() { try { - final HttpServletServer testServer = HttpServletServer.factory.build("dmaapSim", - "localhost", MOCK_SERVER_PORT, "/", false, true); - testServer.addServletClass("/*", DummyRest.class.getName()); - testServer.waitedStart(2000); - if (!NetworkUtil.isTcpPortOpen("localhost", testServer.getPort(), 5, 10000L)) - throw new IllegalStateException("cannot connect to port " + testServer.getPort()); + final HttpServletServer testServer = + HttpServletServer.factory.build("dmaapSim", "localhost", MOCK_SERVER_PORT, "/", false, true); + testServer.addServletClass("/*", DummyRest.class.getName()); + testServer.waitedStart(2000); + if (!NetworkUtil.isTcpPortOpen("localhost", testServer.getPort(), 5, 10000L)) { + throw new IllegalStateException("cannot connect to port " + testServer.getPort()); + } } catch (final Exception e) { - fail(e.getMessage()); + fail(e.getMessage()); } - - } - + + } + @AfterClass public static void tearDownSimulator() { HttpServletServer.factory.destroy(); } - - @Before - public void setUp() throws Exception { - new OnapFunctionDefinitionFactory(); - request = JSONRequest.load(xPathExampleFromSpec); - - try { - XACMLProperties.reloadProperties(); - System.setProperty(XACMLProperties.XACML_PROPERTIES_NAME, "src/test/resources/xacml.pdp.properties"); - XACMLProperties.getProperties(); - - assertTrue(true); - } catch (Exception e) { - fail(); - - } - } - - - @Test - public final void testRun() { - FindAction action = new FindAction(); - // fully-loaded multiple response - StdMutableResponse response = new StdMutableResponse(); - // create a Status object - StdMutableStatus status = new StdMutableStatus(StdStatusCode.STATUS_CODE_MISSING_ATTRIBUTE); - status.setStatusMessage("some status message"); - StdMutableStatusDetail statusDetailIn = new StdMutableStatusDetail(); - StdMutableMissingAttributeDetail mad = new StdMutableMissingAttributeDetail(); - mad.addAttributeValue(new StdAttributeValue(DataTypes.DT_STRING.getId(), "PEPACTION")); - mad.addAttributeValue(new StdAttributeValue(DataTypes.DT_INTEGER.getId(), "PDPACTION")); - mad.setAttributeId(XACML3.ID_ACTION_PURPOSE); - mad.setCategory(XACML3.ID_ATTRIBUTE_CATEGORY_ACTION); - mad.setDataTypeId(XACML3.ID_DATATYPE_STRING); - mad.setIssuer("an Issuer"); - statusDetailIn.addMissingAttributeDetail(mad); - status.setStatusDetail(statusDetailIn); - // create a single result object - StdMutableResult result = new StdMutableResult(status); - // set the decision - result.setDecision(Decision.INDETERMINATE); - // put the Result into the Response - response.add(result); - // create a new Result with a different Decision - status = new StdMutableStatus(StdStatusCode.STATUS_CODE_OK); - result = new StdMutableResult(status); - result.setDecision(Decision.DENY); - - StdMutableObligation obligation = new StdMutableObligation(); - obligation.setId(XACML3.ID_ACTION_IMPLIED_ACTION); - obligation.addAttributeAssignment(new StdMutableAttributeAssignment( - XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE, - XACML3.ID_SUBJECT, - "obligation-issuer1", - new StdAttributeValue(DataTypes.DT_STRING.getId(), "Test"))); - result.addObligation(obligation); - - StdMutableAdvice advice = new StdMutableAdvice(); - advice.setId(XACML3.ID_ACTION_IMPLIED_ACTION); - advice.addAttributeAssignment(new StdMutableAttributeAssignment( - XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE, - XACML3.ID_SUBJECT, - "advice-issuer1", - new StdAttributeValue(DataTypes.DT_STRING.getId(), "Test"))); - response.add(result); - - // The logic below exercises the callRest and takeAction methods in FindAction - // GET request - status = new StdMutableStatus(StdStatusCode.STATUS_CODE_OK); - result = new StdMutableResult(status); - result.setDecision(Decision.PERMIT); - - obligation = new StdMutableObligation(); - obligation.setId(XACML3.ID_ACTION_IMPLIED_ACTION); - obligation.addAttributeAssignment(new StdMutableAttributeAssignment( - XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE, - new IdentifierImpl("performer"), - "obligation-issuer", - new StdAttributeValue(DataTypes.DT_STRING.getId(), "PDPACTION"))); - - obligation.addAttributeAssignment(new StdMutableAttributeAssignment( - XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE, - new IdentifierImpl("URL"), - "obligation-issuer", - new StdAttributeValue(DataTypes.DT_STRING.getId(), "http://localhost:" + MOCK_SERVER_PORT))); - obligation.addAttributeAssignment(new StdMutableAttributeAssignment( - XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE, - new IdentifierImpl("method"), - "obligation-issuer", - new StdAttributeValue(DataTypes.DT_STRING.getId(), "GET"))); - obligation.addAttributeAssignment(new StdMutableAttributeAssignment( - XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE, - new IdentifierImpl("headers"), - "obligation-issuer", - new StdAttributeValue(DataTypes.DT_STRING.getId(), "foobar"))); - - - result.addObligation(obligation); - response.add(result); - - // POST request - status = new StdMutableStatus(StdStatusCode.STATUS_CODE_OK); - result = new StdMutableResult(status); - result.setDecision(Decision.PERMIT); - - obligation = new StdMutableObligation(); - obligation.setId(XACML3.ID_ACTION_IMPLIED_ACTION); - obligation.addAttributeAssignment(new StdMutableAttributeAssignment( - XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE, - new IdentifierImpl("performer"), - "obligation-issuer", - new StdAttributeValue(DataTypes.DT_STRING.getId(), "PDPACTION"))); - - obligation.addAttributeAssignment(new StdMutableAttributeAssignment( - XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE, - new IdentifierImpl("URL"), - "obligation-issuer", - new StdAttributeValue(DataTypes.DT_STRING.getId(), "http://localhost:" + MOCK_SERVER_PORT))); - obligation.addAttributeAssignment(new StdMutableAttributeAssignment( - XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE, - new IdentifierImpl("method"), - "obligation-issuer", - new StdAttributeValue(DataTypes.DT_STRING.getId(), "POST"))); - obligation.addAttributeAssignment(new StdMutableAttributeAssignment( - XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE, - new IdentifierImpl("body"), - "obligation-issuer", - new StdAttributeValue(DataTypes.DT_STRING.getId(), "http://localhost:" + MOCK_SERVER_PORT + "/foobar"))); - - result.addObligation(obligation); - response.add(result); - - //PUT request - status = new StdMutableStatus(StdStatusCode.STATUS_CODE_OK); - result = new StdMutableResult(status); - result.setDecision(Decision.PERMIT); - - obligation = new StdMutableObligation(); - obligation.setId(XACML3.ID_ACTION_IMPLIED_ACTION); - obligation.addAttributeAssignment(new StdMutableAttributeAssignment( - XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE, - new IdentifierImpl("performer"), - "obligation-issuer", - new StdAttributeValue(DataTypes.DT_STRING.getId(), "PDPACTION"))); - - obligation.addAttributeAssignment(new StdMutableAttributeAssignment( - XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE, - new IdentifierImpl("URL"), - "obligation-issuer", - new StdAttributeValue(DataTypes.DT_STRING.getId(), "http://localhost:" + MOCK_SERVER_PORT))); - obligation.addAttributeAssignment(new StdMutableAttributeAssignment( - XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE, - new IdentifierImpl("method"), - "obligation-issuer", - new StdAttributeValue(DataTypes.DT_STRING.getId(), "PUT"))); - obligation.addAttributeAssignment(new StdMutableAttributeAssignment( - XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE, - new IdentifierImpl("body"), - "obligation-issuer", - new StdAttributeValue(DataTypes.DT_STRING.getId(), "http://localhost:" + MOCK_SERVER_PORT + "/foobar"))); - - result.addObligation(obligation); - response.add(result); - - try { - assertTrue(action.run(response, request) != null); - } catch (Exception e) { - fail("operation failed, e="+e); - } - } + + /** + * Setup before test case execution. + * + * @throws Exception if any error occurs + */ + @Before + public void setUp() throws Exception { + new OnapFunctionDefinitionFactory(); + request = JSONRequest.load(xpathexamplefromspec); + + try { + XACMLProperties.reloadProperties(); + System.setProperty(XACMLProperties.XACML_PROPERTIES_NAME, "src/test/resources/xacml.pdp.properties"); + XACMLProperties.getProperties(); + + assertTrue(true); + } catch (final Exception e) { + fail(); + + } + } + + + @Test + public final void testRun() { + final FindAction action = new FindAction(); + // fully-loaded multiple response + final StdMutableResponse response = new StdMutableResponse(); + // create a Status object + StdMutableStatus status = new StdMutableStatus(StdStatusCode.STATUS_CODE_MISSING_ATTRIBUTE); + status.setStatusMessage("some status message"); + final StdMutableStatusDetail statusDetailIn = new StdMutableStatusDetail(); + final StdMutableMissingAttributeDetail mad = new StdMutableMissingAttributeDetail(); + mad.addAttributeValue(new StdAttributeValue(DataTypes.DT_STRING.getId(), "PEPACTION")); + mad.addAttributeValue(new StdAttributeValue(DataTypes.DT_INTEGER.getId(), "PDPACTION")); + mad.setAttributeId(XACML3.ID_ACTION_PURPOSE); + mad.setCategory(XACML3.ID_ATTRIBUTE_CATEGORY_ACTION); + mad.setDataTypeId(XACML3.ID_DATATYPE_STRING); + mad.setIssuer("an Issuer"); + statusDetailIn.addMissingAttributeDetail(mad); + status.setStatusDetail(statusDetailIn); + // create a single result object + StdMutableResult result = new StdMutableResult(status); + // set the decision + result.setDecision(Decision.INDETERMINATE); + // put the Result into the Response + response.add(result); + // create a new Result with a different Decision + status = new StdMutableStatus(StdStatusCode.STATUS_CODE_OK); + result = new StdMutableResult(status); + result.setDecision(Decision.DENY); + + StdMutableObligation obligation = new StdMutableObligation(); + obligation.setId(XACML3.ID_ACTION_IMPLIED_ACTION); + obligation.addAttributeAssignment( + new StdMutableAttributeAssignment(XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE, XACML3.ID_SUBJECT, + "obligation-issuer1", new StdAttributeValue(DataTypes.DT_STRING.getId(), "Test"))); + result.addObligation(obligation); + + final StdMutableAdvice advice = new StdMutableAdvice(); + advice.setId(XACML3.ID_ACTION_IMPLIED_ACTION); + advice.addAttributeAssignment( + new StdMutableAttributeAssignment(XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE, XACML3.ID_SUBJECT, + "advice-issuer1", new StdAttributeValue(DataTypes.DT_STRING.getId(), "Test"))); + response.add(result); + + // The logic below exercises the callRest and takeAction methods in FindAction + // GET request + status = new StdMutableStatus(StdStatusCode.STATUS_CODE_OK); + result = new StdMutableResult(status); + result.setDecision(Decision.PERMIT); + + obligation = new StdMutableObligation(); + obligation.setId(XACML3.ID_ACTION_IMPLIED_ACTION); + obligation.addAttributeAssignment(new StdMutableAttributeAssignment(XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE, + new IdentifierImpl("performer"), "obligation-issuer", + new StdAttributeValue(DataTypes.DT_STRING.getId(), "PDPACTION"))); + + obligation.addAttributeAssignment(new StdMutableAttributeAssignment(XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE, + new IdentifierImpl("URL"), "obligation-issuer", + new StdAttributeValue(DataTypes.DT_STRING.getId(), "http://localhost:" + MOCK_SERVER_PORT))); + obligation.addAttributeAssignment( + new StdMutableAttributeAssignment(XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE, new IdentifierImpl("method"), + "obligation-issuer", new StdAttributeValue(DataTypes.DT_STRING.getId(), "GET"))); + obligation.addAttributeAssignment( + new StdMutableAttributeAssignment(XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE, new IdentifierImpl("headers"), + "obligation-issuer", new StdAttributeValue(DataTypes.DT_STRING.getId(), "foobar"))); + + + result.addObligation(obligation); + response.add(result); + + // POST request + status = new StdMutableStatus(StdStatusCode.STATUS_CODE_OK); + result = new StdMutableResult(status); + result.setDecision(Decision.PERMIT); + + obligation = new StdMutableObligation(); + obligation.setId(XACML3.ID_ACTION_IMPLIED_ACTION); + obligation.addAttributeAssignment(new StdMutableAttributeAssignment(XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE, + new IdentifierImpl("performer"), "obligation-issuer", + new StdAttributeValue(DataTypes.DT_STRING.getId(), "PDPACTION"))); + + obligation.addAttributeAssignment(new StdMutableAttributeAssignment(XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE, + new IdentifierImpl("URL"), "obligation-issuer", + new StdAttributeValue(DataTypes.DT_STRING.getId(), "http://localhost:" + MOCK_SERVER_PORT))); + obligation.addAttributeAssignment( + new StdMutableAttributeAssignment(XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE, new IdentifierImpl("method"), + "obligation-issuer", new StdAttributeValue(DataTypes.DT_STRING.getId(), "POST"))); + obligation.addAttributeAssignment(new StdMutableAttributeAssignment(XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE, + new IdentifierImpl("body"), "obligation-issuer", new StdAttributeValue( + DataTypes.DT_STRING.getId(), "http://localhost:" + MOCK_SERVER_PORT + "/foobar"))); + + result.addObligation(obligation); + response.add(result); + + // PUT request + status = new StdMutableStatus(StdStatusCode.STATUS_CODE_OK); + result = new StdMutableResult(status); + result.setDecision(Decision.PERMIT); + + obligation = new StdMutableObligation(); + obligation.setId(XACML3.ID_ACTION_IMPLIED_ACTION); + obligation.addAttributeAssignment(new StdMutableAttributeAssignment(XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE, + new IdentifierImpl("performer"), "obligation-issuer", + new StdAttributeValue(DataTypes.DT_STRING.getId(), "PDPACTION"))); + + obligation.addAttributeAssignment(new StdMutableAttributeAssignment(XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE, + new IdentifierImpl("URL"), "obligation-issuer", + new StdAttributeValue(DataTypes.DT_STRING.getId(), "http://localhost:" + MOCK_SERVER_PORT))); + obligation.addAttributeAssignment( + new StdMutableAttributeAssignment(XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE, new IdentifierImpl("method"), + "obligation-issuer", new StdAttributeValue(DataTypes.DT_STRING.getId(), "PUT"))); + obligation.addAttributeAssignment(new StdMutableAttributeAssignment(XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE, + new IdentifierImpl("body"), "obligation-issuer", new StdAttributeValue( + DataTypes.DT_STRING.getId(), "http://localhost:" + MOCK_SERVER_PORT + "/foobar"))); + + result.addObligation(obligation); + response.add(result); + + try { + assertTrue(action.run(response, request) != null); + } catch (final Exception e) { + fail("operation failed, e=" + e); + } + } }