From 82da1e9fa73e6a19455ea979bbf084aeed43af90 Mon Sep 17 00:00:00 2001 From: "a.sreekumar" Date: Wed, 16 Oct 2019 11:46:21 +0100 Subject: Resolve mapping between TOSCA policies and APEX policy models Change-Id: Ifaedc5074bcc51a5d495e342feae89b6a2aac1cf Issue-ID: POLICY-1626 Signed-off-by: a.sreekumar --- .../services/onappf/TestApexStarterActivator.java | 2 +- .../apex/services/onappf/TestApexStarterMain.java | 5 +- .../services/onappf/comm/TestListenerUtils.java | 99 ++++++++++++++++ .../onappf/comm/TestPdpStateChangeListener.java | 119 +++++++++++--------- .../onappf/comm/TestPdpUpdateListener.java | 124 ++++++++++++++++----- .../dummyclasses/DummyStateFinalizerExecutor.java | 42 +++++++ .../parameters/dummyclasses/DummyTaskExecutor.java | 58 ++++++++++ .../dummyclasses/DummyTaskSelectExecutor.java | 49 ++++++++ .../SuperDooperExecutorParameters.java | 39 +++++++ 9 files changed, 450 insertions(+), 87 deletions(-) create mode 100644 services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/comm/TestListenerUtils.java create mode 100644 services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/parameters/dummyclasses/DummyStateFinalizerExecutor.java create mode 100644 services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/parameters/dummyclasses/DummyTaskExecutor.java create mode 100644 services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/parameters/dummyclasses/DummyTaskSelectExecutor.java create mode 100644 services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/parameters/dummyclasses/SuperDooperExecutorParameters.java (limited to 'services/services-onappf/src/test/java/org') diff --git a/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/TestApexStarterActivator.java b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/TestApexStarterActivator.java index c8f889fde..77ffa5317 100644 --- a/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/TestApexStarterActivator.java +++ b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/TestApexStarterActivator.java @@ -56,7 +56,7 @@ public class TestApexStarterActivator { @Before public void setUp() throws Exception { Registry.newRegistry(); - final String[] apexStarterConfigParameters = { "-c", "src/test/resources/ApexStarterConfigParameters.json"}; + final String[] apexStarterConfigParameters = { "-c", "src/test/resources/ApexStarterConfigParametersNoop.json"}; final ApexStarterCommandLineArguments arguments = new ApexStarterCommandLineArguments(apexStarterConfigParameters); final ApexStarterParameterGroup parGroup = new ApexStarterParameterHandler().getParameters(arguments); diff --git a/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/TestApexStarterMain.java b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/TestApexStarterMain.java index 68d30258b..2fcfe886f 100644 --- a/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/TestApexStarterMain.java +++ b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/TestApexStarterMain.java @@ -27,9 +27,6 @@ import static org.junit.Assert.assertTrue; import org.junit.After; import org.junit.Before; import org.junit.Test; -import org.onap.policy.apex.services.onappf.ApexStarterActivator; -import org.onap.policy.apex.services.onappf.ApexStarterConstants; -import org.onap.policy.apex.services.onappf.ApexStarterMain; import org.onap.policy.apex.services.onappf.exception.ApexStarterException; import org.onap.policy.apex.services.onappf.parameters.CommonTestData; import org.onap.policy.common.utils.services.Registry; @@ -67,7 +64,7 @@ public class TestApexStarterMain { @Test public void testApexStarter() throws ApexStarterException { - final String[] apexStarterConfigParameters = { "-c", "src/test/resources/ApexStarterConfigParameters.json"}; + final String[] apexStarterConfigParameters = { "-c", "src/test/resources/ApexStarterConfigParametersNoop.json"}; apexStarter = new ApexStarterMain(apexStarterConfigParameters); assertTrue(apexStarter.getParameters().isValid()); assertEquals(CommonTestData.APEX_STARTER_GROUP_NAME, apexStarter.getParameters().getName()); diff --git a/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/comm/TestListenerUtils.java b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/comm/TestListenerUtils.java new file mode 100644 index 000000000..78d9d769b --- /dev/null +++ b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/comm/TestListenerUtils.java @@ -0,0 +1,99 @@ +/*- + * ============LICENSE_START======================================================= + * 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.apex.services.onappf.comm; + +import com.google.gson.JsonObject; +import java.io.File; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import org.onap.policy.common.utils.coder.CoderException; +import org.onap.policy.common.utils.coder.StandardCoder; +import org.onap.policy.models.pdp.concepts.PdpStateChange; +import org.onap.policy.models.pdp.concepts.PdpStatus; +import org.onap.policy.models.pdp.concepts.PdpUpdate; +import org.onap.policy.models.pdp.enums.PdpState; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; + +public class TestListenerUtils { + + /** + * Method to create PdpUpdate message from the arguments passed. + * + * @param pdpStatus pdp status + * @param toscaPolicies list of tosca policies + * + * @return PdpUpdate message + */ + public static PdpUpdate createPdpUpdateMsg(final PdpStatus pdpStatus, List toscaPolicies ) { + final PdpUpdate pdpUpdateMsg = new PdpUpdate(); + pdpUpdateMsg.setDescription("dummy pdp status for test"); + pdpUpdateMsg.setPdpGroup("pdpGroup"); + pdpUpdateMsg.setPdpSubgroup("pdpSubgroup"); + pdpUpdateMsg.setName(pdpStatus.getName()); + pdpUpdateMsg.setPdpHeartbeatIntervalMs(Long.valueOf(3000)); + pdpUpdateMsg.setPolicies(toscaPolicies); + return pdpUpdateMsg; + } + + /** + * Method to create ToscaPolicy using the arguments passed. + * + * @param policyName the name of the policy + * @param policyVersion the version of the policy + * @param policyFilePath the path of the policy content + * + * @return PdpUpdate message + * @throws CoderException exception while reading the file to object + */ + public static ToscaPolicy createToscaPolicy(String policyName, String policyVersion, String policyFilePath) + throws CoderException { + final ToscaPolicy toscaPolicy = new ToscaPolicy(); + toscaPolicy.setType("apexpolicytype"); + toscaPolicy.setVersion(policyVersion); + toscaPolicy.setName(policyName); + final Map propertiesMap = new LinkedHashMap<>(); + Object properties = new StandardCoder().decode(new File(policyFilePath), JsonObject.class); + propertiesMap.put("content", properties); + toscaPolicy.setProperties(propertiesMap); + return toscaPolicy; + } + + /** + * Method to create PdpStateChange message from the arguments passed. + * + * @param state the new pdp state + * @param pdpGroup name of the pdpGroup + * @param pdpSubgroup name of the pdpSubGroup + * @param name the name of the message + * + * @return PdpStateChange message + */ + public static PdpStateChange createPdpStateChangeMsg(PdpState state, String pdpGroup, String pdpSubgroup, + String name) { + final PdpStateChange pdpStateChangeMsg = new PdpStateChange(); + pdpStateChangeMsg.setState(state); + pdpStateChangeMsg.setPdpGroup(pdpGroup); + pdpStateChangeMsg.setPdpSubgroup(pdpSubgroup); + pdpStateChangeMsg.setName(name); + return pdpStateChangeMsg; + } +} diff --git a/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/comm/TestPdpStateChangeListener.java b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/comm/TestPdpStateChangeListener.java index c475b50ff..7f7de3b42 100644 --- a/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/comm/TestPdpStateChangeListener.java +++ b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/comm/TestPdpStateChangeListener.java @@ -23,16 +23,15 @@ package org.onap.policy.apex.services.onappf.comm; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import java.io.ByteArrayOutputStream; import java.io.FileNotFoundException; import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Paths; +import java.io.OutputStream; +import java.io.PrintStream; import java.util.ArrayList; -import java.util.LinkedHashMap; import java.util.List; -import java.util.Map; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -40,9 +39,11 @@ import org.onap.policy.apex.services.onappf.ApexStarterActivator; import org.onap.policy.apex.services.onappf.ApexStarterCommandLineArguments; import org.onap.policy.apex.services.onappf.ApexStarterConstants; import org.onap.policy.apex.services.onappf.exception.ApexStarterException; +import org.onap.policy.apex.services.onappf.handler.ApexEngineHandler; import org.onap.policy.apex.services.onappf.parameters.ApexStarterParameterGroup; import org.onap.policy.apex.services.onappf.parameters.ApexStarterParameterHandler; import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure; +import org.onap.policy.common.utils.coder.CoderException; import org.onap.policy.common.utils.services.Registry; import org.onap.policy.models.pdp.concepts.PdpStateChange; import org.onap.policy.models.pdp.concepts.PdpStatus; @@ -61,6 +62,8 @@ public class TestPdpStateChangeListener { private static final CommInfrastructure INFRA = CommInfrastructure.NOOP; private static final String TOPIC = "my-topic"; private ApexStarterActivator activator; + private ApexEngineHandler apexEngineHandler; + private PrintStream stdout = System.out; /** * Method for setup before each test. @@ -74,7 +77,7 @@ public class TestPdpStateChangeListener { pdpUpdateMessageListener = new PdpUpdateListener(); pdpStateChangeListener = new PdpStateChangeListener(); Registry.newRegistry(); - final String[] apexStarterConfigParameters = { "-c", "src/test/resources/ApexStarterConfigParameters.json" }; + final String[] apexStarterConfigParameters = {"-c", "src/test/resources/ApexStarterConfigParametersNoop.json"}; final ApexStarterCommandLineArguments arguments = new ApexStarterCommandLineArguments(); ApexStarterParameterGroup parameterGroup; // The arguments return a string if there is a message to print and we should @@ -101,55 +104,25 @@ public class TestPdpStateChangeListener { */ @After public void teardown() throws Exception { - + System.setOut(stdout); + apexEngineHandler = + Registry.getOrDefault(ApexStarterConstants.REG_APEX_ENGINE_HANDLER, ApexEngineHandler.class, null); + if (null != apexEngineHandler && apexEngineHandler.isApexEngineRunning()) { + apexEngineHandler.shutdown(); + } // clear the apex starter activator if (activator != null && activator.isAlive()) { activator.terminate(); } } - /** - * Method to initiate a PdpUpdate. - * - * @param instance the instance id - * @return PdpUpdate the pdp update message - */ - private PdpUpdate performPdpUpdate(final String instance) { - final PdpUpdate pdpUpdateMsg = new PdpUpdate(); - pdpUpdateMsg.setDescription("dummy pdp status for test"); - pdpUpdateMsg.setPdpGroup("pdpGroup"); - pdpUpdateMsg.setPdpSubgroup("pdpSubgroup"); - pdpUpdateMsg.setName(instance); - final ToscaPolicy toscaPolicy = new ToscaPolicy(); - toscaPolicy.setType("apexpolicytype"); - toscaPolicy.setVersion("1.0"); - final Map propertiesMap = new LinkedHashMap<>(); - - String properties; - try { - properties = new String(Files.readAllBytes(Paths.get("src\\test\\resources\\dummyProperties.json")), - StandardCharsets.UTF_8); - propertiesMap.put("content", properties); - } catch (final IOException e) { - propertiesMap.put("content", ""); - } - toscaPolicy.setProperties(propertiesMap); - final List toscaPolicies = new ArrayList(); - toscaPolicies.add(toscaPolicy); - pdpUpdateMsg.setPolicies(toscaPolicies); - pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null, pdpUpdateMsg); - return pdpUpdateMsg; - } - @Test public void testPdpStateChangeMessageListener_passivetopassive() { final PdpStatus pdpStatus = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT); - performPdpUpdate(pdpStatus.getName()); - final PdpStateChange pdpStateChangeMsg = new PdpStateChange(); - pdpStateChangeMsg.setState(PdpState.PASSIVE); - pdpStateChangeMsg.setPdpGroup("pdpGroup"); - pdpStateChangeMsg.setPdpSubgroup("pdpSubgroup"); - pdpStateChangeMsg.setName(pdpStatus.getName()); + pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null, + TestListenerUtils.createPdpUpdateMsg(pdpStatus, new ArrayList())); + PdpStateChange pdpStateChangeMsg = + TestListenerUtils.createPdpStateChangeMsg(PdpState.PASSIVE, "pdpGroup", "pdpSubgroup", pdpStatus.getName()); pdpStateChangeListener.onTopicEvent(INFRA, TOPIC, null, pdpStateChangeMsg); assertEquals(pdpStatus.getState(), pdpStateChangeMsg.getState()); @@ -158,15 +131,57 @@ public class TestPdpStateChangeListener { @Test public void testPdpStateChangeMessageListener_activetoactive() { final PdpStatus pdpStatus = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT); - performPdpUpdate(pdpStatus.getName()); + pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null, + TestListenerUtils.createPdpUpdateMsg(pdpStatus, new ArrayList())); pdpStatus.setState(PdpState.ACTIVE); - final PdpStateChange pdpStateChangeMsg = new PdpStateChange(); - pdpStateChangeMsg.setState(PdpState.ACTIVE); - pdpStateChangeMsg.setPdpGroup("pdpGroup"); - pdpStateChangeMsg.setPdpSubgroup("pdpSubgroup"); - pdpStateChangeMsg.setName(pdpStatus.getName()); + PdpStateChange pdpStateChangeMsg = + TestListenerUtils.createPdpStateChangeMsg(PdpState.ACTIVE, "pdpGroup", "pdpSubgroup", pdpStatus.getName()); pdpStateChangeListener.onTopicEvent(INFRA, TOPIC, null, pdpStateChangeMsg); assertEquals(pdpStatus.getState(), pdpStateChangeMsg.getState()); } + + @Test + public void testPdpStateChangeMessageListener() throws InterruptedException, CoderException { + OutputStream outContent = new ByteArrayOutputStream(); + System.setOut(new PrintStream(outContent)); + final PdpStatus pdpStatus = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT); + pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null, + TestListenerUtils.createPdpUpdateMsg(pdpStatus, new ArrayList())); + PdpStateChange pdpStateChangeMsg = + TestListenerUtils.createPdpStateChangeMsg(PdpState.ACTIVE, "pdpGroup", "pdpSubgroup", pdpStatus.getName()); + pdpStateChangeListener.onTopicEvent(INFRA, TOPIC, null, pdpStateChangeMsg); + assertTrue(outContent.toString().contains("State changed to active. No policies found.")); + + final ToscaPolicy toscaPolicy = + TestListenerUtils.createToscaPolicy("apex policy name", "1.0", "src/test/resources/dummyProperties.json"); + final List toscaPolicies = new ArrayList(); + toscaPolicies.add(toscaPolicy); + final PdpUpdate pdpUpdateMsg = TestListenerUtils.createPdpUpdateMsg(pdpStatus, toscaPolicies); + pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null, pdpUpdateMsg); + assertTrue(outContent.toString().contains("Apex engine started and policies are running.")); + assertEquals(PdpState.ACTIVE, pdpStatus.getState()); + } + + @Test + public void testPdpStateChangeMessageListener_activetopassive() throws InterruptedException, CoderException { + final PdpStatus pdpStatus = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT); + final ToscaPolicy toscaPolicy = + TestListenerUtils.createToscaPolicy("apex policy name", "1.0", "src/test/resources/dummyProperties.json"); + final List toscaPolicies = new ArrayList(); + toscaPolicies.add(toscaPolicy); + final PdpUpdate pdpUpdateMsg = TestListenerUtils.createPdpUpdateMsg(pdpStatus, toscaPolicies); + pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null, pdpUpdateMsg); + PdpStateChange pdpStateChangeMsg = + TestListenerUtils.createPdpStateChangeMsg(PdpState.ACTIVE, "pdpGroup", "pdpSubgroup", pdpStatus.getName()); + pdpStateChangeListener.onTopicEvent(INFRA, TOPIC, null, pdpStateChangeMsg); + pdpStateChangeMsg = + TestListenerUtils.createPdpStateChangeMsg(PdpState.PASSIVE, "pdpGroup", "pdpSubgroup", pdpStatus.getName()); + OutputStream outContent = new ByteArrayOutputStream(); + System.setOut(new PrintStream(outContent)); + pdpStateChangeListener.onTopicEvent(INFRA, TOPIC, null, pdpStateChangeMsg); + final String outString = outContent.toString(); + assertTrue(outString.contains("Apex pdp state changed from Active to Passive.")); + assertEquals(PdpState.PASSIVE, pdpStatus.getState()); + } } diff --git a/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/comm/TestPdpUpdateListener.java b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/comm/TestPdpUpdateListener.java index 700797812..05656af96 100644 --- a/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/comm/TestPdpUpdateListener.java +++ b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/comm/TestPdpUpdateListener.java @@ -2,7 +2,6 @@ * ============LICENSE_START======================================================= * Copyright (C) 2019 Nordix Foundation. * Modifications 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. @@ -23,16 +22,15 @@ package org.onap.policy.apex.services.onappf.comm; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import java.io.ByteArrayOutputStream; import java.io.FileNotFoundException; import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Paths; +import java.io.OutputStream; +import java.io.PrintStream; import java.util.ArrayList; -import java.util.LinkedHashMap; import java.util.List; -import java.util.Map; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -40,13 +38,17 @@ import org.onap.policy.apex.services.onappf.ApexStarterActivator; import org.onap.policy.apex.services.onappf.ApexStarterCommandLineArguments; import org.onap.policy.apex.services.onappf.ApexStarterConstants; import org.onap.policy.apex.services.onappf.exception.ApexStarterException; +import org.onap.policy.apex.services.onappf.handler.ApexEngineHandler; import org.onap.policy.apex.services.onappf.handler.PdpMessageHandler; import org.onap.policy.apex.services.onappf.parameters.ApexStarterParameterGroup; import org.onap.policy.apex.services.onappf.parameters.ApexStarterParameterHandler; import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure; +import org.onap.policy.common.utils.coder.CoderException; import org.onap.policy.common.utils.services.Registry; +import org.onap.policy.models.pdp.concepts.PdpStateChange; import org.onap.policy.models.pdp.concepts.PdpStatus; import org.onap.policy.models.pdp.concepts.PdpUpdate; +import org.onap.policy.models.pdp.enums.PdpState; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; /** @@ -56,9 +58,12 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; */ public class TestPdpUpdateListener { private PdpUpdateListener pdpUpdateMessageListener; + private PdpStateChangeListener pdpStateChangeListener; private static final CommInfrastructure INFRA = CommInfrastructure.NOOP; private static final String TOPIC = "my-topic"; private ApexStarterActivator activator; + private ApexEngineHandler apexEngineHandler; + private PrintStream stdout = System.out; /** * Method for setup before each test. @@ -70,7 +75,7 @@ public class TestPdpUpdateListener { @Before public void setUp() throws ApexStarterException, FileNotFoundException, IOException { Registry.newRegistry(); - final String[] apexStarterConfigParameters = { "-c", "src/test/resources/ApexStarterConfigParameters.json" }; + final String[] apexStarterConfigParameters = {"-c", "src/test/resources/ApexStarterConfigParametersNoop.json"}; final ApexStarterCommandLineArguments arguments = new ApexStarterCommandLineArguments(); ApexStarterParameterGroup parameterGroup; // The arguments return a string if there is a message to print and we should @@ -89,6 +94,7 @@ public class TestPdpUpdateListener { Registry.register(ApexStarterConstants.REG_APEX_STARTER_ACTIVATOR, activator); activator.initialize(); pdpUpdateMessageListener = new PdpUpdateListener(); + pdpStateChangeListener = new PdpStateChangeListener(); } /** @@ -98,7 +104,12 @@ public class TestPdpUpdateListener { */ @After public void teardown() throws Exception { - + System.setOut(stdout); + apexEngineHandler = + Registry.getOrDefault(ApexStarterConstants.REG_APEX_ENGINE_HANDLER, ApexEngineHandler.class, null); + if (null != apexEngineHandler && apexEngineHandler.isApexEngineRunning()) { + apexEngineHandler.shutdown(); + } // clear the apex starter activator if (activator != null && activator.isAlive()) { activator.terminate(); @@ -106,35 +117,88 @@ public class TestPdpUpdateListener { } @Test - public void testPdpUpdateMssageListener() { + public void testPdpUpdateMssageListener() throws CoderException { final PdpStatus pdpStatus = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT); - final PdpUpdate pdpUpdateMsg = new PdpUpdate(); - pdpUpdateMsg.setDescription("dummy pdp status for test"); - pdpUpdateMsg.setPdpGroup("pdpGroup"); - pdpUpdateMsg.setPdpSubgroup("pdpSubgroup"); - pdpUpdateMsg.setName(pdpStatus.getName()); - pdpUpdateMsg.setPdpHeartbeatIntervalMs(Long.valueOf(3000)); - final ToscaPolicy toscaPolicy = new ToscaPolicy(); - toscaPolicy.setType("apexpolicytype"); - toscaPolicy.setVersion("1.0"); - toscaPolicy.setName("apex policy name"); - final Map propertiesMap = new LinkedHashMap<>(); - String properties; - try { - properties = new String(Files.readAllBytes(Paths.get("src\\test\\resources\\dummyProperties.json")), - StandardCharsets.UTF_8); - propertiesMap.put("content", properties); - } catch (final IOException e) { - propertiesMap.put("content", ""); - } - toscaPolicy.setProperties(propertiesMap); + final ToscaPolicy toscaPolicy = + TestListenerUtils.createToscaPolicy("apex policy name", "1.0", "src/test/resources/dummyProperties.json"); + final List toscaPolicies = new ArrayList(); + toscaPolicies.add(toscaPolicy); + final PdpUpdate pdpUpdateMsg = TestListenerUtils.createPdpUpdateMsg(pdpStatus, toscaPolicies); + pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null, pdpUpdateMsg); + assertEquals(pdpStatus.getPdpGroup(), pdpUpdateMsg.getPdpGroup()); + assertEquals(pdpStatus.getPdpSubgroup(), pdpUpdateMsg.getPdpSubgroup()); + assertEquals(pdpStatus.getPolicies(), + new PdpMessageHandler().getToscaPolicyIdentifiers(pdpUpdateMsg.getPolicies())); + } + + @Test + public void testPdpUpdateMssageListener_success() throws InterruptedException, CoderException { + OutputStream outContent = new ByteArrayOutputStream(); + System.setOut(new PrintStream(outContent)); + final PdpStatus pdpStatus = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT); + pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null, + TestListenerUtils.createPdpUpdateMsg(pdpStatus, new ArrayList())); + PdpStateChange pdpStateChangeMsg = + TestListenerUtils.createPdpStateChangeMsg(PdpState.ACTIVE, "pdpGroup", "pdpSubgroup", pdpStatus.getName()); + pdpStateChangeListener.onTopicEvent(INFRA, TOPIC, null, pdpStateChangeMsg); + final ToscaPolicy toscaPolicy = + TestListenerUtils.createToscaPolicy("apex policy name", "1.0", "src/test/resources/dummyProperties.json"); final List toscaPolicies = new ArrayList(); toscaPolicies.add(toscaPolicy); - pdpUpdateMsg.setPolicies(toscaPolicies); + final PdpUpdate pdpUpdateMsg = TestListenerUtils.createPdpUpdateMsg(pdpStatus, toscaPolicies); pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null, pdpUpdateMsg); + final String outString = outContent.toString(); assertEquals(pdpStatus.getPdpGroup(), pdpUpdateMsg.getPdpGroup()); assertEquals(pdpStatus.getPdpSubgroup(), pdpUpdateMsg.getPdpSubgroup()); assertEquals(pdpStatus.getPolicies(), new PdpMessageHandler().getToscaPolicyIdentifiers(pdpUpdateMsg.getPolicies())); + assertTrue(outString.contains("Apex engine started and policies are running.")); + } + + @Test + public void testPdpUpdateMssageListener_undeploy() throws InterruptedException, CoderException { + final PdpStatus pdpStatus = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT); + pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null, + TestListenerUtils.createPdpUpdateMsg(pdpStatus, new ArrayList())); + PdpStateChange pdpStateChangeMsg = + TestListenerUtils.createPdpStateChangeMsg(PdpState.ACTIVE, "pdpGroup", "pdpSubgroup", pdpStatus.getName()); + pdpStateChangeListener.onTopicEvent(INFRA, TOPIC, null, pdpStateChangeMsg); + final ToscaPolicy toscaPolicy = + TestListenerUtils.createToscaPolicy("apex policy name", "1.0", "src/test/resources/dummyProperties.json"); + final List toscaPolicies = new ArrayList(); + toscaPolicies.add(toscaPolicy); + final PdpUpdate pdpUpdateMsg = TestListenerUtils.createPdpUpdateMsg(pdpStatus, toscaPolicies); + pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null, pdpUpdateMsg); + OutputStream outContent = new ByteArrayOutputStream(); + System.setOut(new PrintStream(outContent)); + pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null, + TestListenerUtils.createPdpUpdateMsg(pdpStatus, new ArrayList())); + final String outString = outContent.toString(); + assertTrue(outString.contains("Pdp update successful. No policies are running.")); + + } + + @Test + public void testPdpUpdateMssageListener_multi_policy_duplicate() + throws InterruptedException, ApexStarterException, CoderException { + OutputStream outContent = new ByteArrayOutputStream(); + System.setOut(new PrintStream(outContent)); + final PdpStatus pdpStatus = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT); + final ToscaPolicy toscaPolicy = + TestListenerUtils.createToscaPolicy("apex policy name", "1.0", "src/test/resources/dummyProperties.json"); + final ToscaPolicy toscaPolicy2 = + TestListenerUtils.createToscaPolicy("apexpolicy2", "1.0", "src/test/resources/dummyProperties.json"); + final List toscaPolicies = new ArrayList(); + toscaPolicies.add(toscaPolicy); + toscaPolicies.add(toscaPolicy2); + final PdpUpdate pdpUpdateMsg = TestListenerUtils.createPdpUpdateMsg(pdpStatus, toscaPolicies); + pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null, pdpUpdateMsg); + PdpStateChange pdpStateChangeMsg = + TestListenerUtils.createPdpStateChangeMsg(PdpState.ACTIVE, "pdpGroup", "pdpSubgroup", pdpStatus.getName()); + pdpStateChangeListener.onTopicEvent(INFRA, TOPIC, null, pdpStateChangeMsg); + final String outString = outContent.toString(); + assertTrue(outString.contains( + "Apex engine started. But, only the following polices are running - apex policy name:1.0 . " + + "Other policies failed execution. Please see the logs for more details.")); } } diff --git a/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/parameters/dummyclasses/DummyStateFinalizerExecutor.java b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/parameters/dummyclasses/DummyStateFinalizerExecutor.java new file mode 100644 index 000000000..cdbd27b8f --- /dev/null +++ b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/parameters/dummyclasses/DummyStateFinalizerExecutor.java @@ -0,0 +1,42 @@ +/*- + * ============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.apex.services.onappf.parameters.dummyclasses; + +import java.util.Map; +import java.util.Properties; +import org.onap.policy.apex.context.ContextException; +import org.onap.policy.apex.core.engine.executor.StateFinalizerExecutor; +import org.onap.policy.apex.core.engine.executor.exception.StateMachineException; + +/** + * Dummy state finalizer executor for testing. + */ +public class DummyStateFinalizerExecutor extends StateFinalizerExecutor { + public DummyStateFinalizerExecutor() {} + + @Override + public String execute(final long executionId, final Properties executorProperties, + final Map newIncomingFields) throws StateMachineException, ContextException { + + return "stateOutput0"; + } +} diff --git a/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/parameters/dummyclasses/DummyTaskExecutor.java b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/parameters/dummyclasses/DummyTaskExecutor.java new file mode 100644 index 000000000..d3fa9b570 --- /dev/null +++ b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/parameters/dummyclasses/DummyTaskExecutor.java @@ -0,0 +1,58 @@ +/*- + * ============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.apex.services.onappf.parameters.dummyclasses; + +import java.util.Map; +import java.util.Properties; +import org.onap.policy.apex.context.ContextException; +import org.onap.policy.apex.core.engine.event.EnEvent; +import org.onap.policy.apex.core.engine.executor.TaskExecutor; +import org.onap.policy.apex.core.engine.executor.exception.StateMachineException; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.policymodel.concepts.AxTask; + +/** + * Dummy task executor for testing. + */ +public class DummyTaskExecutor extends TaskExecutor { + public DummyTaskExecutor() {} + + @Override + public void prepare() throws StateMachineException {} + + @Override + public Map execute(final long executionId, final Properties executorProperties, + final Map newIncomingFields) throws StateMachineException, ContextException { + + AxArtifactKey event0Key = new AxArtifactKey("Event0:0.0.1"); + return new EnEvent(event0Key); + } + + @Override + public AxTask getSubject() { + AxArtifactKey taskKey = new AxArtifactKey("FirstTask:0.0.1"); + return new AxTask(taskKey); + } + + @Override + public void cleanUp() throws StateMachineException {} +} diff --git a/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/parameters/dummyclasses/DummyTaskSelectExecutor.java b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/parameters/dummyclasses/DummyTaskSelectExecutor.java new file mode 100644 index 000000000..b27ff6ebc --- /dev/null +++ b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/parameters/dummyclasses/DummyTaskSelectExecutor.java @@ -0,0 +1,49 @@ +/*- + * ============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.apex.services.onappf.parameters.dummyclasses; + +import java.util.Properties; +import org.onap.policy.apex.context.ContextException; +import org.onap.policy.apex.core.engine.event.EnEvent; +import org.onap.policy.apex.core.engine.executor.TaskSelectExecutor; +import org.onap.policy.apex.core.engine.executor.exception.StateMachineException; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; + +/** + * Dummy task selection executor for testing. + */ +public class DummyTaskSelectExecutor extends TaskSelectExecutor { + public DummyTaskSelectExecutor() {} + + @Override + public void prepare() throws StateMachineException {} + + @Override + public AxArtifactKey execute(final long executionId, final Properties executorProperties, + final EnEvent newIncomingEvent) throws StateMachineException, ContextException { + + return new AxArtifactKey("task:0.0.1"); + } + + @Override + public void cleanUp() throws StateMachineException {} +} diff --git a/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/parameters/dummyclasses/SuperDooperExecutorParameters.java b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/parameters/dummyclasses/SuperDooperExecutorParameters.java new file mode 100644 index 000000000..cbcd23c73 --- /dev/null +++ b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/parameters/dummyclasses/SuperDooperExecutorParameters.java @@ -0,0 +1,39 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-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.apex.services.onappf.parameters.dummyclasses; + +import org.onap.policy.apex.core.engine.ExecutorParameters; + +/** + * Dummy executor parameters. + */ +public class SuperDooperExecutorParameters extends ExecutorParameters { + + /** + * Instantiates a new super dooper executor parameters. + */ + public SuperDooperExecutorParameters() { + this.setTaskExecutorPluginClass(DummyTaskExecutor.class.getName()); + this.setTaskSelectionExecutorPluginClass(DummyTaskSelectExecutor.class.getName()); + this.setStateFinalizerExecutorPluginClass(DummyStateFinalizerExecutor.class.getName()); + } +} -- cgit 1.2.3-korg