2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022 Bell Canada. All rights reserved.
4 * Modifications Copyright (C) 2022-2023 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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.pap.main.service;
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
26 import static org.junit.jupiter.api.Assertions.assertThrows;
28 import java.time.Instant;
29 import java.time.temporal.ChronoUnit;
30 import java.util.List;
31 import org.junit.jupiter.api.AfterEach;
32 import org.junit.jupiter.api.Test;
33 import org.onap.policy.models.base.PfModelRuntimeException;
34 import org.onap.policy.models.pap.concepts.PolicyAudit;
35 import org.onap.policy.models.pap.concepts.PolicyAudit.AuditAction;
36 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
37 import org.onap.policy.pap.main.repository.PolicyAuditRepository;
38 import org.onap.policy.pap.main.rest.CommonPapRestServer;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.test.context.ActiveProfiles;
42 @ActiveProfiles("test")
43 class PolicyAuditServiceTest extends CommonPapRestServer {
45 private static final String FIELD_IS_NULL = "%s is marked .*ull but is null";
46 private static final String GROUP_A = "groupA";
47 private static final String GROUP_B = "groupB";
48 private static final ToscaConceptIdentifier MY_POLICY = new ToscaConceptIdentifier("MyPolicy", "1.2.3");
49 private static final ToscaConceptIdentifier MY_POLICY2 = new ToscaConceptIdentifier("MyPolicyB", "2.3.4");
50 private static final Integer NUMBER_RECORDS = 10;
53 private PolicyAuditService policyAuditService;
56 private PolicyAuditRepository policyAuditRepository;
59 * Teardown after tests.
63 public void tearDown() {
64 policyAuditRepository.deleteAll();
68 void testCreateAuditRecordsSuccess() {
69 policyAuditService.createAuditRecords(generatePolicyAudits(Instant.now(), GROUP_A, MY_POLICY));
71 assertThat(policyAuditService.getAuditRecords(NUMBER_RECORDS, null, null)).hasSize(2);
75 void testCreatePolicyAuditFailure() {
76 List<PolicyAudit> audits = List.of(
77 PolicyAudit.builder().pdpType("pdpType").action(AuditAction.DEPLOYMENT).timestamp(Instant.now()).build());
79 assertThrows(PfModelRuntimeException.class, () -> policyAuditService.createAuditRecords(audits));
80 assertThatThrownBy(() -> policyAuditService.createAuditRecords(audits))
81 .isInstanceOf(PfModelRuntimeException.class)
82 .hasMessageContaining("\"createAuditRecords\" INVALID, item has status INVALID");
84 assertThatThrownBy(() -> policyAuditService.createAuditRecords(null))
85 .hasMessageMatching(String.format(FIELD_IS_NULL, "audits"));
89 void testGetAuditRecords() {
90 Instant startDate1 = Instant.now();
92 policyAuditService.createAuditRecords(generatePolicyAudits(startDate1, GROUP_A, MY_POLICY));
94 assertThat(policyAuditService.getAuditRecords(NUMBER_RECORDS, null, null)).hasSize(2);
96 assertThat(policyAuditService.getAuditRecords(1, null, null)).hasSize(1);
98 // as the start date is 10 min ahead of first record, shouldn't return any records
99 assertThat(policyAuditService.getAuditRecords(NUMBER_RECORDS, Instant.now().plusSeconds(600), null)).isEmpty();
101 policyAuditService.createAuditRecords(generatePolicyAudits(Instant.now(), GROUP_B, MY_POLICY2));
103 assertThat(policyAuditService.getAuditRecords(NUMBER_RECORDS, null, null)).hasSize(4);
104 assertThat(policyAuditService.getAuditRecords(NUMBER_RECORDS, null, null)).hasSize(4);
106 assertThat(policyAuditService.getAuditRecords(GROUP_A, NUMBER_RECORDS, null, null)).hasSize(2);
107 assertThat(policyAuditService.getAuditRecords(GROUP_B, NUMBER_RECORDS, null, null)).hasSize(2);
109 assertThat(policyAuditService.getAuditRecords(GROUP_A, MY_POLICY.getName(), MY_POLICY.getVersion(),
110 NUMBER_RECORDS, null, null)).hasSize(2);
112 policyAuditService.getAuditRecords(GROUP_A, MY_POLICY.getName(), "9.9.9", NUMBER_RECORDS, null, null))
114 assertThat(policyAuditService.getAuditRecords(GROUP_B, MY_POLICY.getName(), MY_POLICY.getVersion(),
115 NUMBER_RECORDS, null, null)).isEmpty();
116 assertThat(policyAuditService.getAuditRecords(GROUP_B, MY_POLICY2.getName(), MY_POLICY2.getVersion(),
117 NUMBER_RECORDS, null, null)).hasSize(2);
118 assertThat(policyAuditService.getAuditRecords(MY_POLICY2.getName(), MY_POLICY2.getVersion(), NUMBER_RECORDS,
119 null, null)).hasSize(2);
121 policyAuditService.getAuditRecords(MY_POLICY.getName(), MY_POLICY.getVersion(), NUMBER_RECORDS, null, null))
126 private List<PolicyAudit> generatePolicyAudits(Instant date, String group, ToscaConceptIdentifier policy) {
127 PolicyAudit deploy = PolicyAudit.builder().pdpGroup(group).pdpType("pdpType").policy(policy)
128 .action(AuditAction.DEPLOYMENT).timestamp(date.truncatedTo(ChronoUnit.SECONDS)).build();
130 PolicyAudit undeploy = PolicyAudit.builder().pdpGroup(group).pdpType("pdpType").policy(policy)
131 .action(AuditAction.UNDEPLOYMENT).timestamp(date.plusSeconds(1).truncatedTo(ChronoUnit.SECONDS)).build();
133 return List.of(deploy, undeploy);