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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.pap.main.service;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
26 import java.time.Instant;
27 import java.util.ArrayList;
28 import java.util.List;
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;
39 public class PdpStatisticsServiceTest extends CommonPapRestServer {
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;
52 private PdpStatisticsService pdpStatisticsService;
55 private PdpStatisticsRepository pdpStatisticsRepository;
57 private PdpStatistics pdpStatistics1;
58 private PdpStatistics pdpStatistics2;
59 private PdpStatistics pdpStatistics3;
60 private PdpStatistics pdpStatistics4;
65 * @throws Exception the exception
69 public void setUp() throws Exception {
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);
78 * Teardown after tests.
82 public void tearDown() {
83 pdpStatisticsRepository.deleteAll();
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);
95 public void testCreatePdpStatisticsFailure() {
97 assertThatThrownBy(() -> {
98 pdpStatisticsService.createPdpStatistics(null);
99 }).hasMessageMatching(LIST_IS_NULL);
101 PdpStatistics pdpStatisticsErr = new PdpStatistics();
102 pdpStatisticsErr.setPdpInstanceId("NULL");
103 pdpStatisticsErr.setPdpGroupName(GROUP);
105 assertThatThrownBy(() -> {
106 pdpStatisticsService.createPdpStatistics(List.of(pdpStatisticsErr));
107 }).hasMessageContaining("pdp statistics").hasMessageContaining("key")
108 .hasMessageContaining(Validated.IS_A_NULL_KEY);
112 public void testFetchDatabaseStatistics() {
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);
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));
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);
136 created = pdpStatisticsService.fetchDatabaseStatistics(NUMBER_RECORDS, TIMESTAMP2, null);
137 assertThat(created).hasSize(2);
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));
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);
149 created = pdpStatisticsService.fetchDatabaseStatistics(GROUP, NUMBER_RECORDS, TIMESTAMP2, null);
150 assertThat(created).hasSize(1);
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);
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);
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));
166 created = pdpStatisticsService.fetchDatabaseStatistics(GROUP, SUBGROUP, NAME1, NUMBER_RECORDS, TIMESTAMP1,
168 assertThat(created).hasSize(1);
169 assertThat(created.get(GROUP)).hasSize(1);
170 assertThat(created.get(GROUP).get(SUBGROUP)).hasSize(2);
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);
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));
184 private PdpStatistics generatePdpStatistics(String pdpInstanceId, Instant date, String group,
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<>());
199 return pdpStatistics11;