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
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.models.pap.persistence.provider;
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.junit.Assert.assertThrows;
27 import static org.junit.Assert.assertTrue;
29 import java.time.Instant;
30 import java.time.temporal.ChronoUnit;
31 import java.util.ArrayList;
32 import java.util.List;
33 import java.util.Properties;
34 import java.util.concurrent.TimeUnit;
35 import org.awaitility.Awaitility;
36 import org.eclipse.persistence.config.PersistenceUnitProperties;
37 import org.junit.After;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.onap.policy.models.base.PfModelRuntimeException;
41 import org.onap.policy.models.dao.DaoParameters;
42 import org.onap.policy.models.dao.PfDao;
43 import org.onap.policy.models.dao.PfDaoFactory;
44 import org.onap.policy.models.dao.impl.DefaultPfDao;
45 import org.onap.policy.models.pap.concepts.PolicyAudit;
46 import org.onap.policy.models.pap.concepts.PolicyAudit.AuditAction;
47 import org.onap.policy.models.pap.persistence.provider.PolicyAuditProvider.AuditFilter;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
51 * Class for unit testing {@link PolicyAuditProvider}.
53 * @author Adheli Tavares (adheli.tavares@est.tech)
56 public class PolicyAuditProviderTest {
58 private static final String FIELD_IS_NULL = "%s is marked .*ull but is null";
59 private static final String GROUP_A = "groupA";
60 private static final String GROUP_B = "groupB";
61 private static final ToscaConceptIdentifier MY_POLICY = new ToscaConceptIdentifier("MyPolicy", "1.2.3");
62 private static final ToscaConceptIdentifier MY_POLICY2 = new ToscaConceptIdentifier("MyPolicyB", "2.3.4");
63 private static final Integer NUMBER_RECORDS = 10;
68 * Set up the DAO towards the database.
70 * @throws Exception on database errors
73 public void setupDao() throws Exception {
74 final DaoParameters daoParameters = new DaoParameters();
75 daoParameters.setPluginClass(DefaultPfDao.class.getName());
77 daoParameters.setPersistenceUnit("ToscaConceptTest");
79 Properties jdbcProperties = new Properties();
80 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_USER, "policy");
81 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_PASSWORD, "P01icY");
83 if (System.getProperty("USE-MARIADB") != null) {
84 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_DRIVER, "org.mariadb.jdbc.Driver");
85 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_URL, "jdbc:mariadb://localhost:3306/policy");
87 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_DRIVER, "org.h2.Driver");
88 jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_URL, "jdbc:h2:mem:PolicyAuditProviderTest");
91 daoParameters.setJdbcProperties(jdbcProperties);
93 pfDao = new PfDaoFactory().createPfDao(daoParameters);
94 pfDao.init(daoParameters);
98 public void teardown() {
103 public void testCreatePolicyAudit() {
104 PolicyAuditProvider provider = new PolicyAuditProvider();
106 Instant date = Instant.now();
107 provider.createAuditRecords(pfDao, generatePolicyAudits(date, GROUP_A, MY_POLICY));
109 List<PolicyAudit> records = provider.getAuditRecords(pfDao, NUMBER_RECORDS);
110 assertThat(records).hasSize(2);
112 // as the start date is 10 min ahead of first record, shouldn't return any records
113 List<PolicyAudit> emptyList = provider.getAuditRecords(pfDao,
114 AuditFilter.builder().fromDate(Instant.now().plusSeconds(600)).build(), 600);
115 assertThat(emptyList).isEmpty();
119 public void testCreatePolicyAuditInvalid() {
120 PolicyAuditProvider provider = new PolicyAuditProvider();
122 List<PolicyAudit> audits = List.of(PolicyAudit.builder().pdpType("pdpType").action(AuditAction.DEPLOYMENT)
123 .timestamp(Instant.now()).build());
125 assertThrows(PfModelRuntimeException.class, () -> provider.createAuditRecords(pfDao, audits));
127 List<PolicyAudit> records = provider.getAuditRecords(pfDao, NUMBER_RECORDS);
128 assertThat(records).isEmpty();
132 public void testFilters() {
133 PolicyAuditProvider provider = new PolicyAuditProvider();
135 Instant date = Instant.now().truncatedTo(ChronoUnit.SECONDS);
136 System.out.println(date);
137 provider.createAuditRecords(pfDao, generatePolicyAudits(date, GROUP_A, MY_POLICY));
138 provider.createAuditRecords(pfDao, generatePolicyAudits(date, GROUP_B, MY_POLICY));
139 provider.createAuditRecords(pfDao, generatePolicyAudits(date, GROUP_B, MY_POLICY2));
140 Awaitility.await().pollDelay(3, TimeUnit.SECONDS).until(() -> {
144 List<PolicyAudit> records = provider.getAuditRecords(pfDao,
145 AuditFilter.builder().fromDate(date).toDate(Instant.now()).build(), NUMBER_RECORDS);
146 assertThat(records).hasSize(6);
148 List<PolicyAudit> recordsWithGroupB =
149 provider.getAuditRecords(pfDao, AuditFilter.builder().pdpGroup(GROUP_B).build(), NUMBER_RECORDS);
150 assertThat(recordsWithGroupB).hasSize(4);
152 List<PolicyAudit> recordsWithActionDeploy = provider.getAuditRecords(pfDao,
153 AuditFilter.builder().action(AuditAction.DEPLOYMENT).build(), NUMBER_RECORDS);
154 assertThat(recordsWithActionDeploy).hasSize(3);
156 List<PolicyAudit> recordsWithMyPolicy = provider.getAuditRecords(pfDao,
157 AuditFilter.builder().name(MY_POLICY.getName()).version(MY_POLICY.getVersion()).build(),
159 assertThat(recordsWithMyPolicy).hasSize(4);
163 public void testLoadRecordsForLimit() {
164 PolicyAuditProvider provider = new PolicyAuditProvider();
166 List<PolicyAudit> loadAudits = new ArrayList<>();
168 // going to create 102 records.
169 for (int i = 0; i <= 50; i++) {
170 loadAudits.addAll(generatePolicyAudits(Instant.now().plusSeconds(i), GROUP_A, MY_POLICY));
173 provider.createAuditRecords(pfDao, loadAudits);
175 List<PolicyAudit> records = provider.getAuditRecords(pfDao, NUMBER_RECORDS);
176 assertThat(records).hasSize(10);
178 // check that is being ordered
179 assertTrue(records.get(0).getTimestamp().isAfter(records.get(9).getTimestamp()));
180 assertEquals(loadAudits.get(loadAudits.size() - 1).getTimestamp(), records.get(0).getTimestamp());
182 // try to get 102 records should return 100
183 records = provider.getAuditRecords(pfDao, 102);
184 assertThat(records).hasSize(100);
188 public void policyProviderExceptions() {
189 PolicyAuditProvider provider = new PolicyAuditProvider();
191 assertThatThrownBy(() -> {
192 provider.createAuditRecords(null, null);
193 }).hasMessageMatching(String.format(FIELD_IS_NULL, "dao"));
195 assertThatThrownBy(() -> {
196 provider.createAuditRecords(pfDao, null);
197 }).hasMessageMatching(String.format(FIELD_IS_NULL, "audits"));
199 assertThatThrownBy(() -> {
200 provider.getAuditRecords(null, NUMBER_RECORDS);
201 }).hasMessageMatching(String.format(FIELD_IS_NULL, "dao"));
203 assertThatThrownBy(() -> {
204 provider.getAuditRecords(pfDao, null);
205 }).hasMessageMatching(String.format(FIELD_IS_NULL, "numRecords"));
207 assertThatThrownBy(() -> {
208 provider.getAuditRecords(null, AuditFilter.builder().build(), NUMBER_RECORDS);
209 }).hasMessageMatching(String.format(FIELD_IS_NULL, "dao"));
211 assertThatThrownBy(() -> {
212 provider.getAuditRecords(pfDao, AuditFilter.builder().build(), null);
213 }).hasMessageMatching(String.format(FIELD_IS_NULL, "numRecords"));
215 assertThatThrownBy(() -> {
216 provider.getAuditRecords(pfDao, null, NUMBER_RECORDS);
217 }).hasMessageMatching(String.format(FIELD_IS_NULL, "auditFilter"));
220 private List<PolicyAudit> generatePolicyAudits(Instant date, String group, ToscaConceptIdentifier policy) {
222 PolicyAudit deploy = PolicyAudit.builder()
226 .action(AuditAction.DEPLOYMENT)
227 .timestamp(date.truncatedTo(ChronoUnit.SECONDS))
230 PolicyAudit undeploy = PolicyAudit.builder()
234 .action(AuditAction.UNDEPLOYMENT)
235 .timestamp(date.plusSeconds(1).truncatedTo(ChronoUnit.SECONDS))
239 return List.of(deploy, undeploy);