Move PAP database provider to spring boot default
[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  * ================================================================================
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.pap.main.service;
22
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;
26
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;
39
40 public class PolicyAuditServiceTest extends CommonPapRestServer {
41
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;
48
49     @Autowired
50     private PolicyAuditService policyAuditService;
51
52     @Autowired
53     private PolicyAuditRepository policyAuditRepository;
54
55     /**
56      * Teardown after tests.
57      */
58     @Override
59     @After
60     public void tearDown() {
61         policyAuditRepository.deleteAll();
62     }
63
64     @Test
65     public void testCreateAuditRecordsSuccess() {
66         policyAuditService.createAuditRecords(generatePolicyAudits(Instant.now(), GROUP_A, MY_POLICY));
67
68         assertThat(policyAuditService.getAuditRecords(NUMBER_RECORDS, null, null)).hasSize(2);
69     }
70
71     @Test
72     public void testCreatePolicyAuditFailure() {
73         List<PolicyAudit> audits = List.of(
74             PolicyAudit.builder().pdpType("pdpType").action(AuditAction.DEPLOYMENT).timestamp(Instant.now()).build());
75
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");
81
82         assertThatThrownBy(() -> {
83             policyAuditService.createAuditRecords(null);
84         }).hasMessageMatching(String.format(FIELD_IS_NULL, "audits"));
85     }
86
87     @Test
88     public void testGetAuditRecords() {
89         Instant startDate1 = Instant.now();
90
91         policyAuditService.createAuditRecords(generatePolicyAudits(startDate1, GROUP_A, MY_POLICY));
92
93         assertThat(policyAuditService.getAuditRecords(NUMBER_RECORDS, null, null)).hasSize(2);
94
95         assertThat(policyAuditService.getAuditRecords(1, null, null)).hasSize(1);
96
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();
99
100         policyAuditService.createAuditRecords(generatePolicyAudits(Instant.now(), GROUP_B, MY_POLICY2));
101
102         assertThat(policyAuditService.getAuditRecords(NUMBER_RECORDS, null, null)).hasSize(4);
103         assertThat(policyAuditService.getAuditRecords(NUMBER_RECORDS, null, null)).hasSize(4);
104
105         assertThat(policyAuditService.getAuditRecords(GROUP_A, NUMBER_RECORDS, null, null)).hasSize(2);
106         assertThat(policyAuditService.getAuditRecords(GROUP_B, NUMBER_RECORDS, null, null)).hasSize(2);
107
108         assertThat(policyAuditService.getAuditRecords(GROUP_A, MY_POLICY.getName(), MY_POLICY.getVersion(),
109             NUMBER_RECORDS, null, null)).hasSize(2);
110         assertThat(
111             policyAuditService.getAuditRecords(GROUP_A, MY_POLICY.getName(), "9.9.9", NUMBER_RECORDS, null, null))
112                 .hasSize(0);
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);
119         assertThat(
120             policyAuditService.getAuditRecords(MY_POLICY.getName(), MY_POLICY.getVersion(), NUMBER_RECORDS, null, null))
121                 .hasSize(2);
122
123     }
124
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();
128
129         PolicyAudit undeploy = PolicyAudit.builder().pdpGroup(group).pdpType("pdpType").policy(policy)
130             .action(AuditAction.UNDEPLOYMENT).timestamp(date.plusSeconds(1).truncatedTo(ChronoUnit.SECONDS)).build();
131
132         return List.of(deploy, undeploy);
133     }
134 }