diff options
author | rameshiyer27 <ramesh.murugan.iyer@est.tech> | 2024-10-14 16:09:34 +0100 |
---|---|---|
committer | rameshiyer27 <ramesh.murugan.iyer@est.tech> | 2024-10-22 21:08:59 +0100 |
commit | 6b164a94d894c969811c9388b6af8c9a1fca2be2 (patch) | |
tree | b62eb7be108d5eb7faa9986c500c30fa7d653594 | |
parent | 85f95e93a19c778e72b6022a713f0eea55002585 (diff) |
Improve coverage on apex-pdp
Issue-ID: POLICY-5059
Signed-off-by: rameshiyer27 <ramesh.murugan.iyer@est.tech>
Change-Id: I3d8d71f4ee9db10fe43bfe51586156cf768a8560
11 files changed, 870 insertions, 15 deletions
diff --git a/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/AnomalyDetectionTaskSelectionTest.java b/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/AnomalyDetectionTaskSelectionTest.java new file mode 100644 index 000000000..0bcf5ac03 --- /dev/null +++ b/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/AnomalyDetectionTaskSelectionTest.java @@ -0,0 +1,179 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (c) 2024 Nordix Foundation. + * ================================================================================ + * 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.examples.adaptive; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.spy; + +import java.util.List; +import java.util.Map; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.onap.policy.apex.context.ContextRuntimeException; +import org.onap.policy.apex.context.impl.ContextAlbumImpl; +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.SchemaParameters; +import org.onap.policy.apex.core.engine.context.ApexInternalContext; +import org.onap.policy.apex.core.engine.event.EnEvent; +import org.onap.policy.apex.core.engine.executor.TaskSelectExecutor; +import org.onap.policy.apex.core.engine.executor.context.TaskSelectionExecutionContext; +import org.onap.policy.apex.examples.adaptive.model.java.AnomalyDetectionPolicyDecideTaskSelectionLogic; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.basicmodel.concepts.AxModel; +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.AxContextAlbums; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas; +import org.onap.policy.apex.model.eventmodel.concepts.AxEvent; +import org.onap.policy.apex.model.eventmodel.concepts.AxEvents; +import org.onap.policy.apex.model.eventmodel.concepts.AxField; +import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; +import org.onap.policy.apex.model.policymodel.concepts.AxState; +import org.onap.policy.apex.model.policymodel.concepts.AxStateTaskReference; +import org.onap.policy.apex.model.policymodel.concepts.AxTask; +import org.onap.policy.apex.model.policymodel.concepts.AxTasks; +import org.onap.policy.common.parameters.ParameterService; +import org.springframework.util.ReflectionUtils; + +class AnomalyDetectionTaskSelectionTest { + + private SchemaParameters schemaParameters; + + + @BeforeEach + void beforeTest() { + schemaParameters = new SchemaParameters(); + schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME); + schemaParameters.getSchemaHelperParameterMap().put("JAVA", new JavaSchemaHelperParameters()); + ParameterService.register(schemaParameters); + } + + /** + * After test. + */ + @AfterEach + void afterTest() { + ParameterService.deregister(schemaParameters); + } + + @Test + void testGetTask() { + var key = new AxArtifactKey(); + var axEvent = new AxEvent(key); + ModelService.registerModel(AxEvent.class, axEvent); + var model = new AxPolicyModel(key); + model.setAlbums(new AxContextAlbums(key)); + ModelService.registerModel(AxModel.class, model); + + var event0Key = new AxArtifactKey("Event0:0.0.1"); + var event0 = new AxEvent(event0Key, "a.name.space", "source", "target"); + var schemas = new AxContextSchemas(event0Key); + var axField = new AxField(); + axField.setSchema(schemas.getKey()); + event0.getParameterMap().put("MonitoredValue", axField); + event0.getParameterMap().put("Iteration", axField); + var events = new AxEvents(); + events.getEventMap().put(event0Key, event0); + + ModelService.registerModel(AxEvents.class, events); + var simpleDoubleSchema = new AxContextSchema(new AxArtifactKey(event0Key), + "JAVA", "java.lang.Double"); + schemas.getSchemasMap().put(simpleDoubleSchema.getKey(), simpleDoubleSchema); + ModelService.registerModel(AxContextSchemas.class, schemas); + + var event = new EnEvent(event0Key); + event.put("MonitoredValue", 100.0D); + + var anomalyDetectionLogic = spy(new AnomalyDetectionPolicyDecideTaskSelectionLogic()); + var internalContext = mock(ApexInternalContext.class); + + + var executionContext = + spy(new TaskSelectionExecutionContext(mock(TaskSelectExecutor.class), 15326, new AxState(), + event, new AxArtifactKey(), internalContext)); + + assertThatThrownBy(() -> anomalyDetectionLogic.getTask(executionContext)) + .isInstanceOf(ContextRuntimeException.class) + .hasMessageContaining("cannot find definition of context album \"AnomalyDetectionAlbum\""); + + var taskKey = new AxArtifactKey("task1", "0.0.1"); + var task = new AxTask(taskKey); + task.setInputEvent(event0); + + var tasks = new AxTasks(new AxArtifactKey("task1", "0.0.1"), + Map.of(new AxArtifactKey(taskKey), task)); + ModelService.registerModel(AxTasks.class, tasks); + + var axState = new AxState(new AxReferenceKey("axstate:0.0.1:newtask:test")); + axState.setTaskReferences(Map.of(taskKey, new AxStateTaskReference())); + + var executionContext2 = + spy(new TaskSelectionExecutionContext(mock(TaskSelectExecutor.class), 15326, axState, + event, new AxArtifactKey(), internalContext)); + + doReturn(mock(ContextAlbumImpl.class)).when(executionContext2).getContextAlbum("AnomalyDetectionAlbum"); + + assertDoesNotThrow(() -> anomalyDetectionLogic.getTask(executionContext2)); + } + + @Test + void testGetStats() { + var getStats = ReflectionUtils.findMethod(AnomalyDetectionPolicyDecideTaskSelectionLogic.class, + "getStatsTest", List.class, double.class); + assertNotNull(getStats); + ReflectionUtils.makeAccessible(getStats); + var anomalyDetectionLogic = spy(new AnomalyDetectionPolicyDecideTaskSelectionLogic()); + assertDoesNotThrow(() -> ReflectionUtils.invokeMethod(getStats, anomalyDetectionLogic, + List.of(35.5, 56.7, 89.7), 22.3)); + } + + @Test + void testRemoveValue() { + var removeValue = ReflectionUtils.findMethod(AnomalyDetectionPolicyDecideTaskSelectionLogic.class, + "removevalue", + Double[].class, double.class); + assertNotNull(removeValue); + ReflectionUtils.makeAccessible(removeValue); + var anomalyDetectionLogic = spy(new AnomalyDetectionPolicyDecideTaskSelectionLogic()); + Double[] array = {22.1, 23.2}; + assertDoesNotThrow(() -> ReflectionUtils.invokeMethod(removeValue, anomalyDetectionLogic, array, 23.2)); + } + + @Test + void testisAllEqual() { + var isAllEqual = ReflectionUtils.findMethod(AnomalyDetectionPolicyDecideTaskSelectionLogic.class, "isAllEqual", + Double[].class); + assertNotNull(isAllEqual); + ReflectionUtils.makeAccessible(isAllEqual); + var anomalyDetectionLogic = spy(new AnomalyDetectionPolicyDecideTaskSelectionLogic()); + Double[] array = {22.1, 23.2, 22.1}; + assertDoesNotThrow(() -> ReflectionUtils.invokeMethod(isAllEqual, anomalyDetectionLogic, (Object) array)); + + } + +} diff --git a/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/AutoLearnConceptTest.java b/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/AutoLearnConceptTest.java new file mode 100644 index 000000000..cae889c0b --- /dev/null +++ b/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/AutoLearnConceptTest.java @@ -0,0 +1,69 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (c) 2024 Nordix Foundation. + * ================================================================================ + * 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.examples.adaptive; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.ArrayList; +import java.util.List; +import org.junit.jupiter.api.Test; +import org.onap.policy.apex.examples.adaptive.concepts.AutoLearn; + + +class AutoLearnConceptTest { + + @Test + void testToString() { + AutoLearn autoLearn = new AutoLearn(); + AutoLearn compareAutoLearn = new AutoLearn(); + assertEquals(autoLearn.hashCode(), compareAutoLearn.hashCode()); + List<Double> avDiffs = new ArrayList<>(); + avDiffs.add(27d); + List<Long> counts = new ArrayList<>(); + counts.add(2L); + autoLearn.setCounts(counts); + autoLearn.setAvDiffs(avDiffs); + assertEquals(avDiffs, autoLearn.getAvDiffs()); + assertEquals(counts, autoLearn.getCounts()); + assertTrue(autoLearn.isInitialized()); + assertEquals("AutoLearn(avDiffs=[27.0], counts=[2])", autoLearn.toString()); + } + + @Test + void testConditions() { + AutoLearn a1 = new AutoLearn(); + a1.setCounts(List.of(1L, 3L)); + assertTrue(a1.checkSetCounts()); + assertFalse(a1.checkSetAvDiffs()); + AutoLearn a2 = new AutoLearn(); + a2.init(1); + assertNotNull(a2.getCounts()); + a1.unsetAvDiffs(); + a1.unsetCounts(); + assertNull(a1.getCounts()); + assertNull(a1.getAvDiffs()); + } + +} diff --git a/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/AutoLearnTaskSelectionTest.java b/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/AutoLearnTaskSelectionTest.java new file mode 100644 index 000000000..863579010 --- /dev/null +++ b/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/AutoLearnTaskSelectionTest.java @@ -0,0 +1,177 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (c) 2024 Nordix Foundation. + * ================================================================================ + * 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.examples.adaptive; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.spy; + +import java.util.List; +import java.util.Map; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.onap.policy.apex.context.ContextRuntimeException; +import org.onap.policy.apex.context.impl.ContextAlbumImpl; +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.SchemaParameters; +import org.onap.policy.apex.core.engine.context.ApexInternalContext; +import org.onap.policy.apex.core.engine.event.EnEvent; +import org.onap.policy.apex.core.engine.executor.TaskSelectExecutor; +import org.onap.policy.apex.core.engine.executor.context.TaskSelectionExecutionContext; +import org.onap.policy.apex.examples.adaptive.concepts.AutoLearn; +import org.onap.policy.apex.examples.adaptive.model.java.AutoLearnPolicyDecideTaskSelectionLogic; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.basicmodel.concepts.AxModel; +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.AxContextAlbums; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema; +import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas; +import org.onap.policy.apex.model.eventmodel.concepts.AxEvent; +import org.onap.policy.apex.model.eventmodel.concepts.AxEvents; +import org.onap.policy.apex.model.eventmodel.concepts.AxField; +import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel; +import org.onap.policy.apex.model.policymodel.concepts.AxState; +import org.onap.policy.apex.model.policymodel.concepts.AxStateTaskReference; +import org.onap.policy.apex.model.policymodel.concepts.AxTask; +import org.onap.policy.apex.model.policymodel.concepts.AxTasks; +import org.onap.policy.common.parameters.ParameterService; +import org.springframework.util.ReflectionUtils; + + +class AutoLearnTaskSelectionTest { + + private SchemaParameters schemaParameters; + + + @BeforeEach + void beforeTest() { + schemaParameters = new SchemaParameters(); + schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME); + schemaParameters.getSchemaHelperParameterMap().put("JAVA", new JavaSchemaHelperParameters()); + ParameterService.register(schemaParameters); + } + + /** + * After test. + */ + @AfterEach + void afterTest() { + ParameterService.deregister(schemaParameters); + } + + @Test + void testGetTask() { + var key = new AxArtifactKey(); + var axEvent = new AxEvent(key); + ModelService.registerModel(AxEvent.class, axEvent); + var model = new AxPolicyModel(key); + model.setAlbums(new AxContextAlbums(key)); + ModelService.registerModel(AxModel.class, model); + + var event0Key = new AxArtifactKey("Event0:0.0.1"); + var event0 = new AxEvent(event0Key, "a.name.space", "source", "target"); + var schemas = new AxContextSchemas(event0Key); + var axField = new AxField(); + axField.setSchema(schemas.getKey()); + event0.getParameterMap().put("MonitoredValue", axField); + var events = new AxEvents(); + events.getEventMap().put(event0Key, event0); + + ModelService.registerModel(AxEvents.class, events); + var simpleDoubleSchema = new AxContextSchema(new AxArtifactKey(event0Key), + "JAVA", "java.lang.Double"); + schemas.getSchemasMap().put(simpleDoubleSchema.getKey(), simpleDoubleSchema); + ModelService.registerModel(AxContextSchemas.class, schemas); + + var event = new EnEvent(event0Key); + event.put("MonitoredValue", 100.0D); + + var autoLearnLogic = new AutoLearnPolicyDecideTaskSelectionLogic(); + var internalContext = mock(ApexInternalContext.class); + + + var executionContext = + spy(new TaskSelectionExecutionContext(mock(TaskSelectExecutor.class), 15326, new AxState(), + event, new AxArtifactKey(), internalContext)); + + assertThatThrownBy(() -> autoLearnLogic.getTask(executionContext)).isInstanceOf(ContextRuntimeException.class) + .hasMessageContaining("cannot find definition of context album \"AutoLearnAlbum\""); + + + var taskKey = new AxArtifactKey("task1", "0.0.1"); + var task = new AxTask(taskKey); + task.setInputEvent(event0); + + var tasks = new AxTasks(new AxArtifactKey("task1", "0.0.1"), + Map.of(new AxArtifactKey(taskKey), task)); + ModelService.registerModel(AxTasks.class, tasks); + + var axState = new AxState(new AxReferenceKey("axstate:0.0.1:newtask:test")); + axState.setTaskReferences(Map.of(taskKey, new AxStateTaskReference())); + + var executionContext2 = + spy(new TaskSelectionExecutionContext(mock(TaskSelectExecutor.class), 15326, axState, + event, new AxArtifactKey(), internalContext)); + + doReturn(mock(ContextAlbumImpl.class)).when(executionContext2).getContextAlbum("AutoLearnAlbum"); + assertDoesNotThrow(() -> autoLearnLogic.getTask(executionContext2)); + + } + + @Test + void testGetOption() { + var autoLearn = new AutoLearn(); + autoLearn.setCounts(List.of(2L)); + autoLearn.setAvDiffs(List.of(2.0, 3.0)); + var getOption = ReflectionUtils.findMethod(AutoLearnPolicyDecideTaskSelectionLogic.class, + "getOption", double.class, AutoLearn.class); + assertNotNull(getOption); + + var size = ReflectionUtils.findField(AutoLearnPolicyDecideTaskSelectionLogic.class, "size"); + + ReflectionUtils.makeAccessible(getOption); + ReflectionUtils.makeAccessible(size); + var autoLearnTaskSelectionLogic = spy(new AutoLearnPolicyDecideTaskSelectionLogic()); + ReflectionUtils.setField(size, autoLearnTaskSelectionLogic, 2); + assertDoesNotThrow(() -> ReflectionUtils.invokeMethod(getOption, autoLearnTaskSelectionLogic, 2.0, + autoLearn)); + } + + @Test + void testLearn() { + var autoLearn = new AutoLearn(); + autoLearn.setCounts(List.of(2L, 3L)); + autoLearn.setAvDiffs(List.of(2.0, 3.0)); + var learn = ReflectionUtils.findMethod(AutoLearnPolicyDecideTaskSelectionLogic.class, "learn", int.class, + double.class, AutoLearn.class); + assertNotNull(learn); + ReflectionUtils.makeAccessible(learn); + var autoLearnTaskSelectionLogic = spy(new AutoLearnPolicyDecideTaskSelectionLogic()); + assertDoesNotThrow(() -> ReflectionUtils.invokeMethod(learn, autoLearnTaskSelectionLogic, 1, 2.0, + autoLearn)); + } +} diff --git a/plugins/plugins-context/plugins-context-distribution/plugins-context-distribution-hazelcast/src/test/java/org/onap/policy/apex/plugins/context/distribution/hazelcast/HazelCastDistributorTest.java b/plugins/plugins-context/plugins-context-distribution/plugins-context-distribution-hazelcast/src/test/java/org/onap/policy/apex/plugins/context/distribution/hazelcast/HazelCastDistributorTest.java new file mode 100644 index 000000000..6db3084d8 --- /dev/null +++ b/plugins/plugins-context/plugins-context-distribution/plugins-context-distribution-hazelcast/src/test/java/org/onap/policy/apex/plugins/context/distribution/hazelcast/HazelCastDistributorTest.java @@ -0,0 +1,72 @@ +/*- + * ============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.plugins.context.distribution.hazelcast; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; + +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.parameters.ContextParameterConstants; +import org.onap.policy.apex.context.parameters.ContextParameters; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.common.parameters.ParameterService; + + +class HazelCastDistributorTest { + + @BeforeAll + public static void prepareForTest() { + final ContextParameters contextParameters = new ContextParameters(); + contextParameters.getLockManagerParameters() + .setPluginClass("org.onap.policy.apex.context.impl.locking.jvmlocal.JvmLocalLockManager"); + + contextParameters.setName(ContextParameterConstants.MAIN_GROUP_NAME); + contextParameters.getLockManagerParameters().setName(ContextParameterConstants.LOCKING_GROUP_NAME); + contextParameters.getPersistorParameters().setName(ContextParameterConstants.PERSISTENCE_GROUP_NAME); + + ParameterService.register(contextParameters); + ParameterService.register(contextParameters.getLockManagerParameters()); + ParameterService.register(contextParameters.getPersistorParameters()); + } + + /** + * Clear down the test data. + */ + @AfterAll + public static void cleanUpAfterTest() { + ParameterService.deregister(ContextParameterConstants.LOCKING_GROUP_NAME); + ParameterService.deregister(ContextParameterConstants.PERSISTENCE_GROUP_NAME); + ParameterService.deregister(ContextParameterConstants.MAIN_GROUP_NAME); + ParameterService.clear(); + } + + @Test + void testHazelcastDistributor() throws ContextException { + var distributor = new HazelcastContextDistributor(); + var key = new AxArtifactKey("dummyKey", "1.0.1"); + assertDoesNotThrow(() -> distributor.init(new AxArtifactKey("dummyKey", "1.0.1"))); + assertDoesNotThrow(() -> distributor.getContextAlbumMap(key).values()); + assertDoesNotThrow(distributor::shutdown); + } + +} diff --git a/plugins/plugins-context/plugins-context-distribution/plugins-context-distribution-infinispan/src/main/java/org/onap/policy/apex/plugins/context/distribution/infinispan/InfinispanManager.java b/plugins/plugins-context/plugins-context-distribution/plugins-context-distribution-infinispan/src/main/java/org/onap/policy/apex/plugins/context/distribution/infinispan/InfinispanManager.java index a9346bcc3..05d7cefb6 100644 --- a/plugins/plugins-context/plugins-context-distribution/plugins-context-distribution-infinispan/src/main/java/org/onap/policy/apex/plugins/context/distribution/infinispan/InfinispanManager.java +++ b/plugins/plugins-context/plugins-context-distribution/plugins-context-distribution-infinispan/src/main/java/org/onap/policy/apex/plugins/context/distribution/infinispan/InfinispanManager.java @@ -2,6 +2,7 @@ * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2024 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -59,7 +60,7 @@ public class InfinispanManager { } catch (final IOException ioException) { final String errorMessage = "failed to start infinispan cache manager, " + "no infinispan configuration found on local file system or in classpath, " - + "try setting Infinspan \"configFile\" parameter"; + + "try setting Infinispan \"configFile\" parameter"; LOGGER.error(errorMessage); throw new ContextException(errorMessage, ioException); } catch (final Exception e) { diff --git a/plugins/plugins-context/plugins-context-distribution/plugins-context-distribution-infinispan/src/test/java/org/onap/policy/apex/plugins/context/distribution/infinispan/InfinispanDistributorTest.java b/plugins/plugins-context/plugins-context-distribution/plugins-context-distribution-infinispan/src/test/java/org/onap/policy/apex/plugins/context/distribution/infinispan/InfinispanDistributorTest.java new file mode 100644 index 000000000..4a87b5410 --- /dev/null +++ b/plugins/plugins-context/plugins-context-distribution/plugins-context-distribution-infinispan/src/test/java/org/onap/policy/apex/plugins/context/distribution/infinispan/InfinispanDistributorTest.java @@ -0,0 +1,77 @@ +/*- + * ============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.plugins.context.distribution.infinispan; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; + +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.parameters.ContextParameterConstants; +import org.onap.policy.apex.context.parameters.ContextParameters; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.common.parameters.ParameterService; + + +class InfinispanDistributorTest { + + private static InfinispanDistributorParameters distributorParams; + + @BeforeAll + static void prepareForTest() { + final ContextParameters contextParameters = new ContextParameters(); + + contextParameters.setName(ContextParameterConstants.MAIN_GROUP_NAME); + contextParameters.getLockManagerParameters().setName(ContextParameterConstants.LOCKING_GROUP_NAME); + contextParameters.getPersistorParameters().setName(ContextParameterConstants.PERSISTENCE_GROUP_NAME); + + ParameterService.register(contextParameters); + ParameterService.register(contextParameters.getLockManagerParameters()); + ParameterService.register(contextParameters.getPersistorParameters()); + + distributorParams = new InfinispanDistributorParameters(); + ParameterService.register(distributorParams); + } + + /** + * Clear down the test data. + */ + @AfterAll + static void cleanUpAfterTest() { + ParameterService.deregister(ContextParameterConstants.LOCKING_GROUP_NAME); + ParameterService.deregister(ContextParameterConstants.PERSISTENCE_GROUP_NAME); + ParameterService.clear(); + } + + @Test + void testInvalidParameters() throws ContextException { + var infiniSpanDistributor = new InfinispanContextDistributor(); + var key = new AxArtifactKey("test", "1.1.1"); + assertThatThrownBy(() -> infiniSpanDistributor.init(key)).isInstanceOf(NoClassDefFoundError.class) + .hasMessageContaining("TransactionManager"); + assertThatThrownBy(() -> infiniSpanDistributor.getContextAlbumMap(key)) + .hasMessageContaining("infinispanManager\" is null"); + assertDoesNotThrow(infiniSpanDistributor::shutdown); + } + +} diff --git a/plugins/plugins-context/plugins-context-locking/plugins-context-locking-curator/src/test/java/org/onap/policy/apex/plugins/context/locking/curator/CuratorLockFacadeTest.java b/plugins/plugins-context/plugins-context-locking/plugins-context-locking-curator/src/test/java/org/onap/policy/apex/plugins/context/locking/curator/CuratorLockFacadeTest.java new file mode 100644 index 000000000..e4d7d6608 --- /dev/null +++ b/plugins/plugins-context/plugins-context-locking/plugins-context-locking-curator/src/test/java/org/onap/policy/apex/plugins/context/locking/curator/CuratorLockFacadeTest.java @@ -0,0 +1,66 @@ +/*- + * ============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.plugins.context.locking.curator; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.mock; + +import java.util.concurrent.TimeUnit; +import org.apache.curator.framework.recipes.locks.InterProcessMutex; +import org.junit.jupiter.api.Test; + +class CuratorLockFacadeTest { + + @Test + void testLock() throws Exception { + var mutex = mock(InterProcessMutex.class); + var curatorLockFacade = new CuratorLockFacade(mutex, "test"); + assertDoesNotThrow(curatorLockFacade::lock); + doThrow(new RuntimeException()).when(mutex).acquire(); + assertDoesNotThrow(curatorLockFacade::lock); + assertDoesNotThrow(curatorLockFacade::lockInterruptibly); + assertFalse(curatorLockFacade.tryLock()); + doNothing().when(mutex).acquire(); + assertTrue(curatorLockFacade.tryLock()); + } + + @Test + void testLockWithTime() throws Exception { + var mutex = mock(InterProcessMutex.class); + var curatorLockFacade = new CuratorLockFacade(mutex, "test"); + assertTrue(curatorLockFacade.tryLock(2L, TimeUnit.MILLISECONDS)); + + doThrow(new RuntimeException()).when(mutex).acquire(2L, TimeUnit.MILLISECONDS); + assertFalse(curatorLockFacade.tryLock(2L, TimeUnit.MILLISECONDS)); + + assertDoesNotThrow(curatorLockFacade::unlock); + doThrow(new RuntimeException()).when(mutex).release(); + assertDoesNotThrow(curatorLockFacade::unlock); + + assertNull(curatorLockFacade.newCondition()); + } + +} diff --git a/plugins/plugins-context/plugins-context-locking/plugins-context-locking-curator/src/test/java/org/onap/policy/apex/plugins/context/locking/curator/CuratorLockManagerTest.java b/plugins/plugins-context/plugins-context-locking/plugins-context-locking-curator/src/test/java/org/onap/policy/apex/plugins/context/locking/curator/CuratorLockManagerTest.java new file mode 100644 index 000000000..7ff1c5e46 --- /dev/null +++ b/plugins/plugins-context/plugins-context-locking/plugins-context-locking-curator/src/test/java/org/onap/policy/apex/plugins/context/locking/curator/CuratorLockManagerTest.java @@ -0,0 +1,109 @@ +/*- + * ============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.plugins.context.locking.curator; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.mock; + +import org.apache.curator.framework.CuratorFramework; +import org.junit.jupiter.api.Test; +import org.onap.policy.apex.context.ContextException; +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 CuratorLockManagerTest { + + @Test + void testLockManagerInvalidParameter() throws ContextException { + var manager = new CuratorLockManager(); + var key = new AxArtifactKey("test", "1.0.1"); + + var parameters = new LockManagerParameters(); + parameters.setName(ContextParameterConstants.LOCKING_GROUP_NAME); + ParameterService.register(parameters); + + assertThatThrownBy(() -> manager.init(key)).isInstanceOf(ContextException.class) + .hasMessageContaining("curator lock manager parameters are not set"); + + ParameterService.deregister(ContextParameterConstants.LOCKING_GROUP_NAME); + ParameterService.clear(); + } + + @Test + void testLockManagerValidParams() throws ContextException { + var manager = new CuratorLockManager(); + var key = new AxArtifactKey("test", "1.0.1"); + var params = new CuratorLockManagerParameters(); + params.setName(ContextParameterConstants.LOCKING_GROUP_NAME); + ParameterService.register(params); + + assertThatThrownBy(() -> manager.init(key)).isInstanceOf(ContextException.class) + .hasMessageContaining("could not connect to Zookeeper server"); + + ParameterService.deregister(ContextParameterConstants.LOCKING_GROUP_NAME); + ParameterService.clear(); + } + + @Test + void testNullZookeeperAddr() throws ContextException { + var params = new CuratorLockManagerParameters(); + params.setName(ContextParameterConstants.LOCKING_GROUP_NAME); + params.setZookeeperAddress(""); + ParameterService.register(params); + + var manager = new CuratorLockManager(); + var key = new AxArtifactKey("test", "1.0.1"); + + assertThatThrownBy(() -> manager.init(key)).isInstanceOf(ContextException.class) + .hasMessageContaining("check if the curator Zookeeper address parameter is set correctly"); + + assertThatThrownBy(() -> manager.getReentrantReadWriteLock("test")).isInstanceOf(ContextException.class) + .hasMessageContaining("creation of lock using Zookeeper server at \"\", failed"); + + assertDoesNotThrow(manager::shutdown); + + ParameterService.deregister(ContextParameterConstants.LOCKING_GROUP_NAME); + ParameterService.clear(); + } + + @Test + void testLockManagerParameters() { + var params = new CuratorLockManagerParameters(); + assertDoesNotThrow(params::toString); + assertEquals("localhost:2181", params.getZookeeperAddress()); + assertEquals(1000, params.getZookeeperConnectSleepTime()); + assertEquals(3, params.getZookeeperContextRetries()); + } + + @Test + void testReentrantReadWriteLock() { + var curatorLock = new CuratorReentrantReadWriteLock(mock(CuratorFramework.class), "/test"); + assertEquals("/test", curatorLock.getLockId()); + assertDoesNotThrow(curatorLock::readLock); + assertDoesNotThrow(curatorLock::writeLock); + } + +} diff --git a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroNullableMapperTest.java b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroNullableMapperTest.java new file mode 100644 index 000000000..27a0ca3bf --- /dev/null +++ b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroNullableMapperTest.java @@ -0,0 +1,58 @@ +/*- + * ============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.plugins.context.schema.avro; + +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 static org.mockito.Mockito.mock; + +import org.apache.avro.Schema; +import org.junit.jupiter.api.Test; +import org.onap.policy.apex.context.ContextRuntimeException; +import org.onap.policy.apex.model.basicmodel.concepts.ApexRuntimeException; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; + +class AvroNullableMapperTest { + + @Test + void avroNullableMapperTests() { + var avroObjectMapper = new AvroDirectObjectMapper(); + var avroNullableMapper = new AvroNullableMapper(avroObjectMapper); + assertDoesNotThrow(() -> avroNullableMapper.init(new AxArtifactKey("test", "1.0.1"), + Schema.Type.BOOLEAN)); + + assertEquals("class java.lang.Boolean", avroNullableMapper.getJavaClass().toString()); + assertThat(avroNullableMapper.getAvroType()).isEqualByComparingTo(Schema.Type.UNION); + + var avroObjMapper = new AvroDirectObjectMapper(); + avroObjMapper.init(new AxArtifactKey("test2", "1.1.2"), Schema.Type.BOOLEAN); + assertThrows(ContextRuntimeException.class, () -> avroNullableMapper.mapFromAvro(avroObjMapper)); + assertNull(avroNullableMapper.mapFromAvro(null)); + + assertNull(avroNullableMapper.mapToAvro(null)); + assertThrows(ApexRuntimeException.class, () -> avroNullableMapper.mapToAvro(avroObjMapper)); + + assertDoesNotThrow(() -> avroNullableMapper.createNewInstance(mock(Schema.class))); + } +} diff --git a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroObjMapperFactoryTest.java b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroObjMapperFactoryTest.java new file mode 100644 index 000000000..4ac7a20be --- /dev/null +++ b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-avro/src/test/java/org/onap/policy/apex/plugins/context/schema/avro/AvroObjMapperFactoryTest.java @@ -0,0 +1,60 @@ +/*- + * ============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.plugins.context.schema.avro; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.List; +import org.apache.avro.Schema; +import org.junit.jupiter.api.Test; +import org.onap.policy.apex.context.ContextRuntimeException; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; + +class AvroObjMapperFactoryTest { + @Test + void testObjMapperFactory() { + var objMapperFactory = new AvroObjectMapperFactory(); + var key = new AxArtifactKey("test", "1.0.1"); + var schema1 = Schema.createUnion(List.of(Schema.create(Schema.Type.NULL))); + assertThatThrownBy(() -> objMapperFactory.get(key, schema1)) + .isInstanceOf(ContextRuntimeException.class) + .hasMessageContaining("Apex currently only supports UNION schemas with 2 options, " + + "one must be NULL"); + + var schema2 = mock(Schema.class); + when(schema2.getType()).thenReturn(Schema.Type.UNION); + var nullSchema = Schema.create(Schema.Type.NULL); + when(schema2.getTypes()).thenReturn(List.of(nullSchema, nullSchema)); + assertThatThrownBy(() -> objMapperFactory.get(key, schema2)) + .isInstanceOf(ContextRuntimeException.class) + .hasMessageContaining("Apex currently only supports UNION schema2 with 2 options, " + + "only one can be NULL, and the other cannot be another UNION"); + + var fixedSchema = Schema.createFixed("test1", "doc", "test", 2); + when(schema2.getTypes()).thenReturn(List.of(fixedSchema, nullSchema)); + assertDoesNotThrow(() -> objMapperFactory.get(key, schema2)); + + } + +} diff --git a/testsuites/integration/integration-common/src/test/java/org/onap/policy/apex/testsuites/integration/common/concepts/ConceptsTest.java b/testsuites/integration/integration-common/src/test/java/org/onap/policy/apex/context/test/concepts/ConceptsTest.java index 7592978cd..e72975271 100644 --- a/testsuites/integration/integration-common/src/test/java/org/onap/policy/apex/testsuites/integration/common/concepts/ConceptsTest.java +++ b/testsuites/integration/integration-common/src/test/java/org/onap/policy/apex/context/test/concepts/ConceptsTest.java @@ -18,7 +18,7 @@ * ============LICENSE_END========================================================= */ -package org.onap.policy.apex.testsuites.integration.common.concepts; +package org.onap.policy.apex.context.test.concepts; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; @@ -30,19 +30,6 @@ import java.util.Date; import java.util.Map; import java.util.TreeSet; import org.junit.jupiter.api.Test; -import org.onap.policy.apex.context.test.concepts.TestContextBooleanItem; -import org.onap.policy.apex.context.test.concepts.TestContextByteItem; -import org.onap.policy.apex.context.test.concepts.TestContextDateItem; -import org.onap.policy.apex.context.test.concepts.TestContextDateLocaleItem; -import org.onap.policy.apex.context.test.concepts.TestContextDateTzItem; -import org.onap.policy.apex.context.test.concepts.TestContextDoubleItem; -import org.onap.policy.apex.context.test.concepts.TestContextFloatItem; -import org.onap.policy.apex.context.test.concepts.TestContextIntItem; -import org.onap.policy.apex.context.test.concepts.TestContextLongItem; -import org.onap.policy.apex.context.test.concepts.TestContextLongObjectItem; -import org.onap.policy.apex.context.test.concepts.TestContextStringItem; -import org.onap.policy.apex.context.test.concepts.TestContextTreeMapItem; -import org.onap.policy.apex.context.test.concepts.TestContextTreeSetItem; class ConceptsTest { |