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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.client.monitoring.rest;
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;
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;
42 * Test the monitoring rest resource.
44 public class RestResourceTest {
46 private EngineServiceFacade engineServiceFacadeMock;
47 private ApexMonitoringRestResource restResource;
50 * Set up mocking of the engine service facade.
52 * @throws ApexException on engine service facade setup errors
55 public void initializeMocking() throws ApexException {
56 MockitoAnnotations.initMocks(this);
58 final AxArtifactKey engineServiceKey = new AxArtifactKey("EngineServiceKey", "0.0.1");
59 final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1");
60 final AxArtifactKey[] engineServiceKeyArray =
62 final AxEngineModel engineModel = new AxEngineModel(engineServiceKeyArray[0]);
64 restResource = Mockito.spy(new ApexMonitoringRestResource());
65 Mockito.doReturn(engineServiceFacadeMock).when(restResource).getEngineServiceFacade("apexServer", 12345);
67 Mockito.doReturn(engineServiceKey).when(engineServiceFacadeMock).getKey();
68 Mockito.doReturn(engineServiceKeyArray).when(engineServiceFacadeMock).getEngineKeyArray();
69 Mockito.doReturn(engineModel).when(engineServiceFacadeMock).getEngineStatus(engineKey);
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"));
80 public void testRestResourceCreateSessionWithApexModelKey() throws ApexException {
81 Mockito.doReturn(new AxArtifactKey("ModelKey:0.0.1")).when(engineServiceFacadeMock).getApexModelKey();
83 Response response = restResource.createSession("apexServer", 12345);
84 assertEquals(200, response.getStatus());
85 assertTrue(((String) response.getEntity()).contains("Engine0:0.0.1"));
89 public void testRestResourceCreateSessionConnectException() throws ApexException {
90 Mockito.doThrow(new ApexDeploymentException("Connection Failed")).when(engineServiceFacadeMock).init();
92 Response response = restResource.createSession("apexServer", 12345);
93 assertEquals(500, response.getStatus());
94 assertTrue(((String) response.getEntity()).contains("Error connecting to Apex Engine Service"));
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);
102 Response response = restResource.createSession("apexServer", 12345);
103 assertEquals(200, response.getStatus());
107 public void testRestResourceCreateSessionInfo() throws ApexException {
108 final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1");
109 Mockito.doReturn("{}").when(engineServiceFacadeMock).getEngineInfo(engineKey);
111 Response response = restResource.createSession("apexServer", 12345);
112 assertEquals(200, response.getStatus());
116 public void testRestResourceCreateSessionNullInfo() throws ApexException {
117 final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1");
118 Mockito.doReturn(null).when(engineServiceFacadeMock).getEngineInfo(engineKey);
120 Response response = restResource.createSession("apexServer", 12345);
121 assertEquals(200, response.getStatus());
125 public void testRestResourceCreateSessionEmptyInfo() throws ApexException {
126 final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1");
127 Mockito.doReturn(" ").when(engineServiceFacadeMock).getEngineInfo(engineKey);
129 Response response = restResource.createSession("apexServer", 12345);
130 assertEquals(200, response.getStatus());
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);
138 Response response = restResource.createSession("apexServer", 12345);
139 assertEquals(200, response.getStatus());
143 public void testRestResourceStartEngine() throws ApexException {
144 final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1");
146 Response response = restResource.startStop("apexServer", 12345, engineKey.getId(), "Start");
147 assertEquals(200, response.getStatus());
151 public void testRestResourceStopEngine() throws ApexException {
152 final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1");
154 Response response = restResource.startStop("apexServer", 12345, engineKey.getId(), "Stop");
155 assertEquals(200, response.getStatus());
159 public void testRestResourceNotStartStopEngine() throws ApexException {
160 final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1");
162 Response response = restResource.startStop("apexServer", 12345, engineKey.getId(), "Hello");
163 assertEquals(200, response.getStatus());
167 public void testRestResourceInitExceptionStartStopEngine() throws ApexException {
168 Mockito.doThrow(new ApexDeploymentException("Exception on init")).when(engineServiceFacadeMock).init();
170 final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1");
172 Response response = restResource.startStop("apexServer", 12345, engineKey.getId(), "Hello");
173 assertEquals(500, response.getStatus());
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);
182 Response response = restResource.startStop("apexServer", 12345, engineKey.getId(), "Start");
183 assertEquals(500, response.getStatus());
187 public void testRestResourceStartPeriodicEvents() throws ApexException {
188 final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1");
190 Response response = restResource.periodiceventStartStop("apexServer", 12345, engineKey.getId(), "Start", 1000);
191 assertEquals(200, response.getStatus());
195 public void testRestResourceStopPeriodicEvents() throws ApexException {
196 final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1");
198 Response response = restResource.periodiceventStartStop("apexServer", 12345, engineKey.getId(), "Stop", 1000);
199 assertEquals(200, response.getStatus());
203 public void testRestResourceNotStartStopPeriodicEvents() throws ApexException {
204 final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1");
206 Response response = restResource.periodiceventStartStop("apexServer", 12345, engineKey.getId(), "Hello", 1000);
207 assertEquals(200, response.getStatus());
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);
216 Response response = restResource.periodiceventStartStop("apexServer", 12345, engineKey.getId(), "Stop", 1000);
217 assertEquals(500, response.getStatus());
221 public void testCounter() {
222 ApexMonitoringRestResource.Counter counter = restResource.new Counter(1538338576, 1538338592);
224 assertEquals(1538338576, counter.getTimestamp());
225 assertEquals(1538338592, counter.getValue());
229 public void testSlidingWindow() {
230 ApexMonitoringRestResource.SlidingWindowList<String> slidingWindowList0 = restResource.new SlidingWindowList<>(
233 assertNotEquals(0, slidingWindowList0.hashCode());
235 assertTrue(slidingWindowList0.add("Hello"));
236 assertTrue(slidingWindowList0.add("Hi"));
237 assertTrue(slidingWindowList0.add("Howdy"));
239 assertNotNull(slidingWindowList0);
240 assertEquals(slidingWindowList0, slidingWindowList0);
242 ApexMonitoringRestResource.SlidingWindowList<String> slidingWindowList1 = restResource.new SlidingWindowList<>(
244 ApexMonitoringRestResource.SlidingWindowList<String> slidingWindowList2 = restResource.new SlidingWindowList<>(
246 assertNotEquals(slidingWindowList0, slidingWindowList1);
247 assertNotEquals(slidingWindowList0, slidingWindowList2);
248 assertEquals(slidingWindowList1, slidingWindowList2);
249 ApexMonitoringRestResource.SlidingWindowList<String> slidingWindowList3 = restResource.new SlidingWindowList<>(
251 assertNotEquals(slidingWindowList1, slidingWindowList3);
252 ApexMonitoringRestResource.SlidingWindowList<Integer> slidingWindowList4 = restResource.new SlidingWindowList<>(
254 assertTrue(slidingWindowList3.add("Hello"));
255 assertTrue(slidingWindowList4.add(10));
256 assertNotEquals(slidingWindowList3, slidingWindowList4);
260 public void mopUp() {
261 assertEquals(engineServiceFacadeMock, restResource.getEngineServiceFacade("apexServer", 12345));