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
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=========================================================
21 package org.onap.policy.apex.service.engine.engdep;
23 import static org.awaitility.Awaitility.await;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.fail;
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;
53 * Test the EngDep messaging Service.
55 public class EngDepMessageListenerTest {
57 private WebSocket webSocketMock;
60 * Set up mocking of the engine service facade.
62 * @throws ApexException on engine service facade setup errors
65 public void initializeMocking() throws ApexException {
66 MockitoAnnotations.initMocks(this);
68 Mockito.doReturn(new InetSocketAddress("HostAddress", 123)).when(webSocketMock).getRemoteSocketAddress();
69 Mockito.doReturn(true).when(webSocketMock).isOpen();
73 public void testMessageListener() throws ApexException {
74 DummyEngineService dummyEngineService = new DummyEngineService();
75 EngDepMessageListener listener = new EngDepMessageListener(dummyEngineService);
76 listener.startProcessorThread();
79 listener.onMessage("bad string message");
80 fail("test should throw an exception");
81 } catch (Exception uoe) {
82 assertEquals("String messages are not supported on the EngDep protocol", uoe.getMessage());
85 List<Message> messageList = new ArrayList<>();
86 messageList.add(new StartEngine(new AxArtifactKey("Start:0.0.1")));
87 listener.onMessage(new MessageBlock<>(messageList, webSocketMock));
88 BlockingQueue<?> messageQueue = (BlockingQueue<?>) Whitebox.getInternalState(listener, "messageQueue");
89 await().until(messageQueue::isEmpty);
90 assertEquals("Start:0.0.1", dummyEngineService.getStartEngineKey().getId());
93 messageList.add(new StopEngine(new AxArtifactKey("Stop:0.0.1")));
94 listener.onMessage(new MessageBlock<>(messageList, webSocketMock));
95 await().until(messageQueue::isEmpty);
96 assertEquals("Stop:0.0.1", dummyEngineService.getStopEngineKey().getId());
99 messageList.add(new StartPeriodicEvents(new AxArtifactKey("StartPeriodic:0.0.1"), "12345"));
100 listener.onMessage(new MessageBlock<>(messageList, webSocketMock));
101 await().until(messageQueue::isEmpty);
102 assertEquals(12345, dummyEngineService.getPeriodicPeriod());
105 messageList.add(new StopPeriodicEvents(new AxArtifactKey("StopPeriodic:0.0.1")));
106 listener.onMessage(new MessageBlock<>(messageList, webSocketMock));
107 await().until(messageQueue::isEmpty);
108 assertEquals(0, dummyEngineService.getPeriodicPeriod());
111 messageList.add(new GetEngineInfo(new AxArtifactKey("EngineInfo:0.0.1")));
112 listener.onMessage(new MessageBlock<>(messageList, webSocketMock));
113 await().until(messageQueue::isEmpty);
114 assertEquals("EngineInfo:0.0.1", dummyEngineService.getRuntimeInfoKey().getId());
117 messageList.add(new GetEngineStatus(new AxArtifactKey("EngineStatus:0.0.1")));
118 listener.onMessage(new MessageBlock<>(messageList, webSocketMock));
119 await().until(messageQueue::isEmpty);
120 assertEquals("EngineStatus:0.0.1", dummyEngineService.getStatusKey().getId());
123 messageList.add(new GetEngineServiceInfo(new AxArtifactKey("EngineServiceInfo:0.0.1")));
124 listener.onMessage(new MessageBlock<>(messageList, webSocketMock));
125 await().until(messageQueue::isEmpty);
126 assertEquals(1, dummyEngineService.getModelKeyGetCalled());
129 messageList.add(new UpdateModel(new AxArtifactKey("UpdateModel:0.0.1")));
130 listener.onMessage(new MessageBlock<>(messageList, webSocketMock));
131 await().until(messageQueue::isEmpty);
132 assertEquals("UpdateModel:0.0.1", dummyEngineService.getUpdateModelKey().getId());
136 messageList.add(new Response(new AxArtifactKey("UpdateModel:0.0.1"), false,
137 new GetEngineInfo(new AxArtifactKey("EngineInfo:0.0.1"))));
138 listener.onMessage(new MessageBlock<>(messageList, webSocketMock));
139 await().until(messageQueue::isEmpty);
140 assertEquals("UpdateModel:0.0.1", dummyEngineService.getUpdateModelKey().getId());
143 Message badMessage0 = new DummyMessage(null, null);
144 messageList.add(badMessage0);
145 listener.onMessage(new MessageBlock<>(messageList, webSocketMock));
146 await().until(messageQueue::isEmpty);
149 Message badMessage1 = new DummyMessage(new DummyAction(null), null);
150 messageList.add(badMessage1);
151 listener.onMessage(new MessageBlock<>(messageList, webSocketMock));
152 await().until(messageQueue::isEmpty);
155 Message badMessage2 = new DummyMessage(new DummyAction("throw exception"), null);
156 messageList.add(badMessage2);
157 listener.onMessage(new MessageBlock<>(messageList, webSocketMock));
158 await().until(messageQueue::isEmpty);
161 Mockito.doReturn(false).when(webSocketMock).isOpen();
162 messageList.add(new StartEngine(new AxArtifactKey("Start:0.0.1")));
163 listener.onMessage(new MessageBlock<>(messageList, webSocketMock));
164 await().until(messageQueue::isEmpty);
165 } catch (Exception e) {
166 fail("test should not throw exceptions on bad messages");
168 listener.stopProcessorThreads();