9f5329e55f9df4e951696c44fc1d84ac05c37610
[policy/apex-pdp.git] / services / services-engine / src / test / java / org / onap / policy / apex / service / engine / engdep / EngDepMessageListenerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Ericsson. 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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.service.engine.engdep;
22
23 import static org.awaitility.Awaitility.await;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.fail;
26
27 import java.net.InetSocketAddress;
28 import java.util.ArrayList;
29 import java.util.List;
30 import java.util.concurrent.BlockingQueue;
31 import org.java_websocket.WebSocket;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.mockito.Mock;
35 import org.mockito.Mockito;
36 import org.mockito.MockitoAnnotations;
37 import org.mockito.internal.util.reflection.Whitebox;
38 import org.onap.policy.apex.core.infrastructure.messaging.impl.ws.messageblock.MessageBlock;
39 import org.onap.policy.apex.core.protocols.Message;
40 import org.onap.policy.apex.core.protocols.engdep.messages.GetEngineInfo;
41 import org.onap.policy.apex.core.protocols.engdep.messages.GetEngineServiceInfo;
42 import org.onap.policy.apex.core.protocols.engdep.messages.GetEngineStatus;
43 import org.onap.policy.apex.core.protocols.engdep.messages.Response;
44 import org.onap.policy.apex.core.protocols.engdep.messages.StartEngine;
45 import org.onap.policy.apex.core.protocols.engdep.messages.StartPeriodicEvents;
46 import org.onap.policy.apex.core.protocols.engdep.messages.StopEngine;
47 import org.onap.policy.apex.core.protocols.engdep.messages.StopPeriodicEvents;
48 import org.onap.policy.apex.core.protocols.engdep.messages.UpdateModel;
49 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
50 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
51
52 /**
53  * Test the EngDep messaging Service.
54  */
55 public class EngDepMessageListenerTest {
56     @Mock
57     private WebSocket webSocketMock;
58
59     /**
60      * Set up mocking of the engine service facade.
61      *
62      * @throws ApexException on engine service facade setup errors
63      */
64     @Before
65     public void initializeMocking() throws ApexException {
66         MockitoAnnotations.initMocks(this);
67
68         Mockito.doReturn(new InetSocketAddress("HostAddress", 123)).when(webSocketMock).getRemoteSocketAddress();
69         Mockito.doReturn(true).when(webSocketMock).isOpen();
70     }
71
72     @Test
73     public void testMessageListener() throws ApexException {
74         DummyEngineService dummyEngineService = new DummyEngineService();
75         EngDepMessageListener listener = new EngDepMessageListener(dummyEngineService);
76         BlockingQueue<?> messageQueue
77             = (BlockingQueue<?>) Whitebox.getInternalState(listener, "messageQueue");
78         listener.startProcessorThread();
79
80         try {
81             listener.onMessage("bad string message");
82             fail("test should throw an exception");
83         } catch (Exception uoe) {
84             assertEquals("String messages are not supported on the EngDep protocol", uoe.getMessage());
85         }
86
87         List<Message> messageList = new ArrayList<>();
88         messageList.add(new StartEngine(new AxArtifactKey("Start:0.0.1")));
89         listener.onMessage(new MessageBlock<>(messageList, webSocketMock));
90         await().until(messageQueue::isEmpty);
91         assertEquals("Start:0.0.1", dummyEngineService.getStartEngineKey().getId());
92
93         messageList.clear();
94         messageList.add(new StopEngine(new AxArtifactKey("Stop:0.0.1")));
95         listener.onMessage(new MessageBlock<>(messageList, webSocketMock));
96         await().until(messageQueue::isEmpty);
97         assertEquals("Stop:0.0.1", dummyEngineService.getStopEngineKey().getId());
98
99         messageList.clear();
100         messageList.add(new StartPeriodicEvents(new AxArtifactKey("StartPeriodic:0.0.1"), "12345"));
101         listener.onMessage(new MessageBlock<>(messageList, webSocketMock));
102         await().until(messageQueue::isEmpty);
103         assertEquals(12345, dummyEngineService.getPeriodicPeriod());
104
105         messageList.clear();
106         messageList.add(new StopPeriodicEvents(new AxArtifactKey("StopPeriodic:0.0.1")));
107         listener.onMessage(new MessageBlock<>(messageList, webSocketMock));
108         await().until(messageQueue::isEmpty);
109         assertEquals(0, dummyEngineService.getPeriodicPeriod());
110
111         messageList.clear();
112         messageList.add(new GetEngineInfo(new AxArtifactKey("EngineInfo:0.0.1")));
113         listener.onMessage(new MessageBlock<>(messageList, webSocketMock));
114         await().until(messageQueue::isEmpty);
115         assertEquals("EngineInfo:0.0.1", dummyEngineService.getRuntimeInfoKey().getId());
116
117         messageList.clear();
118         messageList.add(new GetEngineStatus(new AxArtifactKey("EngineStatus:0.0.1")));
119         listener.onMessage(new MessageBlock<>(messageList, webSocketMock));
120         await().until(messageQueue::isEmpty);
121         assertEquals("EngineStatus:0.0.1", dummyEngineService.getStatusKey().getId());
122
123         messageList.clear();
124         messageList.add(new GetEngineServiceInfo(new AxArtifactKey("EngineServiceInfo:0.0.1")));
125         listener.onMessage(new MessageBlock<>(messageList, webSocketMock));
126         await().until(messageQueue::isEmpty);
127         assertEquals(1, dummyEngineService.getModelKeyGetCalled());
128
129         messageList.clear();
130         messageList.add(new UpdateModel(new AxArtifactKey("UpdateModel:0.0.1")));
131         listener.onMessage(new MessageBlock<>(messageList, webSocketMock));
132         await().until(messageQueue::isEmpty);
133         assertEquals("UpdateModel:0.0.1", dummyEngineService.getUpdateModelKey().getId());
134
135         try {
136             messageList.clear();
137             messageList.add(new Response(new AxArtifactKey("UpdateModel:0.0.1"), false,
138                             new GetEngineInfo(new AxArtifactKey("EngineInfo:0.0.1"))));
139             listener.onMessage(new MessageBlock<>(messageList, webSocketMock));
140             await().until(messageQueue::isEmpty);
141             assertEquals("UpdateModel:0.0.1", dummyEngineService.getUpdateModelKey().getId());
142
143             messageList.clear();
144             Message badMessage0 = new DummyMessage(null, null);
145             messageList.add(badMessage0);
146             listener.onMessage(new MessageBlock<>(messageList, webSocketMock));
147             await().until(messageQueue::isEmpty);
148
149             messageList.clear();
150             Message badMessage1 = new DummyMessage(new DummyAction(null), null);
151             messageList.add(badMessage1);
152             listener.onMessage(new MessageBlock<>(messageList, webSocketMock));
153             await().until(messageQueue::isEmpty);
154
155             messageList.clear();
156             Message badMessage2 = new DummyMessage(new DummyAction("throw exception"), null);
157             messageList.add(badMessage2);
158             listener.onMessage(new MessageBlock<>(messageList, webSocketMock));
159             await().until(messageQueue::isEmpty);
160
161             messageList.clear();
162             Mockito.doReturn(false).when(webSocketMock).isOpen();
163             messageList.add(new StartEngine(new AxArtifactKey("Start:0.0.1")));
164             listener.onMessage(new MessageBlock<>(messageList, webSocketMock));
165             await().until(messageQueue::isEmpty);
166         } catch (Exception e) {
167             fail("test should not throw exceptions on bad messages");
168         }
169         listener.stopProcessorThreads();
170     }
171 }