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