Move PAP database provider to spring boot default
[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.After;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.onap.policy.models.base.Validated;
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
39 public class PdpStatisticsServiceTest extends CommonPapRestServer {
40
41     private static final String NAME3 = "name3";
42     private static final String NAME1 = "name1";
43     private static final String LIST_IS_NULL = "pdpStatisticsList is marked .*ull but is null";
44     private static final String GROUP0 = "group0";
45     private static final String GROUP = "group";
46     private static final String SUBGROUP = "subgroup";
47     private static final Instant TIMESTAMP1 = Instant.ofEpochSecond(1078884319);
48     private static final Instant TIMESTAMP2 = Instant.ofEpochSecond(1078884350);
49     private static final Integer NUMBER_RECORDS = 10;
50
51     @Autowired
52     private PdpStatisticsService pdpStatisticsService;
53
54     @Autowired
55     private PdpStatisticsRepository pdpStatisticsRepository;
56
57     private PdpStatistics pdpStatistics1;
58     private PdpStatistics pdpStatistics2;
59     private PdpStatistics pdpStatistics3;
60     private PdpStatistics pdpStatistics4;
61
62     /**
63      * Setup before tests.
64      *
65      * @throws Exception the exception
66      */
67     @Override
68     @Before
69     public void setUp() throws Exception {
70         super.setUp();
71         pdpStatistics1 = generatePdpStatistics(NAME1, TIMESTAMP1, GROUP, SUBGROUP);
72         pdpStatistics2 = generatePdpStatistics("name2", TIMESTAMP1, GROUP, SUBGROUP);
73         pdpStatistics3 = generatePdpStatistics(NAME1, TIMESTAMP2, GROUP, SUBGROUP);
74         pdpStatistics4 = generatePdpStatistics(NAME3, TIMESTAMP2, GROUP0, SUBGROUP);
75     }
76
77     /**
78      * Teardown after tests.
79      */
80     @Override
81     @After
82     public void tearDown() {
83         pdpStatisticsRepository.deleteAll();
84     }
85
86     @Test
87     public void testCreatePdpStatisticsSuccess() {
88         List<PdpStatistics> createList = List.of(pdpStatistics1, pdpStatistics3, pdpStatistics4, pdpStatistics2);
89         List<PdpStatistics> createdPdpStatisticsList = pdpStatisticsService.createPdpStatistics(createList);
90         // these should match AND be in the same order
91         assertThat(createdPdpStatisticsList).isEqualTo(createList);
92     }
93
94     @Test
95     public void testCreatePdpStatisticsFailure() {
96
97         assertThatThrownBy(() -> {
98             pdpStatisticsService.createPdpStatistics(null);
99         }).hasMessageMatching(LIST_IS_NULL);
100
101         PdpStatistics pdpStatisticsErr = new PdpStatistics();
102         pdpStatisticsErr.setPdpInstanceId("NULL");
103         pdpStatisticsErr.setPdpGroupName(GROUP);
104
105         assertThatThrownBy(() -> {
106             pdpStatisticsService.createPdpStatistics(List.of(pdpStatisticsErr));
107         }).hasMessageContaining("pdp statistics").hasMessageContaining("key")
108             .hasMessageContaining(Validated.IS_A_NULL_KEY);
109     }
110
111     @Test
112     public void testFetchDatabaseStatistics() {
113
114         List<PdpStatistics> createList = List.of(pdpStatistics1, pdpStatistics3, pdpStatistics4, pdpStatistics2);
115         pdpStatisticsService.createPdpStatistics(createList);
116         Map<String, Map<String, List<PdpStatistics>>> created =
117             pdpStatisticsService.fetchDatabaseStatistics(NUMBER_RECORDS, null, null);
118         assertThat(created).hasSize(2);
119         assertThat(created.get(GROUP0)).hasSize(1);
120         assertThat(created.get(GROUP0).get(SUBGROUP)).hasSize(1);
121         assertThat(created.get(GROUP)).hasSize(1);
122         assertThat(created.get(GROUP).get(SUBGROUP)).hasSize(3);
123
124         created = pdpStatisticsService.fetchDatabaseStatistics(NUMBER_RECORDS, TIMESTAMP2, TIMESTAMP2);
125         assertThat(created).hasSize(2);
126         assertThat(created.get(GROUP0)).hasSize(1);
127         assertThat(created.get(GROUP0).get(SUBGROUP)).isEqualTo(List.of(pdpStatistics4));
128         assertThat(created.get(GROUP)).hasSize(1);
129         assertThat(created.get(GROUP).get(SUBGROUP)).isEqualTo(List.of(pdpStatistics3));
130
131         created = pdpStatisticsService.fetchDatabaseStatistics(NUMBER_RECORDS, null, TIMESTAMP1);
132         assertThat(created).hasSize(1);
133         assertThat(created.get(GROUP)).hasSize(1);
134         assertThat(created.get(GROUP).get(SUBGROUP)).hasSize(2);
135
136         created = pdpStatisticsService.fetchDatabaseStatistics(NUMBER_RECORDS, TIMESTAMP2, null);
137         assertThat(created).hasSize(2);
138
139         created = pdpStatisticsService.fetchDatabaseStatistics(GROUP0, NUMBER_RECORDS, TIMESTAMP2, TIMESTAMP2);
140         assertThat(created).hasSize(1);
141         assertThat(created.get(GROUP0)).hasSize(1);
142         assertThat(created.get(GROUP0).get(SUBGROUP)).isEqualTo(List.of(pdpStatistics4));
143
144         created = pdpStatisticsService.fetchDatabaseStatistics(GROUP, NUMBER_RECORDS, null, TIMESTAMP1);
145         assertThat(created).hasSize(1);
146         assertThat(created.get(GROUP)).hasSize(1);
147         assertThat(created.get(GROUP).get(SUBGROUP)).hasSize(2);
148
149         created = pdpStatisticsService.fetchDatabaseStatistics(GROUP, NUMBER_RECORDS, TIMESTAMP2, null);
150         assertThat(created).hasSize(1);
151
152         created = pdpStatisticsService.fetchDatabaseStatistics(GROUP, SUBGROUP, NUMBER_RECORDS, TIMESTAMP1, TIMESTAMP2);
153         assertThat(created).hasSize(1);
154         assertThat(created.get(GROUP)).hasSize(1);
155         assertThat(created.get(GROUP).get(SUBGROUP)).hasSize(3);
156
157         created = pdpStatisticsService.fetchDatabaseStatistics(GROUP, SUBGROUP, NUMBER_RECORDS, null, TIMESTAMP1);
158         assertThat(created).hasSize(1);
159         assertThat(created.get(GROUP)).hasSize(1);
160         assertThat(created.get(GROUP).get(SUBGROUP)).hasSize(2);
161
162         created = pdpStatisticsService.fetchDatabaseStatistics(GROUP, SUBGROUP, NUMBER_RECORDS, TIMESTAMP2, null);
163         assertThat(created).hasSize(1);
164         assertThat(created.get(GROUP).get(SUBGROUP)).isEqualTo(List.of(pdpStatistics3));
165
166         created = pdpStatisticsService.fetchDatabaseStatistics(GROUP, SUBGROUP, NAME1, NUMBER_RECORDS, TIMESTAMP1,
167             TIMESTAMP2);
168         assertThat(created).hasSize(1);
169         assertThat(created.get(GROUP)).hasSize(1);
170         assertThat(created.get(GROUP).get(SUBGROUP)).hasSize(2);
171
172         created =
173             pdpStatisticsService.fetchDatabaseStatistics(GROUP, SUBGROUP, NAME1, NUMBER_RECORDS, null, TIMESTAMP1);
174         assertThat(created).hasSize(1);
175         assertThat(created.get(GROUP)).hasSize(1);
176         assertThat(created.get(GROUP).get(SUBGROUP)).hasSize(1);
177
178         created =
179             pdpStatisticsService.fetchDatabaseStatistics(GROUP0, SUBGROUP, NAME3, NUMBER_RECORDS, TIMESTAMP2, null);
180         assertThat(created).hasSize(1);
181         assertThat(created.get(GROUP0).get(SUBGROUP)).isEqualTo(List.of(pdpStatistics4));
182     }
183
184     private PdpStatistics generatePdpStatistics(String pdpInstanceId, Instant date, String group,
185         String subgroup) {
186         PdpStatistics pdpStatistics11 = new PdpStatistics();
187         pdpStatistics11.setPdpInstanceId(pdpInstanceId);
188         pdpStatistics11.setTimeStamp(date);
189         pdpStatistics11.setPdpGroupName(group);
190         pdpStatistics11.setPdpSubGroupName(subgroup);
191         pdpStatistics11.setPolicyDeployCount(2);
192         pdpStatistics11.setPolicyDeployFailCount(1);
193         pdpStatistics11.setPolicyDeploySuccessCount(1);
194         pdpStatistics11.setPolicyExecutedCount(2);
195         pdpStatistics11.setPolicyExecutedFailCount(1);
196         pdpStatistics11.setPolicyExecutedSuccessCount(1);
197         pdpStatistics11.setEngineStats(new ArrayList<>());
198
199         return pdpStatistics11;
200     }
201 }