aboutsummaryrefslogtreecommitdiffstats
path: root/services/services-engine/src/test/java/org/onap/policy/apex/service/engine/event/impl/eventrequestor/EventRequestorProducerTest.java
blob: 2f4764fb55d0cfdc77c05e407fa38f148005568a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2021  Nordix Foundation
 *  ================================================================================
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *  SPDX-License-Identifier: Apache-2.0
 *  ============LICENSE_END=========================================================
 */

package org.onap.policy.apex.service.engine.event.impl.eventrequestor;

import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.Assert.assertEquals;

import java.util.Random;
import org.apache.commons.lang3.RandomStringUtils;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Matchers;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.onap.policy.apex.service.engine.event.ApexEventException;
import org.onap.policy.apex.service.engine.event.ApexEventProducer;
import org.onap.policy.apex.service.engine.event.ApexEventRuntimeException;
import org.onap.policy.apex.service.engine.event.PeeredReference;
import org.onap.policy.apex.service.engine.event.SynchronousEventCache;
import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.consumer.ApexFileEventConsumer;
import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters;
import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerPeeredMode;

public class EventRequestorProducerTest {
    private final Random random = new Random();
    private EventRequestorProducer producer;

    @Mock
    private ApexEventProducer apexProducer;
    @Mock
    private EventRequestorConsumer apexConsumer;

    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
        producer = new EventRequestorProducer();
    }

    @Test
    public void initWithEmptyParams() {
        final String producerName = RandomStringUtils.random(4);
        final EventHandlerParameters eventHandlerParameters = new EventHandlerParameters();

        assertThatThrownBy(() -> producer.init(producerName, eventHandlerParameters))
            .isInstanceOf(ApexEventException.class);
    }

    @Test
    public void initNotPeered() {
        final String producerName = RandomStringUtils.random(4);
        final EventHandlerParameters eventHandlerParameters = new EventHandlerParameters();
        eventHandlerParameters.setCarrierTechnologyParameters(new EventRequestorCarrierTechnologyParameters());

        assertThatThrownBy(() -> producer.init(producerName, eventHandlerParameters))
            .isInstanceOf(ApexEventException.class);
    }

    @Test
    public void getName() throws ApexEventException {
        final String expected = RandomStringUtils.random(4);
        final EventHandlerParameters eventHandlerParameters = new EventHandlerParameters();
        eventHandlerParameters.setCarrierTechnologyParameters(new EventRequestorCarrierTechnologyParameters());
        eventHandlerParameters.setPeeredMode(EventHandlerPeeredMode.REQUESTOR, true);

        producer.init(expected, eventHandlerParameters);
        final String actual = producer.getName();

        assertEquals(expected, actual);
    }

    @Test
    public void getSetPeeredReference() {
        final PeeredReference peeredReference = new PeeredReference(EventHandlerPeeredMode.SYNCHRONOUS, apexConsumer,
            apexProducer);
        producer.setPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS, peeredReference);

        final PeeredReference actual = this.producer.getPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS);
        assertEquals(peeredReference, actual);
    }

    @Test
    public void sendEventNoRequestor() {
        final int id = random.nextInt(1000);

        assertThatThrownBy(() -> producer.sendEvent(id, null, null, null))
            .isInstanceOf(ApexEventRuntimeException.class);
    }

    @Test
    public void sendEventNoEventRequestorConsumer() {
        final int id = random.nextInt(1000);

        final ApexFileEventConsumer fileEventConsumer = Mockito.mock(ApexFileEventConsumer.class);

        final PeeredReference reference =
            new PeeredReference(EventHandlerPeeredMode.REQUESTOR, fileEventConsumer, apexProducer);

        producer.setPeeredReference(EventHandlerPeeredMode.REQUESTOR, reference);

        assertThatThrownBy(() -> producer.sendEvent(id, null, null, null))
            .isInstanceOf(ApexEventRuntimeException.class);
    }

    @Test
    public void sendEvent() {
        final int id = random.nextInt(1000);
        // Prepare mocks
        final PeeredReference peeredReference = Mockito.mock(PeeredReference.class);

        Mockito.when(apexConsumer.getPeeredReference(Matchers.any())).thenReturn(peeredReference);
        Mockito.when(peeredReference.getPeeredConsumer()).thenReturn(apexConsumer);

        final PeeredReference reference =
            new PeeredReference(EventHandlerPeeredMode.REQUESTOR, apexConsumer, apexProducer);
        producer.setPeeredReference(EventHandlerPeeredMode.REQUESTOR, reference);

        producer.sendEvent(id, null, null, null);
        Mockito.verify(apexConsumer, Mockito.times(1)).processEvent(Matchers.any());
    }

    @Test
    public void sendEventCached() {
        final int id = random.nextInt(1000);

        // Set event cache
        final SynchronousEventCache eventCache = Mockito.mock(SynchronousEventCache.class);
        producer.setPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS, eventCache);

        // Prepare other mocks
        final PeeredReference peeredReference = Mockito.mock(PeeredReference.class);

        Mockito.when(peeredReference.getPeeredConsumer()).thenReturn(apexConsumer);
        Mockito.when(apexConsumer.getPeeredReference(Matchers.any())).thenReturn(peeredReference);

        final PeeredReference reference =
            new PeeredReference(EventHandlerPeeredMode.REQUESTOR, apexConsumer, apexProducer);
        producer.setPeeredReference(EventHandlerPeeredMode.REQUESTOR, reference);

        producer.sendEvent(id, null, null, null);
        Mockito.verify(apexConsumer, Mockito.times(1)).processEvent(Matchers.any());
        Mockito.verify(eventCache, Mockito.times(1)).removeCachedEventToApexIfExists(id);
    }
}