3ac6b6fa4643293db54e4225bcf63a3831b8e998
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Nordix Foundation.
4  * ================================================================================
5  * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.clamp.controlloop.runtime.monitoring;
24
25 import static org.assertj.core.api.Assertions.assertThat;
26 import static org.assertj.core.api.Assertions.assertThatThrownBy;
27 import static org.junit.jupiter.api.Assertions.assertEquals;
28 import static org.mockito.ArgumentMatchers.eq;
29 import static org.mockito.Mockito.when;
30
31 import java.io.File;
32 import java.time.Instant;
33 import java.util.LinkedHashMap;
34 import java.util.Map;
35 import java.util.UUID;
36 import org.junit.jupiter.api.AfterEach;
37 import org.junit.jupiter.api.BeforeAll;
38 import org.junit.jupiter.api.Test;
39 import org.mockito.Mockito;
40 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ClElementStatisticsList;
41 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
42 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
43 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantStatisticsList;
44 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ClElementStatisticsProvider;
45 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ControlLoopProvider;
46 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ParticipantStatisticsProvider;
47 import org.onap.policy.clamp.controlloop.runtime.main.parameters.ClRuntimeParameterGroup;
48 import org.onap.policy.clamp.controlloop.runtime.util.CommonTestData;
49 import org.onap.policy.common.utils.coder.Coder;
50 import org.onap.policy.common.utils.coder.CoderException;
51 import org.onap.policy.common.utils.coder.StandardCoder;
52 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
53
54 class TestMonitoringProvider {
55
56     private static final String CL_PARTICIPANT_STATISTICS_JSON =
57             "src/test/resources/rest/monitoring/TestParticipantStatistics.json";
58     private static final String INVALID_PARTICIPANT_JSON_INPUT =
59             "src/test/resources/rest/monitoring/TestParticipantStatistics_Invalid.json";
60     private static final String CL_ELEMENT_STATISTICS_JSON =
61             "src/test/resources/rest/monitoring/TestClElementStatistics.json";
62     private static final String INVALID_CL_ELEMENT_JSON_INPUT =
63             "src/test/resources/rest/monitoring/TestClElementStatistics_Invalid.json";
64     private static final Coder CODER = new StandardCoder();
65
66     private static final String LIST_IS_NULL = ".*StatisticsList is marked .*ull but is null";
67     private static ParticipantStatisticsList inputParticipantStatistics;
68     private static ParticipantStatisticsList invalidParticipantInput;
69     private static ClElementStatisticsList inputClElementStatistics;
70     private static ClElementStatisticsList invalidClElementInput;
71
72     private ParticipantStatisticsProvider participantStatisticsProvider = null;
73     private ClElementStatisticsProvider clElementStatisticsProvider = null;
74     private ControlLoopProvider clProvider = null;
75
76     @BeforeAll
77     public static void beforeSetupStatistics() throws CoderException {
78         // Reading input json for statistics data
79         inputParticipantStatistics =
80                 CODER.decode(new File(CL_PARTICIPANT_STATISTICS_JSON), ParticipantStatisticsList.class);
81         invalidParticipantInput =
82                 CODER.decode(new File(INVALID_PARTICIPANT_JSON_INPUT), ParticipantStatisticsList.class);
83         inputClElementStatistics = CODER.decode(new File(CL_ELEMENT_STATISTICS_JSON), ClElementStatisticsList.class);
84         invalidClElementInput = CODER.decode(new File(INVALID_CL_ELEMENT_JSON_INPUT), ClElementStatisticsList.class);
85     }
86
87     @AfterEach
88     void close() throws Exception {
89         if (participantStatisticsProvider != null) {
90             participantStatisticsProvider.close();
91         }
92         if (clElementStatisticsProvider != null) {
93             clElementStatisticsProvider.close();
94         }
95         if (clProvider != null) {
96             clProvider.close();
97         }
98     }
99
100     @Test
101     void testCreateParticipantStatistics() throws Exception {
102         ClRuntimeParameterGroup parameters = CommonTestData.geParameterGroup("createparStat");
103         participantStatisticsProvider = new ParticipantStatisticsProvider(parameters.getDatabaseProviderParameters());
104         clElementStatisticsProvider = new ClElementStatisticsProvider(parameters.getDatabaseProviderParameters());
105         clProvider = new ControlLoopProvider(parameters.getDatabaseProviderParameters());
106         MonitoringProvider provider =
107                 new MonitoringProvider(participantStatisticsProvider, clElementStatisticsProvider, clProvider);
108         // Creating statistics data in db with null input
109         assertThatThrownBy(() -> {
110             provider.createParticipantStatistics(null);
111         }).hasMessageMatching(LIST_IS_NULL);
112
113         assertThatThrownBy(() -> {
114             provider.createParticipantStatistics(invalidParticipantInput.getStatisticsList());
115         }).hasMessageMatching("participantStatisticsList is marked .*null but is null");
116
117         // Creating statistics data from input json
118         ParticipantStatisticsList createResponse =
119                 provider.createParticipantStatistics(inputParticipantStatistics.getStatisticsList());
120
121         assertThat(createResponse.getStatisticsList()).hasSize(3);
122         assertEquals(createResponse.getStatisticsList().toString().replaceAll("\\s+", ""),
123                 inputParticipantStatistics.getStatisticsList().toString().replaceAll("\\s+", ""));
124     }
125
126     @Test
127     void testGetParticipantStatistics() throws Exception {
128         ClRuntimeParameterGroup parameters = CommonTestData.geParameterGroup("getparStat");
129         participantStatisticsProvider = new ParticipantStatisticsProvider(parameters.getDatabaseProviderParameters());
130         clElementStatisticsProvider = new ClElementStatisticsProvider(parameters.getDatabaseProviderParameters());
131         clProvider = new ControlLoopProvider(parameters.getDatabaseProviderParameters());
132         MonitoringProvider provider =
133                 new MonitoringProvider(participantStatisticsProvider, clElementStatisticsProvider, clProvider);
134
135         provider.createParticipantStatistics(inputParticipantStatistics.getStatisticsList());
136
137         assertThatThrownBy(() -> {
138             provider.fetchFilteredParticipantStatistics(null, null, 0, null, null);
139         }).hasMessageMatching("name is marked .*null but is null");
140
141         // Fetch specific statistics record with name, version and record count
142         ParticipantStatisticsList getResponse =
143                 provider.fetchFilteredParticipantStatistics("name2", "1.001", 1, null, null);
144         assertThat(getResponse.getStatisticsList()).hasSize(1);
145         assertEquals(getResponse.getStatisticsList().get(0).toString().replaceAll("\\s+", ""),
146                 inputParticipantStatistics.getStatisticsList().get(2).toString().replaceAll("\\s+", ""));
147
148         // Fetch statistics using timestamp
149         getResponse = provider.fetchFilteredParticipantStatistics("name1", "1.001", 0, null,
150                 Instant.parse("2021-01-10T15:00:00.000Z"));
151         assertThat(getResponse.getStatisticsList()).hasSize(1);
152
153         getResponse = provider.fetchFilteredParticipantStatistics("name1", "1.001", 0,
154                 Instant.parse("2021-01-11T12:00:00.000Z"), Instant.parse("2021-01-11T16:00:00.000Z"));
155
156         assertThat(getResponse.getStatisticsList()).isEmpty();
157     }
158
159     @Test
160     void testCreateClElementStatistics() throws Exception {
161         ClRuntimeParameterGroup parameters = CommonTestData.geParameterGroup("createelemstat");
162         participantStatisticsProvider = new ParticipantStatisticsProvider(parameters.getDatabaseProviderParameters());
163         clElementStatisticsProvider = new ClElementStatisticsProvider(parameters.getDatabaseProviderParameters());
164         clProvider = new ControlLoopProvider(parameters.getDatabaseProviderParameters());
165
166         MonitoringProvider provider =
167                 new MonitoringProvider(participantStatisticsProvider, clElementStatisticsProvider, clProvider);
168         // Creating statistics data in db with null input
169         assertThatThrownBy(() -> {
170             provider.createClElementStatistics(null);
171         }).hasMessageMatching(LIST_IS_NULL);
172
173         assertThatThrownBy(() -> {
174             provider.createClElementStatistics(invalidClElementInput.getClElementStatistics());
175         }).hasMessageMatching("clElementStatisticsList is marked .*null but is null");
176
177         // Creating clElement statistics data from input json
178         ClElementStatisticsList createResponse =
179                 provider.createClElementStatistics(inputClElementStatistics.getClElementStatistics());
180
181         assertThat(createResponse.getClElementStatistics()).hasSize(4);
182         assertEquals(createResponse.getClElementStatistics().toString().replaceAll("\\s+", ""),
183                 inputClElementStatistics.getClElementStatistics().toString().replaceAll("\\s+", ""));
184     }
185
186     @Test
187     void testGetClElementStatistics() throws Exception {
188         ClRuntimeParameterGroup parameters = CommonTestData.geParameterGroup("getelemstat");
189         participantStatisticsProvider = new ParticipantStatisticsProvider(parameters.getDatabaseProviderParameters());
190         clElementStatisticsProvider = new ClElementStatisticsProvider(parameters.getDatabaseProviderParameters());
191         clProvider = new ControlLoopProvider(parameters.getDatabaseProviderParameters());
192
193         MonitoringProvider provider =
194                 new MonitoringProvider(participantStatisticsProvider, clElementStatisticsProvider, clProvider);
195         assertThatThrownBy(() -> {
196             provider.fetchFilteredClElementStatistics(null, null, null, null, null, 0);
197         }).hasMessageMatching("name is marked .*null but is null");
198
199         provider.createClElementStatistics(inputClElementStatistics.getClElementStatistics());
200
201         ClElementStatisticsList getResponse =
202                 provider.fetchFilteredClElementStatistics("name1", null, null, null, null, 0);
203
204         assertThat(getResponse.getClElementStatistics()).hasSize(2);
205         assertEquals(getResponse.getClElementStatistics().get(0).toString().replaceAll("\\s+", ""),
206                 inputClElementStatistics.getClElementStatistics().get(0).toString().replaceAll("\\s+", ""));
207
208         // Fetch specific statistics record with name, id and record count
209         getResponse = provider.fetchFilteredClElementStatistics("name1", "1.001",
210                 "709c62b3-8918-41b9-a747-d21eb79c6c20", null, null, 0);
211         assertThat(getResponse.getClElementStatistics()).hasSize(2);
212
213         // Fetch statistics using timestamp
214         getResponse = provider.fetchFilteredClElementStatistics("name1", "1.001", null,
215                 Instant.parse("2021-01-10T13:45:00.000Z"), null, 0);
216         assertThat(getResponse.getClElementStatistics()).hasSize(2);
217     }
218
219     @Test
220     void testGetParticipantStatsPerCL() throws Exception {
221         ClRuntimeParameterGroup parameters = CommonTestData.geParameterGroup("getparStatCL");
222         participantStatisticsProvider = new ParticipantStatisticsProvider(parameters.getDatabaseProviderParameters());
223         clElementStatisticsProvider = new ClElementStatisticsProvider(parameters.getDatabaseProviderParameters());
224         var mockClProvider = Mockito.mock(ControlLoopProvider.class);
225         var provider =
226                 new MonitoringProvider(participantStatisticsProvider, clElementStatisticsProvider, mockClProvider);
227
228         provider.createParticipantStatistics(inputParticipantStatistics.getStatisticsList());
229
230         var controlLoop = new ControlLoop();
231         var element = new ControlLoopElement();
232         element.setParticipantId(new ToscaConceptIdentifier("name1", "1.001"));
233         controlLoop.setElements(Map.of(UUID.randomUUID(), element));
234         when(mockClProvider.getControlLoop(new ToscaConceptIdentifier("testName", "1.001")))
235                 .thenReturn(controlLoop);
236
237         ParticipantStatisticsList getResponse = provider.fetchParticipantStatsPerControlLoop("testName", "1.001");
238         assertThat(getResponse.getStatisticsList()).hasSize(2);
239         assertEquals(getResponse.getStatisticsList().get(0).toString().replaceAll("\\s+", ""),
240                 inputParticipantStatistics.getStatisticsList().get(0).toString().replaceAll("\\s+", ""));
241         assertThat(provider.fetchParticipantStatsPerControlLoop("invalidCLName", "1.002").getStatisticsList())
242                 .isEmpty();
243     }
244
245     @Test
246     void testClElementStatsPerCL() throws Exception {
247         // Setup a dummy Control loop data
248         ControlLoopElement mockClElement = new ControlLoopElement();
249         mockClElement.setId(inputClElementStatistics.getClElementStatistics().get(0).getId());
250         mockClElement.setParticipantId(new ToscaConceptIdentifier(
251                 inputClElementStatistics.getClElementStatistics().get(0).getParticipantId().getName(),
252                 inputClElementStatistics.getClElementStatistics().get(0).getParticipantId().getVersion()));
253         ControlLoop mockCL = new ControlLoop();
254         mockCL.setElements(new LinkedHashMap<>());
255         mockCL.getElements().put(mockClElement.getId(), mockClElement);
256
257         ClRuntimeParameterGroup parameters = CommonTestData.geParameterGroup("getelemstatPerCL");
258         participantStatisticsProvider = new ParticipantStatisticsProvider(parameters.getDatabaseProviderParameters());
259         clElementStatisticsProvider = new ClElementStatisticsProvider(parameters.getDatabaseProviderParameters());
260         ControlLoopProvider mockClProvider = Mockito.mock(ControlLoopProvider.class);
261         var monitoringProvider =
262                 new MonitoringProvider(participantStatisticsProvider, clElementStatisticsProvider, mockClProvider);
263
264         // Mock controlloop data to be returned for the given CL Id
265         when(mockClProvider.getControlLoop(new ToscaConceptIdentifier("testCLName", "1.001"))).thenReturn(mockCL);
266
267         monitoringProvider.createClElementStatistics(inputClElementStatistics.getClElementStatistics());
268
269         ClElementStatisticsList getResponse =
270                 monitoringProvider.fetchClElementStatsPerControlLoop("testCLName", "1.001");
271
272         assertThat(getResponse.getClElementStatistics()).hasSize(2);
273         assertEquals(getResponse.getClElementStatistics().get(1).toString().replaceAll("\\s+", ""),
274                 inputClElementStatistics.getClElementStatistics().get(1).toString().replaceAll("\\s+", ""));
275
276         assertThat(
277                 monitoringProvider.fetchClElementStatsPerControlLoop("invalidCLName", "1.002").getClElementStatistics())
278                         .isEmpty();
279
280         Map<String, ToscaConceptIdentifier> clElementIds =
281                 monitoringProvider.getAllClElementsIdPerControlLoop("testCLName", "1.001");
282         assertThat(clElementIds)
283                 .containsKey(inputClElementStatistics.getClElementStatistics().get(0).getId().toString());
284     }
285 }