diff options
author | Ram Krishna Verma <ram_krishna.verma@bell.ca> | 2020-03-02 15:27:21 -0500 |
---|---|---|
committer | Ram Krishna Verma <ram_krishna.verma@bell.ca> | 2020-03-03 09:42:51 -0500 |
commit | ccc3e28512988345e640097381d2ae0be46fe986 (patch) | |
tree | e80d52e8d27c1492df9807e94296d79ce567fa1c /plugins/forwarding-plugins/src/test/java | |
parent | 7b63879e6837a0242ca0d68540830031c55b4ed6 (diff) |
Remove policy/engine & apex dependency from distribution
Issue-ID: POLICY-2404
Change-Id: Ia3084c55ea65a3119354079d9afd3ee35689b7d4
Signed-off-by: Ram Krishna Verma <ram_krishna.verma@bell.ca>
Diffstat (limited to 'plugins/forwarding-plugins/src/test/java')
11 files changed, 8 insertions, 654 deletions
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 deleted file mode 100644 index fc0fe3db..00000000 --- a/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/apex/pdp/ApexPdpPolicyForwarderParameterGroupTest.java +++ /dev/null @@ -1,70 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2019 Nordix Foundation. - * ================================================================================ - * 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; -import org.onap.policy.distribution.forwarding.xacml.pdp.testclasses.CommonTestData; - -/** - * Class to perform unit test of {@link ApexPdpPolicyForwarderParameterGroup}. - * - * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com) - */ -public class ApexPdpPolicyForwarderParameterGroupTest { - - @Test - public void testValidParameters() { - final ApexPdpPolicyForwarderParameterGroup configurationParameters = CommonTestData - .getPolicyForwarderParameters("src/test/resources/parameters/ApexPdpPolicyForwarderParameters.json", - ApexPdpPolicyForwarderParameterGroup.class); - assertEquals(ApexPdpPolicyForwarderParameterGroup.class.getSimpleName(), 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 testInvalidParameters() { - final ApexPdpPolicyForwarderParameterGroup configurationParameters = - CommonTestData.getPolicyForwarderParameters( - "src/test/resources/parameters/ApexPdpPolicyForwarderParametersInvalid.json", - ApexPdpPolicyForwarderParameterGroup.class); - - assertEquals(ValidationStatus.INVALID, configurationParameters.validate().getStatus()); - } - - @Test - public void testEmptyParameters() { - final ApexPdpPolicyForwarderParameterGroup configurationParameters = - CommonTestData.getPolicyForwarderParameters("src/test/resources/parameters/EmptyParameters.json", - ApexPdpPolicyForwarderParameterGroup.class); - - 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 deleted file mode 100644 index 7d6743e1..00000000 --- a/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/apex/pdp/ApexPdpPolicyForwarderTest.java +++ /dev/null @@ -1,204 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2019 Nordix Foundation. - * ================================================================================ - * 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.Matchers.anyObject; -import static org.mockito.Matchers.eq; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; - -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.forwarding.xacml.pdp.testclasses.CommonTestData; -import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity; -import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; - -/** - * Class to perform unit test of {@link ApexPdpPolicyForwarder}. - * - * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com) - */ -@RunWith(MockitoJUnitRunner.class) -public class ApexPdpPolicyForwarderTest { - - 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 ParameterGroup parameterGroup = CommonTestData.getPolicyForwarderParameters( - "src/test/resources/parameters/ApexPdpPolicyForwarderParameters.json", - ApexPdpPolicyForwarderParameterGroup.class); - - 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 Collection<ToscaEntity> 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); - - createPolicy(policies, "policy", "APEX", "Sample Policy of apex"); - - try { - forwarder.forward(policies); - verify(engineServiceFacade, times(1)).init(); - verify(engineServiceFacade, times(1)).deployModel(eq("policy"), anyObject(), eq(IGNORE_CONFLICTS), - eq(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 Collection<ToscaEntity> policies = new ArrayList<>(); - final ApexPdpPolicyForwarder forwarder = new ApexPdpPolicyForwarder(); - forwarder.configure(GROUP_NAME); - - Mockito.doThrow(new ApexException("Failed")).when(engineServiceFacade).deployModel(eq("policy1"), anyObject(), - eq(IGNORE_CONFLICTS), eq(FORCE_UPDATE)); - - final Field decodersField = forwarder.getClass().getDeclaredField("engineServiceFacade"); - decodersField.setAccessible(true); - decodersField.set(forwarder, engineServiceFacade); - - createPolicy(policies, "policy1", "APEX", "Sample Policy of apex"); - - 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<ToscaEntity> 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); - - createPolicy(policies, "policy1", "APEX", "Sample Policy of apex"); - createPolicy(policies, "policy2", "APEX", "Sample Policy of apex"); - - 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<ToscaEntity> 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 ToscaEntity 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 of type ToscaPolicy")); - } - } - - class UnsupportedPolicy extends ToscaEntity { - - @Override - public String getName() { - return "unsupported"; - } - } - - private void createPolicy(final Collection<ToscaEntity> policies, final String name, final String type, - final String description) { - final ToscaPolicy policy = new ToscaPolicy(); - policy.setName(name); - policy.setType(type); - policy.setDescription(description); - policies.add(policy); - } -} diff --git a/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/lifecycle/api/LifecycleApiForwarderParametersTest.java b/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/lifecycle/api/LifecycleApiForwarderParametersTest.java index fd8422d6..0f010bfc 100644 --- a/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/lifecycle/api/LifecycleApiForwarderParametersTest.java +++ b/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/lifecycle/api/LifecycleApiForwarderParametersTest.java @@ -26,7 +26,7 @@ import static org.junit.Assert.assertTrue; import org.junit.Test; import org.onap.policy.common.parameters.ValidationStatus; -import org.onap.policy.distribution.forwarding.xacml.pdp.testclasses.CommonTestData; +import org.onap.policy.distribution.forwarding.testclasses.CommonTestData; /** * Class to perform unit test of {@link LifecycleApiForwarderParameters}. diff --git a/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/lifecycle/api/LifecycleApiPolicyForwarderTest.java b/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/lifecycle/api/LifecycleApiPolicyForwarderTest.java index 4d837cdf..7d84657a 100644 --- a/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/lifecycle/api/LifecycleApiPolicyForwarderTest.java +++ b/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/lifecycle/api/LifecycleApiPolicyForwarderTest.java @@ -35,8 +35,8 @@ import org.onap.policy.common.utils.coder.StandardCoder; import org.onap.policy.common.utils.network.NetworkUtil; import org.onap.policy.common.utils.resources.ResourceUtils; import org.onap.policy.distribution.forwarding.PolicyForwardingException; -import org.onap.policy.distribution.forwarding.xacml.pdp.testclasses.CommonTestData; -import org.onap.policy.distribution.forwarding.xacml.pdp.testclasses.LifecycleApiSimulatorMain; +import org.onap.policy.distribution.forwarding.testclasses.CommonTestData; +import org.onap.policy.distribution.forwarding.testclasses.LifecycleApiSimulatorMain; import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; diff --git a/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/xacml/pdp/testclasses/CommonTestData.java b/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/testclasses/CommonTestData.java index 386526e5..9da1b613 100644 --- a/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/xacml/pdp/testclasses/CommonTestData.java +++ b/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/testclasses/CommonTestData.java @@ -18,7 +18,7 @@ * ============LICENSE_END========================================================= */ -package org.onap.policy.distribution.forwarding.xacml.pdp.testclasses; +package org.onap.policy.distribution.forwarding.testclasses; import java.io.File; diff --git a/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/xacml/pdp/testclasses/DummyDecoder.java b/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/testclasses/DummyDecoder.java index f2fb144a..b40aabf8 100644 --- a/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/xacml/pdp/testclasses/DummyDecoder.java +++ b/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/testclasses/DummyDecoder.java @@ -19,7 +19,7 @@ * ============LICENSE_END========================================================= */ -package org.onap.policy.distribution.forwarding.xacml.pdp.testclasses; +package org.onap.policy.distribution.forwarding.testclasses; import java.util.Collection; diff --git a/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/xacml/pdp/testclasses/DummyReceptionHandler.java b/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/testclasses/DummyReceptionHandler.java index f9bde3c3..b3cf489c 100644 --- a/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/xacml/pdp/testclasses/DummyReceptionHandler.java +++ b/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/testclasses/DummyReceptionHandler.java @@ -19,7 +19,7 @@ * ============LICENSE_END========================================================= */ -package org.onap.policy.distribution.forwarding.xacml.pdp.testclasses; +package org.onap.policy.distribution.forwarding.testclasses; import org.onap.policy.distribution.reception.handling.AbstractReceptionHandler; diff --git a/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/xacml/pdp/testclasses/LifecycleApiSimulatorMain.java b/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/testclasses/LifecycleApiSimulatorMain.java index 9aa68876..07c4b6c6 100644 --- a/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/xacml/pdp/testclasses/LifecycleApiSimulatorMain.java +++ b/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/testclasses/LifecycleApiSimulatorMain.java @@ -18,7 +18,7 @@ * ============LICENSE_END========================================================= */ -package org.onap.policy.distribution.forwarding.xacml.pdp.testclasses; +package org.onap.policy.distribution.forwarding.testclasses; import org.onap.policy.common.endpoints.http.server.RestServer; import org.onap.policy.common.endpoints.parameters.RestServerParameters; diff --git a/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/xacml/pdp/testclasses/LifecycycleApiSimulatorEndpoint.java b/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/testclasses/LifecycycleApiSimulatorEndpoint.java index f07605f0..4d7d2600 100644 --- a/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/xacml/pdp/testclasses/LifecycycleApiSimulatorEndpoint.java +++ b/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/testclasses/LifecycycleApiSimulatorEndpoint.java @@ -18,7 +18,7 @@ * ============LICENSE_END========================================================= */ -package org.onap.policy.distribution.forwarding.xacml.pdp.testclasses; +package org.onap.policy.distribution.forwarding.testclasses; import io.swagger.annotations.ApiParam; diff --git a/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/xacml/pdp/engine/XacmlPdpPolicyForwarderParameterGroupTest.java b/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/xacml/pdp/engine/XacmlPdpPolicyForwarderParameterGroupTest.java deleted file mode 100644 index 6e98b225..00000000 --- a/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/xacml/pdp/engine/XacmlPdpPolicyForwarderParameterGroupTest.java +++ /dev/null @@ -1,74 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2019 Nordix Foundation. - * ================================================================================ - * 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.xacml.pdp.engine; - -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; -import org.onap.policy.distribution.forwarding.xacml.pdp.XacmlPdpPolicyForwarderParameterGroup; -import org.onap.policy.distribution.forwarding.xacml.pdp.testclasses.CommonTestData; - -public class XacmlPdpPolicyForwarderParameterGroupTest { - - private static final String CLIENT_AUTH = "myClientAuth"; - private static final String PASSWORD = "myPassword"; - private static final String USER = "myUser"; - private static final int PORT = 1234; - private static final String HOST_NAME = "10.10.10.10"; - - @Test - public void testValidParameters() { - final XacmlPdpPolicyForwarderParameterGroup configurationParameters = CommonTestData - .getPolicyForwarderParameters("src/test/resources/parameters/XacmlPdpPolicyForwarderParameters.json", - XacmlPdpPolicyForwarderParameterGroup.class); - - assertTrue(configurationParameters.isUseHttps()); - assertEquals(HOST_NAME, configurationParameters.getHostname()); - assertEquals(PORT, configurationParameters.getPort()); - assertEquals(USER, configurationParameters.getUserName()); - assertEquals(PASSWORD, configurationParameters.getPassword()); - assertEquals(CLIENT_AUTH, configurationParameters.getClientAuth()); - assertFalse(configurationParameters.isManaged()); - } - - @Test - public void testInvalidParameters() { - final XacmlPdpPolicyForwarderParameterGroup configurationParameters = - CommonTestData.getPolicyForwarderParameters( - "src/test/resources/parameters/XacmlPdpPolicyForwarderParametersInvalid.json", - XacmlPdpPolicyForwarderParameterGroup.class); - - assertEquals(ValidationStatus.INVALID, configurationParameters.validate().getStatus()); - } - - @Test - public void testEmptyParameters() { - final XacmlPdpPolicyForwarderParameterGroup configurationParameters = - CommonTestData.getPolicyForwarderParameters("src/test/resources/parameters/EmptyParameters.json", - XacmlPdpPolicyForwarderParameterGroup.class); - - assertEquals(ValidationStatus.INVALID, configurationParameters.validate().getStatus()); - } -} diff --git a/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/xacml/pdp/engine/XacmlPdpPolicyForwarderTest.java b/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/xacml/pdp/engine/XacmlPdpPolicyForwarderTest.java deleted file mode 100644 index 222614d9..00000000 --- a/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/xacml/pdp/engine/XacmlPdpPolicyForwarderTest.java +++ /dev/null @@ -1,298 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved. - * Modifications Copyright (C) 2019 Nordix Foundation. - * ================================================================================ - * 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.xacml.pdp.engine; - -import static org.junit.Assert.assertSame; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.argThat; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; - -import javax.ws.rs.client.Entity; -import javax.ws.rs.core.Response; - -import org.junit.BeforeClass; -import org.junit.Test; -import org.mockito.ArgumentMatcher; -import org.onap.policy.api.PolicyParameters; -import org.onap.policy.api.PushPolicyParameters; -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.endpoints.http.client.HttpClientConfigException; -import org.onap.policy.common.endpoints.http.client.HttpClientFactory; -import org.onap.policy.common.parameters.ParameterGroup; -import org.onap.policy.common.parameters.ParameterService; -import org.onap.policy.distribution.forwarding.xacml.pdp.XacmlPdpPolicyForwarder; -import org.onap.policy.distribution.forwarding.xacml.pdp.XacmlPdpPolicyForwarderParameterGroup; -import org.onap.policy.distribution.forwarding.xacml.pdp.testclasses.CommonTestData; -import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity; -import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; - -public class XacmlPdpPolicyForwarderTest { - - private static final BusTopicParams BUS_TOPIC_PARAMS = BusTopicParams.builder().useHttps(true) - .hostname("10.10.10.10").port(1234).userName("myUser").password("myPassword").managed(false).build(); - private static final String CLIENT_AUTH = "ClientAuth"; - private static final String CLIENT_AUTH_VALUE = "myClientAuth"; - private static final String PDP_GROUP_VALUE = "myPdpGroup"; - private HashMap<String, Object> headers = new HashMap<>(); - private BusTopicParamsMatcher matcher = new BusTopicParamsMatcher(BUS_TOPIC_PARAMS); - - /** - * Set up. - */ - @BeforeClass - public static void setUp() { - final ParameterGroup parameterGroup = CommonTestData.getPolicyForwarderParameters( - "src/test/resources/parameters/XacmlPdpPolicyForwarderParameters.json", - XacmlPdpPolicyForwarderParameterGroup.class); - parameterGroup.setName("xacmlPdpConfiguration"); - ParameterService.register(parameterGroup); - } - - @Test - public void testForwardPolicy() throws Exception { - - final HttpClient httpClientMock = mock(HttpClient.class); - headers.put(CLIENT_AUTH, CLIENT_AUTH_VALUE); - when(httpClientMock.put(eq("createPolicy"), any(), eq(headers))).thenReturn(Response.ok().build()); - when(httpClientMock.put(eq("pushPolicy"), any(), eq(headers))).thenReturn(Response.ok().build()); - - final HttpClientFactory httpClientFactoryMock = mock(HttpClientFactory.class); - when(httpClientFactoryMock.build(argThat(matcher))).thenReturn(httpClientMock); - - final XacmlPdpPolicyForwarder forwarder = new MyXacmlPdpPolicyForwarder(httpClientFactoryMock); - forwarder.configure("xacmlPdpConfiguration"); - - final Collection<ToscaEntity> policies = new ArrayList<>(); - - final ToscaPolicy policy1 = createPolicy(policies, "policy1", "optimization"); - - final ToscaEntity policy2 = new UnsupportedPolicy(); - policies.add(policy2); - - final ToscaPolicy policy3 = createPolicy(policies, "policy3", "optimization"); - - forwarder.forward(policies); - - verify(httpClientMock).put(eq("createPolicy"), argThat(new PolicyParametersEntityMatcher(policy1)), - eq(headers)); - verify(httpClientMock).put(eq("createPolicy"), argThat(new PolicyParametersEntityMatcher(policy3)), - eq(headers)); - verify(httpClientMock).put(eq("pushPolicy"), argThat(new PushPolicyParametersEntityMatcher(policy1)), - eq(headers)); - verify(httpClientMock).put(eq("pushPolicy"), argThat(new PushPolicyParametersEntityMatcher(policy3)), - eq(headers)); - } - - @Test - public void testForwardPolicy_CreateFailsPushNotInvoked() throws Exception { - - final HttpClient httpClientMock = mock(HttpClient.class); - headers.put(CLIENT_AUTH, CLIENT_AUTH_VALUE); - when(httpClientMock.put(eq("createPolicy"), any(), eq(headers))).thenReturn(Response.status(400).build()); - when(httpClientMock.put(eq("pushPolicy"), any(), eq(headers))).thenReturn(Response.ok().build()); - - final HttpClientFactory httpClientFactoryMock = mock(HttpClientFactory.class); - when(httpClientFactoryMock.build(argThat(matcher))).thenReturn(httpClientMock); - - final XacmlPdpPolicyForwarder forwarder = new MyXacmlPdpPolicyForwarder(httpClientFactoryMock); - forwarder.configure("xacmlPdpConfiguration"); - - final Collection<ToscaEntity> policies = new ArrayList<>(); - final ToscaPolicy policy = createPolicy(policies, "policy", "optimization"); - forwarder.forward(policies); - - verify(httpClientMock).put(eq("createPolicy"), argThat(new PolicyParametersEntityMatcher(policy)), eq(headers)); - verify(httpClientMock, times(0)).put(eq("pushPolicy"), any(), any()); - } - - @Test - public void testForwardPolicy_PushFails() throws Exception { - - final HttpClient httpClientMock = mock(HttpClient.class); - headers.put(CLIENT_AUTH, CLIENT_AUTH_VALUE); - when(httpClientMock.put(eq("createPolicy"), any(), eq(headers))).thenReturn(Response.ok().build()); - when(httpClientMock.put(eq("pushPolicy"), any(), eq(headers))).thenReturn(Response.status(400).build()); - - final HttpClientFactory httpClientFactoryMock = mock(HttpClientFactory.class); - when(httpClientFactoryMock.build(argThat(matcher))).thenReturn(httpClientMock); - - final XacmlPdpPolicyForwarder forwarder = new MyXacmlPdpPolicyForwarder(httpClientFactoryMock); - forwarder.configure("xacmlPdpConfiguration"); - - final Collection<ToscaEntity> policies = new ArrayList<>(); - final ToscaPolicy policy = createPolicy(policies, "policy", "optimization"); - forwarder.forward(policies); - - verify(httpClientMock).put(eq("createPolicy"), argThat(new PolicyParametersEntityMatcher(policy)), eq(headers)); - verify(httpClientMock).put(eq("pushPolicy"), argThat(new PushPolicyParametersEntityMatcher(policy)), - eq(headers)); - } - - @Test - public void testForwardPolicy_HttpClientInitFailureForPolicyCreate() throws Exception { - - final HttpClient httpClientMock = mock(HttpClient.class); - headers.put(CLIENT_AUTH, CLIENT_AUTH_VALUE); - when(httpClientMock.put(eq("createPolicy"), any(), eq(headers))).thenReturn(Response.ok().build()); - when(httpClientMock.put(eq("pushPolicy"), any(), eq(headers))).thenReturn(Response.status(400).build()); - - final HttpClientFactory httpClientFactoryMock = mock(HttpClientFactory.class); - when(httpClientFactoryMock.build(argThat(matcher))).thenThrow(new HttpClientConfigException()); - - final XacmlPdpPolicyForwarder forwarder = new MyXacmlPdpPolicyForwarder(httpClientFactoryMock); - forwarder.configure("xacmlPdpConfiguration"); - - final Collection<ToscaEntity> policies = new ArrayList<>(); - final ToscaPolicy policy = createPolicy(policies, "policy", "optimization"); - forwarder.forward(policies); - - assertSame(policy, policies.iterator().next()); - - verify(httpClientMock, times(0)).put(eq("createPolicy"), any(), any()); - verify(httpClientMock, times(0)).put(eq("pushPolicy"), any(), any()); - } - - @Test - public void testForwardPolicy_HttpClientInitFailureForPolicyPush() throws Exception { - - final HttpClient httpClientMock = mock(HttpClient.class); - headers.put(CLIENT_AUTH, CLIENT_AUTH_VALUE); - when(httpClientMock.put(eq("createPolicy"), any(), eq(headers))).thenReturn(Response.ok().build()); - when(httpClientMock.put(eq("pushPolicy"), any(), eq(headers))).thenReturn(Response.status(400).build()); - - final HttpClientFactory httpClientFactoryMock = mock(HttpClientFactory.class); - when(httpClientFactoryMock.build(argThat(matcher))).thenReturn(httpClientMock) - .thenThrow(new HttpClientConfigException()); - - final XacmlPdpPolicyForwarder forwarder = new MyXacmlPdpPolicyForwarder(httpClientFactoryMock); - forwarder.configure("xacmlPdpConfiguration"); - - final Collection<ToscaEntity> policies = new ArrayList<>(); - final ToscaPolicy policy = createPolicy(policies, "policy", "optimization"); - forwarder.forward(policies); - - verify(httpClientMock).put(eq("createPolicy"), argThat(new PolicyParametersEntityMatcher(policy)), eq(headers)); - verify(httpClientMock, times(0)).put(eq("pushPolicy"), any(), any()); - } - - class BusTopicParamsMatcher implements ArgumentMatcher<BusTopicParams> { - - private BusTopicParams busTopicParams; - - BusTopicParamsMatcher(final BusTopicParams busTopicParams) { - this.busTopicParams = busTopicParams; - } - - @Override - public boolean matches(final BusTopicParams arg0) { - if (arg0 instanceof BusTopicParams) { - final BusTopicParams toCompareTo = (BusTopicParams) arg0; - return toCompareTo.isUseHttps() == busTopicParams.isUseHttps() - && toCompareTo.getHostname().equals(busTopicParams.getHostname()) - && toCompareTo.getPort() == busTopicParams.getPort() - && toCompareTo.getUserName().equals(busTopicParams.getUserName()) - && toCompareTo.getPassword().equals(busTopicParams.getPassword()) - && toCompareTo.isManaged() == busTopicParams.isManaged(); - } - return false; - } - } - - class PolicyParametersEntityMatcher implements ArgumentMatcher<Entity<PolicyParameters>> { - - private ToscaPolicy policy; - - PolicyParametersEntityMatcher(final ToscaPolicy policy) { - this.policy = policy; - } - - @SuppressWarnings("unchecked") - @Override - public boolean matches(final Entity<PolicyParameters> arg0) { - if (arg0 instanceof Entity) { - final PolicyParameters toCompareTo = ((Entity<PolicyParameters>) arg0).getEntity(); - return toCompareTo.getPolicyName().equals(policy.getName()); - } - return false; - } - } - - class PushPolicyParametersEntityMatcher implements ArgumentMatcher<Entity<PushPolicyParameters>> { - - private ToscaPolicy policy; - - PushPolicyParametersEntityMatcher(final ToscaPolicy policy) { - this.policy = policy; - } - - @SuppressWarnings("unchecked") - @Override - public boolean matches(final Entity<PushPolicyParameters> arg0) { - if (arg0 instanceof Entity) { - final PushPolicyParameters toCompareTo = ((Entity<PushPolicyParameters>) arg0).getEntity(); - return toCompareTo.getPolicyName().equals(policy.getName()) - && toCompareTo.getPolicyType().equals(policy.getType()) - && toCompareTo.getPdpGroup().equals(PDP_GROUP_VALUE); - } - return false; - } - } - - class UnsupportedPolicy extends ToscaEntity { - - @Override - public String getName() { - return "unsupported"; - } - } - - private class MyXacmlPdpPolicyForwarder extends XacmlPdpPolicyForwarder { - private HttpClientFactory factory; - - public MyXacmlPdpPolicyForwarder(final HttpClientFactory httpClientFactory) { - this.factory = httpClientFactory; - } - - @Override - protected HttpClientFactory getHttpClientFactory() { - return this.factory; - } - } - - private ToscaPolicy createPolicy(final Collection<ToscaEntity> policies, final String name, final String type) { - final ToscaPolicy policy1 = new ToscaPolicy(); - policy1.setName(name); - policy1.setType(type); - policies.add(policy1); - return policy1; - } -} |