3021b8af2942b28268cbab0f4a0714fb117d0690
[policy/apex-pdp.git] /
1 /*-
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
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.websocket;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertNull;
27
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;
41
42 public class ApexWebSocketConsumerTest {
43
44     ApexWebSocketConsumer apexWebSocketConsumer = null;
45     EventHandlerParameters consumerParameters = null;
46     ApexEventReceiver incomingEventReceiver = null;
47     ApexEventProducer apexWebSocketProducer = null;
48     WebSocketCarrierTechnologyParameters webSocketCarrierTechnologyParameters = null;
49
50     /**
51      * Set up testing.
52      *
53      * @throws Exception on test set up errors.
54      */
55     @Before
56     public void setUp() throws Exception {
57         apexWebSocketConsumer = new ApexWebSocketConsumer();
58         consumerParameters = new EventHandlerParameters();
59         apexWebSocketProducer = new ApexWebSocketProducer();
60         apexWebSocketConsumer.start();
61     }
62
63     @After
64     public void tearDown() {
65         apexWebSocketConsumer.stop();
66     }
67
68     @Test(expected = ApexEventException.class)
69     public void testInitWithNonWebSocketCarrierTechnologyParameters() throws ApexEventException {
70         consumerParameters.setCarrierTechnologyParameters(new CarrierTechnologyParameters() {});
71         apexWebSocketConsumer.init("TestApexWebSocketConsumer", consumerParameters,
72                 incomingEventReceiver);
73     }
74
75     @Test
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());
82     }
83
84     @Test
85     public void testGetName() {
86         assertNull(apexWebSocketConsumer.getName());
87     }
88
89     @Test
90     public void testGetPeeredReference() {
91         assertNull(apexWebSocketConsumer.getPeeredReference(EventHandlerPeeredMode.REQUESTOR));
92     }
93
94     @Test
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));
100     }
101
102     @Test
103     public void testReceiveString() {
104         MockitoAnnotations.initMocks(this);
105         ApexWebSocketConsumer apexWebSocketConsumerMock = Mockito.mock(ApexWebSocketConsumer.class);
106
107         Mockito.doAnswer((Answer<?>) invocation -> {
108             Object[] args = invocation.getArguments();
109             assertEquals("TestRecieveString", args[0]);
110             return null;
111         }).when(apexWebSocketConsumerMock).receiveString("TestRecieveString");
112
113         apexWebSocketConsumerMock.receiveString("TestRecieveString");
114
115     }
116 }