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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.client.monitoring.rest;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertTrue;
27 import javax.ws.rs.core.Response;
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;
41 * Test the monitoring rest resource.
43 public class RestResourceTest {
45 private EngineServiceFacade engineServiceFacadeMock;
46 private ApexMonitoringRestResource restResource;
49 * Set up mocking of the engine service facade.
51 * @throws ApexException on engine service facade setup errors
54 public void initializeMocking() throws ApexException {
55 MockitoAnnotations.initMocks(this);
57 final AxArtifactKey engineServiceKey = new AxArtifactKey("EngineServiceKey", "0.0.1");
58 final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1");
59 final AxArtifactKey[] engineServiceKeyArray =
61 final AxEngineModel engineModel = new AxEngineModel(engineServiceKeyArray[0]);
63 restResource = Mockito.spy(new ApexMonitoringRestResource());
64 Mockito.doReturn(engineServiceFacadeMock).when(restResource).getEngineServiceFacade("apexServer", 12345);
66 Mockito.doReturn(engineServiceKey).when(engineServiceFacadeMock).getKey();
67 Mockito.doReturn(engineServiceKeyArray).when(engineServiceFacadeMock).getEngineKeyArray();
68 Mockito.doReturn(engineModel).when(engineServiceFacadeMock).getEngineStatus(engineKey);
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"));
79 public void testRestResourceCreateSessionWithApexModelKey() throws ApexException {
80 Mockito.doReturn(new AxArtifactKey("ModelKey:0.0.1")).when(engineServiceFacadeMock).getApexModelKey();
82 Response response = restResource.createSession("apexServer", 12345);
83 assertEquals(200, response.getStatus());
84 assertTrue(((String) response.getEntity()).contains("Engine0:0.0.1"));
88 public void testRestResourceCreateSessionConnectException() throws ApexException {
89 Mockito.doThrow(new ApexDeploymentException("Connection Failed")).when(engineServiceFacadeMock).init();
91 Response response = restResource.createSession("apexServer", 12345);
92 assertEquals(500, response.getStatus());
93 assertTrue(((String) response.getEntity()).contains("Error connecting to Apex Engine Service"));
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);
101 Response response = restResource.createSession("apexServer", 12345);
102 assertEquals(200, response.getStatus());
106 public void testRestResourceCreateSessionInfo() throws ApexException {
107 final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1");
108 Mockito.doReturn("{}").when(engineServiceFacadeMock).getEngineInfo(engineKey);
110 Response response = restResource.createSession("apexServer", 12345);
111 assertEquals(200, response.getStatus());
115 public void testRestResourceCreateSessionNullInfo() throws ApexException {
116 final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1");
117 Mockito.doReturn(null).when(engineServiceFacadeMock).getEngineInfo(engineKey);
119 Response response = restResource.createSession("apexServer", 12345);
120 assertEquals(200, response.getStatus());
124 public void testRestResourceCreateSessionEmptyInfo() throws ApexException {
125 final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1");
126 Mockito.doReturn(" ").when(engineServiceFacadeMock).getEngineInfo(engineKey);
128 Response response = restResource.createSession("apexServer", 12345);
129 assertEquals(200, response.getStatus());
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);
137 Response response = restResource.createSession("apexServer", 12345);
138 assertEquals(200, response.getStatus());
142 public void testRestResourceStartEngine() throws ApexException {
143 final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1");
145 Response response = restResource.startStop("apexServer", 12345, engineKey.getId(), "Start");
146 assertEquals(200, response.getStatus());
150 public void testRestResourceStopEngine() throws ApexException {
151 final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1");
153 Response response = restResource.startStop("apexServer", 12345, engineKey.getId(), "Stop");
154 assertEquals(200, response.getStatus());
158 public void testRestResourceNotStartStopEngine() throws ApexException {
159 final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1");
161 Response response = restResource.startStop("apexServer", 12345, engineKey.getId(), "Hello");
162 assertEquals(200, response.getStatus());
166 public void testRestResourceInitExceptionStartStopEngine() throws ApexException {
167 Mockito.doThrow(new ApexDeploymentException("Exception on init")).when(engineServiceFacadeMock).init();
169 final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1");
171 Response response = restResource.startStop("apexServer", 12345, engineKey.getId(), "Hello");
172 assertEquals(500, response.getStatus());
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);
181 Response response = restResource.startStop("apexServer", 12345, engineKey.getId(), "Start");
182 assertEquals(500, response.getStatus());
186 public void testRestResourceStartPeriodicEvents() throws ApexException {
187 final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1");
189 Response response = restResource.periodiceventStartStop("apexServer", 12345, engineKey.getId(), "Start", 1000);
190 assertEquals(200, response.getStatus());
194 public void testRestResourceStopPeriodicEvents() throws ApexException {
195 final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1");
197 Response response = restResource.periodiceventStartStop("apexServer", 12345, engineKey.getId(), "Stop", 1000);
198 assertEquals(200, response.getStatus());
202 public void testRestResourceNotStartStopPeriodicEvents() throws ApexException {
203 final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1");
205 Response response = restResource.periodiceventStartStop("apexServer", 12345, engineKey.getId(), "Hello", 1000);
206 assertEquals(200, response.getStatus());
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);
215 Response response = restResource.periodiceventStartStop("apexServer", 12345, engineKey.getId(), "Stop", 1000);
216 assertEquals(500, response.getStatus());
220 public void testCounter() {
221 ApexMonitoringRestResource.Counter counter = restResource.new Counter(1538338576, 1538338592);
223 assertEquals(1538338576, counter.getTimestamp());
224 assertEquals(1538338592, counter.getValue());
228 public void testSlidingWindow() {
229 ApexMonitoringRestResource.SlidingWindowList<String> slidingWindowList0 = restResource.new SlidingWindowList<>(
232 assertFalse(slidingWindowList0.hashCode() == 0);
234 assertTrue(slidingWindowList0.add("Hello"));
235 assertTrue(slidingWindowList0.add("Hi"));
236 assertTrue(slidingWindowList0.add("Howdy"));
238 assertFalse(slidingWindowList0.equals(null));
239 assertTrue(slidingWindowList0.equals(slidingWindowList0));
241 ApexMonitoringRestResource.SlidingWindowList<String> slidingWindowList1 = restResource.new SlidingWindowList<>(
243 ApexMonitoringRestResource.SlidingWindowList<String> slidingWindowList2 = restResource.new SlidingWindowList<>(
245 assertFalse(slidingWindowList0.equals(slidingWindowList1));
246 assertFalse(slidingWindowList0.equals(slidingWindowList2));
247 assertTrue(slidingWindowList1.equals(slidingWindowList2));
248 ApexMonitoringRestResource.SlidingWindowList<String> slidingWindowList3 = restResource.new SlidingWindowList<>(
250 assertFalse(slidingWindowList1.equals(slidingWindowList3));
251 ApexMonitoringRestResource.SlidingWindowList<Integer> slidingWindowList4 = restResource.new SlidingWindowList<>(
253 assertTrue(slidingWindowList3.add("Hello"));
254 assertTrue(slidingWindowList4.add(10));
255 assertFalse(slidingWindowList3.equals(slidingWindowList4));
259 public void mopUp() {
260 assertEquals(engineServiceFacadeMock, restResource.getEngineServiceFacade("apexServer", 12345));