2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Samsung. All rights reserved.
4 * Modifications Copyright (C) 2019 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.websocket;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertNull;
28 import org.junit.After;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.mockito.Mockito;
32 import org.mockito.MockitoAnnotations;
33 import org.mockito.stubbing.Answer;
34 import org.onap.policy.apex.service.engine.event.ApexEventException;
35 import org.onap.policy.apex.service.engine.event.ApexEventProducer;
36 import org.onap.policy.apex.service.engine.event.ApexEventReceiver;
37 import org.onap.policy.apex.service.engine.event.PeeredReference;
38 import org.onap.policy.apex.service.parameters.carriertechnology.CarrierTechnologyParameters;
39 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters;
40 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerPeeredMode;
42 public class ApexWebSocketConsumerTest {
44 ApexWebSocketConsumer apexWebSocketConsumer = null;
45 EventHandlerParameters consumerParameters = null;
46 ApexEventReceiver incomingEventReceiver = null;
47 ApexEventProducer apexWebSocketProducer = null;
48 WebSocketCarrierTechnologyParameters webSocketCarrierTechnologyParameters = null;
53 * @throws Exception on test set up errors.
56 public void setUp() throws Exception {
57 apexWebSocketConsumer = new ApexWebSocketConsumer();
58 consumerParameters = new EventHandlerParameters();
59 apexWebSocketProducer = new ApexWebSocketProducer();
60 apexWebSocketConsumer.start();
64 public void tearDown() {
65 apexWebSocketConsumer.stop();
68 @Test(expected = ApexEventException.class)
69 public void testInitWithNonWebSocketCarrierTechnologyParameters() throws ApexEventException {
70 consumerParameters.setCarrierTechnologyParameters(new CarrierTechnologyParameters() {});
71 apexWebSocketConsumer.init("TestApexWebSocketConsumer", consumerParameters,
72 incomingEventReceiver);
76 public void testInitWithWebSocketCarrierTechnologyParameters() throws ApexEventException {
77 webSocketCarrierTechnologyParameters = new WebSocketCarrierTechnologyParameters();
78 consumerParameters.setCarrierTechnologyParameters(webSocketCarrierTechnologyParameters);
79 apexWebSocketConsumer.init("TestApexWebSocketConsumer", consumerParameters,
80 incomingEventReceiver);
81 assertEquals("TestApexWebSocketConsumer", apexWebSocketConsumer.getName());
85 public void testGetName() {
86 assertNull(apexWebSocketConsumer.getName());
90 public void testGetPeeredReference() {
91 assertNull(apexWebSocketConsumer.getPeeredReference(EventHandlerPeeredMode.REQUESTOR));
95 public void testSetPeeredReference() {
96 PeeredReference peeredReference = new PeeredReference(EventHandlerPeeredMode.REQUESTOR,
97 apexWebSocketConsumer, apexWebSocketProducer);
98 apexWebSocketConsumer.setPeeredReference(EventHandlerPeeredMode.REQUESTOR, peeredReference);
99 assertNotNull(apexWebSocketConsumer.getPeeredReference(EventHandlerPeeredMode.REQUESTOR));
103 public void testReceiveString() {
104 MockitoAnnotations.initMocks(this);
105 ApexWebSocketConsumer apexWebSocketConsumerMock = Mockito.mock(ApexWebSocketConsumer.class);
107 Mockito.doAnswer((Answer<?>) invocation -> {
108 Object[] args = invocation.getArguments();
109 assertEquals("TestRecieveString", args[0]);
111 }).when(apexWebSocketConsumerMock).receiveString("TestRecieveString");
113 apexWebSocketConsumerMock.receiveString("TestRecieveString");