From e8b197315437cac84872752e2ea090d8fb233941 Mon Sep 17 00:00:00 2001 From: Parshad Patel Date: Tue, 20 Nov 2018 14:46:45 +0900 Subject: Rename test classes in apex-pdp Make test classes name consistence under auth, model, plugins, testsuits, tools projects Issue-ID: POLICY-1263 Change-Id: I49ec9a9f5b457d6381e693de2c04ec0268ad1b02 Signed-off-by: Parshad Patel --- .../model/policymodel/concepts/ExceptionsTest.java | 54 +++ .../apex/model/policymodel/concepts/LogicTest.java | 182 ++++++++ .../model/policymodel/concepts/PoliciesTest.java | 432 ++++++++++++++++++ .../policymodel/concepts/PolicyModelTest.java | 356 +++++++++++++++ .../policymodel/concepts/StateOutputTest.java | 139 ++++++ .../concepts/StateTaskReferenceTest.java | 162 +++++++ .../apex/model/policymodel/concepts/StateTest.java | 501 +++++++++++++++++++++ .../policymodel/concepts/TaskParameterTest.java | 117 +++++ .../apex/model/policymodel/concepts/TasksTest.java | 333 ++++++++++++++ .../model/policymodel/concepts/TestExceptions.java | 54 --- .../apex/model/policymodel/concepts/TestLogic.java | 182 -------- .../model/policymodel/concepts/TestPolicies.java | 432 ------------------ .../policymodel/concepts/TestPolicyModel.java | 356 --------------- .../apex/model/policymodel/concepts/TestState.java | 501 --------------------- .../policymodel/concepts/TestStateOutput.java | 139 ------ .../concepts/TestStateTaskReference.java | 162 ------- .../policymodel/concepts/TestTaskParameter.java | 117 ----- .../apex/model/policymodel/concepts/TestTasks.java | 333 -------------- .../handling/ApexPolicyModelCreatorTest.java | 455 +++++++++++++++++++ .../policymodel/handling/ApexPolicyModelTest.java | 179 ++++++++ .../policymodel/handling/PolicyAnalyserTest.java | 60 +++ .../handling/PolicyLogicReaderTest.java | 114 +++++ .../handling/PolicyModelComparerTest.java | 129 ++++++ .../handling/PolicyModelMergerTest.java | 111 +++++ .../handling/PolicyModelSplitterTest.java | 95 ++++ .../policymodel/handling/TestApexPolicyModel.java | 179 -------- .../handling/TestApexPolicyModelCreator.java | 455 ------------------- .../policymodel/handling/TestPolicyAnalyser.java | 60 --- .../handling/TestPolicyLogicReader.java | 114 ----- .../handling/TestPolicyModelComparer.java | 129 ------ .../handling/TestPolicyModelMerger.java | 111 ----- .../handling/TestPolicyModelSplitter.java | 95 ---- 32 files changed, 3419 insertions(+), 3419 deletions(-) create mode 100644 model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/ExceptionsTest.java create mode 100644 model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/LogicTest.java create mode 100644 model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/PoliciesTest.java create mode 100644 model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/PolicyModelTest.java create mode 100644 model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/StateOutputTest.java create mode 100644 model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/StateTaskReferenceTest.java create mode 100644 model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/StateTest.java create mode 100644 model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/TaskParameterTest.java create mode 100644 model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/TasksTest.java delete mode 100644 model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/TestExceptions.java delete mode 100644 model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/TestLogic.java delete mode 100644 model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/TestPolicies.java delete mode 100644 model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/TestPolicyModel.java delete mode 100644 model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/TestState.java delete mode 100644 model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/TestStateOutput.java delete mode 100644 model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/TestStateTaskReference.java delete mode 100644 model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/TestTaskParameter.java delete mode 100644 model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/TestTasks.java create mode 100644 model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/ApexPolicyModelCreatorTest.java create mode 100644 model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/ApexPolicyModelTest.java create mode 100644 model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/PolicyAnalyserTest.java create mode 100644 model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/PolicyLogicReaderTest.java create mode 100644 model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/PolicyModelComparerTest.java create mode 100644 model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/PolicyModelMergerTest.java create mode 100644 model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/PolicyModelSplitterTest.java delete mode 100644 model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/TestApexPolicyModel.java delete mode 100644 model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/TestApexPolicyModelCreator.java delete mode 100644 model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/TestPolicyAnalyser.java delete mode 100644 model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/TestPolicyLogicReader.java delete mode 100644 model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/TestPolicyModelComparer.java delete mode 100644 model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/TestPolicyModelMerger.java delete mode 100644 model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/TestPolicyModelSplitter.java (limited to 'model/policy-model') diff --git a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/ExceptionsTest.java b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/ExceptionsTest.java new file mode 100644 index 000000000..70c79a1ef --- /dev/null +++ b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/ExceptionsTest.java @@ -0,0 +1,54 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-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.apex.model.policymodel.concepts; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.io.IOException; + +import org.junit.Test; +import org.onap.policy.apex.model.policymodel.concepts.PolicyException; +import org.onap.policy.apex.model.policymodel.concepts.PolicyRuntimeException; + +/** + * Test model exceptions. + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class ExceptionsTest { + + @Test + public void test() { + assertNotNull(new PolicyException("Message")); + assertNotNull(new PolicyException("Message", new IOException())); + + final PolicyException ae = new PolicyException("Message", new IOException("IO exception message")); + assertEquals("Message\ncaused by: Message\ncaused by: IO exception message", ae.getCascadedMessage()); + + assertNotNull(new PolicyRuntimeException("Message")); + assertNotNull(new PolicyRuntimeException("Message", new IOException())); + + final PolicyRuntimeException re = + new PolicyRuntimeException("Runtime Message", new IOException("IO runtime exception message")); + assertEquals("Runtime Message\ncaused by: Runtime Message\ncaused by: IO runtime exception message", + re.getCascadedMessage()); + } +} diff --git a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/LogicTest.java b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/LogicTest.java new file mode 100644 index 000000000..70db66348 --- /dev/null +++ b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/LogicTest.java @@ -0,0 +1,182 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-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.apex.model.policymodel.concepts; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import org.junit.Test; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey; +import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult; +import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult; +import org.onap.policy.apex.model.policymodel.concepts.AxLogic; +import org.onap.policy.apex.model.policymodel.concepts.AxStateFinalizerLogic; +import org.onap.policy.apex.model.policymodel.concepts.AxTaskLogic; +import org.onap.policy.apex.model.policymodel.concepts.AxTaskSelectionLogic; + +/** + * Test apex logic. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class LogicTest { + + @Test + public void testLogic() { + final DummyLogicReader logicReader = new DummyLogicReader(); + + assertNotNull(new AxLogic()); + assertNotNull(new AxLogic(new AxReferenceKey())); + assertNotNull(new AxLogic(new AxReferenceKey(), "LogicFlavour", "Logic")); + assertNotNull(new AxLogic(new AxReferenceKey(), "LogicName", "LogicFlavour", "Logic")); + assertNotNull(new AxLogic(new AxReferenceKey(), "LogicFlavour", logicReader)); + + assertNotNull(new AxTaskLogic()); + assertNotNull(new AxTaskLogic(new AxReferenceKey())); + assertNotNull(new AxTaskLogic(new AxReferenceKey(), "LogicFlavour", "Logic")); + assertNotNull(new AxTaskLogic(new AxReferenceKey(), "LogicFlavour", logicReader)); + assertNotNull(new AxTaskLogic(new AxLogic())); + assertNotNull(new AxTaskLogic(new AxArtifactKey(), "LogicName", "LogicFlavour", logicReader)); + assertNotNull(new AxTaskLogic(new AxArtifactKey(), "LogicName", "LogicFlavour", "Logic")); + assertNotNull(new AxTaskLogic(new AxReferenceKey(), "LogicFlavour", logicReader)); + + assertNotNull(new AxTaskSelectionLogic()); + assertNotNull(new AxTaskSelectionLogic(new AxReferenceKey())); + assertNotNull(new AxTaskSelectionLogic(new AxReferenceKey(), "LogicFlavour", "Logic")); + assertNotNull(new AxTaskSelectionLogic(new AxReferenceKey(), "LogicName", "LogicFlavour", "Logic")); + assertNotNull(new AxTaskSelectionLogic(new AxReferenceKey(), "LogicFlavour", logicReader)); + assertNotNull(new AxTaskSelectionLogic(new AxLogic())); + assertNotNull(new AxTaskSelectionLogic(new AxReferenceKey(), "LogicFlavour", logicReader)); + assertNotNull(new AxTaskSelectionLogic(new AxReferenceKey(), "LogicName", "LogicFlavour", logicReader)); + + assertNotNull(new AxStateFinalizerLogic()); + assertNotNull(new AxStateFinalizerLogic(new AxReferenceKey())); + assertNotNull(new AxStateFinalizerLogic(new AxReferenceKey(), "LogicFlavour", "Logic")); + assertNotNull(new AxStateFinalizerLogic(new AxReferenceKey(), "LogicName", "LogicFlavour", "Logic")); + assertNotNull(new AxStateFinalizerLogic(new AxReferenceKey(), "LogicFlavour", logicReader)); + assertNotNull(new AxStateFinalizerLogic(new AxLogic())); + assertNotNull(new AxStateFinalizerLogic(new AxReferenceKey(), "LogicFlavour", logicReader)); + assertNotNull(new AxStateFinalizerLogic(new AxReferenceKey(), "LogicName", "LogicFlavour", logicReader)); + + final AxLogic logic = new AxLogic(); + + final AxReferenceKey logicKey = new AxReferenceKey("LogicParentName", "0.0.1", "PLN", "LN"); + logic.setKey(logicKey); + assertEquals("LogicParentName:0.0.1:PLN:LN", logic.getKey().getId()); + assertEquals("LogicParentName:0.0.1:PLN:LN", logic.getKeys().get(0).getId()); + + logic.setLogicFlavour("LogicFlavour"); + assertEquals("LogicFlavour", logic.getLogicFlavour()); + + logic.setLogic("Logic"); + assertEquals("Logic", logic.getLogic()); + + AxValidationResult result = new AxValidationResult(); + result = logic.validate(result); + assertEquals(AxValidationResult.ValidationResult.VALID, result.getValidationResult()); + + logic.setKey(AxReferenceKey.getNullKey()); + result = new AxValidationResult(); + result = logic.validate(result); + assertEquals(ValidationResult.INVALID, result.getValidationResult()); + + logic.setKey(logicKey); + result = new AxValidationResult(); + result = logic.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + try { + logic.setLogicFlavour(null); + fail("test shold throw an exception here"); + } catch (final Exception e) { + assertEquals("parameter \"logicFlavour\" is null", e.getMessage()); + } + + try { + logic.setLogicFlavour(""); + fail("test shold throw an exception here"); + } catch (final Exception e) { + assertEquals("parameter \"logicFlavour\": value \"\", " + + "does not match regular expression \"[A-Za-z0-9\\-_]+\"", e.getMessage()); + } + + logic.setLogicFlavour(AxLogic.LOGIC_FLAVOUR_UNDEFINED); + result = new AxValidationResult(); + result = logic.validate(result); + assertEquals(ValidationResult.INVALID, result.getValidationResult()); + + logic.setLogicFlavour("LogicFlavour"); + result = new AxValidationResult(); + result = logic.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + try { + logic.setLogic(null); + fail("test shold throw an exception here"); + } catch (final Exception e) { + assertEquals("logic may not be null", e.getMessage()); + } + + logic.setLogic(""); + result = new AxValidationResult(); + result = logic.validate(result); + assertEquals(ValidationResult.INVALID, result.getValidationResult()); + + logic.setLogic("Logic"); + result = new AxValidationResult(); + result = logic.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + logic.clean(); + + final AxLogic clonedLogic = new AxLogic(logic); + assertEquals("AxLogic:(key=AxReferenceKey:(parentKeyName=LogicParentName,parentKeyVersion=0.0.1," + + "parentLocalName=PLN,localName=LN),logicFlavour=LogicFlavour,logic=Logic)", + clonedLogic.toString()); + + assertFalse(logic.hashCode() == 0); + + assertTrue(logic.equals(logic)); + assertTrue(logic.equals(clonedLogic)); + assertFalse(logic.equals(null)); + assertFalse(logic.equals("Hello")); + assertFalse(logic.equals(new AxLogic(AxReferenceKey.getNullKey(), "LogicFlavour", "Logic"))); + assertFalse(logic.equals(new AxLogic(logicKey, "AnotherLogicFlavour", "Logic"))); + assertFalse(logic.equals(new AxLogic(logicKey, "LogicFlavour", "AnotherLogic"))); + assertTrue(logic.equals(new AxLogic(logicKey, "LogicFlavour", "Logic"))); + + assertEquals(0, logic.compareTo(logic)); + assertEquals(0, logic.compareTo(clonedLogic)); + assertNotEquals(0, logic.compareTo(new AxArtifactKey())); + assertNotEquals(0, logic.compareTo(null)); + assertNotEquals(0, logic.compareTo(new AxLogic(AxReferenceKey.getNullKey(), "LogicFlavour", "Logic"))); + assertNotEquals(0, logic.compareTo(new AxLogic(logicKey, "AnotherLogicFlavour", "Logic"))); + assertNotEquals(0, logic.compareTo(new AxLogic(logicKey, "LogicFlavour", "AnotherLogic"))); + assertEquals(0, logic.compareTo(new AxLogic(logicKey, "LogicFlavour", "Logic"))); + + assertNotNull(logic.getKeys()); + } +} diff --git a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/PoliciesTest.java b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/PoliciesTest.java new file mode 100644 index 000000000..edf045a06 --- /dev/null +++ b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/PoliciesTest.java @@ -0,0 +1,432 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-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.apex.model.policymodel.concepts; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.util.Map; +import java.util.TreeMap; + +import org.junit.Test; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.basicmodel.concepts.AxKey; +import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey; +import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult; +import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult; +import org.onap.policy.apex.model.policymodel.concepts.AxPolicies; +import org.onap.policy.apex.model.policymodel.concepts.AxPolicy; +import org.onap.policy.apex.model.policymodel.concepts.AxState; +import org.onap.policy.apex.model.policymodel.concepts.AxStateOutput; +import org.onap.policy.apex.model.policymodel.concepts.AxStateTree; +import org.onap.policy.apex.model.policymodel.handling.ApexPolicyModelCreatorTest; + +/** + * Test apex policies. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class PoliciesTest { + + @Test + public void testPolicies() { + final TreeMap stateMap = new TreeMap<>(); + final TreeMap stateMapEmpty = new TreeMap<>(); + + assertNotNull(new AxPolicy()); + assertNotNull(new AxPolicy(new AxArtifactKey())); + assertNotNull(new AxPolicy(new AxArtifactKey(), "PolicyTemplate", stateMapEmpty, "FirstState")); + + AxPolicy policy = new AxPolicy(); + + final AxArtifactKey policyKey = new AxArtifactKey("PolicyName", "0.0.1"); + + final AxState firstState = new AxState(new AxReferenceKey(policy.getKey(), "FirstState")); + final AxState badState = new AxState(new AxReferenceKey(policy.getKey(), "BadState")); + final AxStateOutput badStateOutput = new AxStateOutput(badState.getKey(), AxArtifactKey.getNullKey(), + new AxReferenceKey(policyKey, "BadNextState")); + badState.getStateOutputs().put(badStateOutput.getKey().getLocalName(), badStateOutput); + stateMap.put(firstState.getKey().getLocalName(), firstState); + + try { + policy.setKey(null); + fail("test should throw an exception here"); + } catch (final Exception e) { + assertEquals("key may not be null", e.getMessage()); + } + + policy.setKey(policyKey); + assertEquals("PolicyName:0.0.1", policy.getKey().getId()); + assertEquals("PolicyName:0.0.1", policy.getKeys().get(0).getId()); + + try { + policy.setTemplate(null); + fail("test should throw an exception here"); + } catch (final Exception e) { + assertEquals("template may not be null", e.getMessage()); + } + + policy.setTemplate("PolicyTemplate"); + assertEquals("PolicyTemplate", policy.getTemplate()); + + try { + policy.setStateMap(null); + fail("test should throw an exception here"); + } catch (final Exception e) { + assertEquals("stateMap may not be null", e.getMessage()); + } + + policy.setStateMap(stateMap); + assertEquals(stateMap, policy.getStateMap()); + + try { + policy.setFirstState(null); + fail("test should throw an exception here"); + } catch (final Exception e) { + assertEquals("firstState may not be null", e.getMessage()); + } + + policy.setFirstState("FirstState"); + assertEquals("FirstState", policy.getFirstState()); + + assertEquals("PolicyName:0.0.1", policy.getKeys().get(0).getId()); + + policy = new ApexPolicyModelCreatorTest().getModel().getPolicies().get("policy"); + + AxValidationResult result = new AxValidationResult(); + result = policy.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + final AxArtifactKey savedPolicyKey = policy.getKey(); + policy.setKey(AxArtifactKey.getNullKey()); + result = new AxValidationResult(); + result = policy.validate(result); + assertEquals(ValidationResult.INVALID, result.getValidationResult()); + + policy.setKey(savedPolicyKey); + result = new AxValidationResult(); + result = policy.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + final String savedTemplate = policy.getTemplate(); + policy.setTemplate(""); + result = new AxValidationResult(); + result = policy.validate(result); + assertEquals(ValidationResult.OBSERVATION, result.getValidationResult()); + + policy.setTemplate(savedTemplate); + result = new AxValidationResult(); + result = policy.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + final Map savedStateMap = policy.getStateMap(); + + policy.setStateMap(stateMapEmpty); + result = new AxValidationResult(); + result = policy.validate(result); + assertEquals(ValidationResult.INVALID, result.getValidationResult()); + + policy.setStateMap(savedStateMap); + result = new AxValidationResult(); + result = policy.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + savedStateMap.put(AxKey.NULL_KEY_NAME, firstState); + result = new AxValidationResult(); + result = policy.validate(result); + assertEquals(ValidationResult.INVALID, result.getValidationResult()); + + savedStateMap.remove(AxKey.NULL_KEY_NAME); + result = new AxValidationResult(); + result = policy.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + savedStateMap.put("NullState", null); + result = new AxValidationResult(); + result = policy.validate(result); + assertEquals(ValidationResult.INVALID, result.getValidationResult()); + + savedStateMap.remove("NullState"); + result = new AxValidationResult(); + result = policy.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + savedStateMap.put("BadStateKey", firstState); + result = new AxValidationResult(); + result = policy.validate(result); + assertEquals(ValidationResult.INVALID, result.getValidationResult()); + + savedStateMap.remove("BadStateKey"); + result = new AxValidationResult(); + result = policy.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + savedStateMap.put(badState.getKey().getLocalName(), badState); + result = new AxValidationResult(); + result = policy.validate(result); + assertEquals(ValidationResult.INVALID, result.getValidationResult()); + + savedStateMap.remove(badState.getKey().getLocalName()); + result = new AxValidationResult(); + result = policy.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + final String savedFirstState = policy.getFirstState(); + + policy.setFirstState(""); + result = new AxValidationResult(); + result = policy.validate(result); + assertEquals(ValidationResult.INVALID, result.getValidationResult()); + + policy.setFirstState(savedFirstState); + result = new AxValidationResult(); + result = policy.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + policy.setFirstState("NonExistantFirstState"); + result = new AxValidationResult(); + result = policy.validate(result); + assertEquals(ValidationResult.INVALID, result.getValidationResult()); + + policy.setFirstState(savedFirstState); + result = new AxValidationResult(); + result = policy.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + final AxState clonedState = new AxState(policy.getStateMap().get("state")); + clonedState.getKey().setLocalName("ClonedState"); + clonedState.afterUnmarshal(null, null); + + savedStateMap.put(clonedState.getKey().getLocalName(), clonedState); + result = new AxValidationResult(); + result = policy.validate(result); + assertEquals(ValidationResult.WARNING, result.getValidationResult()); + + savedStateMap.remove(clonedState.getKey().getLocalName()); + result = new AxValidationResult(); + result = policy.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + policy.clean(); + + final AxPolicy clonedPolicy = new AxPolicy(policy); + assertEquals("AxPolicy:(key=AxArtifactKey:(name=policy,version=0.0.1),template=FREEFORM,sta", + clonedPolicy.toString().substring(0, 77)); + + assertFalse(policy.hashCode() == 0); + + assertTrue(policy.equals(policy)); + assertTrue(policy.equals(clonedPolicy)); + assertFalse(policy.equals(null)); + assertFalse(policy.equals("Hello")); + assertFalse(policy.equals( + new AxPolicy(AxArtifactKey.getNullKey(), savedTemplate, savedStateMap, savedFirstState))); + assertFalse(policy.equals(new AxPolicy(savedPolicyKey, "SomeTemplate", savedStateMap, savedFirstState))); + assertFalse(policy.equals(new AxPolicy(savedPolicyKey, savedTemplate, stateMapEmpty, savedFirstState))); + assertFalse(policy.equals(new AxPolicy(savedPolicyKey, savedTemplate, savedStateMap, "SomeFirstState"))); + assertTrue(policy.equals(new AxPolicy(savedPolicyKey, savedTemplate, savedStateMap, savedFirstState))); + + assertEquals(0, policy.compareTo(policy)); + assertEquals(0, policy.compareTo(clonedPolicy)); + assertNotEquals(0, policy.compareTo(new AxArtifactKey())); + assertNotEquals(0, policy.compareTo(null)); + assertNotEquals(0, policy.compareTo( + new AxPolicy(AxArtifactKey.getNullKey(), savedTemplate, savedStateMap, savedFirstState))); + assertNotEquals(0, + policy.compareTo(new AxPolicy(savedPolicyKey, "SomeTemplate", savedStateMap, savedFirstState))); + assertNotEquals(0, + policy.compareTo(new AxPolicy(savedPolicyKey, savedTemplate, stateMapEmpty, savedFirstState))); + assertNotEquals(0, + policy.compareTo(new AxPolicy(savedPolicyKey, savedTemplate, savedStateMap, "SomeFirstState"))); + assertEquals(0, policy.compareTo(new AxPolicy(savedPolicyKey, savedTemplate, savedStateMap, savedFirstState))); + + assertNotNull(policy.getKeys()); + + final AxPolicies policies = new AxPolicies(); + result = new AxValidationResult(); + result = policies.validate(result); + assertEquals(ValidationResult.INVALID, result.getValidationResult()); + + // Invalid, no events in event map + policies.setKey(new AxArtifactKey("PoliciesKey", "0.0.1")); + assertEquals("PoliciesKey:0.0.1", policies.getKey().getId()); + + result = new AxValidationResult(); + result = policies.validate(result); + assertEquals(ValidationResult.INVALID, result.getValidationResult()); + + policies.getPolicyMap().put(savedPolicyKey, policy); + result = new AxValidationResult(); + result = policies.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + policies.getPolicyMap().put(AxArtifactKey.getNullKey(), null); + result = new AxValidationResult(); + result = policies.validate(result); + assertEquals(ValidationResult.INVALID, result.getValidationResult()); + + policies.getPolicyMap().remove(AxArtifactKey.getNullKey()); + result = new AxValidationResult(); + result = policies.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + policies.getPolicyMap().put(new AxArtifactKey("NullValueKey", "0.0.1"), null); + result = new AxValidationResult(); + result = policies.validate(result); + assertEquals(ValidationResult.INVALID, result.getValidationResult()); + + policies.getPolicyMap().remove(new AxArtifactKey("NullValueKey", "0.0.1")); + result = new AxValidationResult(); + result = policies.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + policies.getPolicyMap().put(new AxArtifactKey("BadEventKey", "0.0.1"), policy); + result = new AxValidationResult(); + result = policies.validate(result); + assertEquals(ValidationResult.INVALID, result.getValidationResult()); + + policies.getPolicyMap().remove(new AxArtifactKey("BadEventKey", "0.0.1")); + result = new AxValidationResult(); + result = policies.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + policies.clean(); + policies.afterUnmarshal(null, null); + + final AxPolicies clonedPolicies = new AxPolicies(policies); + assertEquals("AxPolicies:(key=AxArtifactKey:(name=PoliciesKey,version=0.0.", + clonedPolicies.toString().substring(0, 60)); + + assertFalse(policies.hashCode() == 0); + + assertTrue(policies.equals(policies)); + assertTrue(policies.equals(clonedPolicies)); + assertFalse(policies.equals(null)); + assertFalse(policies.equals("Hello")); + assertFalse(policies.equals(new AxPolicies(new AxArtifactKey()))); + + assertEquals(0, policies.compareTo(policies)); + assertEquals(0, policies.compareTo(clonedPolicies)); + assertNotEquals(0, policies.compareTo(null)); + assertNotEquals(0, policies.compareTo(new AxArtifactKey())); + assertNotEquals(0, policies.compareTo(new AxPolicies(new AxArtifactKey()))); + + clonedPolicies.get(savedPolicyKey).setTemplate("AnotherTemplate"); + assertNotEquals(0, policies.compareTo(clonedPolicies)); + + assertEquals(policies.getKey(), policies.getKeys().get(0)); + + assertEquals("policy", policies.get("policy").getKey().getName()); + assertEquals("policy", policies.get("policy", "0.0.1").getKey().getName()); + assertEquals(1, policies.getAll("policy", "0.0.1").size()); + assertEquals(0, policies.getAll("NonExistantPolicy").size()); + + AxStateTree stateTree = policy.getStateTree(); + assertNotNull(stateTree); + assertNotNull(stateTree.getReferencedStateList()); + assertNotNull(stateTree.getReferencedStateSet()); + + final AxState secondState = new AxState(policy.getStateMap().get("state")); + secondState.getKey().setLocalName("SecondState"); + secondState.afterUnmarshal(null, null); + policy.getStateMap().put("SecondState", secondState); + policy.getStateMap().get("state").getStateOutputs().get("stateOutput0").setNextState(secondState.getKey()); + + stateTree = policy.getStateTree(); + assertNotNull(stateTree); + assertNotNull(stateTree.getReferencedStateList()); + assertNotNull(stateTree.getReferencedStateSet()); + assertNotNull(stateTree.getNextStates()); + + policy.getStateMap().get("SecondState").getStateOutputs().get("stateOutput0") + .setNextState(policy.getStateMap().get("state").getKey()); + try { + policy.getStateTree(); + fail("test should throw an exception here"); + } catch (final Exception e) { + assertEquals("loop detected in state tree for policy policy:0.0.1 state SecondState, " + + "next state state referenced more than once", e.getMessage()); + } + + policy.getStateMap().get("SecondState").getStateOutputs().get("stateOutput0") + .setNextState(AxReferenceKey.getNullKey()); + + final AxState thirdState = new AxState(policy.getStateMap().get("state")); + thirdState.getKey().setLocalName("ThirdState"); + thirdState.afterUnmarshal(null, null); + policy.getStateMap().put("ThirdState", thirdState); + policy.getStateMap().get("SecondState").getStateOutputs().get("stateOutput0").setNextState(thirdState.getKey()); + policy.getStateMap().get("ThirdState").getStateOutputs().get("stateOutput0") + .setNextState(AxReferenceKey.getNullKey()); + + stateTree = policy.getStateTree(); + + final AxStateOutput ssS0Clone = new AxStateOutput( + policy.getStateMap().get("SecondState").getStateOutputs().get("stateOutput0")); + ssS0Clone.getKey().setLocalName("ssS0Clone"); + policy.getStateMap().get("SecondState").getStateOutputs().put("ssS0Clone", ssS0Clone); + + try { + policy.getStateTree(); + fail("test should throw an exception here"); + } catch (final Exception e) { + assertEquals("loop detected in state tree for policy policy:0.0.1 state SecondState, " + + "next state ThirdState referenced more than once", e.getMessage()); + } + + policy.getStateMap().get("SecondState").getStateOutputs().remove("ssS0Clone"); + + policy.getStateMap().get("state").getStateOutputs().get("stateOutput0").setNextState(secondState.getKey()); + secondState.getStateOutputs().get("stateOutput0").setNextState(thirdState.getKey()); + thirdState.getStateOutputs().get("stateOutput0").setNextState(AxReferenceKey.getNullKey()); + + stateTree = policy.getStateTree(); + assertNotNull(stateTree.getState()); + + thirdState.getStateOutputs().get("stateOutput0").setNextState(secondState.getKey()); + + try { + policy.getStateTree(); + fail("test should throw an exception here"); + } catch (final Exception e) { + assertEquals("loop detected in state tree for policy policy:0.0.1 state ThirdState, " + + "next state SecondState referenced more than once", e.getMessage()); + } + + thirdState.getStateOutputs().get("stateOutput0").setNextState(AxReferenceKey.getNullKey()); + + stateTree = policy.getStateTree(); + + final AxStateTree otherStateTree = policy.getStateTree(); + assertEquals(0, stateTree.compareTo(otherStateTree)); + + for (final AxStateTree childStateTree : stateTree.getNextStates()) { + assertNotEquals(0, stateTree.compareTo(childStateTree)); + } + + otherStateTree.getNextStates().clear(); + assertNotEquals(0, stateTree.compareTo(otherStateTree)); + } +} diff --git a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/PolicyModelTest.java b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/PolicyModelTest.java new file mode 100644 index 000000000..e1f1f90c3 --- /dev/null +++ b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/PolicyModelTest.java @@ -0,0 +1,356 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-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.apex.model.policymodel.concepts; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.basicmodel.concepts.AxKeyInformation; +import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey; +import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult; +import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbum; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbums; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas; +import org.onap.policy.apex.model.eventmodel.concepts.AxEvents; +import org.onap.policy.apex.model.eventmodel.concepts.AxField; +import org.onap.policy.apex.model.eventmodel.concepts.AxInputField; +import org.onap.policy.apex.model.eventmodel.concepts.AxOutputField; +import org.onap.policy.apex.model.policymodel.concepts.AxPolicies; +import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; +import org.onap.policy.apex.model.policymodel.concepts.AxStateOutput; +import org.onap.policy.apex.model.policymodel.concepts.AxStateTaskOutputType; +import org.onap.policy.apex.model.policymodel.concepts.AxStateTaskReference; +import org.onap.policy.apex.model.policymodel.concepts.AxTaskSelectionLogic; +import org.onap.policy.apex.model.policymodel.concepts.AxTasks; +import org.onap.policy.apex.model.policymodel.handling.ApexPolicyModelCreatorTest; + +/** + * Test policy models. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class PolicyModelTest { + + @Test + public void testPolicyModel() { + assertNotNull(new AxPolicyModel()); + assertNotNull(new AxPolicyModel(new AxArtifactKey())); + assertNotNull(new AxPolicyModel(new AxArtifactKey(), new AxContextSchemas(), new AxKeyInformation(), + new AxEvents(), new AxContextAlbums(), new AxTasks(), new AxPolicies())); + + final AxArtifactKey modelKey = new AxArtifactKey("ModelKey", "0.0.1"); + final AxArtifactKey schemasKey = new AxArtifactKey("SchemasKey", "0.0.1"); + final AxArtifactKey eventsKey = new AxArtifactKey("EventsKey", "0.0.1"); + final AxArtifactKey keyInfoKey = new AxArtifactKey("SchemasKey", "0.0.1"); + final AxArtifactKey albumsKey = new AxArtifactKey("AlbumsKey", "0.0.1"); + final AxArtifactKey tasksKey = new AxArtifactKey("TasksKey", "0.0.1"); + final AxArtifactKey policiesKey = new AxArtifactKey("PoliciesKey", "0.0.1"); + + AxPolicyModel model = new AxPolicyModel(modelKey, new AxContextSchemas(schemasKey), + new AxKeyInformation(keyInfoKey), new AxEvents(eventsKey), new AxContextAlbums(albumsKey), + new AxTasks(tasksKey), new AxPolicies(policiesKey)); + + model.register(); + + assertNotNull(model.getContextModel()); + assertEquals("ModelKey:0.0.1", model.getKeys().get(0).getId()); + + model.clean(); + assertNotNull(model); + assertEquals("AxPolicyModel:(AxPolicyModel:(key=AxArtifactKey:(n", model.toString().substring(0, 50)); + + final AxPolicyModel clonedModel = new AxPolicyModel(model); + + assertFalse(model.hashCode() == 0); + + assertTrue(model.equals(model)); + assertTrue(model.equals(clonedModel)); + assertFalse(model.equals("Hello")); + assertFalse(model.equals(new AxPolicyModel(new AxArtifactKey()))); + assertFalse(model.equals(new AxPolicyModel(AxArtifactKey.getNullKey(), new AxContextSchemas(schemasKey), + new AxKeyInformation(keyInfoKey), new AxEvents(eventsKey), new AxContextAlbums(albumsKey), + new AxTasks(tasksKey), new AxPolicies(policiesKey)))); + assertFalse(model.equals(new AxPolicyModel(modelKey, new AxContextSchemas(), new AxKeyInformation(keyInfoKey), + new AxEvents(eventsKey), new AxContextAlbums(albumsKey), new AxTasks(tasksKey), + new AxPolicies(policiesKey)))); + assertFalse(model.equals(new AxPolicyModel(modelKey, new AxContextSchemas(schemasKey), new AxKeyInformation(), + new AxEvents(eventsKey), new AxContextAlbums(albumsKey), new AxTasks(tasksKey), + new AxPolicies(policiesKey)))); + assertFalse(model.equals(new AxPolicyModel(modelKey, new AxContextSchemas(schemasKey), + new AxKeyInformation(keyInfoKey), new AxEvents(), new AxContextAlbums(albumsKey), + new AxTasks(tasksKey), new AxPolicies(policiesKey)))); + assertFalse(model.equals(new AxPolicyModel(modelKey, new AxContextSchemas(schemasKey), + new AxKeyInformation(keyInfoKey), new AxEvents(eventsKey), new AxContextAlbums(), + new AxTasks(tasksKey), new AxPolicies(policiesKey)))); + assertFalse(model.equals(new AxPolicyModel(modelKey, new AxContextSchemas(schemasKey), + new AxKeyInformation(keyInfoKey), new AxEvents(eventsKey), new AxContextAlbums(albumsKey), + new AxTasks(), new AxPolicies(policiesKey)))); + assertFalse(model.equals(new AxPolicyModel(modelKey, new AxContextSchemas(schemasKey), + new AxKeyInformation(keyInfoKey), new AxEvents(eventsKey), new AxContextAlbums(albumsKey), + new AxTasks(tasksKey), new AxPolicies()))); + assertTrue(model.equals(new AxPolicyModel(modelKey, new AxContextSchemas(schemasKey), + new AxKeyInformation(keyInfoKey), new AxEvents(eventsKey), new AxContextAlbums(albumsKey), + new AxTasks(tasksKey), new AxPolicies(policiesKey)))); + + assertEquals(0, model.compareTo(model)); + assertEquals(0, model.compareTo(clonedModel)); + assertNotEquals(0, model.compareTo(new AxArtifactKey())); + assertNotEquals(0, model.compareTo(new AxPolicyModel(AxArtifactKey.getNullKey(), + new AxContextSchemas(schemasKey), new AxKeyInformation(keyInfoKey), new AxEvents(eventsKey), + new AxContextAlbums(albumsKey), new AxTasks(tasksKey), new AxPolicies(policiesKey)))); + assertNotEquals(0, model.compareTo(new AxPolicyModel(modelKey, new AxContextSchemas(), + new AxKeyInformation(keyInfoKey), new AxEvents(eventsKey), new AxContextAlbums(albumsKey), + new AxTasks(tasksKey), new AxPolicies(policiesKey)))); + assertNotEquals(0, + model.compareTo(new AxPolicyModel(modelKey, new AxContextSchemas(schemasKey), + new AxKeyInformation(), new AxEvents(eventsKey), new AxContextAlbums(albumsKey), + new AxTasks(tasksKey), new AxPolicies(policiesKey)))); + assertNotEquals(0, model.compareTo(new AxPolicyModel(modelKey, new AxContextSchemas(schemasKey), + new AxKeyInformation(keyInfoKey), new AxEvents(), new AxContextAlbums(albumsKey), + new AxTasks(tasksKey), new AxPolicies(policiesKey)))); + assertNotEquals(0, + model.compareTo(new AxPolicyModel(modelKey, new AxContextSchemas(schemasKey), + new AxKeyInformation(keyInfoKey), new AxEvents(eventsKey), + new AxContextAlbums(), new AxTasks(tasksKey), new AxPolicies(policiesKey)))); + assertNotEquals(0, + model.compareTo(new AxPolicyModel(modelKey, new AxContextSchemas(schemasKey), + new AxKeyInformation(keyInfoKey), new AxEvents(eventsKey), + new AxContextAlbums(albumsKey), new AxTasks(), new AxPolicies(policiesKey)))); + assertNotEquals(0, + model.compareTo(new AxPolicyModel(modelKey, new AxContextSchemas(schemasKey), + new AxKeyInformation(keyInfoKey), new AxEvents(eventsKey), + new AxContextAlbums(albumsKey), new AxTasks(tasksKey), new AxPolicies()))); + assertEquals(0, model.compareTo(new AxPolicyModel(modelKey, new AxContextSchemas(schemasKey), + new AxKeyInformation(keyInfoKey), new AxEvents(eventsKey), new AxContextAlbums(albumsKey), + new AxTasks(tasksKey), new AxPolicies(policiesKey)))); + + model = new ApexPolicyModelCreatorTest().getModel(); + + AxValidationResult result = new AxValidationResult(); + result = model.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + final AxArtifactKey savedPolicyKey = model.getKey(); + model.setKey(AxArtifactKey.getNullKey()); + result = new AxValidationResult(); + result = model.validate(result); + assertEquals(ValidationResult.INVALID, result.getValidationResult()); + + model.setKey(savedPolicyKey); + result = new AxValidationResult(); + result = model.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + final AxField badField = new AxField(new AxReferenceKey(model.getEvents().get("inEvent").getKey(), "BadField"), + new AxArtifactKey("NonExistantSchema", "0.0.1")); + model.getEvents().get("inEvent").getParameterMap().put(badField.getKey().getLocalName(), badField); + result = new AxValidationResult(); + result = model.validate(result); + assertEquals(ValidationResult.INVALID, result.getValidationResult()); + + model.getEvents().get("inEvent").getParameterMap().remove(badField.getKey().getLocalName()); + result = new AxValidationResult(); + result = model.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + final AxContextAlbum badAlbum = new AxContextAlbum(new AxArtifactKey("BadAlbum", "0.0.1"), "SomeScope", true, + new AxArtifactKey("NonExistantSchema", "0.0.1")); + model.getAlbums().getAlbumsMap().put(badAlbum.getKey(), badAlbum); + result = new AxValidationResult(); + result = model.validate(result); + assertEquals(ValidationResult.INVALID, result.getValidationResult()); + + model.getAlbums().getAlbumsMap().remove(badAlbum.getKey()); + result = new AxValidationResult(); + result = model.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + final AxInputField badInField = new AxInputField( + new AxReferenceKey(model.getTasks().get("task").getKey(), "BadInField"), + new AxArtifactKey("NonExistantSchema", "0.0.1")); + model.getTasks().get("task").getInputFields().put(badInField.getKey().getLocalName(), badInField); + result = new AxValidationResult(); + result = model.validate(result); + assertEquals(ValidationResult.INVALID, result.getValidationResult()); + + model.getTasks().get("task").getInputFields().remove(badInField.getKey().getLocalName()); + result = new AxValidationResult(); + result = model.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + final AxOutputField badOutField = new AxOutputField( + new AxReferenceKey(model.getTasks().get("task").getKey(), "BadOutField"), + new AxArtifactKey("NonExistantSchema", "0.0.1")); + model.getTasks().get("task").getOutputFields().put(badOutField.getKey().getLocalName(), badOutField); + result = new AxValidationResult(); + result = model.validate(result); + assertEquals(ValidationResult.INVALID, result.getValidationResult()); + + model.getTasks().get("task").getOutputFields().remove(badOutField.getKey().getLocalName()); + result = new AxValidationResult(); + result = model.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + model.getTasks().get("task").getContextAlbumReferences() + .add(new AxArtifactKey("NonExistantContextAlbum", "0.0.1")); + result = new AxValidationResult(); + result = model.validate(result); + assertEquals(ValidationResult.INVALID, result.getValidationResult()); + + model.getTasks().get("task").getContextAlbumReferences() + .remove(new AxArtifactKey("NonExistantContextAlbum", "0.0.1")); + result = new AxValidationResult(); + result = model.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + model.getPolicies().get("policy").getStateMap().get("state").getContextAlbumReferences() + .add(new AxArtifactKey("NonExistantContextAlbum", "0.0.1")); + result = new AxValidationResult(); + result = model.validate(result); + assertEquals(ValidationResult.INVALID, result.getValidationResult()); + + model.getPolicies().get("policy").getStateMap().get("state").getContextAlbumReferences() + .remove(new AxArtifactKey("NonExistantContextAlbum", "0.0.1")); + result = new AxValidationResult(); + result = model.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + final AxArtifactKey savedTrigger = model.getPolicies().get("policy").getStateMap().get("state").getTrigger(); + model.getPolicies().get("policy").getStateMap().get("state") + .setTrigger(new AxArtifactKey("NonExistantEvent", "0.0.1")); + result = new AxValidationResult(); + result = model.validate(result); + assertEquals(ValidationResult.INVALID, result.getValidationResult()); + + model.getPolicies().get("policy").getStateMap().get("state").setTrigger(savedTrigger); + result = new AxValidationResult(); + result = model.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + final AxArtifactKey savedDefaultTask = model.getPolicies().get("policy").getStateMap().get("state") + .getDefaultTask(); + model.getPolicies().get("policy").getStateMap().get("state") + .setDefaultTask(new AxArtifactKey("NonExistantTask", "0.0.1")); + result = new AxValidationResult(); + result = model.validate(result); + assertEquals(ValidationResult.INVALID, result.getValidationResult()); + + model.getPolicies().get("policy").getStateMap().get("state").setDefaultTask(savedDefaultTask); + result = new AxValidationResult(); + result = model.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + // It is OK not to have TSL + final AxTaskSelectionLogic savedTaskSelectionLogic = model.getPolicies().get("policy").getStateMap() + .get("state").getTaskSelectionLogic(); + model.getPolicies().get("policy").getStateMap().get("state") + .setTaskSelectionLogic(new AxTaskSelectionLogic(AxReferenceKey.getNullKey())); + result = new AxValidationResult(); + result = model.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + model.getTasks().get("task").getInputFields().put(badInField.getKey().getLocalName(), badInField); + result = new AxValidationResult(); + result = model.validate(result); + assertEquals(ValidationResult.INVALID, result.getValidationResult()); + + model.getTasks().get("task").getInputFields().remove(badInField.getKey().getLocalName()); + result = new AxValidationResult(); + result = model.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + model.getPolicies().get("policy").getStateMap().get("state").setTaskSelectionLogic(savedTaskSelectionLogic); + result = new AxValidationResult(); + result = model.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + final AxStateOutput badStateOutput = new AxStateOutput( + new AxReferenceKey(model.getPolicies().get("policy").getStateMap().get("state").getKey(), + "BadSO"), + new AxArtifactKey("NonExistantEvent", "0.0.1"), AxReferenceKey.getNullKey()); + model.getPolicies().get("policy").getStateMap().get("state").getStateOutputs() + .put(badStateOutput.getKey().getLocalName(), badStateOutput); + result = new AxValidationResult(); + result = model.validate(result); + assertEquals(ValidationResult.INVALID, result.getValidationResult()); + + model.getPolicies().get("policy").getStateMap().get("state").getStateOutputs() + .remove(badStateOutput.getKey().getLocalName()); + result = new AxValidationResult(); + result = model.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + final AxStateTaskReference badTaskReference = new AxStateTaskReference( + new AxReferenceKey(model.getPolicies().get("policy").getStateMap().get("state").getKey(), + "NonExistantTask"), + AxStateTaskOutputType.LOGIC, badStateOutput.getKey()); + model.getPolicies().get("policy").getStateMap().get("state").getTaskReferences() + .put(new AxArtifactKey("NonExistantTask", "0.0.1"), badTaskReference); + result = new AxValidationResult(); + result = model.validate(result); + assertEquals(ValidationResult.INVALID, result.getValidationResult()); + + badTaskReference.setStateTaskOutputType(AxStateTaskOutputType.DIRECT); + result = new AxValidationResult(); + result = model.validate(result); + assertEquals(ValidationResult.INVALID, result.getValidationResult()); + + model.getPolicies().get("policy").getStateMap().get("state").getTaskReferences() + .remove(new AxArtifactKey("NonExistantTask", "0.0.1")); + result = new AxValidationResult(); + result = model.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + final AxStateTaskReference tr = model.getPolicies().get("policy").getStateMap().get("state").getTaskReferences() + .get(new AxArtifactKey("task", "0.0.1")); + + final String savedStateOutputName = tr.getOutput().getLocalName(); + tr.getOutput().setLocalName("NonExistantOutput"); + result = new AxValidationResult(); + result = model.validate(result); + assertEquals(ValidationResult.INVALID, result.getValidationResult()); + + tr.getOutput().setLocalName(savedStateOutputName); + result = new AxValidationResult(); + result = model.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + final AxStateOutput so = model.getPolicies().get("policy").getStateMap().get("state").getStateOutputs() + .get(savedStateOutputName); + + final AxArtifactKey savedOutEvent = so.getOutgingEvent(); + so.setOutgoingEvent(new AxArtifactKey("NonExistantEvent", "0.0.1")); + result = new AxValidationResult(); + result = model.validate(result); + assertEquals(ValidationResult.INVALID, result.getValidationResult()); + + so.setOutgoingEvent(savedOutEvent); + result = new AxValidationResult(); + result = model.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + } +} diff --git a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/StateOutputTest.java b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/StateOutputTest.java new file mode 100644 index 000000000..901d641cb --- /dev/null +++ b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/StateOutputTest.java @@ -0,0 +1,139 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-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.apex.model.policymodel.concepts; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import org.junit.Test; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey; +import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult; +import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult; +import org.onap.policy.apex.model.policymodel.concepts.AxStateOutput; + +/** + * Test state outputs. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class StateOutputTest { + + @Test + public void testStateOutput() { + assertNotNull(new AxStateOutput()); + assertNotNull(new AxStateOutput(new AxReferenceKey())); + assertNotNull(new AxStateOutput(new AxReferenceKey(), new AxReferenceKey(), new AxArtifactKey())); + assertNotNull(new AxStateOutput(new AxReferenceKey(), new AxArtifactKey(), new AxReferenceKey())); + + final AxStateOutput so = new AxStateOutput(); + + final AxReferenceKey soKey = new AxReferenceKey("SOStateParent", "0.0.1", "SOState", "SOName"); + final AxReferenceKey nsKey = new AxReferenceKey("SOStateParent", "0.0.1", "NotUsed", "NextStateName"); + final AxArtifactKey eKey = new AxArtifactKey("EventName", "0.0.1"); + + try { + so.setKey(null); + fail("test should throw an exception here"); + } catch (final Exception e) { + assertEquals("key may not be null", e.getMessage()); + } + + so.setKey(soKey); + assertEquals("SOStateParent:0.0.1:SOState:SOName", so.getKey().getId()); + assertEquals("SOStateParent:0.0.1:SOState:SOName", so.getKeys().get(0).getId()); + + try { + so.setNextState(null); + fail("test should throw an exception here"); + } catch (final Exception e) { + assertEquals("nextState may not be null", e.getMessage()); + } + + so.setNextState(nsKey); + assertEquals(nsKey, so.getNextState()); + + try { + so.setOutgoingEvent(null); + fail("test should throw an exception here"); + } catch (final Exception e) { + assertEquals("outgoingEvent may not be null", e.getMessage()); + } + + so.setOutgoingEvent(eKey); + assertEquals(eKey, so.getOutgingEvent()); + + AxValidationResult result = new AxValidationResult(); + result = so.validate(result); + assertEquals(AxValidationResult.ValidationResult.VALID, result.getValidationResult()); + + so.setKey(AxReferenceKey.getNullKey()); + result = new AxValidationResult(); + result = so.validate(result); + assertEquals(ValidationResult.INVALID, result.getValidationResult()); + + so.setKey(soKey); + result = new AxValidationResult(); + result = so.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + so.setOutgoingEvent(AxArtifactKey.getNullKey()); + result = new AxValidationResult(); + result = so.validate(result); + assertEquals(ValidationResult.INVALID, result.getValidationResult()); + + so.setOutgoingEvent(eKey); + result = new AxValidationResult(); + result = so.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + so.clean(); + + final AxStateOutput clonedPar = new AxStateOutput(so); + assertEquals("AxStateOutput:(stateKey=AxReferenceKey:(parentKeyN", clonedPar.toString().substring(0, 50)); + + assertFalse(so.hashCode() == 0); + + assertTrue(so.equals(so)); + assertTrue(so.equals(clonedPar)); + assertFalse(so.equals(null)); + assertFalse(so.equals("Hello")); + assertFalse(so.equals(new AxStateOutput(AxReferenceKey.getNullKey(), eKey, nsKey))); + assertFalse(so.equals(new AxStateOutput(soKey, new AxArtifactKey(), nsKey))); + assertFalse(so.equals(new AxStateOutput(soKey, eKey, new AxReferenceKey()))); + assertTrue(so.equals(new AxStateOutput(soKey, eKey, nsKey))); + + assertEquals(0, so.compareTo(so)); + assertEquals(0, so.compareTo(clonedPar)); + assertNotEquals(0, so.compareTo(new AxArtifactKey())); + assertNotEquals(0, so.compareTo(null)); + assertNotEquals(0, so.compareTo(new AxStateOutput(AxReferenceKey.getNullKey(), eKey, nsKey))); + assertNotEquals(0, so.compareTo(new AxStateOutput(soKey, new AxArtifactKey(), nsKey))); + assertNotEquals(0, so.compareTo(new AxStateOutput(soKey, eKey, new AxReferenceKey()))); + assertEquals(0, so.compareTo(new AxStateOutput(soKey, eKey, nsKey))); + + assertNotNull(so.getKeys()); + } +} diff --git a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/StateTaskReferenceTest.java b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/StateTaskReferenceTest.java new file mode 100644 index 000000000..cfc8a494e --- /dev/null +++ b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/StateTaskReferenceTest.java @@ -0,0 +1,162 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-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.apex.model.policymodel.concepts; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import org.junit.Test; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey; +import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult; +import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult; +import org.onap.policy.apex.model.policymodel.concepts.AxStateTaskOutputType; +import org.onap.policy.apex.model.policymodel.concepts.AxStateTaskReference; + +/** + * Test state task references. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class StateTaskReferenceTest { + + @Test + public void testStateTaskReference() { + assertNotNull(new AxStateTaskReference()); + assertNotNull(new AxStateTaskReference(new AxReferenceKey())); + assertNotNull(new AxStateTaskReference(new AxReferenceKey(), AxStateTaskOutputType.UNDEFINED, + new AxReferenceKey())); + assertNotNull(new AxStateTaskReference(new AxReferenceKey(), new AxArtifactKey(), + AxStateTaskOutputType.UNDEFINED, new AxReferenceKey())); + + AxStateTaskReference stRef = new AxStateTaskReference(); + + AxReferenceKey stRefKey = new AxReferenceKey("StateParent", "0.0.1", "SOState", "SOName"); + + try { + stRef.setKey(null); + fail("test should throw an exception here"); + } catch (Exception e) { + assertEquals("key may not be null", e.getMessage()); + } + + stRef.setKey(stRefKey); + assertEquals("StateParent:0.0.1:SOState:SOName", stRef.getKey().getId()); + assertEquals("StateParent:0.0.1:SOState:SOName", stRef.getKeys().get(0).getId()); + + try { + stRef.setStateTaskOutputType(null); + fail("test should throw an exception here"); + } catch (Exception e) { + assertEquals("outputType may not be null", e.getMessage()); + } + + stRef.setStateTaskOutputType(AxStateTaskOutputType.UNDEFINED); + assertEquals(AxStateTaskOutputType.UNDEFINED, stRef.getStateTaskOutputType()); + stRef.setStateTaskOutputType(AxStateTaskOutputType.DIRECT); + assertEquals(AxStateTaskOutputType.DIRECT, stRef.getStateTaskOutputType()); + stRef.setStateTaskOutputType(AxStateTaskOutputType.LOGIC); + assertEquals(AxStateTaskOutputType.LOGIC, stRef.getStateTaskOutputType()); + + try { + stRef.setOutput(null); + fail("test should throw an exception here"); + } catch (Exception e) { + assertEquals("output may not be null", e.getMessage()); + } + + AxReferenceKey soKey = new AxReferenceKey("StateParent", "0.0.1", "SOState", "STRef0"); + stRef.setOutput(soKey); + assertEquals(soKey, stRef.getOutput()); + + AxValidationResult result = new AxValidationResult(); + result = stRef.validate(result); + assertEquals(AxValidationResult.ValidationResult.VALID, result.getValidationResult()); + + stRef.setKey(AxReferenceKey.getNullKey()); + result = new AxValidationResult(); + result = stRef.validate(result); + assertEquals(ValidationResult.INVALID, result.getValidationResult()); + + stRef.setKey(stRefKey); + result = new AxValidationResult(); + result = stRef.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + stRef.setStateTaskOutputType(AxStateTaskOutputType.UNDEFINED); + result = new AxValidationResult(); + result = stRef.validate(result); + assertEquals(ValidationResult.INVALID, result.getValidationResult()); + + stRef.setStateTaskOutputType(AxStateTaskOutputType.LOGIC); + result = new AxValidationResult(); + result = stRef.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + stRef.setOutput(AxReferenceKey.getNullKey()); + result = new AxValidationResult(); + result = stRef.validate(result); + assertEquals(ValidationResult.INVALID, result.getValidationResult()); + + stRef.setOutput(soKey); + result = new AxValidationResult(); + result = stRef.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + stRef.clean(); + + AxStateTaskReference clonedStRef = new AxStateTaskReference(stRef); + assertEquals("AxStateTaskReference:(stateKey=AxReferenceKey:(par", clonedStRef.toString().substring(0, 50)); + + assertFalse(stRef.hashCode() == 0); + + assertTrue(stRef.equals(stRef)); + assertTrue(stRef.equals(clonedStRef)); + assertFalse(stRef.equals(null)); + assertFalse(stRef.equals("Hello")); + assertFalse(stRef.equals( + new AxStateTaskReference(AxReferenceKey.getNullKey(), AxStateTaskOutputType.LOGIC, soKey))); + assertFalse(stRef.equals(new AxStateTaskReference(stRefKey, AxStateTaskOutputType.DIRECT, soKey))); + assertFalse(stRef + .equals(new AxStateTaskReference(stRefKey, AxStateTaskOutputType.LOGIC, new AxReferenceKey()))); + assertTrue(stRef.equals(new AxStateTaskReference(stRefKey, AxStateTaskOutputType.LOGIC, soKey))); + + assertNotNull(new AxStateTaskReference(new AxReferenceKey(), new AxArtifactKey(), + AxStateTaskOutputType.UNDEFINED, new AxReferenceKey())); + + assertEquals(0, stRef.compareTo(stRef)); + assertEquals(0, stRef.compareTo(clonedStRef)); + assertNotEquals(0, stRef.compareTo(new AxArtifactKey())); + assertNotEquals(0, stRef.compareTo(null)); + assertNotEquals(0, stRef.compareTo( + new AxStateTaskReference(AxReferenceKey.getNullKey(), AxStateTaskOutputType.LOGIC, soKey))); + assertNotEquals(0, stRef.compareTo(new AxStateTaskReference(stRefKey, AxStateTaskOutputType.DIRECT, soKey))); + assertNotEquals(0, stRef.compareTo( + new AxStateTaskReference(stRefKey, AxStateTaskOutputType.LOGIC, new AxReferenceKey()))); + assertEquals(0, stRef.compareTo(new AxStateTaskReference(stRefKey, AxStateTaskOutputType.LOGIC, soKey))); + + assertNotNull(stRef.getKeys()); + } +} diff --git a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/StateTest.java b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/StateTest.java new file mode 100644 index 000000000..1f71e9ec0 --- /dev/null +++ b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/StateTest.java @@ -0,0 +1,501 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-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.apex.model.policymodel.concepts; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.util.Set; +import java.util.TreeMap; +import java.util.TreeSet; + +import org.junit.Test; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.basicmodel.concepts.AxKey; +import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey; +import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult; +import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult; + +/** + * Test policy states. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class StateTest { + + @Test + public void testState() { + final TreeMap soEmptyMap = new TreeMap<>(); + final TreeSet ctxtEmptySet = new TreeSet<>(); + final TreeMap sflEmptyMap = new TreeMap<>(); + final TreeMap trEmptyMap = new TreeMap<>(); + + final TreeMap soMap = new TreeMap<>(); + final TreeSet ctxtSet = new TreeSet<>(); + final TreeMap sflMap = new TreeMap<>(); + final TreeMap trMap = new TreeMap<>(); + + assertNotNull(new AxState()); + assertNotNull(new AxState(new AxReferenceKey())); + assertNotNull(new AxState(new AxStateParamsBuilder().key(new AxReferenceKey()).trigger(new AxArtifactKey()) + .stateOutputs(soEmptyMap).contextAlbumReferenceSet(ctxtEmptySet) + .taskSelectionLogic(new AxTaskSelectionLogic()).stateFinalizerLogicMap(sflEmptyMap) + .defaultTask(new AxArtifactKey()).taskReferenceMap(trEmptyMap))); + + final AxState state = new AxState(); + + final AxReferenceKey stateKey = new AxReferenceKey("PolicyName", "0.0.1", "StateName"); + final AxReferenceKey stateKeyNext = new AxReferenceKey("PolicyName", "0.0.1", "StateNameNext"); + final AxReferenceKey stateKeyBad = new AxReferenceKey("PolicyName", "0.0.1", "BadStateName"); + final AxArtifactKey triggerKey = new AxArtifactKey("TriggerName", "0.0.1"); + final AxTaskSelectionLogic tsl = new AxTaskSelectionLogic(stateKey, "TSL", "LogicFlavour", "Some Logic"); + final AxArtifactKey defTaskKey = new AxArtifactKey("TaskName", "0.0.1"); + final AxArtifactKey taskKey1 = new AxArtifactKey("Task1", "0.0.1"); + final AxArtifactKey taskKey2 = new AxArtifactKey("Task2", "0.0.1"); + final AxArtifactKey taskKeyBad = new AxArtifactKey("TaskBad", "0.0.1"); + + try { + state.setKey(null); + fail("test should throw an exception here"); + } catch (final Exception e) { + assertEquals("key may not be null", e.getMessage()); + } + + state.setKey(stateKey); + assertEquals("PolicyName:0.0.1:NULL:StateName", state.getKey().getId()); + assertEquals("PolicyName:0.0.1:NULL:StateName", state.getKeys().get(0).getId()); + + final AxStateOutput so0 = new AxStateOutput(new AxReferenceKey(stateKey, "SO0"), triggerKey, + new AxReferenceKey()); + final AxStateOutput soU = new AxStateOutput(new AxReferenceKey(stateKey, "SOU"), triggerKey, stateKeyNext); + final AxStateOutput soSame = new AxStateOutput(new AxReferenceKey(stateKey, "SOU"), triggerKey, stateKey); + final AxArtifactKey cr0 = new AxArtifactKey("ContextReference", "0.0.1"); + final AxStateFinalizerLogic sfl = new AxStateFinalizerLogic(stateKey, "SFLogicName", "LogicFlavour", "Logic"); + final AxStateFinalizerLogic sflU = new AxStateFinalizerLogic(stateKey, "UnusedSFLogicName", "LogicFlavour", + "Logic"); + final AxStateTaskReference str0 = new AxStateTaskReference(new AxReferenceKey(stateKey, "STR0"), + AxStateTaskOutputType.DIRECT, so0.getKey()); + final AxStateTaskReference str1 = new AxStateTaskReference(new AxReferenceKey(stateKey, "STR1"), + AxStateTaskOutputType.DIRECT, so0.getKey()); + final AxStateTaskReference str2 = new AxStateTaskReference(new AxReferenceKey(stateKey, "STR2"), + AxStateTaskOutputType.LOGIC, sfl.getKey()); + + final AxStateTaskReference strBadState = new AxStateTaskReference(new AxReferenceKey(stateKeyBad, "STR2"), + AxStateTaskOutputType.LOGIC, sfl.getKey()); + final AxStateTaskReference strBadStateOutput = new AxStateTaskReference(new AxReferenceKey(stateKey, "STR2"), + AxStateTaskOutputType.UNDEFINED, sfl.getKey()); + final AxStateTaskReference strBadStateFinalizerLogic = new AxStateTaskReference( + new AxReferenceKey(stateKeyBad, "STR2"), AxStateTaskOutputType.LOGIC, + new AxReferenceKey(stateKey, "SomeSFL")); + + soMap.put(so0.getKey().getLocalName(), so0); + ctxtSet.add(cr0); + sflMap.put(sfl.getKey().getLocalName(), sfl); + trMap.put(defTaskKey.getKey(), str0); + trMap.put(taskKey1.getKey(), str1); + trMap.put(taskKey2.getKey(), str2); + + try { + state.setTrigger(null); + fail("test should throw an exception here"); + } catch (final Exception e) { + assertEquals("trigger may not be null", e.getMessage()); + } + + state.setTrigger(triggerKey); + assertEquals(triggerKey, state.getTrigger()); + + try { + state.setStateOutputs(null); + fail("test should throw an exception here"); + } catch (final Exception e) { + assertEquals("stateOutputs may not be null", e.getMessage()); + } + + state.setStateOutputs(soMap); + assertEquals(soMap, state.getStateOutputs()); + + try { + state.setContextAlbumReferences(null); + fail("test should throw an exception here"); + } catch (final Exception e) { + assertEquals("contextAlbumReferenceSet may not be null", e.getMessage()); + } + + state.setContextAlbumReferences(ctxtSet); + assertEquals(ctxtSet, state.getContextAlbumReferences()); + + try { + state.setTaskSelectionLogic(null); + fail("test should throw an exception here"); + } catch (final Exception e) { + assertEquals("taskSelectionLogic may not be null", e.getMessage()); + } + + assertEquals(false, state.checkSetTaskSelectionLogic()); + state.setTaskSelectionLogic(tsl); + assertEquals(tsl, state.getTaskSelectionLogic()); + assertEquals(true, state.checkSetTaskSelectionLogic()); + + try { + state.setStateFinalizerLogicMap(null); + fail("test should throw an exception here"); + } catch (final Exception e) { + assertEquals("stateFinalizerLogic may not be null", e.getMessage()); + } + + state.setStateFinalizerLogicMap(sflMap); + assertEquals(sflMap, state.getStateFinalizerLogicMap()); + + try { + state.setDefaultTask(null); + fail("test should throw an exception here"); + } catch (final Exception e) { + assertEquals("defaultTask may not be null", e.getMessage()); + } + + state.setDefaultTask(defTaskKey); + assertEquals(defTaskKey, state.getDefaultTask()); + + try { + state.setTaskReferences(null); + fail("test should throw an exception here"); + } catch (final Exception e) { + assertEquals("taskReferenceMap may not be null", e.getMessage()); + } + + state.setTaskReferences(trMap); + assertEquals(trMap, state.getTaskReferences()); + + state.afterUnmarshal(null, null); + assertEquals(state.getKey(), state.getKeys().get(0)); + state.getTaskSelectionLogic().getKey().setLocalName(AxKey.NULL_KEY_NAME); + state.afterUnmarshal(null, null); + assertEquals(state.getKey(), state.getKeys().get(0)); + + final Set stateSet = state.getNextStateSet(); + assertEquals(1, stateSet.size()); + + AxValidationResult result = new AxValidationResult(); + result = state.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + state.setKey(AxReferenceKey.getNullKey()); + result = new AxValidationResult(); + result = state.validate(result); + assertEquals(ValidationResult.INVALID, result.getValidationResult()); + + state.setKey(stateKey); + result = new AxValidationResult(); + result = state.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + state.setTrigger(AxArtifactKey.getNullKey()); + result = new AxValidationResult(); + result = state.validate(result); + assertEquals(ValidationResult.INVALID, result.getValidationResult()); + + state.setTrigger(triggerKey); + result = new AxValidationResult(); + result = state.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + state.setStateOutputs(soEmptyMap); + result = new AxValidationResult(); + result = state.validate(result); + assertEquals(ValidationResult.INVALID, result.getValidationResult()); + + state.setStateOutputs(soMap); + result = new AxValidationResult(); + result = state.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + so0.getKey().setParentLocalName("Zooby"); + result = new AxValidationResult(); + result = state.validate(result); + assertEquals(ValidationResult.INVALID, result.getValidationResult()); + + so0.getKey().setParentLocalName("StateName"); + state.setStateOutputs(soMap); + result = new AxValidationResult(); + result = state.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + soMap.put("NullOutput", null); + result = new AxValidationResult(); + result = state.validate(result); + assertEquals(ValidationResult.INVALID, result.getValidationResult()); + + soMap.remove("NullOutput"); + state.setStateOutputs(soMap); + result = new AxValidationResult(); + result = state.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + soMap.put("DupOutput", so0); + result = new AxValidationResult(); + result = state.validate(result); + assertEquals(ValidationResult.INVALID, result.getValidationResult()); + + soMap.remove("DupOutput"); + state.setStateOutputs(soMap); + result = new AxValidationResult(); + result = state.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + soMap.put("UnusedOutput", soU); + result = new AxValidationResult(); + result = state.validate(result); + assertEquals(ValidationResult.OBSERVATION, result.getValidationResult()); + + soMap.remove("UnusedOutput"); + state.setStateOutputs(soMap); + result = new AxValidationResult(); + result = state.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + soMap.put("OutputToSameState", soSame); + result = new AxValidationResult(); + result = state.validate(result); + assertEquals(ValidationResult.INVALID, result.getValidationResult()); + + soMap.remove("OutputToSameState"); + state.setStateOutputs(soMap); + result = new AxValidationResult(); + result = state.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + // Empty context reference set is OK + state.setContextAlbumReferences(ctxtEmptySet); + result = new AxValidationResult(); + result = state.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + state.setContextAlbumReferences(ctxtSet); + result = new AxValidationResult(); + result = state.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + ctxtSet.add(AxArtifactKey.getNullKey()); + result = new AxValidationResult(); + result = state.validate(result); + assertEquals(ValidationResult.INVALID, result.getValidationResult()); + + ctxtSet.remove(AxArtifactKey.getNullKey()); + result = new AxValidationResult(); + result = state.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + // Null TSL is OK + state.getTaskSelectionLogic().setKey(AxReferenceKey.getNullKey()); + result = new AxValidationResult(); + result = state.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + state.getTaskSelectionLogic().setKey(new AxReferenceKey(stateKey, "TSL")); + result = new AxValidationResult(); + result = state.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + state.setDefaultTask(AxArtifactKey.getNullKey()); + result = new AxValidationResult(); + result = state.validate(result); + assertEquals(ValidationResult.INVALID, result.getValidationResult()); + + state.setDefaultTask(defTaskKey); + result = new AxValidationResult(); + result = state.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + state.setTaskReferences(trEmptyMap); + result = new AxValidationResult(); + result = state.validate(result); + assertEquals(ValidationResult.INVALID, result.getValidationResult()); + + state.setTaskReferences(trMap); + result = new AxValidationResult(); + result = state.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + trMap.put(AxArtifactKey.getNullKey(), null); + result = new AxValidationResult(); + result = state.validate(result); + assertEquals(ValidationResult.INVALID, result.getValidationResult()); + + trMap.remove(AxArtifactKey.getNullKey()); + result = new AxValidationResult(); + result = state.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + trMap.put(AxArtifactKey.getNullKey(), str0); + result = new AxValidationResult(); + result = state.validate(result); + assertEquals(ValidationResult.INVALID, result.getValidationResult()); + + trMap.remove(AxArtifactKey.getNullKey()); + result = new AxValidationResult(); + result = state.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + trMap.put(taskKeyBad, strBadStateOutput); + result = new AxValidationResult(); + result = state.validate(result); + assertEquals(ValidationResult.INVALID, result.getValidationResult()); + + trMap.remove(taskKeyBad); + result = new AxValidationResult(); + result = state.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + trMap.put(taskKeyBad, strBadState); + result = new AxValidationResult(); + result = state.validate(result); + assertEquals(ValidationResult.INVALID, result.getValidationResult()); + + trMap.remove(taskKeyBad); + result = new AxValidationResult(); + result = state.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + trMap.put(taskKeyBad, strBadStateFinalizerLogic); + result = new AxValidationResult(); + result = state.validate(result); + assertEquals(ValidationResult.INVALID, result.getValidationResult()); + + trMap.remove(taskKeyBad); + result = new AxValidationResult(); + result = state.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + state.setDefaultTask(new AxArtifactKey("NonExistantTask", "0.0.1")); + result = new AxValidationResult(); + result = state.validate(result); + assertEquals(ValidationResult.INVALID, result.getValidationResult()); + + state.setDefaultTask(defTaskKey); + result = new AxValidationResult(); + result = state.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + sflMap.put("NullSFL", null); + result = new AxValidationResult(); + result = state.validate(result); + assertEquals(ValidationResult.INVALID, result.getValidationResult()); + + sflMap.remove("NullSFL"); + result = new AxValidationResult(); + result = state.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + sflMap.put(sflU.getKey().getLocalName(), sflU); + result = new AxValidationResult(); + result = state.validate(result); + assertEquals(ValidationResult.OBSERVATION, result.getValidationResult()); + + sflMap.remove(sflU.getKey().getLocalName()); + result = new AxValidationResult(); + result = state.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + state.clean(); + + final AxState clonedState = new AxState(state); + assertEquals("AxState:(stateKey=AxReferenceKey:(parent", clonedState.toString().substring(0, 40)); + + assertFalse(state.hashCode() == 0); + + assertTrue(state.equals(state)); + assertTrue(state.equals(clonedState)); + assertFalse(state.equals(null)); + assertFalse(state.equals((Object)"Hello")); + assertFalse(state.equals(new AxState(new AxStateParamsBuilder().key(new AxReferenceKey()).trigger(triggerKey) + .stateOutputs(soMap).contextAlbumReferenceSet(ctxtSet).taskSelectionLogic(tsl) + .stateFinalizerLogicMap(sflMap).defaultTask(defTaskKey).taskReferenceMap(trMap)))); + assertFalse(state.equals(new AxState(new AxStateParamsBuilder().key(stateKey).trigger(new AxArtifactKey()) + .stateOutputs(soMap).contextAlbumReferenceSet(ctxtSet).taskSelectionLogic(tsl) + .stateFinalizerLogicMap(sflMap).defaultTask(defTaskKey).taskReferenceMap(trMap)))); + assertFalse(state.equals(new AxState(new AxStateParamsBuilder().key(stateKey).trigger(triggerKey) + .stateOutputs(soEmptyMap).contextAlbumReferenceSet(ctxtSet).taskSelectionLogic(tsl) + .stateFinalizerLogicMap(sflMap).defaultTask(defTaskKey).taskReferenceMap(trMap)))); + assertFalse(state.equals(new AxState(new AxStateParamsBuilder().key(stateKey).trigger(triggerKey) + .stateOutputs(soMap).contextAlbumReferenceSet(ctxtEmptySet).taskSelectionLogic(tsl) + .stateFinalizerLogicMap(sflMap).defaultTask(defTaskKey).taskReferenceMap(trMap)))); + assertFalse(state.equals(new AxState(new AxStateParamsBuilder().key(stateKey).trigger(triggerKey) + .stateOutputs(soMap).contextAlbumReferenceSet(ctxtSet) + .taskSelectionLogic(new AxTaskSelectionLogic()).stateFinalizerLogicMap(sflMap) + .defaultTask(defTaskKey).taskReferenceMap(trMap)))); + assertFalse(state.equals(new AxState(new AxStateParamsBuilder().key(stateKey).trigger(triggerKey) + .stateOutputs(soMap).contextAlbumReferenceSet(ctxtSet).taskSelectionLogic(tsl) + .stateFinalizerLogicMap(sflEmptyMap).defaultTask(defTaskKey).taskReferenceMap(trMap)))); + assertFalse(state.equals(new AxState(new AxStateParamsBuilder().key(stateKey).trigger(triggerKey) + .stateOutputs(soMap).contextAlbumReferenceSet(ctxtSet).taskSelectionLogic(tsl) + .stateFinalizerLogicMap(sflMap).defaultTask(new AxArtifactKey()).taskReferenceMap(trMap)))); + assertFalse(state.equals(new AxState(new AxStateParamsBuilder().key(stateKey).trigger(triggerKey) + .stateOutputs(soMap).contextAlbumReferenceSet(ctxtSet).taskSelectionLogic(tsl) + .stateFinalizerLogicMap(sflMap).defaultTask(defTaskKey).taskReferenceMap(trEmptyMap)))); + assertTrue(state.equals(new AxState(new AxStateParamsBuilder().key(stateKey).trigger(triggerKey) + .stateOutputs(soMap).contextAlbumReferenceSet(ctxtSet).taskSelectionLogic(tsl) + .stateFinalizerLogicMap(sflMap).defaultTask(defTaskKey).taskReferenceMap(trMap)))); + + assertEquals(0, state.compareTo(state)); + assertEquals(0, state.compareTo(clonedState)); + assertNotEquals(0, state.compareTo(new AxArtifactKey())); + assertNotEquals(0, state.compareTo(null)); + assertNotEquals(0, + state.compareTo(new AxState(new AxStateParamsBuilder().key(new AxReferenceKey()) + .trigger(triggerKey).stateOutputs(soMap).contextAlbumReferenceSet(ctxtSet) + .taskSelectionLogic(tsl).stateFinalizerLogicMap(sflMap).defaultTask(defTaskKey) + .taskReferenceMap(trMap)))); + assertNotEquals(0, state.compareTo(new AxState(new AxStateParamsBuilder().key(stateKey) + .trigger(new AxArtifactKey()).stateOutputs(soMap).contextAlbumReferenceSet(ctxtSet) + .taskSelectionLogic(tsl).stateFinalizerLogicMap(sflMap).defaultTask(defTaskKey) + .taskReferenceMap(trMap)))); + assertNotEquals(0, state.compareTo(new AxState(new AxStateParamsBuilder().key(stateKey).trigger(triggerKey) + .stateOutputs(soEmptyMap).contextAlbumReferenceSet(ctxtSet).taskSelectionLogic(tsl) + .stateFinalizerLogicMap(sflMap).defaultTask(defTaskKey).taskReferenceMap(trMap)))); + assertNotEquals(0, state.compareTo(new AxState(new AxStateParamsBuilder().key(stateKey).trigger(triggerKey) + .stateOutputs(soMap).contextAlbumReferenceSet(ctxtEmptySet).taskSelectionLogic(tsl) + .stateFinalizerLogicMap(sflMap).defaultTask(defTaskKey).taskReferenceMap(trMap)))); + assertNotEquals(0, + state.compareTo(new AxState(new AxStateParamsBuilder().key(stateKey).trigger(triggerKey) + .stateOutputs(soMap).contextAlbumReferenceSet(ctxtSet) + .taskSelectionLogic(new AxTaskSelectionLogic()).stateFinalizerLogicMap(sflMap) + .defaultTask(defTaskKey).taskReferenceMap(trMap)))); + assertNotEquals(0, state.compareTo(new AxState(new AxStateParamsBuilder().key(stateKey).trigger(triggerKey) + .stateOutputs(soMap).contextAlbumReferenceSet(ctxtSet).taskSelectionLogic(tsl) + .stateFinalizerLogicMap(sflEmptyMap).defaultTask(defTaskKey).taskReferenceMap(trMap)))); + assertNotEquals(0, state.compareTo(new AxState(new AxStateParamsBuilder().key(stateKey).trigger(triggerKey) + .stateOutputs(soMap).contextAlbumReferenceSet(ctxtSet).taskSelectionLogic(tsl) + .stateFinalizerLogicMap(sflMap).defaultTask(new AxArtifactKey()).taskReferenceMap(trMap)))); + assertNotEquals(0, state.compareTo(new AxState(new AxStateParamsBuilder().key(stateKey).trigger(triggerKey) + .stateOutputs(soMap).contextAlbumReferenceSet(ctxtSet).taskSelectionLogic(tsl) + .stateFinalizerLogicMap(sflMap).defaultTask(defTaskKey).taskReferenceMap(trEmptyMap)))); + assertEquals(0, state.compareTo(new AxState(new AxStateParamsBuilder().key(stateKey).trigger(triggerKey) + .stateOutputs(soMap).contextAlbumReferenceSet(ctxtSet).taskSelectionLogic(tsl) + .stateFinalizerLogicMap(sflMap).defaultTask(defTaskKey).taskReferenceMap(trMap)))); + + assertNotNull(state.getKeys()); + } +} diff --git a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/TaskParameterTest.java b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/TaskParameterTest.java new file mode 100644 index 000000000..ecc8a4f66 --- /dev/null +++ b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/TaskParameterTest.java @@ -0,0 +1,117 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-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.apex.model.policymodel.concepts; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import org.junit.Test; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey; +import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult; +import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult; +import org.onap.policy.apex.model.policymodel.concepts.AxTaskParameter; + +/** + * Test task parameters. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class TaskParameterTest { + + @Test + public void testTaskParameter() { + assertNotNull(new AxTaskParameter()); + assertNotNull(new AxTaskParameter(new AxReferenceKey())); + assertNotNull(new AxTaskParameter(new AxReferenceKey(), "DefaultValue")); + + final AxTaskParameter par = new AxTaskParameter(); + + final AxReferenceKey parKey = new AxReferenceKey("ParParentName", "0.0.1", "PLN", "LN"); + par.setKey(parKey); + assertEquals("ParParentName:0.0.1:PLN:LN", par.getKey().getId()); + assertEquals("ParParentName:0.0.1:PLN:LN", par.getKeys().get(0).getId()); + + par.setDefaultValue("DefaultValue"); + assertEquals("DefaultValue", par.getTaskParameterValue()); + + AxValidationResult result = new AxValidationResult(); + result = par.validate(result); + assertEquals(AxValidationResult.ValidationResult.VALID, result.getValidationResult()); + + par.setKey(AxReferenceKey.getNullKey()); + result = new AxValidationResult(); + result = par.validate(result); + assertEquals(ValidationResult.INVALID, result.getValidationResult()); + + par.setKey(parKey); + result = new AxValidationResult(); + result = par.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + try { + par.setDefaultValue(null); + fail("test should throw an exception here"); + } catch (final Exception e) { + assertEquals("defaultValue may not be null", e.getMessage()); + } + + par.setDefaultValue(""); + result = new AxValidationResult(); + result = par.validate(result); + assertEquals(ValidationResult.WARNING, result.getValidationResult()); + + par.setDefaultValue("DefaultValue"); + result = new AxValidationResult(); + result = par.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + par.clean(); + + final AxTaskParameter clonedPar = new AxTaskParameter(par); + assertEquals("AxTaskParameter:(key=AxReferenceKey:(parentKeyName=ParParentName,parentKeyVersion=0.0.1," + + "parentLocalName=PLN,localName=LN),defaultValue=DefaultValue)", clonedPar.toString()); + + assertFalse(par.hashCode() == 0); + + assertTrue(par.equals(par)); + assertTrue(par.equals(clonedPar)); + assertFalse(par.equals(null)); + assertFalse(par.equals("Hello")); + assertFalse(par.equals(new AxTaskParameter(AxReferenceKey.getNullKey(), "DefaultValue"))); + assertFalse(par.equals(new AxTaskParameter(parKey, "OtherDefaultValue"))); + assertTrue(par.equals(new AxTaskParameter(parKey, "DefaultValue"))); + + assertEquals(0, par.compareTo(par)); + assertEquals(0, par.compareTo(clonedPar)); + assertNotEquals(0, par.compareTo(new AxArtifactKey())); + assertNotEquals(0, par.compareTo(null)); + assertNotEquals(0, par.compareTo(new AxTaskParameter(AxReferenceKey.getNullKey(), "DefaultValue"))); + assertNotEquals(0, par.compareTo(new AxTaskParameter(parKey, "OtherDefaultValue"))); + assertEquals(0, par.compareTo(new AxTaskParameter(parKey, "DefaultValue"))); + + assertNotNull(par.getKeys()); + } +} diff --git a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/TasksTest.java b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/TasksTest.java new file mode 100644 index 000000000..248dd6307 --- /dev/null +++ b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/TasksTest.java @@ -0,0 +1,333 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-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.apex.model.policymodel.concepts; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.util.TreeMap; +import java.util.TreeSet; + +import org.junit.Test; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey; +import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult; +import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult; +import org.onap.policy.apex.model.eventmodel.concepts.AxField; +import org.onap.policy.apex.model.eventmodel.concepts.AxInputField; +import org.onap.policy.apex.model.eventmodel.concepts.AxOutputField; +import org.onap.policy.apex.model.policymodel.concepts.AxTask; +import org.onap.policy.apex.model.policymodel.concepts.AxTaskLogic; +import org.onap.policy.apex.model.policymodel.concepts.AxTaskParameter; +import org.onap.policy.apex.model.policymodel.concepts.AxTasks; + +/** + * Test policy tasks. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class TasksTest { + + @Test + public void testTasks() { + final TreeMap ifEmptyMap = new TreeMap<>(); + final TreeMap ofEmptyMap = new TreeMap<>(); + final TreeMap tpEmptyMap = new TreeMap<>(); + final TreeSet ctxtEmptySet = new TreeSet<>(); + + final TreeMap ifMap = new TreeMap<>(); + final TreeMap ofMap = new TreeMap<>(); + final TreeMap tpMap = new TreeMap<>(); + final TreeSet ctxtSet = new TreeSet<>(); + + assertNotNull(new AxTask()); + assertNotNull(new AxTask(new AxArtifactKey())); + assertNotNull(new AxTask(new AxArtifactKey(), ifMap, ofMap, tpMap, ctxtSet, new AxTaskLogic())); + + final AxTask task = new AxTask(); + + final AxArtifactKey taskKey = new AxArtifactKey("TaskName", "0.0.1"); + task.setKey(taskKey); + assertEquals("TaskName:0.0.1", task.getKey().getId()); + assertEquals("TaskName:0.0.1", task.getKeys().get(0).getId()); + + final AxArtifactKey f0SchemaKey = new AxArtifactKey("FS0", "0.0.1"); + + final AxInputField if0 = new AxInputField(new AxReferenceKey(taskKey, "IF0"), f0SchemaKey, false); + final AxInputField if1 = new AxInputField(new AxReferenceKey(taskKey, "IF1"), f0SchemaKey, false); + final AxOutputField of0 = new AxOutputField(new AxReferenceKey(taskKey, "OF0"), f0SchemaKey, false); + final AxOutputField of1 = new AxOutputField(new AxReferenceKey(taskKey, "OF1"), f0SchemaKey, false); + final AxTaskParameter tp0 = new AxTaskParameter(new AxReferenceKey(taskKey, "TP0"), "DefaultValue"); + final AxArtifactKey cr0 = new AxArtifactKey("ContextReference", "0.0.1"); + final AxTaskLogic tl = new AxTaskLogic(taskKey, "LogicName", "LogicFlavour", "Logic"); + + ifMap.put(if0.getKey().getLocalName(), if0); + ofMap.put(of0.getKey().getLocalName(), of0); + tpMap.put(tp0.getKey().getLocalName(), tp0); + ctxtSet.add(cr0); + + task.setInputFields(ifMap); + assertEquals(ifMap, task.getInputFields()); + assertTrue(task.getInputFieldSet().contains(if0)); + assertTrue(task.getRawInputFields().keySet().contains(if0.getKey().getLocalName())); + + task.setOutputFields(ofMap); + assertEquals(ofMap, task.getOutputFields()); + assertTrue(task.getOutputFieldSet().contains(of0)); + assertTrue(task.getRawOutputFields().keySet().contains(of0.getKey().getLocalName())); + + final TreeMap ifDupMap = new TreeMap<>(); + final TreeMap ofDupMap = new TreeMap<>(); + ifDupMap.put(if1.getKey().getLocalName(), if1); + ofDupMap.put(of1.getKey().getLocalName(), of1); + task.duplicateInputFields(ifDupMap); + task.duplicateOutputFields(ofDupMap); + assertTrue(ifMap.containsKey("IF1")); + assertTrue(ofMap.containsKey("OF1")); + + task.setTaskParameters(tpMap); + assertEquals(tpMap, task.getTaskParameters()); + + task.setContextAlbumReferences(ctxtSet); + assertEquals(ctxtSet, task.getContextAlbumReferences()); + + task.setTaskLogic(tl); + assertEquals(tl, task.getTaskLogic()); + + task.setKey(taskKey); + assertEquals("TaskName:0.0.1", task.getKey().getId()); + assertEquals("TaskName:0.0.1", task.getKeys().get(0).getId()); + + task.afterUnmarshal(null, null); + assertEquals(1, task.getTaskParameters().size()); + + AxValidationResult result = new AxValidationResult(); + result = task.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + task.setKey(AxArtifactKey.getNullKey()); + result = new AxValidationResult(); + result = task.validate(result); + assertEquals(ValidationResult.INVALID, result.getValidationResult()); + + task.setKey(taskKey); + result = new AxValidationResult(); + result = task.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + task.setInputFields(ifEmptyMap); + result = new AxValidationResult(); + result = task.validate(result); + assertEquals(ValidationResult.INVALID, result.getValidationResult()); + + task.setInputFields(ifMap); + result = new AxValidationResult(); + result = task.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + ifMap.put("NullField", null); + result = new AxValidationResult(); + result = task.validate(result); + assertEquals(ValidationResult.INVALID, result.getValidationResult()); + + ifMap.remove("NullField"); + result = new AxValidationResult(); + result = task.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + task.setOutputFields(ofEmptyMap); + result = new AxValidationResult(); + result = task.validate(result); + assertEquals(ValidationResult.INVALID, result.getValidationResult()); + + task.setOutputFields(ofMap); + result = new AxValidationResult(); + result = task.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + ofMap.put("NullField", null); + result = new AxValidationResult(); + result = task.validate(result); + assertEquals(ValidationResult.INVALID, result.getValidationResult()); + + ofMap.remove("NullField"); + result = new AxValidationResult(); + result = task.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + // Empty task parameter map is OK + task.setTaskParameters(tpEmptyMap); + result = new AxValidationResult(); + result = task.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + task.setTaskParameters(tpMap); + result = new AxValidationResult(); + result = task.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + tpMap.put("NullField", null); + result = new AxValidationResult(); + result = task.validate(result); + assertEquals(ValidationResult.INVALID, result.getValidationResult()); + + tpMap.remove("NullField"); + result = new AxValidationResult(); + result = task.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + // Empty context reference set is OK + task.setContextAlbumReferences(ctxtEmptySet); + result = new AxValidationResult(); + result = task.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + task.setContextAlbumReferences(ctxtSet); + result = new AxValidationResult(); + result = task.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + ctxtSet.add(AxArtifactKey.getNullKey()); + result = new AxValidationResult(); + result = task.validate(result); + assertEquals(ValidationResult.INVALID, result.getValidationResult()); + + ctxtSet.remove(AxArtifactKey.getNullKey()); + result = new AxValidationResult(); + result = task.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + task.clean(); + + final AxTask clonedTask = new AxTask(task); + assertEquals("AxTask:(key=AxArtifactKey:(name=TaskName", clonedTask.toString().substring(0, 40)); + + assertFalse(task.hashCode() == 0); + + assertTrue(task.equals(task)); + assertTrue(task.equals(clonedTask)); + assertFalse(task.equals(null)); + assertFalse(task.equals("Hello")); + assertFalse(task.equals(new AxTask(new AxArtifactKey(), ifMap, ofMap, tpMap, ctxtSet, tl))); + assertFalse(task.equals(new AxTask(taskKey, ifEmptyMap, ofMap, tpMap, ctxtSet, tl))); + assertFalse(task.equals(new AxTask(taskKey, ifMap, ofEmptyMap, tpMap, ctxtSet, tl))); + assertFalse(task.equals(new AxTask(taskKey, ifMap, ofMap, tpEmptyMap, ctxtSet, tl))); + assertFalse(task.equals(new AxTask(taskKey, ifMap, ofMap, tpMap, ctxtEmptySet, tl))); + assertFalse(task.equals(new AxTask(taskKey, ifMap, ofMap, tpMap, ctxtSet, new AxTaskLogic()))); + assertTrue(task.equals(new AxTask(taskKey, ifMap, ofMap, tpMap, ctxtSet, tl))); + + assertEquals(0, task.compareTo(task)); + assertEquals(0, task.compareTo(clonedTask)); + assertNotEquals(0, task.compareTo(new AxArtifactKey())); + assertNotEquals(0, task.compareTo(null)); + assertNotEquals(0, task.compareTo(new AxTask(new AxArtifactKey(), ifMap, ofMap, tpMap, ctxtSet, tl))); + assertNotEquals(0, task.compareTo(new AxTask(taskKey, ifEmptyMap, ofMap, tpMap, ctxtSet, tl))); + assertNotEquals(0, task.compareTo(new AxTask(taskKey, ifMap, ofEmptyMap, tpMap, ctxtSet, tl))); + assertNotEquals(0, task.compareTo(new AxTask(taskKey, ifMap, ofMap, tpEmptyMap, ctxtSet, tl))); + assertNotEquals(0, task.compareTo(new AxTask(taskKey, ifMap, ofMap, tpMap, ctxtEmptySet, tl))); + assertNotEquals(0, task.compareTo(new AxTask(taskKey, ifMap, ofMap, tpMap, ctxtSet, new AxTaskLogic()))); + assertEquals(0, task.compareTo(new AxTask(taskKey, ifMap, ofMap, tpMap, ctxtSet, tl))); + + assertNotNull(task.getKeys()); + + final AxTasks tasks = new AxTasks(); + result = new AxValidationResult(); + result = tasks.validate(result); + assertEquals(ValidationResult.INVALID, result.getValidationResult()); + + // Invalid, no tasks in task map + tasks.setKey(new AxArtifactKey("TasksKey", "0.0.1")); + assertEquals("TasksKey:0.0.1", tasks.getKey().getId()); + + result = new AxValidationResult(); + result = tasks.validate(result); + assertEquals(ValidationResult.INVALID, result.getValidationResult()); + + tasks.getTaskMap().put(taskKey, task); + result = new AxValidationResult(); + result = tasks.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + tasks.getTaskMap().put(AxArtifactKey.getNullKey(), null); + result = new AxValidationResult(); + result = tasks.validate(result); + assertEquals(ValidationResult.INVALID, result.getValidationResult()); + + tasks.getTaskMap().remove(AxArtifactKey.getNullKey()); + result = new AxValidationResult(); + result = tasks.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + tasks.getTaskMap().put(new AxArtifactKey("NullValueKey", "0.0.1"), null); + result = new AxValidationResult(); + result = tasks.validate(result); + assertEquals(ValidationResult.INVALID, result.getValidationResult()); + + tasks.getTaskMap().remove(new AxArtifactKey("NullValueKey", "0.0.1")); + result = new AxValidationResult(); + result = tasks.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + tasks.getTaskMap().put(new AxArtifactKey("BadTaskKey", "0.0.1"), task); + result = new AxValidationResult(); + result = tasks.validate(result); + assertEquals(ValidationResult.INVALID, result.getValidationResult()); + + tasks.getTaskMap().remove(new AxArtifactKey("BadTaskKey", "0.0.1")); + result = new AxValidationResult(); + result = tasks.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + tasks.clean(); + tasks.afterUnmarshal(null, null); + + final AxTasks clonedTasks = new AxTasks(tasks); + assertEquals("AxTasks:(key=AxArtifactKey:(name=TasksKey,version=0.0.1),tas", + clonedTasks.toString().substring(0, 60)); + + assertFalse(tasks.hashCode() == 0); + + assertTrue(tasks.equals(tasks)); + assertTrue(tasks.equals(clonedTasks)); + assertFalse(tasks.equals(null)); + assertFalse(tasks.equals("Hello")); + assertFalse(tasks.equals(new AxTasks(new AxArtifactKey()))); + + assertEquals(0, tasks.compareTo(tasks)); + assertEquals(0, tasks.compareTo(clonedTasks)); + assertNotEquals(0, tasks.compareTo(null)); + assertNotEquals(0, tasks.compareTo(new AxArtifactKey())); + assertNotEquals(0, tasks.compareTo(new AxTasks(new AxArtifactKey()))); + + clonedTasks.get(taskKey).getTaskLogic().setLogic("SomeChangedLogic"); + assertNotEquals(0, tasks.compareTo(clonedTasks)); + + assertEquals(tasks.getKey(), tasks.getKeys().get(0)); + + assertEquals("TaskName", tasks.get("TaskName").getKey().getName()); + assertEquals("TaskName", tasks.get("TaskName", "0.0.1").getKey().getName()); + assertEquals(1, tasks.getAll("TaskName", "0.0.1").size()); + assertEquals(0, tasks.getAll("NonExistantTaskName").size()); + } +} diff --git a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/TestExceptions.java b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/TestExceptions.java deleted file mode 100644 index 1fa3928b5..000000000 --- a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/TestExceptions.java +++ /dev/null @@ -1,54 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-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.apex.model.policymodel.concepts; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; - -import java.io.IOException; - -import org.junit.Test; -import org.onap.policy.apex.model.policymodel.concepts.PolicyException; -import org.onap.policy.apex.model.policymodel.concepts.PolicyRuntimeException; - -/** - * Test model exceptions. - * @author Liam Fallon (liam.fallon@ericsson.com) - */ -public class TestExceptions { - - @Test - public void test() { - assertNotNull(new PolicyException("Message")); - assertNotNull(new PolicyException("Message", new IOException())); - - final PolicyException ae = new PolicyException("Message", new IOException("IO exception message")); - assertEquals("Message\ncaused by: Message\ncaused by: IO exception message", ae.getCascadedMessage()); - - assertNotNull(new PolicyRuntimeException("Message")); - assertNotNull(new PolicyRuntimeException("Message", new IOException())); - - final PolicyRuntimeException re = - new PolicyRuntimeException("Runtime Message", new IOException("IO runtime exception message")); - assertEquals("Runtime Message\ncaused by: Runtime Message\ncaused by: IO runtime exception message", - re.getCascadedMessage()); - } -} diff --git a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/TestLogic.java b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/TestLogic.java deleted file mode 100644 index 710b360f9..000000000 --- a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/TestLogic.java +++ /dev/null @@ -1,182 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-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.apex.model.policymodel.concepts; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - -import org.junit.Test; -import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; -import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey; -import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult; -import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult; -import org.onap.policy.apex.model.policymodel.concepts.AxLogic; -import org.onap.policy.apex.model.policymodel.concepts.AxStateFinalizerLogic; -import org.onap.policy.apex.model.policymodel.concepts.AxTaskLogic; -import org.onap.policy.apex.model.policymodel.concepts.AxTaskSelectionLogic; - -/** - * Test apex logic. - * - * @author Liam Fallon (liam.fallon@ericsson.com) - */ -public class TestLogic { - - @Test - public void testLogic() { - final DummyLogicReader logicReader = new DummyLogicReader(); - - assertNotNull(new AxLogic()); - assertNotNull(new AxLogic(new AxReferenceKey())); - assertNotNull(new AxLogic(new AxReferenceKey(), "LogicFlavour", "Logic")); - assertNotNull(new AxLogic(new AxReferenceKey(), "LogicName", "LogicFlavour", "Logic")); - assertNotNull(new AxLogic(new AxReferenceKey(), "LogicFlavour", logicReader)); - - assertNotNull(new AxTaskLogic()); - assertNotNull(new AxTaskLogic(new AxReferenceKey())); - assertNotNull(new AxTaskLogic(new AxReferenceKey(), "LogicFlavour", "Logic")); - assertNotNull(new AxTaskLogic(new AxReferenceKey(), "LogicFlavour", logicReader)); - assertNotNull(new AxTaskLogic(new AxLogic())); - assertNotNull(new AxTaskLogic(new AxArtifactKey(), "LogicName", "LogicFlavour", logicReader)); - assertNotNull(new AxTaskLogic(new AxArtifactKey(), "LogicName", "LogicFlavour", "Logic")); - assertNotNull(new AxTaskLogic(new AxReferenceKey(), "LogicFlavour", logicReader)); - - assertNotNull(new AxTaskSelectionLogic()); - assertNotNull(new AxTaskSelectionLogic(new AxReferenceKey())); - assertNotNull(new AxTaskSelectionLogic(new AxReferenceKey(), "LogicFlavour", "Logic")); - assertNotNull(new AxTaskSelectionLogic(new AxReferenceKey(), "LogicName", "LogicFlavour", "Logic")); - assertNotNull(new AxTaskSelectionLogic(new AxReferenceKey(), "LogicFlavour", logicReader)); - assertNotNull(new AxTaskSelectionLogic(new AxLogic())); - assertNotNull(new AxTaskSelectionLogic(new AxReferenceKey(), "LogicFlavour", logicReader)); - assertNotNull(new AxTaskSelectionLogic(new AxReferenceKey(), "LogicName", "LogicFlavour", logicReader)); - - assertNotNull(new AxStateFinalizerLogic()); - assertNotNull(new AxStateFinalizerLogic(new AxReferenceKey())); - assertNotNull(new AxStateFinalizerLogic(new AxReferenceKey(), "LogicFlavour", "Logic")); - assertNotNull(new AxStateFinalizerLogic(new AxReferenceKey(), "LogicName", "LogicFlavour", "Logic")); - assertNotNull(new AxStateFinalizerLogic(new AxReferenceKey(), "LogicFlavour", logicReader)); - assertNotNull(new AxStateFinalizerLogic(new AxLogic())); - assertNotNull(new AxStateFinalizerLogic(new AxReferenceKey(), "LogicFlavour", logicReader)); - assertNotNull(new AxStateFinalizerLogic(new AxReferenceKey(), "LogicName", "LogicFlavour", logicReader)); - - final AxLogic logic = new AxLogic(); - - final AxReferenceKey logicKey = new AxReferenceKey("LogicParentName", "0.0.1", "PLN", "LN"); - logic.setKey(logicKey); - assertEquals("LogicParentName:0.0.1:PLN:LN", logic.getKey().getId()); - assertEquals("LogicParentName:0.0.1:PLN:LN", logic.getKeys().get(0).getId()); - - logic.setLogicFlavour("LogicFlavour"); - assertEquals("LogicFlavour", logic.getLogicFlavour()); - - logic.setLogic("Logic"); - assertEquals("Logic", logic.getLogic()); - - AxValidationResult result = new AxValidationResult(); - result = logic.validate(result); - assertEquals(AxValidationResult.ValidationResult.VALID, result.getValidationResult()); - - logic.setKey(AxReferenceKey.getNullKey()); - result = new AxValidationResult(); - result = logic.validate(result); - assertEquals(ValidationResult.INVALID, result.getValidationResult()); - - logic.setKey(logicKey); - result = new AxValidationResult(); - result = logic.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - try { - logic.setLogicFlavour(null); - fail("test shold throw an exception here"); - } catch (final Exception e) { - assertEquals("parameter \"logicFlavour\" is null", e.getMessage()); - } - - try { - logic.setLogicFlavour(""); - fail("test shold throw an exception here"); - } catch (final Exception e) { - assertEquals("parameter \"logicFlavour\": value \"\", " - + "does not match regular expression \"[A-Za-z0-9\\-_]+\"", e.getMessage()); - } - - logic.setLogicFlavour(AxLogic.LOGIC_FLAVOUR_UNDEFINED); - result = new AxValidationResult(); - result = logic.validate(result); - assertEquals(ValidationResult.INVALID, result.getValidationResult()); - - logic.setLogicFlavour("LogicFlavour"); - result = new AxValidationResult(); - result = logic.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - try { - logic.setLogic(null); - fail("test shold throw an exception here"); - } catch (final Exception e) { - assertEquals("logic may not be null", e.getMessage()); - } - - logic.setLogic(""); - result = new AxValidationResult(); - result = logic.validate(result); - assertEquals(ValidationResult.INVALID, result.getValidationResult()); - - logic.setLogic("Logic"); - result = new AxValidationResult(); - result = logic.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - logic.clean(); - - final AxLogic clonedLogic = new AxLogic(logic); - assertEquals("AxLogic:(key=AxReferenceKey:(parentKeyName=LogicParentName,parentKeyVersion=0.0.1," - + "parentLocalName=PLN,localName=LN),logicFlavour=LogicFlavour,logic=Logic)", - clonedLogic.toString()); - - assertFalse(logic.hashCode() == 0); - - assertTrue(logic.equals(logic)); - assertTrue(logic.equals(clonedLogic)); - assertFalse(logic.equals(null)); - assertFalse(logic.equals("Hello")); - assertFalse(logic.equals(new AxLogic(AxReferenceKey.getNullKey(), "LogicFlavour", "Logic"))); - assertFalse(logic.equals(new AxLogic(logicKey, "AnotherLogicFlavour", "Logic"))); - assertFalse(logic.equals(new AxLogic(logicKey, "LogicFlavour", "AnotherLogic"))); - assertTrue(logic.equals(new AxLogic(logicKey, "LogicFlavour", "Logic"))); - - assertEquals(0, logic.compareTo(logic)); - assertEquals(0, logic.compareTo(clonedLogic)); - assertNotEquals(0, logic.compareTo(new AxArtifactKey())); - assertNotEquals(0, logic.compareTo(null)); - assertNotEquals(0, logic.compareTo(new AxLogic(AxReferenceKey.getNullKey(), "LogicFlavour", "Logic"))); - assertNotEquals(0, logic.compareTo(new AxLogic(logicKey, "AnotherLogicFlavour", "Logic"))); - assertNotEquals(0, logic.compareTo(new AxLogic(logicKey, "LogicFlavour", "AnotherLogic"))); - assertEquals(0, logic.compareTo(new AxLogic(logicKey, "LogicFlavour", "Logic"))); - - assertNotNull(logic.getKeys()); - } -} diff --git a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/TestPolicies.java b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/TestPolicies.java deleted file mode 100644 index 1e0e88c7b..000000000 --- a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/TestPolicies.java +++ /dev/null @@ -1,432 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-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.apex.model.policymodel.concepts; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - -import java.util.Map; -import java.util.TreeMap; - -import org.junit.Test; -import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; -import org.onap.policy.apex.model.basicmodel.concepts.AxKey; -import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey; -import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult; -import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult; -import org.onap.policy.apex.model.policymodel.concepts.AxPolicies; -import org.onap.policy.apex.model.policymodel.concepts.AxPolicy; -import org.onap.policy.apex.model.policymodel.concepts.AxState; -import org.onap.policy.apex.model.policymodel.concepts.AxStateOutput; -import org.onap.policy.apex.model.policymodel.concepts.AxStateTree; -import org.onap.policy.apex.model.policymodel.handling.TestApexPolicyModelCreator; - -/** - * Test apex policies. - * - * @author Liam Fallon (liam.fallon@ericsson.com) - */ -public class TestPolicies { - - @Test - public void testPolicies() { - final TreeMap stateMap = new TreeMap<>(); - final TreeMap stateMapEmpty = new TreeMap<>(); - - assertNotNull(new AxPolicy()); - assertNotNull(new AxPolicy(new AxArtifactKey())); - assertNotNull(new AxPolicy(new AxArtifactKey(), "PolicyTemplate", stateMapEmpty, "FirstState")); - - AxPolicy policy = new AxPolicy(); - - final AxArtifactKey policyKey = new AxArtifactKey("PolicyName", "0.0.1"); - - final AxState firstState = new AxState(new AxReferenceKey(policy.getKey(), "FirstState")); - final AxState badState = new AxState(new AxReferenceKey(policy.getKey(), "BadState")); - final AxStateOutput badStateOutput = new AxStateOutput(badState.getKey(), AxArtifactKey.getNullKey(), - new AxReferenceKey(policyKey, "BadNextState")); - badState.getStateOutputs().put(badStateOutput.getKey().getLocalName(), badStateOutput); - stateMap.put(firstState.getKey().getLocalName(), firstState); - - try { - policy.setKey(null); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("key may not be null", e.getMessage()); - } - - policy.setKey(policyKey); - assertEquals("PolicyName:0.0.1", policy.getKey().getId()); - assertEquals("PolicyName:0.0.1", policy.getKeys().get(0).getId()); - - try { - policy.setTemplate(null); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("template may not be null", e.getMessage()); - } - - policy.setTemplate("PolicyTemplate"); - assertEquals("PolicyTemplate", policy.getTemplate()); - - try { - policy.setStateMap(null); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("stateMap may not be null", e.getMessage()); - } - - policy.setStateMap(stateMap); - assertEquals(stateMap, policy.getStateMap()); - - try { - policy.setFirstState(null); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("firstState may not be null", e.getMessage()); - } - - policy.setFirstState("FirstState"); - assertEquals("FirstState", policy.getFirstState()); - - assertEquals("PolicyName:0.0.1", policy.getKeys().get(0).getId()); - - policy = new TestApexPolicyModelCreator().getModel().getPolicies().get("policy"); - - AxValidationResult result = new AxValidationResult(); - result = policy.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - final AxArtifactKey savedPolicyKey = policy.getKey(); - policy.setKey(AxArtifactKey.getNullKey()); - result = new AxValidationResult(); - result = policy.validate(result); - assertEquals(ValidationResult.INVALID, result.getValidationResult()); - - policy.setKey(savedPolicyKey); - result = new AxValidationResult(); - result = policy.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - final String savedTemplate = policy.getTemplate(); - policy.setTemplate(""); - result = new AxValidationResult(); - result = policy.validate(result); - assertEquals(ValidationResult.OBSERVATION, result.getValidationResult()); - - policy.setTemplate(savedTemplate); - result = new AxValidationResult(); - result = policy.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - final Map savedStateMap = policy.getStateMap(); - - policy.setStateMap(stateMapEmpty); - result = new AxValidationResult(); - result = policy.validate(result); - assertEquals(ValidationResult.INVALID, result.getValidationResult()); - - policy.setStateMap(savedStateMap); - result = new AxValidationResult(); - result = policy.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - savedStateMap.put(AxKey.NULL_KEY_NAME, firstState); - result = new AxValidationResult(); - result = policy.validate(result); - assertEquals(ValidationResult.INVALID, result.getValidationResult()); - - savedStateMap.remove(AxKey.NULL_KEY_NAME); - result = new AxValidationResult(); - result = policy.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - savedStateMap.put("NullState", null); - result = new AxValidationResult(); - result = policy.validate(result); - assertEquals(ValidationResult.INVALID, result.getValidationResult()); - - savedStateMap.remove("NullState"); - result = new AxValidationResult(); - result = policy.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - savedStateMap.put("BadStateKey", firstState); - result = new AxValidationResult(); - result = policy.validate(result); - assertEquals(ValidationResult.INVALID, result.getValidationResult()); - - savedStateMap.remove("BadStateKey"); - result = new AxValidationResult(); - result = policy.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - savedStateMap.put(badState.getKey().getLocalName(), badState); - result = new AxValidationResult(); - result = policy.validate(result); - assertEquals(ValidationResult.INVALID, result.getValidationResult()); - - savedStateMap.remove(badState.getKey().getLocalName()); - result = new AxValidationResult(); - result = policy.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - final String savedFirstState = policy.getFirstState(); - - policy.setFirstState(""); - result = new AxValidationResult(); - result = policy.validate(result); - assertEquals(ValidationResult.INVALID, result.getValidationResult()); - - policy.setFirstState(savedFirstState); - result = new AxValidationResult(); - result = policy.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - policy.setFirstState("NonExistantFirstState"); - result = new AxValidationResult(); - result = policy.validate(result); - assertEquals(ValidationResult.INVALID, result.getValidationResult()); - - policy.setFirstState(savedFirstState); - result = new AxValidationResult(); - result = policy.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - final AxState clonedState = new AxState(policy.getStateMap().get("state")); - clonedState.getKey().setLocalName("ClonedState"); - clonedState.afterUnmarshal(null, null); - - savedStateMap.put(clonedState.getKey().getLocalName(), clonedState); - result = new AxValidationResult(); - result = policy.validate(result); - assertEquals(ValidationResult.WARNING, result.getValidationResult()); - - savedStateMap.remove(clonedState.getKey().getLocalName()); - result = new AxValidationResult(); - result = policy.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - policy.clean(); - - final AxPolicy clonedPolicy = new AxPolicy(policy); - assertEquals("AxPolicy:(key=AxArtifactKey:(name=policy,version=0.0.1),template=FREEFORM,sta", - clonedPolicy.toString().substring(0, 77)); - - assertFalse(policy.hashCode() == 0); - - assertTrue(policy.equals(policy)); - assertTrue(policy.equals(clonedPolicy)); - assertFalse(policy.equals(null)); - assertFalse(policy.equals("Hello")); - assertFalse(policy.equals( - new AxPolicy(AxArtifactKey.getNullKey(), savedTemplate, savedStateMap, savedFirstState))); - assertFalse(policy.equals(new AxPolicy(savedPolicyKey, "SomeTemplate", savedStateMap, savedFirstState))); - assertFalse(policy.equals(new AxPolicy(savedPolicyKey, savedTemplate, stateMapEmpty, savedFirstState))); - assertFalse(policy.equals(new AxPolicy(savedPolicyKey, savedTemplate, savedStateMap, "SomeFirstState"))); - assertTrue(policy.equals(new AxPolicy(savedPolicyKey, savedTemplate, savedStateMap, savedFirstState))); - - assertEquals(0, policy.compareTo(policy)); - assertEquals(0, policy.compareTo(clonedPolicy)); - assertNotEquals(0, policy.compareTo(new AxArtifactKey())); - assertNotEquals(0, policy.compareTo(null)); - assertNotEquals(0, policy.compareTo( - new AxPolicy(AxArtifactKey.getNullKey(), savedTemplate, savedStateMap, savedFirstState))); - assertNotEquals(0, - policy.compareTo(new AxPolicy(savedPolicyKey, "SomeTemplate", savedStateMap, savedFirstState))); - assertNotEquals(0, - policy.compareTo(new AxPolicy(savedPolicyKey, savedTemplate, stateMapEmpty, savedFirstState))); - assertNotEquals(0, - policy.compareTo(new AxPolicy(savedPolicyKey, savedTemplate, savedStateMap, "SomeFirstState"))); - assertEquals(0, policy.compareTo(new AxPolicy(savedPolicyKey, savedTemplate, savedStateMap, savedFirstState))); - - assertNotNull(policy.getKeys()); - - final AxPolicies policies = new AxPolicies(); - result = new AxValidationResult(); - result = policies.validate(result); - assertEquals(ValidationResult.INVALID, result.getValidationResult()); - - // Invalid, no events in event map - policies.setKey(new AxArtifactKey("PoliciesKey", "0.0.1")); - assertEquals("PoliciesKey:0.0.1", policies.getKey().getId()); - - result = new AxValidationResult(); - result = policies.validate(result); - assertEquals(ValidationResult.INVALID, result.getValidationResult()); - - policies.getPolicyMap().put(savedPolicyKey, policy); - result = new AxValidationResult(); - result = policies.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - policies.getPolicyMap().put(AxArtifactKey.getNullKey(), null); - result = new AxValidationResult(); - result = policies.validate(result); - assertEquals(ValidationResult.INVALID, result.getValidationResult()); - - policies.getPolicyMap().remove(AxArtifactKey.getNullKey()); - result = new AxValidationResult(); - result = policies.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - policies.getPolicyMap().put(new AxArtifactKey("NullValueKey", "0.0.1"), null); - result = new AxValidationResult(); - result = policies.validate(result); - assertEquals(ValidationResult.INVALID, result.getValidationResult()); - - policies.getPolicyMap().remove(new AxArtifactKey("NullValueKey", "0.0.1")); - result = new AxValidationResult(); - result = policies.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - policies.getPolicyMap().put(new AxArtifactKey("BadEventKey", "0.0.1"), policy); - result = new AxValidationResult(); - result = policies.validate(result); - assertEquals(ValidationResult.INVALID, result.getValidationResult()); - - policies.getPolicyMap().remove(new AxArtifactKey("BadEventKey", "0.0.1")); - result = new AxValidationResult(); - result = policies.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - policies.clean(); - policies.afterUnmarshal(null, null); - - final AxPolicies clonedPolicies = new AxPolicies(policies); - assertEquals("AxPolicies:(key=AxArtifactKey:(name=PoliciesKey,version=0.0.", - clonedPolicies.toString().substring(0, 60)); - - assertFalse(policies.hashCode() == 0); - - assertTrue(policies.equals(policies)); - assertTrue(policies.equals(clonedPolicies)); - assertFalse(policies.equals(null)); - assertFalse(policies.equals("Hello")); - assertFalse(policies.equals(new AxPolicies(new AxArtifactKey()))); - - assertEquals(0, policies.compareTo(policies)); - assertEquals(0, policies.compareTo(clonedPolicies)); - assertNotEquals(0, policies.compareTo(null)); - assertNotEquals(0, policies.compareTo(new AxArtifactKey())); - assertNotEquals(0, policies.compareTo(new AxPolicies(new AxArtifactKey()))); - - clonedPolicies.get(savedPolicyKey).setTemplate("AnotherTemplate"); - assertNotEquals(0, policies.compareTo(clonedPolicies)); - - assertEquals(policies.getKey(), policies.getKeys().get(0)); - - assertEquals("policy", policies.get("policy").getKey().getName()); - assertEquals("policy", policies.get("policy", "0.0.1").getKey().getName()); - assertEquals(1, policies.getAll("policy", "0.0.1").size()); - assertEquals(0, policies.getAll("NonExistantPolicy").size()); - - AxStateTree stateTree = policy.getStateTree(); - assertNotNull(stateTree); - assertNotNull(stateTree.getReferencedStateList()); - assertNotNull(stateTree.getReferencedStateSet()); - - final AxState secondState = new AxState(policy.getStateMap().get("state")); - secondState.getKey().setLocalName("SecondState"); - secondState.afterUnmarshal(null, null); - policy.getStateMap().put("SecondState", secondState); - policy.getStateMap().get("state").getStateOutputs().get("stateOutput0").setNextState(secondState.getKey()); - - stateTree = policy.getStateTree(); - assertNotNull(stateTree); - assertNotNull(stateTree.getReferencedStateList()); - assertNotNull(stateTree.getReferencedStateSet()); - assertNotNull(stateTree.getNextStates()); - - policy.getStateMap().get("SecondState").getStateOutputs().get("stateOutput0") - .setNextState(policy.getStateMap().get("state").getKey()); - try { - policy.getStateTree(); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("loop detected in state tree for policy policy:0.0.1 state SecondState, " - + "next state state referenced more than once", e.getMessage()); - } - - policy.getStateMap().get("SecondState").getStateOutputs().get("stateOutput0") - .setNextState(AxReferenceKey.getNullKey()); - - final AxState thirdState = new AxState(policy.getStateMap().get("state")); - thirdState.getKey().setLocalName("ThirdState"); - thirdState.afterUnmarshal(null, null); - policy.getStateMap().put("ThirdState", thirdState); - policy.getStateMap().get("SecondState").getStateOutputs().get("stateOutput0").setNextState(thirdState.getKey()); - policy.getStateMap().get("ThirdState").getStateOutputs().get("stateOutput0") - .setNextState(AxReferenceKey.getNullKey()); - - stateTree = policy.getStateTree(); - - final AxStateOutput ssS0Clone = new AxStateOutput( - policy.getStateMap().get("SecondState").getStateOutputs().get("stateOutput0")); - ssS0Clone.getKey().setLocalName("ssS0Clone"); - policy.getStateMap().get("SecondState").getStateOutputs().put("ssS0Clone", ssS0Clone); - - try { - policy.getStateTree(); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("loop detected in state tree for policy policy:0.0.1 state SecondState, " - + "next state ThirdState referenced more than once", e.getMessage()); - } - - policy.getStateMap().get("SecondState").getStateOutputs().remove("ssS0Clone"); - - policy.getStateMap().get("state").getStateOutputs().get("stateOutput0").setNextState(secondState.getKey()); - secondState.getStateOutputs().get("stateOutput0").setNextState(thirdState.getKey()); - thirdState.getStateOutputs().get("stateOutput0").setNextState(AxReferenceKey.getNullKey()); - - stateTree = policy.getStateTree(); - assertNotNull(stateTree.getState()); - - thirdState.getStateOutputs().get("stateOutput0").setNextState(secondState.getKey()); - - try { - policy.getStateTree(); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("loop detected in state tree for policy policy:0.0.1 state ThirdState, " - + "next state SecondState referenced more than once", e.getMessage()); - } - - thirdState.getStateOutputs().get("stateOutput0").setNextState(AxReferenceKey.getNullKey()); - - stateTree = policy.getStateTree(); - - final AxStateTree otherStateTree = policy.getStateTree(); - assertEquals(0, stateTree.compareTo(otherStateTree)); - - for (final AxStateTree childStateTree : stateTree.getNextStates()) { - assertNotEquals(0, stateTree.compareTo(childStateTree)); - } - - otherStateTree.getNextStates().clear(); - assertNotEquals(0, stateTree.compareTo(otherStateTree)); - } -} diff --git a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/TestPolicyModel.java b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/TestPolicyModel.java deleted file mode 100644 index b2803c4f8..000000000 --- a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/TestPolicyModel.java +++ /dev/null @@ -1,356 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-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.apex.model.policymodel.concepts; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -import org.junit.Test; -import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; -import org.onap.policy.apex.model.basicmodel.concepts.AxKeyInformation; -import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey; -import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult; -import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult; -import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbum; -import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbums; -import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas; -import org.onap.policy.apex.model.eventmodel.concepts.AxEvents; -import org.onap.policy.apex.model.eventmodel.concepts.AxField; -import org.onap.policy.apex.model.eventmodel.concepts.AxInputField; -import org.onap.policy.apex.model.eventmodel.concepts.AxOutputField; -import org.onap.policy.apex.model.policymodel.concepts.AxPolicies; -import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; -import org.onap.policy.apex.model.policymodel.concepts.AxStateOutput; -import org.onap.policy.apex.model.policymodel.concepts.AxStateTaskOutputType; -import org.onap.policy.apex.model.policymodel.concepts.AxStateTaskReference; -import org.onap.policy.apex.model.policymodel.concepts.AxTaskSelectionLogic; -import org.onap.policy.apex.model.policymodel.concepts.AxTasks; -import org.onap.policy.apex.model.policymodel.handling.TestApexPolicyModelCreator; - -/** - * Test policy models. - * - * @author Liam Fallon (liam.fallon@ericsson.com) - */ -public class TestPolicyModel { - - @Test - public void testPolicyModel() { - assertNotNull(new AxPolicyModel()); - assertNotNull(new AxPolicyModel(new AxArtifactKey())); - assertNotNull(new AxPolicyModel(new AxArtifactKey(), new AxContextSchemas(), new AxKeyInformation(), - new AxEvents(), new AxContextAlbums(), new AxTasks(), new AxPolicies())); - - final AxArtifactKey modelKey = new AxArtifactKey("ModelKey", "0.0.1"); - final AxArtifactKey schemasKey = new AxArtifactKey("SchemasKey", "0.0.1"); - final AxArtifactKey eventsKey = new AxArtifactKey("EventsKey", "0.0.1"); - final AxArtifactKey keyInfoKey = new AxArtifactKey("SchemasKey", "0.0.1"); - final AxArtifactKey albumsKey = new AxArtifactKey("AlbumsKey", "0.0.1"); - final AxArtifactKey tasksKey = new AxArtifactKey("TasksKey", "0.0.1"); - final AxArtifactKey policiesKey = new AxArtifactKey("PoliciesKey", "0.0.1"); - - AxPolicyModel model = new AxPolicyModel(modelKey, new AxContextSchemas(schemasKey), - new AxKeyInformation(keyInfoKey), new AxEvents(eventsKey), new AxContextAlbums(albumsKey), - new AxTasks(tasksKey), new AxPolicies(policiesKey)); - - model.register(); - - assertNotNull(model.getContextModel()); - assertEquals("ModelKey:0.0.1", model.getKeys().get(0).getId()); - - model.clean(); - assertNotNull(model); - assertEquals("AxPolicyModel:(AxPolicyModel:(key=AxArtifactKey:(n", model.toString().substring(0, 50)); - - final AxPolicyModel clonedModel = new AxPolicyModel(model); - - assertFalse(model.hashCode() == 0); - - assertTrue(model.equals(model)); - assertTrue(model.equals(clonedModel)); - assertFalse(model.equals("Hello")); - assertFalse(model.equals(new AxPolicyModel(new AxArtifactKey()))); - assertFalse(model.equals(new AxPolicyModel(AxArtifactKey.getNullKey(), new AxContextSchemas(schemasKey), - new AxKeyInformation(keyInfoKey), new AxEvents(eventsKey), new AxContextAlbums(albumsKey), - new AxTasks(tasksKey), new AxPolicies(policiesKey)))); - assertFalse(model.equals(new AxPolicyModel(modelKey, new AxContextSchemas(), new AxKeyInformation(keyInfoKey), - new AxEvents(eventsKey), new AxContextAlbums(albumsKey), new AxTasks(tasksKey), - new AxPolicies(policiesKey)))); - assertFalse(model.equals(new AxPolicyModel(modelKey, new AxContextSchemas(schemasKey), new AxKeyInformation(), - new AxEvents(eventsKey), new AxContextAlbums(albumsKey), new AxTasks(tasksKey), - new AxPolicies(policiesKey)))); - assertFalse(model.equals(new AxPolicyModel(modelKey, new AxContextSchemas(schemasKey), - new AxKeyInformation(keyInfoKey), new AxEvents(), new AxContextAlbums(albumsKey), - new AxTasks(tasksKey), new AxPolicies(policiesKey)))); - assertFalse(model.equals(new AxPolicyModel(modelKey, new AxContextSchemas(schemasKey), - new AxKeyInformation(keyInfoKey), new AxEvents(eventsKey), new AxContextAlbums(), - new AxTasks(tasksKey), new AxPolicies(policiesKey)))); - assertFalse(model.equals(new AxPolicyModel(modelKey, new AxContextSchemas(schemasKey), - new AxKeyInformation(keyInfoKey), new AxEvents(eventsKey), new AxContextAlbums(albumsKey), - new AxTasks(), new AxPolicies(policiesKey)))); - assertFalse(model.equals(new AxPolicyModel(modelKey, new AxContextSchemas(schemasKey), - new AxKeyInformation(keyInfoKey), new AxEvents(eventsKey), new AxContextAlbums(albumsKey), - new AxTasks(tasksKey), new AxPolicies()))); - assertTrue(model.equals(new AxPolicyModel(modelKey, new AxContextSchemas(schemasKey), - new AxKeyInformation(keyInfoKey), new AxEvents(eventsKey), new AxContextAlbums(albumsKey), - new AxTasks(tasksKey), new AxPolicies(policiesKey)))); - - assertEquals(0, model.compareTo(model)); - assertEquals(0, model.compareTo(clonedModel)); - assertNotEquals(0, model.compareTo(new AxArtifactKey())); - assertNotEquals(0, model.compareTo(new AxPolicyModel(AxArtifactKey.getNullKey(), - new AxContextSchemas(schemasKey), new AxKeyInformation(keyInfoKey), new AxEvents(eventsKey), - new AxContextAlbums(albumsKey), new AxTasks(tasksKey), new AxPolicies(policiesKey)))); - assertNotEquals(0, model.compareTo(new AxPolicyModel(modelKey, new AxContextSchemas(), - new AxKeyInformation(keyInfoKey), new AxEvents(eventsKey), new AxContextAlbums(albumsKey), - new AxTasks(tasksKey), new AxPolicies(policiesKey)))); - assertNotEquals(0, - model.compareTo(new AxPolicyModel(modelKey, new AxContextSchemas(schemasKey), - new AxKeyInformation(), new AxEvents(eventsKey), new AxContextAlbums(albumsKey), - new AxTasks(tasksKey), new AxPolicies(policiesKey)))); - assertNotEquals(0, model.compareTo(new AxPolicyModel(modelKey, new AxContextSchemas(schemasKey), - new AxKeyInformation(keyInfoKey), new AxEvents(), new AxContextAlbums(albumsKey), - new AxTasks(tasksKey), new AxPolicies(policiesKey)))); - assertNotEquals(0, - model.compareTo(new AxPolicyModel(modelKey, new AxContextSchemas(schemasKey), - new AxKeyInformation(keyInfoKey), new AxEvents(eventsKey), - new AxContextAlbums(), new AxTasks(tasksKey), new AxPolicies(policiesKey)))); - assertNotEquals(0, - model.compareTo(new AxPolicyModel(modelKey, new AxContextSchemas(schemasKey), - new AxKeyInformation(keyInfoKey), new AxEvents(eventsKey), - new AxContextAlbums(albumsKey), new AxTasks(), new AxPolicies(policiesKey)))); - assertNotEquals(0, - model.compareTo(new AxPolicyModel(modelKey, new AxContextSchemas(schemasKey), - new AxKeyInformation(keyInfoKey), new AxEvents(eventsKey), - new AxContextAlbums(albumsKey), new AxTasks(tasksKey), new AxPolicies()))); - assertEquals(0, model.compareTo(new AxPolicyModel(modelKey, new AxContextSchemas(schemasKey), - new AxKeyInformation(keyInfoKey), new AxEvents(eventsKey), new AxContextAlbums(albumsKey), - new AxTasks(tasksKey), new AxPolicies(policiesKey)))); - - model = new TestApexPolicyModelCreator().getModel(); - - AxValidationResult result = new AxValidationResult(); - result = model.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - final AxArtifactKey savedPolicyKey = model.getKey(); - model.setKey(AxArtifactKey.getNullKey()); - result = new AxValidationResult(); - result = model.validate(result); - assertEquals(ValidationResult.INVALID, result.getValidationResult()); - - model.setKey(savedPolicyKey); - result = new AxValidationResult(); - result = model.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - final AxField badField = new AxField(new AxReferenceKey(model.getEvents().get("inEvent").getKey(), "BadField"), - new AxArtifactKey("NonExistantSchema", "0.0.1")); - model.getEvents().get("inEvent").getParameterMap().put(badField.getKey().getLocalName(), badField); - result = new AxValidationResult(); - result = model.validate(result); - assertEquals(ValidationResult.INVALID, result.getValidationResult()); - - model.getEvents().get("inEvent").getParameterMap().remove(badField.getKey().getLocalName()); - result = new AxValidationResult(); - result = model.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - final AxContextAlbum badAlbum = new AxContextAlbum(new AxArtifactKey("BadAlbum", "0.0.1"), "SomeScope", true, - new AxArtifactKey("NonExistantSchema", "0.0.1")); - model.getAlbums().getAlbumsMap().put(badAlbum.getKey(), badAlbum); - result = new AxValidationResult(); - result = model.validate(result); - assertEquals(ValidationResult.INVALID, result.getValidationResult()); - - model.getAlbums().getAlbumsMap().remove(badAlbum.getKey()); - result = new AxValidationResult(); - result = model.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - final AxInputField badInField = new AxInputField( - new AxReferenceKey(model.getTasks().get("task").getKey(), "BadInField"), - new AxArtifactKey("NonExistantSchema", "0.0.1")); - model.getTasks().get("task").getInputFields().put(badInField.getKey().getLocalName(), badInField); - result = new AxValidationResult(); - result = model.validate(result); - assertEquals(ValidationResult.INVALID, result.getValidationResult()); - - model.getTasks().get("task").getInputFields().remove(badInField.getKey().getLocalName()); - result = new AxValidationResult(); - result = model.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - final AxOutputField badOutField = new AxOutputField( - new AxReferenceKey(model.getTasks().get("task").getKey(), "BadOutField"), - new AxArtifactKey("NonExistantSchema", "0.0.1")); - model.getTasks().get("task").getOutputFields().put(badOutField.getKey().getLocalName(), badOutField); - result = new AxValidationResult(); - result = model.validate(result); - assertEquals(ValidationResult.INVALID, result.getValidationResult()); - - model.getTasks().get("task").getOutputFields().remove(badOutField.getKey().getLocalName()); - result = new AxValidationResult(); - result = model.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - model.getTasks().get("task").getContextAlbumReferences() - .add(new AxArtifactKey("NonExistantContextAlbum", "0.0.1")); - result = new AxValidationResult(); - result = model.validate(result); - assertEquals(ValidationResult.INVALID, result.getValidationResult()); - - model.getTasks().get("task").getContextAlbumReferences() - .remove(new AxArtifactKey("NonExistantContextAlbum", "0.0.1")); - result = new AxValidationResult(); - result = model.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - model.getPolicies().get("policy").getStateMap().get("state").getContextAlbumReferences() - .add(new AxArtifactKey("NonExistantContextAlbum", "0.0.1")); - result = new AxValidationResult(); - result = model.validate(result); - assertEquals(ValidationResult.INVALID, result.getValidationResult()); - - model.getPolicies().get("policy").getStateMap().get("state").getContextAlbumReferences() - .remove(new AxArtifactKey("NonExistantContextAlbum", "0.0.1")); - result = new AxValidationResult(); - result = model.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - final AxArtifactKey savedTrigger = model.getPolicies().get("policy").getStateMap().get("state").getTrigger(); - model.getPolicies().get("policy").getStateMap().get("state") - .setTrigger(new AxArtifactKey("NonExistantEvent", "0.0.1")); - result = new AxValidationResult(); - result = model.validate(result); - assertEquals(ValidationResult.INVALID, result.getValidationResult()); - - model.getPolicies().get("policy").getStateMap().get("state").setTrigger(savedTrigger); - result = new AxValidationResult(); - result = model.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - final AxArtifactKey savedDefaultTask = model.getPolicies().get("policy").getStateMap().get("state") - .getDefaultTask(); - model.getPolicies().get("policy").getStateMap().get("state") - .setDefaultTask(new AxArtifactKey("NonExistantTask", "0.0.1")); - result = new AxValidationResult(); - result = model.validate(result); - assertEquals(ValidationResult.INVALID, result.getValidationResult()); - - model.getPolicies().get("policy").getStateMap().get("state").setDefaultTask(savedDefaultTask); - result = new AxValidationResult(); - result = model.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - // It is OK not to have TSL - final AxTaskSelectionLogic savedTaskSelectionLogic = model.getPolicies().get("policy").getStateMap() - .get("state").getTaskSelectionLogic(); - model.getPolicies().get("policy").getStateMap().get("state") - .setTaskSelectionLogic(new AxTaskSelectionLogic(AxReferenceKey.getNullKey())); - result = new AxValidationResult(); - result = model.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - model.getTasks().get("task").getInputFields().put(badInField.getKey().getLocalName(), badInField); - result = new AxValidationResult(); - result = model.validate(result); - assertEquals(ValidationResult.INVALID, result.getValidationResult()); - - model.getTasks().get("task").getInputFields().remove(badInField.getKey().getLocalName()); - result = new AxValidationResult(); - result = model.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - model.getPolicies().get("policy").getStateMap().get("state").setTaskSelectionLogic(savedTaskSelectionLogic); - result = new AxValidationResult(); - result = model.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - final AxStateOutput badStateOutput = new AxStateOutput( - new AxReferenceKey(model.getPolicies().get("policy").getStateMap().get("state").getKey(), - "BadSO"), - new AxArtifactKey("NonExistantEvent", "0.0.1"), AxReferenceKey.getNullKey()); - model.getPolicies().get("policy").getStateMap().get("state").getStateOutputs() - .put(badStateOutput.getKey().getLocalName(), badStateOutput); - result = new AxValidationResult(); - result = model.validate(result); - assertEquals(ValidationResult.INVALID, result.getValidationResult()); - - model.getPolicies().get("policy").getStateMap().get("state").getStateOutputs() - .remove(badStateOutput.getKey().getLocalName()); - result = new AxValidationResult(); - result = model.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - final AxStateTaskReference badTaskReference = new AxStateTaskReference( - new AxReferenceKey(model.getPolicies().get("policy").getStateMap().get("state").getKey(), - "NonExistantTask"), - AxStateTaskOutputType.LOGIC, badStateOutput.getKey()); - model.getPolicies().get("policy").getStateMap().get("state").getTaskReferences() - .put(new AxArtifactKey("NonExistantTask", "0.0.1"), badTaskReference); - result = new AxValidationResult(); - result = model.validate(result); - assertEquals(ValidationResult.INVALID, result.getValidationResult()); - - badTaskReference.setStateTaskOutputType(AxStateTaskOutputType.DIRECT); - result = new AxValidationResult(); - result = model.validate(result); - assertEquals(ValidationResult.INVALID, result.getValidationResult()); - - model.getPolicies().get("policy").getStateMap().get("state").getTaskReferences() - .remove(new AxArtifactKey("NonExistantTask", "0.0.1")); - result = new AxValidationResult(); - result = model.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - final AxStateTaskReference tr = model.getPolicies().get("policy").getStateMap().get("state").getTaskReferences() - .get(new AxArtifactKey("task", "0.0.1")); - - final String savedStateOutputName = tr.getOutput().getLocalName(); - tr.getOutput().setLocalName("NonExistantOutput"); - result = new AxValidationResult(); - result = model.validate(result); - assertEquals(ValidationResult.INVALID, result.getValidationResult()); - - tr.getOutput().setLocalName(savedStateOutputName); - result = new AxValidationResult(); - result = model.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - final AxStateOutput so = model.getPolicies().get("policy").getStateMap().get("state").getStateOutputs() - .get(savedStateOutputName); - - final AxArtifactKey savedOutEvent = so.getOutgingEvent(); - so.setOutgoingEvent(new AxArtifactKey("NonExistantEvent", "0.0.1")); - result = new AxValidationResult(); - result = model.validate(result); - assertEquals(ValidationResult.INVALID, result.getValidationResult()); - - so.setOutgoingEvent(savedOutEvent); - result = new AxValidationResult(); - result = model.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - } -} diff --git a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/TestState.java b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/TestState.java deleted file mode 100644 index ea32c9ca6..000000000 --- a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/TestState.java +++ /dev/null @@ -1,501 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-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.apex.model.policymodel.concepts; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - -import java.util.Set; -import java.util.TreeMap; -import java.util.TreeSet; - -import org.junit.Test; -import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; -import org.onap.policy.apex.model.basicmodel.concepts.AxKey; -import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey; -import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult; -import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult; - -/** - * Test policy states. - * - * @author Liam Fallon (liam.fallon@ericsson.com) - */ -public class TestState { - - @Test - public void testState() { - final TreeMap soEmptyMap = new TreeMap<>(); - final TreeSet ctxtEmptySet = new TreeSet<>(); - final TreeMap sflEmptyMap = new TreeMap<>(); - final TreeMap trEmptyMap = new TreeMap<>(); - - final TreeMap soMap = new TreeMap<>(); - final TreeSet ctxtSet = new TreeSet<>(); - final TreeMap sflMap = new TreeMap<>(); - final TreeMap trMap = new TreeMap<>(); - - assertNotNull(new AxState()); - assertNotNull(new AxState(new AxReferenceKey())); - assertNotNull(new AxState(new AxStateParamsBuilder().key(new AxReferenceKey()).trigger(new AxArtifactKey()) - .stateOutputs(soEmptyMap).contextAlbumReferenceSet(ctxtEmptySet) - .taskSelectionLogic(new AxTaskSelectionLogic()).stateFinalizerLogicMap(sflEmptyMap) - .defaultTask(new AxArtifactKey()).taskReferenceMap(trEmptyMap))); - - final AxState state = new AxState(); - - final AxReferenceKey stateKey = new AxReferenceKey("PolicyName", "0.0.1", "StateName"); - final AxReferenceKey stateKeyNext = new AxReferenceKey("PolicyName", "0.0.1", "StateNameNext"); - final AxReferenceKey stateKeyBad = new AxReferenceKey("PolicyName", "0.0.1", "BadStateName"); - final AxArtifactKey triggerKey = new AxArtifactKey("TriggerName", "0.0.1"); - final AxTaskSelectionLogic tsl = new AxTaskSelectionLogic(stateKey, "TSL", "LogicFlavour", "Some Logic"); - final AxArtifactKey defTaskKey = new AxArtifactKey("TaskName", "0.0.1"); - final AxArtifactKey taskKey1 = new AxArtifactKey("Task1", "0.0.1"); - final AxArtifactKey taskKey2 = new AxArtifactKey("Task2", "0.0.1"); - final AxArtifactKey taskKeyBad = new AxArtifactKey("TaskBad", "0.0.1"); - - try { - state.setKey(null); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("key may not be null", e.getMessage()); - } - - state.setKey(stateKey); - assertEquals("PolicyName:0.0.1:NULL:StateName", state.getKey().getId()); - assertEquals("PolicyName:0.0.1:NULL:StateName", state.getKeys().get(0).getId()); - - final AxStateOutput so0 = new AxStateOutput(new AxReferenceKey(stateKey, "SO0"), triggerKey, - new AxReferenceKey()); - final AxStateOutput soU = new AxStateOutput(new AxReferenceKey(stateKey, "SOU"), triggerKey, stateKeyNext); - final AxStateOutput soSame = new AxStateOutput(new AxReferenceKey(stateKey, "SOU"), triggerKey, stateKey); - final AxArtifactKey cr0 = new AxArtifactKey("ContextReference", "0.0.1"); - final AxStateFinalizerLogic sfl = new AxStateFinalizerLogic(stateKey, "SFLogicName", "LogicFlavour", "Logic"); - final AxStateFinalizerLogic sflU = new AxStateFinalizerLogic(stateKey, "UnusedSFLogicName", "LogicFlavour", - "Logic"); - final AxStateTaskReference str0 = new AxStateTaskReference(new AxReferenceKey(stateKey, "STR0"), - AxStateTaskOutputType.DIRECT, so0.getKey()); - final AxStateTaskReference str1 = new AxStateTaskReference(new AxReferenceKey(stateKey, "STR1"), - AxStateTaskOutputType.DIRECT, so0.getKey()); - final AxStateTaskReference str2 = new AxStateTaskReference(new AxReferenceKey(stateKey, "STR2"), - AxStateTaskOutputType.LOGIC, sfl.getKey()); - - final AxStateTaskReference strBadState = new AxStateTaskReference(new AxReferenceKey(stateKeyBad, "STR2"), - AxStateTaskOutputType.LOGIC, sfl.getKey()); - final AxStateTaskReference strBadStateOutput = new AxStateTaskReference(new AxReferenceKey(stateKey, "STR2"), - AxStateTaskOutputType.UNDEFINED, sfl.getKey()); - final AxStateTaskReference strBadStateFinalizerLogic = new AxStateTaskReference( - new AxReferenceKey(stateKeyBad, "STR2"), AxStateTaskOutputType.LOGIC, - new AxReferenceKey(stateKey, "SomeSFL")); - - soMap.put(so0.getKey().getLocalName(), so0); - ctxtSet.add(cr0); - sflMap.put(sfl.getKey().getLocalName(), sfl); - trMap.put(defTaskKey.getKey(), str0); - trMap.put(taskKey1.getKey(), str1); - trMap.put(taskKey2.getKey(), str2); - - try { - state.setTrigger(null); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("trigger may not be null", e.getMessage()); - } - - state.setTrigger(triggerKey); - assertEquals(triggerKey, state.getTrigger()); - - try { - state.setStateOutputs(null); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("stateOutputs may not be null", e.getMessage()); - } - - state.setStateOutputs(soMap); - assertEquals(soMap, state.getStateOutputs()); - - try { - state.setContextAlbumReferences(null); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("contextAlbumReferenceSet may not be null", e.getMessage()); - } - - state.setContextAlbumReferences(ctxtSet); - assertEquals(ctxtSet, state.getContextAlbumReferences()); - - try { - state.setTaskSelectionLogic(null); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("taskSelectionLogic may not be null", e.getMessage()); - } - - assertEquals(false, state.checkSetTaskSelectionLogic()); - state.setTaskSelectionLogic(tsl); - assertEquals(tsl, state.getTaskSelectionLogic()); - assertEquals(true, state.checkSetTaskSelectionLogic()); - - try { - state.setStateFinalizerLogicMap(null); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("stateFinalizerLogic may not be null", e.getMessage()); - } - - state.setStateFinalizerLogicMap(sflMap); - assertEquals(sflMap, state.getStateFinalizerLogicMap()); - - try { - state.setDefaultTask(null); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("defaultTask may not be null", e.getMessage()); - } - - state.setDefaultTask(defTaskKey); - assertEquals(defTaskKey, state.getDefaultTask()); - - try { - state.setTaskReferences(null); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("taskReferenceMap may not be null", e.getMessage()); - } - - state.setTaskReferences(trMap); - assertEquals(trMap, state.getTaskReferences()); - - state.afterUnmarshal(null, null); - assertEquals(state.getKey(), state.getKeys().get(0)); - state.getTaskSelectionLogic().getKey().setLocalName(AxKey.NULL_KEY_NAME); - state.afterUnmarshal(null, null); - assertEquals(state.getKey(), state.getKeys().get(0)); - - final Set stateSet = state.getNextStateSet(); - assertEquals(1, stateSet.size()); - - AxValidationResult result = new AxValidationResult(); - result = state.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - state.setKey(AxReferenceKey.getNullKey()); - result = new AxValidationResult(); - result = state.validate(result); - assertEquals(ValidationResult.INVALID, result.getValidationResult()); - - state.setKey(stateKey); - result = new AxValidationResult(); - result = state.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - state.setTrigger(AxArtifactKey.getNullKey()); - result = new AxValidationResult(); - result = state.validate(result); - assertEquals(ValidationResult.INVALID, result.getValidationResult()); - - state.setTrigger(triggerKey); - result = new AxValidationResult(); - result = state.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - state.setStateOutputs(soEmptyMap); - result = new AxValidationResult(); - result = state.validate(result); - assertEquals(ValidationResult.INVALID, result.getValidationResult()); - - state.setStateOutputs(soMap); - result = new AxValidationResult(); - result = state.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - so0.getKey().setParentLocalName("Zooby"); - result = new AxValidationResult(); - result = state.validate(result); - assertEquals(ValidationResult.INVALID, result.getValidationResult()); - - so0.getKey().setParentLocalName("StateName"); - state.setStateOutputs(soMap); - result = new AxValidationResult(); - result = state.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - soMap.put("NullOutput", null); - result = new AxValidationResult(); - result = state.validate(result); - assertEquals(ValidationResult.INVALID, result.getValidationResult()); - - soMap.remove("NullOutput"); - state.setStateOutputs(soMap); - result = new AxValidationResult(); - result = state.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - soMap.put("DupOutput", so0); - result = new AxValidationResult(); - result = state.validate(result); - assertEquals(ValidationResult.INVALID, result.getValidationResult()); - - soMap.remove("DupOutput"); - state.setStateOutputs(soMap); - result = new AxValidationResult(); - result = state.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - soMap.put("UnusedOutput", soU); - result = new AxValidationResult(); - result = state.validate(result); - assertEquals(ValidationResult.OBSERVATION, result.getValidationResult()); - - soMap.remove("UnusedOutput"); - state.setStateOutputs(soMap); - result = new AxValidationResult(); - result = state.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - soMap.put("OutputToSameState", soSame); - result = new AxValidationResult(); - result = state.validate(result); - assertEquals(ValidationResult.INVALID, result.getValidationResult()); - - soMap.remove("OutputToSameState"); - state.setStateOutputs(soMap); - result = new AxValidationResult(); - result = state.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - // Empty context reference set is OK - state.setContextAlbumReferences(ctxtEmptySet); - result = new AxValidationResult(); - result = state.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - state.setContextAlbumReferences(ctxtSet); - result = new AxValidationResult(); - result = state.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - ctxtSet.add(AxArtifactKey.getNullKey()); - result = new AxValidationResult(); - result = state.validate(result); - assertEquals(ValidationResult.INVALID, result.getValidationResult()); - - ctxtSet.remove(AxArtifactKey.getNullKey()); - result = new AxValidationResult(); - result = state.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - // Null TSL is OK - state.getTaskSelectionLogic().setKey(AxReferenceKey.getNullKey()); - result = new AxValidationResult(); - result = state.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - state.getTaskSelectionLogic().setKey(new AxReferenceKey(stateKey, "TSL")); - result = new AxValidationResult(); - result = state.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - state.setDefaultTask(AxArtifactKey.getNullKey()); - result = new AxValidationResult(); - result = state.validate(result); - assertEquals(ValidationResult.INVALID, result.getValidationResult()); - - state.setDefaultTask(defTaskKey); - result = new AxValidationResult(); - result = state.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - state.setTaskReferences(trEmptyMap); - result = new AxValidationResult(); - result = state.validate(result); - assertEquals(ValidationResult.INVALID, result.getValidationResult()); - - state.setTaskReferences(trMap); - result = new AxValidationResult(); - result = state.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - trMap.put(AxArtifactKey.getNullKey(), null); - result = new AxValidationResult(); - result = state.validate(result); - assertEquals(ValidationResult.INVALID, result.getValidationResult()); - - trMap.remove(AxArtifactKey.getNullKey()); - result = new AxValidationResult(); - result = state.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - trMap.put(AxArtifactKey.getNullKey(), str0); - result = new AxValidationResult(); - result = state.validate(result); - assertEquals(ValidationResult.INVALID, result.getValidationResult()); - - trMap.remove(AxArtifactKey.getNullKey()); - result = new AxValidationResult(); - result = state.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - trMap.put(taskKeyBad, strBadStateOutput); - result = new AxValidationResult(); - result = state.validate(result); - assertEquals(ValidationResult.INVALID, result.getValidationResult()); - - trMap.remove(taskKeyBad); - result = new AxValidationResult(); - result = state.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - trMap.put(taskKeyBad, strBadState); - result = new AxValidationResult(); - result = state.validate(result); - assertEquals(ValidationResult.INVALID, result.getValidationResult()); - - trMap.remove(taskKeyBad); - result = new AxValidationResult(); - result = state.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - trMap.put(taskKeyBad, strBadStateFinalizerLogic); - result = new AxValidationResult(); - result = state.validate(result); - assertEquals(ValidationResult.INVALID, result.getValidationResult()); - - trMap.remove(taskKeyBad); - result = new AxValidationResult(); - result = state.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - state.setDefaultTask(new AxArtifactKey("NonExistantTask", "0.0.1")); - result = new AxValidationResult(); - result = state.validate(result); - assertEquals(ValidationResult.INVALID, result.getValidationResult()); - - state.setDefaultTask(defTaskKey); - result = new AxValidationResult(); - result = state.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - sflMap.put("NullSFL", null); - result = new AxValidationResult(); - result = state.validate(result); - assertEquals(ValidationResult.INVALID, result.getValidationResult()); - - sflMap.remove("NullSFL"); - result = new AxValidationResult(); - result = state.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - sflMap.put(sflU.getKey().getLocalName(), sflU); - result = new AxValidationResult(); - result = state.validate(result); - assertEquals(ValidationResult.OBSERVATION, result.getValidationResult()); - - sflMap.remove(sflU.getKey().getLocalName()); - result = new AxValidationResult(); - result = state.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - state.clean(); - - final AxState clonedState = new AxState(state); - assertEquals("AxState:(stateKey=AxReferenceKey:(parent", clonedState.toString().substring(0, 40)); - - assertFalse(state.hashCode() == 0); - - assertTrue(state.equals(state)); - assertTrue(state.equals(clonedState)); - assertFalse(state.equals(null)); - assertFalse(state.equals((Object)"Hello")); - assertFalse(state.equals(new AxState(new AxStateParamsBuilder().key(new AxReferenceKey()).trigger(triggerKey) - .stateOutputs(soMap).contextAlbumReferenceSet(ctxtSet).taskSelectionLogic(tsl) - .stateFinalizerLogicMap(sflMap).defaultTask(defTaskKey).taskReferenceMap(trMap)))); - assertFalse(state.equals(new AxState(new AxStateParamsBuilder().key(stateKey).trigger(new AxArtifactKey()) - .stateOutputs(soMap).contextAlbumReferenceSet(ctxtSet).taskSelectionLogic(tsl) - .stateFinalizerLogicMap(sflMap).defaultTask(defTaskKey).taskReferenceMap(trMap)))); - assertFalse(state.equals(new AxState(new AxStateParamsBuilder().key(stateKey).trigger(triggerKey) - .stateOutputs(soEmptyMap).contextAlbumReferenceSet(ctxtSet).taskSelectionLogic(tsl) - .stateFinalizerLogicMap(sflMap).defaultTask(defTaskKey).taskReferenceMap(trMap)))); - assertFalse(state.equals(new AxState(new AxStateParamsBuilder().key(stateKey).trigger(triggerKey) - .stateOutputs(soMap).contextAlbumReferenceSet(ctxtEmptySet).taskSelectionLogic(tsl) - .stateFinalizerLogicMap(sflMap).defaultTask(defTaskKey).taskReferenceMap(trMap)))); - assertFalse(state.equals(new AxState(new AxStateParamsBuilder().key(stateKey).trigger(triggerKey) - .stateOutputs(soMap).contextAlbumReferenceSet(ctxtSet) - .taskSelectionLogic(new AxTaskSelectionLogic()).stateFinalizerLogicMap(sflMap) - .defaultTask(defTaskKey).taskReferenceMap(trMap)))); - assertFalse(state.equals(new AxState(new AxStateParamsBuilder().key(stateKey).trigger(triggerKey) - .stateOutputs(soMap).contextAlbumReferenceSet(ctxtSet).taskSelectionLogic(tsl) - .stateFinalizerLogicMap(sflEmptyMap).defaultTask(defTaskKey).taskReferenceMap(trMap)))); - assertFalse(state.equals(new AxState(new AxStateParamsBuilder().key(stateKey).trigger(triggerKey) - .stateOutputs(soMap).contextAlbumReferenceSet(ctxtSet).taskSelectionLogic(tsl) - .stateFinalizerLogicMap(sflMap).defaultTask(new AxArtifactKey()).taskReferenceMap(trMap)))); - assertFalse(state.equals(new AxState(new AxStateParamsBuilder().key(stateKey).trigger(triggerKey) - .stateOutputs(soMap).contextAlbumReferenceSet(ctxtSet).taskSelectionLogic(tsl) - .stateFinalizerLogicMap(sflMap).defaultTask(defTaskKey).taskReferenceMap(trEmptyMap)))); - assertTrue(state.equals(new AxState(new AxStateParamsBuilder().key(stateKey).trigger(triggerKey) - .stateOutputs(soMap).contextAlbumReferenceSet(ctxtSet).taskSelectionLogic(tsl) - .stateFinalizerLogicMap(sflMap).defaultTask(defTaskKey).taskReferenceMap(trMap)))); - - assertEquals(0, state.compareTo(state)); - assertEquals(0, state.compareTo(clonedState)); - assertNotEquals(0, state.compareTo(new AxArtifactKey())); - assertNotEquals(0, state.compareTo(null)); - assertNotEquals(0, - state.compareTo(new AxState(new AxStateParamsBuilder().key(new AxReferenceKey()) - .trigger(triggerKey).stateOutputs(soMap).contextAlbumReferenceSet(ctxtSet) - .taskSelectionLogic(tsl).stateFinalizerLogicMap(sflMap).defaultTask(defTaskKey) - .taskReferenceMap(trMap)))); - assertNotEquals(0, state.compareTo(new AxState(new AxStateParamsBuilder().key(stateKey) - .trigger(new AxArtifactKey()).stateOutputs(soMap).contextAlbumReferenceSet(ctxtSet) - .taskSelectionLogic(tsl).stateFinalizerLogicMap(sflMap).defaultTask(defTaskKey) - .taskReferenceMap(trMap)))); - assertNotEquals(0, state.compareTo(new AxState(new AxStateParamsBuilder().key(stateKey).trigger(triggerKey) - .stateOutputs(soEmptyMap).contextAlbumReferenceSet(ctxtSet).taskSelectionLogic(tsl) - .stateFinalizerLogicMap(sflMap).defaultTask(defTaskKey).taskReferenceMap(trMap)))); - assertNotEquals(0, state.compareTo(new AxState(new AxStateParamsBuilder().key(stateKey).trigger(triggerKey) - .stateOutputs(soMap).contextAlbumReferenceSet(ctxtEmptySet).taskSelectionLogic(tsl) - .stateFinalizerLogicMap(sflMap).defaultTask(defTaskKey).taskReferenceMap(trMap)))); - assertNotEquals(0, - state.compareTo(new AxState(new AxStateParamsBuilder().key(stateKey).trigger(triggerKey) - .stateOutputs(soMap).contextAlbumReferenceSet(ctxtSet) - .taskSelectionLogic(new AxTaskSelectionLogic()).stateFinalizerLogicMap(sflMap) - .defaultTask(defTaskKey).taskReferenceMap(trMap)))); - assertNotEquals(0, state.compareTo(new AxState(new AxStateParamsBuilder().key(stateKey).trigger(triggerKey) - .stateOutputs(soMap).contextAlbumReferenceSet(ctxtSet).taskSelectionLogic(tsl) - .stateFinalizerLogicMap(sflEmptyMap).defaultTask(defTaskKey).taskReferenceMap(trMap)))); - assertNotEquals(0, state.compareTo(new AxState(new AxStateParamsBuilder().key(stateKey).trigger(triggerKey) - .stateOutputs(soMap).contextAlbumReferenceSet(ctxtSet).taskSelectionLogic(tsl) - .stateFinalizerLogicMap(sflMap).defaultTask(new AxArtifactKey()).taskReferenceMap(trMap)))); - assertNotEquals(0, state.compareTo(new AxState(new AxStateParamsBuilder().key(stateKey).trigger(triggerKey) - .stateOutputs(soMap).contextAlbumReferenceSet(ctxtSet).taskSelectionLogic(tsl) - .stateFinalizerLogicMap(sflMap).defaultTask(defTaskKey).taskReferenceMap(trEmptyMap)))); - assertEquals(0, state.compareTo(new AxState(new AxStateParamsBuilder().key(stateKey).trigger(triggerKey) - .stateOutputs(soMap).contextAlbumReferenceSet(ctxtSet).taskSelectionLogic(tsl) - .stateFinalizerLogicMap(sflMap).defaultTask(defTaskKey).taskReferenceMap(trMap)))); - - assertNotNull(state.getKeys()); - } -} diff --git a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/TestStateOutput.java b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/TestStateOutput.java deleted file mode 100644 index 90b486053..000000000 --- a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/TestStateOutput.java +++ /dev/null @@ -1,139 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-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.apex.model.policymodel.concepts; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - -import org.junit.Test; -import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; -import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey; -import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult; -import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult; -import org.onap.policy.apex.model.policymodel.concepts.AxStateOutput; - -/** - * Test state outputs. - * - * @author Liam Fallon (liam.fallon@ericsson.com) - */ -public class TestStateOutput { - - @Test - public void testStateOutput() { - assertNotNull(new AxStateOutput()); - assertNotNull(new AxStateOutput(new AxReferenceKey())); - assertNotNull(new AxStateOutput(new AxReferenceKey(), new AxReferenceKey(), new AxArtifactKey())); - assertNotNull(new AxStateOutput(new AxReferenceKey(), new AxArtifactKey(), new AxReferenceKey())); - - final AxStateOutput so = new AxStateOutput(); - - final AxReferenceKey soKey = new AxReferenceKey("SOStateParent", "0.0.1", "SOState", "SOName"); - final AxReferenceKey nsKey = new AxReferenceKey("SOStateParent", "0.0.1", "NotUsed", "NextStateName"); - final AxArtifactKey eKey = new AxArtifactKey("EventName", "0.0.1"); - - try { - so.setKey(null); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("key may not be null", e.getMessage()); - } - - so.setKey(soKey); - assertEquals("SOStateParent:0.0.1:SOState:SOName", so.getKey().getId()); - assertEquals("SOStateParent:0.0.1:SOState:SOName", so.getKeys().get(0).getId()); - - try { - so.setNextState(null); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("nextState may not be null", e.getMessage()); - } - - so.setNextState(nsKey); - assertEquals(nsKey, so.getNextState()); - - try { - so.setOutgoingEvent(null); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("outgoingEvent may not be null", e.getMessage()); - } - - so.setOutgoingEvent(eKey); - assertEquals(eKey, so.getOutgingEvent()); - - AxValidationResult result = new AxValidationResult(); - result = so.validate(result); - assertEquals(AxValidationResult.ValidationResult.VALID, result.getValidationResult()); - - so.setKey(AxReferenceKey.getNullKey()); - result = new AxValidationResult(); - result = so.validate(result); - assertEquals(ValidationResult.INVALID, result.getValidationResult()); - - so.setKey(soKey); - result = new AxValidationResult(); - result = so.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - so.setOutgoingEvent(AxArtifactKey.getNullKey()); - result = new AxValidationResult(); - result = so.validate(result); - assertEquals(ValidationResult.INVALID, result.getValidationResult()); - - so.setOutgoingEvent(eKey); - result = new AxValidationResult(); - result = so.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - so.clean(); - - final AxStateOutput clonedPar = new AxStateOutput(so); - assertEquals("AxStateOutput:(stateKey=AxReferenceKey:(parentKeyN", clonedPar.toString().substring(0, 50)); - - assertFalse(so.hashCode() == 0); - - assertTrue(so.equals(so)); - assertTrue(so.equals(clonedPar)); - assertFalse(so.equals(null)); - assertFalse(so.equals("Hello")); - assertFalse(so.equals(new AxStateOutput(AxReferenceKey.getNullKey(), eKey, nsKey))); - assertFalse(so.equals(new AxStateOutput(soKey, new AxArtifactKey(), nsKey))); - assertFalse(so.equals(new AxStateOutput(soKey, eKey, new AxReferenceKey()))); - assertTrue(so.equals(new AxStateOutput(soKey, eKey, nsKey))); - - assertEquals(0, so.compareTo(so)); - assertEquals(0, so.compareTo(clonedPar)); - assertNotEquals(0, so.compareTo(new AxArtifactKey())); - assertNotEquals(0, so.compareTo(null)); - assertNotEquals(0, so.compareTo(new AxStateOutput(AxReferenceKey.getNullKey(), eKey, nsKey))); - assertNotEquals(0, so.compareTo(new AxStateOutput(soKey, new AxArtifactKey(), nsKey))); - assertNotEquals(0, so.compareTo(new AxStateOutput(soKey, eKey, new AxReferenceKey()))); - assertEquals(0, so.compareTo(new AxStateOutput(soKey, eKey, nsKey))); - - assertNotNull(so.getKeys()); - } -} diff --git a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/TestStateTaskReference.java b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/TestStateTaskReference.java deleted file mode 100644 index 90c7001b8..000000000 --- a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/TestStateTaskReference.java +++ /dev/null @@ -1,162 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-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.apex.model.policymodel.concepts; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - -import org.junit.Test; -import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; -import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey; -import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult; -import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult; -import org.onap.policy.apex.model.policymodel.concepts.AxStateTaskOutputType; -import org.onap.policy.apex.model.policymodel.concepts.AxStateTaskReference; - -/** - * Test state task references. - * - * @author Liam Fallon (liam.fallon@ericsson.com) - */ -public class TestStateTaskReference { - - @Test - public void testStateTaskReference() { - assertNotNull(new AxStateTaskReference()); - assertNotNull(new AxStateTaskReference(new AxReferenceKey())); - assertNotNull(new AxStateTaskReference(new AxReferenceKey(), AxStateTaskOutputType.UNDEFINED, - new AxReferenceKey())); - assertNotNull(new AxStateTaskReference(new AxReferenceKey(), new AxArtifactKey(), - AxStateTaskOutputType.UNDEFINED, new AxReferenceKey())); - - AxStateTaskReference stRef = new AxStateTaskReference(); - - AxReferenceKey stRefKey = new AxReferenceKey("StateParent", "0.0.1", "SOState", "SOName"); - - try { - stRef.setKey(null); - fail("test should throw an exception here"); - } catch (Exception e) { - assertEquals("key may not be null", e.getMessage()); - } - - stRef.setKey(stRefKey); - assertEquals("StateParent:0.0.1:SOState:SOName", stRef.getKey().getId()); - assertEquals("StateParent:0.0.1:SOState:SOName", stRef.getKeys().get(0).getId()); - - try { - stRef.setStateTaskOutputType(null); - fail("test should throw an exception here"); - } catch (Exception e) { - assertEquals("outputType may not be null", e.getMessage()); - } - - stRef.setStateTaskOutputType(AxStateTaskOutputType.UNDEFINED); - assertEquals(AxStateTaskOutputType.UNDEFINED, stRef.getStateTaskOutputType()); - stRef.setStateTaskOutputType(AxStateTaskOutputType.DIRECT); - assertEquals(AxStateTaskOutputType.DIRECT, stRef.getStateTaskOutputType()); - stRef.setStateTaskOutputType(AxStateTaskOutputType.LOGIC); - assertEquals(AxStateTaskOutputType.LOGIC, stRef.getStateTaskOutputType()); - - try { - stRef.setOutput(null); - fail("test should throw an exception here"); - } catch (Exception e) { - assertEquals("output may not be null", e.getMessage()); - } - - AxReferenceKey soKey = new AxReferenceKey("StateParent", "0.0.1", "SOState", "STRef0"); - stRef.setOutput(soKey); - assertEquals(soKey, stRef.getOutput()); - - AxValidationResult result = new AxValidationResult(); - result = stRef.validate(result); - assertEquals(AxValidationResult.ValidationResult.VALID, result.getValidationResult()); - - stRef.setKey(AxReferenceKey.getNullKey()); - result = new AxValidationResult(); - result = stRef.validate(result); - assertEquals(ValidationResult.INVALID, result.getValidationResult()); - - stRef.setKey(stRefKey); - result = new AxValidationResult(); - result = stRef.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - stRef.setStateTaskOutputType(AxStateTaskOutputType.UNDEFINED); - result = new AxValidationResult(); - result = stRef.validate(result); - assertEquals(ValidationResult.INVALID, result.getValidationResult()); - - stRef.setStateTaskOutputType(AxStateTaskOutputType.LOGIC); - result = new AxValidationResult(); - result = stRef.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - stRef.setOutput(AxReferenceKey.getNullKey()); - result = new AxValidationResult(); - result = stRef.validate(result); - assertEquals(ValidationResult.INVALID, result.getValidationResult()); - - stRef.setOutput(soKey); - result = new AxValidationResult(); - result = stRef.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - stRef.clean(); - - AxStateTaskReference clonedStRef = new AxStateTaskReference(stRef); - assertEquals("AxStateTaskReference:(stateKey=AxReferenceKey:(par", clonedStRef.toString().substring(0, 50)); - - assertFalse(stRef.hashCode() == 0); - - assertTrue(stRef.equals(stRef)); - assertTrue(stRef.equals(clonedStRef)); - assertFalse(stRef.equals(null)); - assertFalse(stRef.equals("Hello")); - assertFalse(stRef.equals( - new AxStateTaskReference(AxReferenceKey.getNullKey(), AxStateTaskOutputType.LOGIC, soKey))); - assertFalse(stRef.equals(new AxStateTaskReference(stRefKey, AxStateTaskOutputType.DIRECT, soKey))); - assertFalse(stRef - .equals(new AxStateTaskReference(stRefKey, AxStateTaskOutputType.LOGIC, new AxReferenceKey()))); - assertTrue(stRef.equals(new AxStateTaskReference(stRefKey, AxStateTaskOutputType.LOGIC, soKey))); - - assertNotNull(new AxStateTaskReference(new AxReferenceKey(), new AxArtifactKey(), - AxStateTaskOutputType.UNDEFINED, new AxReferenceKey())); - - assertEquals(0, stRef.compareTo(stRef)); - assertEquals(0, stRef.compareTo(clonedStRef)); - assertNotEquals(0, stRef.compareTo(new AxArtifactKey())); - assertNotEquals(0, stRef.compareTo(null)); - assertNotEquals(0, stRef.compareTo( - new AxStateTaskReference(AxReferenceKey.getNullKey(), AxStateTaskOutputType.LOGIC, soKey))); - assertNotEquals(0, stRef.compareTo(new AxStateTaskReference(stRefKey, AxStateTaskOutputType.DIRECT, soKey))); - assertNotEquals(0, stRef.compareTo( - new AxStateTaskReference(stRefKey, AxStateTaskOutputType.LOGIC, new AxReferenceKey()))); - assertEquals(0, stRef.compareTo(new AxStateTaskReference(stRefKey, AxStateTaskOutputType.LOGIC, soKey))); - - assertNotNull(stRef.getKeys()); - } -} diff --git a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/TestTaskParameter.java b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/TestTaskParameter.java deleted file mode 100644 index 4eb2c4232..000000000 --- a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/TestTaskParameter.java +++ /dev/null @@ -1,117 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-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.apex.model.policymodel.concepts; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - -import org.junit.Test; -import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; -import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey; -import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult; -import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult; -import org.onap.policy.apex.model.policymodel.concepts.AxTaskParameter; - -/** - * Test task parameters. - * - * @author Liam Fallon (liam.fallon@ericsson.com) - */ -public class TestTaskParameter { - - @Test - public void testTaskParameter() { - assertNotNull(new AxTaskParameter()); - assertNotNull(new AxTaskParameter(new AxReferenceKey())); - assertNotNull(new AxTaskParameter(new AxReferenceKey(), "DefaultValue")); - - final AxTaskParameter par = new AxTaskParameter(); - - final AxReferenceKey parKey = new AxReferenceKey("ParParentName", "0.0.1", "PLN", "LN"); - par.setKey(parKey); - assertEquals("ParParentName:0.0.1:PLN:LN", par.getKey().getId()); - assertEquals("ParParentName:0.0.1:PLN:LN", par.getKeys().get(0).getId()); - - par.setDefaultValue("DefaultValue"); - assertEquals("DefaultValue", par.getTaskParameterValue()); - - AxValidationResult result = new AxValidationResult(); - result = par.validate(result); - assertEquals(AxValidationResult.ValidationResult.VALID, result.getValidationResult()); - - par.setKey(AxReferenceKey.getNullKey()); - result = new AxValidationResult(); - result = par.validate(result); - assertEquals(ValidationResult.INVALID, result.getValidationResult()); - - par.setKey(parKey); - result = new AxValidationResult(); - result = par.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - try { - par.setDefaultValue(null); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("defaultValue may not be null", e.getMessage()); - } - - par.setDefaultValue(""); - result = new AxValidationResult(); - result = par.validate(result); - assertEquals(ValidationResult.WARNING, result.getValidationResult()); - - par.setDefaultValue("DefaultValue"); - result = new AxValidationResult(); - result = par.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - par.clean(); - - final AxTaskParameter clonedPar = new AxTaskParameter(par); - assertEquals("AxTaskParameter:(key=AxReferenceKey:(parentKeyName=ParParentName,parentKeyVersion=0.0.1," - + "parentLocalName=PLN,localName=LN),defaultValue=DefaultValue)", clonedPar.toString()); - - assertFalse(par.hashCode() == 0); - - assertTrue(par.equals(par)); - assertTrue(par.equals(clonedPar)); - assertFalse(par.equals(null)); - assertFalse(par.equals("Hello")); - assertFalse(par.equals(new AxTaskParameter(AxReferenceKey.getNullKey(), "DefaultValue"))); - assertFalse(par.equals(new AxTaskParameter(parKey, "OtherDefaultValue"))); - assertTrue(par.equals(new AxTaskParameter(parKey, "DefaultValue"))); - - assertEquals(0, par.compareTo(par)); - assertEquals(0, par.compareTo(clonedPar)); - assertNotEquals(0, par.compareTo(new AxArtifactKey())); - assertNotEquals(0, par.compareTo(null)); - assertNotEquals(0, par.compareTo(new AxTaskParameter(AxReferenceKey.getNullKey(), "DefaultValue"))); - assertNotEquals(0, par.compareTo(new AxTaskParameter(parKey, "OtherDefaultValue"))); - assertEquals(0, par.compareTo(new AxTaskParameter(parKey, "DefaultValue"))); - - assertNotNull(par.getKeys()); - } -} diff --git a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/TestTasks.java b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/TestTasks.java deleted file mode 100644 index 4f6292f04..000000000 --- a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/TestTasks.java +++ /dev/null @@ -1,333 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-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.apex.model.policymodel.concepts; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -import java.util.TreeMap; -import java.util.TreeSet; - -import org.junit.Test; -import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; -import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey; -import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult; -import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult; -import org.onap.policy.apex.model.eventmodel.concepts.AxField; -import org.onap.policy.apex.model.eventmodel.concepts.AxInputField; -import org.onap.policy.apex.model.eventmodel.concepts.AxOutputField; -import org.onap.policy.apex.model.policymodel.concepts.AxTask; -import org.onap.policy.apex.model.policymodel.concepts.AxTaskLogic; -import org.onap.policy.apex.model.policymodel.concepts.AxTaskParameter; -import org.onap.policy.apex.model.policymodel.concepts.AxTasks; - -/** - * Test policy tasks. - * - * @author Liam Fallon (liam.fallon@ericsson.com) - */ -public class TestTasks { - - @Test - public void testTasks() { - final TreeMap ifEmptyMap = new TreeMap<>(); - final TreeMap ofEmptyMap = new TreeMap<>(); - final TreeMap tpEmptyMap = new TreeMap<>(); - final TreeSet ctxtEmptySet = new TreeSet<>(); - - final TreeMap ifMap = new TreeMap<>(); - final TreeMap ofMap = new TreeMap<>(); - final TreeMap tpMap = new TreeMap<>(); - final TreeSet ctxtSet = new TreeSet<>(); - - assertNotNull(new AxTask()); - assertNotNull(new AxTask(new AxArtifactKey())); - assertNotNull(new AxTask(new AxArtifactKey(), ifMap, ofMap, tpMap, ctxtSet, new AxTaskLogic())); - - final AxTask task = new AxTask(); - - final AxArtifactKey taskKey = new AxArtifactKey("TaskName", "0.0.1"); - task.setKey(taskKey); - assertEquals("TaskName:0.0.1", task.getKey().getId()); - assertEquals("TaskName:0.0.1", task.getKeys().get(0).getId()); - - final AxArtifactKey f0SchemaKey = new AxArtifactKey("FS0", "0.0.1"); - - final AxInputField if0 = new AxInputField(new AxReferenceKey(taskKey, "IF0"), f0SchemaKey, false); - final AxInputField if1 = new AxInputField(new AxReferenceKey(taskKey, "IF1"), f0SchemaKey, false); - final AxOutputField of0 = new AxOutputField(new AxReferenceKey(taskKey, "OF0"), f0SchemaKey, false); - final AxOutputField of1 = new AxOutputField(new AxReferenceKey(taskKey, "OF1"), f0SchemaKey, false); - final AxTaskParameter tp0 = new AxTaskParameter(new AxReferenceKey(taskKey, "TP0"), "DefaultValue"); - final AxArtifactKey cr0 = new AxArtifactKey("ContextReference", "0.0.1"); - final AxTaskLogic tl = new AxTaskLogic(taskKey, "LogicName", "LogicFlavour", "Logic"); - - ifMap.put(if0.getKey().getLocalName(), if0); - ofMap.put(of0.getKey().getLocalName(), of0); - tpMap.put(tp0.getKey().getLocalName(), tp0); - ctxtSet.add(cr0); - - task.setInputFields(ifMap); - assertEquals(ifMap, task.getInputFields()); - assertTrue(task.getInputFieldSet().contains(if0)); - assertTrue(task.getRawInputFields().keySet().contains(if0.getKey().getLocalName())); - - task.setOutputFields(ofMap); - assertEquals(ofMap, task.getOutputFields()); - assertTrue(task.getOutputFieldSet().contains(of0)); - assertTrue(task.getRawOutputFields().keySet().contains(of0.getKey().getLocalName())); - - final TreeMap ifDupMap = new TreeMap<>(); - final TreeMap ofDupMap = new TreeMap<>(); - ifDupMap.put(if1.getKey().getLocalName(), if1); - ofDupMap.put(of1.getKey().getLocalName(), of1); - task.duplicateInputFields(ifDupMap); - task.duplicateOutputFields(ofDupMap); - assertTrue(ifMap.containsKey("IF1")); - assertTrue(ofMap.containsKey("OF1")); - - task.setTaskParameters(tpMap); - assertEquals(tpMap, task.getTaskParameters()); - - task.setContextAlbumReferences(ctxtSet); - assertEquals(ctxtSet, task.getContextAlbumReferences()); - - task.setTaskLogic(tl); - assertEquals(tl, task.getTaskLogic()); - - task.setKey(taskKey); - assertEquals("TaskName:0.0.1", task.getKey().getId()); - assertEquals("TaskName:0.0.1", task.getKeys().get(0).getId()); - - task.afterUnmarshal(null, null); - assertEquals(1, task.getTaskParameters().size()); - - AxValidationResult result = new AxValidationResult(); - result = task.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - task.setKey(AxArtifactKey.getNullKey()); - result = new AxValidationResult(); - result = task.validate(result); - assertEquals(ValidationResult.INVALID, result.getValidationResult()); - - task.setKey(taskKey); - result = new AxValidationResult(); - result = task.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - task.setInputFields(ifEmptyMap); - result = new AxValidationResult(); - result = task.validate(result); - assertEquals(ValidationResult.INVALID, result.getValidationResult()); - - task.setInputFields(ifMap); - result = new AxValidationResult(); - result = task.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - ifMap.put("NullField", null); - result = new AxValidationResult(); - result = task.validate(result); - assertEquals(ValidationResult.INVALID, result.getValidationResult()); - - ifMap.remove("NullField"); - result = new AxValidationResult(); - result = task.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - task.setOutputFields(ofEmptyMap); - result = new AxValidationResult(); - result = task.validate(result); - assertEquals(ValidationResult.INVALID, result.getValidationResult()); - - task.setOutputFields(ofMap); - result = new AxValidationResult(); - result = task.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - ofMap.put("NullField", null); - result = new AxValidationResult(); - result = task.validate(result); - assertEquals(ValidationResult.INVALID, result.getValidationResult()); - - ofMap.remove("NullField"); - result = new AxValidationResult(); - result = task.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - // Empty task parameter map is OK - task.setTaskParameters(tpEmptyMap); - result = new AxValidationResult(); - result = task.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - task.setTaskParameters(tpMap); - result = new AxValidationResult(); - result = task.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - tpMap.put("NullField", null); - result = new AxValidationResult(); - result = task.validate(result); - assertEquals(ValidationResult.INVALID, result.getValidationResult()); - - tpMap.remove("NullField"); - result = new AxValidationResult(); - result = task.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - // Empty context reference set is OK - task.setContextAlbumReferences(ctxtEmptySet); - result = new AxValidationResult(); - result = task.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - task.setContextAlbumReferences(ctxtSet); - result = new AxValidationResult(); - result = task.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - ctxtSet.add(AxArtifactKey.getNullKey()); - result = new AxValidationResult(); - result = task.validate(result); - assertEquals(ValidationResult.INVALID, result.getValidationResult()); - - ctxtSet.remove(AxArtifactKey.getNullKey()); - result = new AxValidationResult(); - result = task.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - task.clean(); - - final AxTask clonedTask = new AxTask(task); - assertEquals("AxTask:(key=AxArtifactKey:(name=TaskName", clonedTask.toString().substring(0, 40)); - - assertFalse(task.hashCode() == 0); - - assertTrue(task.equals(task)); - assertTrue(task.equals(clonedTask)); - assertFalse(task.equals(null)); - assertFalse(task.equals("Hello")); - assertFalse(task.equals(new AxTask(new AxArtifactKey(), ifMap, ofMap, tpMap, ctxtSet, tl))); - assertFalse(task.equals(new AxTask(taskKey, ifEmptyMap, ofMap, tpMap, ctxtSet, tl))); - assertFalse(task.equals(new AxTask(taskKey, ifMap, ofEmptyMap, tpMap, ctxtSet, tl))); - assertFalse(task.equals(new AxTask(taskKey, ifMap, ofMap, tpEmptyMap, ctxtSet, tl))); - assertFalse(task.equals(new AxTask(taskKey, ifMap, ofMap, tpMap, ctxtEmptySet, tl))); - assertFalse(task.equals(new AxTask(taskKey, ifMap, ofMap, tpMap, ctxtSet, new AxTaskLogic()))); - assertTrue(task.equals(new AxTask(taskKey, ifMap, ofMap, tpMap, ctxtSet, tl))); - - assertEquals(0, task.compareTo(task)); - assertEquals(0, task.compareTo(clonedTask)); - assertNotEquals(0, task.compareTo(new AxArtifactKey())); - assertNotEquals(0, task.compareTo(null)); - assertNotEquals(0, task.compareTo(new AxTask(new AxArtifactKey(), ifMap, ofMap, tpMap, ctxtSet, tl))); - assertNotEquals(0, task.compareTo(new AxTask(taskKey, ifEmptyMap, ofMap, tpMap, ctxtSet, tl))); - assertNotEquals(0, task.compareTo(new AxTask(taskKey, ifMap, ofEmptyMap, tpMap, ctxtSet, tl))); - assertNotEquals(0, task.compareTo(new AxTask(taskKey, ifMap, ofMap, tpEmptyMap, ctxtSet, tl))); - assertNotEquals(0, task.compareTo(new AxTask(taskKey, ifMap, ofMap, tpMap, ctxtEmptySet, tl))); - assertNotEquals(0, task.compareTo(new AxTask(taskKey, ifMap, ofMap, tpMap, ctxtSet, new AxTaskLogic()))); - assertEquals(0, task.compareTo(new AxTask(taskKey, ifMap, ofMap, tpMap, ctxtSet, tl))); - - assertNotNull(task.getKeys()); - - final AxTasks tasks = new AxTasks(); - result = new AxValidationResult(); - result = tasks.validate(result); - assertEquals(ValidationResult.INVALID, result.getValidationResult()); - - // Invalid, no tasks in task map - tasks.setKey(new AxArtifactKey("TasksKey", "0.0.1")); - assertEquals("TasksKey:0.0.1", tasks.getKey().getId()); - - result = new AxValidationResult(); - result = tasks.validate(result); - assertEquals(ValidationResult.INVALID, result.getValidationResult()); - - tasks.getTaskMap().put(taskKey, task); - result = new AxValidationResult(); - result = tasks.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - tasks.getTaskMap().put(AxArtifactKey.getNullKey(), null); - result = new AxValidationResult(); - result = tasks.validate(result); - assertEquals(ValidationResult.INVALID, result.getValidationResult()); - - tasks.getTaskMap().remove(AxArtifactKey.getNullKey()); - result = new AxValidationResult(); - result = tasks.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - tasks.getTaskMap().put(new AxArtifactKey("NullValueKey", "0.0.1"), null); - result = new AxValidationResult(); - result = tasks.validate(result); - assertEquals(ValidationResult.INVALID, result.getValidationResult()); - - tasks.getTaskMap().remove(new AxArtifactKey("NullValueKey", "0.0.1")); - result = new AxValidationResult(); - result = tasks.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - tasks.getTaskMap().put(new AxArtifactKey("BadTaskKey", "0.0.1"), task); - result = new AxValidationResult(); - result = tasks.validate(result); - assertEquals(ValidationResult.INVALID, result.getValidationResult()); - - tasks.getTaskMap().remove(new AxArtifactKey("BadTaskKey", "0.0.1")); - result = new AxValidationResult(); - result = tasks.validate(result); - assertEquals(ValidationResult.VALID, result.getValidationResult()); - - tasks.clean(); - tasks.afterUnmarshal(null, null); - - final AxTasks clonedTasks = new AxTasks(tasks); - assertEquals("AxTasks:(key=AxArtifactKey:(name=TasksKey,version=0.0.1),tas", - clonedTasks.toString().substring(0, 60)); - - assertFalse(tasks.hashCode() == 0); - - assertTrue(tasks.equals(tasks)); - assertTrue(tasks.equals(clonedTasks)); - assertFalse(tasks.equals(null)); - assertFalse(tasks.equals("Hello")); - assertFalse(tasks.equals(new AxTasks(new AxArtifactKey()))); - - assertEquals(0, tasks.compareTo(tasks)); - assertEquals(0, tasks.compareTo(clonedTasks)); - assertNotEquals(0, tasks.compareTo(null)); - assertNotEquals(0, tasks.compareTo(new AxArtifactKey())); - assertNotEquals(0, tasks.compareTo(new AxTasks(new AxArtifactKey()))); - - clonedTasks.get(taskKey).getTaskLogic().setLogic("SomeChangedLogic"); - assertNotEquals(0, tasks.compareTo(clonedTasks)); - - assertEquals(tasks.getKey(), tasks.getKeys().get(0)); - - assertEquals("TaskName", tasks.get("TaskName").getKey().getName()); - assertEquals("TaskName", tasks.get("TaskName", "0.0.1").getKey().getName()); - assertEquals(1, tasks.getAll("TaskName", "0.0.1").size()); - assertEquals(0, tasks.getAll("NonExistantTaskName").size()); - } -} diff --git a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/ApexPolicyModelCreatorTest.java b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/ApexPolicyModelCreatorTest.java new file mode 100644 index 000000000..604d8c3bc --- /dev/null +++ b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/ApexPolicyModelCreatorTest.java @@ -0,0 +1,455 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-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.apex.model.policymodel.handling; + +import java.util.UUID; + +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.basicmodel.concepts.AxKeyInfo; +import org.onap.policy.apex.model.basicmodel.concepts.AxKeyInformation; +import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey; +import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult; +import org.onap.policy.apex.model.basicmodel.test.TestApexModelCreator; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbum; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbums; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas; +import org.onap.policy.apex.model.eventmodel.concepts.AxEvent; +import org.onap.policy.apex.model.eventmodel.concepts.AxEvents; +import org.onap.policy.apex.model.eventmodel.concepts.AxField; +import org.onap.policy.apex.model.eventmodel.concepts.AxInputField; +import org.onap.policy.apex.model.eventmodel.concepts.AxOutputField; +import org.onap.policy.apex.model.policymodel.concepts.AxPolicies; +import org.onap.policy.apex.model.policymodel.concepts.AxPolicy; +import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; +import org.onap.policy.apex.model.policymodel.concepts.AxState; +import org.onap.policy.apex.model.policymodel.concepts.AxStateFinalizerLogic; +import org.onap.policy.apex.model.policymodel.concepts.AxStateOutput; +import org.onap.policy.apex.model.policymodel.concepts.AxStateTaskOutputType; +import org.onap.policy.apex.model.policymodel.concepts.AxStateTaskReference; +import org.onap.policy.apex.model.policymodel.concepts.AxTask; +import org.onap.policy.apex.model.policymodel.concepts.AxTaskLogic; +import org.onap.policy.apex.model.policymodel.concepts.AxTaskParameter; +import org.onap.policy.apex.model.policymodel.concepts.AxTaskSelectionLogic; +import org.onap.policy.apex.model.policymodel.concepts.AxTasks; + +/** + * Model creator for model tests. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class ApexPolicyModelCreatorTest implements TestApexModelCreator { + + @Override + public AxPolicyModel getModel() { + final AxContextSchema schema0 = new AxContextSchema(new AxArtifactKey("eventContextItem0", "0.0.1"), "Java", + "java.lang.String"); + final AxContextSchema schema1 = new AxContextSchema(new AxArtifactKey("eventContextItem1", "0.0.1"), "Java", + "java.lang.Long"); + final AxContextSchema schema2 = new AxContextSchema(new AxArtifactKey("StringType", "0.0.1"), "Java", + "org.onap.policy.apex.model.policymodel.concepts.TestContextItem000"); + final AxContextSchema schema3 = new AxContextSchema(new AxArtifactKey("MapType", "0.0.1"), "Java", + "org.onap.policy.apex.model.policymodel.concepts.TestContextItem00A"); + + final AxContextSchemas schemas = new AxContextSchemas(new AxArtifactKey("ContextSchemas", "0.0.1")); + schemas.getSchemasMap().put(schema0.getKey(), schema0); + schemas.getSchemasMap().put(schema1.getKey(), schema1); + schemas.getSchemasMap().put(schema2.getKey(), schema2); + schemas.getSchemasMap().put(schema3.getKey(), schema3); + + final AxContextAlbum album0 = new AxContextAlbum(new AxArtifactKey("contextAlbum0", "0.0.1"), "APPLICATION", + true, schema3.getKey()); + final AxContextAlbum album1 = new AxContextAlbum(new AxArtifactKey("contextAlbum1", "0.0.1"), "GLOBAL", false, + schema2.getKey()); + + final AxContextAlbums albums = new AxContextAlbums(new AxArtifactKey("context", "0.0.1")); + albums.getAlbumsMap().put(album0.getKey(), album0); + albums.getAlbumsMap().put(album1.getKey(), album1); + + final AxEvent inEvent = new AxEvent(new AxArtifactKey("inEvent", "0.0.1"), + "org.onap.policy.apex.model.policymodel.events", "Source", "Target"); + inEvent.getParameterMap().put("IEPAR0", + new AxField(new AxReferenceKey(inEvent.getKey(), "IEPAR0"), schema0.getKey())); + inEvent.getParameterMap().put("IEPAR1", + new AxField(new AxReferenceKey(inEvent.getKey(), "IEPAR1"), schema1.getKey())); + + final AxEvent outEvent0 = new AxEvent(new AxArtifactKey("outEvent0", "0.0.1"), + "org.onap.policy.apex.model.policymodel.events", "Source", "Target"); + outEvent0.getParameterMap().put("OE0PAR0", + new AxField(new AxReferenceKey(outEvent0.getKey(), "OE0PAR0"), schema0.getKey())); + outEvent0.getParameterMap().put("OE0PAR1", + new AxField(new AxReferenceKey(outEvent0.getKey(), "OE0PAR1"), schema1.getKey())); + outEvent0.getParameterMap().put("OE1PAR0", + new AxField(new AxReferenceKey(outEvent0.getKey(), "OE1PAR0"), schema0.getKey())); + outEvent0.getParameterMap().put("OE1PAR1", + new AxField(new AxReferenceKey(outEvent0.getKey(), "OE1PAR1"), schema1.getKey())); + + final AxEvent outEvent1 = new AxEvent(new AxArtifactKey("outEvent1", "0.0.1"), + "org.onap.policy.apex.model.policymodel.events", "Source", "Target"); + outEvent1.getParameterMap().put("OE1PAR0", + new AxField(new AxReferenceKey(outEvent1.getKey(), "OE1PAR0"), schema0.getKey())); + outEvent1.getParameterMap().put("OE1PAR1", + new AxField(new AxReferenceKey(outEvent1.getKey(), "OE1PAR1"), schema1.getKey())); + + final AxEvents events = new AxEvents(new AxArtifactKey("events", "0.0.1")); + events.getEventMap().put(inEvent.getKey(), inEvent); + events.getEventMap().put(outEvent0.getKey(), outEvent0); + events.getEventMap().put(outEvent1.getKey(), outEvent1); + + final AxTask task = new AxTask(new AxArtifactKey("task", "0.0.1")); + + for (final AxField field : inEvent.getFields()) { + final AxReferenceKey fieldkey = new AxReferenceKey(task.getKey().getName(), task.getKey().getVersion(), + "inputFields", field.getKey().getLocalName()); + final AxInputField inputField = new AxInputField(fieldkey, field.getSchema()); + task.getInputFields().put(inputField.getKey().getLocalName(), inputField); + } + + for (final AxField field : outEvent0.getFields()) { + final AxReferenceKey fieldkey = new AxReferenceKey(task.getKey().getName(), task.getKey().getVersion(), + "outputFields", field.getKey().getLocalName()); + final AxOutputField outputField = new AxOutputField(fieldkey, field.getSchema()); + task.getOutputFields().put(outputField.getKey().getLocalName(), outputField); + } + + for (final AxField field : outEvent1.getFields()) { + final AxReferenceKey fieldkey = new AxReferenceKey(task.getKey().getName(), task.getKey().getVersion(), + "outputFields", field.getKey().getLocalName()); + final AxOutputField outputField = new AxOutputField(fieldkey, field.getSchema()); + task.getOutputFields().put(outputField.getKey().getLocalName(), outputField); + } + + final AxTaskParameter taskPar0 = new AxTaskParameter(new AxReferenceKey(task.getKey(), "taskParameter0"), + "Task parameter 0 value"); + final AxTaskParameter taskPar1 = new AxTaskParameter(new AxReferenceKey(task.getKey(), "taskParameter1"), + "Task parameter 1 value"); + + task.getTaskParameters().put(taskPar0.getKey().getLocalName(), taskPar0); + task.getTaskParameters().put(taskPar1.getKey().getLocalName(), taskPar1); + task.getContextAlbumReferences().add(album0.getKey()); + task.getContextAlbumReferences().add(album1.getKey()); + + final AxTaskLogic taskLogic = new AxTaskLogic(new AxReferenceKey(task.getKey(), "taskLogic"), "MVEL", + "Some task logic"); + task.setTaskLogic(taskLogic); + + final AxTasks tasks = new AxTasks(new AxArtifactKey("tasks", "0.0.1")); + tasks.getTaskMap().put(task.getKey(), task); + + final AxPolicy policy = new AxPolicy(new AxArtifactKey("policy", "0.0.1")); + policy.setTemplate("FREEFORM"); + + final AxState state = new AxState(new AxReferenceKey(policy.getKey(), "state")); + final AxTaskSelectionLogic taskSelectionLogic = new AxTaskSelectionLogic( + new AxReferenceKey(state.getKey(), "taskSelectionLogic"), "MVEL", "Some TS logic "); + + state.setTrigger(inEvent.getKey()); + state.getContextAlbumReferences().add(album0.getKey()); + state.getContextAlbumReferences().add(album1.getKey()); + state.setTaskSelectionLogic(taskSelectionLogic); + state.setDefaultTask(task.getKey()); + + final AxStateOutput stateOutput0 = new AxStateOutput(new AxReferenceKey(state.getKey(), "stateOutput0"), + outEvent0.getKey(), AxReferenceKey.getNullKey()); + state.getStateOutputs().put(stateOutput0.getKey().getLocalName(), stateOutput0); + + final AxStateTaskReference stateTaskReference = new AxStateTaskReference( + new AxReferenceKey(state.getKey(), task.getKey().getName()), AxStateTaskOutputType.DIRECT, + stateOutput0.getKey()); + + state.getTaskReferences().put(task.getKey(), stateTaskReference); + + policy.getStateMap().put(state.getKey().getLocalName(), state); + policy.setFirstState(state.getKey().getLocalName()); + + final AxPolicies policies = new AxPolicies(new AxArtifactKey("policies", "0.0.1")); + policies.getPolicyMap().put(policy.getKey(), policy); + + final AxKeyInformation keyInformation = new AxKeyInformation(new AxArtifactKey("KeyInfoMapKey", "0.0.1")); + final AxPolicyModel policyModel = new AxPolicyModel(new AxArtifactKey("PolicyModel", "0.0.1")); + policyModel.setKeyInformation(keyInformation); + policyModel.setSchemas(schemas); + policyModel.setAlbums(albums); + policyModel.setEvents(events); + policyModel.setTasks(tasks); + policyModel.setPolicies(policies); + policyModel.getKeyInformation().generateKeyInfo(policyModel); + + int uuidIncrementer = 0; + for (final AxKeyInfo keyInfo : policyModel.getKeyInformation().getKeyInfoMap().values()) { + final String uuidString = String.format("ce9168c-e6df-414f-9646-6da464b6e%03d", uuidIncrementer++); + keyInfo.setUuid(UUID.fromString(uuidString)); + } + + final AxValidationResult result = new AxValidationResult(); + policyModel.validate(result); + + return policyModel; + } + + /** + * Gets another policy model. + * + * @return the model + */ + public AxPolicyModel getAnotherModel() { + final AxContextSchema schema0 = new AxContextSchema(new AxArtifactKey("eventContextItemA0", "0.0.1"), "Java", + "java.lang.String"); + final AxContextSchema schema1 = new AxContextSchema(new AxArtifactKey("eventContextItemA1", "0.0.1"), "Java", + "java.lang.Long"); + final AxContextSchema schema2 = new AxContextSchema(new AxArtifactKey("StringTypeA", "0.0.1"), "Java", + "org.onap.policy.apex.model.policymodel.concepts.TestContextItem000"); + final AxContextSchema schema3 = new AxContextSchema(new AxArtifactKey("MapTypeA", "0.0.1"), "Java", + "org.onap.policy.apex.model.policymodel.concepts.TestContextItem00A"); + + final AxContextSchemas schemas = new AxContextSchemas(new AxArtifactKey("ContextSchemasA", "0.0.1")); + schemas.getSchemasMap().put(schema0.getKey(), schema0); + schemas.getSchemasMap().put(schema1.getKey(), schema1); + schemas.getSchemasMap().put(schema2.getKey(), schema2); + schemas.getSchemasMap().put(schema3.getKey(), schema3); + + final AxContextAlbum album0 = new AxContextAlbum(new AxArtifactKey("contextAlbumA0", "0.0.1"), "APPLICATION", + true, schema3.getKey()); + final AxContextAlbum album1 = new AxContextAlbum(new AxArtifactKey("contextAlbumA1", "0.0.1"), "GLOBAL", false, + schema2.getKey()); + + final AxContextAlbums albums = new AxContextAlbums(new AxArtifactKey("contextA", "0.0.1")); + albums.getAlbumsMap().put(album0.getKey(), album0); + albums.getAlbumsMap().put(album1.getKey(), album1); + + final AxEvent inEvent = new AxEvent(new AxArtifactKey("inEventA", "0.0.1"), + "org.onap.policy.apex.model.policymodel.events", "Source", "Target"); + inEvent.getParameterMap().put("IEPARA0", + new AxField(new AxReferenceKey(inEvent.getKey(), "IEPARA0"), schema0.getKey())); + inEvent.getParameterMap().put("IEPARA1", + new AxField(new AxReferenceKey(inEvent.getKey(), "IEPARA1"), schema1.getKey())); + + final AxEvent outEvent0 = new AxEvent(new AxArtifactKey("outEventA0", "0.0.1"), + "org.onap.policy.apex.model.policymodel.events", "Source", "Target"); + outEvent0.getParameterMap().put("OE0PARA0", + new AxField(new AxReferenceKey(outEvent0.getKey(), "OE0PARA0"), schema0.getKey())); + outEvent0.getParameterMap().put("OE0PARA1", + new AxField(new AxReferenceKey(outEvent0.getKey(), "OE0PARA1"), schema1.getKey())); + outEvent0.getParameterMap().put("OE1PARA0", + new AxField(new AxReferenceKey(outEvent0.getKey(), "OE1PARA0"), schema0.getKey())); + outEvent0.getParameterMap().put("OE1PARA1", + new AxField(new AxReferenceKey(outEvent0.getKey(), "OE1PARA1"), schema1.getKey())); + + final AxEvent outEvent1 = new AxEvent(new AxArtifactKey("outEventA1", "0.0.1"), + "org.onap.policy.apex.model.policymodel.events", "Source", "Target"); + outEvent1.getParameterMap().put("OE1PARA0", + new AxField(new AxReferenceKey(outEvent1.getKey(), "OE1PARA0"), schema0.getKey())); + outEvent1.getParameterMap().put("OE1PARA1", + new AxField(new AxReferenceKey(outEvent1.getKey(), "OE1PARA1"), schema1.getKey())); + + final AxEvents events = new AxEvents(new AxArtifactKey("eventsA", "0.0.1")); + events.getEventMap().put(inEvent.getKey(), inEvent); + events.getEventMap().put(outEvent0.getKey(), outEvent0); + events.getEventMap().put(outEvent1.getKey(), outEvent1); + + final AxTask task = new AxTask(new AxArtifactKey("taskA", "0.0.1")); + + for (final AxField field : inEvent.getFields()) { + final AxReferenceKey fieldkey = new AxReferenceKey(task.getKey().getName(), task.getKey().getVersion(), + "inputFieldsA", field.getKey().getLocalName()); + final AxInputField inputField = new AxInputField(fieldkey, field.getSchema()); + task.getInputFields().put(inputField.getKey().getLocalName(), inputField); + } + + for (final AxField field : outEvent0.getFields()) { + final AxReferenceKey fieldkey = new AxReferenceKey(task.getKey().getName(), task.getKey().getVersion(), + "outputFieldsA", field.getKey().getLocalName()); + final AxOutputField outputField = new AxOutputField(fieldkey, field.getSchema()); + task.getOutputFields().put(outputField.getKey().getLocalName(), outputField); + } + + for (final AxField field : outEvent1.getFields()) { + final AxReferenceKey fieldkey = new AxReferenceKey(task.getKey().getName(), task.getKey().getVersion(), + "outputFieldsA", field.getKey().getLocalName()); + final AxOutputField outputField = new AxOutputField(fieldkey, field.getSchema()); + task.getOutputFields().put(outputField.getKey().getLocalName(), outputField); + } + + final AxTaskParameter taskPar0 = new AxTaskParameter(new AxReferenceKey(task.getKey(), "taskParameterA0"), + "Task parameter 0 value"); + final AxTaskParameter taskPar1 = new AxTaskParameter(new AxReferenceKey(task.getKey(), "taskParameterA1"), + "Task parameter 1 value"); + + task.getTaskParameters().put(taskPar0.getKey().getLocalName(), taskPar0); + task.getTaskParameters().put(taskPar1.getKey().getLocalName(), taskPar1); + task.getContextAlbumReferences().add(album0.getKey()); + task.getContextAlbumReferences().add(album1.getKey()); + + final AxTaskLogic taskLogic = new AxTaskLogic(new AxReferenceKey(task.getKey(), "taskLogicA"), "MVEL", + "Some task logic"); + task.setTaskLogic(taskLogic); + + final AxTasks tasks = new AxTasks(new AxArtifactKey("tasksA", "0.0.1")); + tasks.getTaskMap().put(task.getKey(), task); + + final AxPolicy policy = new AxPolicy(new AxArtifactKey("policyA", "0.0.1")); + policy.setTemplate("FREEFORM"); + + final AxState state = new AxState(new AxReferenceKey(policy.getKey(), "stateA")); + final AxTaskSelectionLogic taskSelectionLogic = new AxTaskSelectionLogic( + new AxReferenceKey(state.getKey(), "taskSelectionLogicA"), "MVEL", "Some TS logic "); + + state.setTrigger(inEvent.getKey()); + state.getContextAlbumReferences().add(album0.getKey()); + state.getContextAlbumReferences().add(album1.getKey()); + state.setTaskSelectionLogic(taskSelectionLogic); + state.setDefaultTask(task.getKey()); + + final AxStateOutput stateOutput0 = new AxStateOutput(new AxReferenceKey(state.getKey(), "stateOutputA0"), + outEvent0.getKey(), AxReferenceKey.getNullKey()); + state.getStateOutputs().put(stateOutput0.getKey().getLocalName(), stateOutput0); + + final AxStateTaskReference stateTaskReference = new AxStateTaskReference( + new AxReferenceKey(state.getKey(), task.getKey().getName()), AxStateTaskOutputType.DIRECT, + stateOutput0.getKey()); + + state.getTaskReferences().put(task.getKey(), stateTaskReference); + + policy.getStateMap().put(state.getKey().getLocalName(), state); + policy.setFirstState(state.getKey().getLocalName()); + + final AxPolicies policies = new AxPolicies(new AxArtifactKey("policiesA", "0.0.1")); + policies.getPolicyMap().put(policy.getKey(), policy); + + final AxKeyInformation keyInformation = new AxKeyInformation(new AxArtifactKey("KeyInfoMapKeyA", "0.0.1")); + final AxPolicyModel policyModel = new AxPolicyModel(new AxArtifactKey("PolicyModelA", "0.0.1")); + policyModel.setKeyInformation(keyInformation); + policyModel.setSchemas(schemas); + policyModel.setAlbums(albums); + policyModel.setEvents(events); + policyModel.setTasks(tasks); + policyModel.setPolicies(policies); + policyModel.getKeyInformation().generateKeyInfo(policyModel); + + int uuidIncrementer = 0; + for (final AxKeyInfo keyInfo : policyModel.getKeyInformation().getKeyInfoMap().values()) { + final String uuidString = String.format("ce9168c-e6df-414f-9646-6da464b6e%03d", uuidIncrementer++); + keyInfo.setUuid(UUID.fromString(uuidString)); + } + + final AxValidationResult result = new AxValidationResult(); + policyModel.validate(result); + + return policyModel; + } + + @Override + public AxPolicyModel getMalstructuredModel() { + final AxPolicyModel policyModel = new AxPolicyModel(new AxArtifactKey("policyModel", "0.0.1")); + return policyModel; + } + + @Override + public AxPolicyModel getObservationModel() { + final AxPolicyModel policyModel = getModel(); + + final AxState state = policyModel.getPolicies().get("policy").getStateMap().get("state"); + final AxTask task = policyModel.getTasks().get("task"); + + final AxStateFinalizerLogic stateFinalizerLogic = new AxStateFinalizerLogic( + new AxReferenceKey(state.getKey(), "SFL"), "MVEL", "Some SF logic "); + state.getStateFinalizerLogicMap().put(stateFinalizerLogic.getKey().getLocalName(), stateFinalizerLogic); + final AxStateTaskReference stateTaskReference = new AxStateTaskReference( + new AxReferenceKey(state.getKey(), task.getKey().getName()), AxStateTaskOutputType.LOGIC, + stateFinalizerLogic.getKey()); + + state.getTaskReferences().put(task.getKey(), stateTaskReference); + + return policyModel; + } + + @Override + public AxPolicyModel getWarningModel() { + final AxPolicyModel policyModel = getModel(); + + final AxState anotherState = new AxState( + new AxReferenceKey(new AxArtifactKey("policy", "0.0.1"), "anotherState")); + + final AxEvent inEvent = policyModel.getEvents().getEventMap().get(new AxArtifactKey("inEvent", "0.0.1")); + final AxEvent outEvent0 = policyModel.getEvents().getEventMap().get(new AxArtifactKey("outEvent0", "0.0.1")); + + final AxTask anotherTask = new AxTask(new AxArtifactKey("anotherTask", "0.0.1")); + + for (final AxField field : inEvent.getFields()) { + final AxReferenceKey fieldkey = new AxReferenceKey(anotherTask.getKey().getName(), + anotherTask.getKey().getVersion(), "inputFields", field.getKey().getLocalName()); + final AxInputField inputField = new AxInputField(fieldkey, field.getSchema()); + anotherTask.getInputFields().put(inputField.getKey().getLocalName(), inputField); + } + + for (final AxField field : outEvent0.getFields()) { + final AxReferenceKey fieldkey = new AxReferenceKey(anotherTask.getKey().getName(), + anotherTask.getKey().getVersion(), "outputFields", field.getKey().getLocalName()); + final AxOutputField outputField = new AxOutputField(fieldkey, field.getSchema()); + anotherTask.getOutputFields().put(outputField.getKey().getLocalName(), outputField); + } + + final AxTaskParameter taskPar0 = new AxTaskParameter(new AxReferenceKey(anotherTask.getKey(), "taskParameter0"), + "Task parameter 0 value"); + final AxTaskParameter taskPar1 = new AxTaskParameter(new AxReferenceKey(anotherTask.getKey(), "taskParameter1"), + "Task parameter 1 value"); + + anotherTask.getTaskParameters().put(taskPar0.getKey().getLocalName(), taskPar0); + anotherTask.getTaskParameters().put(taskPar1.getKey().getLocalName(), taskPar1); + + final AxTaskLogic taskLogic = new AxTaskLogic(new AxReferenceKey(anotherTask.getKey(), "taskLogic"), "MVEL", + "Some task logic"); + anotherTask.setTaskLogic(taskLogic); + policyModel.getTasks().getTaskMap().put(anotherTask.getKey(), anotherTask); + + final AxStateOutput anotherStateOutput0 = new AxStateOutput( + new AxReferenceKey(anotherState.getKey(), "stateOutput0"), outEvent0.getKey(), + AxReferenceKey.getNullKey()); + anotherState.setTrigger(inEvent.getKey()); + anotherState.getStateOutputs().put(anotherStateOutput0.getKey().getLocalName(), anotherStateOutput0); + anotherState.setDefaultTask(anotherTask.getKey()); + final AxStateTaskReference anotherStateTaskReference = new AxStateTaskReference( + new AxReferenceKey(anotherState.getKey(), anotherTask.getKey().getName()), + AxStateTaskOutputType.DIRECT, anotherStateOutput0.getKey()); + anotherState.getTaskReferences().put(anotherTask.getKey(), anotherStateTaskReference); + + policyModel.getPolicies().getPolicyMap().get(new AxArtifactKey("policy", "0.0.1")).getStateMap() + .put(anotherState.getKey().getLocalName(), anotherState); + + policyModel.getKeyInformation().generateKeyInfo(policyModel); + + return policyModel; + } + + @Override + public AxPolicyModel getInvalidModel() { + final AxPolicyModel policyModel = getModel(); + + policyModel.getAlbums().get(new AxArtifactKey("contextAlbum0", "0.0.1")).setScope("UNDEFINED"); + policyModel.getAlbums().get(new AxArtifactKey("contextAlbum1", "0.0.1")).setScope("UNDEFINED"); + + final AxEvent outEvent0 = policyModel.getEvents().get("outEvent0"); + outEvent0.getParameterMap().remove("OE1PAR0"); + outEvent0.getParameterMap().remove("OE1PAR1"); + + return policyModel; + } +} diff --git a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/ApexPolicyModelTest.java b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/ApexPolicyModelTest.java new file mode 100644 index 000000000..997664b13 --- /dev/null +++ b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/ApexPolicyModelTest.java @@ -0,0 +1,179 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-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.apex.model.policymodel.handling; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.sql.Connection; +import java.sql.DriverManager; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult; +import org.onap.policy.apex.model.basicmodel.dao.DaoParameters; +import org.onap.policy.apex.model.basicmodel.test.TestApexModel; +import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; + +public class ApexPolicyModelTest { + private Connection connection; + TestApexModel testApexModel; + + /** + * Set up the policy model tests. + * + * @throws Exception on setup errors + */ + @Before + public void setup() throws Exception { + Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance(); + connection = DriverManager.getConnection("jdbc:derby:memory:apex_test;create=true"); + + testApexModel = new TestApexModel(AxPolicyModel.class, new ApexPolicyModelCreatorTest()); + } + + @After + public void teardown() throws Exception { + connection.close(); + new File("derby.log").delete(); + } + + @Test + public void testModelValid() throws Exception { + final AxValidationResult result = testApexModel.testApexModelValid(); + assertTrue(result.toString().equals(VALID_MODEL_STRING)); + } + + @Test + public void testApexModelVaidateObservation() throws Exception { + final AxValidationResult result = testApexModel.testApexModelVaidateObservation(); + assertTrue(result.toString().equals(OBSERVATION_MODEL_STRING)); + } + + @Test + public void testApexModelVaidateWarning() throws Exception { + final AxValidationResult result = testApexModel.testApexModelVaidateWarning(); + assertTrue(result.toString().equals(WARNING_MODEL_STRING)); + } + + @Test + public void testModelVaidateInvalidModel() throws Exception { + final AxValidationResult result = testApexModel.testApexModelVaidateInvalidModel(); + assertEquals(INVALID_MODEL_STRING, result.toString()); + } + + @Test + public void testModelVaidateMalstructured() throws Exception { + final AxValidationResult result = testApexModel.testApexModelVaidateMalstructured(); + assertTrue(result.toString().equals(INVALID_MODEL_MALSTRUCTURED_STRING)); + } + + @Test + public void testModelWriteReadXml() throws Exception { + testApexModel.testApexModelWriteReadXml(); + } + + @Test + public void testModelWriteReadJson() throws Exception { + testApexModel.testApexModelWriteReadJson(); + } + + @Test + public void testModelWriteReadJpa() throws Exception { + final DaoParameters DaoParameters = new DaoParameters(); + DaoParameters.setPluginClass("org.onap.policy.apex.model.basicmodel.dao.impl.DefaultApexDao"); + DaoParameters.setPersistenceUnit("DAOTest"); + + testApexModel.testApexModelWriteReadJpa(DaoParameters); + } + + private static final String VALID_MODEL_STRING = "***validation of model successful***"; + + private static final String OBSERVATION_MODEL_STRING = "\n" + + "***observations noted during validation of model***\n" + + "AxReferenceKey:(parentKeyName=policy,parentKeyVersion=0.0.1,parentLocalName=NULL," + + "localName=state):org.onap.policy.apex.model.policymodel.concepts.AxState:OBSERVATION:" + + "state output stateOutput0 is not used directly by any task\n" + + "********************************"; + + private static final String WARNING_MODEL_STRING = "\n" + "***warnings issued during validation of model***\n" + + "AxArtifactKey:(name=policy,version=0.0.1)" + + ":org.onap.policy.apex.model.policymodel.concepts.AxPolicy:WARNING:state AxReferenceKey:" + + "(parentKeyName=policy,parentKeyVersion=0.0.1,parentLocalName=NULL,localName=anotherState) " + + "is not referenced in the policy execution tree\n" + "********************************"; + + private static final String INVALID_MODEL_STRING = "\n" + "***validation of model failed***\n" + + "AxArtifactKey:(name=contextAlbum0,version=0.0.1):" + + "org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbum:INVALID:scope is not defined\n" + + "AxArtifactKey:(name=contextAlbum1,version=0.0.1):" + + "org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbum:INVALID:scope is not defined\n" + + "AxReferenceKey:" + + "(parentKeyName=policy,parentKeyVersion=0.0.1,parentLocalName=NULL,localName=state):" + + "org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel:INVALID:" + + "task output field AxOutputField:(key=AxReferenceKey:" + + "(parentKeyName=task,parentKeyVersion=0.0.1,parentLocalName=outputFields,localName=OE1PAR0)," + + "fieldSchemaKey=AxArtifactKey:(name=eventContextItem0,version=0.0.1),optional=false) for task " + + "task:0.0.1 not in output event outEvent0:0.0.1\n" + "AxReferenceKey:" + + "(parentKeyName=policy,parentKeyVersion=0.0.1,parentLocalName=NULL,localName=state):" + + "org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel:INVALID:" + + "task output field AxOutputField:(key=AxReferenceKey:" + + "(parentKeyName=task,parentKeyVersion=0.0.1,parentLocalName=outputFields,localName=OE1PAR1)," + + "fieldSchemaKey=AxArtifactKey:(name=eventContextItem1,version=0.0.1),optional=false) for task " + + "task:0.0.1 not in output event outEvent0:0.0.1\n" + "********************************"; + + private static final String INVALID_MODEL_MALSTRUCTURED_STRING = "\n" + "***validation of model failed***\n" + + "AxArtifactKey:(name=policyModel_KeyInfo,version=0.0.1):" + + "org.onap.policy.apex.model.basicmodel.concepts.AxKeyInformation:INVALID:" + + "keyInfoMap may not be empty\n" + "AxArtifactKey:(name=policyModel,version=0.0.1)" + + ":org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel:INVALID:" + + "key information not found for key AxArtifactKey:(name=policyModel,version=0.0.1)\n" + + "AxArtifactKey:(name=policyModel,version=0.0.1)" + + ":org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel:INVALID:" + + "key information not found for key AxArtifactKey:(name=policyModel_KeyInfo,version=0.0.1)\n" + + "AxArtifactKey:(name=policyModel,version=0.0.1)" + + ":org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel:INVALID:" + + "key information not found for key AxArtifactKey:(name=policyModel_Schemas,version=0.0.1)\n" + + "AxArtifactKey:(name=policyModel,version=0.0.1)" + + ":org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel:INVALID:" + + "key information not found for key AxArtifactKey:(name=policyModel_Events,version=0.0.1)\n" + + "AxArtifactKey:(name=policyModel,version=0.0.1)" + + ":org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel:INVALID:" + + "key information not found for key AxArtifactKey:(name=policyModel_Albums,version=0.0.1)\n" + + "AxArtifactKey:(name=policyModel,version=0.0.1)" + + ":org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel:INVALID:" + + "key information not found for key AxArtifactKey:(name=policyModel_Tasks,version=0.0.1)\n" + + "AxArtifactKey:(name=policyModel,version=0.0.1)" + + ":org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel:INVALID:" + + "key information not found for key AxArtifactKey:(name=policyModel_Policies,version=0.0.1)\n" + + "AxArtifactKey:(name=policyModel_Schemas,version=0.0.1):" + + "org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas:INVALID:" + + "contextSchemas may not be empty\n" + "AxArtifactKey:(name=policyModel_Events,version=0.0.1):" + + "org.onap.policy.apex.model.eventmodel.concepts.AxEvents:INVALID:eventMap may not be empty\n" + + "AxArtifactKey:(name=policyModel_Albums,version=0.0.1):" + + "org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbums:OBSERVATION:albums are empty\n" + + "AxArtifactKey:(name=policyModel_Tasks,version=0.0.1)" + + ":org.onap.policy.apex.model.policymodel.concepts.AxTasks:INVALID:taskMap may not be empty\n" + + "AxArtifactKey:(name=policyModel_Policies,version=0.0.1)" + + ":org.onap.policy.apex.model.policymodel.concepts.AxPolicies:INVALID:policyMap may not be empty\n" + + "********************************"; +} diff --git a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/PolicyAnalyserTest.java b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/PolicyAnalyserTest.java new file mode 100644 index 000000000..425509d3a --- /dev/null +++ b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/PolicyAnalyserTest.java @@ -0,0 +1,60 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-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.apex.model.policymodel.handling; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; +import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; +import org.onap.policy.apex.model.policymodel.handling.PolicyAnalyser; +import org.onap.policy.apex.model.policymodel.handling.PolicyAnalysisResult; + +public class PolicyAnalyserTest { + @Test + public void test() { + final AxPolicyModel apexModel = new ApexPolicyModelCreatorTest().getModel(); + + final PolicyAnalyser policyAnalyser = new PolicyAnalyser(); + final PolicyAnalysisResult analysisResult = policyAnalyser.analyse(apexModel); + + assertTrue(analysisResult.toString().equals(EXPECTED_ANALYSIS_RESULT)); + + assertNotNull(analysisResult.getUsedContextAlbums()); + assertNotNull(analysisResult.getUsedContextSchemas()); + assertNotNull(analysisResult.getUsedEvents()); + assertNotNull(analysisResult.getUsedTasks()); + assertNotNull(analysisResult.getUnusedContextAlbums()); + assertNotNull(analysisResult.getUnusedContextSchemas()); + assertNotNull(analysisResult.getUnusedEvents()); + assertNotNull(analysisResult.getUnusedTasks()); + } + + private static final String EXPECTED_ANALYSIS_RESULT = "" + "Context Schema usage\n" + " MapType:0.0.1\n" + + " contextAlbum0:0.0.1\n" + " StringType:0.0.1\n" + " contextAlbum1:0.0.1\n" + + " eventContextItem0:0.0.1\n" + " inEvent:0.0.1\n" + " outEvent0:0.0.1\n" + " outEvent1:0.0.1\n" + + " task:0.0.1\n" + " eventContextItem1:0.0.1\n" + " inEvent:0.0.1\n" + " outEvent0:0.0.1\n" + + " outEvent1:0.0.1\n" + " task:0.0.1\n" + "Context Album usage\n" + " contextAlbum0:0.0.1\n" + + " task:0.0.1\n" + " policy:0.0.1:NULL:state\n" + " contextAlbum1:0.0.1\n" + " task:0.0.1\n" + + " policy:0.0.1:NULL:state\n" + "Event usage\n" + " inEvent:0.0.1\n" + " policy:0.0.1:NULL:state\n" + + " outEvent0:0.0.1\n" + " policy:0.0.1:NULL:state\n" + " outEvent1:0.0.1 (unused)\n" + "Task usage\n" + + " task:0.0.1\n" + " policy:0.0.1:NULL:state\n"; +} diff --git a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/PolicyLogicReaderTest.java b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/PolicyLogicReaderTest.java new file mode 100644 index 000000000..23f9d3820 --- /dev/null +++ b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/PolicyLogicReaderTest.java @@ -0,0 +1,114 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-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.apex.model.policymodel.handling; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import org.junit.Test; +import org.onap.policy.apex.model.basicmodel.concepts.AxKey; +import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey; +import org.onap.policy.apex.model.policymodel.concepts.AxLogic; +import org.onap.policy.apex.model.policymodel.handling.PolicyLogicReader; + +/** + * Logic reader for policy tests. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class PolicyLogicReaderTest { + + @Test + public void test() { + final AxReferenceKey logicKey = new AxReferenceKey("LogicParent", "0.0.1", "LogicInstanceName"); + + final PolicyLogicReader plReader = new PolicyLogicReader(); + + plReader.setLogicPackage("somewhere.over.the.rainbow"); + assertEquals("somewhere.over.the.rainbow", plReader.getLogicPackage()); + + plReader.setDefaultLogic("FunkyDefaultLogic"); + assertEquals("FunkyDefaultLogic", plReader.getDefaultLogic()); + + try { + new AxLogic(logicKey, "FunkyLogic", plReader); + fail("test should throw an exception here"); + } catch (final Exception e) { + assertEquals("logic not found for logic " + + "\"somewhere/over/the/rainbow/funkylogic/FunkyDefaultLogic.funkylogic\"", e.getMessage()); + } + + plReader.setDefaultLogic(null); + try { + new AxLogic(logicKey, "FunkyLogic", plReader); + fail("test should throw an exception here"); + } catch (final Exception e) { + assertEquals("logic not found for logic " + + "\"somewhere/over/the/rainbow/funkylogic/LogicParentLogicInstanceName.funkylogic\"", + e.getMessage()); + } + + logicKey.setParentLocalName("LogicParentLocalName"); + try { + new AxLogic(logicKey, "FunkyLogic", plReader); + fail("test should throw an exception here"); + } catch (final Exception e) { + assertEquals("logic not found for logic " + "\"somewhere/over/the/rainbow/funkylogic/" + + "LogicParentLogicParentLocalNameLogicInstanceName.funkylogic\"", e.getMessage()); + } + + plReader.setLogicPackage("path.to.apex.logic"); + try { + final AxLogic logic = new AxLogic(logicKey, "FunkyLogic", plReader); + assertTrue(logic.getLogic().endsWith("Way out man, this is funky logic!")); + } catch (final Exception e) { + fail("test should not throw an exception"); + } + + plReader.setLogicPackage("somewhere.over.the.rainbow"); + plReader.setDefaultLogic("JavaLogic"); + + try { + final AxLogic logic = new AxLogic(logicKey, "JAVA", plReader); + assertEquals("somewhere.over.the.rainbow.java.JavaLogic", logic.getLogic()); + } catch (final Exception e) { + fail("test should not throw an exception"); + } + + plReader.setDefaultLogic(null); + try { + final AxLogic logic = new AxLogic(logicKey, "JAVA", plReader); + assertEquals("somewhere.over.the.rainbow.java.LogicParentLogicParentLocalNameLogicInstanceName", + logic.getLogic()); + } catch (final Exception e) { + fail("test should not throw an exception"); + } + + logicKey.setParentLocalName(AxKey.NULL_KEY_NAME); + try { + final AxLogic logic = new AxLogic(logicKey, "JAVA", plReader); + assertEquals("somewhere.over.the.rainbow.java.LogicParentLogicInstanceName", logic.getLogic()); + } catch (final Exception e) { + fail("test should not throw an exception"); + } + } +} diff --git a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/PolicyModelComparerTest.java b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/PolicyModelComparerTest.java new file mode 100644 index 000000000..08d00e628 --- /dev/null +++ b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/PolicyModelComparerTest.java @@ -0,0 +1,129 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-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.apex.model.policymodel.handling; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.io.IOException; +import java.util.UUID; + +import org.junit.Test; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.basicmodel.concepts.AxKeyInfo; +import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; +import org.onap.policy.apex.model.policymodel.handling.PolicyComparer; +import org.onap.policy.apex.model.policymodel.handling.PolicyModelComparer; +import org.onap.policy.apex.model.utilities.TextFileUtils; + +public class PolicyModelComparerTest { + + @Test + public void testPolicyComparer() throws IOException { + final AxPolicyModel leftApexModel = new ApexPolicyModelCreatorTest().getModel(); + final AxPolicyModel rightApexModel = new AxPolicyModel(leftApexModel); + + PolicyModelComparer policyModelComparer = new PolicyModelComparer(leftApexModel, rightApexModel); + + String resultString = policyModelComparer.asString(false, false); + String checkString = TextFileUtils + .getTextFileAsString("src/test/resources/checkFiles/PolicyModelComparisonIdenticalVerboseValues.txt"); + assertEquals(resultString.trim().replaceAll("[\\r?\\n]+", " "), + checkString.trim().replaceAll("[\\r?\\n]+", " ")); + + resultString = policyModelComparer.asString(false, true); + checkString = TextFileUtils + .getTextFileAsString("src/test/resources/checkFiles/PolicyModelComparisonIdenticalVerboseKeys.txt"); + assertTrue(resultString.trim().replaceAll("[\\r?\\n]+", " ") + .equals(checkString.trim().replaceAll("[\\r?\\n]+", " "))); + + resultString = policyModelComparer.asString(true, false); + checkString = TextFileUtils + .getTextFileAsString("src/test/resources/checkFiles/PolicyModelComparisonIdenticalTerse.txt"); + assertTrue(resultString.trim().replaceAll("[\\r?\\n]+", " ") + .equals(checkString.trim().replaceAll("[\\r?\\n]+", " "))); + + resultString = policyModelComparer.asString(true, true); + checkString = TextFileUtils + .getTextFileAsString("src/test/resources/checkFiles/PolicyModelComparisonIdenticalTerse.txt"); + assertTrue(resultString.trim().replaceAll("[\\r?\\n]+", " ") + .equals(checkString.trim().replaceAll("[\\r?\\n]+", " "))); + + final AxKeyInfo leftOnlyKeyInfo = new AxKeyInfo(new AxArtifactKey("LeftOnlyKeyInfo", "0.0.1"), + UUID.fromString("ce9168c-e6df-414f-9646-6da464b6f000"), "Left only key info"); + final AxKeyInfo rightOnlyKeyInfo = new AxKeyInfo(new AxArtifactKey("RightOnlyKeyInfo", "0.0.1"), + UUID.fromString("ce9168c-e6df-414f-9646-6da464b6f001"), "Right only key info"); + + leftApexModel.getKeyInformation().getKeyInfoMap().put(leftOnlyKeyInfo.getKey(), leftOnlyKeyInfo); + rightApexModel.getKeyInformation().getKeyInfoMap().put(rightOnlyKeyInfo.getKey(), rightOnlyKeyInfo); + + leftApexModel.getKeyInformation().getKeyInfoMap().get(new AxArtifactKey("inEvent", "0.0.1")) + .setDescription("Left InEvent Description"); + rightApexModel.getKeyInformation().getKeyInfoMap().get(new AxArtifactKey("inEvent", "0.0.1")) + .setDescription("Right InEvent Description"); + + policyModelComparer = new PolicyModelComparer(leftApexModel, rightApexModel); + + resultString = policyModelComparer.asString(false, false); + checkString = TextFileUtils + .getTextFileAsString("src/test/resources/checkFiles/PolicyModelComparisonDifferentVerboseValues.txt"); + assertEquals(resultString.trim().replaceAll("[\\r?\\n]+", " "), + checkString.trim().replaceAll("[\\r?\\n]+", " ")); + + resultString = policyModelComparer.asString(false, true); + checkString = TextFileUtils + .getTextFileAsString("src/test/resources/checkFiles/PolicyModelComparisonDifferentVerboseKeys.txt"); + assertTrue(resultString.trim().replaceAll("[\\r?\\n]+", " ") + .equals(checkString.trim().replaceAll("[\\r?\\n]+", " "))); + + resultString = policyModelComparer.asString(true, false); + checkString = TextFileUtils + .getTextFileAsString("src/test/resources/checkFiles/PolicyModelComparisonDifferentTerseValues.txt"); + assertTrue(resultString.trim().replaceAll("[\\r?\\n]+", " ") + .equals(checkString.trim().replaceAll("[\\r?\\n]+", " "))); + + resultString = policyModelComparer.asString(true, true); + checkString = TextFileUtils + .getTextFileAsString("src/test/resources/checkFiles/PolicyModelComparisonDifferentTerseKeys.txt"); + assertTrue(resultString.trim().replaceAll("[\\r?\\n]+", " ") + .equals(checkString.trim().replaceAll("[\\r?\\n]+", " "))); + + assertNotNull(policyModelComparer.getContextAlbumComparisonResult()); + assertNotNull(policyModelComparer.getContextAlbumKeyDifference()); + assertNotNull(policyModelComparer.getContextSchemaComparisonResult()); + assertNotNull(policyModelComparer.getContextSchemaKeyDifference()); + assertNotNull(policyModelComparer.getEventComparisonResult()); + assertNotNull(policyModelComparer.getEventKeyDifference()); + assertNotNull(policyModelComparer.getKeyInfoComparisonResult()); + assertNotNull(policyModelComparer.getKeyInformationKeyDifference()); + assertNotNull(policyModelComparer.getPolicyComparisonResult()); + assertNotNull(policyModelComparer.getPolicykeyDifference()); + assertNotNull(policyModelComparer.getPolicyModelsKeyDifference()); + assertNotNull(policyModelComparer.getTaskComparisonResult()); + assertNotNull(policyModelComparer.getTaskKeyDifference()); + + assertNotNull(new PolicyComparer().compare(leftApexModel.getPolicies(), rightApexModel.getPolicies())); + + assertEquals("****** policy map differences ******\n*** context s", + policyModelComparer.toString().substring(0, 50)); + } +} diff --git a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/PolicyModelMergerTest.java b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/PolicyModelMergerTest.java new file mode 100644 index 000000000..7d5ba6a40 --- /dev/null +++ b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/PolicyModelMergerTest.java @@ -0,0 +1,111 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-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.apex.model.policymodel.handling; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; + +import org.junit.Test; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.basicmodel.handling.ApexModelException; +import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; +import org.onap.policy.apex.model.policymodel.handling.PolicyModelMerger; + +/** + * Test model merging. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class PolicyModelMergerTest { + + @Test + public void testPolicyModelMerger() { + final AxPolicyModel leftPolicyModel = new ApexPolicyModelCreatorTest().getModel(); + AxPolicyModel rightPolicyModel = new ApexPolicyModelCreatorTest().getModel(); + + try { + final AxPolicyModel mergedPolicyModel = PolicyModelMerger.getMergedPolicyModel(leftPolicyModel, + rightPolicyModel, false); + assertEquals(leftPolicyModel, mergedPolicyModel); + assertEquals(rightPolicyModel, mergedPolicyModel); + } catch (final ApexModelException e) { + fail("test should not throw an exception"); + } + + leftPolicyModel.setKey(new AxArtifactKey("LeftPolicyModel", "0.0.1")); + try { + PolicyModelMerger.getMergedPolicyModel(leftPolicyModel, rightPolicyModel, false); + fail("test should throw an exception here"); + } catch (final ApexModelException e) { + assertEquals("left model is invalid: \n***validation of model fai", e.getMessage().substring(0, 50)); + } + + leftPolicyModel.setKey(new AxArtifactKey("LeftPolicyModel", "0.0.1")); + try { + assertNotNull(PolicyModelMerger.getMergedPolicyModel(leftPolicyModel, rightPolicyModel, false, true)); + } catch (final ApexModelException e) { + fail("test should not throw an exception"); + } + + leftPolicyModel.getKeyInformation().generateKeyInfo(leftPolicyModel); + try { + final AxPolicyModel mergedPolicyModel = PolicyModelMerger.getMergedPolicyModel(leftPolicyModel, + rightPolicyModel, true); + assertNotNull(mergedPolicyModel); + } catch (final ApexModelException e) { + fail("test should not throw an exception"); + } + + rightPolicyModel.setKey(new AxArtifactKey("RightPolicyModel", "0.0.1")); + try { + PolicyModelMerger.getMergedPolicyModel(leftPolicyModel, rightPolicyModel, false); + fail("test should throw an exception here"); + } catch (final ApexModelException e) { + assertEquals("right model is invalid: \n***validation of model fa", e.getMessage().substring(0, 50)); + } + + rightPolicyModel.setKey(new AxArtifactKey("RightPolicyModel", "0.0.1")); + try { + assertNotNull(PolicyModelMerger.getMergedPolicyModel(leftPolicyModel, rightPolicyModel, false, true)); + } catch (final ApexModelException e) { + fail("test should not throw an exception"); + } + + rightPolicyModel.getKeyInformation().generateKeyInfo(rightPolicyModel); + try { + final AxPolicyModel mergedPolicyModel = PolicyModelMerger.getMergedPolicyModel(leftPolicyModel, + rightPolicyModel, false); + assertNotNull(mergedPolicyModel); + } catch (final ApexModelException e) { + fail("test should not throw an exception"); + } + + rightPolicyModel = new ApexPolicyModelCreatorTest().getAnotherModel(); + try { + final AxPolicyModel mergedPolicyModel = PolicyModelMerger.getMergedPolicyModel(leftPolicyModel, + rightPolicyModel, true); + assertNotNull(mergedPolicyModel); + } catch (final ApexModelException e) { + fail("test should not throw an exception"); + } + } +} diff --git a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/PolicyModelSplitterTest.java b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/PolicyModelSplitterTest.java new file mode 100644 index 000000000..29e7307f4 --- /dev/null +++ b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/PolicyModelSplitterTest.java @@ -0,0 +1,95 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-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.apex.model.policymodel.handling; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.util.Set; +import java.util.TreeSet; + +import org.junit.Test; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.basicmodel.handling.ApexModelException; +import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; +import org.onap.policy.apex.model.policymodel.handling.PolicyModelSplitter; + +public class PolicyModelSplitterTest { + @Test + public void test() { + final AxPolicyModel apexModel = new ApexPolicyModelCreatorTest().getModel(); + + final Set requiredPolicySet = new TreeSet(); + requiredPolicySet.add(new AxArtifactKey("policy", "0.0.1")); + + // There's only one policy so a split of this model on that policy should return the same + // model + AxPolicyModel splitApexModel = null; + try { + splitApexModel = PolicyModelSplitter.getSubPolicyModel(apexModel, requiredPolicySet); + } catch (final ApexModelException e) { + fail(e.getMessage()); + } + + // The only difference between the models should be that the unused event outEvent1 should + // not be in the split model + apexModel.getEvents().getEventMap().remove(new AxArtifactKey("outEvent1", "0.0.1")); + apexModel.getKeyInformation().getKeyInfoMap().remove(new AxArtifactKey("outEvent1", "0.0.1")); + assertTrue(apexModel.equals(splitApexModel)); + + final Set requiredMissingPolicySet = new TreeSet(); + requiredPolicySet.add(new AxArtifactKey("MissingPolicy", "0.0.1")); + + AxPolicyModel missingSplitApexModel = null; + try { + missingSplitApexModel = PolicyModelSplitter.getSubPolicyModel(apexModel, requiredMissingPolicySet); + } catch (final ApexModelException e) { + fail(e.getMessage()); + } + assertNotNull(missingSplitApexModel); + + splitApexModel = null; + try { + splitApexModel = PolicyModelSplitter.getSubPolicyModel(apexModel, requiredPolicySet, true); + } catch (final ApexModelException e) { + fail(e.getMessage()); + } + + // The only difference between the models should be that the unused event outEvent1 should + // not be in the split model + apexModel.getEvents().getEventMap().remove(new AxArtifactKey("outEvent1", "0.0.1")); + apexModel.getKeyInformation().getKeyInfoMap().remove(new AxArtifactKey("outEvent1", "0.0.1")); + assertTrue(apexModel.equals(splitApexModel)); + + // There's only one policy so a split of this model on that policy should return the same + // model + try { + apexModel.getKey().setName("InvalidPolicyModelName"); + PolicyModelSplitter.getSubPolicyModel(apexModel, requiredPolicySet); + fail("test should throw an exception here"); + } catch (final Exception e) { + assertEquals("source model is invalid: \n***validation of model f", e.getMessage().substring(0, 50)); + } + + } +} diff --git a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/TestApexPolicyModel.java b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/TestApexPolicyModel.java deleted file mode 100644 index 6c4fd5174..000000000 --- a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/TestApexPolicyModel.java +++ /dev/null @@ -1,179 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-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.apex.model.policymodel.handling; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - -import java.io.File; -import java.sql.Connection; -import java.sql.DriverManager; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult; -import org.onap.policy.apex.model.basicmodel.dao.DaoParameters; -import org.onap.policy.apex.model.basicmodel.test.TestApexModel; -import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; - -public class TestApexPolicyModel { - private Connection connection; - TestApexModel testApexModel; - - /** - * Set up the policy model tests. - * - * @throws Exception on setup errors - */ - @Before - public void setup() throws Exception { - Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance(); - connection = DriverManager.getConnection("jdbc:derby:memory:apex_test;create=true"); - - testApexModel = new TestApexModel(AxPolicyModel.class, new TestApexPolicyModelCreator()); - } - - @After - public void teardown() throws Exception { - connection.close(); - new File("derby.log").delete(); - } - - @Test - public void testModelValid() throws Exception { - final AxValidationResult result = testApexModel.testApexModelValid(); - assertTrue(result.toString().equals(VALID_MODEL_STRING)); - } - - @Test - public void testApexModelVaidateObservation() throws Exception { - final AxValidationResult result = testApexModel.testApexModelVaidateObservation(); - assertTrue(result.toString().equals(OBSERVATION_MODEL_STRING)); - } - - @Test - public void testApexModelVaidateWarning() throws Exception { - final AxValidationResult result = testApexModel.testApexModelVaidateWarning(); - assertTrue(result.toString().equals(WARNING_MODEL_STRING)); - } - - @Test - public void testModelVaidateInvalidModel() throws Exception { - final AxValidationResult result = testApexModel.testApexModelVaidateInvalidModel(); - assertEquals(INVALID_MODEL_STRING, result.toString()); - } - - @Test - public void testModelVaidateMalstructured() throws Exception { - final AxValidationResult result = testApexModel.testApexModelVaidateMalstructured(); - assertTrue(result.toString().equals(INVALID_MODEL_MALSTRUCTURED_STRING)); - } - - @Test - public void testModelWriteReadXml() throws Exception { - testApexModel.testApexModelWriteReadXml(); - } - - @Test - public void testModelWriteReadJson() throws Exception { - testApexModel.testApexModelWriteReadJson(); - } - - @Test - public void testModelWriteReadJpa() throws Exception { - final DaoParameters DaoParameters = new DaoParameters(); - DaoParameters.setPluginClass("org.onap.policy.apex.model.basicmodel.dao.impl.DefaultApexDao"); - DaoParameters.setPersistenceUnit("DAOTest"); - - testApexModel.testApexModelWriteReadJpa(DaoParameters); - } - - private static final String VALID_MODEL_STRING = "***validation of model successful***"; - - private static final String OBSERVATION_MODEL_STRING = "\n" - + "***observations noted during validation of model***\n" - + "AxReferenceKey:(parentKeyName=policy,parentKeyVersion=0.0.1,parentLocalName=NULL," - + "localName=state):org.onap.policy.apex.model.policymodel.concepts.AxState:OBSERVATION:" - + "state output stateOutput0 is not used directly by any task\n" - + "********************************"; - - private static final String WARNING_MODEL_STRING = "\n" + "***warnings issued during validation of model***\n" - + "AxArtifactKey:(name=policy,version=0.0.1)" - + ":org.onap.policy.apex.model.policymodel.concepts.AxPolicy:WARNING:state AxReferenceKey:" - + "(parentKeyName=policy,parentKeyVersion=0.0.1,parentLocalName=NULL,localName=anotherState) " - + "is not referenced in the policy execution tree\n" + "********************************"; - - private static final String INVALID_MODEL_STRING = "\n" + "***validation of model failed***\n" - + "AxArtifactKey:(name=contextAlbum0,version=0.0.1):" - + "org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbum:INVALID:scope is not defined\n" - + "AxArtifactKey:(name=contextAlbum1,version=0.0.1):" - + "org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbum:INVALID:scope is not defined\n" - + "AxReferenceKey:" - + "(parentKeyName=policy,parentKeyVersion=0.0.1,parentLocalName=NULL,localName=state):" - + "org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel:INVALID:" - + "task output field AxOutputField:(key=AxReferenceKey:" - + "(parentKeyName=task,parentKeyVersion=0.0.1,parentLocalName=outputFields,localName=OE1PAR0)," - + "fieldSchemaKey=AxArtifactKey:(name=eventContextItem0,version=0.0.1),optional=false) for task " - + "task:0.0.1 not in output event outEvent0:0.0.1\n" + "AxReferenceKey:" - + "(parentKeyName=policy,parentKeyVersion=0.0.1,parentLocalName=NULL,localName=state):" - + "org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel:INVALID:" - + "task output field AxOutputField:(key=AxReferenceKey:" - + "(parentKeyName=task,parentKeyVersion=0.0.1,parentLocalName=outputFields,localName=OE1PAR1)," - + "fieldSchemaKey=AxArtifactKey:(name=eventContextItem1,version=0.0.1),optional=false) for task " - + "task:0.0.1 not in output event outEvent0:0.0.1\n" + "********************************"; - - private static final String INVALID_MODEL_MALSTRUCTURED_STRING = "\n" + "***validation of model failed***\n" - + "AxArtifactKey:(name=policyModel_KeyInfo,version=0.0.1):" - + "org.onap.policy.apex.model.basicmodel.concepts.AxKeyInformation:INVALID:" - + "keyInfoMap may not be empty\n" + "AxArtifactKey:(name=policyModel,version=0.0.1)" - + ":org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel:INVALID:" - + "key information not found for key AxArtifactKey:(name=policyModel,version=0.0.1)\n" - + "AxArtifactKey:(name=policyModel,version=0.0.1)" - + ":org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel:INVALID:" - + "key information not found for key AxArtifactKey:(name=policyModel_KeyInfo,version=0.0.1)\n" - + "AxArtifactKey:(name=policyModel,version=0.0.1)" - + ":org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel:INVALID:" - + "key information not found for key AxArtifactKey:(name=policyModel_Schemas,version=0.0.1)\n" - + "AxArtifactKey:(name=policyModel,version=0.0.1)" - + ":org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel:INVALID:" - + "key information not found for key AxArtifactKey:(name=policyModel_Events,version=0.0.1)\n" - + "AxArtifactKey:(name=policyModel,version=0.0.1)" - + ":org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel:INVALID:" - + "key information not found for key AxArtifactKey:(name=policyModel_Albums,version=0.0.1)\n" - + "AxArtifactKey:(name=policyModel,version=0.0.1)" - + ":org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel:INVALID:" - + "key information not found for key AxArtifactKey:(name=policyModel_Tasks,version=0.0.1)\n" - + "AxArtifactKey:(name=policyModel,version=0.0.1)" - + ":org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel:INVALID:" - + "key information not found for key AxArtifactKey:(name=policyModel_Policies,version=0.0.1)\n" - + "AxArtifactKey:(name=policyModel_Schemas,version=0.0.1):" - + "org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas:INVALID:" - + "contextSchemas may not be empty\n" + "AxArtifactKey:(name=policyModel_Events,version=0.0.1):" - + "org.onap.policy.apex.model.eventmodel.concepts.AxEvents:INVALID:eventMap may not be empty\n" - + "AxArtifactKey:(name=policyModel_Albums,version=0.0.1):" - + "org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbums:OBSERVATION:albums are empty\n" - + "AxArtifactKey:(name=policyModel_Tasks,version=0.0.1)" - + ":org.onap.policy.apex.model.policymodel.concepts.AxTasks:INVALID:taskMap may not be empty\n" - + "AxArtifactKey:(name=policyModel_Policies,version=0.0.1)" - + ":org.onap.policy.apex.model.policymodel.concepts.AxPolicies:INVALID:policyMap may not be empty\n" - + "********************************"; -} diff --git a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/TestApexPolicyModelCreator.java b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/TestApexPolicyModelCreator.java deleted file mode 100644 index cbcecc589..000000000 --- a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/TestApexPolicyModelCreator.java +++ /dev/null @@ -1,455 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-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.apex.model.policymodel.handling; - -import java.util.UUID; - -import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; -import org.onap.policy.apex.model.basicmodel.concepts.AxKeyInfo; -import org.onap.policy.apex.model.basicmodel.concepts.AxKeyInformation; -import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey; -import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult; -import org.onap.policy.apex.model.basicmodel.test.TestApexModelCreator; -import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbum; -import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbums; -import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema; -import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas; -import org.onap.policy.apex.model.eventmodel.concepts.AxEvent; -import org.onap.policy.apex.model.eventmodel.concepts.AxEvents; -import org.onap.policy.apex.model.eventmodel.concepts.AxField; -import org.onap.policy.apex.model.eventmodel.concepts.AxInputField; -import org.onap.policy.apex.model.eventmodel.concepts.AxOutputField; -import org.onap.policy.apex.model.policymodel.concepts.AxPolicies; -import org.onap.policy.apex.model.policymodel.concepts.AxPolicy; -import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; -import org.onap.policy.apex.model.policymodel.concepts.AxState; -import org.onap.policy.apex.model.policymodel.concepts.AxStateFinalizerLogic; -import org.onap.policy.apex.model.policymodel.concepts.AxStateOutput; -import org.onap.policy.apex.model.policymodel.concepts.AxStateTaskOutputType; -import org.onap.policy.apex.model.policymodel.concepts.AxStateTaskReference; -import org.onap.policy.apex.model.policymodel.concepts.AxTask; -import org.onap.policy.apex.model.policymodel.concepts.AxTaskLogic; -import org.onap.policy.apex.model.policymodel.concepts.AxTaskParameter; -import org.onap.policy.apex.model.policymodel.concepts.AxTaskSelectionLogic; -import org.onap.policy.apex.model.policymodel.concepts.AxTasks; - -/** - * Model creator for model tests. - * - * @author Liam Fallon (liam.fallon@ericsson.com) - */ -public class TestApexPolicyModelCreator implements TestApexModelCreator { - - @Override - public AxPolicyModel getModel() { - final AxContextSchema schema0 = new AxContextSchema(new AxArtifactKey("eventContextItem0", "0.0.1"), "Java", - "java.lang.String"); - final AxContextSchema schema1 = new AxContextSchema(new AxArtifactKey("eventContextItem1", "0.0.1"), "Java", - "java.lang.Long"); - final AxContextSchema schema2 = new AxContextSchema(new AxArtifactKey("StringType", "0.0.1"), "Java", - "org.onap.policy.apex.model.policymodel.concepts.TestContextItem000"); - final AxContextSchema schema3 = new AxContextSchema(new AxArtifactKey("MapType", "0.0.1"), "Java", - "org.onap.policy.apex.model.policymodel.concepts.TestContextItem00A"); - - final AxContextSchemas schemas = new AxContextSchemas(new AxArtifactKey("ContextSchemas", "0.0.1")); - schemas.getSchemasMap().put(schema0.getKey(), schema0); - schemas.getSchemasMap().put(schema1.getKey(), schema1); - schemas.getSchemasMap().put(schema2.getKey(), schema2); - schemas.getSchemasMap().put(schema3.getKey(), schema3); - - final AxContextAlbum album0 = new AxContextAlbum(new AxArtifactKey("contextAlbum0", "0.0.1"), "APPLICATION", - true, schema3.getKey()); - final AxContextAlbum album1 = new AxContextAlbum(new AxArtifactKey("contextAlbum1", "0.0.1"), "GLOBAL", false, - schema2.getKey()); - - final AxContextAlbums albums = new AxContextAlbums(new AxArtifactKey("context", "0.0.1")); - albums.getAlbumsMap().put(album0.getKey(), album0); - albums.getAlbumsMap().put(album1.getKey(), album1); - - final AxEvent inEvent = new AxEvent(new AxArtifactKey("inEvent", "0.0.1"), - "org.onap.policy.apex.model.policymodel.events", "Source", "Target"); - inEvent.getParameterMap().put("IEPAR0", - new AxField(new AxReferenceKey(inEvent.getKey(), "IEPAR0"), schema0.getKey())); - inEvent.getParameterMap().put("IEPAR1", - new AxField(new AxReferenceKey(inEvent.getKey(), "IEPAR1"), schema1.getKey())); - - final AxEvent outEvent0 = new AxEvent(new AxArtifactKey("outEvent0", "0.0.1"), - "org.onap.policy.apex.model.policymodel.events", "Source", "Target"); - outEvent0.getParameterMap().put("OE0PAR0", - new AxField(new AxReferenceKey(outEvent0.getKey(), "OE0PAR0"), schema0.getKey())); - outEvent0.getParameterMap().put("OE0PAR1", - new AxField(new AxReferenceKey(outEvent0.getKey(), "OE0PAR1"), schema1.getKey())); - outEvent0.getParameterMap().put("OE1PAR0", - new AxField(new AxReferenceKey(outEvent0.getKey(), "OE1PAR0"), schema0.getKey())); - outEvent0.getParameterMap().put("OE1PAR1", - new AxField(new AxReferenceKey(outEvent0.getKey(), "OE1PAR1"), schema1.getKey())); - - final AxEvent outEvent1 = new AxEvent(new AxArtifactKey("outEvent1", "0.0.1"), - "org.onap.policy.apex.model.policymodel.events", "Source", "Target"); - outEvent1.getParameterMap().put("OE1PAR0", - new AxField(new AxReferenceKey(outEvent1.getKey(), "OE1PAR0"), schema0.getKey())); - outEvent1.getParameterMap().put("OE1PAR1", - new AxField(new AxReferenceKey(outEvent1.getKey(), "OE1PAR1"), schema1.getKey())); - - final AxEvents events = new AxEvents(new AxArtifactKey("events", "0.0.1")); - events.getEventMap().put(inEvent.getKey(), inEvent); - events.getEventMap().put(outEvent0.getKey(), outEvent0); - events.getEventMap().put(outEvent1.getKey(), outEvent1); - - final AxTask task = new AxTask(new AxArtifactKey("task", "0.0.1")); - - for (final AxField field : inEvent.getFields()) { - final AxReferenceKey fieldkey = new AxReferenceKey(task.getKey().getName(), task.getKey().getVersion(), - "inputFields", field.getKey().getLocalName()); - final AxInputField inputField = new AxInputField(fieldkey, field.getSchema()); - task.getInputFields().put(inputField.getKey().getLocalName(), inputField); - } - - for (final AxField field : outEvent0.getFields()) { - final AxReferenceKey fieldkey = new AxReferenceKey(task.getKey().getName(), task.getKey().getVersion(), - "outputFields", field.getKey().getLocalName()); - final AxOutputField outputField = new AxOutputField(fieldkey, field.getSchema()); - task.getOutputFields().put(outputField.getKey().getLocalName(), outputField); - } - - for (final AxField field : outEvent1.getFields()) { - final AxReferenceKey fieldkey = new AxReferenceKey(task.getKey().getName(), task.getKey().getVersion(), - "outputFields", field.getKey().getLocalName()); - final AxOutputField outputField = new AxOutputField(fieldkey, field.getSchema()); - task.getOutputFields().put(outputField.getKey().getLocalName(), outputField); - } - - final AxTaskParameter taskPar0 = new AxTaskParameter(new AxReferenceKey(task.getKey(), "taskParameter0"), - "Task parameter 0 value"); - final AxTaskParameter taskPar1 = new AxTaskParameter(new AxReferenceKey(task.getKey(), "taskParameter1"), - "Task parameter 1 value"); - - task.getTaskParameters().put(taskPar0.getKey().getLocalName(), taskPar0); - task.getTaskParameters().put(taskPar1.getKey().getLocalName(), taskPar1); - task.getContextAlbumReferences().add(album0.getKey()); - task.getContextAlbumReferences().add(album1.getKey()); - - final AxTaskLogic taskLogic = new AxTaskLogic(new AxReferenceKey(task.getKey(), "taskLogic"), "MVEL", - "Some task logic"); - task.setTaskLogic(taskLogic); - - final AxTasks tasks = new AxTasks(new AxArtifactKey("tasks", "0.0.1")); - tasks.getTaskMap().put(task.getKey(), task); - - final AxPolicy policy = new AxPolicy(new AxArtifactKey("policy", "0.0.1")); - policy.setTemplate("FREEFORM"); - - final AxState state = new AxState(new AxReferenceKey(policy.getKey(), "state")); - final AxTaskSelectionLogic taskSelectionLogic = new AxTaskSelectionLogic( - new AxReferenceKey(state.getKey(), "taskSelectionLogic"), "MVEL", "Some TS logic "); - - state.setTrigger(inEvent.getKey()); - state.getContextAlbumReferences().add(album0.getKey()); - state.getContextAlbumReferences().add(album1.getKey()); - state.setTaskSelectionLogic(taskSelectionLogic); - state.setDefaultTask(task.getKey()); - - final AxStateOutput stateOutput0 = new AxStateOutput(new AxReferenceKey(state.getKey(), "stateOutput0"), - outEvent0.getKey(), AxReferenceKey.getNullKey()); - state.getStateOutputs().put(stateOutput0.getKey().getLocalName(), stateOutput0); - - final AxStateTaskReference stateTaskReference = new AxStateTaskReference( - new AxReferenceKey(state.getKey(), task.getKey().getName()), AxStateTaskOutputType.DIRECT, - stateOutput0.getKey()); - - state.getTaskReferences().put(task.getKey(), stateTaskReference); - - policy.getStateMap().put(state.getKey().getLocalName(), state); - policy.setFirstState(state.getKey().getLocalName()); - - final AxPolicies policies = new AxPolicies(new AxArtifactKey("policies", "0.0.1")); - policies.getPolicyMap().put(policy.getKey(), policy); - - final AxKeyInformation keyInformation = new AxKeyInformation(new AxArtifactKey("KeyInfoMapKey", "0.0.1")); - final AxPolicyModel policyModel = new AxPolicyModel(new AxArtifactKey("PolicyModel", "0.0.1")); - policyModel.setKeyInformation(keyInformation); - policyModel.setSchemas(schemas); - policyModel.setAlbums(albums); - policyModel.setEvents(events); - policyModel.setTasks(tasks); - policyModel.setPolicies(policies); - policyModel.getKeyInformation().generateKeyInfo(policyModel); - - int uuidIncrementer = 0; - for (final AxKeyInfo keyInfo : policyModel.getKeyInformation().getKeyInfoMap().values()) { - final String uuidString = String.format("ce9168c-e6df-414f-9646-6da464b6e%03d", uuidIncrementer++); - keyInfo.setUuid(UUID.fromString(uuidString)); - } - - final AxValidationResult result = new AxValidationResult(); - policyModel.validate(result); - - return policyModel; - } - - /** - * Gets another policy model. - * - * @return the model - */ - public AxPolicyModel getAnotherModel() { - final AxContextSchema schema0 = new AxContextSchema(new AxArtifactKey("eventContextItemA0", "0.0.1"), "Java", - "java.lang.String"); - final AxContextSchema schema1 = new AxContextSchema(new AxArtifactKey("eventContextItemA1", "0.0.1"), "Java", - "java.lang.Long"); - final AxContextSchema schema2 = new AxContextSchema(new AxArtifactKey("StringTypeA", "0.0.1"), "Java", - "org.onap.policy.apex.model.policymodel.concepts.TestContextItem000"); - final AxContextSchema schema3 = new AxContextSchema(new AxArtifactKey("MapTypeA", "0.0.1"), "Java", - "org.onap.policy.apex.model.policymodel.concepts.TestContextItem00A"); - - final AxContextSchemas schemas = new AxContextSchemas(new AxArtifactKey("ContextSchemasA", "0.0.1")); - schemas.getSchemasMap().put(schema0.getKey(), schema0); - schemas.getSchemasMap().put(schema1.getKey(), schema1); - schemas.getSchemasMap().put(schema2.getKey(), schema2); - schemas.getSchemasMap().put(schema3.getKey(), schema3); - - final AxContextAlbum album0 = new AxContextAlbum(new AxArtifactKey("contextAlbumA0", "0.0.1"), "APPLICATION", - true, schema3.getKey()); - final AxContextAlbum album1 = new AxContextAlbum(new AxArtifactKey("contextAlbumA1", "0.0.1"), "GLOBAL", false, - schema2.getKey()); - - final AxContextAlbums albums = new AxContextAlbums(new AxArtifactKey("contextA", "0.0.1")); - albums.getAlbumsMap().put(album0.getKey(), album0); - albums.getAlbumsMap().put(album1.getKey(), album1); - - final AxEvent inEvent = new AxEvent(new AxArtifactKey("inEventA", "0.0.1"), - "org.onap.policy.apex.model.policymodel.events", "Source", "Target"); - inEvent.getParameterMap().put("IEPARA0", - new AxField(new AxReferenceKey(inEvent.getKey(), "IEPARA0"), schema0.getKey())); - inEvent.getParameterMap().put("IEPARA1", - new AxField(new AxReferenceKey(inEvent.getKey(), "IEPARA1"), schema1.getKey())); - - final AxEvent outEvent0 = new AxEvent(new AxArtifactKey("outEventA0", "0.0.1"), - "org.onap.policy.apex.model.policymodel.events", "Source", "Target"); - outEvent0.getParameterMap().put("OE0PARA0", - new AxField(new AxReferenceKey(outEvent0.getKey(), "OE0PARA0"), schema0.getKey())); - outEvent0.getParameterMap().put("OE0PARA1", - new AxField(new AxReferenceKey(outEvent0.getKey(), "OE0PARA1"), schema1.getKey())); - outEvent0.getParameterMap().put("OE1PARA0", - new AxField(new AxReferenceKey(outEvent0.getKey(), "OE1PARA0"), schema0.getKey())); - outEvent0.getParameterMap().put("OE1PARA1", - new AxField(new AxReferenceKey(outEvent0.getKey(), "OE1PARA1"), schema1.getKey())); - - final AxEvent outEvent1 = new AxEvent(new AxArtifactKey("outEventA1", "0.0.1"), - "org.onap.policy.apex.model.policymodel.events", "Source", "Target"); - outEvent1.getParameterMap().put("OE1PARA0", - new AxField(new AxReferenceKey(outEvent1.getKey(), "OE1PARA0"), schema0.getKey())); - outEvent1.getParameterMap().put("OE1PARA1", - new AxField(new AxReferenceKey(outEvent1.getKey(), "OE1PARA1"), schema1.getKey())); - - final AxEvents events = new AxEvents(new AxArtifactKey("eventsA", "0.0.1")); - events.getEventMap().put(inEvent.getKey(), inEvent); - events.getEventMap().put(outEvent0.getKey(), outEvent0); - events.getEventMap().put(outEvent1.getKey(), outEvent1); - - final AxTask task = new AxTask(new AxArtifactKey("taskA", "0.0.1")); - - for (final AxField field : inEvent.getFields()) { - final AxReferenceKey fieldkey = new AxReferenceKey(task.getKey().getName(), task.getKey().getVersion(), - "inputFieldsA", field.getKey().getLocalName()); - final AxInputField inputField = new AxInputField(fieldkey, field.getSchema()); - task.getInputFields().put(inputField.getKey().getLocalName(), inputField); - } - - for (final AxField field : outEvent0.getFields()) { - final AxReferenceKey fieldkey = new AxReferenceKey(task.getKey().getName(), task.getKey().getVersion(), - "outputFieldsA", field.getKey().getLocalName()); - final AxOutputField outputField = new AxOutputField(fieldkey, field.getSchema()); - task.getOutputFields().put(outputField.getKey().getLocalName(), outputField); - } - - for (final AxField field : outEvent1.getFields()) { - final AxReferenceKey fieldkey = new AxReferenceKey(task.getKey().getName(), task.getKey().getVersion(), - "outputFieldsA", field.getKey().getLocalName()); - final AxOutputField outputField = new AxOutputField(fieldkey, field.getSchema()); - task.getOutputFields().put(outputField.getKey().getLocalName(), outputField); - } - - final AxTaskParameter taskPar0 = new AxTaskParameter(new AxReferenceKey(task.getKey(), "taskParameterA0"), - "Task parameter 0 value"); - final AxTaskParameter taskPar1 = new AxTaskParameter(new AxReferenceKey(task.getKey(), "taskParameterA1"), - "Task parameter 1 value"); - - task.getTaskParameters().put(taskPar0.getKey().getLocalName(), taskPar0); - task.getTaskParameters().put(taskPar1.getKey().getLocalName(), taskPar1); - task.getContextAlbumReferences().add(album0.getKey()); - task.getContextAlbumReferences().add(album1.getKey()); - - final AxTaskLogic taskLogic = new AxTaskLogic(new AxReferenceKey(task.getKey(), "taskLogicA"), "MVEL", - "Some task logic"); - task.setTaskLogic(taskLogic); - - final AxTasks tasks = new AxTasks(new AxArtifactKey("tasksA", "0.0.1")); - tasks.getTaskMap().put(task.getKey(), task); - - final AxPolicy policy = new AxPolicy(new AxArtifactKey("policyA", "0.0.1")); - policy.setTemplate("FREEFORM"); - - final AxState state = new AxState(new AxReferenceKey(policy.getKey(), "stateA")); - final AxTaskSelectionLogic taskSelectionLogic = new AxTaskSelectionLogic( - new AxReferenceKey(state.getKey(), "taskSelectionLogicA"), "MVEL", "Some TS logic "); - - state.setTrigger(inEvent.getKey()); - state.getContextAlbumReferences().add(album0.getKey()); - state.getContextAlbumReferences().add(album1.getKey()); - state.setTaskSelectionLogic(taskSelectionLogic); - state.setDefaultTask(task.getKey()); - - final AxStateOutput stateOutput0 = new AxStateOutput(new AxReferenceKey(state.getKey(), "stateOutputA0"), - outEvent0.getKey(), AxReferenceKey.getNullKey()); - state.getStateOutputs().put(stateOutput0.getKey().getLocalName(), stateOutput0); - - final AxStateTaskReference stateTaskReference = new AxStateTaskReference( - new AxReferenceKey(state.getKey(), task.getKey().getName()), AxStateTaskOutputType.DIRECT, - stateOutput0.getKey()); - - state.getTaskReferences().put(task.getKey(), stateTaskReference); - - policy.getStateMap().put(state.getKey().getLocalName(), state); - policy.setFirstState(state.getKey().getLocalName()); - - final AxPolicies policies = new AxPolicies(new AxArtifactKey("policiesA", "0.0.1")); - policies.getPolicyMap().put(policy.getKey(), policy); - - final AxKeyInformation keyInformation = new AxKeyInformation(new AxArtifactKey("KeyInfoMapKeyA", "0.0.1")); - final AxPolicyModel policyModel = new AxPolicyModel(new AxArtifactKey("PolicyModelA", "0.0.1")); - policyModel.setKeyInformation(keyInformation); - policyModel.setSchemas(schemas); - policyModel.setAlbums(albums); - policyModel.setEvents(events); - policyModel.setTasks(tasks); - policyModel.setPolicies(policies); - policyModel.getKeyInformation().generateKeyInfo(policyModel); - - int uuidIncrementer = 0; - for (final AxKeyInfo keyInfo : policyModel.getKeyInformation().getKeyInfoMap().values()) { - final String uuidString = String.format("ce9168c-e6df-414f-9646-6da464b6e%03d", uuidIncrementer++); - keyInfo.setUuid(UUID.fromString(uuidString)); - } - - final AxValidationResult result = new AxValidationResult(); - policyModel.validate(result); - - return policyModel; - } - - @Override - public AxPolicyModel getMalstructuredModel() { - final AxPolicyModel policyModel = new AxPolicyModel(new AxArtifactKey("policyModel", "0.0.1")); - return policyModel; - } - - @Override - public AxPolicyModel getObservationModel() { - final AxPolicyModel policyModel = getModel(); - - final AxState state = policyModel.getPolicies().get("policy").getStateMap().get("state"); - final AxTask task = policyModel.getTasks().get("task"); - - final AxStateFinalizerLogic stateFinalizerLogic = new AxStateFinalizerLogic( - new AxReferenceKey(state.getKey(), "SFL"), "MVEL", "Some SF logic "); - state.getStateFinalizerLogicMap().put(stateFinalizerLogic.getKey().getLocalName(), stateFinalizerLogic); - final AxStateTaskReference stateTaskReference = new AxStateTaskReference( - new AxReferenceKey(state.getKey(), task.getKey().getName()), AxStateTaskOutputType.LOGIC, - stateFinalizerLogic.getKey()); - - state.getTaskReferences().put(task.getKey(), stateTaskReference); - - return policyModel; - } - - @Override - public AxPolicyModel getWarningModel() { - final AxPolicyModel policyModel = getModel(); - - final AxState anotherState = new AxState( - new AxReferenceKey(new AxArtifactKey("policy", "0.0.1"), "anotherState")); - - final AxEvent inEvent = policyModel.getEvents().getEventMap().get(new AxArtifactKey("inEvent", "0.0.1")); - final AxEvent outEvent0 = policyModel.getEvents().getEventMap().get(new AxArtifactKey("outEvent0", "0.0.1")); - - final AxTask anotherTask = new AxTask(new AxArtifactKey("anotherTask", "0.0.1")); - - for (final AxField field : inEvent.getFields()) { - final AxReferenceKey fieldkey = new AxReferenceKey(anotherTask.getKey().getName(), - anotherTask.getKey().getVersion(), "inputFields", field.getKey().getLocalName()); - final AxInputField inputField = new AxInputField(fieldkey, field.getSchema()); - anotherTask.getInputFields().put(inputField.getKey().getLocalName(), inputField); - } - - for (final AxField field : outEvent0.getFields()) { - final AxReferenceKey fieldkey = new AxReferenceKey(anotherTask.getKey().getName(), - anotherTask.getKey().getVersion(), "outputFields", field.getKey().getLocalName()); - final AxOutputField outputField = new AxOutputField(fieldkey, field.getSchema()); - anotherTask.getOutputFields().put(outputField.getKey().getLocalName(), outputField); - } - - final AxTaskParameter taskPar0 = new AxTaskParameter(new AxReferenceKey(anotherTask.getKey(), "taskParameter0"), - "Task parameter 0 value"); - final AxTaskParameter taskPar1 = new AxTaskParameter(new AxReferenceKey(anotherTask.getKey(), "taskParameter1"), - "Task parameter 1 value"); - - anotherTask.getTaskParameters().put(taskPar0.getKey().getLocalName(), taskPar0); - anotherTask.getTaskParameters().put(taskPar1.getKey().getLocalName(), taskPar1); - - final AxTaskLogic taskLogic = new AxTaskLogic(new AxReferenceKey(anotherTask.getKey(), "taskLogic"), "MVEL", - "Some task logic"); - anotherTask.setTaskLogic(taskLogic); - policyModel.getTasks().getTaskMap().put(anotherTask.getKey(), anotherTask); - - final AxStateOutput anotherStateOutput0 = new AxStateOutput( - new AxReferenceKey(anotherState.getKey(), "stateOutput0"), outEvent0.getKey(), - AxReferenceKey.getNullKey()); - anotherState.setTrigger(inEvent.getKey()); - anotherState.getStateOutputs().put(anotherStateOutput0.getKey().getLocalName(), anotherStateOutput0); - anotherState.setDefaultTask(anotherTask.getKey()); - final AxStateTaskReference anotherStateTaskReference = new AxStateTaskReference( - new AxReferenceKey(anotherState.getKey(), anotherTask.getKey().getName()), - AxStateTaskOutputType.DIRECT, anotherStateOutput0.getKey()); - anotherState.getTaskReferences().put(anotherTask.getKey(), anotherStateTaskReference); - - policyModel.getPolicies().getPolicyMap().get(new AxArtifactKey("policy", "0.0.1")).getStateMap() - .put(anotherState.getKey().getLocalName(), anotherState); - - policyModel.getKeyInformation().generateKeyInfo(policyModel); - - return policyModel; - } - - @Override - public AxPolicyModel getInvalidModel() { - final AxPolicyModel policyModel = getModel(); - - policyModel.getAlbums().get(new AxArtifactKey("contextAlbum0", "0.0.1")).setScope("UNDEFINED"); - policyModel.getAlbums().get(new AxArtifactKey("contextAlbum1", "0.0.1")).setScope("UNDEFINED"); - - final AxEvent outEvent0 = policyModel.getEvents().get("outEvent0"); - outEvent0.getParameterMap().remove("OE1PAR0"); - outEvent0.getParameterMap().remove("OE1PAR1"); - - return policyModel; - } -} diff --git a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/TestPolicyAnalyser.java b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/TestPolicyAnalyser.java deleted file mode 100644 index 40aa51ffe..000000000 --- a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/TestPolicyAnalyser.java +++ /dev/null @@ -1,60 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-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.apex.model.policymodel.handling; - -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -import org.junit.Test; -import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; -import org.onap.policy.apex.model.policymodel.handling.PolicyAnalyser; -import org.onap.policy.apex.model.policymodel.handling.PolicyAnalysisResult; - -public class TestPolicyAnalyser { - @Test - public void test() { - final AxPolicyModel apexModel = new TestApexPolicyModelCreator().getModel(); - - final PolicyAnalyser policyAnalyser = new PolicyAnalyser(); - final PolicyAnalysisResult analysisResult = policyAnalyser.analyse(apexModel); - - assertTrue(analysisResult.toString().equals(EXPECTED_ANALYSIS_RESULT)); - - assertNotNull(analysisResult.getUsedContextAlbums()); - assertNotNull(analysisResult.getUsedContextSchemas()); - assertNotNull(analysisResult.getUsedEvents()); - assertNotNull(analysisResult.getUsedTasks()); - assertNotNull(analysisResult.getUnusedContextAlbums()); - assertNotNull(analysisResult.getUnusedContextSchemas()); - assertNotNull(analysisResult.getUnusedEvents()); - assertNotNull(analysisResult.getUnusedTasks()); - } - - private static final String EXPECTED_ANALYSIS_RESULT = "" + "Context Schema usage\n" + " MapType:0.0.1\n" - + " contextAlbum0:0.0.1\n" + " StringType:0.0.1\n" + " contextAlbum1:0.0.1\n" - + " eventContextItem0:0.0.1\n" + " inEvent:0.0.1\n" + " outEvent0:0.0.1\n" + " outEvent1:0.0.1\n" - + " task:0.0.1\n" + " eventContextItem1:0.0.1\n" + " inEvent:0.0.1\n" + " outEvent0:0.0.1\n" - + " outEvent1:0.0.1\n" + " task:0.0.1\n" + "Context Album usage\n" + " contextAlbum0:0.0.1\n" - + " task:0.0.1\n" + " policy:0.0.1:NULL:state\n" + " contextAlbum1:0.0.1\n" + " task:0.0.1\n" - + " policy:0.0.1:NULL:state\n" + "Event usage\n" + " inEvent:0.0.1\n" + " policy:0.0.1:NULL:state\n" - + " outEvent0:0.0.1\n" + " policy:0.0.1:NULL:state\n" + " outEvent1:0.0.1 (unused)\n" + "Task usage\n" - + " task:0.0.1\n" + " policy:0.0.1:NULL:state\n"; -} diff --git a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/TestPolicyLogicReader.java b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/TestPolicyLogicReader.java deleted file mode 100644 index 528898d30..000000000 --- a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/TestPolicyLogicReader.java +++ /dev/null @@ -1,114 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-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.apex.model.policymodel.handling; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - -import org.junit.Test; -import org.onap.policy.apex.model.basicmodel.concepts.AxKey; -import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey; -import org.onap.policy.apex.model.policymodel.concepts.AxLogic; -import org.onap.policy.apex.model.policymodel.handling.PolicyLogicReader; - -/** - * Logic reader for policy tests. - * - * @author Liam Fallon (liam.fallon@ericsson.com) - */ -public class TestPolicyLogicReader { - - @Test - public void test() { - final AxReferenceKey logicKey = new AxReferenceKey("LogicParent", "0.0.1", "LogicInstanceName"); - - final PolicyLogicReader plReader = new PolicyLogicReader(); - - plReader.setLogicPackage("somewhere.over.the.rainbow"); - assertEquals("somewhere.over.the.rainbow", plReader.getLogicPackage()); - - plReader.setDefaultLogic("FunkyDefaultLogic"); - assertEquals("FunkyDefaultLogic", plReader.getDefaultLogic()); - - try { - new AxLogic(logicKey, "FunkyLogic", plReader); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("logic not found for logic " - + "\"somewhere/over/the/rainbow/funkylogic/FunkyDefaultLogic.funkylogic\"", e.getMessage()); - } - - plReader.setDefaultLogic(null); - try { - new AxLogic(logicKey, "FunkyLogic", plReader); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("logic not found for logic " - + "\"somewhere/over/the/rainbow/funkylogic/LogicParentLogicInstanceName.funkylogic\"", - e.getMessage()); - } - - logicKey.setParentLocalName("LogicParentLocalName"); - try { - new AxLogic(logicKey, "FunkyLogic", plReader); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("logic not found for logic " + "\"somewhere/over/the/rainbow/funkylogic/" - + "LogicParentLogicParentLocalNameLogicInstanceName.funkylogic\"", e.getMessage()); - } - - plReader.setLogicPackage("path.to.apex.logic"); - try { - final AxLogic logic = new AxLogic(logicKey, "FunkyLogic", plReader); - assertTrue(logic.getLogic().endsWith("Way out man, this is funky logic!")); - } catch (final Exception e) { - fail("test should not throw an exception"); - } - - plReader.setLogicPackage("somewhere.over.the.rainbow"); - plReader.setDefaultLogic("JavaLogic"); - - try { - final AxLogic logic = new AxLogic(logicKey, "JAVA", plReader); - assertEquals("somewhere.over.the.rainbow.java.JavaLogic", logic.getLogic()); - } catch (final Exception e) { - fail("test should not throw an exception"); - } - - plReader.setDefaultLogic(null); - try { - final AxLogic logic = new AxLogic(logicKey, "JAVA", plReader); - assertEquals("somewhere.over.the.rainbow.java.LogicParentLogicParentLocalNameLogicInstanceName", - logic.getLogic()); - } catch (final Exception e) { - fail("test should not throw an exception"); - } - - logicKey.setParentLocalName(AxKey.NULL_KEY_NAME); - try { - final AxLogic logic = new AxLogic(logicKey, "JAVA", plReader); - assertEquals("somewhere.over.the.rainbow.java.LogicParentLogicInstanceName", logic.getLogic()); - } catch (final Exception e) { - fail("test should not throw an exception"); - } - } -} diff --git a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/TestPolicyModelComparer.java b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/TestPolicyModelComparer.java deleted file mode 100644 index 3511b54ba..000000000 --- a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/TestPolicyModelComparer.java +++ /dev/null @@ -1,129 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-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.apex.model.policymodel.handling; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -import java.io.IOException; -import java.util.UUID; - -import org.junit.Test; -import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; -import org.onap.policy.apex.model.basicmodel.concepts.AxKeyInfo; -import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; -import org.onap.policy.apex.model.policymodel.handling.PolicyComparer; -import org.onap.policy.apex.model.policymodel.handling.PolicyModelComparer; -import org.onap.policy.apex.model.utilities.TextFileUtils; - -public class TestPolicyModelComparer { - - @Test - public void testPolicyComparer() throws IOException { - final AxPolicyModel leftApexModel = new TestApexPolicyModelCreator().getModel(); - final AxPolicyModel rightApexModel = new AxPolicyModel(leftApexModel); - - PolicyModelComparer policyModelComparer = new PolicyModelComparer(leftApexModel, rightApexModel); - - String resultString = policyModelComparer.asString(false, false); - String checkString = TextFileUtils - .getTextFileAsString("src/test/resources/checkFiles/PolicyModelComparisonIdenticalVerboseValues.txt"); - assertEquals(resultString.trim().replaceAll("[\\r?\\n]+", " "), - checkString.trim().replaceAll("[\\r?\\n]+", " ")); - - resultString = policyModelComparer.asString(false, true); - checkString = TextFileUtils - .getTextFileAsString("src/test/resources/checkFiles/PolicyModelComparisonIdenticalVerboseKeys.txt"); - assertTrue(resultString.trim().replaceAll("[\\r?\\n]+", " ") - .equals(checkString.trim().replaceAll("[\\r?\\n]+", " "))); - - resultString = policyModelComparer.asString(true, false); - checkString = TextFileUtils - .getTextFileAsString("src/test/resources/checkFiles/PolicyModelComparisonIdenticalTerse.txt"); - assertTrue(resultString.trim().replaceAll("[\\r?\\n]+", " ") - .equals(checkString.trim().replaceAll("[\\r?\\n]+", " "))); - - resultString = policyModelComparer.asString(true, true); - checkString = TextFileUtils - .getTextFileAsString("src/test/resources/checkFiles/PolicyModelComparisonIdenticalTerse.txt"); - assertTrue(resultString.trim().replaceAll("[\\r?\\n]+", " ") - .equals(checkString.trim().replaceAll("[\\r?\\n]+", " "))); - - final AxKeyInfo leftOnlyKeyInfo = new AxKeyInfo(new AxArtifactKey("LeftOnlyKeyInfo", "0.0.1"), - UUID.fromString("ce9168c-e6df-414f-9646-6da464b6f000"), "Left only key info"); - final AxKeyInfo rightOnlyKeyInfo = new AxKeyInfo(new AxArtifactKey("RightOnlyKeyInfo", "0.0.1"), - UUID.fromString("ce9168c-e6df-414f-9646-6da464b6f001"), "Right only key info"); - - leftApexModel.getKeyInformation().getKeyInfoMap().put(leftOnlyKeyInfo.getKey(), leftOnlyKeyInfo); - rightApexModel.getKeyInformation().getKeyInfoMap().put(rightOnlyKeyInfo.getKey(), rightOnlyKeyInfo); - - leftApexModel.getKeyInformation().getKeyInfoMap().get(new AxArtifactKey("inEvent", "0.0.1")) - .setDescription("Left InEvent Description"); - rightApexModel.getKeyInformation().getKeyInfoMap().get(new AxArtifactKey("inEvent", "0.0.1")) - .setDescription("Right InEvent Description"); - - policyModelComparer = new PolicyModelComparer(leftApexModel, rightApexModel); - - resultString = policyModelComparer.asString(false, false); - checkString = TextFileUtils - .getTextFileAsString("src/test/resources/checkFiles/PolicyModelComparisonDifferentVerboseValues.txt"); - assertEquals(resultString.trim().replaceAll("[\\r?\\n]+", " "), - checkString.trim().replaceAll("[\\r?\\n]+", " ")); - - resultString = policyModelComparer.asString(false, true); - checkString = TextFileUtils - .getTextFileAsString("src/test/resources/checkFiles/PolicyModelComparisonDifferentVerboseKeys.txt"); - assertTrue(resultString.trim().replaceAll("[\\r?\\n]+", " ") - .equals(checkString.trim().replaceAll("[\\r?\\n]+", " "))); - - resultString = policyModelComparer.asString(true, false); - checkString = TextFileUtils - .getTextFileAsString("src/test/resources/checkFiles/PolicyModelComparisonDifferentTerseValues.txt"); - assertTrue(resultString.trim().replaceAll("[\\r?\\n]+", " ") - .equals(checkString.trim().replaceAll("[\\r?\\n]+", " "))); - - resultString = policyModelComparer.asString(true, true); - checkString = TextFileUtils - .getTextFileAsString("src/test/resources/checkFiles/PolicyModelComparisonDifferentTerseKeys.txt"); - assertTrue(resultString.trim().replaceAll("[\\r?\\n]+", " ") - .equals(checkString.trim().replaceAll("[\\r?\\n]+", " "))); - - assertNotNull(policyModelComparer.getContextAlbumComparisonResult()); - assertNotNull(policyModelComparer.getContextAlbumKeyDifference()); - assertNotNull(policyModelComparer.getContextSchemaComparisonResult()); - assertNotNull(policyModelComparer.getContextSchemaKeyDifference()); - assertNotNull(policyModelComparer.getEventComparisonResult()); - assertNotNull(policyModelComparer.getEventKeyDifference()); - assertNotNull(policyModelComparer.getKeyInfoComparisonResult()); - assertNotNull(policyModelComparer.getKeyInformationKeyDifference()); - assertNotNull(policyModelComparer.getPolicyComparisonResult()); - assertNotNull(policyModelComparer.getPolicykeyDifference()); - assertNotNull(policyModelComparer.getPolicyModelsKeyDifference()); - assertNotNull(policyModelComparer.getTaskComparisonResult()); - assertNotNull(policyModelComparer.getTaskKeyDifference()); - - assertNotNull(new PolicyComparer().compare(leftApexModel.getPolicies(), rightApexModel.getPolicies())); - - assertEquals("****** policy map differences ******\n*** context s", - policyModelComparer.toString().substring(0, 50)); - } -} diff --git a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/TestPolicyModelMerger.java b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/TestPolicyModelMerger.java deleted file mode 100644 index 8f717a234..000000000 --- a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/TestPolicyModelMerger.java +++ /dev/null @@ -1,111 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-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.apex.model.policymodel.handling; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.fail; - -import org.junit.Test; -import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; -import org.onap.policy.apex.model.basicmodel.handling.ApexModelException; -import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; -import org.onap.policy.apex.model.policymodel.handling.PolicyModelMerger; - -/** - * Test model merging. - * - * @author Liam Fallon (liam.fallon@ericsson.com) - */ -public class TestPolicyModelMerger { - - @Test - public void testPolicyModelMerger() { - final AxPolicyModel leftPolicyModel = new TestApexPolicyModelCreator().getModel(); - AxPolicyModel rightPolicyModel = new TestApexPolicyModelCreator().getModel(); - - try { - final AxPolicyModel mergedPolicyModel = PolicyModelMerger.getMergedPolicyModel(leftPolicyModel, - rightPolicyModel, false); - assertEquals(leftPolicyModel, mergedPolicyModel); - assertEquals(rightPolicyModel, mergedPolicyModel); - } catch (final ApexModelException e) { - fail("test should not throw an exception"); - } - - leftPolicyModel.setKey(new AxArtifactKey("LeftPolicyModel", "0.0.1")); - try { - PolicyModelMerger.getMergedPolicyModel(leftPolicyModel, rightPolicyModel, false); - fail("test should throw an exception here"); - } catch (final ApexModelException e) { - assertEquals("left model is invalid: \n***validation of model fai", e.getMessage().substring(0, 50)); - } - - leftPolicyModel.setKey(new AxArtifactKey("LeftPolicyModel", "0.0.1")); - try { - assertNotNull(PolicyModelMerger.getMergedPolicyModel(leftPolicyModel, rightPolicyModel, false, true)); - } catch (final ApexModelException e) { - fail("test should not throw an exception"); - } - - leftPolicyModel.getKeyInformation().generateKeyInfo(leftPolicyModel); - try { - final AxPolicyModel mergedPolicyModel = PolicyModelMerger.getMergedPolicyModel(leftPolicyModel, - rightPolicyModel, true); - assertNotNull(mergedPolicyModel); - } catch (final ApexModelException e) { - fail("test should not throw an exception"); - } - - rightPolicyModel.setKey(new AxArtifactKey("RightPolicyModel", "0.0.1")); - try { - PolicyModelMerger.getMergedPolicyModel(leftPolicyModel, rightPolicyModel, false); - fail("test should throw an exception here"); - } catch (final ApexModelException e) { - assertEquals("right model is invalid: \n***validation of model fa", e.getMessage().substring(0, 50)); - } - - rightPolicyModel.setKey(new AxArtifactKey("RightPolicyModel", "0.0.1")); - try { - assertNotNull(PolicyModelMerger.getMergedPolicyModel(leftPolicyModel, rightPolicyModel, false, true)); - } catch (final ApexModelException e) { - fail("test should not throw an exception"); - } - - rightPolicyModel.getKeyInformation().generateKeyInfo(rightPolicyModel); - try { - final AxPolicyModel mergedPolicyModel = PolicyModelMerger.getMergedPolicyModel(leftPolicyModel, - rightPolicyModel, false); - assertNotNull(mergedPolicyModel); - } catch (final ApexModelException e) { - fail("test should not throw an exception"); - } - - rightPolicyModel = new TestApexPolicyModelCreator().getAnotherModel(); - try { - final AxPolicyModel mergedPolicyModel = PolicyModelMerger.getMergedPolicyModel(leftPolicyModel, - rightPolicyModel, true); - assertNotNull(mergedPolicyModel); - } catch (final ApexModelException e) { - fail("test should not throw an exception"); - } - } -} diff --git a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/TestPolicyModelSplitter.java b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/TestPolicyModelSplitter.java deleted file mode 100644 index fde6e3712..000000000 --- a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/TestPolicyModelSplitter.java +++ /dev/null @@ -1,95 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-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.apex.model.policymodel.handling; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - -import java.util.Set; -import java.util.TreeSet; - -import org.junit.Test; -import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; -import org.onap.policy.apex.model.basicmodel.handling.ApexModelException; -import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; -import org.onap.policy.apex.model.policymodel.handling.PolicyModelSplitter; - -public class TestPolicyModelSplitter { - @Test - public void test() { - final AxPolicyModel apexModel = new TestApexPolicyModelCreator().getModel(); - - final Set requiredPolicySet = new TreeSet(); - requiredPolicySet.add(new AxArtifactKey("policy", "0.0.1")); - - // There's only one policy so a split of this model on that policy should return the same - // model - AxPolicyModel splitApexModel = null; - try { - splitApexModel = PolicyModelSplitter.getSubPolicyModel(apexModel, requiredPolicySet); - } catch (final ApexModelException e) { - fail(e.getMessage()); - } - - // The only difference between the models should be that the unused event outEvent1 should - // not be in the split model - apexModel.getEvents().getEventMap().remove(new AxArtifactKey("outEvent1", "0.0.1")); - apexModel.getKeyInformation().getKeyInfoMap().remove(new AxArtifactKey("outEvent1", "0.0.1")); - assertTrue(apexModel.equals(splitApexModel)); - - final Set requiredMissingPolicySet = new TreeSet(); - requiredPolicySet.add(new AxArtifactKey("MissingPolicy", "0.0.1")); - - AxPolicyModel missingSplitApexModel = null; - try { - missingSplitApexModel = PolicyModelSplitter.getSubPolicyModel(apexModel, requiredMissingPolicySet); - } catch (final ApexModelException e) { - fail(e.getMessage()); - } - assertNotNull(missingSplitApexModel); - - splitApexModel = null; - try { - splitApexModel = PolicyModelSplitter.getSubPolicyModel(apexModel, requiredPolicySet, true); - } catch (final ApexModelException e) { - fail(e.getMessage()); - } - - // The only difference between the models should be that the unused event outEvent1 should - // not be in the split model - apexModel.getEvents().getEventMap().remove(new AxArtifactKey("outEvent1", "0.0.1")); - apexModel.getKeyInformation().getKeyInfoMap().remove(new AxArtifactKey("outEvent1", "0.0.1")); - assertTrue(apexModel.equals(splitApexModel)); - - // There's only one policy so a split of this model on that policy should return the same - // model - try { - apexModel.getKey().setName("InvalidPolicyModelName"); - PolicyModelSplitter.getSubPolicyModel(apexModel, requiredPolicySet); - fail("test should throw an exception here"); - } catch (final Exception e) { - assertEquals("source model is invalid: \n***validation of model f", e.getMessage().substring(0, 50)); - } - - } -} -- cgit 1.2.3-korg