2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021. Nordix Foundation.
4 * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.service.engine.event;
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.assertj.core.api.Assertions.assertThatCode;
27 import java.util.Random;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.onap.policy.apex.service.engine.event.impl.eventrequestor.EventRequestorConsumer;
31 import org.onap.policy.apex.service.engine.event.impl.eventrequestor.EventRequestorProducer;
32 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerPeeredMode;
34 public class SynchronousEventCacheTest {
35 private final Random random = new Random();
36 private ApexEventConsumer consumer;
37 private ApexEventProducer producer;
40 public void setUp() throws Exception {
41 consumer = new EventRequestorConsumer();
42 producer = new EventRequestorProducer();
46 public void removedCachedFromApexNotExists() {
47 int timeout = random.nextInt(100);
48 int executionId = random.nextInt();
49 final SynchronousEventCache cache =
50 new SynchronousEventCache(EventHandlerPeeredMode.SYNCHRONOUS, consumer, producer, timeout);
52 final Object actual = cache.removeCachedEventFromApexIfExists(executionId);
53 assertThat(actual).isNull();
57 public void removeCachedFromApex() {
58 int timeout = random.nextInt(100);
59 int executionId = random.nextInt();
60 final SynchronousEventCache cache =
61 new SynchronousEventCache(EventHandlerPeeredMode.SYNCHRONOUS, consumer, producer, timeout);
62 final Object event = new Object();
63 cache.cacheSynchronizedEventFromApex(executionId, event);
65 final Object actual = cache.removeCachedEventFromApexIfExists(executionId);
66 assertThat(actual).isSameAs(event);
70 public void removedCachedToApexNotExists() {
71 int timeout = random.nextInt(100);
72 int executionId = random.nextInt();
73 final SynchronousEventCache cache =
74 new SynchronousEventCache(EventHandlerPeeredMode.SYNCHRONOUS, consumer, producer, timeout);
76 final Object actual = cache.removeCachedEventToApexIfExists(executionId);
77 assertThat(actual).isNull();
81 public void removeCachedToApex() {
82 int timeout = random.nextInt(100);
83 int executionId = random.nextInt();
84 final SynchronousEventCache cache =
85 new SynchronousEventCache(EventHandlerPeeredMode.SYNCHRONOUS, consumer, producer, timeout);
86 final Object event = new Object();
87 cache.cacheSynchronizedEventToApex(executionId, event);
89 final Object actual = cache.removeCachedEventToApexIfExists(executionId);
90 assertThat(actual).isSameAs(event);
94 public void apexExistsFromApexNo() {
95 int executionId = random.nextInt();
96 final SynchronousEventCache cache =
97 new SynchronousEventCache(EventHandlerPeeredMode.SYNCHRONOUS, consumer, producer, 0);
99 final boolean actual = cache.existsEventFromApex(executionId);
100 assertThat(actual).isFalse();
104 public void apexExistsFromApexYes() {
105 int executionId = random.nextInt();
106 final SynchronousEventCache cache =
107 new SynchronousEventCache(EventHandlerPeeredMode.SYNCHRONOUS, consumer, producer, 0);
108 cache.cacheSynchronizedEventFromApex(executionId, new Object());
110 final boolean actual = cache.existsEventFromApex(executionId);
111 assertThat(actual).isTrue();
115 public void apexExistsToApexNo() {
116 int executionId = random.nextInt();
117 final SynchronousEventCache cache =
118 new SynchronousEventCache(EventHandlerPeeredMode.SYNCHRONOUS, consumer, producer, 0);
120 final boolean actual = cache.existsEventToApex(executionId);
121 assertThat(actual).isFalse();
125 public void apexExistsToApexYes() {
126 int executionId = random.nextInt();
127 final SynchronousEventCache cache =
128 new SynchronousEventCache(EventHandlerPeeredMode.SYNCHRONOUS, consumer, producer, 0);
129 cache.cacheSynchronizedEventToApex(executionId, new Object());
131 final boolean actual = cache.existsEventToApex(executionId);
132 assertThat(actual).isTrue();
136 public void addEventsFromApexDuplicatedExecutionId() {
137 int timeout = random.nextInt(100);
138 int executionId = random.nextInt();
139 final SynchronousEventCache cache =
140 new SynchronousEventCache(EventHandlerPeeredMode.SYNCHRONOUS, consumer, producer, timeout);
142 final var obj1 = new Object();
143 cache.cacheSynchronizedEventFromApex(executionId, obj1);
145 final var obj2 = new Object();
146 assertThatCode(() -> cache.cacheSynchronizedEventFromApex(executionId, obj2))
147 .isInstanceOf(ApexEventRuntimeException.class);
152 int timeout = random.nextInt(100);
153 final SynchronousEventCache cache =
154 new SynchronousEventCache(EventHandlerPeeredMode.SYNCHRONOUS, consumer, producer, timeout);
155 assertThatCode(cache::stop)
156 .doesNotThrowAnyException();
160 public void stopNotEmpty() {
161 final SynchronousEventCache cache =
162 new SynchronousEventCache(EventHandlerPeeredMode.SYNCHRONOUS, consumer, producer, 2000);
163 assertThatCode(() -> {
164 cache.cacheSynchronizedEventToApex(random.nextInt(), new Object());
167 .doesNotThrowAnyException();