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