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