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;
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;
40 * Test the monitoring rest resource.
42 public class RestResourceTest {
44 private EngineServiceFacade engineServiceFacadeMock;
45 private ApexMonitoringRestResource restResource;
48 * Set up mocking of the engine service facade.
50 * @throws ApexException on engine service facade setup errors
53 public void initializeMocking() throws ApexException {
54 MockitoAnnotations.initMocks(this);
56 final AxArtifactKey engineServiceKey = new AxArtifactKey("EngineServiceKey", "0.0.1");
57 final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1");
58 final AxArtifactKey[] engineServiceKeyArray =
60 final AxEngineModel engineModel = new AxEngineModel(engineServiceKeyArray[0]);
62 restResource = Mockito.spy(new ApexMonitoringRestResource());
63 Mockito.doReturn(engineServiceFacadeMock).when(restResource).getEngineServiceFacade("apexServer", 12345);
65 Mockito.doReturn(engineServiceKey).when(engineServiceFacadeMock).getKey();
66 Mockito.doReturn(engineServiceKeyArray).when(engineServiceFacadeMock).getEngineKeyArray();
67 Mockito.doReturn(engineModel).when(engineServiceFacadeMock).getEngineStatus(engineKey);
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"));
78 public void testRestResourceCreateSessionWithApexModelKey() throws ApexException {
79 Mockito.doReturn(new AxArtifactKey("ModelKey:0.0.1")).when(engineServiceFacadeMock).getApexModelKey();
81 Response response = restResource.createSession("apexServer", 12345);
82 assertEquals(200, response.getStatus());
83 assertTrue(((String) response.getEntity()).contains("Engine0:0.0.1"));
87 public void testRestResourceCreateSessionConnectException() throws ApexException {
88 Mockito.doThrow(new ApexDeploymentException("Connection Failed")).when(engineServiceFacadeMock).init();
90 Response response = restResource.createSession("apexServer", 12345);
91 assertEquals(500, response.getStatus());
92 assertTrue(((String) response.getEntity()).contains("Error connecting to Apex Engine Service"));
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);
100 Response response = restResource.createSession("apexServer", 12345);
101 assertEquals(200, response.getStatus());
105 public void testRestResourceCreateSessionInfo() throws ApexException {
106 final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1");
107 Mockito.doReturn("{}").when(engineServiceFacadeMock).getEngineInfo(engineKey);
109 Response response = restResource.createSession("apexServer", 12345);
110 assertEquals(200, response.getStatus());
114 public void testRestResourceCreateSessionNullInfo() throws ApexException {
115 final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1");
116 Mockito.doReturn(null).when(engineServiceFacadeMock).getEngineInfo(engineKey);
118 Response response = restResource.createSession("apexServer", 12345);
119 assertEquals(200, response.getStatus());
123 public void testRestResourceCreateSessionEmptyInfo() throws ApexException {
124 final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1");
125 Mockito.doReturn(" ").when(engineServiceFacadeMock).getEngineInfo(engineKey);
127 Response response = restResource.createSession("apexServer", 12345);
128 assertEquals(200, response.getStatus());
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);
136 Response response = restResource.createSession("apexServer", 12345);
137 assertEquals(200, response.getStatus());
141 public void testRestResourceStartEngine() throws ApexException {
142 final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1");
144 Response response = restResource.startStop("apexServer", 12345, engineKey.getId(), "Start");
145 assertEquals(200, response.getStatus());
149 public void testRestResourceStopEngine() throws ApexException {
150 final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1");
152 Response response = restResource.startStop("apexServer", 12345, engineKey.getId(), "Stop");
153 assertEquals(200, response.getStatus());
157 public void testRestResourceNotStartStopEngine() throws ApexException {
158 final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1");
160 Response response = restResource.startStop("apexServer", 12345, engineKey.getId(), "Hello");
161 assertEquals(200, response.getStatus());
165 public void testRestResourceInitExceptionStartStopEngine() throws ApexException {
166 Mockito.doThrow(new ApexDeploymentException("Exception on init")).when(engineServiceFacadeMock).init();
168 final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1");
170 Response response = restResource.startStop("apexServer", 12345, engineKey.getId(), "Hello");
171 assertEquals(500, response.getStatus());
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);
180 Response response = restResource.startStop("apexServer", 12345, engineKey.getId(), "Start");
181 assertEquals(500, response.getStatus());
185 public void testRestResourceStartPeriodicEvents() throws ApexException {
186 final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1");
188 Response response = restResource.periodiceventStartStop("apexServer", 12345, engineKey.getId(), "Start", 1000);
189 assertEquals(200, response.getStatus());
193 public void testRestResourceStopPeriodicEvents() throws ApexException {
194 final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1");
196 Response response = restResource.periodiceventStartStop("apexServer", 12345, engineKey.getId(), "Stop", 1000);
197 assertEquals(200, response.getStatus());
201 public void testRestResourceNotStartStopPeriodicEvents() throws ApexException {
202 final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1");
204 Response response = restResource.periodiceventStartStop("apexServer", 12345, engineKey.getId(), "Hello", 1000);
205 assertEquals(200, response.getStatus());
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);
214 Response response = restResource.periodiceventStartStop("apexServer", 12345, engineKey.getId(), "Stop", 1000);
215 assertEquals(500, response.getStatus());
219 public void testCounter() {
220 ApexMonitoringRestResource.Counter counter = restResource.new Counter(1538338576, 1538338592);
222 assertEquals(1538338576, counter.getTimestamp());
223 assertEquals(1538338592, counter.getValue());
227 public void testSlidingWindow() {
228 ApexMonitoringRestResource.SlidingWindowList<String> slidingWindowList0 = restResource.new SlidingWindowList<>(
231 assertFalse(slidingWindowList0.hashCode() == 0);
233 assertTrue(slidingWindowList0.add("Hello"));
234 assertTrue(slidingWindowList0.add("Hi"));
235 assertTrue(slidingWindowList0.add("Howdy"));
237 assertFalse(slidingWindowList0.equals(null));
238 assertTrue(slidingWindowList0.equals(slidingWindowList0));
240 ApexMonitoringRestResource.SlidingWindowList<String> slidingWindowList1 = restResource.new SlidingWindowList<>(
242 ApexMonitoringRestResource.SlidingWindowList<String> slidingWindowList2 = restResource.new SlidingWindowList<>(
244 assertFalse(slidingWindowList0.equals(slidingWindowList1));
245 assertFalse(slidingWindowList0.equals(slidingWindowList2));
246 assertTrue(slidingWindowList1.equals(slidingWindowList2));
247 ApexMonitoringRestResource.SlidingWindowList<String> slidingWindowList3 = restResource.new SlidingWindowList<>(
249 assertFalse(slidingWindowList1.equals(slidingWindowList3));
250 ApexMonitoringRestResource.SlidingWindowList<Integer> slidingWindowList4 = restResource.new SlidingWindowList<>(
252 assertTrue(slidingWindowList3.add("Hello"));
253 assertTrue(slidingWindowList4.add(10));
254 assertFalse(slidingWindowList3.equals(slidingWindowList4));
258 public void mopUp() {
259 assertEquals(engineServiceFacadeMock, restResource.getEngineServiceFacade("apexServer", 12345));