2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Nordix Foundation.
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.clamp.controlloop.runtime.monitoring.rest;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
28 import java.time.Instant;
29 import javax.ws.rs.client.Invocation;
30 import javax.ws.rs.core.Response;
31 import org.junit.AfterClass;
32 import org.junit.BeforeClass;
33 import org.junit.Ignore;
34 import org.junit.Test;
35 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ClElementStatisticsList;
36 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantStatisticsList;
37 import org.onap.policy.clamp.controlloop.runtime.monitoring.MonitoringProvider;
38 import org.onap.policy.clamp.controlloop.runtime.util.rest.CommonRestController;
39 import org.onap.policy.common.utils.coder.Coder;
40 import org.onap.policy.common.utils.coder.StandardCoder;
42 public class MonitoringQueryControllerTest extends CommonRestController {
44 private static final String CL_PARTICIPANT_STATISTICS_JSON =
45 "src/test/resources/rest/monitoring/TestParticipantStatistics.json";
46 private static final String CL_ELEMENT_STATISTICS_JSON =
47 "src/test/resources/rest/monitoring/TestClElementStatistics.json";
49 private static final Coder CODER = new StandardCoder();
51 private static ParticipantStatisticsList inputParticipantStatistics;
52 private static ClElementStatisticsList inputClElementStatistics;
54 private static ParticipantStatisticsList participantStatisticsList;
55 private static ClElementStatisticsList clElementStatisticsList;
57 private static final String CLELEMENT_STATS_ENDPOINT = "monitoring/clelement";
58 private static final String PARTICIPANT_STATS_ENDPOINT = "monitoring/participant";
59 private static final String PARTICIPANT_STATS_PER_CL_ENDPOINT = "monitoring/participants/controlloop";
60 private static final String CLELEMENT_STATS_PER_CL_ENDPOINT = "monitoring/clelements/controlloop";
66 * @throws Exception if an error occurs
69 public static void setUpBeforeClass() throws Exception {
70 CommonRestController.setUpBeforeClass("testStatisticsQuery");
71 inputParticipantStatistics = CODER.decode(new File(CL_PARTICIPANT_STATISTICS_JSON),
72 ParticipantStatisticsList.class);
73 inputClElementStatistics = CODER.decode(new File(CL_ELEMENT_STATISTICS_JSON),
74 ClElementStatisticsList.class);
76 try (MonitoringProvider monitoringProvider = new MonitoringProvider(getParameters())) {
77 // Insert Participant statistics to DB
78 participantStatisticsList = monitoringProvider.createParticipantStatistics(inputParticipantStatistics
79 .getStatisticsList());
80 // Insert CL Element statistics to DB
81 clElementStatisticsList = monitoringProvider.createClElementStatistics(inputClElementStatistics
82 .getClElementStatistics());
87 public static void teardownAfterClass() {
88 CommonRestController.teardownAfterClass();
92 public void testQuery_Unauthorized_for_ClElementStats() throws Exception {
93 assertUnauthorizedGet(CLELEMENT_STATS_ENDPOINT);
97 public void testQuery_Unauthorized_for_ClParticipantStats() throws Exception {
98 assertUnauthorizedGet(PARTICIPANT_STATS_ENDPOINT);
102 public void testQuery_Unauthorized_for_ParticipantStatsPerCl() throws Exception {
103 assertUnauthorizedGet(PARTICIPANT_STATS_PER_CL_ENDPOINT);
107 public void testQuery_Unauthorized_for_ClElementStatsPerCl() throws Exception {
108 assertUnauthorizedGet(CLELEMENT_STATS_PER_CL_ENDPOINT);
112 public void testSwagger_ClStats() throws Exception {
113 super.testSwagger(CLELEMENT_STATS_ENDPOINT);
114 super.testSwagger(PARTICIPANT_STATS_ENDPOINT);
115 super.testSwagger(CLELEMENT_STATS_PER_CL_ENDPOINT);
116 super.testSwagger(PARTICIPANT_STATS_PER_CL_ENDPOINT);
120 public void testClElementStatisticsEndpoint() throws Exception {
121 // Filter statistics only based on participant Id and UUID
122 Invocation.Builder invokeRequest1 =
123 super.sendRequest(CLELEMENT_STATS_ENDPOINT + "?name=" + clElementStatisticsList
124 .getClElementStatistics().get(0).getParticipantId().getName() + "&version=" + clElementStatisticsList
125 .getClElementStatistics().get(0).getParticipantId().getVersion() + "&id=" + clElementStatisticsList
126 .getClElementStatistics().get(0).getId().toString());
127 Response response1 = invokeRequest1.buildGet().invoke();
128 assertEquals(Response.Status.OK.getStatusCode(), response1.getStatus());
130 ClElementStatisticsList result1 = response1.readEntity(ClElementStatisticsList.class);
132 assertNotNull(result1);
133 assertThat(result1.getClElementStatistics()).hasSize(2);
134 assertEquals(result1.getClElementStatistics().get(0), clElementStatisticsList
135 .getClElementStatistics().get(0));
137 // Filter statistics based on timestamp
138 Invocation.Builder invokeRequest2 =
139 super.sendRequest(CLELEMENT_STATS_ENDPOINT + "?name=" + clElementStatisticsList
140 .getClElementStatistics().get(1).getParticipantId().getName() + "&version=" + clElementStatisticsList
141 .getClElementStatistics().get(1).getParticipantId().getVersion() + "&startTime="
142 + Instant.parse("2021-01-10T13:00:00.000Z") + "&endTime=" + Instant.parse("2021-01-10T14:00:00.000Z"));
143 Response response2 = invokeRequest2.buildGet().invoke();
144 assertEquals(Response.Status.OK.getStatusCode(), response2.getStatus());
145 ClElementStatisticsList result2 = response2.readEntity(ClElementStatisticsList.class);
147 assertNotNull(result2);
148 assertThat(result2.getClElementStatistics()).hasSize(1);
149 assertEquals(result1.getClElementStatistics().get(0), clElementStatisticsList
150 .getClElementStatistics().get(0));
154 public void testClElementStats_BadRequest() throws Exception {
155 Invocation.Builder invokeRequest1 =
156 super.sendRequest(CLELEMENT_STATS_ENDPOINT + "?version=1.0.0");
157 Response response1 = invokeRequest1.buildGet().invoke();
158 assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response1.getStatus());
162 public void testParticipantStatisticsEndpoint() throws Exception {
164 // Filter statistics only based on participant Id
165 Invocation.Builder invokeRequest1 =
166 super.sendRequest(PARTICIPANT_STATS_ENDPOINT + "?name=" + participantStatisticsList
167 .getStatisticsList().get(0).getParticipantId().getName() + "&version=" + participantStatisticsList
168 .getStatisticsList().get(0).getParticipantId().getVersion());
169 Response response1 = invokeRequest1.buildGet().invoke();
170 assertEquals(Response.Status.OK.getStatusCode(), response1.getStatus());
171 ParticipantStatisticsList result1 = response1.readEntity(ParticipantStatisticsList.class);
173 assertNotNull(result1);
174 assertThat(result1.getStatisticsList()).hasSize(2);
175 assertEquals(result1.getStatisticsList().get(0), participantStatisticsList
176 .getStatisticsList().get(0));
178 // Filter statistics based on timestamp
179 Invocation.Builder invokeRequest2 =
180 super.sendRequest(PARTICIPANT_STATS_ENDPOINT + "?name=" + participantStatisticsList
181 .getStatisticsList().get(1).getParticipantId().getName() + "&version=" + participantStatisticsList
182 .getStatisticsList().get(1).getParticipantId().getVersion() + "&startTime="
183 + Instant.parse("2021-01-10T13:00:00.000Z") + "&endTime=" + Instant.parse("2021-01-10T14:00:00.000Z"));
184 Response response2 = invokeRequest2.buildGet().invoke();
185 assertEquals(Response.Status.OK.getStatusCode(), response2.getStatus());
186 ParticipantStatisticsList result2 = response2.readEntity(ParticipantStatisticsList.class);
188 assertNotNull(result2);
189 assertThat(result2.getStatisticsList()).hasSize(1);
190 assertEquals(result1.getStatisticsList().get(0), participantStatisticsList
191 .getStatisticsList().get(0));
195 public void testParticipantStats_BadRequest() throws Exception {
196 Invocation.Builder invokeRequest1 =
197 super.sendRequest(PARTICIPANT_STATS_ENDPOINT + "?version=0.0");
198 Response response1 = invokeRequest1.buildGet().invoke();
199 assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response1.getStatus());
203 public void testParticipantStatsPerClEndpoint() throws Exception {
204 Invocation.Builder invokeRequest1 =
205 super.sendRequest(PARTICIPANT_STATS_PER_CL_ENDPOINT + "?name=dummyName&version=1.001");
206 Response response1 = invokeRequest1.buildGet().invoke();
207 assertEquals(Response.Status.OK.getStatusCode(), response1.getStatus());
208 ParticipantStatisticsList result1 = response1.readEntity(ParticipantStatisticsList.class);
209 assertThat(result1.getStatisticsList()).isEmpty();
213 public void testParticipantStatsPerCl_BadRequest() throws Exception {
214 Invocation.Builder invokeRequest1 =
215 super.sendRequest(PARTICIPANT_STATS_PER_CL_ENDPOINT);
216 Response response1 = invokeRequest1.buildGet().invoke();
217 assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response1.getStatus());
221 public void testClElementStatisticsPerClEndpoint() throws Exception {
222 Invocation.Builder invokeRequest1 =
223 super.sendRequest(CLELEMENT_STATS_PER_CL_ENDPOINT + "?name=dummyName&version=1.001");
224 Response response1 = invokeRequest1.buildGet().invoke();
225 assertEquals(Response.Status.OK.getStatusCode(), response1.getStatus());
226 ClElementStatisticsList result1 = response1.readEntity(ClElementStatisticsList.class);
227 assertThat(result1.getClElementStatistics()).isEmpty();
231 public void testClElementStatsPerCl_BadRequest() throws Exception {
232 Invocation.Builder invokeRequest1 =
233 super.sendRequest(CLELEMENT_STATS_PER_CL_ENDPOINT);
234 Response response1 = invokeRequest1.buildGet().invoke();
235 assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response1.getStatus());