Replace try/catch with assertj
[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  *  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
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.service.engine.engdep;
23
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.awaitility.Awaitility.await;
26 import static org.junit.Assert.assertEquals;
27
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;
52
53 /**
54  * Test the EngDep messaging Service.
55  */
56 public class EngDepMessageListenerTest {
57     @Mock
58     private WebSocket webSocketMock;
59
60     /**
61      * Set up mocking of the engine service facade.
62      *
63      * @throws ApexException on engine service facade setup errors
64      */
65     @Before
66     public void initializeMocking() throws ApexException {
67         MockitoAnnotations.initMocks(this);
68
69         Mockito.doReturn(new InetSocketAddress("HostAddress", 123)).when(webSocketMock).getRemoteSocketAddress();
70         Mockito.doReturn(true).when(webSocketMock).isOpen();
71     }
72
73     @Test
74     public void testMessageListener() throws ApexException {
75         DummyEngineService dummyEngineService = new DummyEngineService();
76         EngDepMessageListener listener = new EngDepMessageListener(dummyEngineService);
77         listener.startProcessorThread();
78
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());
87
88         messageList.clear();
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());
93
94         messageList.clear();
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());
99
100         messageList.clear();
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());
105
106         messageList.clear();
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());
111
112         messageList.clear();
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());
117
118         messageList.clear();
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());
123
124         messageList.clear();
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());
129
130         messageList.clear();
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());
136         messageList.clear();
137         Message badMessage0 = new DummyMessage(null, null);
138         messageList.add(badMessage0);
139         listener.onMessage(new MessageBlock<>(messageList, webSocketMock));
140         await().until(messageQueue::isEmpty);
141
142         messageList.clear();
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);
147
148         messageList.clear();
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);
153
154         messageList.clear();
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);
159
160         listener.stopProcessorThreads();
161     }
162 }