34992ff1d0b1dc465acba63c3fccd5537700d352
[policy/apex-pdp.git] /
1 /*-
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
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.plugins.event.carrier.restserver;
23
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;
29
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;
43
44 public class ApexRestServerProducerTest {
45
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();
53
54     /**
55      * Set up testing.
56      *
57      * @throws Exception on test set up errors.
58      */
59     @Before
60     public void setUp() throws Exception {
61         apexRestServerConsumer = new ApexRestServerConsumer();
62         producerParameters = new EventHandlerParameters();
63         apexRestServerProducer = new ApexRestServerProducer();
64     }
65
66     @After
67     public void tearDown() {
68         apexRestServerProducer.stop();
69     }
70
71     @Test(expected = ApexEventException.class)
72     public void testInitWithNonWebSocketCarrierTechnologyParameters() throws ApexEventException {
73         producerParameters.setCarrierTechnologyParameters(new CarrierTechnologyParameters() {});
74         apexRestServerProducer.init("TestApexRestServerProducer", producerParameters);
75     }
76
77     @Test(expected = ApexEventException.class)
78     public void testInitWithWebSocketCarrierTechnologyParameters() throws ApexEventException {
79         restServerCarrierTechnologyParameters = new RestServerCarrierTechnologyParameters();
80         producerParameters.setCarrierTechnologyParameters(restServerCarrierTechnologyParameters);
81         apexRestServerProducer.init("TestApexRestServerProducer", producerParameters);
82     }
83
84
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);
97     }
98
99     @Test
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());
106     }
107
108     @Test
109     public void testGetName() {
110         assertNull(apexRestServerProducer.getName());
111     }
112
113     @Test
114     public void testGetPeeredReference() {
115         assertNull(apexRestServerProducer.getPeeredReference(EventHandlerPeeredMode.REQUESTOR));
116     }
117
118     @Test
119     public void testSetPeeredReference() {
120         PeeredReference peeredReference = new PeeredReference(EventHandlerPeeredMode.REQUESTOR,
121                 apexRestServerConsumer, apexRestServerProducer);
122         apexRestServerProducer.setPeeredReference(EventHandlerPeeredMode.REQUESTOR,
123                 peeredReference);
124         assertNotNull(apexRestServerProducer.getPeeredReference(EventHandlerPeeredMode.REQUESTOR));
125     }
126
127     @Test
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));
136
137         this.apexRestServerProducer.setPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS, cache);
138
139         // test
140         this.apexRestServerProducer.sendEvent(executionId, null, eventName, event);
141
142         assertFalse(cache.existsEventFromApex(executionId));
143     }
144
145     @Test
146     public void testSendEvent() {
147         final long executionId = random.nextLong();
148         final String eventName = RandomStringUtils.randomAlphabetic(7);
149         final Object expected = new Object();
150
151         final ApexRestServerConsumer consumer = new ApexRestServerConsumer();
152         final SynchronousEventCache cache = new SynchronousEventCache(
153             EventHandlerPeeredMode.SYNCHRONOUS,
154             consumer,
155             apexRestServerProducer,
156             10000);
157
158         // Set EventToApex on cache object
159         cache.cacheSynchronizedEventToApex(executionId, new Object());
160
161         this.apexRestServerProducer.setPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS, cache);
162
163         this.apexRestServerProducer.sendEvent(executionId, null, eventName, expected);
164         final Object actual = cache.removeCachedEventFromApexIfExists(executionId);
165
166         assertSame(expected, actual);
167     }
168 }