diff options
Diffstat (limited to 'context/context-management')
3 files changed, 399 insertions, 8 deletions
diff --git a/context/context-management/src/main/java/org/onap/policy/apex/context/impl/ContextAlbumImpl.java b/context/context-management/src/main/java/org/onap/policy/apex/context/impl/ContextAlbumImpl.java index 6382992d9..221c1987a 100644 --- a/context/context-management/src/main/java/org/onap/policy/apex/context/impl/ContextAlbumImpl.java +++ b/context/context-management/src/main/java/org/onap/policy/apex/context/impl/ContextAlbumImpl.java @@ -47,7 +47,7 @@ import org.slf4j.ext.XLoggerFactory; * * @author Liam Fallon (liam.fallon@ericsson.com) */ -public final class ContextAlbumImpl implements ContextAlbum { +public final class ContextAlbumImpl implements ContextAlbum, Comparable<ContextAlbumImpl> { // Logger for this class private static final XLogger LOGGER = XLoggerFactory.getXLogger(ContextAlbumImpl.class); @@ -502,4 +502,45 @@ public final class ContextAlbumImpl implements ContextAlbum { // Clear the map albumMap.clear(); } + + /* (non-Javadoc) + * @see java.lang.Comparable#compareTo(java.lang.Object) + */ + @Override + public int compareTo(ContextAlbumImpl otherContextAlbumImpl) { + return (equals(otherContextAlbumImpl) ? 0 : 1); + } + + /* (non-Javadoc) + * @see java.lang.Object#hashCode() + */ + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + albumDefinition.hashCode(); + result = prime * result + albumMap.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 (obj == null) { + return false; + } + if (!(obj instanceof ContextAlbumImpl)) { + return false; + } + ContextAlbumImpl other = (ContextAlbumImpl) obj; + if (!albumDefinition.equals(other.albumDefinition)) { + return false; + } + return albumMap.equals(other.albumMap); + } } diff --git a/context/context-management/src/test/java/org/onap/policy/apex/context/impl/ContextAlbumImplTest.java b/context/context-management/src/test/java/org/onap/policy/apex/context/impl/ContextAlbumImplTest.java index 25928716e..cb99fc965 100644 --- a/context/context-management/src/test/java/org/onap/policy/apex/context/impl/ContextAlbumImplTest.java +++ b/context/context-management/src/test/java/org/onap/policy/apex/context/impl/ContextAlbumImplTest.java @@ -21,6 +21,7 @@ package org.onap.policy.apex.context.impl; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; @@ -45,6 +46,7 @@ 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.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.common.parameters.ParameterService; @@ -123,7 +125,7 @@ public class ContextAlbumImplTest { fail("this test should throw an exception"); } catch (ApexRuntimeException e) { assertEquals("Model for org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas " - + "not found in model service", e.getMessage()); + + "not found in model service", e.getMessage()); } catch (ContextException e) { fail("this test should throw an ApexRuntimeException"); } @@ -154,7 +156,7 @@ public class ContextAlbumImplTest { ContextAlbum album = new ContextAlbumImpl(axContextAlbum, distributor, new LinkedHashMap<String, Object>()); AxContextAlbum axContextAlbumRo = new AxContextAlbum(new AxArtifactKey("TestContextAlbum", "0.0.1"), "Policy", - false, simpleStringSchema.getKey()); + false, simpleStringSchema.getKey()); ContextAlbum albumRo = new ContextAlbumImpl(axContextAlbumRo, distributor, new LinkedHashMap<String, Object>()); assertEquals("TestContextAlbum", album.getName()); @@ -224,9 +226,8 @@ public class ContextAlbumImplTest { albumRo.remove("AllKey0"); fail("test should throw an exception"); } catch (ContextRuntimeException e) { - assertEquals( - "album \"TestContextAlbum:0.0.1\" remove() not allowed on read only albums for key=\"AllKey0\"", - e.getMessage()); + assertEquals("album \"TestContextAlbum:0.0.1\" remove() not allowed " + + "on read only albums for key=\"AllKey0\"", e.getMessage()); } try { @@ -273,7 +274,8 @@ public class ContextAlbumImplTest { AxArtifactKey somePolicyKey = new AxArtifactKey("MyPolicy", "0.0.1"); AxReferenceKey somePolicyState = new AxReferenceKey(somePolicyKey, "SomeState"); - AxConcept[] userArtifactStack = { somePolicyKey, somePolicyState }; + AxConcept[] userArtifactStack = + { somePolicyKey, somePolicyState }; album.setUserArtifactStack(userArtifactStack); assertEquals("MyPolicy:0.0.1", album.getUserArtifactStack()[0].getId()); assertEquals("MyPolicy:0.0.1:NULL:SomeState", album.getUserArtifactStack()[1].getId()); @@ -309,10 +311,78 @@ public class ContextAlbumImplTest { } assertEquals("New value of Key0", album.remove("Key0")); - + album.clear(); assertTrue(album.isEmpty()); + + ModelService.clear(); + } + + @SuppressWarnings("unlikely-arg-type") + @Test + public void testCompareToEqualsHash() throws ContextException { + AxContextSchemas schemas = new AxContextSchemas(); + AxContextSchema simpleIntSchema = new AxContextSchema(new AxArtifactKey("SimpleIntSchema", "0.0.1"), "JAVA", + "java.lang.Integer"); + schemas.getSchemasMap().put(simpleIntSchema.getKey(), simpleIntSchema); + AxContextSchema simpleStringSchema = new AxContextSchema(new AxArtifactKey("SimpleStringSchema", "0.0.1"), + "JAVA", "java.lang.String"); + schemas.getSchemasMap().put(simpleStringSchema.getKey(), simpleStringSchema); + ModelService.registerModel(AxContextSchemas.class, schemas); + + AxContextAlbum axContextAlbum = new AxContextAlbum(new AxArtifactKey("TestContextAlbum", "0.0.1"), "Policy", + true, AxArtifactKey.getNullKey()); + + axContextAlbum.setItemSchema(simpleIntSchema.getKey()); + Distributor distributor = new JvmLocalDistributor(); + distributor.init(axContextAlbum.getKey()); + ContextAlbumImpl album = new ContextAlbumImpl(axContextAlbum, distributor, new LinkedHashMap<String, Object>()); + + assertTrue(album.hashCode() != 0); + + assertEquals(0, album.compareTo(album)); + assertEquals(1, album.compareTo(null)); + + assertTrue(album.equals(album)); + assertFalse(album.equals(new DummyContextAlbumImpl())); + + ContextAlbumImpl otherAlbum = new ContextAlbumImpl(axContextAlbum, distributor, + new LinkedHashMap<String, Object>()); + assertTrue(album.equals(otherAlbum)); + + otherAlbum.put("Key", 123); + assertFalse(album.equals(otherAlbum)); + + try { + otherAlbum.put("Key", "BadValue"); + fail("test should throw an exception here"); + } catch (ContextRuntimeException cre) { + assertEquals("Failed to set context value for key \"Key\" in album \"TestContextAlbum:0.0.1\": " + + "TestContextAlbum:0.0.1: object \"BadValue\" of class \"java.lang.String\" " + + "not compatible with class \"java.lang.Integer\"", cre.getMessage()); + } + + AxContextAlbum otherAxContextAlbum = new AxContextAlbum(new AxArtifactKey("OtherTestContextAlbum", "0.0.1"), + "Policy", true, AxArtifactKey.getNullKey()); + + otherAxContextAlbum.setItemSchema(simpleStringSchema.getKey()); + otherAlbum = new ContextAlbumImpl(otherAxContextAlbum, distributor, new LinkedHashMap<String, Object>()); + assertFalse(album.equals(otherAlbum)); + + try { + album.flush(); + fail("test should throw an exception here"); + } catch (ContextException ce) { + assertEquals("map flush failed, supplied map is null", ce.getMessage()); + } + + AxContextAlbums albums = new AxContextAlbums(); + ModelService.registerModel(AxContextAlbums.class, albums); + albums.getAlbumsMap().put(axContextAlbum.getKey(), axContextAlbum); + distributor.createContextAlbum(album.getKey()); + album.flush(); + ModelService.clear(); } } diff --git a/context/context-management/src/test/java/org/onap/policy/apex/context/impl/DummyContextAlbumImpl.java b/context/context-management/src/test/java/org/onap/policy/apex/context/impl/DummyContextAlbumImpl.java new file mode 100644 index 000000000..9109ec694 --- /dev/null +++ b/context/context-management/src/test/java/org/onap/policy/apex/context/impl/DummyContextAlbumImpl.java @@ -0,0 +1,280 @@ +/*- + * ============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.context.impl; + +import java.util.Collection; +import java.util.Map; +import java.util.Set; + +import org.apache.commons.lang3.NotImplementedException; +import org.onap.policy.apex.context.ContextAlbum; +import org.onap.policy.apex.context.ContextException; +import org.onap.policy.apex.context.SchemaHelper; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.basicmodel.concepts.AxConcept; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbum; + +/** + * Dummy album implementation class. + * + */ +public class DummyContextAlbumImpl implements ContextAlbum { + + /* + * (non-Javadoc) + * + * @see java.util.Map#clear() + */ + @Override + public void clear() { + throw new NotImplementedException("Not Implemeted on dummy class"); + } + + /* + * (non-Javadoc) + * + * @see java.util.Map#containsKey(java.lang.Object) + */ + @Override + public boolean containsKey(Object key) { + throw new NotImplementedException("Not Implemeted on dummy class"); + } + + /* + * (non-Javadoc) + * + * @see java.util.Map#containsValue(java.lang.Object) + */ + @Override + public boolean containsValue(Object value) { + throw new NotImplementedException("Not Implemeted on dummy class"); + } + + /* + * (non-Javadoc) + * + * @see java.util.Map#entrySet() + */ + @Override + public Set<Entry<String, Object>> entrySet() { + throw new NotImplementedException("Not Implemeted on dummy class"); + } + + /* + * (non-Javadoc) + * + * @see java.util.Map#get(java.lang.Object) + */ + @Override + public Object get(Object key) { + throw new NotImplementedException("Not Implemeted on dummy class"); + } + + /* + * (non-Javadoc) + * + * @see java.util.Map#isEmpty() + */ + @Override + public boolean isEmpty() { + throw new NotImplementedException("Not Implemeted on dummy class"); + } + + /* + * (non-Javadoc) + * + * @see java.util.Map#keySet() + */ + @Override + public Set<String> keySet() { + throw new NotImplementedException("Not Implemeted on dummy class"); + } + + /* + * (non-Javadoc) + * + * @see java.util.Map#put(java.lang.Object, java.lang.Object) + */ + @Override + public Object put(String key, Object value) { + throw new NotImplementedException("Not Implemeted on dummy class"); + } + + /* + * (non-Javadoc) + * + * @see java.util.Map#putAll(java.util.Map) + */ + @Override + public void putAll(Map<? extends String, ? extends Object> map) { + throw new NotImplementedException("Not Implemeted on dummy class"); + + } + + /* + * (non-Javadoc) + * + * @see java.util.Map#remove(java.lang.Object) + */ + @Override + public Object remove(Object key) { + throw new NotImplementedException("Not Implemeted on dummy class"); + } + + /* + * (non-Javadoc) + * + * @see java.util.Map#size() + */ + @Override + public int size() { + throw new NotImplementedException("Not Implemeted on dummy class"); + } + + /* + * (non-Javadoc) + * + * @see java.util.Map#values() + */ + @Override + public Collection<Object> values() { + throw new NotImplementedException("Not Implemeted on dummy class"); + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.context.ContextAlbum#getKey() + */ + @Override + public AxArtifactKey getKey() { + throw new NotImplementedException("Not Implemeted on dummy class"); + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.context.ContextAlbum#getName() + */ + @Override + public String getName() { + throw new NotImplementedException("Not Implemeted on dummy class"); + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.context.ContextAlbum#getAlbumDefinition() + */ + @Override + public AxContextAlbum getAlbumDefinition() { + throw new NotImplementedException("Not Implemeted on dummy class"); + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.context.ContextAlbum#getSchemaHelper() + */ + @Override + public SchemaHelper getSchemaHelper() { + throw new NotImplementedException("Not Implemeted on dummy class"); + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.context.ContextAlbum#lockForReading(java.lang.String) + */ + @Override + public void lockForReading(String key) throws ContextException { + throw new NotImplementedException("Not Implemeted on dummy class"); + + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.context.ContextAlbum#lockForWriting(java.lang.String) + */ + @Override + public void lockForWriting(String key) throws ContextException { + throw new NotImplementedException("Not Implemeted on dummy class"); + + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.context.ContextAlbum#unlockForReading(java.lang.String) + */ + @Override + public void unlockForReading(String key) throws ContextException { + throw new NotImplementedException("Not Implemeted on dummy class"); + + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.context.ContextAlbum#unlockForWriting(java.lang.String) + */ + @Override + public void unlockForWriting(String key) throws ContextException { + throw new NotImplementedException("Not Implemeted on dummy class"); + + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.context.ContextAlbum#getUserArtifactStack() + */ + @Override + public AxConcept[] getUserArtifactStack() { + throw new NotImplementedException("Not Implemeted on dummy class"); + } + + /* + * (non-Javadoc) + * + * @see + * org.onap.policy.apex.context.ContextAlbum#setUserArtifactStack(org.onap.policy.apex.model.basicmodel.concepts. + * AxConcept[]) + */ + @Override + public void setUserArtifactStack(AxConcept[] userArtifactStack) { + throw new NotImplementedException("Not Implemeted on dummy class"); + + } + + /* + * (non-Javadoc) + * + * @see org.onap.policy.apex.context.ContextAlbum#flush() + */ + @Override + public void flush() throws ContextException { + throw new NotImplementedException("Not Implemeted on dummy class"); + + } + +} |