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