diff options
Diffstat (limited to 'context/context-management')
9 files changed, 409 insertions, 55 deletions
diff --git a/context/context-management/pom.xml b/context/context-management/pom.xml index 4754ff8dc..472bcfd35 100644 --- a/context/context-management/pom.xml +++ b/context/context-management/pom.xml @@ -23,7 +23,7 @@ <parent> <groupId>org.onap.policy.apex-pdp.context</groupId> <artifactId>context</artifactId> - <version>4.0.0-SNAPSHOT</version> + <version>4.0.1-SNAPSHOT</version> </parent> <artifactId>context-management</artifactId> 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 e45c47952..096e68889 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 @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2019 Nordix Foundation. + * Modifications Copyright (C) 2019, 2024 Nordix Foundation. * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -30,6 +30,7 @@ import java.util.Map; import java.util.Set; import lombok.EqualsAndHashCode; import lombok.Getter; +import lombok.NonNull; import lombok.Setter; import org.onap.policy.apex.context.ContextAlbum; import org.onap.policy.apex.context.ContextException; @@ -88,12 +89,12 @@ public final class ContextAlbumImpl implements ContextAlbum, Comparable<ContextA * Constructor, instantiate the context album. * * @param albumDefinition The model definition of this context album - * @param distributor The context distributor passed to us to distribute context across ContextAlbum instances - * @param albumMap the album map + * @param distributor The context distributor passed to us to distribute context across ContextAlbum instances + * @param albumMap the album map * @throws ContextException on errors creating context albums */ public ContextAlbumImpl(final AxContextAlbum albumDefinition, final Distributor distributor, - final Map<String, Object> albumMap) throws ContextException { + 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"); @@ -109,7 +110,7 @@ public final class ContextAlbumImpl implements ContextAlbum, Comparable<ContextA try { // Get a schema helper to manage the translations between objects on the album map for this album schemaHelper = new SchemaHelperFactory().createSchemaHelper(albumDefinition.getKey(), - albumDefinition.getItemSchema()); + albumDefinition.getItemSchema()); } catch (final ContextRuntimeException e) { final var resultString = "could not initiate schema management for context album " + albumDefinition; LOGGER.warn(resultString, e); @@ -144,7 +145,7 @@ public final class ContextAlbumImpl implements ContextAlbum, Comparable<ContextA public void lockForReading(final String keyOnAlbum) throws ContextException { distributor.lockForReading(albumDefinition.getKey(), keyOnAlbum); monitor.monitorReadLock(albumDefinition.getKey(), albumDefinition.getItemSchema(), keyOnAlbum, - userArtifactStack); + userArtifactStack); } /** @@ -154,7 +155,7 @@ public final class ContextAlbumImpl implements ContextAlbum, Comparable<ContextA public void lockForWriting(final String keyOnAlbum) throws ContextException { distributor.lockForWriting(albumDefinition.getKey(), keyOnAlbum); monitor.monitorWriteLock(albumDefinition.getKey(), albumDefinition.getItemSchema(), keyOnAlbum, - userArtifactStack); + userArtifactStack); } /** @@ -164,7 +165,7 @@ public final class ContextAlbumImpl implements ContextAlbum, Comparable<ContextA public void unlockForReading(final String keyOnAlbum) throws ContextException { distributor.unlockForReading(albumDefinition.getKey(), keyOnAlbum); monitor.monitorReadUnlock(albumDefinition.getKey(), albumDefinition.getItemSchema(), keyOnAlbum, - userArtifactStack); + userArtifactStack); } /** @@ -174,7 +175,7 @@ public final class ContextAlbumImpl implements ContextAlbum, Comparable<ContextA public void unlockForWriting(final String keyOnAlbum) throws ContextException { distributor.unlockForWriting(albumDefinition.getKey(), keyOnAlbum); monitor.monitorWriteUnlock(albumDefinition.getKey(), albumDefinition.getItemSchema(), keyOnAlbum, - userArtifactStack); + userArtifactStack); } /** @@ -238,7 +239,7 @@ public final class ContextAlbumImpl implements ContextAlbum, Comparable<ContextA public Object get(final Object key) { if (key == null) { final var returnString = - ALBUM + albumDefinition.getId() + "\" null keys are illegal on keys for get()"; + ALBUM + albumDefinition.getId() + "\" null keys are illegal on keys for get()"; LOGGER.warn(returnString); throw new ContextRuntimeException(returnString); } @@ -250,7 +251,7 @@ public final class ContextAlbumImpl implements ContextAlbum, Comparable<ContextA // Get the context value and monitor it monitor.monitorGet(albumDefinition.getKey(), albumDefinition.getItemSchema(), key.toString(), item, - userArtifactStack); + userArtifactStack); return item; } @@ -273,7 +274,7 @@ public final class ContextAlbumImpl implements ContextAlbum, Comparable<ContextA for (final Entry<String, Object> contextAlbumEntry : albumMap.entrySet()) { final Object item = contextAlbumEntry.getValue(); monitor.monitorGet(albumDefinition.getKey(), albumDefinition.getItemSchema(), contextAlbumEntry.getKey(), - item, userArtifactStack); + item, userArtifactStack); valueList.add(contextAlbumEntry.getValue()); } @@ -291,7 +292,7 @@ public final class ContextAlbumImpl implements ContextAlbum, Comparable<ContextA for (final Entry<String, Object> contextAlbumEntry : albumMap.entrySet()) { final Object item = contextAlbumEntry.getValue(); monitor.monitorGet(albumDefinition.getKey(), albumDefinition.getItemSchema(), contextAlbumEntry.getKey(), - item, userArtifactStack); + item, userArtifactStack); entrySet.add(new SimpleEntry<>(contextAlbumEntry.getKey(), contextAlbumEntry.getValue())); } @@ -305,21 +306,21 @@ public final class ContextAlbumImpl implements ContextAlbum, Comparable<ContextA public Object put(final String key, final Object incomingValue) { if (key == null) { final var returnString = - ALBUM + albumDefinition.getId() + "\" null keys are illegal on keys for put()"; + ALBUM + albumDefinition.getId() + "\" null keys are illegal on keys for put()"; LOGGER.warn(returnString); throw new ContextRuntimeException(returnString); } if (incomingValue == null) { final var returnString = ALBUM + albumDefinition.getId() + "\" null values are illegal on key \"" - + key + "\" for put()"; + + key + "\" for put()"; LOGGER.warn(returnString); throw new ContextRuntimeException(returnString); } if (!albumDefinition.isWritable()) { final var returnString = ALBUM + albumDefinition.getId() - + "\" put() not allowed on read only albums for key=\"" + key + "\", value=\"" + incomingValue; + + "\" put() not allowed on read only albums for key=\"" + key + "\", value=\"" + incomingValue; LOGGER.warn(returnString); throw new ContextRuntimeException(returnString); } @@ -332,18 +333,18 @@ public final class ContextAlbumImpl implements ContextAlbum, Comparable<ContextA if (albumMap.containsKey(key)) { // Update the value in the context item and in the context value map monitor.monitorSet(albumDefinition.getKey(), albumDefinition.getItemSchema(), key, incomingValue, - userArtifactStack); + userArtifactStack); } else { // Update the value in the context item and in the context value map monitor.monitorInit(albumDefinition.getKey(), albumDefinition.getItemSchema(), key, incomingValue, - userArtifactStack); + userArtifactStack); } // Put the translated value on the map and return the old map value return albumMap.put(key, valueToPut); } catch (final ContextRuntimeException e) { final var returnString = "Failed to set context value for key \"" + key + "\" in album \"" - + albumDefinition.getId() + "\": " + e.getMessage(); + + albumDefinition.getId() + "\": " + e.getMessage(); LOGGER.warn(returnString); throw new ContextRuntimeException(returnString, e); } @@ -353,17 +354,17 @@ public final class ContextAlbumImpl implements ContextAlbum, Comparable<ContextA * {@inheritDoc}. */ @Override - public void putAll(final Map<? extends String, ? extends Object> incomingContextAlbum) { + public void putAll(@NonNull final Map<? extends String, ?> incomingContextAlbum) { if (!albumDefinition.isWritable()) { final var returnString = - ALBUM + albumDefinition.getId() + "\" putAll() not allowed on read only albums"; + ALBUM + albumDefinition.getId() + "\" putAll() not allowed on read only albums"; LOGGER.warn(returnString); throw new ContextRuntimeException(returnString); } // Sanity check on incoming context Assertions.argumentOfClassNotNull(incomingContextAlbum, ContextRuntimeException.class, - "cannot update context, context album is null"); + "cannot update context, context album is null"); // Iterate over the incoming context for (final Entry<String, Object> entry : albumMap.entrySet()) { @@ -378,7 +379,7 @@ public final class ContextAlbumImpl implements ContextAlbum, Comparable<ContextA } // Put all the objects on the context album - for (final Entry<? extends String, ? extends Object> incomingMapEntry : incomingContextAlbum.entrySet()) { + for (final Entry<? extends String, ?> incomingMapEntry : incomingContextAlbum.entrySet()) { // NOSONAR // Put the entry on the map this.put(incomingMapEntry.getKey(), incomingMapEntry.getValue()); } @@ -391,7 +392,7 @@ public final class ContextAlbumImpl implements ContextAlbum, Comparable<ContextA public Object remove(final Object key) { if (!albumDefinition.isWritable()) { final var 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); } @@ -404,7 +405,7 @@ public final class ContextAlbumImpl implements ContextAlbum, Comparable<ContextA // Delete the item final Object removedValue = albumMap.remove(key); monitor.monitorDelete(albumDefinition.getKey(), albumDefinition.getItemSchema(), key.toString(), removedValue, - userArtifactStack); + userArtifactStack); // Return the value of the deleted item return removedValue; @@ -417,7 +418,7 @@ public final class ContextAlbumImpl implements ContextAlbum, Comparable<ContextA public void clear() { if (!albumDefinition.isWritable()) { final var returnString = - ALBUM + albumDefinition.getId() + "\" clear() not allowed on read only albums"; + ALBUM + albumDefinition.getId() + "\" clear() not allowed on read only albums"; LOGGER.warn(returnString); throw new ContextRuntimeException(returnString); } @@ -426,7 +427,7 @@ public final class ContextAlbumImpl implements ContextAlbum, Comparable<ContextA for (final Entry<String, Object> contextAlbumEntry : albumMap.entrySet()) { final Object item = contextAlbumEntry.getValue(); monitor.monitorDelete(albumDefinition.getKey(), albumDefinition.getItemSchema(), contextAlbumEntry.getKey(), - item, userArtifactStack); + item, userArtifactStack); } // Clear the map diff --git a/context/context-management/src/main/java/org/onap/policy/apex/context/impl/schema/SchemaHelperFactory.java b/context/context-management/src/main/java/org/onap/policy/apex/context/impl/schema/SchemaHelperFactory.java index 08289e642..3602ce12a 100644 --- a/context/context-management/src/main/java/org/onap/policy/apex/context/impl/schema/SchemaHelperFactory.java +++ b/context/context-management/src/main/java/org/onap/policy/apex/context/impl/schema/SchemaHelperFactory.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2019-2020 Nordix Foundation. + * Modifications Copyright (C) 2019-2020, 2024 Nordix Foundation. * Modifications Copyright (C) 2021 Bell Canada. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -84,7 +84,7 @@ public class SchemaHelperFactory { } // Get the class for the schema helper using reflection - Object schemaHelperObject = null; + Object schemaHelperObject; final String pluginClass = schemaHelperParameters.getSchemaHelperPluginClass(); try { schemaHelperObject = Class.forName(pluginClass).getDeclaredConstructor().newInstance(); @@ -96,7 +96,7 @@ public class SchemaHelperFactory { } // Check the class is a schema helper - if (!(schemaHelperObject instanceof SchemaHelper)) { + if (!(schemaHelperObject instanceof SchemaHelper schemaHelper)) { final var resultString = "Specified Apex context schema helper plugin class \"" + pluginClass + "\" does not implement the SchemaHelper interface"; LOGGER.warn(resultString); @@ -104,7 +104,6 @@ public class SchemaHelperFactory { } // The context schema helper to return - final var schemaHelper = (SchemaHelper) schemaHelperObject; // Lock and load the schema helper schemaHelper.init(owningEntityKey.getKey(), schema); diff --git a/context/context-management/src/main/java/org/onap/policy/apex/context/impl/schema/java/JavaSchemaHelper.java b/context/context-management/src/main/java/org/onap/policy/apex/context/impl/schema/java/JavaSchemaHelper.java index f601062bb..b1a1d36df 100644 --- a/context/context-management/src/main/java/org/onap/policy/apex/context/impl/schema/java/JavaSchemaHelper.java +++ b/context/context-management/src/main/java/org/onap/policy/apex/context/impl/schema/java/JavaSchemaHelper.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2019-2020 Nordix Foundation. + * Modifications Copyright (C) 2019-2020, 2024 Nordix Foundation. * Modifications Copyright (C) 2021 Bell Canada. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -49,7 +49,7 @@ public class JavaSchemaHelper extends AbstractSchemaHelper { // Get a reference to the logger private static final XLogger LOGGER = XLoggerFactory.getXLogger(JavaSchemaHelper.class); - // This map defines the built in types in types in Java + // This map defines the built-in types in Java // @formatter:off private static final Map<String, Class<?>> BUILT_IN_MAP = new HashMap<>(); @@ -81,7 +81,7 @@ public class JavaSchemaHelper extends AbstractSchemaHelper { } catch (final Exception e) { String resultSting = userKey.getId() + ": class/type " + schema.getSchema() + " for context schema \"" - + schema.getId() + "\" not found."; + + schema.getId() + "\" not found."; if (JavaSchemaHelper.BUILT_IN_MAP.get(javatype) != null) { resultSting += " Primitive types are not supported. Use the appropriate Java boxing type instead."; } else { @@ -103,13 +103,13 @@ public class JavaSchemaHelper extends AbstractSchemaHelper { if (getSchemaClass() == null) { final var returnString = - getUserKey().getId() + ": could not create an instance, schema class for the schema is null"; + getUserKey().getId() + ": could not create an instance, schema class for the schema is null"; LOGGER.warn(returnString); throw new ContextRuntimeException(returnString); } - if (incomingObject instanceof JsonElement) { - final var elementJsonString = getGson().toJson((JsonElement) incomingObject); + if (incomingObject instanceof JsonElement jsonObject) { + final var elementJsonString = getGson().toJson(jsonObject); return getGson().fromJson(elementJsonString, this.getSchemaClass()); } @@ -118,8 +118,8 @@ public class JavaSchemaHelper extends AbstractSchemaHelper { } final var returnString = getUserKey().getId() + ": the object \"" + incomingObject + "\" of type \"" - + incomingObject.getClass().getName() - + "\" is not an instance of JsonObject and is not assignable to \"" + getSchemaClass().getName() + "\""; + + incomingObject.getClass().getName() + + "\" is not an instance of JsonObject and is not assignable to \"" + getSchemaClass().getName() + "\""; LOGGER.warn(returnString); throw new ContextRuntimeException(returnString); } @@ -165,8 +165,8 @@ public class JavaSchemaHelper extends AbstractSchemaHelper { return getGson().toJson(schemaObject); } else { final var returnString = getUserKey().getId() + ": object \"" + schemaObject.toString() - + "\" of class \"" + schemaObject.getClass().getName() + "\" not compatible with class \"" - + getSchemaClass().getName() + "\""; + + "\" of class \"" + schemaObject.getClass().getName() + "\" not compatible with class \"" + + getSchemaClass().getName() + "\""; LOGGER.warn(returnString); throw new ContextRuntimeException(returnString); } @@ -189,19 +189,19 @@ public class JavaSchemaHelper extends AbstractSchemaHelper { */ private Object numericConversion(final Object object) { // Check if the incoming object is a number, if not do a string conversion - if (object instanceof Number) { + if (object instanceof Number myNumber) { if (getSchemaClass().isAssignableFrom(Byte.class)) { - return ((Number) object).byteValue(); + return myNumber.byteValue(); } else if (getSchemaClass().isAssignableFrom(Short.class)) { - return ((Number) object).shortValue(); + return myNumber.shortValue(); } else if (getSchemaClass().isAssignableFrom(Integer.class)) { - return ((Number) object).intValue(); + return myNumber.intValue(); } else if (getSchemaClass().isAssignableFrom(Long.class)) { - return ((Number) object).longValue(); + return myNumber.longValue(); } else if (getSchemaClass().isAssignableFrom(Float.class)) { - return ((Number) object).floatValue(); + return myNumber.floatValue(); } else if (getSchemaClass().isAssignableFrom(Double.class)) { - return ((Number) object).doubleValue(); + return myNumber.doubleValue(); } } @@ -222,8 +222,8 @@ public class JavaSchemaHelper extends AbstractSchemaHelper { return stringConstructor.newInstance(object.toString()); } catch (final Exception e) { final var returnString = getUserKey().getId() + ": object \"" + object.toString() + "\" of class \"" - + object.getClass().getName() + "\" not compatible with class \"" + getSchemaClass().getName() - + "\""; + + object.getClass().getName() + "\" not compatible with class \"" + getSchemaClass().getName() + + "\""; LOGGER.warn(returnString, e); throw new ContextRuntimeException(returnString); } @@ -241,22 +241,22 @@ public class JavaSchemaHelper extends AbstractSchemaHelper { var schemaParameters = (SchemaParameters) ParameterService.get(ContextParameterConstants.SCHEMA_GROUP_NAME); JavaSchemaHelperParameters javaSchemaHelperParmeters = - (JavaSchemaHelperParameters) schemaParameters.getSchemaHelperParameterMap().get("Java"); + (JavaSchemaHelperParameters) schemaParameters.getSchemaHelperParameterMap().get("Java"); if (javaSchemaHelperParmeters == null) { javaSchemaHelperParmeters = new JavaSchemaHelperParameters(); } for (JavaSchemaHelperJsonAdapterParameters jsonAdapterEntry : javaSchemaHelperParmeters.getJsonAdapters() - .values()) { + .values()) { Object adapterObject; try { adapterObject = jsonAdapterEntry.getAdaptorClazz().getDeclaredConstructor().newInstance(); } catch (Exception e) { final var returnString = getUserKey().getId() + ": instantiation of adapter class \"" - + jsonAdapterEntry.getAdaptorClass() + "\" to decode and encode class \"" - + jsonAdapterEntry.getAdaptedClass() + "\" failed: " + e.getMessage(); + + jsonAdapterEntry.getAdaptorClass() + "\" to decode and encode class \"" + + jsonAdapterEntry.getAdaptedClass() + "\" failed: " + e.getMessage(); LOGGER.warn(returnString, e); throw new ContextRuntimeException(returnString); } 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 216e3686a..5b1f3ee7f 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.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -40,6 +41,8 @@ import org.onap.policy.apex.context.impl.distribution.jvmlocal.JvmLocalDistribut import org.onap.policy.apex.context.impl.schema.java.JavaSchemaHelperParameters; import org.onap.policy.apex.context.parameters.ContextParameterConstants; import org.onap.policy.apex.context.parameters.ContextParameters; +import org.onap.policy.apex.context.parameters.DistributorParameters; +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.basicmodel.concepts.AxConcept; @@ -65,6 +68,7 @@ class ContextAlbumImplTest { contextParameters.getDistributorParameters().setName(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME); contextParameters.getLockManagerParameters().setName(ContextParameterConstants.LOCKING_GROUP_NAME); contextParameters.getPersistorParameters().setName(ContextParameterConstants.PERSISTENCE_GROUP_NAME); + contextParameters.getPersistorParameters().setFlushPeriod(30000); ParameterService.register(contextParameters); ParameterService.register(contextParameters.getDistributorParameters()); @@ -295,11 +299,53 @@ class ContextAlbumImplTest { assertThatThrownBy(album::flush).hasMessage("map flush failed, supplied map is null"); AxContextAlbums albums = new AxContextAlbums(); ModelService.registerModel(AxContextAlbums.class, albums); + + assertThatThrownBy(() -> { + distributor.createContextAlbum(new AxContextAlbums().getKey()); + }).hasMessage("context album NULL:0.0.0 does not exist"); + albums.getAlbumsMap().put(axContextAlbum.getKey(), axContextAlbum); distributor.createContextAlbum(album.getKey()); + axContextAlbum.setItemSchema(new AxArtifactKey()); + albums.getAlbumsMap().put(axContextAlbum.getKey(), axContextAlbum); + assertThatThrownBy(() -> { + distributor.createContextAlbum(album.getKey()); + }).hasMessageContaining("context album definition for TestContextAlbum:0.0.1 is invalid"); + + axContextAlbum.setItemSchema(new AxArtifactKey("invalid", "0.0.1")); + albums.getAlbumsMap().put(axContextAlbum.getKey(), axContextAlbum); + assertThatThrownBy(() -> { + distributor.createContextAlbum(album.getKey()); + }).hasMessage("schema \"invalid:0.0.1\" for context album TestContextAlbum:0.0.1 does not exist"); + album.flush(); ModelService.clear(); } + + @Test + void testParametersToString() { + assertThat(ParameterService.get(ContextParameterConstants.MAIN_GROUP_NAME).toString()) + .isInstanceOf(String.class); + assertThat(ParameterService.get(ContextParameterConstants.SCHEMA_GROUP_NAME).toString()) + .isInstanceOf(String.class); + } + + @Test + void testParameterSetter() { + var parameters = new PersistorParameters(); + parameters.setFlushPeriod(0); + parameters.setPluginClass("TestPlugin.class"); + assertEquals(300000, parameters.getFlushPeriod()); + + var distributorParameters = new DistributorParameters(); + distributorParameters.setName("test"); + distributorParameters.setPluginClass("TestPlugin.class"); + assertEquals("test", distributorParameters.getName()); + + var contextParameters = new ContextParameters(); + contextParameters.setDistributorParameters(distributorParameters); + assertEquals("test", contextParameters.getDistributorParameters().getName()); + } } diff --git a/context/context-management/src/test/java/org/onap/policy/apex/context/impl/LockManagerTest.java b/context/context-management/src/test/java/org/onap/policy/apex/context/impl/LockManagerTest.java new file mode 100644 index 000000000..2aac635a1 --- /dev/null +++ b/context/context-management/src/test/java/org/onap/policy/apex/context/impl/LockManagerTest.java @@ -0,0 +1,75 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2024 Nordix Foundation. 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.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import org.junit.jupiter.api.Test; +import org.onap.policy.apex.context.ContextException; +import org.onap.policy.apex.context.impl.distribution.jvmlocal.JvmLocalDistributor; +import org.onap.policy.apex.context.impl.locking.LockManagerFactory; +import org.onap.policy.apex.context.impl.locking.jvmlocal.JvmLocalLockManager; +import org.onap.policy.apex.context.parameters.ContextParameterConstants; +import org.onap.policy.apex.context.parameters.LockManagerParameters; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.common.parameters.ParameterService; + +class LockManagerTest { + + @Test + void testLock() { + var lockManager = new JvmLocalLockManager(); + assertDoesNotThrow(() -> lockManager.lockForReading("test", "test")); + assertDoesNotThrow(() -> lockManager.unlockForReading("test", "test")); + assertThrows(ContextException.class, () -> lockManager.unlockForReading("test", "test")); + assertDoesNotThrow(() -> lockManager.lockForWriting("test", "test")); + assertDoesNotThrow(() -> lockManager.unlockForWriting("test", "test")); + assertThrows(ContextException.class, () -> lockManager.unlockForWriting("test", "test")); + } + + @Test + void testShutDown() { + var lockManager = new JvmLocalLockManager(); + assertDoesNotThrow(lockManager::shutdown); + } + + @Test + void testCreateLockManager() { + var lockManagerFactory = new LockManagerFactory(); + var parameters = new LockManagerParameters(); + parameters.setPluginClass("invalid.class"); + ParameterService.register(parameters); + assertThrows(ContextException.class, () -> lockManagerFactory.createLockManager(new AxArtifactKey())); + ParameterService.deregister(ContextParameterConstants.LOCKING_GROUP_NAME); + + // different plugin instance + parameters.setPluginClass(JvmLocalDistributor.class.getName()); + ParameterService.register(parameters); + assertThrows(ContextException.class, () -> lockManagerFactory.createLockManager(new AxArtifactKey())); + ParameterService.deregister(ContextParameterConstants.LOCKING_GROUP_NAME); + + parameters.setPluginClass(LockManagerParameters.DEFAULT_LOCK_MANAGER_PLUGIN_CLASS); + ParameterService.register(parameters); + assertDoesNotThrow(() -> lockManagerFactory.createLockManager(new AxArtifactKey())); + ParameterService.deregister(ContextParameterConstants.LOCKING_GROUP_NAME); + } +} diff --git a/context/context-management/src/test/java/org/onap/policy/apex/context/impl/PersistorTest.java b/context/context-management/src/test/java/org/onap/policy/apex/context/impl/PersistorTest.java new file mode 100644 index 000000000..277598b4a --- /dev/null +++ b/context/context-management/src/test/java/org/onap/policy/apex/context/impl/PersistorTest.java @@ -0,0 +1,84 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2024 Nordix Foundation. 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.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.util.TreeSet; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.Test; +import org.onap.policy.apex.context.ContextException; +import org.onap.policy.apex.context.impl.distribution.jvmlocal.JvmLocalDistributor; +import org.onap.policy.apex.context.impl.persistence.PersistorFactory; +import org.onap.policy.apex.context.impl.persistence.ephemeral.EphemeralPersistor; +import org.onap.policy.apex.context.parameters.ContextParameterConstants; +import org.onap.policy.apex.context.parameters.PersistorParameters; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey; +import org.onap.policy.common.parameters.ParameterService; + + +class PersistorTest { + + @AfterAll + public static void cleanUpAfterTest() { + ParameterService.deregister(ContextParameterConstants.PERSISTENCE_GROUP_NAME); + ParameterService.clear(); + } + + @Test + void testContextItemRead() throws ContextException { + var persistor = new EphemeralPersistor(); + assertNull(persistor.readContextItem(new AxReferenceKey(), null)); + assertThat(persistor.readContextItems(null, null)).isInstanceOf(TreeSet.class); + persistor.init(new AxArtifactKey("testkey", "1.0.0")); + assertEquals("testkey", persistor.getKey().getName()); + } + + @Test + void testWriteContextItem() { + var persistor = new EphemeralPersistor(); + assertThat(persistor.writeContextItem(new Object())).isInstanceOf(Object.class); + } + + @Test + void testPersistorFactory() { + var factory = new PersistorFactory(); + PersistorParameters params = new PersistorParameters(); + params.setPluginClass("invalid.class"); + ParameterService.register(params); + assertThrows(ContextException.class, () -> factory.createPersistor(new AxArtifactKey())); + ParameterService.deregister(ContextParameterConstants.PERSISTENCE_GROUP_NAME); + + params.setPluginClass(JvmLocalDistributor.class.getName()); + ParameterService.register(params); + assertThrows(ContextException.class, () -> factory.createPersistor(new AxArtifactKey())); + ParameterService.deregister(ContextParameterConstants.PERSISTENCE_GROUP_NAME); + + params.setPluginClass(PersistorParameters.DEFAULT_PERSISTOR_PLUGIN_CLASS); + ParameterService.register(params); + assertDoesNotThrow(() -> factory.createPersistor(new AxArtifactKey())); + } +} diff --git a/context/context-management/src/test/java/org/onap/policy/apex/context/impl/schema/SchemaHelperFactoryTest.java b/context/context-management/src/test/java/org/onap/policy/apex/context/impl/schema/SchemaHelperFactoryTest.java index 501b76c0a..453a614ab 100644 --- a/context/context-management/src/test/java/org/onap/policy/apex/context/impl/schema/SchemaHelperFactoryTest.java +++ b/context/context-management/src/test/java/org/onap/policy/apex/context/impl/schema/SchemaHelperFactoryTest.java @@ -22,13 +22,16 @@ package org.onap.policy.apex.context.impl.schema; import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; +import java.util.Map; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import org.onap.policy.apex.context.impl.schema.java.JavaSchemaHelperParameters; import org.onap.policy.apex.context.parameters.ContextParameterConstants; +import org.onap.policy.apex.context.parameters.SchemaHelperParameters; import org.onap.policy.apex.context.parameters.SchemaParameters; import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; import org.onap.policy.apex.model.basicmodel.service.ModelService; @@ -86,4 +89,18 @@ class SchemaHelperFactoryTest { .hasMessage("Owner:0.0.1: class/type java.lang.Bad for context schema \"IntSchema:0.0.1\" " + "not found. Check the class path of the JVM"); } + + @Test + void testSchemaHelperParameter() { + var schemaHelperParameters = new SchemaHelperParameters(); + assertNotNull(schemaHelperParameters.toString()); + } + + @Test + void testSchemaParameterSetter() { + var parameters = new SchemaParameters(); + parameters.setName("testSchema"); + parameters.setSchemaHelperParameterMap(Map.of("test", new SchemaHelperParameters())); + assertEquals("testSchema", parameters.getName()); + } } diff --git a/context/context-management/src/test/java/org/onap/policy/apex/context/impl/schema/java/DistributorTest.java b/context/context-management/src/test/java/org/onap/policy/apex/context/impl/schema/java/DistributorTest.java new file mode 100644 index 000000000..5e27be99f --- /dev/null +++ b/context/context-management/src/test/java/org/onap/policy/apex/context/impl/schema/java/DistributorTest.java @@ -0,0 +1,132 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2024 Nordix Foundation. 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.schema.java; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.onap.policy.apex.context.ContextException; +import org.onap.policy.apex.context.impl.distribution.DistributorFactory; +import org.onap.policy.apex.context.impl.distribution.DistributorFlushTimerTask; +import org.onap.policy.apex.context.impl.distribution.jvmlocal.JvmLocalDistributor; +import org.onap.policy.apex.context.parameters.ContextParameterConstants; +import org.onap.policy.apex.context.parameters.ContextParameters; +import org.onap.policy.apex.context.parameters.DistributorParameters; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +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.AxContextModel; +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; + + +class DistributorTest { + + @BeforeAll + public static void prepareForTest() { + final var contextParameters = new ContextParameters(); + contextParameters.getLockManagerParameters() + .setPluginClass("org.onap.policy.apex.context.impl.locking.jvmlocal.JvmLocalLockManager"); + contextParameters.getDistributorParameters().setName(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME); + contextParameters.getLockManagerParameters().setName(ContextParameterConstants.LOCKING_GROUP_NAME); + + ParameterService.register(contextParameters); + ParameterService.register(contextParameters.getDistributorParameters()); + ParameterService.register(contextParameters.getLockManagerParameters()); + ParameterService.register(contextParameters.getPersistorParameters()); + } + + /** + * Clear down the test data. + */ + @AfterAll + public static void cleanUpAfterTest() { + ParameterService.deregister(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME); + ParameterService.deregister(ContextParameterConstants.LOCKING_GROUP_NAME); + ParameterService.deregister(ContextParameterConstants.PERSISTENCE_GROUP_NAME); + ParameterService.clear(); + } + + @Test + void testModelRegister() throws ContextException { + var schemas = new AxContextSchemas(); + var simpleIntSchema = new AxContextSchema(new AxArtifactKey("SimpleIntSchema", "0.0.1"), "JAVA", + "java.lang.Integer"); + schemas.getSchemasMap().put(simpleIntSchema.getKey(), simpleIntSchema); + + var axContextAlbum = new AxContextAlbum(new AxArtifactKey("TestContextAlbum", "0.0.1"), "Policy", + true, AxArtifactKey.getNullKey()); + + axContextAlbum.setItemSchema(simpleIntSchema.getKey()); + var model = new AxContextModel(new AxArtifactKey("TestArtifact", "0.0.1")); + var albums = new AxContextAlbums(); + albums.getAlbumsMap().put(axContextAlbum.getKey(), axContextAlbum); + model.setAlbums(albums); + model.setSchemas(schemas); + var distributor = new JvmLocalDistributor(); + assertDoesNotThrow(() -> distributor.registerModel(model)); + + distributor.flush(); + ModelService.clear(); + } + + @Test + void testDistributorFlushTimer() throws ContextException { + var distributor = new JvmLocalDistributor(); + distributor.init(new AxArtifactKey("test", "0.0.1")); + var timerTask = new DistributorFlushTimerTask(distributor); + assertDoesNotThrow(timerTask::run); + assertDoesNotThrow(timerTask::toString); + assertTrue(timerTask.cancel()); + } + + @Test + void testDistributorFactory() { + var dfactory = new DistributorFactory(); + var axArtifactKey = new AxArtifactKey("testKey", "1.0.1"); + assertDoesNotThrow(() -> dfactory.getDistributor(axArtifactKey)); + ParameterService.deregister(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME); + var params = new DistributorParameters(); + params.setPluginClass("invalid.class"); + ParameterService.register(params); + assertThrows(ContextException.class, () -> dfactory.getDistributor(axArtifactKey)); + ParameterService.deregister(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME); + params.setPluginClass("org.onap.policy.apex.context.impl.persistence.ephemeral.EphemeralPersistor"); + ParameterService.register(params); + assertThrows(ContextException.class, () -> dfactory.getDistributor(axArtifactKey)); + } + + @Test + void testAbstractDistributor() throws ContextException { + var distributor = new JvmLocalDistributor(); + assertThrows(ContextException.class, () -> distributor.removeContextAlbum(new AxArtifactKey())); + assertDoesNotThrow(distributor::flush); + distributor.init(new AxArtifactKey()); + assertDoesNotThrow(distributor::clear); + + } +} |