2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021. Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.service.engine.event;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.assertj.core.api.Assertions.assertThatCode;
26 import java.util.Random;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.onap.policy.apex.service.engine.event.impl.eventrequestor.EventRequestorConsumer;
30 import org.onap.policy.apex.service.engine.event.impl.eventrequestor.EventRequestorProducer;
31 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerPeeredMode;
33 public class SynchronousEventCacheTest {
34 private final Random random = new Random();
35 private ApexEventConsumer consumer;
36 private ApexEventProducer producer;
39 public void setUp() throws Exception {
40 consumer = new EventRequestorConsumer();
41 producer = new EventRequestorProducer();
45 public void removedCachedFromApexNotExists() {
46 int timeout = random.nextInt(100);
47 int executionId = random.nextInt();
48 final SynchronousEventCache cache =
49 new SynchronousEventCache(EventHandlerPeeredMode.SYNCHRONOUS, consumer, producer, timeout);
51 final Object actual = cache.removeCachedEventFromApexIfExists(executionId);
52 assertThat(actual).isNull();
56 public void removeCachedFromApex() {
57 int timeout = random.nextInt(100);
58 int executionId = random.nextInt();
59 final SynchronousEventCache cache =
60 new SynchronousEventCache(EventHandlerPeeredMode.SYNCHRONOUS, consumer, producer, timeout);
61 final Object event = new Object();
62 cache.cacheSynchronizedEventFromApex(executionId, event);
64 final Object actual = cache.removeCachedEventFromApexIfExists(executionId);
65 assertThat(actual).isSameAs(event);
69 public void removedCachedToApexNotExists() {
70 int timeout = random.nextInt(100);
71 int executionId = random.nextInt();
72 final SynchronousEventCache cache =
73 new SynchronousEventCache(EventHandlerPeeredMode.SYNCHRONOUS, consumer, producer, timeout);
75 final Object actual = cache.removeCachedEventToApexIfExists(executionId);
76 assertThat(actual).isNull();
80 public void removeCachedToApex() {
81 int timeout = random.nextInt(100);
82 int executionId = random.nextInt();
83 final SynchronousEventCache cache =
84 new SynchronousEventCache(EventHandlerPeeredMode.SYNCHRONOUS, consumer, producer, timeout);
85 final Object event = new Object();
86 cache.cacheSynchronizedEventToApex(executionId, event);
88 final Object actual = cache.removeCachedEventToApexIfExists(executionId);
89 assertThat(actual).isSameAs(event);
93 public void apexExistsFromApexNo() {
94 int executionId = random.nextInt();
95 final SynchronousEventCache cache =
96 new SynchronousEventCache(EventHandlerPeeredMode.SYNCHRONOUS, consumer, producer, 0);
98 final boolean actual = cache.existsEventFromApex(executionId);
99 assertThat(actual).isFalse();
103 public void apexExistsFromApexYes() {
104 int executionId = random.nextInt();
105 final SynchronousEventCache cache =
106 new SynchronousEventCache(EventHandlerPeeredMode.SYNCHRONOUS, consumer, producer, 0);
107 cache.cacheSynchronizedEventFromApex(executionId, new Object());
109 final boolean actual = cache.existsEventFromApex(executionId);
110 assertThat(actual).isTrue();
114 public void apexExistsToApexNo() {
115 int executionId = random.nextInt();
116 final SynchronousEventCache cache =
117 new SynchronousEventCache(EventHandlerPeeredMode.SYNCHRONOUS, consumer, producer, 0);
119 final boolean actual = cache.existsEventToApex(executionId);
120 assertThat(actual).isFalse();
124 public void apexExistsToApexYes() {
125 int executionId = random.nextInt();
126 final SynchronousEventCache cache =
127 new SynchronousEventCache(EventHandlerPeeredMode.SYNCHRONOUS, consumer, producer, 0);
128 cache.cacheSynchronizedEventToApex(executionId, new Object());
130 final boolean actual = cache.existsEventToApex(executionId);
131 assertThat(actual).isTrue();
135 public void addEventsFromApexDuplicatedExecutionId() {
136 int timeout = random.nextInt(100);
137 int executionId = random.nextInt();
138 final SynchronousEventCache cache =
139 new SynchronousEventCache(EventHandlerPeeredMode.SYNCHRONOUS, consumer, producer, timeout);
141 assertThatCode(() -> {
142 cache.cacheSynchronizedEventFromApex(executionId, new Object());
143 cache.cacheSynchronizedEventFromApex(executionId, new Object());
145 .isInstanceOf(ApexEventRuntimeException.class);
150 int timeout = random.nextInt(100);
151 final SynchronousEventCache cache =
152 new SynchronousEventCache(EventHandlerPeeredMode.SYNCHRONOUS, consumer, producer, timeout);
153 assertThatCode(cache::stop)
154 .doesNotThrowAnyException();
158 public void stopNotEmpty() {
159 final SynchronousEventCache cache =
160 new SynchronousEventCache(EventHandlerPeeredMode.SYNCHRONOUS, consumer, producer, 2000);
161 assertThatCode(() -> {
162 cache.cacheSynchronizedEventToApex(random.nextInt(), new Object());
165 .doesNotThrowAnyException();