469f095861abf55d9e6f327330fd87c8b2f139c0
[policy/apex-pdp.git] /
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.client.monitoring.rest;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotEquals;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertTrue;
28
29 import javax.ws.rs.core.Response;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.mockito.Mock;
33 import org.mockito.Mockito;
34 import org.mockito.MockitoAnnotations;
35 import org.onap.policy.apex.core.deployment.ApexDeploymentException;
36 import org.onap.policy.apex.core.deployment.EngineServiceFacade;
37 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
38 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
39 import org.onap.policy.apex.model.enginemodel.concepts.AxEngineModel;
40
41 /**
42  * Test the monitoring rest resource.
43  */
44 public class RestResourceTest {
45     @Mock
46     private EngineServiceFacade engineServiceFacadeMock;
47     private ApexMonitoringRestResource restResource;
48
49     /**
50      * Set up mocking of the engine service facade.
51      *
52      * @throws ApexException on engine service facade setup errors
53      */
54     @Before
55     public void initializeMocking() throws ApexException {
56         MockitoAnnotations.initMocks(this);
57
58         final AxArtifactKey engineServiceKey = new AxArtifactKey("EngineServiceKey", "0.0.1");
59         final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1");
60         final AxArtifactKey[] engineServiceKeyArray =
61             { engineKey };
62         final AxEngineModel engineModel = new AxEngineModel(engineServiceKeyArray[0]);
63
64         restResource = Mockito.spy(new ApexMonitoringRestResource());
65         Mockito.doReturn(engineServiceFacadeMock).when(restResource).getEngineServiceFacade("apexServer", 12345);
66
67         Mockito.doReturn(engineServiceKey).when(engineServiceFacadeMock).getKey();
68         Mockito.doReturn(engineServiceKeyArray).when(engineServiceFacadeMock).getEngineKeyArray();
69         Mockito.doReturn(engineModel).when(engineServiceFacadeMock).getEngineStatus(engineKey);
70     }
71
72     @Test
73     public void testRestResourceCreateSession() throws ApexException {
74         Response response = restResource.createSession("apexServer", 12345);
75         assertEquals(200, response.getStatus());
76         assertTrue(((String) response.getEntity()).contains("Engine0:0.0.1"));
77     }
78
79     @Test
80     public void testRestResourceCreateSessionWithApexModelKey() throws ApexException {
81         Mockito.doReturn(new AxArtifactKey("ModelKey:0.0.1")).when(engineServiceFacadeMock).getApexModelKey();
82
83         Response response = restResource.createSession("apexServer", 12345);
84         assertEquals(200, response.getStatus());
85         assertTrue(((String) response.getEntity()).contains("Engine0:0.0.1"));
86     }
87
88     @Test
89     public void testRestResourceCreateSessionConnectException() throws ApexException {
90         Mockito.doThrow(new ApexDeploymentException("Connection Failed")).when(engineServiceFacadeMock).init();
91
92         Response response = restResource.createSession("apexServer", 12345);
93         assertEquals(500, response.getStatus());
94         assertTrue(((String) response.getEntity()).contains("Error connecting to Apex Engine Service"));
95     }
96
97     @Test
98     public void testRestResourceCreateSessionGetException() throws ApexException {
99         final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1");
100         Mockito.doThrow(new ApexException("Exception on get")).when(engineServiceFacadeMock).getEngineStatus(engineKey);
101
102         Response response = restResource.createSession("apexServer", 12345);
103         assertEquals(200, response.getStatus());
104     }
105
106     @Test
107     public void testRestResourceCreateSessionInfo() throws ApexException {
108         final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1");
109         Mockito.doReturn("{}").when(engineServiceFacadeMock).getEngineInfo(engineKey);
110
111         Response response = restResource.createSession("apexServer", 12345);
112         assertEquals(200, response.getStatus());
113     }
114
115     @Test
116     public void testRestResourceCreateSessionNullInfo() throws ApexException {
117         final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1");
118         Mockito.doReturn(null).when(engineServiceFacadeMock).getEngineInfo(engineKey);
119
120         Response response = restResource.createSession("apexServer", 12345);
121         assertEquals(200, response.getStatus());
122     }
123
124     @Test
125     public void testRestResourceCreateSessionEmptyInfo() throws ApexException {
126         final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1");
127         Mockito.doReturn(" ").when(engineServiceFacadeMock).getEngineInfo(engineKey);
128
129         Response response = restResource.createSession("apexServer", 12345);
130         assertEquals(200, response.getStatus());
131     }
132
133     @Test
134     public void testRestResourceCreateSessionExceptionInfo() throws ApexException {
135         final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1");
136         Mockito.doThrow(new ApexException("Exception on info")).when(engineServiceFacadeMock).getEngineInfo(engineKey);
137
138         Response response = restResource.createSession("apexServer", 12345);
139         assertEquals(200, response.getStatus());
140     }
141
142     @Test
143     public void testRestResourceStartEngine() throws ApexException {
144         final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1");
145
146         Response response = restResource.startStop("apexServer", 12345, engineKey.getId(), "Start");
147         assertEquals(200, response.getStatus());
148     }
149
150     @Test
151     public void testRestResourceStopEngine() throws ApexException {
152         final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1");
153
154         Response response = restResource.startStop("apexServer", 12345, engineKey.getId(), "Stop");
155         assertEquals(200, response.getStatus());
156     }
157
158     @Test
159     public void testRestResourceNotStartStopEngine() throws ApexException {
160         final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1");
161
162         Response response = restResource.startStop("apexServer", 12345, engineKey.getId(), "Hello");
163         assertEquals(200, response.getStatus());
164     }
165
166     @Test
167     public void testRestResourceInitExceptionStartStopEngine() throws ApexException {
168         Mockito.doThrow(new ApexDeploymentException("Exception on init")).when(engineServiceFacadeMock).init();
169
170         final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1");
171
172         Response response = restResource.startStop("apexServer", 12345, engineKey.getId(), "Hello");
173         assertEquals(500, response.getStatus());
174     }
175
176     @Test
177     public void testRestResourceExceptionStartStopEngine() throws ApexException {
178         final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1");
179         Mockito.doThrow(new ApexDeploymentException("Exception on Start/Stop")).when(engineServiceFacadeMock)
180                         .startEngine(engineKey);
181
182         Response response = restResource.startStop("apexServer", 12345, engineKey.getId(), "Start");
183         assertEquals(500, response.getStatus());
184     }
185
186     @Test
187     public void testRestResourceStartPeriodicEvents() throws ApexException {
188         final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1");
189
190         Response response = restResource.periodiceventStartStop("apexServer", 12345, engineKey.getId(), "Start", 1000);
191         assertEquals(200, response.getStatus());
192     }
193
194     @Test
195     public void testRestResourceStopPeriodicEvents() throws ApexException {
196         final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1");
197
198         Response response = restResource.periodiceventStartStop("apexServer", 12345, engineKey.getId(), "Stop", 1000);
199         assertEquals(200, response.getStatus());
200     }
201
202     @Test
203     public void testRestResourceNotStartStopPeriodicEvents() throws ApexException {
204         final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1");
205
206         Response response = restResource.periodiceventStartStop("apexServer", 12345, engineKey.getId(), "Hello", 1000);
207         assertEquals(200, response.getStatus());
208     }
209
210     @Test
211     public void testRestResourceExceptionPeriodicEvents() throws ApexException {
212         final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1");
213         Mockito.doThrow(new ApexDeploymentException("Exception on Periodic Events")).when(engineServiceFacadeMock)
214                         .stopPerioidicEvents(engineKey);
215
216         Response response = restResource.periodiceventStartStop("apexServer", 12345, engineKey.getId(), "Stop", 1000);
217         assertEquals(500, response.getStatus());
218     }
219
220     @Test
221     public void testCounter() {
222         ApexMonitoringRestResource.Counter counter = restResource.new Counter(1538338576, 1538338592);
223
224         assertEquals(1538338576, counter.getTimestamp());
225         assertEquals(1538338592, counter.getValue());
226     }
227
228     @Test
229     public void testSlidingWindow() {
230         ApexMonitoringRestResource.SlidingWindowList<String> slidingWindowList0 = restResource.new SlidingWindowList<>(
231                         2);
232
233         assertNotEquals(0, slidingWindowList0.hashCode());
234
235         assertTrue(slidingWindowList0.add("Hello"));
236         assertTrue(slidingWindowList0.add("Hi"));
237         assertTrue(slidingWindowList0.add("Howdy"));
238
239         assertNotNull(slidingWindowList0);
240         assertEquals(slidingWindowList0, slidingWindowList0);
241
242         ApexMonitoringRestResource.SlidingWindowList<String> slidingWindowList1 = restResource.new SlidingWindowList<>(
243                         2);
244         ApexMonitoringRestResource.SlidingWindowList<String> slidingWindowList2 = restResource.new SlidingWindowList<>(
245                         2);
246         assertNotEquals(slidingWindowList0, slidingWindowList1);
247         assertNotEquals(slidingWindowList0, slidingWindowList2);
248         assertEquals(slidingWindowList1, slidingWindowList2);
249         ApexMonitoringRestResource.SlidingWindowList<String> slidingWindowList3 = restResource.new SlidingWindowList<>(
250                         3);
251         assertNotEquals(slidingWindowList1, slidingWindowList3);
252         ApexMonitoringRestResource.SlidingWindowList<Integer> slidingWindowList4 = restResource.new SlidingWindowList<>(
253                         3);
254         assertTrue(slidingWindowList3.add("Hello"));
255         assertTrue(slidingWindowList4.add(10));
256         assertNotEquals(slidingWindowList3, slidingWindowList4);
257     }
258
259     @Test
260     public void mopUp() {
261         assertEquals(engineServiceFacadeMock, restResource.getEngineServiceFacade("apexServer", 12345));
262     }
263 }