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
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 * SPDX-License-Identifier: Apache-2.0
17 * ============LICENSE_END=========================================================
20 package org.onap.policy.apex.service.engine.event.impl.eventrequestor;
22 import static org.assertj.core.api.Assertions.assertThatThrownBy;
23 import static org.junit.Assert.assertEquals;
25 import java.util.Random;
26 import org.apache.commons.lang3.RandomStringUtils;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.mockito.Matchers;
30 import org.mockito.Mock;
31 import org.mockito.Mockito;
32 import org.mockito.MockitoAnnotations;
33 import org.onap.policy.apex.service.engine.event.ApexEventException;
34 import org.onap.policy.apex.service.engine.event.ApexEventProducer;
35 import org.onap.policy.apex.service.engine.event.ApexEventRuntimeException;
36 import org.onap.policy.apex.service.engine.event.PeeredReference;
37 import org.onap.policy.apex.service.engine.event.SynchronousEventCache;
38 import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.consumer.ApexFileEventConsumer;
39 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters;
40 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerPeeredMode;
42 public class EventRequestorProducerTest {
43 private final Random random = new Random();
44 private EventRequestorProducer producer;
47 private ApexEventProducer apexProducer;
49 private EventRequestorConsumer apexConsumer;
52 public void setUp() throws Exception {
53 MockitoAnnotations.initMocks(this);
54 producer = new EventRequestorProducer();
58 public void initWithEmptyParams() {
59 final String producerName = RandomStringUtils.random(4);
60 final EventHandlerParameters eventHandlerParameters = new EventHandlerParameters();
62 assertThatThrownBy(() -> producer.init(producerName, eventHandlerParameters))
63 .isInstanceOf(ApexEventException.class);
67 public void initNotPeered() {
68 final String producerName = RandomStringUtils.random(4);
69 final EventHandlerParameters eventHandlerParameters = new EventHandlerParameters();
70 eventHandlerParameters.setCarrierTechnologyParameters(new EventRequestorCarrierTechnologyParameters());
72 assertThatThrownBy(() -> producer.init(producerName, eventHandlerParameters))
73 .isInstanceOf(ApexEventException.class);
77 public void getName() throws ApexEventException {
78 final String expected = RandomStringUtils.random(4);
79 final EventHandlerParameters eventHandlerParameters = new EventHandlerParameters();
80 eventHandlerParameters.setCarrierTechnologyParameters(new EventRequestorCarrierTechnologyParameters());
81 eventHandlerParameters.setPeeredMode(EventHandlerPeeredMode.REQUESTOR, true);
83 producer.init(expected, eventHandlerParameters);
84 final String actual = producer.getName();
86 assertEquals(expected, actual);
90 public void getSetPeeredReference() {
91 final PeeredReference peeredReference = new PeeredReference(EventHandlerPeeredMode.SYNCHRONOUS, apexConsumer,
93 producer.setPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS, peeredReference);
95 final PeeredReference actual = this.producer.getPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS);
96 assertEquals(peeredReference, actual);
100 public void sendEventNoRequestor() {
101 final int id = random.nextInt(1000);
103 assertThatThrownBy(() -> producer.sendEvent(id, null, null, null))
104 .isInstanceOf(ApexEventRuntimeException.class);
108 public void sendEventNoEventRequestorConsumer() {
109 final int id = random.nextInt(1000);
111 final ApexFileEventConsumer fileEventConsumer = Mockito.mock(ApexFileEventConsumer.class);
113 final PeeredReference reference =
114 new PeeredReference(EventHandlerPeeredMode.REQUESTOR, fileEventConsumer, apexProducer);
116 producer.setPeeredReference(EventHandlerPeeredMode.REQUESTOR, reference);
118 assertThatThrownBy(() -> producer.sendEvent(id, null, null, null))
119 .isInstanceOf(ApexEventRuntimeException.class);
123 public void sendEvent() {
124 final int id = random.nextInt(1000);
126 final PeeredReference peeredReference = Mockito.mock(PeeredReference.class);
128 Mockito.when(apexConsumer.getPeeredReference(Matchers.any())).thenReturn(peeredReference);
129 Mockito.when(peeredReference.getPeeredConsumer()).thenReturn(apexConsumer);
131 final PeeredReference reference =
132 new PeeredReference(EventHandlerPeeredMode.REQUESTOR, apexConsumer, apexProducer);
133 producer.setPeeredReference(EventHandlerPeeredMode.REQUESTOR, reference);
135 producer.sendEvent(id, null, null, null);
136 Mockito.verify(apexConsumer, Mockito.times(1)).processEvent(Matchers.any());
140 public void sendEventCached() {
141 final int id = random.nextInt(1000);
144 final SynchronousEventCache eventCache = Mockito.mock(SynchronousEventCache.class);
145 producer.setPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS, eventCache);
147 // Prepare other mocks
148 final PeeredReference peeredReference = Mockito.mock(PeeredReference.class);
150 Mockito.when(peeredReference.getPeeredConsumer()).thenReturn(apexConsumer);
151 Mockito.when(apexConsumer.getPeeredReference(Matchers.any())).thenReturn(peeredReference);
153 final PeeredReference reference =
154 new PeeredReference(EventHandlerPeeredMode.REQUESTOR, apexConsumer, apexProducer);
155 producer.setPeeredReference(EventHandlerPeeredMode.REQUESTOR, reference);
157 producer.sendEvent(id, null, null, null);
158 Mockito.verify(apexConsumer, Mockito.times(1)).processEvent(Matchers.any());
159 Mockito.verify(eventCache, Mockito.times(1)).removeCachedEventToApexIfExists(id);