86fd9b0436e1ae912708c4800816a8b994cda55d
[policy/pap.git] / main / src / test / java / org / onap / policy / pap / main / service / PdpStatisticsServiceTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2022 Bell Canada. All rights reserved.
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.pap.main.service;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25
26 import java.time.Instant;
27 import java.util.ArrayList;
28 import java.util.List;
29 import java.util.Map;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.onap.policy.models.base.Validated;
33 import org.onap.policy.models.pdp.concepts.PdpStatistics;
34 import org.onap.policy.pap.main.rest.CommonPapRestServer;
35 import org.springframework.beans.factory.annotation.Autowired;
36
37 public class PdpStatisticsServiceTest extends CommonPapRestServer {
38
39     private static final String NAME3 = "name3";
40     private static final String NAME1 = "name1";
41     private static final String LIST_IS_NULL = "pdpStatisticsList is marked .*ull but is null";
42     private static final String GROUP0 = "group0";
43     private static final String GROUP = "group";
44     private static final String SUBGROUP = "subgroup";
45     private static final Instant TIMESTAMP1 = Instant.ofEpochSecond(1078884319);
46     private static final Instant TIMESTAMP2 = Instant.ofEpochSecond(1078884350);
47     private static final Integer NUMBER_RECORDS = 10;
48
49     @Autowired
50     private PdpStatisticsService pdpStatisticsService;
51
52     private PdpStatistics pdpStatistics1;
53     private PdpStatistics pdpStatistics2;
54     private PdpStatistics pdpStatistics3;
55     private PdpStatistics pdpStatistics4;
56
57     /**
58      * Setup before tests.
59      *
60      * @throws Exception the exception
61      */
62     @Override
63     @Before
64     public void setUp() throws Exception {
65         super.setUp();
66         pdpStatistics1 = generatePdpStatistics(NAME1, TIMESTAMP1, 1L, GROUP, SUBGROUP);
67         pdpStatistics2 = generatePdpStatistics("name2", TIMESTAMP1, 2L, GROUP, SUBGROUP);
68         pdpStatistics3 = generatePdpStatistics(NAME1, TIMESTAMP2, 3L, GROUP, SUBGROUP);
69         pdpStatistics4 = generatePdpStatistics(NAME3, TIMESTAMP2, 4L, GROUP0, SUBGROUP);
70     }
71
72     @Test
73     public void testCreatePdpStatisticsSuccess() {
74         List<PdpStatistics> createList = List.of(pdpStatistics1, pdpStatistics3, pdpStatistics4, pdpStatistics2);
75         List<PdpStatistics> createdPdpStatisticsList = pdpStatisticsService.createPdpStatistics(createList);
76         // these should match AND be in the same order
77         assertThat(createdPdpStatisticsList).isEqualTo(createList);
78     }
79
80     @Test
81     public void testCreatePdpStatisticsFailure() {
82
83         assertThatThrownBy(() -> {
84             pdpStatisticsService.createPdpStatistics(null);
85         }).hasMessageMatching(LIST_IS_NULL);
86
87         PdpStatistics pdpStatisticsErr = new PdpStatistics();
88         pdpStatisticsErr.setPdpInstanceId("NULL");
89         pdpStatisticsErr.setPdpGroupName(GROUP);
90
91         assertThatThrownBy(() -> {
92             pdpStatisticsService.createPdpStatistics(List.of(pdpStatisticsErr));
93         }).hasMessageContaining("pdp statistics").hasMessageContaining("key")
94             .hasMessageContaining(Validated.IS_A_NULL_KEY);
95     }
96
97     @Test
98     public void testFetchDatabaseStatistics() {
99
100         List<PdpStatistics> createList = List.of(pdpStatistics1, pdpStatistics3, pdpStatistics4, pdpStatistics2);
101         pdpStatisticsService.createPdpStatistics(createList);
102         Map<String, Map<String, List<PdpStatistics>>> created =
103             pdpStatisticsService.fetchDatabaseStatistics(NUMBER_RECORDS, null, null);
104         assertThat(created).hasSize(2);
105         assertThat(created.get(GROUP0)).hasSize(1);
106         assertThat(created.get(GROUP0).get(SUBGROUP)).hasSize(1);
107         assertThat(created.get(GROUP)).hasSize(1);
108         assertThat(created.get(GROUP).get(SUBGROUP)).hasSize(3);
109
110         created = pdpStatisticsService.fetchDatabaseStatistics(NUMBER_RECORDS, TIMESTAMP2, TIMESTAMP2);
111         assertThat(created).hasSize(2);
112         assertThat(created.get(GROUP0)).hasSize(1);
113         assertThat(created.get(GROUP0).get(SUBGROUP)).isEqualTo(List.of(pdpStatistics4));
114         assertThat(created.get(GROUP)).hasSize(1);
115         assertThat(created.get(GROUP).get(SUBGROUP)).isEqualTo(List.of(pdpStatistics3));
116
117         created = pdpStatisticsService.fetchDatabaseStatistics(NUMBER_RECORDS, null, TIMESTAMP1);
118         assertThat(created).hasSize(1);
119         assertThat(created.get(GROUP)).hasSize(1);
120         assertThat(created.get(GROUP).get(SUBGROUP)).hasSize(2);
121
122         created = pdpStatisticsService.fetchDatabaseStatistics(NUMBER_RECORDS, TIMESTAMP2, null);
123         assertThat(created).hasSize(2);
124
125         created = pdpStatisticsService.fetchDatabaseStatistics(GROUP0, NUMBER_RECORDS, TIMESTAMP2, TIMESTAMP2);
126         assertThat(created).hasSize(1);
127         assertThat(created.get(GROUP0)).hasSize(1);
128         assertThat(created.get(GROUP0).get(SUBGROUP)).isEqualTo(List.of(pdpStatistics4));
129
130         created = pdpStatisticsService.fetchDatabaseStatistics(GROUP, NUMBER_RECORDS, null, TIMESTAMP1);
131         assertThat(created).hasSize(1);
132         assertThat(created.get(GROUP)).hasSize(1);
133         assertThat(created.get(GROUP).get(SUBGROUP)).hasSize(2);
134
135         created = pdpStatisticsService.fetchDatabaseStatistics(GROUP, NUMBER_RECORDS, TIMESTAMP2, null);
136         assertThat(created).hasSize(1);
137
138         created = pdpStatisticsService.fetchDatabaseStatistics(GROUP, SUBGROUP, NUMBER_RECORDS, TIMESTAMP1, TIMESTAMP2);
139         assertThat(created).hasSize(1);
140         assertThat(created.get(GROUP)).hasSize(1);
141         assertThat(created.get(GROUP).get(SUBGROUP)).hasSize(3);
142
143         created = pdpStatisticsService.fetchDatabaseStatistics(GROUP, SUBGROUP, NUMBER_RECORDS, null, TIMESTAMP1);
144         assertThat(created).hasSize(1);
145         assertThat(created.get(GROUP)).hasSize(1);
146         assertThat(created.get(GROUP).get(SUBGROUP)).hasSize(2);
147
148         created = pdpStatisticsService.fetchDatabaseStatistics(GROUP, SUBGROUP, NUMBER_RECORDS, TIMESTAMP2, null);
149         assertThat(created).hasSize(1);
150         assertThat(created.get(GROUP).get(SUBGROUP)).isEqualTo(List.of(pdpStatistics3));
151
152         created = pdpStatisticsService.fetchDatabaseStatistics(GROUP, SUBGROUP, NAME1, NUMBER_RECORDS, TIMESTAMP1,
153             TIMESTAMP2);
154         assertThat(created).hasSize(1);
155         assertThat(created.get(GROUP)).hasSize(1);
156         assertThat(created.get(GROUP).get(SUBGROUP)).hasSize(2);
157
158         created =
159             pdpStatisticsService.fetchDatabaseStatistics(GROUP, SUBGROUP, NAME1, NUMBER_RECORDS, null, TIMESTAMP1);
160         assertThat(created).hasSize(1);
161         assertThat(created.get(GROUP)).hasSize(1);
162         assertThat(created.get(GROUP).get(SUBGROUP)).hasSize(1);
163
164         created =
165             pdpStatisticsService.fetchDatabaseStatistics(GROUP0, SUBGROUP, NAME3, NUMBER_RECORDS, TIMESTAMP2, null);
166         assertThat(created).hasSize(1);
167         assertThat(created.get(GROUP0).get(SUBGROUP)).isEqualTo(List.of(pdpStatistics4));
168     }
169
170     private PdpStatistics generatePdpStatistics(String pdpInstanceId, Instant date, Long id, String group,
171         String subgroup) {
172         PdpStatistics pdpStatistics11 = new PdpStatistics();
173         pdpStatistics11.setPdpInstanceId(pdpInstanceId);
174         pdpStatistics11.setTimeStamp(date);
175         pdpStatistics11.setGeneratedId(id);
176         pdpStatistics11.setPdpGroupName(group);
177         pdpStatistics11.setPdpSubGroupName(subgroup);
178         pdpStatistics11.setPolicyDeployCount(2);
179         pdpStatistics11.setPolicyDeployFailCount(1);
180         pdpStatistics11.setPolicyDeploySuccessCount(1);
181         pdpStatistics11.setPolicyExecutedCount(2);
182         pdpStatistics11.setPolicyExecutedFailCount(1);
183         pdpStatistics11.setPolicyExecutedSuccessCount(1);
184         pdpStatistics11.setEngineStats(new ArrayList<>());
185
186         return pdpStatistics11;
187     }
188 }