From 16a8f59fed2e53b1bbedaf65a33165bb4d3225f8 Mon Sep 17 00:00:00 2001 From: liamfallon Date: Wed, 3 Oct 2018 15:22:53 +0100 Subject: 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 --- .../core/engine/context/ApexInternalContext.java | 38 +++++++++------- .../policy/apex/core/engine/event/EnEvent.java | 50 ++++++++++++++++++++++ 2 files changed, 72 insertions(+), 16 deletions(-) (limited to 'core/core-engine/src/main') 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 { // 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 contextAlbums = @@ -72,6 +72,9 @@ public final class ApexInternalContext implements AxConceptGetter * @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 final KeyedMapDifference contextDifference = new ContextComparer().compare(ModelService.getModel(AxContextAlbums.class), newPolicyModel.getAlbums()); - // Remove maps that are no longer used - for (final Entry 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> contextAlbumEntry : contextDifference.getDifferentValues() @@ -144,7 +133,7 @@ public final class ApexInternalContext implements AxConceptGetter 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 + newContextAlbum.getItemSchema().getId() + "\" on incoming model"); } } + + // Remove maps that are no longer used + for (final Entry 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 { */ 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 { 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 { 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; + } } -- cgit 1.2.3-korg