d63f6bd8c809f1bbb935147c71f451e7aa36fa3c
[policy/apex-pdp.git] /
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.assertTrue;
25
26 import javax.ws.rs.core.Response;
27
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;
41
42 /**
43  * Test the monitoring rest resource.
44  */
45 @RunWith(PowerMockRunner.class)
46 @PrepareForTest(ApexMonitoringRestResource.class)
47 public class RestResourceTest {
48     @Mock
49     EngineServiceFacade engineServiceFacadeMock;
50
51     /**
52      * Set up the mocking for this test.
53      * 
54      * @throws Exception on mock setup failures
55      */
56     @Before
57     public void setupFacade() throws Exception {
58         MockitoAnnotations.initMocks(this);
59         PowerMockito.whenNew(EngineServiceFacade.class).withAnyArguments().thenReturn(engineServiceFacadeMock);
60     }
61
62     @Test
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 =
67             { engineKey };
68         final AxEngineModel engineModel = new AxEngineModel(engineServiceKeyArray[0]);
69
70         Mockito.when(engineServiceFacadeMock.getKey()).thenReturn(engineServiceKey);
71         Mockito.when(engineServiceFacadeMock.getEngineKeyArray()).thenReturn(engineServiceKeyArray);
72         Mockito.when(engineServiceFacadeMock.getEngineStatus(engineKey)).thenReturn(engineModel);
73
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()));
78     }
79
80     @Test
81     public void testRestResourceStartEngine() throws ApexException {
82         final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1");
83
84         ApexMonitoringRestResource restResource = new ApexMonitoringRestResource();
85         Response response = restResource.startStop("apexServer", 12345, engineKey.getId(), "Start");
86         assertEquals(200, response.getStatus());
87     }
88
89     @Test
90     public void testRestResourceStopEngine() throws ApexException {
91         final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1");
92
93         ApexMonitoringRestResource restResource = new ApexMonitoringRestResource();
94         Response response = restResource.startStop("apexServer", 12345, engineKey.getId(), "Stop");
95         assertEquals(200, response.getStatus());
96     }
97
98     @Test
99     public void testRestResourceStartPeriodicEvents() throws ApexException {
100         final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1");
101
102         ApexMonitoringRestResource restResource = new ApexMonitoringRestResource();
103         Response response = restResource.periodiceventStartStop("apexServer", 12345, engineKey.getId(), "Start", 1000);
104         assertEquals(200, response.getStatus());
105     }
106
107     @Test
108     public void testRestResourceStopEPeriodicEvents() throws ApexException {
109         final AxArtifactKey engineKey = new AxArtifactKey("Engine0", "0.0.1");
110
111         ApexMonitoringRestResource restResource = new ApexMonitoringRestResource();
112         Response response = restResource.periodiceventStartStop("apexServer", 12345, engineKey.getId(), "Stop", 1000);
113         assertEquals(200, response.getStatus());
114     }
115
116     @Test
117     public void testCounter() {
118         ApexMonitoringRestResource restResource = new ApexMonitoringRestResource();
119
120         ApexMonitoringRestResource.Counter counter = restResource.new Counter(1538338576, 1538338592);
121
122         assertEquals(1538338576, counter.getTimestamp());
123         assertEquals(1538338592, counter.getValue());
124     }
125 }