From e66664a45f2f0819019855ab4fe831aab5dabd59 Mon Sep 17 00:00:00 2001 From: "arkadiusz.adamski" Date: Tue, 27 Apr 2021 09:17:47 +0100 Subject: Code coverage for Service Engine Event - Increase code coverage for service engine events Issue-ID: POLICY-3092 Signed-off-by: arkadiusz.adamski Change-Id: I40bda05fc53168d86ec4ac1c72b69a1badb02b38 --- services/services-engine/pom.xml | 8 +- .../apex/service/engine/event/ApexEventTest.java | 164 ++++++++++++++++++++ .../event/ApexPeriodicEventGeneratorTest.java | 45 ++++++ .../engine/event/SynchronousEventCacheTest.java | 167 +++++++++++++++++++++ 4 files changed, 381 insertions(+), 3 deletions(-) create mode 100644 services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/ApexEventTest.java create mode 100644 services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/ApexPeriodicEventGeneratorTest.java create mode 100644 services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/SynchronousEventCacheTest.java (limited to 'services') diff --git a/services/services-engine/pom.xml b/services/services-engine/pom.xml index dff9840ca..0749f6db9 100644 --- a/services/services-engine/pom.xml +++ b/services/services-engine/pom.xml @@ -18,7 +18,8 @@ SPDX-License-Identifier: Apache-2.0 ============LICENSE_END========================================================= --> - + 4.0.0 org.onap.policy.apex-pdp.services @@ -28,7 +29,8 @@ services-engine ${project.artifactId} - External services and infrastructure for adding plugins to the Apex policy execution engine + External services and infrastructure for adding plugins to the Apex policy execution engine + @@ -60,7 +62,7 @@ org.mockito - mockito-all + mockito-core test diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/ApexEventTest.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/ApexEventTest.java new file mode 100644 index 000000000..bb299a834 --- /dev/null +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/ApexEventTest.java @@ -0,0 +1,164 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021. 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.event; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.Map; +import java.util.Properties; +import java.util.Random; +import org.apache.commons.lang3.RandomStringUtils; +import org.assertj.core.api.Assertions; +import org.junit.Before; +import org.junit.Test; + +public class ApexEventTest { + private final Random random = new Random(); + private ApexEvent apexEvent; + + /** + * Prepares tests. + * + * @throws Exception when object is created + */ + @Before + public void setUp() throws Exception { + apexEvent = + new ApexEvent("name", "version", "namespace", "source", "target"); + + } + + @Test + public void invalidEventName() { + final String name = "++" + RandomStringUtils.randomAlphabetic(5); + Assertions.assertThatCode(() -> + apexEvent = new ApexEvent(name, "version", "namespace", "source", "target")) + .isInstanceOf(ApexEventException.class); + } + + @Test + public void invalidEventVersion() { + final String version = "++" + RandomStringUtils.randomAlphabetic(5); + Assertions.assertThatCode(() -> + apexEvent = new ApexEvent("name", version, "namespace", "source", "target")) + .isInstanceOf(ApexEventException.class); + } + + @Test + public void invalidEventNamespace() { + final String namespace = "++" + RandomStringUtils.randomAlphabetic(5); + Assertions.assertThatCode(() -> + apexEvent = new ApexEvent("name", "version", namespace, "source", "target")) + .isInstanceOf(ApexEventException.class); + } + + @Test + public void invalidEventSource() { + final String source = "++" + RandomStringUtils.randomAlphabetic(5); + Assertions.assertThatCode(() -> + apexEvent = new ApexEvent("name", "version", "namespace", source, "target")) + .isInstanceOf(ApexEventException.class); + } + + @Test + public void invalidEventTarget() { + final String target = "++" + RandomStringUtils.randomAlphabetic(5); + Assertions.assertThatCode(() -> + apexEvent = new ApexEvent("name", "version", "namespace", "source", target)) + .isInstanceOf(ApexEventException.class); + } + + @Test + public void setExecutionId() { + final int executionId = random.nextInt(); + apexEvent.setExecutionId(executionId); + final long actual = apexEvent.getExecutionId(); + assertThat(actual).isEqualTo(executionId); + } + + @Test + public void setExecutionProperties() { + final Properties properties = new Properties(); + apexEvent.setExecutionProperties(properties); + final Properties actual = apexEvent.getExecutionProperties(); + assertThat(actual).isSameAs(properties); + } + + @Test + public void setExceptionMessage() { + final String message = RandomStringUtils.randomAlphabetic(25); + apexEvent.setExceptionMessage(message); + final String actual = apexEvent.getExceptionMessage(); + assertThat(actual).isEqualTo(message); + } + + @Test + public void put() { + final String key = RandomStringUtils.randomAlphabetic(5); + final Object event = new Object(); + apexEvent.put(key, event); + final Object actual = apexEvent.get(key); + assertThat(actual).isEqualTo(event); + } + + @Test + public void put2() { + final String key = "_#+@" + RandomStringUtils.randomAlphabetic(5); + final Object event = new Object(); + apexEvent.put(key, event); + final Object actual = apexEvent.get(key); + assertThat(actual).isNull(); + } + + @Test + public void putAll() { + final String key1 = RandomStringUtils.randomAlphabetic(5); + final String key2 = RandomStringUtils.randomAlphabetic(6); + final Object event1 = new Object(); + final Object event2 = new Object(); + final Map events = Map.of(key1, event1, key2, event2); + apexEvent.putAll(events); + final Object actual1 = apexEvent.get(key1); + final Object actual3 = apexEvent.get(key2); + assertThat(actual1).isEqualTo(event1); + assertThat(actual3).isEqualTo(event2); + } + + @Test + public void putAllOneInvalidKey() { + final String key1 = RandomStringUtils.randomAlphabetic(5); + final String key2 = "_#+@" + RandomStringUtils.randomAlphabetic(6); + final String key3 = RandomStringUtils.randomAlphabetic(7); + final Object event1 = new Object(); + final Object event2 = new Object(); + final Object event3 = new Object(); + final Map events = Map.of(key1, event1, key2, event2, key3, event3); + + apexEvent.putAll(events); + + final Object actual1 = apexEvent.get(key1); + final Object actual2 = apexEvent.get(key2); + final Object actual3 = apexEvent.get(key3); + assertThat(actual1).isNull(); + assertThat(actual2).isNull(); + assertThat(actual3).isNull(); + } +} diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/ApexPeriodicEventGeneratorTest.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/ApexPeriodicEventGeneratorTest.java new file mode 100644 index 000000000..e40e570cb --- /dev/null +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/ApexPeriodicEventGeneratorTest.java @@ -0,0 +1,45 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021. 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.event; + +import java.util.Random; +import org.junit.Test; +import org.mockito.ArgumentMatchers; +import org.mockito.Mockito; +import org.onap.policy.apex.service.engine.runtime.EngineServiceEventInterface; + +public class ApexPeriodicEventGeneratorTest { + private final Random random = new Random(); + + @Test + public void run() throws ApexEventException { + + final EngineServiceEventInterface engineServiceEventInterface = Mockito.mock(EngineServiceEventInterface.class); + final int period = random.nextInt(200); + final ApexPeriodicEventGenerator generator = new ApexPeriodicEventGenerator(engineServiceEventInterface, + period); + + generator.run(); + Mockito + .verify(engineServiceEventInterface, Mockito.times(1)) + .sendEvent(ArgumentMatchers.any(ApexEvent.class)); + } +} diff --git a/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/SynchronousEventCacheTest.java b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/SynchronousEventCacheTest.java new file mode 100644 index 000000000..068880a9d --- /dev/null +++ b/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/SynchronousEventCacheTest.java @@ -0,0 +1,167 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021. 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.event; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatCode; + +import java.util.Random; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.apex.service.engine.event.impl.eventrequestor.EventRequestorConsumer; +import org.onap.policy.apex.service.engine.event.impl.eventrequestor.EventRequestorProducer; +import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerPeeredMode; + +public class SynchronousEventCacheTest { + private final Random random = new Random(); + private ApexEventConsumer consumer; + private ApexEventProducer producer; + + @Before + public void setUp() throws Exception { + consumer = new EventRequestorConsumer(); + producer = new EventRequestorProducer(); + } + + @Test + public void removedCachedFromApexNotExists() { + int timeout = random.nextInt(100); + int executionId = random.nextInt(); + final SynchronousEventCache cache = + new SynchronousEventCache(EventHandlerPeeredMode.SYNCHRONOUS, consumer, producer, timeout); + + final Object actual = cache.removeCachedEventFromApexIfExists(executionId); + assertThat(actual).isNull(); + } + + @Test + public void removeCachedFromApex() { + int timeout = random.nextInt(100); + int executionId = random.nextInt(); + final SynchronousEventCache cache = + new SynchronousEventCache(EventHandlerPeeredMode.SYNCHRONOUS, consumer, producer, timeout); + final Object event = new Object(); + cache.cacheSynchronizedEventFromApex(executionId, event); + + final Object actual = cache.removeCachedEventFromApexIfExists(executionId); + assertThat(actual).isSameAs(event); + } + + @Test + public void removedCachedToApexNotExists() { + int timeout = random.nextInt(100); + int executionId = random.nextInt(); + final SynchronousEventCache cache = + new SynchronousEventCache(EventHandlerPeeredMode.SYNCHRONOUS, consumer, producer, timeout); + + final Object actual = cache.removeCachedEventToApexIfExists(executionId); + assertThat(actual).isNull(); + } + + @Test + public void removeCachedToApex() { + int timeout = random.nextInt(100); + int executionId = random.nextInt(); + final SynchronousEventCache cache = + new SynchronousEventCache(EventHandlerPeeredMode.SYNCHRONOUS, consumer, producer, timeout); + final Object event = new Object(); + cache.cacheSynchronizedEventToApex(executionId, event); + + final Object actual = cache.removeCachedEventToApexIfExists(executionId); + assertThat(actual).isSameAs(event); + } + + @Test + public void apexExistsFromApexNo() { + int executionId = random.nextInt(); + final SynchronousEventCache cache = + new SynchronousEventCache(EventHandlerPeeredMode.SYNCHRONOUS, consumer, producer, 0); + + final boolean actual = cache.existsEventFromApex(executionId); + assertThat(actual).isFalse(); + } + + @Test + public void apexExistsFromApexYes() { + int executionId = random.nextInt(); + final SynchronousEventCache cache = + new SynchronousEventCache(EventHandlerPeeredMode.SYNCHRONOUS, consumer, producer, 0); + cache.cacheSynchronizedEventFromApex(executionId, new Object()); + + final boolean actual = cache.existsEventFromApex(executionId); + assertThat(actual).isTrue(); + } + + @Test + public void apexExistsToApexNo() { + int executionId = random.nextInt(); + final SynchronousEventCache cache = + new SynchronousEventCache(EventHandlerPeeredMode.SYNCHRONOUS, consumer, producer, 0); + + final boolean actual = cache.existsEventToApex(executionId); + assertThat(actual).isFalse(); + } + + @Test + public void apexExistsToApexYes() { + int executionId = random.nextInt(); + final SynchronousEventCache cache = + new SynchronousEventCache(EventHandlerPeeredMode.SYNCHRONOUS, consumer, producer, 0); + cache.cacheSynchronizedEventToApex(executionId, new Object()); + + final boolean actual = cache.existsEventToApex(executionId); + assertThat(actual).isTrue(); + } + + @Test + public void addEventsFromApexDuplicatedExecutionId() { + int timeout = random.nextInt(100); + int executionId = random.nextInt(); + final SynchronousEventCache cache = + new SynchronousEventCache(EventHandlerPeeredMode.SYNCHRONOUS, consumer, producer, timeout); + + assertThatCode(() -> { + cache.cacheSynchronizedEventFromApex(executionId, new Object()); + cache.cacheSynchronizedEventFromApex(executionId, new Object()); + }) + .isInstanceOf(ApexEventRuntimeException.class); + } + + @Test + public void stop() { + int timeout = random.nextInt(100); + final SynchronousEventCache cache = + new SynchronousEventCache(EventHandlerPeeredMode.SYNCHRONOUS, consumer, producer, timeout); + assertThatCode(cache::stop) + .doesNotThrowAnyException(); + } + + @Test + public void stopNotEmpty() { + final SynchronousEventCache cache = + new SynchronousEventCache(EventHandlerPeeredMode.SYNCHRONOUS, consumer, producer, 2000); + assertThatCode(() -> { + cache.cacheSynchronizedEventToApex(random.nextInt(), new Object()); + cache.stop(); + }) + .doesNotThrowAnyException(); + } +} -- cgit 1.2.3-korg