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.Assert.assertEquals;
26 import static org.mockito.Mockito.when;
29 import java.lang.reflect.Field;
30 import java.time.Instant;
31 import java.util.ArrayList;
32 import java.util.LinkedHashMap;
33 import java.util.List;
35 import org.junit.BeforeClass;
36 import org.junit.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.ControlLoopProvider;
43 import org.onap.policy.clamp.controlloop.runtime.util.CommonTestData;
44 import org.onap.policy.common.utils.coder.Coder;
45 import org.onap.policy.common.utils.coder.CoderException;
46 import org.onap.policy.common.utils.coder.StandardCoder;
47 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
51 public class TestMonitoringProvider {
53 private static final String CL_PARTICIPANT_STATISTICS_JSON =
54 "src/test/resources/rest/monitoring/TestParticipantStatistics.json";
55 private static final String INVALID_PARTICIPANT_JSON_INPUT =
56 "src/test/resources/rest/monitoring/TestParticipantStatistics_Invalid.json";
57 private static final String CL_ELEMENT_STATISTICS_JSON =
58 "src/test/resources/rest/monitoring/TestClElementStatistics.json";
59 private static final String INVALID_CL_ELEMENT_JSON_INPUT =
60 "src/test/resources/rest/monitoring/TestClElementStatistics_Invalid.json";
61 private static final Coder CODER = new StandardCoder();
63 private static final String CL_PROVIDER_FIELD = "controlLoopProvider";
65 private static final String LIST_IS_NULL = ".*StatisticsList is marked .*ull but is null";
66 private static ParticipantStatisticsList inputParticipantStatistics;
67 private static ParticipantStatisticsList invalidParticipantInput;
68 private static ClElementStatisticsList inputClElementStatistics;
69 private static ClElementStatisticsList invalidClElementInput;
74 public static void beforeSetupStatistics() throws CoderException {
75 // Reading input json for statistics data
76 inputParticipantStatistics =
77 CODER.decode(new File(CL_PARTICIPANT_STATISTICS_JSON), ParticipantStatisticsList.class);
78 invalidParticipantInput =
79 CODER.decode(new File(INVALID_PARTICIPANT_JSON_INPUT), ParticipantStatisticsList.class);
80 inputClElementStatistics = CODER.decode(new File(CL_ELEMENT_STATISTICS_JSON), ClElementStatisticsList.class);
81 invalidClElementInput = CODER.decode(new File(INVALID_CL_ELEMENT_JSON_INPUT), ClElementStatisticsList.class);
86 public void testCreateParticipantStatistics() throws Exception {
87 PolicyModelsProviderParameters parameters =
88 CommonTestData.geParameterGroup(0, "createparStat").getDatabaseProviderParameters();
90 try (MonitoringProvider provider = new MonitoringProvider(parameters)) {
91 // Creating statistics data in db with null input
92 assertThatThrownBy(() -> {
93 provider.createParticipantStatistics(null);
94 }).hasMessageMatching(LIST_IS_NULL);
96 assertThatThrownBy(() -> {
97 provider.createParticipantStatistics(invalidParticipantInput.getStatisticsList());
98 }).hasMessageMatching("participantStatisticsList is marked .*null but is null");
100 // Creating statistics data from input json
101 ParticipantStatisticsList createResponse =
102 provider.createParticipantStatistics(inputParticipantStatistics.getStatisticsList());
104 assertThat(createResponse.getStatisticsList()).hasSize(3);
105 assertEquals(createResponse.getStatisticsList().toString().replaceAll("\\s+", ""),
106 inputParticipantStatistics.getStatisticsList().toString().replaceAll("\\s+", ""));
111 public void testGetParticipantStatistics() throws Exception {
112 PolicyModelsProviderParameters parameters =
113 CommonTestData.geParameterGroup(0, "getparStat").getDatabaseProviderParameters();
114 try (MonitoringProvider provider = new MonitoringProvider(parameters)) {
115 ParticipantStatisticsList getResponse;
117 provider.createParticipantStatistics(inputParticipantStatistics.getStatisticsList());
119 assertThatThrownBy(() -> {
120 provider.fetchFilteredParticipantStatistics(null, null, 0, null, null);
121 }).hasMessageMatching("name is marked .*null but is null");
123 // Fetch specific statistics record with name, version and record count
124 getResponse = provider.fetchFilteredParticipantStatistics("name2", "1.001", 1,
126 assertThat(getResponse.getStatisticsList()).hasSize(1);
127 assertEquals(getResponse.getStatisticsList().get(0).toString().replaceAll("\\s+", ""),
128 inputParticipantStatistics.getStatisticsList().get(2).toString().replaceAll("\\s+", ""));
130 // Fetch statistics using timestamp
131 getResponse = provider.fetchFilteredParticipantStatistics("name1", "1.001", 0,
132 null, Instant.parse("2021-01-10T15:00:00.000Z"));
133 assertThat(getResponse.getStatisticsList()).hasSize(1);
135 getResponse = provider.fetchFilteredParticipantStatistics("name1", "1.001", 0,
136 Instant.parse("2021-01-11T12:00:00.000Z"), Instant.parse("2021-01-11T16:00:00.000Z"));
138 assertThat(getResponse.getStatisticsList()).isEmpty();
143 public void testCreateClElementStatistics() throws Exception {
144 PolicyModelsProviderParameters parameters =
145 CommonTestData.geParameterGroup(0, "createelemstat").getDatabaseProviderParameters();
146 try (MonitoringProvider provider = new MonitoringProvider(parameters)) {
147 // Creating statistics data in db with null input
148 assertThatThrownBy(() -> {
149 provider.createClElementStatistics(null);
150 }).hasMessageMatching(LIST_IS_NULL);
152 assertThatThrownBy(() -> {
153 provider.createClElementStatistics(invalidClElementInput.getClElementStatistics());
154 }).hasMessageMatching("clElementStatisticsList is marked .*null but is null");
156 // Creating clElement statistics data from input json
157 ClElementStatisticsList createResponse =
158 provider.createClElementStatistics(inputClElementStatistics.getClElementStatistics());
160 assertThat(createResponse.getClElementStatistics()).hasSize(4);
161 assertEquals(createResponse.getClElementStatistics().toString().replaceAll("\\s+", ""),
162 inputClElementStatistics.getClElementStatistics().toString().replaceAll("\\s+", ""));
167 public void testGetClElementStatistics() throws Exception {
168 PolicyModelsProviderParameters parameters =
169 CommonTestData.geParameterGroup(0, "getelemstat").getDatabaseProviderParameters();
170 try (MonitoringProvider provider = new MonitoringProvider(parameters)) {
171 ClElementStatisticsList getResponse;
173 assertThatThrownBy(() -> {
174 provider.fetchFilteredClElementStatistics(null, null, null, null,
176 }).hasMessageMatching("name is marked .*null but is null");
178 ClElementStatisticsList lists = provider.createClElementStatistics(inputClElementStatistics
179 .getClElementStatistics());
181 getResponse = provider.fetchFilteredClElementStatistics("name1", null, null, null,
184 assertThat(getResponse.getClElementStatistics()).hasSize(2);
185 assertEquals(getResponse.getClElementStatistics().get(0).toString().replaceAll("\\s+", ""),
186 inputClElementStatistics.getClElementStatistics().get(0).toString().replaceAll("\\s+", ""));
188 // Fetch specific statistics record with name, id and record count
189 getResponse = provider.fetchFilteredClElementStatistics("name1", "1.001",
190 "709c62b3-8918-41b9-a747-d21eb79c6c20", null, null, 0);
191 assertThat(getResponse.getClElementStatistics()).hasSize(2);
193 // Fetch statistics using timestamp
194 getResponse = provider.fetchFilteredClElementStatistics("name1", "1.001", null,
195 Instant.parse("2021-01-10T13:45:00.000Z"), null, 0);
196 assertThat(getResponse.getClElementStatistics()).hasSize(2);
201 public void testGetParticipantStatsPerCL() throws Exception {
202 PolicyModelsProviderParameters parameters =
203 CommonTestData.geParameterGroup(0, "getparStatCL").getDatabaseProviderParameters();
204 try (MonitoringProvider provider = Mockito.spy(new MonitoringProvider(parameters))) {
206 provider.createParticipantStatistics(inputParticipantStatistics.getStatisticsList());
207 //Mock the response for fetching participant conceptIdentifiers per control loop
208 List<ToscaConceptIdentifier> conceptIdentifiers = new ArrayList<>();
209 conceptIdentifiers.add(new ToscaConceptIdentifier("name1", "1.001"));
210 when(provider.getAllParticipantIdsPerControlLoop("testName", "1.001"))
211 .thenReturn(conceptIdentifiers);
212 ParticipantStatisticsList getResponse;
213 getResponse = provider.fetchParticipantStatsPerControlLoop("testName", "1.001");
214 assertThat(getResponse.getStatisticsList()).hasSize(2);
215 assertEquals(getResponse.getStatisticsList().get(0).toString().replaceAll("\\s+", ""),
216 inputParticipantStatistics.getStatisticsList().get(0).toString().replaceAll("\\s+", ""));
217 assertThat(provider.fetchParticipantStatsPerControlLoop("invalidCLName", "1.002")
218 .getStatisticsList()).isEmpty();
224 public void testClElementStatsPerCL() throws Exception {
225 PolicyModelsProviderParameters parameters =
226 CommonTestData.geParameterGroup(0, "getelemstatPerCL").getDatabaseProviderParameters();
227 //Setup a dummy Control loop data
228 ControlLoopElement mockClElement = new ControlLoopElement();
229 mockClElement.setId(inputClElementStatistics.getClElementStatistics().get(0).getId());
230 mockClElement.setParticipantId(new ToscaConceptIdentifier(inputClElementStatistics.getClElementStatistics()
231 .get(0).getParticipantId().getName(), inputClElementStatistics.getClElementStatistics().get(0)
232 .getParticipantId().getVersion()));
233 ControlLoop mockCL = new ControlLoop();
234 mockCL.setElements(new LinkedHashMap<>());
235 mockCL.getElements().put(mockClElement.getId(), mockClElement);
237 //Mock controlloop data to be returned for the given CL Id
238 ControlLoopProvider mockClProvider = Mockito.mock(ControlLoopProvider.class);
239 when(mockClProvider.getControlLoop(new ToscaConceptIdentifier("testCLName", "1.001")))
242 try (MonitoringProvider monitoringProvider = new MonitoringProvider(parameters)) {
243 monitoringProvider.createClElementStatistics(inputClElementStatistics.getClElementStatistics());
244 Field controlLoopProviderField = monitoringProvider.getClass().getDeclaredField(CL_PROVIDER_FIELD);
245 controlLoopProviderField.setAccessible(true);
246 controlLoopProviderField.set(monitoringProvider, mockClProvider);
248 ClElementStatisticsList getResponse;
249 getResponse = monitoringProvider.fetchClElementStatsPerControlLoop("testCLName", "1.001");
251 assertThat(getResponse.getClElementStatistics()).hasSize(2);
252 assertEquals(getResponse.getClElementStatistics().get(1).toString().replaceAll("\\s+", ""),
253 inputClElementStatistics.getClElementStatistics().get(1).toString().replaceAll("\\s+", ""));
255 assertThat(monitoringProvider.fetchClElementStatsPerControlLoop("invalidCLName", "1.002")
256 .getClElementStatistics()).isEmpty();
258 Map<String, ToscaConceptIdentifier> clElementIds = monitoringProvider
259 .getAllClElementsIdPerControlLoop("testCLName", "1.001");
260 assertThat(clElementIds).containsKey(inputClElementStatistics.getClElementStatistics().get(0).getId()