2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Nordix Foundation.
4 * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.clamp.controlloop.runtime.monitoring;
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
26 import static org.junit.jupiter.api.Assertions.assertEquals;
27 import static org.mockito.ArgumentMatchers.any;
28 import static org.mockito.ArgumentMatchers.anyMap;
29 import static org.mockito.ArgumentMatchers.eq;
30 import static org.mockito.Mockito.mock;
31 import static org.mockito.Mockito.when;
34 import java.time.Instant;
35 import java.util.LinkedHashMap;
36 import java.util.List;
38 import java.util.Optional;
39 import java.util.UUID;
40 import javax.ws.rs.core.Response;
41 import org.junit.jupiter.api.BeforeAll;
42 import org.junit.jupiter.api.Test;
43 import org.mockito.Mockito;
44 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ClElementStatisticsList;
45 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
46 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
47 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantStatisticsList;
48 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ClElementStatisticsProvider;
49 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ControlLoopProvider;
50 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ParticipantStatisticsProvider;
51 import org.onap.policy.common.utils.coder.Coder;
52 import org.onap.policy.common.utils.coder.CoderException;
53 import org.onap.policy.common.utils.coder.StandardCoder;
54 import org.onap.policy.models.base.PfModelRuntimeException;
55 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
57 class TestMonitoringProvider {
59 private static final String CL_PARTICIPANT_STATISTICS_JSON =
60 "src/test/resources/rest/monitoring/TestParticipantStatistics.json";
61 private static final String INVALID_PARTICIPANT_JSON_INPUT =
62 "src/test/resources/rest/monitoring/TestParticipantStatistics_Invalid.json";
63 private static final String CL_ELEMENT_STATISTICS_JSON =
64 "src/test/resources/rest/monitoring/TestClElementStatistics.json";
65 private static final String INVALID_CL_ELEMENT_JSON_INPUT =
66 "src/test/resources/rest/monitoring/TestClElementStatistics_Invalid.json";
67 private static final Coder CODER = new StandardCoder();
69 private static final String STAT_LIST_IS_NULL = ".*StatisticsList is marked .*ull but is null";
70 private static final String PARTICIPANT_STAT_LIST_IS_NULL =
71 "participantStatisticsList is marked .*null but is null";
72 private static final String NAME_IS_NULL = "name is marked .*null but is null";
73 private static final String CL_LIST_IS_NULL = "clElementStatisticsList is marked .*null but is null";
74 private static final String ID_VERSION1 = "1.001";
75 private static final String ID_VERSION2 = "1.002";
76 private static final String ID_NAME1 = "name1";
77 private static final String ID_NAME2 = "name2";
78 private static final String SORT_DESC = "DESC";
79 private static final String ID_NAME3 = "testCLName";
80 private static final String ID_INVALID_NAME = "invalidCLName";
81 private static ParticipantStatisticsList inputParticipantStatistics;
82 private static ParticipantStatisticsList invalidParticipantInput;
83 private static ClElementStatisticsList inputClElementStatistics;
84 private static ClElementStatisticsList invalidClElementInput;
87 public static void beforeSetupStatistics() throws CoderException {
88 // Reading input json for statistics data
89 inputParticipantStatistics =
90 CODER.decode(new File(CL_PARTICIPANT_STATISTICS_JSON), ParticipantStatisticsList.class);
91 invalidParticipantInput =
92 CODER.decode(new File(INVALID_PARTICIPANT_JSON_INPUT), ParticipantStatisticsList.class);
93 inputClElementStatistics = CODER.decode(new File(CL_ELEMENT_STATISTICS_JSON), ClElementStatisticsList.class);
94 invalidClElementInput = CODER.decode(new File(INVALID_CL_ELEMENT_JSON_INPUT), ClElementStatisticsList.class);
98 void testCreateParticipantStatistics() throws Exception {
99 var participantStatisticsProvider = mock(ParticipantStatisticsProvider.class);
100 var clElementStatisticsProvider = mock(ClElementStatisticsProvider.class);
101 var clProvider = mock(ControlLoopProvider.class);
102 MonitoringProvider provider =
103 new MonitoringProvider(participantStatisticsProvider, clElementStatisticsProvider, clProvider);
105 when(participantStatisticsProvider.createParticipantStatistics(any()))
106 .thenReturn(inputParticipantStatistics.getStatisticsList());
108 when(participantStatisticsProvider.createParticipantStatistics(eq(null)))
109 .thenThrow(new PfModelRuntimeException(Response.Status.BAD_REQUEST, PARTICIPANT_STAT_LIST_IS_NULL));
111 // Creating statistics data in db with null input
113 assertThatThrownBy(() -> {
114 provider.createParticipantStatistics(null);
115 }).hasMessageMatching(STAT_LIST_IS_NULL);
117 assertThatThrownBy(() -> {
118 provider.createParticipantStatistics(invalidParticipantInput.getStatisticsList());
119 }).hasMessageMatching(PARTICIPANT_STAT_LIST_IS_NULL);
121 // Creating statistics data from input json
122 ParticipantStatisticsList createResponse =
123 provider.createParticipantStatistics(inputParticipantStatistics.getStatisticsList());
125 assertThat(createResponse.getStatisticsList()).hasSize(3);
126 assertEquals(createResponse.getStatisticsList().toString().replaceAll("\\s+", ""),
127 inputParticipantStatistics.getStatisticsList().toString().replaceAll("\\s+", ""));
131 void testGetParticipantStatistics() throws Exception {
132 var participantStatisticsProvider = mock(ParticipantStatisticsProvider.class);
133 when(participantStatisticsProvider.getFilteredParticipantStatistics(eq(ID_NAME1), any(), any(), any(), eq(null),
134 eq(SORT_DESC), eq(0))).thenReturn(List.of(inputParticipantStatistics.getStatisticsList().get(0)));
136 when(participantStatisticsProvider.getFilteredParticipantStatistics(eq(ID_NAME1), any(),
137 eq(Instant.parse("2021-01-11T12:00:00.000Z")), eq(Instant.parse("2021-01-11T16:00:00.000Z")), eq(null),
138 eq(SORT_DESC), eq(0))).thenReturn(List.of());
140 when(participantStatisticsProvider.getFilteredParticipantStatistics(eq(ID_NAME2), any(), any(), any(), eq(null),
141 eq(SORT_DESC), eq(1))).thenReturn(List.of(inputParticipantStatistics.getStatisticsList().get(2)));
143 var clProvider = mock(ControlLoopProvider.class);
144 var clElementStatisticsProvider = mock(ClElementStatisticsProvider.class);
145 MonitoringProvider provider =
146 new MonitoringProvider(participantStatisticsProvider, clElementStatisticsProvider, clProvider);
147 provider.createParticipantStatistics(inputParticipantStatistics.getStatisticsList());
149 assertThatThrownBy(() -> {
150 provider.fetchFilteredParticipantStatistics(null, null, 0, null, null);
151 }).hasMessageMatching(NAME_IS_NULL);
153 // Fetch specific statistics record with name, version and record count
154 ParticipantStatisticsList getResponse =
155 provider.fetchFilteredParticipantStatistics(ID_NAME2, ID_VERSION1, 1, null, null);
156 assertThat(getResponse.getStatisticsList()).hasSize(1);
157 assertEquals(getResponse.getStatisticsList().get(0).toString().replaceAll("\\s+", ""),
158 inputParticipantStatistics.getStatisticsList().get(2).toString().replaceAll("\\s+", ""));
160 // Fetch statistics using timestamp
161 getResponse = provider.fetchFilteredParticipantStatistics(ID_NAME1, ID_VERSION1, 0, null,
162 Instant.parse("2021-01-10T15:00:00.000Z"));
163 assertThat(getResponse.getStatisticsList()).hasSize(1);
165 getResponse = provider.fetchFilteredParticipantStatistics(ID_NAME1, ID_VERSION1, 0,
166 Instant.parse("2021-01-11T12:00:00.000Z"), Instant.parse("2021-01-11T16:00:00.000Z"));
168 assertThat(getResponse.getStatisticsList()).isEmpty();
172 void testCreateClElementStatistics() throws Exception {
173 var clElementStatisticsProvider = mock(ClElementStatisticsProvider.class);
174 when(clElementStatisticsProvider.createClElementStatistics(any()))
175 .thenReturn(inputClElementStatistics.getClElementStatistics());
177 when(clElementStatisticsProvider.createClElementStatistics(eq(null)))
178 .thenThrow(new PfModelRuntimeException(Response.Status.BAD_REQUEST, CL_LIST_IS_NULL));
180 var clProvider = mock(ControlLoopProvider.class);
182 var participantStatisticsProvider = mock(ParticipantStatisticsProvider.class);
183 MonitoringProvider provider =
184 new MonitoringProvider(participantStatisticsProvider, clElementStatisticsProvider, clProvider);
185 // Creating statistics data in db with null input
186 assertThatThrownBy(() -> {
187 provider.createClElementStatistics(null);
188 }).hasMessageMatching(STAT_LIST_IS_NULL);
190 assertThatThrownBy(() -> {
191 provider.createClElementStatistics(invalidClElementInput.getClElementStatistics());
192 }).hasMessageMatching(CL_LIST_IS_NULL);
194 // Creating clElement statistics data from input json
195 ClElementStatisticsList createResponse =
196 provider.createClElementStatistics(inputClElementStatistics.getClElementStatistics());
198 assertThat(createResponse.getClElementStatistics()).hasSize(4);
199 assertEquals(createResponse.getClElementStatistics().toString().replaceAll("\\s+", ""),
200 inputClElementStatistics.getClElementStatistics().toString().replaceAll("\\s+", ""));
204 void testGetClElementStatistics() throws Exception {
205 var participantStatisticsProvider = mock(ParticipantStatisticsProvider.class);
206 var clElementStatisticsProvider = mock(ClElementStatisticsProvider.class);
207 var clProvider = mock(ControlLoopProvider.class);
209 when(clElementStatisticsProvider.getFilteredClElementStatistics(eq(ID_NAME1), any(), any(), any(), anyMap(),
210 eq(SORT_DESC), eq(0)))
211 .thenReturn(List.of(inputClElementStatistics.getClElementStatistics().get(0),
212 inputClElementStatistics.getClElementStatistics().get(1)));
214 when(clElementStatisticsProvider.getFilteredClElementStatistics(eq(ID_NAME1), any(), any(), any(), anyMap(),
215 eq(SORT_DESC), eq(0)))
216 .thenReturn(List.of(inputClElementStatistics.getClElementStatistics().get(0),
217 inputClElementStatistics.getClElementStatistics().get(1)));
219 MonitoringProvider provider =
220 new MonitoringProvider(participantStatisticsProvider, clElementStatisticsProvider, clProvider);
221 assertThatThrownBy(() -> {
222 provider.fetchFilteredClElementStatistics(null, null, null, null, null, 0);
223 }).hasMessageMatching(NAME_IS_NULL);
225 provider.createClElementStatistics(inputClElementStatistics.getClElementStatistics());
227 ClElementStatisticsList getResponse =
228 provider.fetchFilteredClElementStatistics(ID_NAME1, null, null, null, null, 0);
230 assertThat(getResponse.getClElementStatistics()).hasSize(2);
231 assertEquals(getResponse.getClElementStatistics().get(0).toString().replaceAll("\\s+", ""),
232 inputClElementStatistics.getClElementStatistics().get(0).toString().replaceAll("\\s+", ""));
234 // Fetch specific statistics record with name, id and record count
235 getResponse = provider.fetchFilteredClElementStatistics(ID_NAME1, ID_VERSION1,
236 "709c62b3-8918-41b9-a747-d21eb79c6c20", null, null, 0);
237 assertThat(getResponse.getClElementStatistics()).hasSize(2);
239 // Fetch statistics using timestamp
240 getResponse = provider.fetchFilteredClElementStatistics(ID_NAME1, ID_VERSION1, null,
241 Instant.parse("2021-01-10T13:45:00.000Z"), null, 0);
242 assertThat(getResponse.getClElementStatistics()).hasSize(2);
246 void testGetParticipantStatsPerCL() throws Exception {
247 var participantStatisticsProvider = mock(ParticipantStatisticsProvider.class);
248 var clElementStatisticsProvider = mock(ClElementStatisticsProvider.class);
249 var mockClProvider = Mockito.mock(ControlLoopProvider.class);
251 new MonitoringProvider(participantStatisticsProvider, clElementStatisticsProvider, mockClProvider);
253 provider.createParticipantStatistics(inputParticipantStatistics.getStatisticsList());
255 var controlLoop = new ControlLoop();
256 var element = new ControlLoopElement();
257 element.setParticipantId(new ToscaConceptIdentifier(ID_NAME1, ID_VERSION1));
258 controlLoop.setElements(Map.of(UUID.randomUUID(), element));
259 when(mockClProvider.findControlLoop(new ToscaConceptIdentifier(ID_NAME2, ID_VERSION1)))
260 .thenReturn(Optional.of(controlLoop));
262 when(participantStatisticsProvider.getFilteredParticipantStatistics(eq(ID_NAME1), eq(ID_VERSION1), any(), any(),
263 eq(null), eq(SORT_DESC), eq(0)))
264 .thenReturn(List.of(inputParticipantStatistics.getStatisticsList().get(0),
265 inputParticipantStatistics.getStatisticsList().get(1)));
267 ParticipantStatisticsList getResponse = provider.fetchParticipantStatsPerControlLoop(ID_NAME2, ID_VERSION1);
268 assertThat(getResponse.getStatisticsList()).hasSize(2);
269 assertEquals(getResponse.getStatisticsList().get(0).toString().replaceAll("\\s+", ""),
270 inputParticipantStatistics.getStatisticsList().get(0).toString().replaceAll("\\s+", ""));
271 assertThat(provider.fetchParticipantStatsPerControlLoop(ID_INVALID_NAME, ID_VERSION2).getStatisticsList())
276 void testClElementStatsPerCL() throws Exception {
277 // Setup a dummy Control loop data
278 var mockClElement = new ControlLoopElement();
279 mockClElement.setId(inputClElementStatistics.getClElementStatistics().get(0).getId());
280 mockClElement.setParticipantId(new ToscaConceptIdentifier(
281 inputClElementStatistics.getClElementStatistics().get(0).getParticipantId().getName(),
282 inputClElementStatistics.getClElementStatistics().get(0).getParticipantId().getVersion()));
283 var mockCL = new ControlLoop();
284 mockCL.setElements(new LinkedHashMap<>());
285 mockCL.getElements().put(mockClElement.getId(), mockClElement);
287 var participantStatisticsProvider = mock(ParticipantStatisticsProvider.class);
288 var clElementStatisticsProvider = mock(ClElementStatisticsProvider.class);
289 var mockClProvider = Mockito.mock(ControlLoopProvider.class);
290 var monitoringProvider =
291 new MonitoringProvider(participantStatisticsProvider, clElementStatisticsProvider, mockClProvider);
293 // Mock controlloop data to be returned for the given CL Id
294 when(mockClProvider.findControlLoop(new ToscaConceptIdentifier(ID_NAME3, ID_VERSION1)))
295 .thenReturn(Optional.of(mockCL));
297 when(clElementStatisticsProvider.getFilteredClElementStatistics(eq(ID_NAME1), eq(ID_VERSION1), any(), any(),
298 anyMap(), eq(SORT_DESC), eq(0)))
299 .thenReturn(List.of(inputClElementStatistics.getClElementStatistics().get(0),
300 inputClElementStatistics.getClElementStatistics().get(1)));
302 monitoringProvider.createClElementStatistics(inputClElementStatistics.getClElementStatistics());
304 ClElementStatisticsList getResponse =
305 monitoringProvider.fetchClElementStatsPerControlLoop(ID_NAME3, ID_VERSION1);
307 assertThat(getResponse.getClElementStatistics()).hasSize(2);
308 assertEquals(getResponse.getClElementStatistics().get(1).toString().replaceAll("\\s+", ""),
309 inputClElementStatistics.getClElementStatistics().get(1).toString().replaceAll("\\s+", ""));
311 assertThat(monitoringProvider.fetchClElementStatsPerControlLoop(ID_INVALID_NAME, ID_VERSION2)
312 .getClElementStatistics()).isEmpty();
314 Map<String, ToscaConceptIdentifier> clElementIds =
315 monitoringProvider.getAllClElementsIdPerControlLoop(ID_NAME3, ID_VERSION1);
316 assertThat(clElementIds)
317 .containsKey(inputClElementStatistics.getClElementStatistics().get(0).getId().toString());