2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Samsung. All rights reserved.
4 * Modifications Copyright (C) 2019-2021 Nordix Foundation.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.plugins.event.carrier.restserver;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertNull;
28 import static org.junit.Assert.assertSame;
30 import java.lang.reflect.Field;
31 import java.util.Random;
32 import org.apache.commons.lang3.RandomStringUtils;
33 import org.junit.After;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.onap.policy.apex.service.engine.event.ApexEventException;
37 import org.onap.policy.apex.service.engine.event.ApexEventReceiver;
38 import org.onap.policy.apex.service.engine.event.PeeredReference;
39 import org.onap.policy.apex.service.engine.event.SynchronousEventCache;
40 import org.onap.policy.apex.service.parameters.carriertechnology.CarrierTechnologyParameters;
41 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters;
42 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerPeeredMode;
44 public class ApexRestServerProducerTest {
46 ApexRestServerProducer apexRestServerProducer = null;
47 EventHandlerParameters producerParameters = null;
48 ApexEventReceiver incomingEventReceiver = null;
49 ApexRestServerConsumer apexRestServerConsumer = null;
50 RestServerCarrierTechnologyParameters restServerCarrierTechnologyParameters = null;
51 SynchronousEventCache synchronousEventCache = null;
52 Random random = new Random();
57 * @throws Exception on test set up errors.
60 public void setUp() throws Exception {
61 apexRestServerConsumer = new ApexRestServerConsumer();
62 producerParameters = new EventHandlerParameters();
63 apexRestServerProducer = new ApexRestServerProducer();
67 public void tearDown() {
68 apexRestServerProducer.stop();
71 @Test(expected = ApexEventException.class)
72 public void testInitWithNonWebSocketCarrierTechnologyParameters() throws ApexEventException {
73 producerParameters.setCarrierTechnologyParameters(new CarrierTechnologyParameters() {});
74 apexRestServerProducer.init("TestApexRestServerProducer", producerParameters);
77 @Test(expected = ApexEventException.class)
78 public void testInitWithWebSocketCarrierTechnologyParameters() throws ApexEventException {
79 restServerCarrierTechnologyParameters = new RestServerCarrierTechnologyParameters();
80 producerParameters.setCarrierTechnologyParameters(restServerCarrierTechnologyParameters);
81 apexRestServerProducer.init("TestApexRestServerProducer", producerParameters);
85 @Test(expected = ApexEventException.class)
86 public void testInitWithNonDefaultValue() throws ApexEventException, NoSuchFieldException,
87 SecurityException, IllegalArgumentException, IllegalAccessException {
88 restServerCarrierTechnologyParameters = new RestServerCarrierTechnologyParameters();
89 Field field = RestServerCarrierTechnologyParameters.class.getDeclaredField("host");
90 field.setAccessible(true);
91 field.set(restServerCarrierTechnologyParameters, "1ocalhost");
92 field = RestServerCarrierTechnologyParameters.class.getDeclaredField("port");
93 field.setAccessible(true);
94 field.set(restServerCarrierTechnologyParameters, 65535);
95 producerParameters.setCarrierTechnologyParameters(restServerCarrierTechnologyParameters);
96 apexRestServerProducer.init("TestApexRestServerProducer", producerParameters);
100 public void testInitWithSynchronousMode() throws ApexEventException {
101 restServerCarrierTechnologyParameters = new RestServerCarrierTechnologyParameters();
102 producerParameters.setCarrierTechnologyParameters(restServerCarrierTechnologyParameters);
103 producerParameters.setPeeredMode(EventHandlerPeeredMode.SYNCHRONOUS, true);
104 apexRestServerProducer.init("TestApexRestServerProducer", producerParameters);
105 assertEquals("TestApexRestServerProducer", apexRestServerProducer.getName());
109 public void testGetName() {
110 assertNull(apexRestServerProducer.getName());
114 public void testGetPeeredReference() {
115 assertNull(apexRestServerProducer.getPeeredReference(EventHandlerPeeredMode.REQUESTOR));
119 public void testSetPeeredReference() {
120 PeeredReference peeredReference = new PeeredReference(EventHandlerPeeredMode.REQUESTOR,
121 apexRestServerConsumer, apexRestServerProducer);
122 apexRestServerProducer.setPeeredReference(EventHandlerPeeredMode.REQUESTOR,
124 assertNotNull(apexRestServerProducer.getPeeredReference(EventHandlerPeeredMode.REQUESTOR));
128 public void testSendEventNotExistingEventToApex() {
129 final long executionId = random.nextLong();
130 final String eventName = RandomStringUtils.randomAlphabetic(7);
131 final Object event = new Object();
132 final ApexRestServerConsumer consumer = new ApexRestServerConsumer();
133 final SynchronousEventCache cache =
134 new SynchronousEventCache(EventHandlerPeeredMode.SYNCHRONOUS, consumer, apexRestServerProducer,
135 random.nextInt(1000));
137 this.apexRestServerProducer.setPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS, cache);
140 this.apexRestServerProducer.sendEvent(executionId, null, eventName, event);
142 assertFalse(cache.existsEventFromApex(executionId));
146 public void testSendEvent() {
147 final long executionId = random.nextLong();
148 final String eventName = RandomStringUtils.randomAlphabetic(7);
149 final Object expected = new Object();
151 final ApexRestServerConsumer consumer = new ApexRestServerConsumer();
152 final SynchronousEventCache cache = new SynchronousEventCache(
153 EventHandlerPeeredMode.SYNCHRONOUS,
155 apexRestServerProducer,
158 // Set EventToApex on cache object
159 cache.cacheSynchronizedEventToApex(executionId, new Object());
161 this.apexRestServerProducer.setPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS, cache);
163 this.apexRestServerProducer.sendEvent(executionId, null, eventName, expected);
164 final Object actual = cache.removeCachedEventFromApexIfExists(executionId);
166 assertSame(expected, actual);