diff options
Diffstat (limited to 'context/context-management')
5 files changed, 354 insertions, 0 deletions
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); + + } +} |