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.assertTrue;
26 import javax.ws.rs.core.Response;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.mockito.Mock;
32 import org.mockito.Mockito;
33 import org.mockito.MockitoAnnotations;
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 import org.powermock.api.mockito.PowerMockito;
39 import org.powermock.core.classloader.annotations.PrepareForTest;
40 import org.powermock.modules.junit4.PowerMockRunner;
43 * Test the monitoring rest resource.
45 @RunWith(PowerMockRunner.class)
46 @PrepareForTest(ApexMonitoringRestResource.class)
47 public class RestResourceTest {
49 EngineServiceFacade engineServiceFacadeMock;
52 * Set up the mocking for this test.
54 * @throws Exception on mock setup failures
57 public void setupFacade() throws Exception {
58 MockitoAnnotations.initMocks(this);
59 PowerMockito.whenNew(EngineServiceFacade.class).withAnyArguments().thenReturn(engineServiceFacadeMock);
63 public void testRestResourceCreateSession() throws ApexException {
64 final AxArtifactKey engineServiceKey = new AxArtifactKey("EngineServiceKey", "0.0.1");
65 final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1");
66 final AxArtifactKey[] engineServiceKeyArray =
68 final AxEngineModel engineModel = new AxEngineModel(engineServiceKeyArray[0]);
70 Mockito.when(engineServiceFacadeMock.getKey()).thenReturn(engineServiceKey);
71 Mockito.when(engineServiceFacadeMock.getEngineKeyArray()).thenReturn(engineServiceKeyArray);
72 Mockito.when(engineServiceFacadeMock.getEngineStatus(engineKey)).thenReturn(engineModel);
74 ApexMonitoringRestResource restResource = new ApexMonitoringRestResource();
75 Response response = restResource.createSession("apexServer", 12345);
76 assertEquals(200, response.getStatus());
77 assertTrue(((String) response.getEntity()).contains(engineKey.getId()));
81 public void testRestResourceStartEngine() throws ApexException {
82 final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1");
84 ApexMonitoringRestResource restResource = new ApexMonitoringRestResource();
85 Response response = restResource.startStop("apexServer", 12345, engineKey.getId(), "Start");
86 assertEquals(200, response.getStatus());
90 public void testRestResourceStopEngine() throws ApexException {
91 final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1");
93 ApexMonitoringRestResource restResource = new ApexMonitoringRestResource();
94 Response response = restResource.startStop("apexServer", 12345, engineKey.getId(), "Stop");
95 assertEquals(200, response.getStatus());
99 public void testRestResourceStartPeriodicEvents() throws ApexException {
100 final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1");
102 ApexMonitoringRestResource restResource = new ApexMonitoringRestResource();
103 Response response = restResource.periodiceventStartStop("apexServer", 12345, engineKey.getId(), "Start", 1000);
104 assertEquals(200, response.getStatus());
108 public void testRestResourceStopEPeriodicEvents() throws ApexException {
109 final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1");
111 ApexMonitoringRestResource restResource = new ApexMonitoringRestResource();
112 Response response = restResource.periodiceventStartStop("apexServer", 12345, engineKey.getId(), "Stop", 1000);
113 assertEquals(200, response.getStatus());
117 public void testCounter() {
118 ApexMonitoringRestResource restResource = new ApexMonitoringRestResource();
120 ApexMonitoringRestResource.Counter counter = restResource.new Counter(1538338576, 1538338592);
122 assertEquals(1538338576, counter.getTimestamp());
123 assertEquals(1538338592, counter.getValue());