56735ae12acb8efc25f4dcc52f1738c07d90d260
[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;
22
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;
27
28 import java.io.File;
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;
34 import java.util.Map;
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;
49
50 public class TestMonitoringProvider {
51
52     private static final String CL_PARTICIPANT_STATISTICS_JSON =
53         "src/test/resources/rest/monitoring/TestParticipantStatistics.json";
54     private static final String INVALID_PARTICIPANT_JSON_INPUT =
55         "src/test/resources/rest/monitoring/TestParticipantStatistics_Invalid.json";
56     private static final String CL_ELEMENT_STATISTICS_JSON =
57         "src/test/resources/rest/monitoring/TestClElementStatistics.json";
58     private static final String INVALID_CL_ELEMENT_JSON_INPUT =
59         "src/test/resources/rest/monitoring/TestClElementStatistics_Invalid.json";
60     private static final Coder CODER = new StandardCoder();
61
62     private static final String CL_PROVIDER_FIELD = "controlLoopProvider";
63
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;
69
70     @BeforeClass
71     public static void beforeSetupStatistics() throws CoderException {
72         // Reading input json for statistics data
73         inputParticipantStatistics =
74             CODER.decode(new File(CL_PARTICIPANT_STATISTICS_JSON), ParticipantStatisticsList.class);
75         invalidParticipantInput =
76             CODER.decode(new File(INVALID_PARTICIPANT_JSON_INPUT), ParticipantStatisticsList.class);
77         inputClElementStatistics = CODER.decode(new File(CL_ELEMENT_STATISTICS_JSON), ClElementStatisticsList.class);
78         invalidClElementInput = CODER.decode(new File(INVALID_CL_ELEMENT_JSON_INPUT), ClElementStatisticsList.class);
79     }
80
81     @Test
82     public void testCreateParticipantStatistics() throws Exception {
83         PolicyModelsProviderParameters parameters =
84             CommonTestData.geParameterGroup(0, "createparStat").getDatabaseProviderParameters();
85
86         try (MonitoringProvider provider = new MonitoringProvider(parameters)) {
87             // Creating statistics data in db with null input
88             assertThatThrownBy(() -> {
89                 provider.createParticipantStatistics(null);
90             }).hasMessageMatching(LIST_IS_NULL);
91
92             assertThatThrownBy(() -> {
93                 provider.createParticipantStatistics(invalidParticipantInput.getStatisticsList());
94             }).hasMessageMatching("participantStatisticsList is marked .*null but is null");
95
96             // Creating statistics data from input json
97             ParticipantStatisticsList createResponse =
98                 provider.createParticipantStatistics(inputParticipantStatistics.getStatisticsList());
99
100             assertThat(createResponse.getStatisticsList()).hasSize(3);
101             assertEquals(createResponse.getStatisticsList().toString().replaceAll("\\s+", ""),
102                 inputParticipantStatistics.getStatisticsList().toString().replaceAll("\\s+", ""));
103         }
104     }
105
106     @Test
107     public void testGetParticipantStatistics() throws Exception {
108         PolicyModelsProviderParameters parameters =
109             CommonTestData.geParameterGroup(0, "getparStat").getDatabaseProviderParameters();
110         try (MonitoringProvider provider = new MonitoringProvider(parameters)) {
111             ParticipantStatisticsList getResponse;
112
113             provider.createParticipantStatistics(inputParticipantStatistics.getStatisticsList());
114
115             assertThatThrownBy(() -> {
116                 provider.fetchFilteredParticipantStatistics(null, null, 0, null, null);
117             }).hasMessageMatching("name is marked .*null but is null");
118
119             // Fetch specific statistics record with name, version and record count
120             getResponse = provider.fetchFilteredParticipantStatistics("name2", "1.001", 1, null, null);
121             assertThat(getResponse.getStatisticsList()).hasSize(1);
122             assertEquals(getResponse.getStatisticsList().get(0).toString().replaceAll("\\s+", ""),
123                 inputParticipantStatistics.getStatisticsList().get(2).toString().replaceAll("\\s+", ""));
124
125             // Fetch statistics using timestamp
126             getResponse = provider.fetchFilteredParticipantStatistics("name1", "1.001", 0, null,
127                 Instant.parse("2021-01-10T15:00:00.000Z"));
128             assertThat(getResponse.getStatisticsList()).hasSize(1);
129
130             getResponse = provider.fetchFilteredParticipantStatistics("name1", "1.001", 0,
131                 Instant.parse("2021-01-11T12:00:00.000Z"), Instant.parse("2021-01-11T16:00:00.000Z"));
132
133             assertThat(getResponse.getStatisticsList()).isEmpty();
134         }
135     }
136
137     @Test
138     public void testCreateClElementStatistics() throws Exception {
139         PolicyModelsProviderParameters parameters =
140             CommonTestData.geParameterGroup(0, "createelemstat").getDatabaseProviderParameters();
141         try (MonitoringProvider provider = new MonitoringProvider(parameters)) {
142             // Creating statistics data in db with null input
143             assertThatThrownBy(() -> {
144                 provider.createClElementStatistics(null);
145             }).hasMessageMatching(LIST_IS_NULL);
146
147             assertThatThrownBy(() -> {
148                 provider.createClElementStatistics(invalidClElementInput.getClElementStatistics());
149             }).hasMessageMatching("clElementStatisticsList is marked .*null but is null");
150
151             // Creating clElement statistics data from input json
152             ClElementStatisticsList createResponse =
153                 provider.createClElementStatistics(inputClElementStatistics.getClElementStatistics());
154
155             assertThat(createResponse.getClElementStatistics()).hasSize(4);
156             assertEquals(createResponse.getClElementStatistics().toString().replaceAll("\\s+", ""),
157                 inputClElementStatistics.getClElementStatistics().toString().replaceAll("\\s+", ""));
158         }
159     }
160
161     @Test
162     public void testGetClElementStatistics() throws Exception {
163         PolicyModelsProviderParameters parameters =
164             CommonTestData.geParameterGroup(0, "getelemstat").getDatabaseProviderParameters();
165         try (MonitoringProvider provider = new MonitoringProvider(parameters)) {
166             ClElementStatisticsList getResponse;
167
168             assertThatThrownBy(() -> {
169                 provider.fetchFilteredClElementStatistics(null, null, null, null, null, 0);
170             }).hasMessageMatching("name is marked .*null but is null");
171
172             provider.createClElementStatistics(inputClElementStatistics.getClElementStatistics());
173
174             getResponse = provider.fetchFilteredClElementStatistics("name1", null, null, null, null, 0);
175
176             assertThat(getResponse.getClElementStatistics()).hasSize(2);
177             assertEquals(getResponse.getClElementStatistics().get(0).toString().replaceAll("\\s+", ""),
178                 inputClElementStatistics.getClElementStatistics().get(0).toString().replaceAll("\\s+", ""));
179
180             // Fetch specific statistics record with name, id and record count
181             getResponse = provider.fetchFilteredClElementStatistics("name1", "1.001",
182                 "709c62b3-8918-41b9-a747-d21eb79c6c20", null, null, 0);
183             assertThat(getResponse.getClElementStatistics()).hasSize(2);
184
185             // Fetch statistics using timestamp
186             getResponse = provider.fetchFilteredClElementStatistics("name1", "1.001", null,
187                 Instant.parse("2021-01-10T13:45:00.000Z"), null, 0);
188             assertThat(getResponse.getClElementStatistics()).hasSize(2);
189         }
190     }
191
192     @Test
193     public void testGetParticipantStatsPerCL() throws Exception {
194         PolicyModelsProviderParameters parameters =
195             CommonTestData.geParameterGroup(0, "getparStatCL").getDatabaseProviderParameters();
196         try (MonitoringProvider provider = Mockito.spy(new MonitoringProvider(parameters))) {
197
198             provider.createParticipantStatistics(inputParticipantStatistics.getStatisticsList());
199             // Mock the response for fetching participant conceptIdentifiers per control loop
200             List<ToscaConceptIdentifier> conceptIdentifiers = new ArrayList<>();
201             conceptIdentifiers.add(new ToscaConceptIdentifier("name1", "1.001"));
202             when(provider.getAllParticipantIdsPerControlLoop("testName", "1.001")).thenReturn(conceptIdentifiers);
203             ParticipantStatisticsList getResponse;
204             getResponse = provider.fetchParticipantStatsPerControlLoop("testName", "1.001");
205             assertThat(getResponse.getStatisticsList()).hasSize(2);
206             assertEquals(getResponse.getStatisticsList().get(0).toString().replaceAll("\\s+", ""),
207                 inputParticipantStatistics.getStatisticsList().get(0).toString().replaceAll("\\s+", ""));
208             assertThat(provider.fetchParticipantStatsPerControlLoop("invalidCLName", "1.002").getStatisticsList())
209                 .isEmpty();
210         }
211
212     }
213
214     @Test
215     public void testClElementStatsPerCL() throws Exception {
216         PolicyModelsProviderParameters parameters =
217             CommonTestData.geParameterGroup(0, "getelemstatPerCL").getDatabaseProviderParameters();
218         // Setup a dummy Control loop data
219         ControlLoopElement mockClElement = new ControlLoopElement();
220         mockClElement.setId(inputClElementStatistics.getClElementStatistics().get(0).getId());
221         mockClElement.setParticipantId(new ToscaConceptIdentifier(
222             inputClElementStatistics.getClElementStatistics().get(0).getParticipantId().getName(),
223             inputClElementStatistics.getClElementStatistics().get(0).getParticipantId().getVersion()));
224         ControlLoop mockCL = new ControlLoop();
225         mockCL.setElements(new LinkedHashMap<>());
226         mockCL.getElements().put(mockClElement.getId(), mockClElement);
227
228         // Mock controlloop data to be returned for the given CL Id
229         ControlLoopProvider mockClProvider = Mockito.mock(ControlLoopProvider.class);
230         when(mockClProvider.getControlLoop(new ToscaConceptIdentifier("testCLName", "1.001"))).thenReturn(mockCL);
231
232         try (MonitoringProvider monitoringProvider = new MonitoringProvider(parameters)) {
233             monitoringProvider.createClElementStatistics(inputClElementStatistics.getClElementStatistics());
234             Field controlLoopProviderField = monitoringProvider.getClass().getDeclaredField(CL_PROVIDER_FIELD);
235             controlLoopProviderField.setAccessible(true);
236             controlLoopProviderField.set(monitoringProvider, mockClProvider);
237
238             ClElementStatisticsList getResponse;
239             getResponse = monitoringProvider.fetchClElementStatsPerControlLoop("testCLName", "1.001");
240
241             assertThat(getResponse.getClElementStatistics()).hasSize(2);
242             assertEquals(getResponse.getClElementStatistics().get(1).toString().replaceAll("\\s+", ""),
243                 inputClElementStatistics.getClElementStatistics().get(1).toString().replaceAll("\\s+", ""));
244
245             assertThat(
246                 monitoringProvider.fetchClElementStatsPerControlLoop("invalidCLName", "1.002").getClElementStatistics())
247                     .isEmpty();
248
249             Map<String, ToscaConceptIdentifier> clElementIds =
250                 monitoringProvider.getAllClElementsIdPerControlLoop("testCLName", "1.001");
251             assertThat(clElementIds)
252                 .containsKey(inputClElementStatistics.getClElementStatistics().get(0).getId().toString());
253         }
254     }
255 }