Merge "PolicyAudit creation when deploy/undeploy triggered."
[policy/pap.git] / main / src / test / java / org / onap / policy / pap / main / rest / TestPolicyAuditManager.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Nordix Foundation.
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.rest;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.assertj.core.api.Assertions.assertThatCode;
25 import static org.junit.Assert.assertEquals;
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.Mockito.doThrow;
28
29 import org.junit.AfterClass;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.onap.policy.common.utils.services.Registry;
33 import org.onap.policy.models.base.PfModelRuntimeException;
34 import org.onap.policy.models.pap.concepts.PolicyAudit.AuditAction;
35 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
36
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     @Before
53     public void setUp() throws Exception {
54         super.setUp();
55         auditManager = new PolicyAuditManager(dao);
56     }
57
58     @AfterClass
59     public static void tearDownAfterClass() {
60         Registry.newRegistry();
61     }
62
63     @Test
64     public 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     public void testSaveRecordsToDb_EmptyList() {
79         assertThat(auditManager.getAuditRecords()).isEmpty();;
80         auditManager.saveRecordsToDb();
81
82         assertThatCode(() -> auditManager.saveRecordsToDb()).doesNotThrowAnyException();
83     }
84
85     @Test
86     public void testSaveRecordsToDb_Exception() {
87         auditManager.addDeploymentAudit(MY_POLICY, GROUP_A, PDP_TYPE, USER);
88
89         assertThat(auditManager.getAuditRecords()).hasSize(1);
90
91         doThrow(PfModelRuntimeException.class).when(dao).createAuditRecords(any());
92         auditManager.saveRecordsToDb();
93
94         assertThat(auditManager.getAuditRecords()).isNotEmpty();
95     }
96 }