Java 17 / Spring 6 / Spring Boot 3 Upgrade
[policy/pap.git] / main / src / test / java / org / onap / policy / pap / main / service / PolicyAuditServiceTest.java
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.pap.main.service;
23
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;
27
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;
41
42 @ActiveProfiles("test")
43 class PolicyAuditServiceTest extends CommonPapRestServer {
44
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;
51
52     @Autowired
53     private PolicyAuditService policyAuditService;
54
55     @Autowired
56     private PolicyAuditRepository policyAuditRepository;
57
58     /**
59      * Teardown after tests.
60      */
61     @Override
62     @AfterEach
63     public void tearDown() {
64         policyAuditRepository.deleteAll();
65     }
66
67     @Test
68     void testCreateAuditRecordsSuccess() {
69         policyAuditService.createAuditRecords(generatePolicyAudits(Instant.now(), GROUP_A, MY_POLICY));
70
71         assertThat(policyAuditService.getAuditRecords(NUMBER_RECORDS, null, null)).hasSize(2);
72     }
73
74     @Test
75     void testCreatePolicyAuditFailure() {
76         List<PolicyAudit> audits = List.of(
77             PolicyAudit.builder().pdpType("pdpType").action(AuditAction.DEPLOYMENT).timestamp(Instant.now()).build());
78
79         assertThrows(PfModelRuntimeException.class, () -> policyAuditService.createAuditRecords(audits));
80         assertThatThrownBy(() -> policyAuditService.createAuditRecords(audits))
81             .isInstanceOf(PfModelRuntimeException.class)
82             .hasMessageContaining("\"createAuditRecords\" INVALID, item has status INVALID");
83
84         assertThatThrownBy(() -> policyAuditService.createAuditRecords(null))
85             .hasMessageMatching(String.format(FIELD_IS_NULL, "audits"));
86     }
87
88     @Test
89     void testGetAuditRecords() {
90         Instant startDate1 = Instant.now();
91
92         policyAuditService.createAuditRecords(generatePolicyAudits(startDate1, GROUP_A, MY_POLICY));
93
94         assertThat(policyAuditService.getAuditRecords(NUMBER_RECORDS, null, null)).hasSize(2);
95
96         assertThat(policyAuditService.getAuditRecords(1, null, null)).hasSize(1);
97
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();
100
101         policyAuditService.createAuditRecords(generatePolicyAudits(Instant.now(), GROUP_B, MY_POLICY2));
102
103         assertThat(policyAuditService.getAuditRecords(NUMBER_RECORDS, null, null)).hasSize(4);
104         assertThat(policyAuditService.getAuditRecords(NUMBER_RECORDS, null, null)).hasSize(4);
105
106         assertThat(policyAuditService.getAuditRecords(GROUP_A, NUMBER_RECORDS, null, null)).hasSize(2);
107         assertThat(policyAuditService.getAuditRecords(GROUP_B, NUMBER_RECORDS, null, null)).hasSize(2);
108
109         assertThat(policyAuditService.getAuditRecords(GROUP_A, MY_POLICY.getName(), MY_POLICY.getVersion(),
110             NUMBER_RECORDS, null, null)).hasSize(2);
111         assertThat(
112             policyAuditService.getAuditRecords(GROUP_A, MY_POLICY.getName(), "9.9.9", NUMBER_RECORDS, null, null))
113                 .isEmpty();
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);
120         assertThat(
121             policyAuditService.getAuditRecords(MY_POLICY.getName(), MY_POLICY.getVersion(), NUMBER_RECORDS, null, null))
122                 .hasSize(2);
123
124     }
125
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();
129
130         PolicyAudit undeploy = PolicyAudit.builder().pdpGroup(group).pdpType("pdpType").policy(policy)
131             .action(AuditAction.UNDEPLOYMENT).timestamp(date.plusSeconds(1).truncatedTo(ChronoUnit.SECONDS)).build();
132
133         return List.of(deploy, undeploy);
134     }
135 }