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