95b2113cdb735b99398cb7f2347bbc3a0264a976
[policy/clamp.git] /
1 /*-
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
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.clamp.controlloop.runtime.monitoring.rest;
22
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;
26
27 import java.io.File;
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.main.parameters.ClRuntimeParameterGroup;
38 import org.onap.policy.clamp.controlloop.runtime.monitoring.MonitoringProvider;
39 import org.onap.policy.clamp.controlloop.runtime.util.rest.CommonRestController;
40 import org.onap.policy.common.utils.coder.Coder;
41 import org.onap.policy.common.utils.coder.StandardCoder;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.boot.test.context.SpringBootTest;
44 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
45 import org.springframework.boot.web.server.LocalServerPort;
46 import org.springframework.test.context.TestPropertySource;
47 import org.springframework.test.context.junit.jupiter.SpringExtension;
48
49 @ExtendWith(SpringExtension.class)
50 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
51 @TestPropertySource(locations = {"classpath:application_test.properties"})
52 class MonitoringQueryControllerTest extends CommonRestController {
53
54     private static final String CL_PARTICIPANT_STATISTICS_JSON =
55             "src/test/resources/rest/monitoring/TestParticipantStatistics.json";
56     private static final String CL_ELEMENT_STATISTICS_JSON =
57             "src/test/resources/rest/monitoring/TestClElementStatistics.json";
58
59     private static final Coder CODER = new StandardCoder();
60
61     private static ParticipantStatisticsList inputParticipantStatistics;
62     private static ClElementStatisticsList inputClElementStatistics;
63
64     private static ParticipantStatisticsList participantStatisticsList;
65     private static ClElementStatisticsList clElementStatisticsList;
66
67     private static final String CLELEMENT_STATS_ENDPOINT = "monitoring/clelement";
68     private static final String PARTICIPANT_STATS_ENDPOINT = "monitoring/participant";
69     private static final String PARTICIPANT_STATS_PER_CL_ENDPOINT = "monitoring/participants/controlloop";
70     private static final String CLELEMENT_STATS_PER_CL_ENDPOINT = "monitoring/clelements/controlloop";
71
72     @Autowired
73     private ClRuntimeParameterGroup clRuntimeParameterGroup;
74
75     @LocalServerPort
76     private int randomServerPort;
77
78     /**
79      * starts Main.
80      *
81      * @throws Exception if an error occurs
82      */
83     @BeforeAll
84     public static void setUpBeforeAll() throws Exception {
85
86         inputParticipantStatistics =
87                 CODER.decode(new File(CL_PARTICIPANT_STATISTICS_JSON), ParticipantStatisticsList.class);
88         inputClElementStatistics = CODER.decode(new File(CL_ELEMENT_STATISTICS_JSON), ClElementStatisticsList.class);
89     }
90
91     @BeforeEach
92     public void setUpBeforeEach() throws Exception {
93         super.setHttpPrefix(randomServerPort);
94
95         try (var monitoringProvider = new MonitoringProvider(clRuntimeParameterGroup)) {
96             // Insert Participant statistics to DB
97             participantStatisticsList =
98                     monitoringProvider.createParticipantStatistics(inputParticipantStatistics.getStatisticsList());
99             // Insert CL Element statistics to DB
100             clElementStatisticsList =
101                     monitoringProvider.createClElementStatistics(inputClElementStatistics.getClElementStatistics());
102         }
103     }
104
105     @Test
106     void testQuery_Unauthorized_for_ClElementStats() throws Exception {
107         assertUnauthorizedGet(CLELEMENT_STATS_ENDPOINT);
108     }
109
110     @Test
111     void testQuery_Unauthorized_for_ClParticipantStats() throws Exception {
112         assertUnauthorizedGet(PARTICIPANT_STATS_ENDPOINT);
113     }
114
115     @Test
116     void testQuery_Unauthorized_for_ParticipantStatsPerCl() throws Exception {
117         assertUnauthorizedGet(PARTICIPANT_STATS_PER_CL_ENDPOINT);
118     }
119
120     @Test
121     void testQuery_Unauthorized_for_ClElementStatsPerCl() throws Exception {
122         assertUnauthorizedGet(CLELEMENT_STATS_PER_CL_ENDPOINT);
123     }
124
125     @Test
126     void testSwagger_ClStats() throws Exception {
127         super.testSwagger(CLELEMENT_STATS_ENDPOINT);
128         super.testSwagger(PARTICIPANT_STATS_ENDPOINT);
129         super.testSwagger(CLELEMENT_STATS_PER_CL_ENDPOINT);
130         super.testSwagger(PARTICIPANT_STATS_PER_CL_ENDPOINT);
131     }
132
133     @Test
134     void testClElementStatisticsEndpoint() throws Exception {
135         // Filter statistics only based on participant Id and UUID
136         Invocation.Builder invokeRequest1 = super.sendRequest(CLELEMENT_STATS_ENDPOINT + "?name="
137                 + clElementStatisticsList.getClElementStatistics().get(0).getParticipantId().getName() + "&version="
138                 + clElementStatisticsList.getClElementStatistics().get(0).getParticipantId().getVersion() + "&id="
139                 + clElementStatisticsList.getClElementStatistics().get(0).getId().toString());
140         Response response1 = invokeRequest1.buildGet().invoke();
141         assertEquals(Response.Status.OK.getStatusCode(), response1.getStatus());
142
143         ClElementStatisticsList result1 = response1.readEntity(ClElementStatisticsList.class);
144
145         assertNotNull(result1);
146         assertThat(result1.getClElementStatistics()).hasSize(2);
147         assertEquals(result1.getClElementStatistics().get(0), clElementStatisticsList.getClElementStatistics().get(0));
148
149         // Filter statistics based on timestamp
150         Invocation.Builder invokeRequest2 = super.sendRequest(CLELEMENT_STATS_ENDPOINT + "?name="
151                 + clElementStatisticsList.getClElementStatistics().get(1).getParticipantId().getName() + "&version="
152                 + clElementStatisticsList.getClElementStatistics().get(1).getParticipantId().getVersion()
153                 + "&startTime=" + Instant.parse("2021-01-10T13:00:00.000Z") + "&endTime="
154                 + Instant.parse("2021-01-10T14:00:00.000Z"));
155         Response response2 = invokeRequest2.buildGet().invoke();
156         assertEquals(Response.Status.OK.getStatusCode(), response2.getStatus());
157         ClElementStatisticsList result2 = response2.readEntity(ClElementStatisticsList.class);
158
159         assertNotNull(result2);
160         assertThat(result2.getClElementStatistics()).hasSize(1);
161         assertEquals(result1.getClElementStatistics().get(0), clElementStatisticsList.getClElementStatistics().get(0));
162     }
163
164     @Test
165     void testClElementStats_BadRequest() throws Exception {
166         Invocation.Builder invokeRequest1 = super.sendRequest(CLELEMENT_STATS_ENDPOINT + "?version=1.0.0");
167         Response response1 = invokeRequest1.buildGet().invoke();
168         assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response1.getStatus());
169     }
170
171     @Test
172     void testParticipantStatisticsEndpoint() throws Exception {
173
174         // Filter statistics only based on participant Id
175         Invocation.Builder invokeRequest1 = super.sendRequest(PARTICIPANT_STATS_ENDPOINT + "?name="
176                 + participantStatisticsList.getStatisticsList().get(0).getParticipantId().getName() + "&version="
177                 + participantStatisticsList.getStatisticsList().get(0).getParticipantId().getVersion());
178         Response response1 = invokeRequest1.buildGet().invoke();
179         assertEquals(Response.Status.OK.getStatusCode(), response1.getStatus());
180         ParticipantStatisticsList result1 = response1.readEntity(ParticipantStatisticsList.class);
181
182         assertNotNull(result1);
183         assertThat(result1.getStatisticsList()).hasSize(2);
184         assertEquals(result1.getStatisticsList().get(0), participantStatisticsList.getStatisticsList().get(0));
185
186         // Filter statistics based on timestamp
187         Invocation.Builder invokeRequest2 = super.sendRequest(PARTICIPANT_STATS_ENDPOINT + "?name="
188                 + participantStatisticsList.getStatisticsList().get(1).getParticipantId().getName() + "&version="
189                 + participantStatisticsList.getStatisticsList().get(1).getParticipantId().getVersion() + "&startTime="
190                 + Instant.parse("2021-01-10T13:00:00.000Z") + "&endTime=" + Instant.parse("2021-01-10T14:00:00.000Z"));
191         Response response2 = invokeRequest2.buildGet().invoke();
192         assertEquals(Response.Status.OK.getStatusCode(), response2.getStatus());
193         ParticipantStatisticsList result2 = response2.readEntity(ParticipantStatisticsList.class);
194
195         assertNotNull(result2);
196         assertThat(result2.getStatisticsList()).hasSize(1);
197         assertEquals(result1.getStatisticsList().get(0), participantStatisticsList.getStatisticsList().get(0));
198     }
199
200     @Test
201     void testParticipantStats_BadRequest() throws Exception {
202         Invocation.Builder invokeRequest1 = super.sendRequest(PARTICIPANT_STATS_ENDPOINT + "?version=0.0");
203         Response response1 = invokeRequest1.buildGet().invoke();
204         assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response1.getStatus());
205     }
206
207     @Test
208     void testParticipantStatsPerClEndpoint() throws Exception {
209         Invocation.Builder invokeRequest1 =
210                 super.sendRequest(PARTICIPANT_STATS_PER_CL_ENDPOINT + "?name=dummyName&version=1.001");
211         Response response1 = invokeRequest1.buildGet().invoke();
212         assertEquals(Response.Status.OK.getStatusCode(), response1.getStatus());
213         ParticipantStatisticsList result1 = response1.readEntity(ParticipantStatisticsList.class);
214         assertThat(result1.getStatisticsList()).isEmpty();
215     }
216
217     @Test
218     void testParticipantStatsPerCl_BadRequest() throws Exception {
219         Invocation.Builder invokeRequest1 = super.sendRequest(PARTICIPANT_STATS_PER_CL_ENDPOINT);
220         Response response1 = invokeRequest1.buildGet().invoke();
221         assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response1.getStatus());
222     }
223
224     @Test
225     void testClElementStatisticsPerClEndpoint() throws Exception {
226         Invocation.Builder invokeRequest1 =
227                 super.sendRequest(CLELEMENT_STATS_PER_CL_ENDPOINT + "?name=dummyName&version=1.001");
228         Response response1 = invokeRequest1.buildGet().invoke();
229         assertEquals(Response.Status.OK.getStatusCode(), response1.getStatus());
230         ClElementStatisticsList result1 = response1.readEntity(ClElementStatisticsList.class);
231         assertThat(result1.getClElementStatistics()).isEmpty();
232     }
233
234     @Test
235     void testClElementStatsPerCl_BadRequest() throws Exception {
236         Invocation.Builder invokeRequest1 = super.sendRequest(CLELEMENT_STATS_PER_CL_ENDPOINT);
237         Response response1 = invokeRequest1.buildGet().invoke();
238         assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response1.getStatus());
239     }
240 }