From 9bf471abe906598ffd2cd280d372e7cb9474a418 Mon Sep 17 00:00:00 2001 From: Pamela Dragosh Date: Tue, 2 Apr 2019 14:48:38 -0700 Subject: Tie XACML REST Decision Ties the decision API to the applications. Adds a static application manager class to manager the applications. Added applicationPath as a parameter to be supported in the Json parameter group. Issue-ID: POLICY-1440 Change-Id: I43cb44b3e308aadcf258fd987f3b53944c2edb72 Signed-off-by: Pamela Dragosh --- .../parameters/TestXacmlPdpParameterGroup.java | 30 +++- .../parameters/TestXacmlPdpParameterHandler.java | 13 +- .../onap/policy/pdpx/main/rest/TestDecision.java | 172 +++++++++++++++++++++ .../pdpx/main/rest/TestXacmlPdpRestServer.java | 17 +- .../pdpx/main/rest/TestXacmlPdpStatistics.java | 19 ++- 5 files changed, 235 insertions(+), 16 deletions(-) create mode 100644 main/src/test/java/org/onap/policy/pdpx/main/rest/TestDecision.java (limited to 'main/src/test/java/org/onap') diff --git a/main/src/test/java/org/onap/policy/pdpx/main/parameters/TestXacmlPdpParameterGroup.java b/main/src/test/java/org/onap/policy/pdpx/main/parameters/TestXacmlPdpParameterGroup.java index 48606c98..2bf6fd81 100644 --- a/main/src/test/java/org/onap/policy/pdpx/main/parameters/TestXacmlPdpParameterGroup.java +++ b/main/src/test/java/org/onap/policy/pdpx/main/parameters/TestXacmlPdpParameterGroup.java @@ -25,7 +25,13 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; +import java.io.File; +import java.io.IOException; + +import org.junit.Before; +import org.junit.ClassRule; import org.junit.Test; +import org.junit.rules.TemporaryFolder; import org.onap.policy.common.parameters.GroupValidationResult; /** @@ -34,12 +40,23 @@ import org.onap.policy.common.parameters.GroupValidationResult; */ public class TestXacmlPdpParameterGroup { CommonTestData commonTestData = new CommonTestData(); + private static File applicationPath; + + @ClassRule + public static final TemporaryFolder applicationFolder = new TemporaryFolder(); + + @Before + public void setupPath() throws IOException { + applicationPath = applicationFolder.newFolder(); + } @Test - public void testXacmlPdpParameterGroup() { + public void testXacmlPdpParameterGroup() throws IOException { final RestServerParameters restServerParameters = commonTestData.getRestServerParameters(false); final XacmlPdpParameterGroup pdpxParameters = - new XacmlPdpParameterGroup(CommonTestData.PDPX_GROUP_NAME, restServerParameters); + new XacmlPdpParameterGroup(CommonTestData.PDPX_GROUP_NAME, + restServerParameters, + applicationPath.getAbsolutePath()); final GroupValidationResult validationResult = pdpxParameters.validate(); assertTrue(validationResult.isValid()); assertEquals(restServerParameters.getHost(), pdpxParameters.getRestServerParameters().getHost()); @@ -54,7 +71,8 @@ public class TestXacmlPdpParameterGroup { @Test public void testXacmlPdpParameterGroup_NullName() { final RestServerParameters restServerParameters = commonTestData.getRestServerParameters(false); - final XacmlPdpParameterGroup pdpxParameters = new XacmlPdpParameterGroup(null, restServerParameters); + final XacmlPdpParameterGroup pdpxParameters = new XacmlPdpParameterGroup(null, restServerParameters, + applicationPath.getAbsolutePath()); final GroupValidationResult validationResult = pdpxParameters.validate(); assertFalse(validationResult.isValid()); assertEquals(null, pdpxParameters.getName()); @@ -66,7 +84,8 @@ public class TestXacmlPdpParameterGroup { public void testXacmlPdpParameterGroup_EmptyName() { final RestServerParameters restServerParameters = commonTestData.getRestServerParameters(false); - final XacmlPdpParameterGroup pdpxParameters = new XacmlPdpParameterGroup("", restServerParameters); + final XacmlPdpParameterGroup pdpxParameters = new XacmlPdpParameterGroup("", restServerParameters, + applicationPath.getAbsolutePath()); final GroupValidationResult validationResult = pdpxParameters.validate(); assertFalse(validationResult.isValid()); assertEquals("", pdpxParameters.getName()); @@ -79,7 +98,8 @@ public class TestXacmlPdpParameterGroup { final RestServerParameters restServerParameters = commonTestData.getRestServerParameters(true); final XacmlPdpParameterGroup pdpxParameters = - new XacmlPdpParameterGroup(CommonTestData.PDPX_GROUP_NAME, restServerParameters); + new XacmlPdpParameterGroup(CommonTestData.PDPX_GROUP_NAME, restServerParameters, + applicationPath.getAbsolutePath()); final GroupValidationResult validationResult = pdpxParameters.validate(); assertFalse(validationResult.isValid()); assertTrue(validationResult.getResult() diff --git a/main/src/test/java/org/onap/policy/pdpx/main/parameters/TestXacmlPdpParameterHandler.java b/main/src/test/java/org/onap/policy/pdpx/main/parameters/TestXacmlPdpParameterHandler.java index ef85a762..de55282e 100644 --- a/main/src/test/java/org/onap/policy/pdpx/main/parameters/TestXacmlPdpParameterHandler.java +++ b/main/src/test/java/org/onap/policy/pdpx/main/parameters/TestXacmlPdpParameterHandler.java @@ -93,11 +93,14 @@ public class TestXacmlPdpParameterHandler { noArguments.parse(noArgumentString); assertThatThrownBy(() -> new XacmlPdpParameterHandler().getParameters(noArguments)) - .hasMessage("validation error(s) on parameters from \"parameters/NoParameters.json\"\nparameter group " - + "\"null\" type \"org.onap.policy.pdpx.main.parameters.XacmlPdpParameterGroup\" INVALID, " - + "parameter group has status INVALID\n" - + " field \"name\" type \"java.lang.String\" value \"null\" " - + "INVALID, must be a non-blank string\n"); + .hasMessage("validation error(s) on parameters from \"parameters/NoParameters.json\"\n" + + "parameter group \"null\" type " + + "\"org.onap.policy.pdpx.main.parameters.XacmlPdpParameterGroup\"" + + " INVALID, parameter group has status INVALID\n" + + " field \"name\" type \"java.lang.String\" value \"null\" INVALID, " + + "must be a non-blank string\n" + + " field \"applicationPath\" type \"java.lang.String\" value \"null\" INVALID, " + + "must have application path for applications to store policies and data.\n"); } @Test diff --git a/main/src/test/java/org/onap/policy/pdpx/main/rest/TestDecision.java b/main/src/test/java/org/onap/policy/pdpx/main/rest/TestDecision.java new file mode 100644 index 00000000..fe307e4f --- /dev/null +++ b/main/src/test/java/org/onap/policy/pdpx/main/rest/TestDecision.java @@ -0,0 +1,172 @@ +/*- + * ============LICENSE_START======================================================= + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.pdpx.main.rest; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertEquals; + +import java.io.IOException; +import java.security.KeyManagementException; +import java.security.NoSuchAlgorithmException; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +import javax.ws.rs.client.Client; +import javax.ws.rs.client.ClientBuilder; +import javax.ws.rs.client.Entity; +import javax.ws.rs.client.Invocation; +import javax.ws.rs.client.WebTarget; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.Response.Status; + +import org.glassfish.jersey.client.ClientConfig; +import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams; +import org.onap.policy.common.endpoints.http.client.HttpClient; +import org.onap.policy.common.gson.GsonMessageBodyHandler; +import org.onap.policy.common.utils.network.NetworkUtil; +import org.onap.policy.models.decisions.concepts.DecisionRequest; +import org.onap.policy.models.decisions.concepts.DecisionResponse; +import org.onap.policy.models.errors.concepts.ErrorResponse; +import org.onap.policy.pdpx.main.PolicyXacmlPdpException; +import org.onap.policy.pdpx.main.startstop.Main; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class TestDecision { + + private static final Logger LOGGER = LoggerFactory.getLogger(TestDecision.class); + + private static Main main; + + /** + * BeforeClass setup environment. + */ + @BeforeClass + public static void beforeClass() { + System.setProperty("org.eclipse.jetty.util.log.class", "org.eclipse.jetty.util.log.StdErrLog"); + System.setProperty("org.eclipse.jetty.LEVEL", "OFF"); + main = startXacmlPdpService(); + } + + @AfterClass + public static void after() throws PolicyXacmlPdpException { + stopXacmlPdpService(main); + } + + @Test + public void testDecision_UnsupportedAction() throws KeyManagementException, NoSuchAlgorithmException, + ClassNotFoundException { + + LOGGER.info("Running test testDecision_UnsupportedAction"); + + DecisionRequest request = new DecisionRequest(); + request.setOnapName("DROOLS"); + request.setAction("foo"); + Map guard = new HashMap(); + guard.put("actor", "foo"); + guard.put("recipe", "bar"); + guard.put("target", "somevnf"); + guard.put("clname", "phoneyloop"); + request.setResource(guard); + + ErrorResponse response = getErrorDecision(request); + LOGGER.info("Response {}", response); + assertThat(response.getResponseCode()).isEqualTo(Status.BAD_REQUEST); + assertThat(response.getErrorMessage()).isEqualToIgnoringCase("No application for action foo"); + } + + @Test + public void testDecision_Guard() throws InterruptedException, IOException { + LOGGER.info("Running test testDecision_Guard"); + + DecisionRequest request = new DecisionRequest(); + request.setOnapName("DROOLS"); + request.setAction("guard"); + Map guard = new HashMap(); + guard.put("actor", "foo"); + guard.put("recipe", "bar"); + guard.put("target", "somevnf"); + guard.put("clname", "phoneyloop"); + request.setResource(guard); + + DecisionResponse response = getDecision(request); + LOGGER.info("Response {}", response); + //assertThat(response.getErrorMessage()).isEqualToIgnoringCase("No application for action foo"); + } + + private static Main startXacmlPdpService() { + final String[] XacmlPdpConfigParameters = {"-c", "parameters/XacmlPdpConfigParameters.json"}; + return new Main(XacmlPdpConfigParameters); + } + + private static void stopXacmlPdpService(final Main main) throws PolicyXacmlPdpException { + main.shutdown(); + } + + private DecisionResponse getDecision(DecisionRequest request) throws InterruptedException, IOException { + final ClientConfig clientConfig = new ClientConfig(); + + final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("healthcheck", "zb!XztG34"); + clientConfig.register(feature); + + final Client client = ClientBuilder.newClient(clientConfig); + final WebTarget webTarget = client.target("http://localhost:6969/policy/pdpx/v1/decision"); + + final Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON); + + if (!NetworkUtil.isTcpPortOpen("localhost", 6969, 6, 10000L)) { + throw new IllegalStateException("Cannot connect to port 6969"); + } + + return invocationBuilder.post(Entity.json(request), DecisionResponse.class); + } + + private ErrorResponse getErrorDecision(DecisionRequest request) throws KeyManagementException, + NoSuchAlgorithmException, ClassNotFoundException { + + HttpClient client = getNoAuthHttpClient(); + + Entity entityRequest = Entity.entity(request, MediaType.APPLICATION_JSON); + Response response = client.post("", entityRequest, Collections.emptyMap()); + + assertEquals(400, response.getStatus()); + + return HttpClient.getBody(response, ErrorResponse.class); + } + + private HttpClient getNoAuthHttpClient() + throws KeyManagementException, NoSuchAlgorithmException, ClassNotFoundException { + return HttpClient.factory.build(BusTopicParams.builder() + .clientName("testDecisionClient") + .serializationProvider(GsonMessageBodyHandler.class.getName()) + .useHttps(false).allowSelfSignedCerts(false).hostname("localhost").port(6969) + .basePath("policy/pdpx/v1/decision") + .userName("healthcheck").password("zb!XztG34").managed(true).build()); + } + + +} \ No newline at end of file diff --git a/main/src/test/java/org/onap/policy/pdpx/main/rest/TestXacmlPdpRestServer.java b/main/src/test/java/org/onap/policy/pdpx/main/rest/TestXacmlPdpRestServer.java index 316abac7..fc3edd73 100644 --- a/main/src/test/java/org/onap/policy/pdpx/main/rest/TestXacmlPdpRestServer.java +++ b/main/src/test/java/org/onap/policy/pdpx/main/rest/TestXacmlPdpRestServer.java @@ -24,6 +24,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import java.io.File; import java.io.IOException; import java.lang.reflect.Constructor; import java.lang.reflect.Modifier; @@ -42,7 +43,9 @@ import org.glassfish.jersey.client.ClientConfig; import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature; import org.junit.After; import org.junit.BeforeClass; +import org.junit.ClassRule; import org.junit.Test; +import org.junit.rules.TemporaryFolder; import org.onap.policy.common.endpoints.report.HealthCheckReport; import org.onap.policy.common.utils.network.NetworkUtil; import org.onap.policy.pdpx.main.PolicyXacmlPdpException; @@ -69,15 +72,21 @@ public class TestXacmlPdpRestServer { private static String KEYSTORE = System.getProperty("user.dir") + "/src/test/resources/ssl/policy-keystore"; private Main main; private XacmlPdpRestServer restServer; + private static File applicationPath; + + @ClassRule + public static final TemporaryFolder applicationFolder = new TemporaryFolder(); /** * setup. + * + * @throws IOException exception if cannot create temporary folder */ @BeforeClass - public static void setUp() { + public static void setUp() throws IOException { System.setProperty("org.eclipse.jetty.util.log.class", "org.eclipse.jetty.util.log.StdErrLog"); System.setProperty("org.eclipse.jetty.LEVEL", "OFF"); - + applicationPath = applicationFolder.newFolder(); } /** @@ -115,7 +124,7 @@ public class TestXacmlPdpRestServer { public void testHealthCheckFailure() throws InterruptedException, IOException { final RestServerParameters restServerParams = new CommonTestData().getRestServerParameters(false); restServerParams.setName(CommonTestData.PDPX_GROUP_NAME); - restServer = new XacmlPdpRestServer(restServerParams); + restServer = new XacmlPdpRestServer(restServerParams, applicationPath.getAbsolutePath()); restServer.start(); final Invocation.Builder invocationBuilder = sendHttpRequest(HEALTHCHECK_ENDPOINT); final HealthCheckReport report = invocationBuilder.get(HealthCheckReport.class); @@ -149,7 +158,7 @@ public class TestXacmlPdpRestServer { public void testStatistics_500() throws IOException, InterruptedException { final RestServerParameters restServerParams = new CommonTestData().getRestServerParameters(false); restServerParams.setName(CommonTestData.PDPX_GROUP_NAME); - restServer = new XacmlPdpRestServer(restServerParams); + restServer = new XacmlPdpRestServer(restServerParams, applicationPath.getAbsolutePath()); restServer.start(); final Invocation.Builder invocationBuilder = sendHttpRequest(STATISTICS_ENDPOINT); final StatisticsReport report = invocationBuilder.get(StatisticsReport.class); diff --git a/main/src/test/java/org/onap/policy/pdpx/main/rest/TestXacmlPdpStatistics.java b/main/src/test/java/org/onap/policy/pdpx/main/rest/TestXacmlPdpStatistics.java index 595301de..8d2f7ce4 100644 --- a/main/src/test/java/org/onap/policy/pdpx/main/rest/TestXacmlPdpStatistics.java +++ b/main/src/test/java/org/onap/policy/pdpx/main/rest/TestXacmlPdpStatistics.java @@ -25,6 +25,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; +import java.io.File; import java.io.IOException; import javax.ws.rs.client.Client; import javax.ws.rs.client.ClientBuilder; @@ -33,8 +34,11 @@ import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.MediaType; import org.glassfish.jersey.client.ClientConfig; import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature; +import org.junit.Before; import org.junit.BeforeClass; +import org.junit.ClassRule; import org.junit.Test; +import org.junit.rules.TemporaryFolder; import org.onap.policy.common.utils.network.NetworkUtil; import org.onap.policy.pdpx.main.PolicyXacmlPdpException; import org.onap.policy.pdpx.main.parameters.CommonTestData; @@ -52,11 +56,21 @@ import org.slf4j.LoggerFactory; public class TestXacmlPdpStatistics { private static final Logger LOGGER = LoggerFactory.getLogger(TestXacmlPdpStatistics.class); + private static File applicationPath; + @ClassRule + public static final TemporaryFolder applicationFolder = new TemporaryFolder(); + + /** + * Turn off some debugging and create temporary folder for applications. + * + * @throws IOException If temporary folder fails + */ @BeforeClass - public static void beforeClass() { + public static void beforeClass() throws IOException { System.setProperty("org.eclipse.jetty.util.log.class", "org.eclipse.jetty.util.log.StdErrLog"); System.setProperty("org.eclipse.jetty.LEVEL", "OFF"); + applicationPath = applicationFolder.newFolder(); } @Test @@ -81,7 +95,8 @@ public class TestXacmlPdpStatistics { public void testXacmlPdpStatistics_500() throws InterruptedException { final RestServerParameters restServerParams = new CommonTestData().getRestServerParameters(false); restServerParams.setName(CommonTestData.PDPX_GROUP_NAME); - final XacmlPdpRestServer restServer = new XacmlPdpRestServer(restServerParams); + final XacmlPdpRestServer restServer = new XacmlPdpRestServer(restServerParams, + applicationPath.getAbsolutePath()); try { restServer.start(); -- cgit 1.2.3-korg