diff options
Diffstat (limited to 'plugins')
14 files changed, 633 insertions, 64 deletions
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-locking/plugins-context-locking-hazelcast/src/test/java/org/onap/policy/apex/plugins/context/locking/hazelcast/HazelcastLockManagerTest.java b/plugins/plugins-context/plugins-context-locking/plugins-context-locking-hazelcast/src/test/java/org/onap/policy/apex/plugins/context/locking/hazelcast/HazelcastLockManagerTest.java new file mode 100644 index 000000000..da2c10c34 --- /dev/null +++ b/plugins/plugins-context/plugins-context-locking/plugins-context-locking-hazelcast/src/test/java/org/onap/policy/apex/plugins/context/locking/hazelcast/HazelcastLockManagerTest.java @@ -0,0 +1,97 @@ +/* + * ============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.plugins.context.locking.hazelcast; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.mockStatic; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.hazelcast.core.Hazelcast; +import com.hazelcast.core.HazelcastInstance; +import com.hazelcast.core.LifecycleService; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.MockedStatic; +import org.onap.policy.apex.context.ContextException; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; + +class HazelcastLockManagerTest { + + private HazelcastLockManager lockManager; + private HazelcastInstance mockHazelcastInstance; + private LifecycleService mockLifecycleService; + private MockedStatic<Hazelcast> mockedHazelcast; + + @BeforeEach + void setUp() throws ContextException { + mockHazelcastInstance = mock(HazelcastInstance.class); + mockLifecycleService = mock(LifecycleService.class); + + mockedHazelcast = mockStatic(Hazelcast.class); + mockedHazelcast.when(Hazelcast::newHazelcastInstance).thenReturn(mockHazelcastInstance); + + when(mockHazelcastInstance.getLifecycleService()).thenReturn(mockLifecycleService); + when(mockLifecycleService.isRunning()).thenReturn(true); + + lockManager = new HazelcastLockManager(); + } + + @AfterEach + void tearDown() { + mockedHazelcast.close(); + } + + @Test + void testInit() { + AxArtifactKey testKey = new AxArtifactKey("TestKey", "1.0"); + + assertDoesNotThrow(() -> lockManager.init(testKey)); + + mockedHazelcast.verify(Hazelcast::newHazelcastInstance); + } + + @Test + void testGetReentrantReadWriteLockWhenHazelcastNotRunning() { + when(mockLifecycleService.isRunning()).thenReturn(false); + + String lockId = "testLock"; + assertThrows(ContextException.class, () -> lockManager.getReentrantReadWriteLock(lockId)); + } + + @Test + void testShutdown() throws ContextException { + lockManager.init(new AxArtifactKey("TestKey", "1.0")); + assertDoesNotThrow(() -> lockManager.shutdown()); + + verify(mockHazelcastInstance).shutdown(); + } + + @Test + void testShutdownWithoutInit() { + assertDoesNotThrow(() -> lockManager.shutdown()); + verify(mockHazelcastInstance, never()).shutdown(); + } +} diff --git a/plugins/plugins-context/plugins-context-locking/plugins-context-locking-hazelcast/src/test/java/org/onap/policy/apex/plugins/context/locking/hazelcast/HazelcastLockTest.java b/plugins/plugins-context/plugins-context-locking/plugins-context-locking-hazelcast/src/test/java/org/onap/policy/apex/plugins/context/locking/hazelcast/HazelcastLockTest.java new file mode 100644 index 000000000..fb3d738cd --- /dev/null +++ b/plugins/plugins-context/plugins-context-locking/plugins-context-locking-hazelcast/src/test/java/org/onap/policy/apex/plugins/context/locking/hazelcast/HazelcastLockTest.java @@ -0,0 +1,78 @@ +/* + * ============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.plugins.context.locking.hazelcast; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.hazelcast.core.HazelcastInstance; +import com.hazelcast.cp.CPSubsystem; +import com.hazelcast.cp.lock.FencedLock; +import java.util.concurrent.locks.Lock; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class HazelcastLockTest { + + private HazelcastInstance hazelcastInstance; + private CPSubsystem cpSubsystem; + private FencedLock mockReadLock; + private FencedLock mockWriteLock; + + private HazelcastLock hazelcastLock; + private final String lockId = "testLock"; + + @BeforeEach + void setUp() { + hazelcastInstance = mock(HazelcastInstance.class); + cpSubsystem = mock(CPSubsystem.class); + mockReadLock = mock(FencedLock.class); + mockWriteLock = mock(FencedLock.class); + + when(hazelcastInstance.getCPSubsystem()).thenReturn(cpSubsystem); + when(cpSubsystem.getLock(lockId + "_READ")).thenReturn(mockReadLock); + when(cpSubsystem.getLock(lockId + "_WRITE")).thenReturn(mockWriteLock); + + hazelcastLock = new HazelcastLock(hazelcastInstance, lockId); + } + + @Test + void testConstructor() { + assertEquals(lockId, hazelcastLock.getLockId()); + + verify(cpSubsystem).getLock(lockId + "_READ"); + verify(cpSubsystem).getLock(lockId + "_WRITE"); + } + + @Test + void testReadLock() { + Lock readLock = hazelcastLock.readLock(); + assertEquals(mockReadLock, readLock); + } + + @Test + void testWriteLock() { + Lock writeLock = hazelcastLock.writeLock(); + assertEquals(mockWriteLock, 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/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-kafka/pom.xml b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-kafka/pom.xml index f243147ad..db543c31f 100644 --- a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-kafka/pom.xml +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-kafka/pom.xml @@ -49,7 +49,7 @@ <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-compress</artifactId> - <version>1.25.0</version> + <version>1.27.0</version> </dependency> </dependencies> diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/java/org/onap/policy/apex/plugins/event/carrier/restclient/ApexRestClientConsumerTest.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/java/org/onap/policy/apex/plugins/event/carrier/restclient/ApexRestClientConsumerTest.java index b3a6bca06..d87d7aab3 100644 --- a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/java/org/onap/policy/apex/plugins/event/carrier/restclient/ApexRestClientConsumerTest.java +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/java/org/onap/policy/apex/plugins/event/carrier/restclient/ApexRestClientConsumerTest.java @@ -37,9 +37,7 @@ import java.io.ByteArrayOutputStream; import java.io.PrintStream; import java.util.concurrent.TimeUnit; import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.Order; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestMethodOrder; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; import org.mockito.Mockito; diff --git a/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/src/main/test/org/onap/policy/apex/plugins/event/protocol/jms/Apex2JmsObjectEventConverterTest.java b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/src/test/java/org/onap/policy/apex/plugins/event/protocol/jms/Apex2JmsObjectEventConverterTest.java index c828f789a..4aa0a0b0a 100644 --- a/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/src/main/test/org/onap/policy/apex/plugins/event/protocol/jms/Apex2JmsObjectEventConverterTest.java +++ b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/src/test/java/org/onap/policy/apex/plugins/event/protocol/jms/Apex2JmsObjectEventConverterTest.java @@ -22,16 +22,11 @@ package org.onap.policy.apex.plugins.event.protocol.jms; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertSame; -import jakarta.jms.JMSException; -import jakarta.jms.ObjectMessage; import java.io.ByteArrayOutputStream; import java.io.PrintStream; -import java.util.List; -import org.apache.activemq.command.ActiveMQObjectMessage; import org.apache.commons.lang3.RandomStringUtils; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; @@ -94,57 +89,12 @@ class Apex2JmsObjectEventConverterTest { final JmsObjectEventProtocolParameters parameters = new JmsObjectEventProtocolParameters(); converter.init(parameters); final String eventName = RandomStringUtils.randomAlphabetic(4); - assertThatThrownBy(() -> converter.toApexEvent(eventName, new Object())) + final Object testObject = new Object(); + assertThatThrownBy(() -> converter.toApexEvent(eventName, testObject)) .isInstanceOf(ApexEventRuntimeException.class); } @Test - void toApexEventNoParams() { - final String eventName = RandomStringUtils.randomAlphabetic(4); - ObjectMessage object = (ObjectMessage) new ActiveMQObjectMessage(); - assertThatThrownBy(() -> converter.toApexEvent(eventName, object)) - .isInstanceOf(ApexEventRuntimeException.class); - } - - @Test - void toApexEventIncomingObjectIsNull() { - final JmsObjectEventProtocolParameters parameters = new JmsObjectEventProtocolParameters(); - - converter.init(parameters); - final String eventName = RandomStringUtils.randomAlphabetic(4); - ObjectMessage object = (ObjectMessage) new ActiveMQObjectMessage(); - assertThatThrownBy(() -> converter.toApexEvent(eventName, object)) - .isInstanceOf(NullPointerException.class); - } - - @Test - void toApexEvent() throws ApexEventException, JMSException { - final JmsObjectEventProtocolParameters parameters = new JmsObjectEventProtocolParameters(); - - converter.init(parameters); - final String eventName = RandomStringUtils.randomAlphabetic(4); - final ObjectMessage object = (ObjectMessage) new ActiveMQObjectMessage(); - final String value = RandomStringUtils.randomAlphabetic(3); - object.setObject(value); - - // Prepare expected object - final ApexEvent expectedEvent = new ApexEvent("String" + parameters.getIncomingEventSuffix(), - parameters.getIncomingEventVersion(), - "java.lang", - parameters.getIncomingEventSource(), - parameters.getIncomingEventTarget()); - // Overwrite executionId to match executionId of actual - expectedEvent.setExecutionId(1); - final Object[] expected = {expectedEvent}; - - // Run tested method - final List<ApexEvent> actual = converter.toApexEvent(eventName, object); - // Overwrite executionId to match executionId of expected - actual.get(0).setExecutionId(1); - assertArrayEquals(expected, actual.toArray()); - } - - @Test void fromApexEventNull() { assertThatThrownBy(() -> converter.fromApexEvent(null)).isInstanceOf(ApexEventException.class); } diff --git a/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/src/main/test/org/onap/policy/apex/plugins/event/protocol/jms/Apex2JmsTextEventConverterTest.java b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/src/test/java/org/onap/policy/apex/plugins/event/protocol/jms/Apex2JmsTextEventConverterTest.java index a2ff9d02f..b2529b48f 100644 --- a/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/src/main/test/org/onap/policy/apex/plugins/event/protocol/jms/Apex2JmsTextEventConverterTest.java +++ b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/src/test/java/org/onap/policy/apex/plugins/event/protocol/jms/Apex2JmsTextEventConverterTest.java @@ -84,6 +84,7 @@ class Apex2JmsTextEventConverterTest { final String source = RandomStringUtils.randomAlphabetic(5); final String target = RandomStringUtils.randomAlphabetic(6); final String nameSpace = "a.name.space"; + final String toscaPolicyState = ""; // Prepare Json String to be translated into ApexEvent final TextBlock object = @@ -100,7 +101,8 @@ class Apex2JmsTextEventConverterTest { // execute test final List<ApexEvent> apexEvents = converter.toApexEvent(eventName, object); - final ApexEvent expectedEvent = new ApexEvent(eventName, eventVersion, nameSpace, source, target); + final ApexEvent expectedEvent = + new ApexEvent(eventName, eventVersion, nameSpace, source, target, toscaPolicyState); // Reset executionId expectedEvent.setExecutionId(0); @@ -125,13 +127,14 @@ class Apex2JmsTextEventConverterTest { final String source = RandomStringUtils.randomAlphabetic(6); final String target = RandomStringUtils.randomAlphabetic(7); - final String expected = "{\n" + - " \"name\": \"" + name + "\",\n" + - " \"version\": \"" + version + "\",\n" + - " \"nameSpace\": \"" + nameSpace + "\",\n" + - " \"source\": \"" + source + "\",\n" + - " \"target\": \"" + target + "\"\n" + - "}"; + final String expected = "{\n" + + " \"name\": \"" + name + "\",\n" + + " \"version\": \"" + version + "\",\n" + + " \"nameSpace\": \"" + nameSpace + "\",\n" + + " \"source\": \"" + source + "\",\n" + + " \"target\": \"" + target + "\",\n" + + " \"toscaPolicyState\": null\n" + + "}"; // Prepare Model service final AxArtifactKey eventKey = new AxArtifactKey(name + ":" + version); diff --git a/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/src/main/test/org/onap/policy/apex/plugins/event/protocol/jms/JmsObjectEventProtocolParametersTest.java b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/src/test/java/org/onap/policy/apex/plugins/event/protocol/jms/JmsObjectEventProtocolParametersTest.java index 1f480a654..1f480a654 100644 --- a/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/src/main/test/org/onap/policy/apex/plugins/event/protocol/jms/JmsObjectEventProtocolParametersTest.java +++ b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/src/test/java/org/onap/policy/apex/plugins/event/protocol/jms/JmsObjectEventProtocolParametersTest.java |