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.jupiter.api.Assertions.assertEquals;
25 import static org.junit.jupiter.api.Assertions.assertNotNull;
28 import java.time.Instant;
29 import javax.ws.rs.client.Invocation;
30 import javax.ws.rs.core.Response;
31 import org.junit.jupiter.api.BeforeAll;
32 import org.junit.jupiter.api.BeforeEach;
33 import org.junit.jupiter.api.Test;
34 import org.junit.jupiter.api.extension.ExtendWith;
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;
41 import org.springframework.beans.factory.annotation.Autowired;
42 import org.springframework.boot.test.context.SpringBootTest;
43 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
44 import org.springframework.boot.web.server.LocalServerPort;
45 import org.springframework.test.context.TestPropertySource;
46 import org.springframework.test.context.junit.jupiter.SpringExtension;
48 @ExtendWith(SpringExtension.class)
49 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
50 @TestPropertySource(locations = {"classpath:application_test.properties"})
51 class MonitoringQueryControllerTest extends CommonRestController {
53 private static final String CL_PARTICIPANT_STATISTICS_JSON =
54 "src/test/resources/rest/monitoring/TestParticipantStatistics.json";
55 private static final String CL_ELEMENT_STATISTICS_JSON =
56 "src/test/resources/rest/monitoring/TestClElementStatistics.json";
58 private static final Coder CODER = new StandardCoder();
60 private static ParticipantStatisticsList inputParticipantStatistics;
61 private static ClElementStatisticsList inputClElementStatistics;
63 private static ParticipantStatisticsList participantStatisticsList;
64 private static ClElementStatisticsList clElementStatisticsList;
66 private static final String CLELEMENT_STATS_ENDPOINT = "monitoring/clelement";
67 private static final String PARTICIPANT_STATS_ENDPOINT = "monitoring/participant";
68 private static final String PARTICIPANT_STATS_PER_CL_ENDPOINT = "monitoring/participants/controlloop";
69 private static final String CLELEMENT_STATS_PER_CL_ENDPOINT = "monitoring/clelements/controlloop";
72 private MonitoringProvider monitoringProvider;
75 private int randomServerPort;
80 * @throws Exception if an error occurs
83 public static void setUpBeforeAll() throws Exception {
85 inputParticipantStatistics =
86 CODER.decode(new File(CL_PARTICIPANT_STATISTICS_JSON), ParticipantStatisticsList.class);
87 inputClElementStatistics = CODER.decode(new File(CL_ELEMENT_STATISTICS_JSON), ClElementStatisticsList.class);
91 public void setUpBeforeEach() throws Exception {
92 super.setHttpPrefix(randomServerPort);
94 // Insert Participant statistics to DB
95 participantStatisticsList =
96 monitoringProvider.createParticipantStatistics(inputParticipantStatistics.getStatisticsList());
97 // Insert CL Element statistics to DB
98 clElementStatisticsList =
99 monitoringProvider.createClElementStatistics(inputClElementStatistics.getClElementStatistics());
103 void testQuery_Unauthorized_for_ClElementStats() throws Exception {
104 assertUnauthorizedGet(CLELEMENT_STATS_ENDPOINT);
108 void testQuery_Unauthorized_for_ClParticipantStats() throws Exception {
109 assertUnauthorizedGet(PARTICIPANT_STATS_ENDPOINT);
113 void testQuery_Unauthorized_for_ParticipantStatsPerCl() throws Exception {
114 assertUnauthorizedGet(PARTICIPANT_STATS_PER_CL_ENDPOINT);
118 void testQuery_Unauthorized_for_ClElementStatsPerCl() throws Exception {
119 assertUnauthorizedGet(CLELEMENT_STATS_PER_CL_ENDPOINT);
123 void testSwagger_ClStats() throws Exception {
124 super.testSwagger(CLELEMENT_STATS_ENDPOINT);
125 super.testSwagger(PARTICIPANT_STATS_ENDPOINT);
126 super.testSwagger(CLELEMENT_STATS_PER_CL_ENDPOINT);
127 super.testSwagger(PARTICIPANT_STATS_PER_CL_ENDPOINT);
131 void testClElementStatisticsEndpoint() throws Exception {
132 // Filter statistics only based on participant Id and UUID
133 Invocation.Builder invokeRequest1 = super.sendRequest(CLELEMENT_STATS_ENDPOINT + "?name="
134 + clElementStatisticsList.getClElementStatistics().get(0).getParticipantId().getName() + "&version="
135 + clElementStatisticsList.getClElementStatistics().get(0).getParticipantId().getVersion() + "&id="
136 + clElementStatisticsList.getClElementStatistics().get(0).getId().toString());
137 Response response1 = invokeRequest1.buildGet().invoke();
138 assertEquals(Response.Status.OK.getStatusCode(), response1.getStatus());
140 ClElementStatisticsList result1 = response1.readEntity(ClElementStatisticsList.class);
142 assertNotNull(result1);
143 assertThat(result1.getClElementStatistics()).hasSize(2);
144 assertEquals(result1.getClElementStatistics().get(0), clElementStatisticsList.getClElementStatistics().get(0));
146 // Filter statistics based on timestamp
147 Invocation.Builder invokeRequest2 = super.sendRequest(CLELEMENT_STATS_ENDPOINT + "?name="
148 + clElementStatisticsList.getClElementStatistics().get(1).getParticipantId().getName() + "&version="
149 + clElementStatisticsList.getClElementStatistics().get(1).getParticipantId().getVersion()
150 + "&startTime=" + Instant.parse("2021-01-10T13:00:00.000Z") + "&endTime="
151 + Instant.parse("2021-01-10T14:00:00.000Z"));
152 Response response2 = invokeRequest2.buildGet().invoke();
153 assertEquals(Response.Status.OK.getStatusCode(), response2.getStatus());
154 ClElementStatisticsList result2 = response2.readEntity(ClElementStatisticsList.class);
156 assertNotNull(result2);
157 assertThat(result2.getClElementStatistics()).hasSize(1);
158 assertEquals(result1.getClElementStatistics().get(0), clElementStatisticsList.getClElementStatistics().get(0));
162 void testClElementStats_BadRequest() throws Exception {
163 Invocation.Builder invokeRequest1 = super.sendRequest(CLELEMENT_STATS_ENDPOINT + "?version=1.0.0");
164 Response response1 = invokeRequest1.buildGet().invoke();
165 assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response1.getStatus());
169 void testParticipantStatisticsEndpoint() throws Exception {
171 // Filter statistics only based on participant Id
172 Invocation.Builder invokeRequest1 = super.sendRequest(PARTICIPANT_STATS_ENDPOINT + "?name="
173 + participantStatisticsList.getStatisticsList().get(0).getParticipantId().getName() + "&version="
174 + participantStatisticsList.getStatisticsList().get(0).getParticipantId().getVersion());
175 Response response1 = invokeRequest1.buildGet().invoke();
176 assertEquals(Response.Status.OK.getStatusCode(), response1.getStatus());
177 ParticipantStatisticsList result1 = response1.readEntity(ParticipantStatisticsList.class);
179 assertNotNull(result1);
180 assertThat(result1.getStatisticsList()).hasSize(2);
181 assertEquals(result1.getStatisticsList().get(0), participantStatisticsList.getStatisticsList().get(0));
183 // Filter statistics based on timestamp
184 Invocation.Builder invokeRequest2 = super.sendRequest(PARTICIPANT_STATS_ENDPOINT + "?name="
185 + participantStatisticsList.getStatisticsList().get(1).getParticipantId().getName() + "&version="
186 + participantStatisticsList.getStatisticsList().get(1).getParticipantId().getVersion() + "&startTime="
187 + Instant.parse("2021-01-10T13:00:00.000Z") + "&endTime=" + Instant.parse("2021-01-10T14:00:00.000Z"));
188 Response response2 = invokeRequest2.buildGet().invoke();
189 assertEquals(Response.Status.OK.getStatusCode(), response2.getStatus());
190 ParticipantStatisticsList result2 = response2.readEntity(ParticipantStatisticsList.class);
192 assertNotNull(result2);
193 assertThat(result2.getStatisticsList()).hasSize(1);
194 assertEquals(result1.getStatisticsList().get(0), participantStatisticsList.getStatisticsList().get(0));
198 void testParticipantStats_BadRequest() throws Exception {
199 Invocation.Builder invokeRequest1 = super.sendRequest(PARTICIPANT_STATS_ENDPOINT + "?version=0.0");
200 Response response1 = invokeRequest1.buildGet().invoke();
201 assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response1.getStatus());
205 void testParticipantStatsPerClEndpoint() throws Exception {
206 Invocation.Builder invokeRequest1 =
207 super.sendRequest(PARTICIPANT_STATS_PER_CL_ENDPOINT + "?name=dummyName&version=1.001");
208 Response response1 = invokeRequest1.buildGet().invoke();
209 assertEquals(Response.Status.OK.getStatusCode(), response1.getStatus());
210 ParticipantStatisticsList result1 = response1.readEntity(ParticipantStatisticsList.class);
211 assertThat(result1.getStatisticsList()).isEmpty();
215 void testParticipantStatsPerCl_BadRequest() throws Exception {
216 Invocation.Builder invokeRequest1 = super.sendRequest(PARTICIPANT_STATS_PER_CL_ENDPOINT);
217 Response response1 = invokeRequest1.buildGet().invoke();
218 assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response1.getStatus());
222 void testClElementStatisticsPerClEndpoint() throws Exception {
223 Invocation.Builder invokeRequest1 =
224 super.sendRequest(CLELEMENT_STATS_PER_CL_ENDPOINT + "?name=dummyName&version=1.001");
225 Response response1 = invokeRequest1.buildGet().invoke();
226 assertEquals(Response.Status.OK.getStatusCode(), response1.getStatus());
227 ClElementStatisticsList result1 = response1.readEntity(ClElementStatisticsList.class);
228 assertThat(result1.getClElementStatistics()).isEmpty();
232 void testClElementStatsPerCl_BadRequest() throws Exception {
233 Invocation.Builder invokeRequest1 = super.sendRequest(CLELEMENT_STATS_PER_CL_ENDPOINT);
234 Response response1 = invokeRequest1.buildGet().invoke();
235 assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response1.getStatus());