From f906185ce1424f707d290d3e4fc3e4b8a3e184f9 Mon Sep 17 00:00:00 2001 From: waynedunican Date: Fri, 25 Oct 2024 09:57:33 +0100 Subject: Increase apex-pdp line and branch coverage Issue-ID: POLICY-5059 Change-Id: Ifa1472dd914ec0ef25372fa057514571a7ea27ac Signed-off-by: waynedunican --- .../hazelcast/HazelcastLockManagerTest.java | 97 +++++++++++ .../locking/hazelcast/HazelcastLockTest.java | 78 +++++++++ .../jms/Apex2JmsObjectEventConverterTest.java | 193 --------------------- .../jms/Apex2JmsTextEventConverterTest.java | 149 ---------------- .../jms/JmsObjectEventProtocolParametersTest.java | 53 ------ .../jms/Apex2JmsObjectEventConverterTest.java | 143 +++++++++++++++ .../jms/Apex2JmsTextEventConverterTest.java | 152 ++++++++++++++++ .../jms/JmsObjectEventProtocolParametersTest.java | 53 ++++++ .../runtime/impl/EnEventListenerImplTest.java | 105 +++++++++++ tools/simple-wsclient/pom.xml | 4 + 10 files changed, 632 insertions(+), 395 deletions(-) create mode 100644 plugins/plugins-context/plugins-context-locking/plugins-context-locking-hazelcast/src/test/java/org/onap/policy/apex/plugins/context/locking/hazelcast/HazelcastLockManagerTest.java create mode 100644 plugins/plugins-context/plugins-context-locking/plugins-context-locking-hazelcast/src/test/java/org/onap/policy/apex/plugins/context/locking/hazelcast/HazelcastLockTest.java delete mode 100644 plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/src/main/test/org/onap/policy/apex/plugins/event/protocol/jms/Apex2JmsObjectEventConverterTest.java delete mode 100644 plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/src/main/test/org/onap/policy/apex/plugins/event/protocol/jms/Apex2JmsTextEventConverterTest.java delete mode 100644 plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/src/main/test/org/onap/policy/apex/plugins/event/protocol/jms/JmsObjectEventProtocolParametersTest.java create mode 100644 plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/src/test/java/org/onap/policy/apex/plugins/event/protocol/jms/Apex2JmsObjectEventConverterTest.java create mode 100644 plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/src/test/java/org/onap/policy/apex/plugins/event/protocol/jms/Apex2JmsTextEventConverterTest.java create mode 100644 plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/src/test/java/org/onap/policy/apex/plugins/event/protocol/jms/JmsObjectEventProtocolParametersTest.java create mode 100644 services/services-engine/src/test/java/org/onap/policy/apex/service/engine/runtime/impl/EnEventListenerImplTest.java 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 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-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/main/test/org/onap/policy/apex/plugins/event/protocol/jms/Apex2JmsObjectEventConverterTest.java deleted file mode 100644 index c828f789a..000000000 --- a/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/src/main/test/org/onap/policy/apex/plugins/event/protocol/jms/Apex2JmsObjectEventConverterTest.java +++ /dev/null @@ -1,193 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2021, 2023-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.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; -import org.junit.jupiter.api.Test; -import org.onap.policy.apex.service.engine.event.ApexEvent; -import org.onap.policy.apex.service.engine.event.ApexEventException; -import org.onap.policy.apex.service.engine.event.ApexEventRuntimeException; -import org.onap.policy.apex.service.engine.event.impl.apexprotocolplugin.ApexEventProtocolParameters; - -class Apex2JmsObjectEventConverterTest { - private Apex2JmsObjectEventConverter converter; - private final PrintStream orgOutBuffer = System.out; - private ByteArrayOutputStream testOutStream; - - @BeforeEach - void setUp() { - converter = new Apex2JmsObjectEventConverter(); - testOutStream = new ByteArrayOutputStream(); - System.setOut(new PrintStream(testOutStream)); - } - - @AfterEach - void tearDown() { - System.setOut(orgOutBuffer); - } - - @Test - void initNull() { - assertThatThrownBy(() -> converter.init(null)) - .isInstanceOf(NullPointerException.class); - } - - @Test - void initWrongClass() { - converter.init(new ApexEventProtocolParameters()); - final String actual = testOutStream.toString(); - assertThat(actual).contains("specified Event Protocol Parameters properties of typ"); - assertNull(converter.getEventProtocolParameters()); - } - - @Test - void init() { - final JmsObjectEventProtocolParameters parameters = new JmsObjectEventProtocolParameters(); - converter.init(parameters); - final JmsObjectEventProtocolParameters actual = converter.getEventProtocolParameters(); - assertSame(parameters, actual); - } - - @Test - void toApexEventNull() { - final JmsObjectEventProtocolParameters parameters = new JmsObjectEventProtocolParameters(); - converter.init(parameters); - final String eventName = RandomStringUtils.randomAlphabetic(4); - assertThatThrownBy(() -> converter.toApexEvent(eventName, null)) - .isInstanceOf(ApexEventRuntimeException.class); - } - - @Test - void toApexEventObject() { - final JmsObjectEventProtocolParameters parameters = new JmsObjectEventProtocolParameters(); - converter.init(parameters); - final String eventName = RandomStringUtils.randomAlphabetic(4); - assertThatThrownBy(() -> converter.toApexEvent(eventName, new Object())) - .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 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); - } - - @Test - void fromApexEventEmptyEvent() throws ApexEventException { - final ApexEvent apexEvent = new ApexEvent( - "a" + RandomStringUtils.randomAlphabetic(3), - "a" + RandomStringUtils.randomAlphabetic(3), - "a" + RandomStringUtils.randomAlphabetic(3), - "", - ""); - assertThatThrownBy(() -> converter.fromApexEvent(apexEvent)) - .isInstanceOf(ApexEventException.class); - } - - @Test - void fromApexEventMultipleEvents() throws ApexEventException { - final ApexEvent apexEvent = new ApexEvent( - "a" + RandomStringUtils.randomAlphabetic(3), - "a" + RandomStringUtils.randomAlphabetic(4), - "a" + RandomStringUtils.randomAlphabetic(5), - "", - ""); - apexEvent.put(RandomStringUtils.randomAlphabetic(2), new Object()); - apexEvent.put(RandomStringUtils.randomAlphabetic(6), new Object()); - assertThatThrownBy(() -> converter.fromApexEvent(apexEvent)).isInstanceOf(ApexEventException.class); - } - - @Test - void fromApexEventSingleEvent() throws ApexEventException { - final ApexEvent apexEvent = new ApexEvent( - "a" + RandomStringUtils.randomAlphabetic(3), - "a" + RandomStringUtils.randomAlphabetic(3), - "a" + RandomStringUtils.randomAlphabetic(3), - "", - ""); - - final Object expected = new Object(); - apexEvent.put(RandomStringUtils.randomAlphabetic(2), expected); - - final Object actual = converter.fromApexEvent(apexEvent); - - assertSame(expected, actual); - } -} 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/main/test/org/onap/policy/apex/plugins/event/protocol/jms/Apex2JmsTextEventConverterTest.java deleted file mode 100644 index a2ff9d02f..000000000 --- a/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/src/main/test/org/onap/policy/apex/plugins/event/protocol/jms/Apex2JmsTextEventConverterTest.java +++ /dev/null @@ -1,149 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2021, 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.event.protocol.jms; - -import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.junit.jupiter.api.Assertions.assertArrayEquals; -import static org.junit.jupiter.api.Assertions.assertEquals; - -import java.util.List; -import org.apache.commons.lang3.RandomStringUtils; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.onap.policy.apex.context.parameters.ContextParameterConstants; -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; -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.service.engine.event.ApexEvent; -import org.onap.policy.apex.service.engine.event.ApexEventException; -import org.onap.policy.apex.service.engine.event.ApexEventRuntimeException; -import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.consumer.TextBlock; -import org.onap.policy.apex.service.engine.event.impl.jsonprotocolplugin.JsonEventProtocolParameters; -import org.onap.policy.common.parameters.ParameterService; - -class Apex2JmsTextEventConverterTest { - private Apex2JmsTextEventConverter converter; - - @BeforeEach - void setUp() { - converter = new Apex2JmsTextEventConverter(); - ModelService.registerModel(AxContextSchemas.class, new AxContextSchemas()); - ModelService.registerModel(AxEvents.class, new AxEvents()); - ParameterService.register(new SchemaParameters()); - } - - @AfterEach - void tearDown() { - ModelService.deregisterModel(AxContextSchema.class); - ModelService.deregisterModel(AxEvents.class); - ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME); - } - - @Test - void toApexEventNull() { - final String eventName = RandomStringUtils.randomAlphabetic(4); - assertThatThrownBy(() -> converter.toApexEvent(eventName, null)) - .isInstanceOf(ApexEventRuntimeException.class); - } - - @Test - void toApexEventObject() { - final String eventName = RandomStringUtils.randomAlphabetic(4); - var object = new Object(); - assertThatThrownBy(() -> converter.toApexEvent(eventName, object)).isInstanceOf( - ApexEventRuntimeException.class); - } - - @Test - void toApexEventJsonString() throws ApexEventException { - final String eventName = RandomStringUtils.randomAlphabetic(4); - final String eventVersion = "0.0.1"; - final String source = RandomStringUtils.randomAlphabetic(5); - final String target = RandomStringUtils.randomAlphabetic(6); - final String nameSpace = "a.name.space"; - - // Prepare Json String to be translated into ApexEvent - final TextBlock object = - new TextBlock(false, "{\"name\": \"" + eventName + "\", \"version\":\"" + eventVersion + "\"}"); - - // Prepare Model service - final AxArtifactKey eventKey = new AxArtifactKey(eventName + ":" + eventVersion); - final AxEvent axEvent = new AxEvent(eventKey, nameSpace, source, target); - ModelService.getModel(AxEvents.class).getEventMap().put(eventKey, axEvent); - - // prepare converter - converter.init(new JsonEventProtocolParameters()); - - // execute test - final List apexEvents = converter.toApexEvent(eventName, object); - - final ApexEvent expectedEvent = new ApexEvent(eventName, eventVersion, nameSpace, source, target); - - // Reset executionId - expectedEvent.setExecutionId(0); - for (ApexEvent event : apexEvents) { - event.setExecutionId(0); - } - Object[] expected = {expectedEvent}; - - assertArrayEquals(expected, apexEvents.toArray()); - } - - @Test - void fromApexNull() { - assertThatThrownBy(() -> converter.fromApexEvent(null)).isInstanceOf(ApexEventException.class); - } - - @Test - void fromApex() throws ApexEventException { - final String name = RandomStringUtils.randomAlphabetic(4); - final String version = "0.2.3"; - final String nameSpace = "a.name.space"; - 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" + - "}"; - - // Prepare Model service - final AxArtifactKey eventKey = new AxArtifactKey(name + ":" + version); - final AxEvent axEvent = new AxEvent(eventKey, nameSpace, source, target); - ModelService.getModel(AxEvents.class).getEventMap().put(eventKey, axEvent); - - converter.init(new JsonEventProtocolParameters()); - - final ApexEvent apexEvent = new ApexEvent(name, version, nameSpace, source, target); - final Object actual = converter.fromApexEvent(apexEvent); - - assertEquals(expected, actual); - } - -} 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/main/test/org/onap/policy/apex/plugins/event/protocol/jms/JmsObjectEventProtocolParametersTest.java deleted file mode 100644 index 1f480a654..000000000 --- a/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/src/main/test/org/onap/policy/apex/plugins/event/protocol/jms/JmsObjectEventProtocolParametersTest.java +++ /dev/null @@ -1,53 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2021, 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.event.protocol.jms; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import org.junit.jupiter.api.Test; - - -class JmsObjectEventProtocolParametersTest { - - @Test - void getIncomingEventVersion() { - final JmsObjectEventProtocolParameters jmsObjectEventProtocolParameters = - new JmsObjectEventProtocolParameters(); - final String actual = jmsObjectEventProtocolParameters.getIncomingEventVersion(); - assertEquals("1.0.0", actual); - } - - @Test - void getIncomingEventSource() { - final JmsObjectEventProtocolParameters jmsObjectEventProtocolParameters = - new JmsObjectEventProtocolParameters(); - final String actual = jmsObjectEventProtocolParameters.getIncomingEventSource(); - assertEquals("JMS", actual); - } - - @Test - void getIncomingEventTarget() { - final JmsObjectEventProtocolParameters jmsObjectEventProtocolParameters = - new JmsObjectEventProtocolParameters(); - final String actual = jmsObjectEventProtocolParameters.getIncomingEventTarget(); - assertEquals("Apex", actual); - } -} diff --git a/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/src/test/java/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 new file mode 100644 index 000000000..4aa0a0b0a --- /dev/null +++ b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/src/test/java/org/onap/policy/apex/plugins/event/protocol/jms/Apex2JmsObjectEventConverterTest.java @@ -0,0 +1,143 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2021, 2023-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.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.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import org.apache.commons.lang3.RandomStringUtils; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.onap.policy.apex.service.engine.event.ApexEvent; +import org.onap.policy.apex.service.engine.event.ApexEventException; +import org.onap.policy.apex.service.engine.event.ApexEventRuntimeException; +import org.onap.policy.apex.service.engine.event.impl.apexprotocolplugin.ApexEventProtocolParameters; + +class Apex2JmsObjectEventConverterTest { + private Apex2JmsObjectEventConverter converter; + private final PrintStream orgOutBuffer = System.out; + private ByteArrayOutputStream testOutStream; + + @BeforeEach + void setUp() { + converter = new Apex2JmsObjectEventConverter(); + testOutStream = new ByteArrayOutputStream(); + System.setOut(new PrintStream(testOutStream)); + } + + @AfterEach + void tearDown() { + System.setOut(orgOutBuffer); + } + + @Test + void initNull() { + assertThatThrownBy(() -> converter.init(null)) + .isInstanceOf(NullPointerException.class); + } + + @Test + void initWrongClass() { + converter.init(new ApexEventProtocolParameters()); + final String actual = testOutStream.toString(); + assertThat(actual).contains("specified Event Protocol Parameters properties of typ"); + assertNull(converter.getEventProtocolParameters()); + } + + @Test + void init() { + final JmsObjectEventProtocolParameters parameters = new JmsObjectEventProtocolParameters(); + converter.init(parameters); + final JmsObjectEventProtocolParameters actual = converter.getEventProtocolParameters(); + assertSame(parameters, actual); + } + + @Test + void toApexEventNull() { + final JmsObjectEventProtocolParameters parameters = new JmsObjectEventProtocolParameters(); + converter.init(parameters); + final String eventName = RandomStringUtils.randomAlphabetic(4); + assertThatThrownBy(() -> converter.toApexEvent(eventName, null)) + .isInstanceOf(ApexEventRuntimeException.class); + } + + @Test + void toApexEventObject() { + final JmsObjectEventProtocolParameters parameters = new JmsObjectEventProtocolParameters(); + converter.init(parameters); + final String eventName = RandomStringUtils.randomAlphabetic(4); + final Object testObject = new Object(); + assertThatThrownBy(() -> converter.toApexEvent(eventName, testObject)) + .isInstanceOf(ApexEventRuntimeException.class); + } + + @Test + void fromApexEventNull() { + assertThatThrownBy(() -> converter.fromApexEvent(null)).isInstanceOf(ApexEventException.class); + } + + @Test + void fromApexEventEmptyEvent() throws ApexEventException { + final ApexEvent apexEvent = new ApexEvent( + "a" + RandomStringUtils.randomAlphabetic(3), + "a" + RandomStringUtils.randomAlphabetic(3), + "a" + RandomStringUtils.randomAlphabetic(3), + "", + ""); + assertThatThrownBy(() -> converter.fromApexEvent(apexEvent)) + .isInstanceOf(ApexEventException.class); + } + + @Test + void fromApexEventMultipleEvents() throws ApexEventException { + final ApexEvent apexEvent = new ApexEvent( + "a" + RandomStringUtils.randomAlphabetic(3), + "a" + RandomStringUtils.randomAlphabetic(4), + "a" + RandomStringUtils.randomAlphabetic(5), + "", + ""); + apexEvent.put(RandomStringUtils.randomAlphabetic(2), new Object()); + apexEvent.put(RandomStringUtils.randomAlphabetic(6), new Object()); + assertThatThrownBy(() -> converter.fromApexEvent(apexEvent)).isInstanceOf(ApexEventException.class); + } + + @Test + void fromApexEventSingleEvent() throws ApexEventException { + final ApexEvent apexEvent = new ApexEvent( + "a" + RandomStringUtils.randomAlphabetic(3), + "a" + RandomStringUtils.randomAlphabetic(3), + "a" + RandomStringUtils.randomAlphabetic(3), + "", + ""); + + final Object expected = new Object(); + apexEvent.put(RandomStringUtils.randomAlphabetic(2), expected); + + final Object actual = converter.fromApexEvent(apexEvent); + + assertSame(expected, actual); + } +} diff --git a/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/src/test/java/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 new file mode 100644 index 000000000..b2529b48f --- /dev/null +++ b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/src/test/java/org/onap/policy/apex/plugins/event/protocol/jms/Apex2JmsTextEventConverterTest.java @@ -0,0 +1,152 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2021, 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.event.protocol.jms; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.List; +import org.apache.commons.lang3.RandomStringUtils; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.onap.policy.apex.context.parameters.ContextParameterConstants; +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; +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.service.engine.event.ApexEvent; +import org.onap.policy.apex.service.engine.event.ApexEventException; +import org.onap.policy.apex.service.engine.event.ApexEventRuntimeException; +import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.consumer.TextBlock; +import org.onap.policy.apex.service.engine.event.impl.jsonprotocolplugin.JsonEventProtocolParameters; +import org.onap.policy.common.parameters.ParameterService; + +class Apex2JmsTextEventConverterTest { + private Apex2JmsTextEventConverter converter; + + @BeforeEach + void setUp() { + converter = new Apex2JmsTextEventConverter(); + ModelService.registerModel(AxContextSchemas.class, new AxContextSchemas()); + ModelService.registerModel(AxEvents.class, new AxEvents()); + ParameterService.register(new SchemaParameters()); + } + + @AfterEach + void tearDown() { + ModelService.deregisterModel(AxContextSchema.class); + ModelService.deregisterModel(AxEvents.class); + ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME); + } + + @Test + void toApexEventNull() { + final String eventName = RandomStringUtils.randomAlphabetic(4); + assertThatThrownBy(() -> converter.toApexEvent(eventName, null)) + .isInstanceOf(ApexEventRuntimeException.class); + } + + @Test + void toApexEventObject() { + final String eventName = RandomStringUtils.randomAlphabetic(4); + var object = new Object(); + assertThatThrownBy(() -> converter.toApexEvent(eventName, object)).isInstanceOf( + ApexEventRuntimeException.class); + } + + @Test + void toApexEventJsonString() throws ApexEventException { + final String eventName = RandomStringUtils.randomAlphabetic(4); + final String eventVersion = "0.0.1"; + 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 = + new TextBlock(false, "{\"name\": \"" + eventName + "\", \"version\":\"" + eventVersion + "\"}"); + + // Prepare Model service + final AxArtifactKey eventKey = new AxArtifactKey(eventName + ":" + eventVersion); + final AxEvent axEvent = new AxEvent(eventKey, nameSpace, source, target); + ModelService.getModel(AxEvents.class).getEventMap().put(eventKey, axEvent); + + // prepare converter + converter.init(new JsonEventProtocolParameters()); + + // execute test + final List apexEvents = converter.toApexEvent(eventName, object); + + final ApexEvent expectedEvent = + new ApexEvent(eventName, eventVersion, nameSpace, source, target, toscaPolicyState); + + // Reset executionId + expectedEvent.setExecutionId(0); + for (ApexEvent event : apexEvents) { + event.setExecutionId(0); + } + Object[] expected = {expectedEvent}; + + assertArrayEquals(expected, apexEvents.toArray()); + } + + @Test + void fromApexNull() { + assertThatThrownBy(() -> converter.fromApexEvent(null)).isInstanceOf(ApexEventException.class); + } + + @Test + void fromApex() throws ApexEventException { + final String name = RandomStringUtils.randomAlphabetic(4); + final String version = "0.2.3"; + final String nameSpace = "a.name.space"; + 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" + + " \"toscaPolicyState\": null\n" + + "}"; + + // Prepare Model service + final AxArtifactKey eventKey = new AxArtifactKey(name + ":" + version); + final AxEvent axEvent = new AxEvent(eventKey, nameSpace, source, target); + ModelService.getModel(AxEvents.class).getEventMap().put(eventKey, axEvent); + + converter.init(new JsonEventProtocolParameters()); + + final ApexEvent apexEvent = new ApexEvent(name, version, nameSpace, source, target); + final Object actual = converter.fromApexEvent(apexEvent); + + assertEquals(expected, actual); + } + +} diff --git a/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/src/test/java/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 new file mode 100644 index 000000000..1f480a654 --- /dev/null +++ b/plugins/plugins-event/plugins-event-protocol/plugins-event-protocol-jms/src/test/java/org/onap/policy/apex/plugins/event/protocol/jms/JmsObjectEventProtocolParametersTest.java @@ -0,0 +1,53 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2021, 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.event.protocol.jms; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; + + +class JmsObjectEventProtocolParametersTest { + + @Test + void getIncomingEventVersion() { + final JmsObjectEventProtocolParameters jmsObjectEventProtocolParameters = + new JmsObjectEventProtocolParameters(); + final String actual = jmsObjectEventProtocolParameters.getIncomingEventVersion(); + assertEquals("1.0.0", actual); + } + + @Test + void getIncomingEventSource() { + final JmsObjectEventProtocolParameters jmsObjectEventProtocolParameters = + new JmsObjectEventProtocolParameters(); + final String actual = jmsObjectEventProtocolParameters.getIncomingEventSource(); + assertEquals("JMS", actual); + } + + @Test + void getIncomingEventTarget() { + final JmsObjectEventProtocolParameters jmsObjectEventProtocolParameters = + new JmsObjectEventProtocolParameters(); + final String actual = jmsObjectEventProtocolParameters.getIncomingEventTarget(); + assertEquals("Apex", actual); + } +} diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/runtime/impl/EnEventListenerImplTest.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/runtime/impl/EnEventListenerImplTest.java new file mode 100644 index 000000000..920901909 --- /dev/null +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/runtime/impl/EnEventListenerImplTest.java @@ -0,0 +1,105 @@ +/*- + * ============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.service.engine.runtime.impl; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.onap.policy.apex.core.engine.event.EnEvent; +import org.onap.policy.apex.model.basicmodel.concepts.ApexException; +import org.onap.policy.apex.service.engine.event.ApexEvent; +import org.onap.policy.apex.service.engine.event.impl.enevent.ApexEvent2EnEventConverter; +import org.onap.policy.apex.service.engine.runtime.ApexEventListener; + +class EnEventListenerImplTest { + + @Mock + private ApexEventListener mockApexEventListener; + + @Mock + private ApexEvent2EnEventConverter mockApexEnEventConverter; + + @Mock + private EnEvent mockEnEvent; + + @Mock + private ApexEvent mockApexEvent; + + @InjectMocks + private EnEventListenerImpl enEventListenerImpl; + + @BeforeEach + void setUp() { + MockitoAnnotations.openMocks(this); + } + + @Test + void testOnEnEvent_success() throws ApexException { + String eventName = "testEvent"; + when(mockEnEvent.getName()).thenReturn(eventName); + List apexEvents = Arrays.asList(mockApexEvent); + when(mockApexEnEventConverter.toApexEvent(eventName, mockEnEvent)).thenReturn(apexEvents); + + assertDoesNotThrow(() -> enEventListenerImpl.onEnEvent(mockEnEvent)); + + verify(mockApexEventListener, times(1)).onApexEvent(mockApexEvent); + } + + @Test + void testOnEnEvent_apexExceptionThrown() throws ApexException { + String eventName = "testEvent"; + when(mockEnEvent.getName()).thenReturn(eventName); + when(mockApexEnEventConverter.toApexEvent(eventName, mockEnEvent)) + .thenThrow(new ApexException("Conversion error")); + + assertThrows(ApexException.class, () -> enEventListenerImpl.onEnEvent(mockEnEvent)); + + verify(mockApexEventListener, never()).onApexEvent(any(ApexEvent.class)); + } + + @Test + void testOnEnEvent_multipleApexEvents() throws ApexException { + String eventName = "testEvent"; + when(mockEnEvent.getName()).thenReturn(eventName); + ApexEvent apexEvent1 = mock(ApexEvent.class); + ApexEvent apexEvent2 = mock(ApexEvent.class); + List apexEvents = Arrays.asList(apexEvent1, apexEvent2); + when(mockApexEnEventConverter.toApexEvent(eventName, mockEnEvent)).thenReturn(apexEvents); + + assertDoesNotThrow(() -> enEventListenerImpl.onEnEvent(mockEnEvent)); + + verify(mockApexEventListener, times(1)).onApexEvent(apexEvent1); + verify(mockApexEventListener, times(1)).onApexEvent(apexEvent2); + } +} diff --git a/tools/simple-wsclient/pom.xml b/tools/simple-wsclient/pom.xml index 2d7de02a5..cb737a4c1 100644 --- a/tools/simple-wsclient/pom.xml +++ b/tools/simple-wsclient/pom.xml @@ -30,6 +30,10 @@ ${project.artifactId} [${project.parent.artifactId}] Simple Websocket client (console and echo) + + true + + jakarta.websocket -- cgit 1.2.3-korg