diff options
author | liamfallon <liam.fallon@ericsson.com> | 2018-10-03 15:22:53 +0100 |
---|---|---|
committer | liamfallon <liam.fallon@ericsson.com> | 2018-10-03 15:23:07 +0100 |
commit | 16a8f59fed2e53b1bbedaf65a33165bb4d3225f8 (patch) | |
tree | 1b6088a4e93ba0ab7fee719c43e57add32afb358 /core/core-engine/src | |
parent | b4a5e9eaeed3b76db48e01875013d694014814bb (diff) |
Add unit test for core events and context
This code is covered by feature testing but does not
show up in Sonar. These unit tests give coverage on engine
modules.
Issue-ID: POLICY-1034
Change-Id: Ib7a288bcb9729ec88346cc8047e38f3a58f9a97c
Signed-off-by: liamfallon <liam.fallon@ericsson.com>
Diffstat (limited to 'core/core-engine/src')
9 files changed, 888 insertions, 16 deletions
diff --git a/core/core-engine/src/main/java/org/onap/policy/apex/core/engine/context/ApexInternalContext.java b/core/core-engine/src/main/java/org/onap/policy/apex/core/engine/context/ApexInternalContext.java index 8bbb333b5..f73281ada 100644 --- a/core/core-engine/src/main/java/org/onap/policy/apex/core/engine/context/ApexInternalContext.java +++ b/core/core-engine/src/main/java/org/onap/policy/apex/core/engine/context/ApexInternalContext.java @@ -53,7 +53,7 @@ import org.onap.policy.apex.model.utilities.comparison.KeyedMapDifference; */ public final class ApexInternalContext implements AxConceptGetter<ContextAlbum> { // The key of the currently running Apex model - private final AxArtifactKey key; + private AxArtifactKey key; // The context albums being used in this engine private final NavigableMap<AxArtifactKey, ContextAlbum> contextAlbums = @@ -72,6 +72,9 @@ public final class ApexInternalContext implements AxConceptGetter<ContextAlbum> * @throws ContextException On errors on context setting */ public ApexInternalContext(final AxPolicyModel apexPolicyModel) throws ContextException { + if (apexPolicyModel == null) { + throw new ContextException("internal context update failed, supplied model is null"); + } apexPolicyModel.register(); // The context distributor used to distribute context across policy engine instances @@ -121,20 +124,6 @@ public final class ApexInternalContext implements AxConceptGetter<ContextAlbum> final KeyedMapDifference<AxArtifactKey, AxContextAlbum> contextDifference = new ContextComparer().compare(ModelService.getModel(AxContextAlbums.class), newPolicyModel.getAlbums()); - // Remove maps that are no longer used - for (final Entry<AxArtifactKey, AxContextAlbum> removedContextAlbumEntry : contextDifference.getLeftOnly() - .entrySet()) { - contextDistributor.removeContextAlbum(removedContextAlbumEntry.getValue()); - contextAlbums.remove(removedContextAlbumEntry.getKey()); - } - - // We switch over to the new Apex model - newPolicyModel.register(); - - // Set up the new context albums - for (final AxArtifactKey contextAlbumKey : contextDifference.getRightOnly().keySet()) { - contextAlbums.put(contextAlbumKey, contextDistributor.createContextAlbum(contextAlbumKey)); - } // Handle the updated maps for (final Entry<AxArtifactKey, List<AxContextAlbum>> contextAlbumEntry : contextDifference.getDifferentValues() @@ -144,7 +133,7 @@ public final class ApexInternalContext implements AxConceptGetter<ContextAlbum> final AxContextAlbum newContextAlbum = contextAlbumEntry.getValue().get(1); // Check that the schemas are the same on the old and new context albums - if (currentContextAlbum.getItemSchema().equals(newContextAlbum.getItemSchema())) { + if (!currentContextAlbum.getItemSchema().equals(newContextAlbum.getItemSchema())) { // The schema is different, throw an exception because the schema should not change if the key of the // album has not changed throw new ContextException("internal context update failed on context album \"" @@ -154,7 +143,24 @@ public final class ApexInternalContext implements AxConceptGetter<ContextAlbum> + newContextAlbum.getItemSchema().getId() + "\" on incoming model"); } } + + // Remove maps that are no longer used + for (final Entry<AxArtifactKey, AxContextAlbum> removedContextAlbumEntry : contextDifference.getLeftOnly() + .entrySet()) { + contextDistributor.removeContextAlbum(removedContextAlbumEntry.getValue()); + contextAlbums.remove(removedContextAlbumEntry.getKey()); + } + // We switch over to the new Apex model + newPolicyModel.register(); + + // Set up the new context albums + for (final AxArtifactKey contextAlbumKey : contextDifference.getRightOnly().keySet()) { + contextAlbums.put(contextAlbumKey, contextDistributor.createContextAlbum(contextAlbumKey)); + } + + // Record the key of the current model + key = newPolicyModel.getKey(); } /** diff --git a/core/core-engine/src/main/java/org/onap/policy/apex/core/engine/event/EnEvent.java b/core/core-engine/src/main/java/org/onap/policy/apex/core/engine/event/EnEvent.java index 58bee8238..c510763cc 100644 --- a/core/core-engine/src/main/java/org/onap/policy/apex/core/engine/event/EnEvent.java +++ b/core/core-engine/src/main/java/org/onap/policy/apex/core/engine/event/EnEvent.java @@ -89,6 +89,10 @@ public class EnEvent extends HashMap<String, Object> { */ public EnEvent(final AxEvent axEvent) { super(); + + if (axEvent == null) { + throw new EnException("event definition is null or was not found in model service"); + } // Save the event definition from the Apex model this.axEvent = axEvent; } @@ -173,6 +177,16 @@ public class EnEvent extends HashMap<String, Object> { this.exceptionMessage = exceptionMessage; } + + /** + * Get the user artifact stack of the event. + * + * @return the event user artifact stack + */ + public AxConcept[] getUserArtifactStack() { + return userArtifactStack; + } + /** * Store the user artifact stack of the event. * @@ -345,4 +359,40 @@ public class EnEvent extends HashMap<String, Object> { return "EnEvent [axEvent=" + axEvent + ", userArtifactStack=" + Arrays.toString(userArtifactStack) + ", map=" + super.toString() + "]"; } + + /* (non-Javadoc) + * @see java.lang.Object#hashCode() + */ + @Override + public int hashCode() { + final int prime = 31; + int result = super.hashCode(); + result = prime * result + axEvent.hashCode(); + return result; + } + + /* (non-Javadoc) + * @see java.lang.Object#equals(java.lang.Object) + */ + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (!super.equals(obj)) { + return false; + } + if (!(obj instanceof EnEvent)) { + return false; + } + EnEvent other = (EnEvent) obj; + if (axEvent == null) { + if (other.axEvent != null) { + return false; + } + } else if (!axEvent.equals(other.axEvent)) { + return false; + } + return true; + } } diff --git a/core/core-engine/src/test/java/org/onap/policy/apex/core/engine/EngineParametersTest.java b/core/core-engine/src/test/java/org/onap/policy/apex/core/engine/EngineParametersTest.java new file mode 100644 index 000000000..1b3489790 --- /dev/null +++ b/core/core-engine/src/test/java/org/onap/policy/apex/core/engine/EngineParametersTest.java @@ -0,0 +1,60 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.core.engine; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.util.LinkedHashMap; +import java.util.Map; + +import org.junit.Test; +import org.onap.policy.apex.context.parameters.ContextParameters; +import org.onap.policy.common.parameters.ParameterService; + +/** + * Test the executor parameters. + * + */ +public class EngineParametersTest { + + @Test + public void test() { + EngineParameters pars = new EngineParameters(); + pars.setName("Name"); + assertEquals("Name", pars.getName()); + + ContextParameters contextPars = new ContextParameters(); + + pars.setContextParameters(contextPars); + assertEquals(contextPars, pars.getContextParameters()); + + Map<String, ExecutorParameters> executorParameterMap = new LinkedHashMap<>(); + executorParameterMap.put("Executor", new ExecutorParameters()); + pars.setExecutorParameterMap(executorParameterMap); + assertEquals(executorParameterMap, pars.getExecutorParameterMap()); + + assertTrue(pars.validate().isValid()); + + ParameterService.register(pars); + ParameterService.deregister(pars); + } +} diff --git a/core/core-engine/src/test/java/org/onap/policy/apex/core/engine/ExecutorParametersTest.java b/core/core-engine/src/test/java/org/onap/policy/apex/core/engine/ExecutorParametersTest.java new file mode 100644 index 000000000..88c8c852d --- /dev/null +++ b/core/core-engine/src/test/java/org/onap/policy/apex/core/engine/ExecutorParametersTest.java @@ -0,0 +1,57 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.core.engine; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; +import org.onap.policy.common.parameters.ParameterService; + +/** + * Test the executor parameters. + * + */ +public class ExecutorParametersTest { + + @Test + public void test() { + ExecutorParameters pars = new ExecutorParameters(); + pars.setName("Name"); + assertEquals("Name", pars.getName()); + pars.setStateFinalizerExecutorPluginClass("some.state.finalizer.plugin.class"); + assertEquals("some.state.finalizer.plugin.class", pars.getStateFinalizerExecutorPluginClass()); + pars.setTaskExecutorPluginClass("some.task.executor.plugin.class"); + assertEquals("some.task.executor.plugin.class", pars.getTaskExecutorPluginClass()); + pars.setTaskSelectionExecutorPluginClass("some.task.selection.executor.plugin.class"); + assertEquals("some.task.selection.executor.plugin.class", pars.getTaskSelectionExecutorPluginClass()); + + assertEquals("ExecutorParameters [name=Name, taskExecutorPluginClass=some.task.executor.plugin.class, " + + "taskSelectionExecutorPluginClass=some.task.selection.executor.plugin.class, " + + "stateFinalizerExecutorPluginClass=some.state.finalizer.plugin.class]", pars.toString()); + + assertTrue(pars.validate().isValid()); + + + ParameterService.register(pars); + ParameterService.deregister(pars); + } +} diff --git a/core/core-engine/src/test/java/org/onap/policy/apex/core/engine/context/ApexInternalContextTest.java b/core/core-engine/src/test/java/org/onap/policy/apex/core/engine/context/ApexInternalContextTest.java new file mode 100644 index 000000000..a75ad37f6 --- /dev/null +++ b/core/core-engine/src/test/java/org/onap/policy/apex/core/engine/context/ApexInternalContextTest.java @@ -0,0 +1,188 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.core.engine.context; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.apex.context.ContextException; +import org.onap.policy.apex.context.parameters.ContextParameterConstants; +import org.onap.policy.apex.context.parameters.DistributorParameters; +import org.onap.policy.apex.context.parameters.LockManagerParameters; +import org.onap.policy.apex.context.parameters.PersistorParameters; +import org.onap.policy.apex.context.parameters.SchemaParameters; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbum; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema; +import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; +import org.onap.policy.common.parameters.ParameterService; + +/** + * Test the Apex engine internal context class. + */ +public class ApexInternalContextTest { + + private AxPolicyModel policyModel; + private AxPolicyModel newVersionPolicyModel; + private AxPolicyModel newPolicyModel; + private AxContextAlbum album; + private AxContextAlbum newAlbum; + private AxPolicyModel incompatiblePolicyModel; + + /** + * Initialize parameters. + */ + @Before + public void registerParameters() { + ParameterService.register(new SchemaParameters()); + ParameterService.register(new DistributorParameters()); + ParameterService.register(new LockManagerParameters()); + ParameterService.register(new PersistorParameters()); + } + + /** + * Create policy model. + */ + @Before + public void createPolicyModels() { + AxArtifactKey modelKey = new AxArtifactKey("PolicyModel:0.0.1"); + policyModel = new AxPolicyModel(modelKey); + + AxArtifactKey schemaKey = new AxArtifactKey("Schema:0.0.1"); + AxContextSchema schema = new AxContextSchema(schemaKey, "Java", "java.lang.String"); + policyModel.getSchemas().getSchemasMap().put(schemaKey, schema); + + AxArtifactKey albumKey = new AxArtifactKey("Album:0.0.1"); + album = new AxContextAlbum(albumKey, "Policy", true, schemaKey); + + policyModel.getAlbums().getAlbumsMap().put(albumKey, album); + + AxArtifactKey newVersionModelKey = new AxArtifactKey("PolicyModel:0.0.2"); + newVersionPolicyModel = new AxPolicyModel(newVersionModelKey); + + newVersionPolicyModel.getSchemas().getSchemasMap().put(schemaKey, schema); + AxContextAlbum compatibleAlbum = new AxContextAlbum(albumKey, "Global", true, schemaKey); + newVersionPolicyModel.getAlbums().getAlbumsMap().put(albumKey, compatibleAlbum); + + AxArtifactKey anotherAlbumKey = new AxArtifactKey("AnotherAlbum:0.0.1"); + AxContextAlbum anotherAlbum = new AxContextAlbum(anotherAlbumKey, "Policy", true, schemaKey); + + newVersionPolicyModel.getAlbums().getAlbumsMap().put(anotherAlbumKey, anotherAlbum); + + AxArtifactKey incompatibleModelKey = new AxArtifactKey("IncompatiblePolicyModel:0.0.2"); + incompatiblePolicyModel = new AxPolicyModel(incompatibleModelKey); + + AxArtifactKey incompatibleSchemaKey = new AxArtifactKey("IncompatibleSchema:0.0.1"); + AxContextSchema incompatibleSchema = new AxContextSchema(incompatibleSchemaKey, "Java", "java.lang.Integer"); + incompatiblePolicyModel.getSchemas().getSchemasMap().put(incompatibleSchemaKey, incompatibleSchema); + + AxContextAlbum incompatibleAlbum = new AxContextAlbum(albumKey, "Policy", true, incompatibleSchemaKey); + incompatiblePolicyModel.getAlbums().getAlbumsMap().put(albumKey, incompatibleAlbum); + + AxArtifactKey newModelKey = new AxArtifactKey("NewPolicyModel:0.0.1"); + newPolicyModel = new AxPolicyModel(newModelKey); + + AxArtifactKey newSchemaKey = new AxArtifactKey("NewSchema:0.0.1"); + AxContextSchema newSchema = new AxContextSchema(newSchemaKey, "Java", "java.lang.Integer"); + newPolicyModel.getSchemas().getSchemasMap().put(newSchemaKey, newSchema); + + AxArtifactKey newAlbumKey = new AxArtifactKey("NewAlbum:0.0.1"); + newAlbum = new AxContextAlbum(newAlbumKey, "Policy", true, newSchemaKey); + + newPolicyModel.getAlbums().getAlbumsMap().put(newAlbumKey, newAlbum); + } + + /** + * Deregister parameters. + */ + @After + public void deregisterParameters() { + ParameterService.deregister(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME); + ParameterService.deregister(ContextParameterConstants.LOCKING_GROUP_NAME); + ParameterService.deregister(ContextParameterConstants.PERSISTENCE_GROUP_NAME); + ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME); + } + + @Test + public void testAlbumInit() throws ContextException { + try { + new ApexInternalContext(null); + fail("test should throw an exception"); + } catch (ContextException ce) { + assertEquals("internal context update failed, supplied model is null", ce.getMessage()); + } + + ApexInternalContext context = new ApexInternalContext(policyModel); + + assertEquals(policyModel.getKey(), context.getKey()); + assertEquals(1, context.getContextAlbums().size()); + + AxArtifactKey albumKey = new AxArtifactKey("Album:0.0.1"); + assertEquals(album.getId(), context.get(albumKey).getKey().getId()); + assertEquals(album.getId(), context.get(albumKey.getName()).getKey().getId()); + assertEquals(album.getId(), context.get(albumKey.getName(), albumKey.getVersion()).getKey().getId()); + assertEquals(album.getId(), context.getAll(albumKey.getName()).iterator().next().getKey().getId()); + assertEquals(album.getId(), + context.getAll(albumKey.getName(), albumKey.getVersion()).iterator().next().getKey().getId()); + + context.clear(); + assertEquals(1, context.getContextAlbums().size()); + + assertEquals("ApexInternalContext [contextAlbums={AxArtifactKey:(name=Album,version=0.0.1)", + context.toString().substring(0, 76)); + } + + @Test + public void testAlbumUpdate() throws ContextException { + ApexInternalContext context = new ApexInternalContext(policyModel); + + try { + context.update(null); + fail("test should throw an exception"); + } catch (ContextException ce) { + assertEquals("internal context update failed, supplied model is null", ce.getMessage()); + } + + assertEquals(policyModel.getKey().getId(), context.getKey().getId()); + assertEquals(1, context.getContextAlbums().size()); + + try { + context.update(incompatiblePolicyModel); + fail("test should throw an exception here"); + } catch (ContextException ce) { + assertEquals("internal context update failed on context album \"Album:0.0.1\" " + + "in model \"PolicyModel:0.0.1\", " + + "schema \"Schema:0.0.1\" on existing context model does not equal " + + "schema \"IncompatibleSchema:0.0.1\" on incoming model", ce.getMessage()); + } + + assertEquals(policyModel.getKey().getId(), context.getKey().getId()); + + context.update(newVersionPolicyModel); + assertEquals(newVersionPolicyModel.getKey().getId(), context.getKey().getId()); + + context.update(newPolicyModel); + assertEquals(newPolicyModel.getKey().getId(), context.getKey().getId()); + } +} diff --git a/core/core-engine/src/test/java/org/onap/policy/apex/core/engine/event/DummyAxKey.java b/core/core-engine/src/test/java/org/onap/policy/apex/core/engine/event/DummyAxKey.java new file mode 100644 index 000000000..0a86c070a --- /dev/null +++ b/core/core-engine/src/test/java/org/onap/policy/apex/core/engine/event/DummyAxKey.java @@ -0,0 +1,163 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.core.engine.event; + +import java.util.List; + +import org.apache.commons.lang3.NotImplementedException; +import org.onap.policy.apex.model.basicmodel.concepts.AxConcept; +import org.onap.policy.apex.model.basicmodel.concepts.AxKey; +import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult; + +/** + * Dummy Key Class. + */ +public class DummyAxKey extends AxKey { + private static final long serialVersionUID = 964899169013353800L; + + /* + * (non-Javadoc) + * + * @see java.lang.Comparable#compareTo(java.lang.Object) + */ + @Override + public int compareTo(AxConcept concept) { + throw new NotImplementedException("Not implemented on dummy class"); + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.model.basicmodel.concepts.AxKey#getId() + */ + @Override + public String getId() { + throw new NotImplementedException("Not implemented on dummy class"); + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.model.basicmodel.concepts.AxKey#getCompatibility(org.onap.policy.apex.model.basicmodel. + * concepts.AxKey) + */ + @Override + public Compatibility getCompatibility(AxKey otherKey) { + throw new NotImplementedException("Not implemented on dummy class"); + } + + /* + * (non-Javadoc) + * + * @see + * org.onap.policy.apex.model.basicmodel.concepts.AxKey#isCompatible(org.onap.policy.apex.model.basicmodel.concepts. + * AxKey) + */ + @Override + public boolean isCompatible(AxKey otherKey) { + throw new NotImplementedException("Not implemented on dummy class"); + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#getKey() + */ + @Override + public AxKey getKey() { + throw new NotImplementedException("Not implemented on dummy class"); + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#getKeys() + */ + @Override + public List<AxKey> getKeys() { + throw new NotImplementedException("Not implemented on dummy class"); + } + + /* + * (non-Javadoc) + * + * @see + * org.onap.policy.apex.model.basicmodel.concepts.AxConcept#validate(org.onap.policy.apex.model.basicmodel.concepts. + * AxValidationResult) + */ + @Override + public AxValidationResult validate(AxValidationResult result) { + throw new NotImplementedException("Not implemented on dummy class"); + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#clean() + */ + @Override + public void clean() { + throw new NotImplementedException("Not implemented on dummy class"); + + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#equals(java.lang.Object) + */ + @Override + public boolean equals(Object otherObject) { + throw new NotImplementedException("Not implemented on dummy class"); + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#toString() + */ + @Override + public String toString() { + return "Dummy Key"; + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.model.basicmodel.concepts.AxConcept#hashCode() + */ + @Override + public int hashCode() { + throw new NotImplementedException("Not implemented on dummy class"); + } + + /* + * (non-Javadoc) + * + * @see + * org.onap.policy.apex.model.basicmodel.concepts.AxConcept#copyTo(org.onap.policy.apex.model.basicmodel.concepts. + * AxConcept) + */ + @Override + public AxConcept copyTo(AxConcept target) { + throw new NotImplementedException("Not implemented on dummy class"); + } +} diff --git a/core/core-engine/src/test/java/org/onap/policy/apex/core/engine/event/EnEventTest.java b/core/core-engine/src/test/java/org/onap/policy/apex/core/engine/event/EnEventTest.java new file mode 100644 index 000000000..4fd9367ca --- /dev/null +++ b/core/core-engine/src/test/java/org/onap/policy/apex/core/engine/event/EnEventTest.java @@ -0,0 +1,209 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.core.engine.event; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.Map; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.apex.context.ContextRuntimeException; +import org.onap.policy.apex.context.parameters.ContextParameterConstants; +import org.onap.policy.apex.context.parameters.SchemaParameters; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.basicmodel.concepts.AxConcept; +import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey; +import org.onap.policy.apex.model.basicmodel.service.ModelService; +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.common.parameters.ParameterService; + +/** + * Test the engine event class. + */ +public class EnEventTest { + /** + * Set up the services. + */ + @Before + public void setupServices() { + ModelService.registerModel(AxContextSchemas.class, new AxContextSchemas()); + ModelService.registerModel(AxEvents.class, new AxEvents()); + ParameterService.register(new SchemaParameters()); + } + + /** + * Tear down the services. + */ + @After + public void teardownServices() { + ModelService.deregisterModel(AxContextSchema.class); + ModelService.deregisterModel(AxEvents.class); + ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME); + } + + @Test + public void testEnEvent() { + AxArtifactKey eventKey = new AxArtifactKey("Event:0.0.1"); + try { + new EnEvent(eventKey); + fail("test should throw an exception here"); + } catch (EnException ee) { + assertEquals("event definition is null or was not found in model service", ee.getMessage()); + } + + try { + new EnEvent((AxEvent) null); + fail("test should throw an exception here"); + } catch (EnException ee) { + assertEquals("event definition is null or was not found in model service", ee.getMessage()); + } + + AxEvent axEvent = new AxEvent(eventKey, "a.name.space", "some source", "some target"); + ModelService.getModel(AxEvents.class).getEventMap().put(eventKey, axEvent); + + EnEvent event = new EnEvent(eventKey); + assertEquals(eventKey, event.getKey()); + assertEquals("Event:0.0.1", event.getId()); + assertEquals("Event", event.getName()); + assertEquals(axEvent, event.getAxEvent()); + event.setExecutionId(123454321L); + assertEquals(123454321L, event.getExecutionId()); + event.setExceptionMessage("Something happened"); + assertEquals("Something happened", event.getExceptionMessage()); + AxConcept[] usedArtifactStackArray = + { eventKey }; + event.setUserArtifactStack(usedArtifactStackArray); + assertEquals(usedArtifactStackArray.length, event.getUserArtifactStack().length); + assertEquals("EnEvent [axEvent=AxEvent:(key=AxArtifactKey:(name=Event,version=0.0.1),nameSpace=a.name.space," + + "source=some source,target=some target,parameter={}), " + + "userArtifactStack=[AxArtifactKey:(name=Event,version=0.0.1)], map={}]", event.toString()); + try { + event.put(null, null); + fail("test should throw an exception here"); + } catch (EnException ee) { + assertEquals("null keys are illegal on method parameter \"key\"", ee.getMessage()); + } + + try { + event.put("NonField", null); + fail("test should throw an exception here"); + } catch (EnException ee) { + assertEquals("parameter with key \"NonField\" not defined on event \"Event\"", ee.getMessage()); + } + + AxReferenceKey fieldKey = new AxReferenceKey("Parent", "0.0.1", "MyParent", "MyField"); + AxArtifactKey fieldSchemaKey = new AxArtifactKey("FieldSchema:0.0.1"); + AxField axField = new AxField(fieldKey, fieldSchemaKey); + + AxConcept[] usedArtifactStackArrayMultiple = + { eventKey, fieldKey, new DummyAxKey() }; + event.setUserArtifactStack(usedArtifactStackArrayMultiple); + + AxContextSchema schema = new AxContextSchema(fieldSchemaKey, "Java", "java.lang.Integer"); + ModelService.getModel(AxContextSchemas.class).getSchemasMap().put(fieldSchemaKey, schema); + + Map<String, AxField> parameterMap = new LinkedHashMap<>(); + parameterMap.put("MyField", axField); + ModelService.getModel(AxEvents.class).get(eventKey).setParameterMap(parameterMap); + + try { + event.put("MyField", null); + } catch (ContextRuntimeException cre) { + fail("test should throw an exception here"); + } + assertNull(event.get("MyField")); + + try { + event.put("MyField", "Hello"); + fail("test should throw an exception here"); + } catch (ContextRuntimeException cre) { + assertEquals("Parent:0.0.1:MyParent:MyField: object \"Hello\" of class \"java.lang.String\" " + + "not compatible with class \"java.lang.Integer\"", cre.getMessage()); + } + + event.put("MyField", 123); + assertEquals(123, event.get("MyField")); + + assertTrue(event.keySet().contains("MyField")); + assertTrue(event.values().contains(123)); + assertEquals("MyField", event.entrySet().iterator().next().getKey()); + + event.putAll(event); + + try { + event.get(null); + fail("test should throw an exception here"); + } catch (EnException ee) { + assertEquals("null values are illegal on method parameter \"key\"", ee.getMessage()); + } + + try { + event.get("NonField"); + fail("test should throw an exception here"); + } catch (EnException ee) { + assertEquals("parameter with key NonField not defined on this event", ee.getMessage()); + } + + try { + event.remove(null); + fail("test should throw an exception here"); + } catch (EnException ee) { + assertEquals("null keys are illegal on method parameter \"key\"", ee.getMessage()); + } + + try { + event.remove("NonField"); + fail("test should throw an exception here"); + } catch (EnException ee) { + assertEquals("parameter with key NonField not defined on this event", ee.getMessage()); + } + + event.remove("MyField"); + assertNull(event.get("MyField")); + + event.put("MyField", 123); + assertEquals(123, event.get("MyField")); + event.clear(); + assertNull(event.get("MyField")); + + assertTrue(event.hashCode() != 0); + + assertTrue(event.equals(event)); + assertFalse(event.equals(null)); + Map<String, Object> hashMap = new HashMap<>(); + assertFalse(event.equals(hashMap)); + + EnEvent otherEvent = new EnEvent(eventKey); + assertTrue(event.equals(otherEvent)); + } +} diff --git a/core/core-engine/src/test/java/org/onap/policy/apex/core/engine/event/EnExceptionTest.java b/core/core-engine/src/test/java/org/onap/policy/apex/core/engine/event/EnExceptionTest.java new file mode 100644 index 000000000..ee54fd959 --- /dev/null +++ b/core/core-engine/src/test/java/org/onap/policy/apex/core/engine/event/EnExceptionTest.java @@ -0,0 +1,42 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.core.engine.event; + +import static org.junit.Assert.assertEquals; + +import java.io.IOException; + +import org.junit.Test; + +/** + * Test the event exception class. + */ +public class EnExceptionTest { + + @Test + public void testEnException() { + EnException ene = new EnException("Message"); + assertEquals("Message", ene.getMessage()); + + ene = new EnException("Message", new IOException()); + assertEquals("Message", ene.getMessage()); + } +} diff --git a/core/core-engine/src/test/java/org/onap/policy/apex/core/engine/event/EnFieldTest.java b/core/core-engine/src/test/java/org/onap/policy/apex/core/engine/event/EnFieldTest.java new file mode 100644 index 000000000..6228c6834 --- /dev/null +++ b/core/core-engine/src/test/java/org/onap/policy/apex/core/engine/event/EnFieldTest.java @@ -0,0 +1,97 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.core.engine.event; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.apex.context.parameters.ContextParameterConstants; +import org.onap.policy.apex.context.parameters.SchemaParameters; +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.service.ModelService; +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.AxField; +import org.onap.policy.common.parameters.ParameterService; + +/** + * Test the EnField class. + */ +public class EnFieldTest { + /** + * Set up the services. + */ + @Before + public void setupServices() { + AxContextSchemas schemas = new AxContextSchemas(); + ModelService.registerModel(AxContextSchemas.class, schemas); + ParameterService.register(new SchemaParameters()); + } + + /** + * Tear down the services. + */ + @After + public void teardownServices() { + ModelService.deregisterModel(AxContextSchemas.class); + ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME); + } + + @Test + public void testEnField() { + AxReferenceKey fieldKey = new AxReferenceKey("Parent", "0.0.1", "MyParent", "MyField"); + AxArtifactKey fieldSchemaKey = new AxArtifactKey("FieldSchema:0.0.1"); + AxField axField = new AxField(fieldKey, fieldSchemaKey); + + try { + new EnField(axField, null); + fail("test should throw an exception"); + } catch (EnException ee) { + assertEquals("schema helper cannot be created for parameter with key \"Parent:0.0.1:MyParent:MyField\" " + + "with schema \"AxArtifactKey:(name=FieldSchema,version=0.0.1)\"", ee.getMessage()); + } + + AxContextSchema schema = new AxContextSchema(fieldSchemaKey, "Java", "java.lang.Integer"); + ModelService.getModel(AxContextSchemas.class).getSchemasMap().put(fieldSchemaKey, schema); + EnField field = new EnField(axField, 123); + + assertEquals(axField, field.getAxField()); + assertEquals(123, field.getValue()); + assertEquals(fieldKey, field.getKey()); + assertEquals("MyField", field.getName()); + assertEquals("org.onap.policy.apex.context.impl.schema.java.JavaSchemaHelper", + field.getSchemaHelper().getClass().getCanonicalName()); + assertEquals(123, field.getAssignableValue()); + assertEquals("EnField [axField=AxField:(key=AxReferenceKey:(parentKeyName=Parent,parentKeyVersion=0.0.1," + + "parentLocalName=MyParent,localName=MyField),fieldSchemaKey=AxArtifactKey:" + + "(name=FieldSchema,version=0.0.1),optional=false), value=123]", field.toString()); + assertTrue(field.isAssignableValue()); + + field = new EnField(axField, "Hello"); + assertFalse(field.isAssignableValue()); + } +} |