2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2020 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.service.engine.engdep;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.awaitility.Awaitility.await;
26 import static org.junit.Assert.assertEquals;
28 import java.net.InetSocketAddress;
29 import java.util.ArrayList;
30 import java.util.List;
31 import java.util.concurrent.BlockingQueue;
32 import org.java_websocket.WebSocket;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.mockito.Mock;
36 import org.mockito.Mockito;
37 import org.mockito.MockitoAnnotations;
38 import org.mockito.internal.util.reflection.Whitebox;
39 import org.onap.policy.apex.core.infrastructure.messaging.impl.ws.messageblock.MessageBlock;
40 import org.onap.policy.apex.core.protocols.Message;
41 import org.onap.policy.apex.core.protocols.engdep.messages.GetEngineInfo;
42 import org.onap.policy.apex.core.protocols.engdep.messages.GetEngineServiceInfo;
43 import org.onap.policy.apex.core.protocols.engdep.messages.GetEngineStatus;
44 import org.onap.policy.apex.core.protocols.engdep.messages.Response;
45 import org.onap.policy.apex.core.protocols.engdep.messages.StartEngine;
46 import org.onap.policy.apex.core.protocols.engdep.messages.StartPeriodicEvents;
47 import org.onap.policy.apex.core.protocols.engdep.messages.StopEngine;
48 import org.onap.policy.apex.core.protocols.engdep.messages.StopPeriodicEvents;
49 import org.onap.policy.apex.core.protocols.engdep.messages.UpdateModel;
50 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
51 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
54 * Test the EngDep messaging Service.
56 public class EngDepMessageListenerTest {
58 private WebSocket webSocketMock;
61 * Set up mocking of the engine service facade.
63 * @throws ApexException on engine service facade setup errors
66 public void initializeMocking() throws ApexException {
67 MockitoAnnotations.initMocks(this);
69 Mockito.doReturn(new InetSocketAddress("HostAddress", 123)).when(webSocketMock).getRemoteSocketAddress();
70 Mockito.doReturn(true).when(webSocketMock).isOpen();
74 public void testMessageListener() throws ApexException {
75 DummyEngineService dummyEngineService = new DummyEngineService();
76 EngDepMessageListener listener = new EngDepMessageListener(dummyEngineService);
77 listener.startProcessorThread();
79 assertThatThrownBy(() -> listener.onMessage("bad string message"))
80 .hasMessage("String messages are not supported on the EngDep protocol");
81 List<Message> messageList = new ArrayList<>();
82 messageList.add(new StartEngine(new AxArtifactKey("Start:0.0.1")));
83 listener.onMessage(new MessageBlock<>(messageList, webSocketMock));
84 BlockingQueue<?> messageQueue = (BlockingQueue<?>) Whitebox.getInternalState(listener, "messageQueue");
85 await().until(messageQueue::isEmpty);
86 assertEquals("Start:0.0.1", dummyEngineService.getStartEngineKey().getId());
89 messageList.add(new StopEngine(new AxArtifactKey("Stop:0.0.1")));
90 listener.onMessage(new MessageBlock<>(messageList, webSocketMock));
91 await().until(messageQueue::isEmpty);
92 assertEquals("Stop:0.0.1", dummyEngineService.getStopEngineKey().getId());
95 messageList.add(new StartPeriodicEvents(new AxArtifactKey("StartPeriodic:0.0.1"), "12345"));
96 listener.onMessage(new MessageBlock<>(messageList, webSocketMock));
97 await().until(messageQueue::isEmpty);
98 assertEquals(12345, dummyEngineService.getPeriodicPeriod());
101 messageList.add(new StopPeriodicEvents(new AxArtifactKey("StopPeriodic:0.0.1")));
102 listener.onMessage(new MessageBlock<>(messageList, webSocketMock));
103 await().until(messageQueue::isEmpty);
104 assertEquals(0, dummyEngineService.getPeriodicPeriod());
107 messageList.add(new GetEngineInfo(new AxArtifactKey("EngineInfo:0.0.1")));
108 listener.onMessage(new MessageBlock<>(messageList, webSocketMock));
109 await().until(messageQueue::isEmpty);
110 assertEquals("EngineInfo:0.0.1", dummyEngineService.getRuntimeInfoKey().getId());
113 messageList.add(new GetEngineStatus(new AxArtifactKey("EngineStatus:0.0.1")));
114 listener.onMessage(new MessageBlock<>(messageList, webSocketMock));
115 await().until(messageQueue::isEmpty);
116 assertEquals("EngineStatus:0.0.1", dummyEngineService.getStatusKey().getId());
119 messageList.add(new GetEngineServiceInfo(new AxArtifactKey("EngineServiceInfo:0.0.1")));
120 listener.onMessage(new MessageBlock<>(messageList, webSocketMock));
121 await().until(messageQueue::isEmpty);
122 assertEquals(1, dummyEngineService.getModelKeyGetCalled());
125 messageList.add(new UpdateModel(new AxArtifactKey("UpdateModel:0.0.1")));
126 listener.onMessage(new MessageBlock<>(messageList, webSocketMock));
127 await().until(messageQueue::isEmpty);
128 assertEquals("UpdateModel:0.0.1", dummyEngineService.getUpdateModelKey().getId());
131 messageList.add(new Response(new AxArtifactKey("UpdateModel:0.0.1"), false,
132 new GetEngineInfo(new AxArtifactKey("EngineInfo:0.0.1"))));
133 listener.onMessage(new MessageBlock<>(messageList, webSocketMock));
134 await().until(messageQueue::isEmpty);
135 assertEquals("UpdateModel:0.0.1", dummyEngineService.getUpdateModelKey().getId());
137 Message badMessage0 = new DummyMessage(null, null);
138 messageList.add(badMessage0);
139 listener.onMessage(new MessageBlock<>(messageList, webSocketMock));
140 await().until(messageQueue::isEmpty);
143 Message badMessage1 = new DummyMessage(new DummyAction(null), null);
144 messageList.add(badMessage1);
145 listener.onMessage(new MessageBlock<>(messageList, webSocketMock));
146 await().until(messageQueue::isEmpty);
149 Message badMessage2 = new DummyMessage(new DummyAction("throw exception"), null);
150 messageList.add(badMessage2);
151 listener.onMessage(new MessageBlock<>(messageList, webSocketMock));
152 await().until(messageQueue::isEmpty);
155 Mockito.doReturn(false).when(webSocketMock).isOpen();
156 messageList.add(new StartEngine(new AxArtifactKey("Start:0.0.1")));
157 listener.onMessage(new MessageBlock<>(messageList, webSocketMock));
158 await().until(messageQueue::isEmpty);
160 listener.stopProcessorThreads();