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 BlockingQueue<?> messageQueue
77 = (BlockingQueue<?>) Whitebox.getInternalState(listener, "messageQueue");
78 listener.startProcessorThread();
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());
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());
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());
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());
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());
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());
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());
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());
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());
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());
144 Message badMessage0 = new DummyMessage(null, null);
145 messageList.add(badMessage0);
146 listener.onMessage(new MessageBlock<>(messageList, webSocketMock));
147 await().until(messageQueue::isEmpty);
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);
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);
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");
169 listener.stopProcessorThreads();