2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Samsung. All rights reserved.
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
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
20 package org.onap.policy.apex.plugins.event.carrier.websocket;
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertNotNull;
24 import static org.junit.Assert.assertNull;
25 import org.junit.After;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.mockito.Mockito;
29 import org.mockito.MockitoAnnotations;
30 import org.mockito.stubbing.Answer;
31 import org.onap.policy.apex.service.engine.event.ApexEventException;
32 import org.onap.policy.apex.service.engine.event.ApexEventProducer;
33 import org.onap.policy.apex.service.engine.event.ApexEventReceiver;
34 import org.onap.policy.apex.service.engine.event.PeeredReference;
35 import org.onap.policy.apex.service.parameters.carriertechnology.CarrierTechnologyParameters;
36 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters;
37 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerPeeredMode;
39 public class ApexWebSocketConsumerTest {
41 ApexWebSocketConsumer apexWebSocketConsumer = null;
42 EventHandlerParameters consumerParameters = null;
43 ApexEventReceiver incomingEventReceiver = null;
44 ApexEventProducer apexWebSocketProducer = null;
45 WebSocketCarrierTechnologyParameters webSocketCarrierTechnologyParameters = null;
50 * @throws Exception on test set up errors.
53 public void setUp() throws Exception {
54 apexWebSocketConsumer = new ApexWebSocketConsumer();
55 consumerParameters = new EventHandlerParameters();
56 apexWebSocketProducer = new ApexWebSocketProducer();
57 apexWebSocketConsumer.start();
61 public void tearDown() {
62 apexWebSocketConsumer.stop();
65 @Test(expected = ApexEventException.class)
66 public void testInitWithNonWebSocketCarrierTechnologyParameters() throws ApexEventException {
67 consumerParameters.setCarrierTechnologyParameters(new CarrierTechnologyParameters() {});
68 apexWebSocketConsumer.init("TestApexWebSocketConsumer", consumerParameters,
69 incomingEventReceiver);
73 public void testInitWithWebSocketCarrierTechnologyParameters() throws ApexEventException {
74 webSocketCarrierTechnologyParameters = new WebSocketCarrierTechnologyParameters();
75 consumerParameters.setCarrierTechnologyParameters(webSocketCarrierTechnologyParameters);
76 apexWebSocketConsumer.init("TestApexWebSocketConsumer", consumerParameters,
77 incomingEventReceiver);
78 assertEquals("TestApexWebSocketConsumer", apexWebSocketConsumer.getName());
82 public void testGetName() {
83 assertNull(apexWebSocketConsumer.getName());
87 public void testGetPeeredReference() {
88 assertNull(apexWebSocketConsumer.getPeeredReference(EventHandlerPeeredMode.REQUESTOR));
92 public void testSetPeeredReference() {
93 PeeredReference peeredReference = new PeeredReference(EventHandlerPeeredMode.REQUESTOR,
94 apexWebSocketConsumer, apexWebSocketProducer);
95 apexWebSocketConsumer.setPeeredReference(EventHandlerPeeredMode.REQUESTOR, peeredReference);
96 assertNotNull(apexWebSocketConsumer.getPeeredReference(EventHandlerPeeredMode.REQUESTOR));
100 public void testReceiveString() {
101 MockitoAnnotations.initMocks(this);
102 ApexWebSocketConsumer apexWebSocketConsumerMock = Mockito.mock(ApexWebSocketConsumer.class);
104 Mockito.doAnswer((Answer<?>) invocation -> {
105 Object[] args = invocation.getArguments();
106 assertEquals("TestRecieveString", args[0]);
108 }).when(apexWebSocketConsumerMock).receiveString("TestRecieveString");
110 apexWebSocketConsumerMock.receiveString("TestRecieveString");