Java 17 / Spring 6 / Spring Boot 3 Upgrade
[policy/pap.git] / main / src / test / java / org / onap / policy / pap / main / rest / TestPolicyAuditManager.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021, 2023 Nordix Foundation.
4  *  Modifications Copyright (C) 2022 Bell Canada. All rights reserved.
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.rest;
23
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.assertj.core.api.Assertions.assertThatCode;
26 import static org.junit.jupiter.api.Assertions.assertEquals;
27 import static org.mockito.ArgumentMatchers.any;
28 import static org.mockito.Mockito.doThrow;
29
30 import org.junit.jupiter.api.AfterAll;
31 import org.junit.jupiter.api.BeforeEach;
32 import org.junit.jupiter.api.Test;
33 import org.onap.policy.common.utils.services.Registry;
34 import org.onap.policy.models.base.PfModelRuntimeException;
35 import org.onap.policy.models.pap.concepts.PolicyAudit.AuditAction;
36 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
37
38 public class TestPolicyAuditManager extends ProviderSuper {
39
40     private static final ToscaConceptIdentifier MY_POLICY = new ToscaConceptIdentifier("myPolicy", "1.0.0");
41     private static final String GROUP_A = "pdpGroup-A";
42     private static final String GROUP_B = "pdpGroup-B";
43     private static final String PDP_TYPE = "typeABC";
44     private static final String USER = "healthcheck";
45
46     PolicyAuditManager auditManager;
47
48     /**
49      * Setup the test variables.
50      */
51     @Override
52     @BeforeEach
53     public void setUp() throws Exception {
54         super.setUp();
55         auditManager = new PolicyAuditManager(policyAuditService);
56     }
57
58     @AfterAll
59     public static void tearDownAfterClass() {
60         Registry.newRegistry();
61     }
62
63     @Test
64     void testDeployments() {
65         auditManager.addDeploymentAudit(MY_POLICY, GROUP_A, PDP_TYPE, USER);
66         auditManager.addUndeploymentAudit(MY_POLICY, GROUP_B, PDP_TYPE, USER);
67
68         assertThat(auditManager.getAuditRecords()).hasSize(2);
69         assertEquals(AuditAction.DEPLOYMENT, auditManager.getAuditRecords().get(0).getAction());
70         assertEquals(AuditAction.UNDEPLOYMENT, auditManager.getAuditRecords().get(1).getAction());
71
72         auditManager.saveRecordsToDb();
73
74         assertThat(auditManager.getAuditRecords()).isEmpty();
75     }
76
77     @Test
78     void testSaveRecordsToDb_EmptyList() {
79         assertThat(auditManager.getAuditRecords()).isEmpty();
80         ;
81         auditManager.saveRecordsToDb();
82
83         assertThatCode(() -> auditManager.saveRecordsToDb()).doesNotThrowAnyException();
84     }
85
86     @Test
87     void testSaveRecordsToDb_Exception() {
88         auditManager.addDeploymentAudit(MY_POLICY, GROUP_A, PDP_TYPE, USER);
89
90         assertThat(auditManager.getAuditRecords()).hasSize(1);
91
92         doThrow(PfModelRuntimeException.class).when(policyAuditService).createAuditRecords(any());
93         auditManager.saveRecordsToDb();
94
95         assertThat(auditManager.getAuditRecords()).isNotEmpty();
96     }
97 }