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;
25 import static org.junit.Assert.assertThrows;
27 import java.time.Instant;
28 import java.time.temporal.ChronoUnit;
29 import java.util.List;
30 import org.junit.After;
31 import org.junit.Test;
32 import org.onap.policy.models.base.PfModelRuntimeException;
33 import org.onap.policy.models.pap.concepts.PolicyAudit;
34 import org.onap.policy.models.pap.concepts.PolicyAudit.AuditAction;
35 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
36 import org.onap.policy.pap.main.repository.PolicyAuditRepository;
37 import org.onap.policy.pap.main.rest.CommonPapRestServer;
38 import org.springframework.beans.factory.annotation.Autowired;
40 public class PolicyAuditServiceTest extends CommonPapRestServer {
42 private static final String FIELD_IS_NULL = "%s is marked .*ull but is null";
43 private static final String GROUP_A = "groupA";
44 private static final String GROUP_B = "groupB";
45 private static final ToscaConceptIdentifier MY_POLICY = new ToscaConceptIdentifier("MyPolicy", "1.2.3");
46 private static final ToscaConceptIdentifier MY_POLICY2 = new ToscaConceptIdentifier("MyPolicyB", "2.3.4");
47 private static final Integer NUMBER_RECORDS = 10;
50 private PolicyAuditService policyAuditService;
53 private PolicyAuditRepository policyAuditRepository;
56 * Teardown after tests.
60 public void tearDown() {
61 policyAuditRepository.deleteAll();
65 public void testCreateAuditRecordsSuccess() {
66 policyAuditService.createAuditRecords(generatePolicyAudits(Instant.now(), GROUP_A, MY_POLICY));
68 assertThat(policyAuditService.getAuditRecords(NUMBER_RECORDS, null, null)).hasSize(2);
72 public void testCreatePolicyAuditFailure() {
73 List<PolicyAudit> audits = List.of(
74 PolicyAudit.builder().pdpType("pdpType").action(AuditAction.DEPLOYMENT).timestamp(Instant.now()).build());
76 assertThrows(PfModelRuntimeException.class, () -> policyAuditService.createAuditRecords(audits));
77 assertThatThrownBy(() -> {
78 policyAuditService.createAuditRecords(audits);
79 }).isInstanceOf(PfModelRuntimeException.class)
80 .hasMessageContaining("\"createAuditRecords\" INVALID, item has status INVALID");
82 assertThatThrownBy(() -> {
83 policyAuditService.createAuditRecords(null);
84 }).hasMessageMatching(String.format(FIELD_IS_NULL, "audits"));
88 public void testGetAuditRecords() {
89 Instant startDate1 = Instant.now();
91 policyAuditService.createAuditRecords(generatePolicyAudits(startDate1, GROUP_A, MY_POLICY));
93 assertThat(policyAuditService.getAuditRecords(NUMBER_RECORDS, null, null)).hasSize(2);
95 assertThat(policyAuditService.getAuditRecords(1, null, null)).hasSize(1);
97 // as the start date is 10 min ahead of first record, shouldn't return any records
98 assertThat(policyAuditService.getAuditRecords(NUMBER_RECORDS, Instant.now().plusSeconds(600), null)).isEmpty();
100 policyAuditService.createAuditRecords(generatePolicyAudits(Instant.now(), GROUP_B, MY_POLICY2));
102 assertThat(policyAuditService.getAuditRecords(NUMBER_RECORDS, null, null)).hasSize(4);
103 assertThat(policyAuditService.getAuditRecords(NUMBER_RECORDS, null, null)).hasSize(4);
105 assertThat(policyAuditService.getAuditRecords(GROUP_A, NUMBER_RECORDS, null, null)).hasSize(2);
106 assertThat(policyAuditService.getAuditRecords(GROUP_B, NUMBER_RECORDS, null, null)).hasSize(2);
108 assertThat(policyAuditService.getAuditRecords(GROUP_A, MY_POLICY.getName(), MY_POLICY.getVersion(),
109 NUMBER_RECORDS, null, null)).hasSize(2);
111 policyAuditService.getAuditRecords(GROUP_A, MY_POLICY.getName(), "9.9.9", NUMBER_RECORDS, null, null))
113 assertThat(policyAuditService.getAuditRecords(GROUP_B, MY_POLICY.getName(), MY_POLICY.getVersion(),
114 NUMBER_RECORDS, null, null)).hasSize(0);
115 assertThat(policyAuditService.getAuditRecords(GROUP_B, MY_POLICY2.getName(), MY_POLICY2.getVersion(),
116 NUMBER_RECORDS, null, null)).hasSize(2);
117 assertThat(policyAuditService.getAuditRecords(MY_POLICY2.getName(), MY_POLICY2.getVersion(), NUMBER_RECORDS,
118 null, null)).hasSize(2);
120 policyAuditService.getAuditRecords(MY_POLICY.getName(), MY_POLICY.getVersion(), NUMBER_RECORDS, null, null))
125 private List<PolicyAudit> generatePolicyAudits(Instant date, String group, ToscaConceptIdentifier policy) {
126 PolicyAudit deploy = PolicyAudit.builder().pdpGroup(group).pdpType("pdpType").policy(policy)
127 .action(AuditAction.DEPLOYMENT).timestamp(date.truncatedTo(ChronoUnit.SECONDS)).build();
129 PolicyAudit undeploy = PolicyAudit.builder().pdpGroup(group).pdpType("pdpType").policy(policy)
130 .action(AuditAction.UNDEPLOYMENT).timestamp(date.plusSeconds(1).truncatedTo(ChronoUnit.SECONDS)).build();
132 return List.of(deploy, undeploy);