Set all cross references of policy/pap
[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  *  Modifications Copyright (C) 2022 Nordix Foundation.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.pap.main.service;
23
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
26
27 import java.time.Instant;
28 import java.util.ArrayList;
29 import java.util.List;
30 import java.util.Map;
31 import org.junit.After;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.onap.policy.models.pdp.concepts.PdpStatistics;
35 import org.onap.policy.pap.main.repository.PdpStatisticsRepository;
36 import org.onap.policy.pap.main.rest.CommonPapRestServer;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.test.context.ActiveProfiles;
39
40 @ActiveProfiles("test")
41 public class PdpStatisticsServiceTest extends CommonPapRestServer {
42
43     private static final String NAME3 = "name3";
44     private static final String NAME1 = "name1";
45     private static final String LIST_IS_NULL = "pdpStatisticsList is marked .*ull but is null";
46     private static final String GROUP0 = "group0";
47     private static final String GROUP = "group";
48     private static final String SUBGROUP = "subgroup";
49     private static final Instant TIMESTAMP1 = Instant.ofEpochSecond(1078884319);
50     private static final Instant TIMESTAMP2 = Instant.ofEpochSecond(1078884350);
51     private static final Integer NUMBER_RECORDS = 10;
52
53     @Autowired
54     private PdpStatisticsService pdpStatisticsService;
55
56     @Autowired
57     private PdpStatisticsRepository pdpStatisticsRepository;
58
59     private PdpStatistics pdpStatistics1;
60     private PdpStatistics pdpStatistics2;
61     private PdpStatistics pdpStatistics3;
62     private PdpStatistics pdpStatistics4;
63
64     /**
65      * Setup before tests.
66      *
67      * @throws Exception the exception
68      */
69     @Override
70     @Before
71     public void setUp() throws Exception {
72         super.setUp();
73         pdpStatistics1 = generatePdpStatistics(NAME1, TIMESTAMP1, GROUP);
74         pdpStatistics2 = generatePdpStatistics("name2", TIMESTAMP1, GROUP);
75         pdpStatistics3 = generatePdpStatistics(NAME1, TIMESTAMP2, GROUP);
76         pdpStatistics4 = generatePdpStatistics(NAME3, TIMESTAMP2, GROUP0);
77     }
78
79     /**
80      * Teardown after tests.
81      */
82     @Override
83     @After
84     public void tearDown() {
85         pdpStatisticsRepository.deleteAll();
86     }
87
88     @Test
89     public void testCreatePdpStatisticsSuccess() {
90         List<PdpStatistics> createList = List.of(pdpStatistics1, pdpStatistics3, pdpStatistics4, pdpStatistics2);
91         List<PdpStatistics> createdPdpStatisticsList = pdpStatisticsService.createPdpStatistics(createList);
92         // these should match AND be in the same order
93         assertThat(createdPdpStatisticsList).isEqualTo(createList);
94     }
95
96     @Test
97     public void testCreatePdpStatisticsFailure() {
98
99         assertThatThrownBy(() -> pdpStatisticsService.createPdpStatistics(null)).hasMessageMatching(LIST_IS_NULL);
100
101         PdpStatistics pdpStatisticsErr = new PdpStatistics();
102         pdpStatisticsErr.setPdpInstanceId("NULL");
103         pdpStatisticsErr.setPdpGroupName(GROUP);
104         assertThatThrownBy(() -> pdpStatisticsService.createPdpStatistics(List.of(pdpStatisticsErr)))
105             .hasMessageContaining("item \"name\" value \"NULL\" INVALID, is null");
106     }
107
108     @Test
109     public void testFetchDatabaseStatistics() {
110         List<PdpStatistics> createList = List.of(pdpStatistics1, pdpStatistics3, pdpStatistics4, pdpStatistics2);
111         pdpStatisticsService.createPdpStatistics(createList);
112
113         Map<String, Map<String, List<PdpStatistics>>> statistics;
114
115         statistics = pdpStatisticsService.fetchDatabaseStatistics(NUMBER_RECORDS, null, null);
116         assertGroupAndSubgroupSize(statistics, 2, GROUP0, 1);
117         assertGroupAndSubgroupSize(statistics, 2, GROUP, 3);
118
119         statistics = pdpStatisticsService.fetchDatabaseStatistics(NUMBER_RECORDS, TIMESTAMP2, TIMESTAMP2);
120         assertGroupAndSubgroupSize(statistics, 2, GROUP0, 1);
121         assertThat(statistics.get(GROUP0)).containsEntry(SUBGROUP, List.of(pdpStatistics4));
122         assertGroupAndSubgroupSize(statistics, 2, GROUP, 1);
123         assertThat(statistics.get(GROUP)).containsEntry(SUBGROUP, List.of(pdpStatistics3));
124
125         statistics = pdpStatisticsService.fetchDatabaseStatistics(NUMBER_RECORDS, null, TIMESTAMP1);
126         assertGroupAndSubgroupSize(statistics, 1, GROUP, 2);
127
128         statistics = pdpStatisticsService.fetchDatabaseStatistics(NUMBER_RECORDS, TIMESTAMP2, null);
129         assertThat(statistics).hasSize(2);
130
131         statistics = pdpStatisticsService.fetchDatabaseStatistics(GROUP0, NUMBER_RECORDS, TIMESTAMP2, TIMESTAMP2);
132         assertThat(statistics).hasSize(1);
133         assertThat(statistics.get(GROUP0)).hasSize(1);
134         assertThat(statistics.get(GROUP0)).containsEntry(SUBGROUP, List.of(pdpStatistics4));
135
136         statistics = pdpStatisticsService.fetchDatabaseStatistics(GROUP, NUMBER_RECORDS, null, TIMESTAMP1);
137         assertGroupAndSubgroupSize(statistics, 1, GROUP, 2);
138
139         statistics = pdpStatisticsService.fetchDatabaseStatistics(GROUP, NUMBER_RECORDS, TIMESTAMP2, null);
140         assertThat(statistics).hasSize(1);
141
142         statistics = pdpStatisticsService.fetchDatabaseStatistics(GROUP, SUBGROUP, NUMBER_RECORDS, TIMESTAMP1,
143             TIMESTAMP2);
144         assertGroupAndSubgroupSize(statistics, 1, GROUP, 3);
145
146         statistics = pdpStatisticsService.fetchDatabaseStatistics(GROUP, SUBGROUP, NUMBER_RECORDS, null, TIMESTAMP1);
147         assertGroupAndSubgroupSize(statistics, 1, GROUP, 2);
148
149         statistics = pdpStatisticsService.fetchDatabaseStatistics(GROUP, SUBGROUP, NUMBER_RECORDS, TIMESTAMP2, null);
150         assertThat(statistics).hasSize(1);
151         assertThat(statistics.get(GROUP)).containsEntry(SUBGROUP, List.of(pdpStatistics3));
152
153         statistics = pdpStatisticsService.fetchDatabaseStatistics(GROUP, SUBGROUP, NAME1, NUMBER_RECORDS, TIMESTAMP1,
154             TIMESTAMP2);
155         assertGroupAndSubgroupSize(statistics, 1, GROUP, 2);
156
157         statistics =
158             pdpStatisticsService.fetchDatabaseStatistics(GROUP, SUBGROUP, NAME1, NUMBER_RECORDS, null, TIMESTAMP1);
159         assertGroupAndSubgroupSize(statistics, 1, GROUP, 1);
160
161         statistics =
162             pdpStatisticsService.fetchDatabaseStatistics(GROUP0, SUBGROUP, NAME3, NUMBER_RECORDS, TIMESTAMP2, null);
163         assertThat(statistics).hasSize(1);
164         assertThat(statistics.get(GROUP0)).containsEntry(SUBGROUP, List.of(pdpStatistics4));
165     }
166
167     /**
168      * Asserts if statistics list is the expected size and the subgroup list is also the expected size.
169      */
170     private void assertGroupAndSubgroupSize(Map<String, Map<String, List<PdpStatistics>>> statistics, int listSize,
171                                             String group, int subGroupSize) {
172         assertThat(statistics).hasSize(listSize);
173         assertThat(statistics.get(group)).hasSize(1);
174         assertThat(statistics.get(group).get(SUBGROUP)).hasSize(subGroupSize);
175     }
176
177     private PdpStatistics generatePdpStatistics(String pdpInstanceId, Instant date, String group) {
178         PdpStatistics pdpStatistics11 = new PdpStatistics();
179         pdpStatistics11.setPdpInstanceId(pdpInstanceId);
180         pdpStatistics11.setTimeStamp(date);
181         pdpStatistics11.setPdpGroupName(group);
182         pdpStatistics11.setPdpSubGroupName(PdpStatisticsServiceTest.SUBGROUP);
183         pdpStatistics11.setPolicyDeployCount(2);
184         pdpStatistics11.setPolicyDeployFailCount(1);
185         pdpStatistics11.setPolicyDeploySuccessCount(1);
186         pdpStatistics11.setPolicyExecutedCount(2);
187         pdpStatistics11.setPolicyExecutedFailCount(1);
188         pdpStatistics11.setPolicyExecutedSuccessCount(1);
189         pdpStatistics11.setEngineStats(new ArrayList<>());
190
191         return pdpStatistics11;
192     }
193 }