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;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.jupiter.api.Assertions.assertEquals;
26 import static org.mockito.ArgumentMatchers.eq;
27 import static org.mockito.Mockito.when;
30 import java.time.Instant;
31 import java.util.LinkedHashMap;
33 import java.util.UUID;
34 import org.junit.jupiter.api.AfterEach;
35 import org.junit.jupiter.api.BeforeAll;
36 import org.junit.jupiter.api.Test;
37 import org.mockito.Mockito;
38 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ClElementStatisticsList;
39 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
40 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
41 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantStatisticsList;
42 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ClElementStatisticsProvider;
43 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ControlLoopProvider;
44 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ParticipantStatisticsProvider;
45 import org.onap.policy.clamp.controlloop.runtime.main.parameters.ClRuntimeParameterGroup;
46 import org.onap.policy.clamp.controlloop.runtime.util.CommonTestData;
47 import org.onap.policy.common.utils.coder.Coder;
48 import org.onap.policy.common.utils.coder.CoderException;
49 import org.onap.policy.common.utils.coder.StandardCoder;
50 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
52 class TestMonitoringProvider {
54 private static final String CL_PARTICIPANT_STATISTICS_JSON =
55 "src/test/resources/rest/monitoring/TestParticipantStatistics.json";
56 private static final String INVALID_PARTICIPANT_JSON_INPUT =
57 "src/test/resources/rest/monitoring/TestParticipantStatistics_Invalid.json";
58 private static final String CL_ELEMENT_STATISTICS_JSON =
59 "src/test/resources/rest/monitoring/TestClElementStatistics.json";
60 private static final String INVALID_CL_ELEMENT_JSON_INPUT =
61 "src/test/resources/rest/monitoring/TestClElementStatistics_Invalid.json";
62 private static final Coder CODER = new StandardCoder();
64 private static final String LIST_IS_NULL = ".*StatisticsList is marked .*ull but is null";
65 private static ParticipantStatisticsList inputParticipantStatistics;
66 private static ParticipantStatisticsList invalidParticipantInput;
67 private static ClElementStatisticsList inputClElementStatistics;
68 private static ClElementStatisticsList invalidClElementInput;
70 private ParticipantStatisticsProvider participantStatisticsProvider = null;
71 private ClElementStatisticsProvider clElementStatisticsProvider = null;
72 private ControlLoopProvider clProvider = null;
75 public static void beforeSetupStatistics() throws CoderException {
76 // Reading input json for statistics data
77 inputParticipantStatistics =
78 CODER.decode(new File(CL_PARTICIPANT_STATISTICS_JSON), ParticipantStatisticsList.class);
79 invalidParticipantInput =
80 CODER.decode(new File(INVALID_PARTICIPANT_JSON_INPUT), ParticipantStatisticsList.class);
81 inputClElementStatistics = CODER.decode(new File(CL_ELEMENT_STATISTICS_JSON), ClElementStatisticsList.class);
82 invalidClElementInput = CODER.decode(new File(INVALID_CL_ELEMENT_JSON_INPUT), ClElementStatisticsList.class);
86 void close() throws Exception {
87 if (participantStatisticsProvider != null) {
88 participantStatisticsProvider.close();
90 if (clElementStatisticsProvider != null) {
91 clElementStatisticsProvider.close();
93 if (clProvider != null) {
99 void testCreateParticipantStatistics() throws Exception {
100 ClRuntimeParameterGroup parameters = CommonTestData.geParameterGroup(0, "createparStat");
101 participantStatisticsProvider = new ParticipantStatisticsProvider(parameters.getDatabaseProviderParameters());
102 clElementStatisticsProvider = new ClElementStatisticsProvider(parameters.getDatabaseProviderParameters());
103 clProvider = new ControlLoopProvider(parameters.getDatabaseProviderParameters());
104 MonitoringProvider provider =
105 new MonitoringProvider(participantStatisticsProvider, clElementStatisticsProvider, clProvider);
106 // Creating statistics data in db with null input
107 assertThatThrownBy(() -> {
108 provider.createParticipantStatistics(null);
109 }).hasMessageMatching(LIST_IS_NULL);
111 assertThatThrownBy(() -> {
112 provider.createParticipantStatistics(invalidParticipantInput.getStatisticsList());
113 }).hasMessageMatching("participantStatisticsList is marked .*null but is null");
115 // Creating statistics data from input json
116 ParticipantStatisticsList createResponse =
117 provider.createParticipantStatistics(inputParticipantStatistics.getStatisticsList());
119 assertThat(createResponse.getStatisticsList()).hasSize(3);
120 assertEquals(createResponse.getStatisticsList().toString().replaceAll("\\s+", ""),
121 inputParticipantStatistics.getStatisticsList().toString().replaceAll("\\s+", ""));
125 void testGetParticipantStatistics() throws Exception {
126 ClRuntimeParameterGroup parameters = CommonTestData.geParameterGroup(0, "getparStat");
127 participantStatisticsProvider = new ParticipantStatisticsProvider(parameters.getDatabaseProviderParameters());
128 clElementStatisticsProvider = new ClElementStatisticsProvider(parameters.getDatabaseProviderParameters());
129 clProvider = new ControlLoopProvider(parameters.getDatabaseProviderParameters());
130 MonitoringProvider provider =
131 new MonitoringProvider(participantStatisticsProvider, clElementStatisticsProvider, clProvider);
133 provider.createParticipantStatistics(inputParticipantStatistics.getStatisticsList());
135 assertThatThrownBy(() -> {
136 provider.fetchFilteredParticipantStatistics(null, null, 0, null, null);
137 }).hasMessageMatching("name is marked .*null but is null");
139 // Fetch specific statistics record with name, version and record count
140 ParticipantStatisticsList getResponse =
141 provider.fetchFilteredParticipantStatistics("name2", "1.001", 1, null, null);
142 assertThat(getResponse.getStatisticsList()).hasSize(1);
143 assertEquals(getResponse.getStatisticsList().get(0).toString().replaceAll("\\s+", ""),
144 inputParticipantStatistics.getStatisticsList().get(2).toString().replaceAll("\\s+", ""));
146 // Fetch statistics using timestamp
147 getResponse = provider.fetchFilteredParticipantStatistics("name1", "1.001", 0, null,
148 Instant.parse("2021-01-10T15:00:00.000Z"));
149 assertThat(getResponse.getStatisticsList()).hasSize(1);
151 getResponse = provider.fetchFilteredParticipantStatistics("name1", "1.001", 0,
152 Instant.parse("2021-01-11T12:00:00.000Z"), Instant.parse("2021-01-11T16:00:00.000Z"));
154 assertThat(getResponse.getStatisticsList()).isEmpty();
158 void testCreateClElementStatistics() throws Exception {
159 ClRuntimeParameterGroup parameters = CommonTestData.geParameterGroup(0, "createelemstat");
160 participantStatisticsProvider = new ParticipantStatisticsProvider(parameters.getDatabaseProviderParameters());
161 clElementStatisticsProvider = new ClElementStatisticsProvider(parameters.getDatabaseProviderParameters());
162 clProvider = new ControlLoopProvider(parameters.getDatabaseProviderParameters());
164 MonitoringProvider provider =
165 new MonitoringProvider(participantStatisticsProvider, clElementStatisticsProvider, clProvider);
166 // Creating statistics data in db with null input
167 assertThatThrownBy(() -> {
168 provider.createClElementStatistics(null);
169 }).hasMessageMatching(LIST_IS_NULL);
171 assertThatThrownBy(() -> {
172 provider.createClElementStatistics(invalidClElementInput.getClElementStatistics());
173 }).hasMessageMatching("clElementStatisticsList is marked .*null but is null");
175 // Creating clElement statistics data from input json
176 ClElementStatisticsList createResponse =
177 provider.createClElementStatistics(inputClElementStatistics.getClElementStatistics());
179 assertThat(createResponse.getClElementStatistics()).hasSize(4);
180 assertEquals(createResponse.getClElementStatistics().toString().replaceAll("\\s+", ""),
181 inputClElementStatistics.getClElementStatistics().toString().replaceAll("\\s+", ""));
185 void testGetClElementStatistics() throws Exception {
186 ClRuntimeParameterGroup parameters = CommonTestData.geParameterGroup(0, "getelemstat");
187 participantStatisticsProvider = new ParticipantStatisticsProvider(parameters.getDatabaseProviderParameters());
188 clElementStatisticsProvider = new ClElementStatisticsProvider(parameters.getDatabaseProviderParameters());
189 clProvider = new ControlLoopProvider(parameters.getDatabaseProviderParameters());
191 MonitoringProvider provider =
192 new MonitoringProvider(participantStatisticsProvider, clElementStatisticsProvider, clProvider);
194 assertThatThrownBy(() -> {
195 provider.fetchFilteredClElementStatistics(null, null, null, null, null, 0);
196 }).hasMessageMatching("name is marked .*null but is null");
198 provider.createClElementStatistics(inputClElementStatistics.getClElementStatistics());
200 ClElementStatisticsList getResponse =
201 provider.fetchFilteredClElementStatistics("name1", null, null, null, null, 0);
203 assertThat(getResponse.getClElementStatistics()).hasSize(2);
204 assertEquals(getResponse.getClElementStatistics().get(0).toString().replaceAll("\\s+", ""),
205 inputClElementStatistics.getClElementStatistics().get(0).toString().replaceAll("\\s+", ""));
207 // Fetch specific statistics record with name, id and record count
208 getResponse = provider.fetchFilteredClElementStatistics("name1", "1.001",
209 "709c62b3-8918-41b9-a747-d21eb79c6c20", null, null, 0);
210 assertThat(getResponse.getClElementStatistics()).hasSize(2);
212 // Fetch statistics using timestamp
213 getResponse = provider.fetchFilteredClElementStatistics("name1", "1.001", null,
214 Instant.parse("2021-01-10T13:45:00.000Z"), null, 0);
215 assertThat(getResponse.getClElementStatistics()).hasSize(2);
219 void testGetParticipantStatsPerCL() throws Exception {
220 ClRuntimeParameterGroup parameters = CommonTestData.geParameterGroup(0, "getparStatCL");
221 participantStatisticsProvider = new ParticipantStatisticsProvider(parameters.getDatabaseProviderParameters());
222 clElementStatisticsProvider = new ClElementStatisticsProvider(parameters.getDatabaseProviderParameters());
223 var mockClProvider = Mockito.mock(ControlLoopProvider.class);
225 new MonitoringProvider(participantStatisticsProvider, clElementStatisticsProvider, mockClProvider);
227 provider.createParticipantStatistics(inputParticipantStatistics.getStatisticsList());
229 var controlLoop = new ControlLoop();
230 var element = new ControlLoopElement();
231 element.setParticipantId(new ToscaConceptIdentifier("name1", "1.001"));
232 controlLoop.setElements(Map.of(UUID.randomUUID(), element));
233 when(mockClProvider.getControlLoop(eq(new ToscaConceptIdentifier("testName", "1.001"))))
234 .thenReturn(controlLoop);
236 ParticipantStatisticsList getResponse = provider.fetchParticipantStatsPerControlLoop("testName", "1.001");
237 assertThat(getResponse.getStatisticsList()).hasSize(2);
238 assertEquals(getResponse.getStatisticsList().get(0).toString().replaceAll("\\s+", ""),
239 inputParticipantStatistics.getStatisticsList().get(0).toString().replaceAll("\\s+", ""));
240 assertThat(provider.fetchParticipantStatsPerControlLoop("invalidCLName", "1.002").getStatisticsList())
245 void testClElementStatsPerCL() throws Exception {
246 // Setup a dummy Control loop data
247 ControlLoopElement mockClElement = new ControlLoopElement();
248 mockClElement.setId(inputClElementStatistics.getClElementStatistics().get(0).getId());
249 mockClElement.setParticipantId(new ToscaConceptIdentifier(
250 inputClElementStatistics.getClElementStatistics().get(0).getParticipantId().getName(),
251 inputClElementStatistics.getClElementStatistics().get(0).getParticipantId().getVersion()));
252 ControlLoop mockCL = new ControlLoop();
253 mockCL.setElements(new LinkedHashMap<>());
254 mockCL.getElements().put(mockClElement.getId(), mockClElement);
256 ClRuntimeParameterGroup parameters = CommonTestData.geParameterGroup(0, "getelemstatPerCL");
257 participantStatisticsProvider = new ParticipantStatisticsProvider(parameters.getDatabaseProviderParameters());
258 clElementStatisticsProvider = new ClElementStatisticsProvider(parameters.getDatabaseProviderParameters());
259 ControlLoopProvider mockClProvider = Mockito.mock(ControlLoopProvider.class);
260 var monitoringProvider =
261 new MonitoringProvider(participantStatisticsProvider, clElementStatisticsProvider, mockClProvider);
263 // Mock controlloop data to be returned for the given CL Id
264 when(mockClProvider.getControlLoop(new ToscaConceptIdentifier("testCLName", "1.001"))).thenReturn(mockCL);
266 monitoringProvider.createClElementStatistics(inputClElementStatistics.getClElementStatistics());
268 ClElementStatisticsList getResponse;
269 getResponse = monitoringProvider.fetchClElementStatsPerControlLoop("testCLName", "1.001");
271 assertThat(getResponse.getClElementStatistics()).hasSize(2);
272 assertEquals(getResponse.getClElementStatistics().get(1).toString().replaceAll("\\s+", ""),
273 inputClElementStatistics.getClElementStatistics().get(1).toString().replaceAll("\\s+", ""));
276 monitoringProvider.fetchClElementStatsPerControlLoop("invalidCLName", "1.002").getClElementStatistics())
279 Map<String, ToscaConceptIdentifier> clElementIds =
280 monitoringProvider.getAllClElementsIdPerControlLoop("testCLName", "1.001");
281 assertThat(clElementIds)
282 .containsKey(inputClElementStatistics.getClElementStatistics().get(0).getId().toString());