diff options
author | waqas.ikram <waqas.ikram@ericsson.com> | 2018-05-28 10:58:07 +0100 |
---|---|---|
committer | waqas.ikram <waqas.ikram@ericsson.com> | 2018-05-28 14:35:30 +0100 |
commit | 55d93a12cc5575c872724f48585304b5eec77fea (patch) | |
tree | ac5e44c5670bbcc5e68cceb1694f87348c371e1b /model/policy-model/src/test/java | |
parent | 6029d25f5f3ad43fe02ffe1a4beb1eda0a6ae5e3 (diff) |
Adding policy-model, model-api & engine-model
Change-Id: I56702b8f0953457d493f894d155b2a6ddc87b10c
Issue-ID: POLICY-856
Signed-off-by: waqas.ikram <waqas.ikram@ericsson.com>
Diffstat (limited to 'model/policy-model/src/test/java')
18 files changed, 3462 insertions, 0 deletions
diff --git a/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/DummyLogicReader.java b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/DummyLogicReader.java new file mode 100644 index 000000000..28f70f48b --- /dev/null +++ b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/DummyLogicReader.java @@ -0,0 +1,84 @@ +/*- + * ============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 org.onap.policy.apex.model.policymodel.concepts.AxLogic; +import org.onap.policy.apex.model.policymodel.concepts.AxLogicReader; + +/** + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class DummyLogicReader implements AxLogicReader { + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.model.policymodel.concepts.AxLogicReader#getLogicPackage() + */ + @Override + public String getLogicPackage() { + return null; + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.model.policymodel.concepts.AxLogicReader#setLogicPackage(java.lang. + * String) + */ + @Override + public AxLogicReader setLogicPackage(final String logicPackage) { + return null; + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.model.policymodel.concepts.AxLogicReader#getDefaultLogic() + */ + @Override + public String getDefaultLogic() { + return null; + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.model.policymodel.concepts.AxLogicReader#setDefaultLogic(java.lang. + * String) + */ + @Override + public AxLogicReader setDefaultLogic(final String defaultLogic) { + return null; + } + + /* + * (non-Javadoc) + * + * @see + * org.onap.policy.apex.model.policymodel.concepts.AxLogicReader#readLogic(org.onap.policy.apex. + * model.policymodel.concepts.AxLogic) + */ + @Override + public String readLogic(final AxLogic axLogic) { + return "Dummy Logic"; + } +} 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 new file mode 100644 index 000000000..9c7405b97 --- /dev/null +++ b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/TestExceptions.java @@ -0,0 +1,53 @@ +/*- + * ============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; + +/** + * @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 new file mode 100644 index 000000000..a415a1c69 --- /dev/null +++ b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/TestLogic.java @@ -0,0 +1,181 @@ +/*- + * ============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; + +/** + * @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 new file mode 100644 index 000000000..9da944b60 --- /dev/null +++ b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/TestPolicies.java @@ -0,0 +1,433 @@ +/*- + * ============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; + +/** + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class TestPolicies { + + @Test + public void testPolicies() { + final TreeMap<String, AxState> stateMap = new TreeMap<>(); + final TreeMap<String, AxState> 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 badSO = new AxStateOutput(badState.getKey(), AxArtifactKey.getNullKey(), + new AxReferenceKey(policyKey, "BadNextState")); + badState.getStateOutputs().put(badSO.getKey().getLocalName(), badSO); + 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<String, AxState> 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 new file mode 100644 index 000000000..db1d1ee62 --- /dev/null +++ b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/TestPolicyModel.java @@ -0,0 +1,357 @@ +/*- + * ============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; + +/** + * @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 savedTSL = + 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(savedTSL); + 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 badTR = 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"), badTR); + result = new AxValidationResult(); + result = model.validate(result); + assertEquals(ValidationResult.INVALID, result.getValidationResult()); + + badTR.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 savedSOName = tr.getOutput().getLocalName(); + tr.getOutput().setLocalName("NonExistantOutput"); + result = new AxValidationResult(); + result = model.validate(result); + assertEquals(ValidationResult.INVALID, result.getValidationResult()); + + tr.getOutput().setLocalName(savedSOName); + result = new AxValidationResult(); + result = model.validate(result); + assertEquals(ValidationResult.VALID, result.getValidationResult()); + + final AxStateOutput so = + model.getPolicies().get("policy").getStateMap().get("state").getStateOutputs().get(savedSOName); + + final AxArtifactKey savedOE = so.getOutgingEvent(); + so.setOutgoingEvent(new AxArtifactKey("NonExistantEvent", "0.0.1")); + result = new AxValidationResult(); + result = model.validate(result); + assertEquals(ValidationResult.INVALID, result.getValidationResult()); + + so.setOutgoingEvent(savedOE); + 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 new file mode 100644 index 000000000..1d62ae63a --- /dev/null +++ b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/TestState.java @@ -0,0 +1,475 @@ +/*- + * ============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; +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.AxTaskSelectionLogic; + +/** + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class TestState { + + @Test + public void testState() { + final TreeMap<String, AxStateOutput> soEmptyMap = new TreeMap<>(); + final TreeSet<AxArtifactKey> ctxtEmptySet = new TreeSet<>(); + final TreeMap<String, AxStateFinalizerLogic> sflEmptyMap = new TreeMap<>(); + final TreeMap<AxArtifactKey, AxStateTaskReference> trEmptyMap = new TreeMap<>(); + + final TreeMap<String, AxStateOutput> soMap = new TreeMap<>(); + final TreeSet<AxArtifactKey> ctxtSet = new TreeSet<>(); + final TreeMap<String, AxStateFinalizerLogic> sflMap = new TreeMap<>(); + final TreeMap<AxArtifactKey, AxStateTaskReference> trMap = new TreeMap<>(); + + assertNotNull(new AxState()); + assertNotNull(new AxState(new AxReferenceKey())); + assertNotNull(new AxState(new AxReferenceKey(), new AxArtifactKey(), soEmptyMap, ctxtEmptySet, + new AxTaskSelectionLogic(), sflEmptyMap, new AxArtifactKey(), 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 strBadSO = new AxStateTaskReference(new AxReferenceKey(stateKey, "STR2"), + AxStateTaskOutputType.UNDEFINED, sfl.getKey()); + final AxStateTaskReference strBadSFL = 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<String> 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, strBadSO); + 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, strBadSFL); + 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("Hello")); + assertFalse(state + .equals(new AxState(new AxReferenceKey(), triggerKey, soMap, ctxtSet, tsl, sflMap, defTaskKey, trMap))); + assertFalse(state + .equals(new AxState(stateKey, new AxArtifactKey(), soMap, ctxtSet, tsl, sflMap, defTaskKey, trMap))); + assertFalse( + state.equals(new AxState(stateKey, triggerKey, soEmptyMap, ctxtSet, tsl, sflMap, defTaskKey, trMap))); + assertFalse( + state.equals(new AxState(stateKey, triggerKey, soMap, ctxtEmptySet, tsl, sflMap, defTaskKey, trMap))); + assertFalse(state.equals(new AxState(stateKey, triggerKey, soMap, ctxtSet, new AxTaskSelectionLogic(), sflMap, + defTaskKey, trMap))); + assertFalse( + state.equals(new AxState(stateKey, triggerKey, soMap, ctxtSet, tsl, sflEmptyMap, defTaskKey, trMap))); + assertFalse(state + .equals(new AxState(stateKey, triggerKey, soMap, ctxtSet, tsl, sflMap, new AxArtifactKey(), trMap))); + assertFalse( + state.equals(new AxState(stateKey, triggerKey, soMap, ctxtSet, tsl, sflMap, defTaskKey, trEmptyMap))); + assertTrue(state.equals(new AxState(stateKey, triggerKey, soMap, ctxtSet, tsl, sflMap, defTaskKey, 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 AxReferenceKey(), triggerKey, soMap, ctxtSet, tsl, sflMap, defTaskKey, trMap))); + assertNotEquals(0, state + .compareTo(new AxState(stateKey, new AxArtifactKey(), soMap, ctxtSet, tsl, sflMap, defTaskKey, trMap))); + assertNotEquals(0, state + .compareTo(new AxState(stateKey, triggerKey, soEmptyMap, ctxtSet, tsl, sflMap, defTaskKey, trMap))); + assertNotEquals(0, state + .compareTo(new AxState(stateKey, triggerKey, soMap, ctxtEmptySet, tsl, sflMap, defTaskKey, trMap))); + assertNotEquals(0, state.compareTo(new AxState(stateKey, triggerKey, soMap, ctxtSet, new AxTaskSelectionLogic(), + sflMap, defTaskKey, trMap))); + assertNotEquals(0, state + .compareTo(new AxState(stateKey, triggerKey, soMap, ctxtSet, tsl, sflEmptyMap, defTaskKey, trMap))); + assertNotEquals(0, state + .compareTo(new AxState(stateKey, triggerKey, soMap, ctxtSet, tsl, sflMap, new AxArtifactKey(), trMap))); + assertNotEquals(0, state + .compareTo(new AxState(stateKey, triggerKey, soMap, ctxtSet, tsl, sflMap, defTaskKey, trEmptyMap))); + assertEquals(0, + state.compareTo(new AxState(stateKey, triggerKey, soMap, ctxtSet, tsl, sflMap, defTaskKey, 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 new file mode 100644 index 000000000..5911d41be --- /dev/null +++ b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/TestStateOutput.java @@ -0,0 +1,137 @@ +/*- + * ============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; + +/** + * @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 new file mode 100644 index 000000000..c85fda4be --- /dev/null +++ b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/TestStateTaskReference.java @@ -0,0 +1,160 @@ +/*- + * ============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; + +/** + * @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"); + AxReferenceKey soKey = new AxReferenceKey("StateParent", "0.0.1", "SOState", "STRef0"); + + 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()); + } + + 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 new file mode 100644 index 000000000..63906add2 --- /dev/null +++ b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/TestTaskParameter.java @@ -0,0 +1,116 @@ +/*- + * ============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; + +/** + * @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 new file mode 100644 index 000000000..967e4e0aa --- /dev/null +++ b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/concepts/TestTasks.java @@ -0,0 +1,331 @@ +/*- + * ============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; + +/** + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class TestTasks { + + @Test + public void testTasks() { + final TreeMap<String, AxInputField> ifEmptyMap = new TreeMap<>(); + final TreeMap<String, AxOutputField> ofEmptyMap = new TreeMap<>(); + final TreeMap<String, AxTaskParameter> tpEmptyMap = new TreeMap<>(); + final TreeSet<AxArtifactKey> ctxtEmptySet = new TreeSet<>(); + + final TreeMap<String, AxInputField> ifMap = new TreeMap<>(); + final TreeMap<String, AxOutputField> ofMap = new TreeMap<>(); + final TreeMap<String, AxTaskParameter> tpMap = new TreeMap<>(); + final TreeSet<AxArtifactKey> 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<String, AxField> ifDupMap = new TreeMap<>(); + final TreeMap<String, AxField> 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/TestApexPolicyModel.java b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/TestApexPolicyModel.java new file mode 100644 index 000000000..b3fd4a1b0 --- /dev/null +++ b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/TestApexPolicyModel.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.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<AxPolicyModel> testApexModel; + + @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>(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 new file mode 100644 index 000000000..cf2bb6524 --- /dev/null +++ b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/TestApexPolicyModelCreator.java @@ -0,0 +1,448 @@ +/*- + * ============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; + +/** + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class TestApexPolicyModelCreator implements TestApexModelCreator<AxPolicyModel> { + + @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; + } + + 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 new file mode 100644 index 000000000..40aa51ffe --- /dev/null +++ b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/TestPolicyAnalyser.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 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 new file mode 100644 index 000000000..446cbeed8 --- /dev/null +++ b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/TestPolicyLogicReader.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; + +/** + * @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/LogicParent_LogicInstanceName.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/LogicParent_LogicParentLocalName_LogicInstanceName.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.LogicParent_LogicParentLocalName_LogicInstanceName", + 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.LogicParent_LogicInstanceName", 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 new file mode 100644 index 000000000..3511b54ba --- /dev/null +++ b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/TestPolicyModelComparer.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 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 new file mode 100644 index 000000000..92d1dc32a --- /dev/null +++ b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/TestPolicyModelMerger.java @@ -0,0 +1,109 @@ +/*- + * ============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; + +/** + * @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 new file mode 100644 index 000000000..fde6e3712 --- /dev/null +++ b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/handling/TestPolicyModelSplitter.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 TestPolicyModelSplitter { + @Test + public void test() { + final AxPolicyModel apexModel = new TestApexPolicyModelCreator().getModel(); + + final Set<AxArtifactKey> requiredPolicySet = new TreeSet<AxArtifactKey>(); + 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<AxArtifactKey> requiredMissingPolicySet = new TreeSet<AxArtifactKey>(); + 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/utils/PolicyModelValidator.java b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/utils/PolicyModelValidator.java new file mode 100644 index 000000000..4610a827e --- /dev/null +++ b/model/policy-model/src/test/java/org/onap/policy/apex/model/policymodel/utils/PolicyModelValidator.java @@ -0,0 +1,41 @@ +/*- + * ============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.utils; + +import java.io.FileInputStream; +import java.io.FileNotFoundException; + +import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult; +import org.onap.policy.apex.model.basicmodel.handling.ApexModelException; +import org.onap.policy.apex.model.basicmodel.handling.ApexModelReader; +import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; + +public class PolicyModelValidator { + public static void main(final String[] args) throws ApexModelException, FileNotFoundException { + final ApexModelReader<AxPolicyModel> policyModelReader = + new ApexModelReader<AxPolicyModel>(AxPolicyModel.class); + + + final AxPolicyModel policyModel = policyModelReader.read(new FileInputStream(args[0])); + final AxValidationResult result = policyModel.validate(new AxValidationResult()); + System.out.println(result); + } +} |