summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorramverma <ram.krishna.verma@ericsson.com>2018-09-11 12:55:27 +0100
committerramverma <ram.krishna.verma@ericsson.com>2018-09-12 13:53:08 +0100
commite52c9ed74a40d86f1494befd89f55f499b28dcfe (patch)
tree38fe808c4179dfda0f9834c2b2bdaaaf2a68c17e
parent1b5763e20c91f40da4ad44b2d6bd09331705b27a (diff)
Adding policy forwarder for apex-pdp
* Adding policy forwarder for apex-pdp * Adding ApexPdpPolicy to hold policies related to apex-pdp * Adding forwarder parameter for configuring apex-pdp forwarder * Adding test cases for all code changes. Change-Id: Ic1cb9bfc11b2b95321ea2d81882c588b025d33fb Issue-ID: POLICY-1101 Signed-off-by: ramverma <ram.krishna.verma@ericsson.com>
-rw-r--r--model/src/main/java/org/onap/policy/distribution/model/ApexPdpPolicy.java69
-rw-r--r--plugins/forwarding-plugins/pom.xml5
-rw-r--r--plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/apex/pdp/ApexPdpPolicyForwarder.java105
-rw-r--r--plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/apex/pdp/ApexPdpPolicyForwarderParameterBuilder.java111
-rw-r--r--plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/apex/pdp/ApexPdpPolicyForwarderParameterGroup.java79
-rw-r--r--plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/apex/pdp/ApexPdpPolicyForwarderParameterGroupTest.java69
-rw-r--r--plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/apex/pdp/ApexPdpPolicyForwarderTest.java202
7 files changed, 640 insertions, 0 deletions
diff --git a/model/src/main/java/org/onap/policy/distribution/model/ApexPdpPolicy.java b/model/src/main/java/org/onap/policy/distribution/model/ApexPdpPolicy.java
new file mode 100644
index 00000000..a8f45764
--- /dev/null
+++ b/model/src/main/java/org/onap/policy/distribution/model/ApexPdpPolicy.java
@@ -0,0 +1,69 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2018 Ericsson. 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.distribution.model;
+
+import java.io.InputStream;
+
+/**
+ * This class represents an apex-pdp policy which can be decoded by a relevant {@link PolicyDecoder}.
+ *
+ * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com)
+ */
+public class ApexPdpPolicy implements Policy {
+ private String policyName;
+ private InputStream policyInputStream;
+
+ /**
+ * Constructor for creating instance of {@link ApexPdpPolicy}.
+ *
+ * @param policyName the policy file name
+ * @param policyInputStream the input stream
+ */
+ public ApexPdpPolicy(final String policyName, final InputStream policyInputStream) {
+ this.policyName = policyName;
+ this.policyInputStream = policyInputStream;
+ }
+
+ /**
+ * Returns the policyInputStream of this ApexPdpPolicy instance.
+ *
+ * @return the policyInputStream
+ */
+ public InputStream getPolicyInputStream() {
+ return policyInputStream;
+ }
+
+ /**
+ * {@inheritDoc}.
+ */
+ @Override
+ public String getPolicyName() {
+ return policyName;
+ }
+
+ /**
+ * {@inheritDoc}.
+ */
+ @Override
+ public String getPolicyType() {
+ return "Method not supported";
+ }
+}
diff --git a/plugins/forwarding-plugins/pom.xml b/plugins/forwarding-plugins/pom.xml
index b82ad084..dee6631f 100644
--- a/plugins/forwarding-plugins/pom.xml
+++ b/plugins/forwarding-plugins/pom.xml
@@ -47,6 +47,11 @@
<version>1.3.0-SNAPSHOT</version>
</dependency>
<dependency>
+ <groupId>org.onap.policy.apex-pdp.core</groupId>
+ <artifactId>core-deployment</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ <dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<scope>test</scope>
diff --git a/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/apex/pdp/ApexPdpPolicyForwarder.java b/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/apex/pdp/ApexPdpPolicyForwarder.java
new file mode 100644
index 00000000..1a603f04
--- /dev/null
+++ b/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/apex/pdp/ApexPdpPolicyForwarder.java
@@ -0,0 +1,105 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2018 Ericsson. 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.distribution.forwarding.apex.pdp;
+
+import java.util.Collection;
+
+import org.onap.policy.apex.core.deployment.EngineServiceFacade;
+import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
+import org.onap.policy.common.logging.flexlogger.FlexLogger;
+import org.onap.policy.common.logging.flexlogger.Logger;
+import org.onap.policy.common.parameters.ParameterService;
+import org.onap.policy.distribution.forwarding.PolicyForwarder;
+import org.onap.policy.distribution.forwarding.PolicyForwardingException;
+import org.onap.policy.distribution.forwarding.xacml.pdp.XacmlPdpPolicyForwarder;
+import org.onap.policy.distribution.model.ApexPdpPolicy;
+import org.onap.policy.distribution.model.Policy;
+
+/**
+ * This class provides an implementation of {@link PolicyForwarder} interface for forwarding the given policies to
+ * apex-pdp.
+ *
+ * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com)
+ */
+public class ApexPdpPolicyForwarder implements PolicyForwarder {
+
+ private static final Logger LOGGER = FlexLogger.getLogger(XacmlPdpPolicyForwarder.class);
+
+ private ApexPdpPolicyForwarderParameterGroup apexForwarderParameters;
+ private EngineServiceFacade engineServiceFacade;
+
+ /**
+ * {@inheritDoc}.
+ */
+ @Override
+ public void configure(final String parameterGroupName) {
+ apexForwarderParameters = ParameterService.get(parameterGroupName);
+ engineServiceFacade =
+ new EngineServiceFacade(apexForwarderParameters.getHostname(), apexForwarderParameters.getPort());
+
+ }
+
+ /**
+ * {@inheritDoc}.
+ */
+ @Override
+ public void forward(final Collection<Policy> policies) throws PolicyForwardingException {
+ if (policies.size() > 1) {
+ final String message = "More than one apex policy cannot be forwarded to an apex engine";
+ LOGGER.debug(message);
+ throw new PolicyForwardingException(message);
+
+ } else {
+ final Policy policy = (Policy) policies.toArray()[0];
+ if (policy.getClass().isAssignableFrom(ApexPdpPolicy.class)) {
+ forwardPolicy((ApexPdpPolicy) policy);
+ } else {
+ final String message = "Ignoring the policy as it is not an apex-pdp policy";
+ LOGGER.debug(message);
+ throw new PolicyForwardingException(message);
+ }
+ }
+ }
+
+ /**
+ * Method to forward a given policy to apex-pdp.
+ *
+ * @param apexPolicy the apex policy
+ * @throws PolicyForwardingException if any exception occurs while forwarding policy
+ */
+ private void forwardPolicy(final ApexPdpPolicy apexPolicy) throws PolicyForwardingException {
+ try {
+ engineServiceFacade.init();
+ engineServiceFacade.deployModel(apexPolicy.getPolicyName(), apexPolicy.getPolicyInputStream(),
+ apexForwarderParameters.isIgnoreConflicts(), apexForwarderParameters.isForceUpdate());
+
+ LOGGER.debug("Sucessfully forwarded the policy to apex-pdp egine at "
+ + apexForwarderParameters.getHostname() + ":" + apexForwarderParameters.getPort());
+
+ } catch (final ApexException exp) {
+ final String message = "Error sending policy to apex-pdp engine at" + apexForwarderParameters.getHostname()
+ + ":" + apexForwarderParameters.getPort();
+ LOGGER.error(message, exp);
+ throw new PolicyForwardingException(message, exp);
+ }
+ }
+}
+
diff --git a/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/apex/pdp/ApexPdpPolicyForwarderParameterBuilder.java b/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/apex/pdp/ApexPdpPolicyForwarderParameterBuilder.java
new file mode 100644
index 00000000..faa066fe
--- /dev/null
+++ b/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/apex/pdp/ApexPdpPolicyForwarderParameterBuilder.java
@@ -0,0 +1,111 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2018 Ericsson. 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.distribution.forwarding.apex.pdp;
+
+/**
+ * This builder holds all the parameters needed to build an instance of {@link ApexPdpPolicyForwarderParameterGroup}
+ * class.
+ *
+ * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com)
+ */
+public class ApexPdpPolicyForwarderParameterBuilder {
+
+ private String hostname;
+ private int port;
+ private boolean ignoreConflicts;
+ private boolean forceUpdate;
+
+ /**
+ * Set host name to this {@link ApexPdpPolicyForwarderParameterBuilder} instance.
+ *
+ * @param hostname the host name
+ */
+ public ApexPdpPolicyForwarderParameterBuilder setHostname(final String hostname) {
+ this.hostname = hostname;
+ return this;
+ }
+
+ /**
+ * Set port to this {@link ApexPdpPolicyForwarderParameterBuilder} instance.
+ *
+ * @param port the port number
+ */
+ public ApexPdpPolicyForwarderParameterBuilder setPort(final int port) {
+ this.port = port;
+ return this;
+ }
+
+ /**
+ * Set ignore conflicts flag to this {@link ApexPdpPolicyForwarderParameterBuilder} instance.
+ *
+ * @param ignoreConflicts the ignore conflicts flag
+ */
+ public ApexPdpPolicyForwarderParameterBuilder setIgnoreConflicts(final boolean ignoreConflicts) {
+ this.ignoreConflicts = ignoreConflicts;
+ return this;
+ }
+
+ /**
+ * Set force update flag to this {@link ApexPdpPolicyForwarderParameterBuilder} instance.
+ *
+ * @param forceUpdate the force update flag
+ */
+ public ApexPdpPolicyForwarderParameterBuilder setForceUpdate(final boolean forceUpdate) {
+ this.forceUpdate = forceUpdate;
+ return this;
+ }
+
+ /**
+ * Returns the host name of this {@link ApexPdpPolicyForwarderParameterBuilder} instance.
+ *
+ * @return the host name
+ */
+ public String getHostname() {
+ return hostname;
+ }
+
+ /**
+ * Returns the port of this {@link ApexPdpPolicyForwarderParameterBuilder} instance.
+ *
+ * @return the port
+ */
+ public int getPort() {
+ return port;
+ }
+
+ /**
+ * Returns the ignore conflicts flag of this {@link ApexPdpPolicyForwarderParameterBuilder} instance.
+ *
+ * @return the ignoreConflicts
+ */
+ public boolean isIgnoreConflicts() {
+ return ignoreConflicts;
+ }
+
+ /**
+ * Returns the force update flag of this {@link ApexPdpPolicyForwarderParameterBuilder} instance.
+ *
+ * @return the forceUpdate
+ */
+ public boolean isForceUpdate() {
+ return forceUpdate;
+ }
+}
diff --git a/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/apex/pdp/ApexPdpPolicyForwarderParameterGroup.java b/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/apex/pdp/ApexPdpPolicyForwarderParameterGroup.java
new file mode 100644
index 00000000..b236fabd
--- /dev/null
+++ b/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/apex/pdp/ApexPdpPolicyForwarderParameterGroup.java
@@ -0,0 +1,79 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2018 Ericsson. 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.distribution.forwarding.apex.pdp;
+
+import org.onap.policy.common.parameters.GroupValidationResult;
+import org.onap.policy.common.parameters.ValidationStatus;
+import org.onap.policy.common.utils.validation.ParameterValidationUtils;
+import org.onap.policy.distribution.main.parameters.PolicyForwarderConfigurationParameterGroup;
+
+/**
+ * Holds the parameters for the{@link ApexPdpPolicyForwarder}.
+ */
+public class ApexPdpPolicyForwarderParameterGroup extends PolicyForwarderConfigurationParameterGroup {
+ public static final String POLICY_FORWARDER_PLUGIN_CLASS = ApexPdpPolicyForwarder.class.getCanonicalName();
+
+ private String hostname;
+ private int port;
+ private boolean ignoreConflicts;
+ private boolean forceUpdate;
+
+ /**
+ * Constructor for instantiating {@link ApexPdpPolicyForwarderParameterGroup} class.
+ *
+ * @param builder the apex forwarder parameter builder
+ */
+ public ApexPdpPolicyForwarderParameterGroup(final ApexPdpPolicyForwarderParameterBuilder builder) {
+ this.hostname = builder.getHostname();
+ this.port = builder.getPort();
+ this.ignoreConflicts = builder.isIgnoreConflicts();
+ this.forceUpdate = builder.isForceUpdate();
+ }
+
+ public String getHostname() {
+ return hostname;
+ }
+
+ public int getPort() {
+ return port;
+ }
+
+ public boolean isIgnoreConflicts() {
+ return ignoreConflicts;
+ }
+
+ public boolean isForceUpdate() {
+ return forceUpdate;
+ }
+
+ @Override
+ public GroupValidationResult validate() {
+ final GroupValidationResult validationResult = new GroupValidationResult(this);
+ if (!ParameterValidationUtils.validateStringParameter(hostname)) {
+ validationResult.setResult("hostname", ValidationStatus.INVALID,
+ "must be a non-blank string containing hostname/ipaddress");
+ }
+ if (!ParameterValidationUtils.validateIntParameter(port)) {
+ validationResult.setResult("port", ValidationStatus.INVALID, "must be a positive integer containing port");
+ }
+ return validationResult;
+ }
+}
diff --git a/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/apex/pdp/ApexPdpPolicyForwarderParameterGroupTest.java b/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/apex/pdp/ApexPdpPolicyForwarderParameterGroupTest.java
new file mode 100644
index 00000000..ca0efb5c
--- /dev/null
+++ b/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/apex/pdp/ApexPdpPolicyForwarderParameterGroupTest.java
@@ -0,0 +1,69 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2018 Ericsson. 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.distribution.forwarding.apex.pdp;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Test;
+import org.onap.policy.common.parameters.ValidationStatus;
+
+public class ApexPdpPolicyForwarderParameterGroupTest {
+
+ @Test
+ public void testBuilderAndGetters() {
+ final ApexPdpPolicyForwarderParameterBuilder builder = new ApexPdpPolicyForwarderParameterBuilder();
+ builder.setHostname("10.10.10.10").setPort(1234).setIgnoreConflicts(false).setForceUpdate(true);
+ final ApexPdpPolicyForwarderParameterGroup configurationParameters =
+ new ApexPdpPolicyForwarderParameterGroup(builder);
+ configurationParameters.setName("myConfiguration");
+
+ assertEquals("myConfiguration", configurationParameters.getName());
+ assertTrue(configurationParameters.isForceUpdate());
+ assertEquals("10.10.10.10", configurationParameters.getHostname());
+ assertEquals(1234, configurationParameters.getPort());
+ assertFalse(configurationParameters.isIgnoreConflicts());
+ assertEquals(ValidationStatus.CLEAN, configurationParameters.validate().getStatus());
+ }
+
+ @Test
+ public void testInvalidHostName() {
+ final ApexPdpPolicyForwarderParameterBuilder builder = new ApexPdpPolicyForwarderParameterBuilder();
+ builder.setHostname("").setPort(1234).setIgnoreConflicts(false).setForceUpdate(true);
+ final ApexPdpPolicyForwarderParameterGroup configurationParameters =
+ new ApexPdpPolicyForwarderParameterGroup(builder);
+ configurationParameters.setName("myConfiguration");
+
+ assertEquals(ValidationStatus.INVALID, configurationParameters.validate().getStatus());
+ }
+
+ @Test
+ public void testInvalidPort() {
+ final ApexPdpPolicyForwarderParameterBuilder builder = new ApexPdpPolicyForwarderParameterBuilder();
+ builder.setHostname("10.10.10.10").setPort(-1).setIgnoreConflicts(false).setForceUpdate(true);
+ final ApexPdpPolicyForwarderParameterGroup configurationParameters =
+ new ApexPdpPolicyForwarderParameterGroup(builder);
+ configurationParameters.setName("myConfiguration");
+
+ assertEquals(ValidationStatus.INVALID, configurationParameters.validate().getStatus());
+ }
+}
diff --git a/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/apex/pdp/ApexPdpPolicyForwarderTest.java b/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/apex/pdp/ApexPdpPolicyForwarderTest.java
new file mode 100644
index 00000000..abf5b508
--- /dev/null
+++ b/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/apex/pdp/ApexPdpPolicyForwarderTest.java
@@ -0,0 +1,202 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2018 Ericsson. 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.distribution.forwarding.apex.pdp;
+
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.lang.reflect.Field;
+import java.util.ArrayList;
+import java.util.Collection;
+
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.runners.MockitoJUnitRunner;
+import org.onap.policy.apex.core.deployment.EngineServiceFacade;
+import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
+import org.onap.policy.common.parameters.ParameterGroup;
+import org.onap.policy.common.parameters.ParameterService;
+import org.onap.policy.distribution.forwarding.PolicyForwardingException;
+import org.onap.policy.distribution.model.ApexPdpPolicy;
+import org.onap.policy.distribution.model.Policy;
+
+@RunWith(MockitoJUnitRunner.class)
+public class ApexPdpPolicyForwarderTest {
+
+ private static final String HOST_NAME = "10.10.10.10";
+ private static final int PORT = 1234;
+ private static final boolean IGNORE_CONFLICTS = false;
+ private static final boolean FORCE_UPDATE = true;
+ private static final String GROUP_NAME = "apexPdpConfiguration";
+
+ @Mock
+ EngineServiceFacade engineServiceFacade;
+
+ /**
+ * Set up.
+ */
+ @BeforeClass
+ public static void setUp() {
+ final ApexPdpPolicyForwarderParameterBuilder builder = new ApexPdpPolicyForwarderParameterBuilder();
+ builder.setHostname(HOST_NAME).setPort(PORT).setIgnoreConflicts(IGNORE_CONFLICTS).setForceUpdate(FORCE_UPDATE);
+ final ParameterGroup parameterGroup = new ApexPdpPolicyForwarderParameterGroup(builder);
+ parameterGroup.setName(GROUP_NAME);
+ ParameterService.register(parameterGroup);
+ }
+
+ /**
+ * Tear down.
+ */
+ @AfterClass
+ public static void tearDown() {
+ ParameterService.deregister(GROUP_NAME);
+ }
+
+ @Test
+ public void testForwardPolicy() throws ApexException, FileNotFoundException, IOException, PolicyForwardingException,
+ NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
+
+ final FileInputStream fis = new FileInputStream(File.createTempFile("policy1", null));
+ final Collection<Policy> policies = new ArrayList<>();
+ final ApexPdpPolicyForwarder forwarder = new ApexPdpPolicyForwarder();
+ forwarder.configure(GROUP_NAME);
+
+ final Field forwarderField = forwarder.getClass().getDeclaredField("engineServiceFacade");
+ forwarderField.setAccessible(true);
+ forwarderField.set(forwarder, engineServiceFacade);
+
+ final ApexPdpPolicy policy = new ApexPdpPolicy("policy", fis);
+ policies.add(policy);
+
+ try {
+ forwarder.forward(policies);
+ verify(engineServiceFacade, times(1)).init();
+ verify(engineServiceFacade, times(1)).deployModel("policy", fis, IGNORE_CONFLICTS, FORCE_UPDATE);
+ } catch (final Exception exp) {
+ fail("Test must not throw an exception");
+ }
+ }
+
+ @Test
+ public void testForwardPolicyError()
+ throws ApexException, FileNotFoundException, IOException, PolicyForwardingException, NoSuchFieldException,
+ SecurityException, IllegalArgumentException, IllegalAccessException {
+
+ final FileInputStream fis = new FileInputStream(File.createTempFile("policy1", null));
+ final Collection<Policy> policies = new ArrayList<>();
+ final ApexPdpPolicyForwarder forwarder = new ApexPdpPolicyForwarder();
+ forwarder.configure(GROUP_NAME);
+
+ Mockito.doThrow(new ApexException("Failed")).when(engineServiceFacade).deployModel("policy1", fis,
+ IGNORE_CONFLICTS, FORCE_UPDATE);
+
+ final Field decodersField = forwarder.getClass().getDeclaredField("engineServiceFacade");
+ decodersField.setAccessible(true);
+ decodersField.set(forwarder, engineServiceFacade);
+
+ final ApexPdpPolicy policy1 = new ApexPdpPolicy("policy1", fis);
+ policies.add(policy1);
+
+ try {
+ forwarder.forward(policies);
+ fail("Test must throw an exception");
+ } catch (final Exception exp) {
+ assertTrue(exp.getMessage().contains("Error sending policy to apex-pdp engine"));
+ }
+
+ }
+
+ @Test
+ public void testForwardMoreThanOnePolicy()
+ throws ApexException, FileNotFoundException, IOException, PolicyForwardingException, NoSuchFieldException,
+ SecurityException, IllegalArgumentException, IllegalAccessException {
+
+ final Collection<Policy> policies = new ArrayList<>();
+ final ApexPdpPolicyForwarder forwarder = new ApexPdpPolicyForwarder();
+ forwarder.configure(GROUP_NAME);
+
+ final Field forwarderField = forwarder.getClass().getDeclaredField("engineServiceFacade");
+ forwarderField.setAccessible(true);
+ forwarderField.set(forwarder, engineServiceFacade);
+
+ final ApexPdpPolicy policy1 =
+ new ApexPdpPolicy("policy1", new FileInputStream(File.createTempFile("policy1", null)));
+ policies.add(policy1);
+
+ final ApexPdpPolicy policy2 =
+ new ApexPdpPolicy("policy2", new FileInputStream(File.createTempFile("policy2", null)));
+ policies.add(policy2);
+
+ try {
+ forwarder.forward(policies);
+ fail("Test must throw an exception");
+ } catch (final Exception exp) {
+ assertTrue(exp.getMessage().contains("More than one apex policy cannot be forwarded to an apex engine"));
+ }
+ }
+
+ @Test
+ public void testForwardUnsupportedPolicy()
+ throws ApexException, FileNotFoundException, IOException, PolicyForwardingException, NoSuchFieldException,
+ SecurityException, IllegalArgumentException, IllegalAccessException {
+
+ final Collection<Policy> policies = new ArrayList<>();
+ final ApexPdpPolicyForwarder forwarder = new ApexPdpPolicyForwarder();
+ forwarder.configure(GROUP_NAME);
+
+ final Field forwarderField = forwarder.getClass().getDeclaredField("engineServiceFacade");
+ forwarderField.setAccessible(true);
+ forwarderField.set(forwarder, engineServiceFacade);
+
+ final Policy policy = new UnsupportedPolicy();
+ policies.add(policy);
+
+ try {
+ forwarder.forward(policies);
+ fail("Test must throw an exception");
+ } catch (final Exception exp) {
+ assertTrue(exp.getMessage().contains("Ignoring the policy as it is not an apex-pdp policy"));
+ }
+ }
+
+ class UnsupportedPolicy implements Policy {
+
+ @Override
+ public String getPolicyName() {
+ return "unsupported";
+ }
+
+ @Override
+ public String getPolicyType() {
+ return "unsupported";
+ }
+ }
+}