diff options
4 files changed, 381 insertions, 3 deletions
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========================================================= --> -<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.onap.policy.apex-pdp.services</groupId> @@ -28,7 +29,8 @@ <artifactId>services-engine</artifactId> <name>${project.artifactId}</name> - <description>External services and infrastructure for adding plugins to the Apex policy execution engine</description> + <description>External services and infrastructure for adding plugins to the Apex policy execution engine + </description> <dependencies> <dependency> @@ -60,7 +62,7 @@ </dependency> <dependency> <groupId>org.mockito</groupId> - <artifactId>mockito-all</artifactId> + <artifactId>mockito-core</artifactId> <scope>test</scope> </dependency> <dependency> 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<String, Object> 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<String, Object> 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(); + } +} |