068880a9d5205f79f170e21524c4dae8f2bc3f25
[policy/apex-pdp.git] /
1 /*
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.service.engine.event;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.assertj.core.api.Assertions.assertThatCode;
25
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;
32
33 public class SynchronousEventCacheTest {
34     private final Random random = new Random();
35     private ApexEventConsumer consumer;
36     private ApexEventProducer producer;
37
38     @Before
39     public void setUp() throws Exception {
40         consumer = new EventRequestorConsumer();
41         producer = new EventRequestorProducer();
42     }
43
44     @Test
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);
50
51         final Object actual = cache.removeCachedEventFromApexIfExists(executionId);
52         assertThat(actual).isNull();
53     }
54
55     @Test
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);
63
64         final Object actual = cache.removeCachedEventFromApexIfExists(executionId);
65         assertThat(actual).isSameAs(event);
66     }
67
68     @Test
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);
74
75         final Object actual = cache.removeCachedEventToApexIfExists(executionId);
76         assertThat(actual).isNull();
77     }
78
79     @Test
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);
87
88         final Object actual = cache.removeCachedEventToApexIfExists(executionId);
89         assertThat(actual).isSameAs(event);
90     }
91
92     @Test
93     public void apexExistsFromApexNo() {
94         int executionId = random.nextInt();
95         final SynchronousEventCache cache =
96             new SynchronousEventCache(EventHandlerPeeredMode.SYNCHRONOUS, consumer, producer, 0);
97
98         final boolean actual = cache.existsEventFromApex(executionId);
99         assertThat(actual).isFalse();
100     }
101
102     @Test
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());
108
109         final boolean actual = cache.existsEventFromApex(executionId);
110         assertThat(actual).isTrue();
111     }
112
113     @Test
114     public void apexExistsToApexNo() {
115         int executionId = random.nextInt();
116         final SynchronousEventCache cache =
117             new SynchronousEventCache(EventHandlerPeeredMode.SYNCHRONOUS, consumer, producer, 0);
118
119         final boolean actual = cache.existsEventToApex(executionId);
120         assertThat(actual).isFalse();
121     }
122
123     @Test
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());
129
130         final boolean actual = cache.existsEventToApex(executionId);
131         assertThat(actual).isTrue();
132     }
133
134     @Test
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);
140
141         assertThatCode(() -> {
142             cache.cacheSynchronizedEventFromApex(executionId, new Object());
143             cache.cacheSynchronizedEventFromApex(executionId, new Object());
144         })
145             .isInstanceOf(ApexEventRuntimeException.class);
146     }
147
148     @Test
149     public void stop() {
150         int timeout = random.nextInt(100);
151         final SynchronousEventCache cache =
152             new SynchronousEventCache(EventHandlerPeeredMode.SYNCHRONOUS, consumer, producer, timeout);
153         assertThatCode(cache::stop)
154             .doesNotThrowAnyException();
155     }
156
157     @Test
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());
163             cache.stop();
164         })
165             .doesNotThrowAnyException();
166     }
167 }