diff options
Diffstat (limited to 'context')
4 files changed, 352 insertions, 1 deletions
diff --git a/context/context-management/src/main/java/org/onap/policy/apex/context/ContextAlbum.java b/context/context-management/src/main/java/org/onap/policy/apex/context/ContextAlbum.java index 73ef668a0..559139321 100644 --- a/context/context-management/src/main/java/org/onap/policy/apex/context/ContextAlbum.java +++ b/context/context-management/src/main/java/org/onap/policy/apex/context/ContextAlbum.java @@ -112,6 +112,13 @@ public interface ContextAlbum extends Map<String, Object> { void unlockForWriting(String key) throws ContextException; /** + * Get the stack of artifact keys currently using this context item. + * + * @return the keys of the artifacts using the context album at the moment + */ + AxConcept[] getUserArtifactStack(); + + /** * Set the stack of artifact keys currently using this context item. * * @param userArtifactStack the keys of the artifacts using the context album at the moment 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 a68271609..fbaeb2c91 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 @@ -79,6 +79,10 @@ public final class ContextAlbumImpl implements ContextAlbum { */ public ContextAlbumImpl(final AxContextAlbum albumDefinition, final Distributor distributor, final Map<String, Object> albumMap) throws ContextException { + Assertions.argumentNotNull(albumDefinition, "Context album definition may not be null"); + Assertions.argumentNotNull(distributor, "Distributor may not be null"); + Assertions.argumentNotNull(albumMap, "Album map may not be null"); + this.albumDefinition = albumDefinition; // Use the context distributor passed to us @@ -194,6 +198,17 @@ public final class ContextAlbumImpl implements ContextAlbum { * (non-Javadoc) * * @see + * org.onap.policy.apex.context.ContextAlbum#getUserArtifactStack() + */ + @Override + public AxConcept[] getUserArtifactStack() { + return userArtifactStack; + } + + /* + * (non-Javadoc) + * + * @see * org.onap.policy.apex.context.ContextAlbum#setUserArtifactStack(org.onap.policy.apex.model.basicmodel.concepts. * AxConcept []) */ @@ -440,7 +455,7 @@ public final class ContextAlbumImpl implements ContextAlbum { public Object remove(final Object key) { if (!albumDefinition.isWritable()) { final String returnString = "album \"" + albumDefinition.getID() - + "\" remove() not allowed on read only albums for key=\"" + key; + + "\" remove() not allowed on read only albums for key=\"" + key + "\""; LOGGER.warn(returnString); throw new ContextRuntimeException(returnString); } diff --git a/context/context-management/src/test/java/org/onap/policy/apex/context/ContextExceptionTest.java b/context/context-management/src/test/java/org/onap/policy/apex/context/ContextExceptionTest.java new file mode 100644 index 000000000..15560f7cc --- /dev/null +++ b/context/context-management/src/test/java/org/onap/policy/apex/context/ContextExceptionTest.java @@ -0,0 +1,41 @@ +/*- + * ============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; + +import static org.junit.Assert.assertEquals; + +import java.io.IOException; + +import org.junit.Test; + +public class ContextExceptionTest { + + @Test + public void testContextException() { + assertEquals("context exception message", new ContextException("context exception message").getMessage()); + assertEquals("context exception message", new ContextException("context exception message", new IOException()).getMessage()); + } + + @Test + public void testContextRuntimeException() { + assertEquals("context exception message", new ContextRuntimeException("context exception message").getMessage()); + assertEquals("context exception message", new ContextRuntimeException("context exception message", new IOException()).getMessage()); + } +} 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 new file mode 100644 index 000000000..e5cc61fad --- /dev/null +++ b/context/context-management/src/test/java/org/onap/policy/apex/context/impl/ContextAlbumImplTest.java @@ -0,0 +1,288 @@ +/*- + * ============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 static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.util.LinkedHashMap; +import java.util.Map; + +import org.junit.BeforeClass; +import org.junit.Test; +import org.onap.policy.apex.context.ContextAlbum; +import org.onap.policy.apex.context.ContextException; +import org.onap.policy.apex.context.ContextRuntimeException; +import org.onap.policy.apex.context.Distributor; +import org.onap.policy.apex.context.impl.distribution.jvmlocal.JVMLocalDistributor; +import org.onap.policy.apex.context.impl.schema.java.JavaSchemaHelperParameters; +import org.onap.policy.apex.context.parameters.ContextParameters; +import org.onap.policy.apex.context.parameters.SchemaParameters; +import org.onap.policy.apex.model.basicmodel.concepts.ApexRuntimeException; +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.basicmodel.service.ParameterService; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbum; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas; + +public class ContextAlbumImplTest { + @BeforeClass + public static void prepareForTest() { + final ContextParameters contextParameters = new ContextParameters(); + contextParameters.getLockManagerParameters() + .setPluginClass("org.onap.policy.apex.context.impl.locking.jvmlocal.JVMLocalLockManager"); + ParameterService.registerParameters(ContextParameters.class, contextParameters); + + final SchemaParameters schemaParameters = new SchemaParameters(); + schemaParameters.getSchemaHelperParameterMap().put("JAVA", new JavaSchemaHelperParameters()); + ParameterService.registerParameters(SchemaParameters.class, schemaParameters); + } + + @Test + public void testNullsOnConstructor() { + try { + new ContextAlbumImpl(null, null, null); + fail("this test should throw an exception"); + } catch (IllegalArgumentException e) { + assertEquals("Context album definition may not be null", e.getMessage()); + } catch (ContextException e) { + fail("this test should throw an IllegalArgumentException"); + } + + try { + new ContextAlbumImpl(new AxContextAlbum(), null, null); + fail("this test should throw an exception"); + } catch (IllegalArgumentException e) { + assertEquals("Distributor may not be null", e.getMessage()); + } catch (ContextException e) { + fail("this test should throw an IllegalArgumentException"); + } + + try { + new ContextAlbumImpl(new AxContextAlbum(), new JVMLocalDistributor(), null); + fail("this test should throw an exception"); + } catch (IllegalArgumentException e) { + assertEquals("Album map may not be null", e.getMessage()); + } catch (ContextException e) { + fail("this test should throw an IllegalArgumentException"); + } + + try { + new ContextAlbumImpl(new AxContextAlbum(), new JVMLocalDistributor(), new LinkedHashMap<String, Object>()); + 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()); + } catch (ContextException e) { + fail("this test should throw an ApexRuntimeException"); + } + } + + @Test + public void testAlbumInterface() throws ContextException { + AxContextSchemas schemas = new AxContextSchemas(); + 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 axContextAlbumRO = new AxContextAlbum(new AxArtifactKey("TestContextAlbum", "0.0.1"), "Policy", + false, simpleStringSchema.getKey()); + + try { + new ContextAlbumImpl(axContextAlbum, new JVMLocalDistributor(), new LinkedHashMap<String, Object>()); + fail("this test should throw an exception"); + } catch (ContextException e) { + assertEquals("could not initiate schema management for context album AxContextAlbum", + e.getMessage().substring(0, 69)); + } + + axContextAlbum.setItemSchema(simpleStringSchema.getKey()); + Distributor distributor = new JVMLocalDistributor(); + distributor.init(axContextAlbum.getKey()); + ContextAlbum album = new ContextAlbumImpl(axContextAlbum, distributor, new LinkedHashMap<String, Object>()); + ContextAlbum albumRO = new ContextAlbumImpl(axContextAlbumRO, distributor, new LinkedHashMap<String, Object>()); + + assertEquals("TestContextAlbum", album.getName()); + assertEquals("TestContextAlbum:0.0.1", album.getKey().getID()); + assertEquals("TestContextAlbum:0.0.1", album.getAlbumDefinition().getID()); + assertEquals("SimpleStringSchema:0.0.1", album.getSchemaHelper().getSchema().getID()); + + try { + album.containsKey(null); + fail("test should throw an exception"); + } catch (ContextRuntimeException e) { + assertEquals("null values are illegal on method parameter \"key\"", e.getMessage()); + } + assertEquals(false, album.containsKey("Key0")); + + try { + album.containsValue(null); + fail("test should throw an exception"); + } catch (ContextRuntimeException e) { + assertEquals("null values are illegal on method parameter \"value\"", e.getMessage()); + } + assertEquals(false, album.containsValue("some value")); + + try { + album.get(null); + fail("test should throw an exception"); + } catch (ContextRuntimeException e) { + assertEquals("album \"TestContextAlbum:0.0.1\" null keys are illegal on keys for get()", e.getMessage()); + } + + try { + album.put(null, null); + fail("test should throw an exception"); + } catch (ContextRuntimeException e) { + assertEquals("album \"TestContextAlbum:0.0.1\" null keys are illegal on keys for put()", e.getMessage()); + } + + try { + album.put("KeyNull", null); + fail("test should throw an exception"); + } catch (ContextRuntimeException e) { + assertEquals("album \"TestContextAlbum:0.0.1\" null values are illegal on key \"KeyNull\" for put()", + e.getMessage()); + } + + try { + albumRO.put("KeyReadOnly", "A value for a Read Only Album"); + fail("test should throw an exception"); + } catch (ContextRuntimeException e) { + assertEquals("album \"TestContextAlbum:0.0.1\" put() not allowed on read only albums " + + "for key=\"KeyReadOnly\", value=\"A value for a Read Only Album", e.getMessage()); + } + + Map<String, Object> putAllData = new LinkedHashMap<>(); + putAllData.put("AllKey0", "vaue of AllKey0"); + putAllData.put("AllKey1", "vaue of AllKey1"); + putAllData.put("AllKey2", "vaue of AllKey2"); + + try { + albumRO.putAll(putAllData); + fail("test should throw an exception"); + } catch (ContextRuntimeException e) { + assertEquals("album \"TestContextAlbum:0.0.1\" putAll() not allowed on read only albums", e.getMessage()); + } + + try { + 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()); + } + + try { + album.remove(null); + fail("test should throw an exception"); + } catch (ContextRuntimeException e) { + assertEquals("null values are illegal on method parameter \"keyID\"", e.getMessage()); + } + + try { + albumRO.clear(); + fail("test should throw an exception"); + } catch (ContextRuntimeException e) { + assertEquals("album \"TestContextAlbum:0.0.1\" clear() not allowed on read only albums", e.getMessage()); + } + + // The following locking tests pass because the locking protects access to Key0 across all + // copies of the distributed album whether the key exists or not + album.lockForReading("Key0"); + assertEquals(null, album.get("Key0")); + album.unlockForReading("Key0"); + assertEquals(null, album.get("Key0")); + + album.lockForWriting("Key0"); + assertEquals(null, album.get("Key0")); + album.unlockForWriting("Key0"); + assertEquals(null, album.get("Key0")); + + // Test write access, trivial test because Integration Test does + // a full test of access locking over albums in different JVMs + album.lockForWriting("Key0"); + assertEquals(null, album.get("Key0")); + album.put("Key0", "value of Key0"); + assertEquals("value of Key0", album.get("Key0")); + album.unlockForWriting("Key0"); + assertEquals("value of Key0", album.get("Key0")); + + // Test read access, trivial test because Integration Test does + // a full test of access locking over albums in different JVMs + album.lockForReading("Key0"); + assertEquals("value of Key0", album.get("Key0")); + album.unlockForReading("Key0"); + + AxArtifactKey somePolicyKey = new AxArtifactKey("MyPolicy", "0.0.1"); + AxReferenceKey somePolicyState = new AxReferenceKey(somePolicyKey, "SomeState"); + + 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()); + + assertEquals(true, album.keySet().contains("Key0")); + assertEquals(true, album.values().contains("value of Key0")); + assertEquals(1, album.entrySet().size()); + + // The flush() operation fails because the distributor is not initialized with the album which + // is fine for unit test + try { + album.flush(); + fail("test should throw an exception"); + } catch (ContextException e) { + assertEquals("map flush failed, supplied map is null", e.getMessage()); + } + + assertEquals(1, album.size()); + assertEquals(false, album.isEmpty()); + + album.put("Key0", "New value of Key0"); + assertEquals("New value of Key0", album.get("Key0")); + + album.putAll(putAllData); + + putAllData.put("AllKey3", null); + try { + album.putAll(putAllData); + fail("test should throw an exception"); + } catch (ContextRuntimeException e) { + assertEquals("album \"TestContextAlbum:0.0.1\" null values are illegal on key \"AllKey3\" for put()", + e.getMessage()); + } + + assertEquals("New value of Key0", album.remove("Key0")); + + album.clear(); + assertTrue(album.isEmpty()); + + ModelService.clear(); + } +} |