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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.pap.main.rest;
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;
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;
38 public class TestPolicyAuditManager extends ProviderSuper {
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";
46 PolicyAuditManager auditManager;
49 * Setup the test variables.
53 public void setUp() throws Exception {
55 auditManager = new PolicyAuditManager(dao);
59 public static void tearDownAfterClass() {
60 Registry.newRegistry();
64 public void testDeployments() {
65 auditManager.addDeploymentAudit(MY_POLICY, GROUP_A, PDP_TYPE, USER);
66 auditManager.addUndeploymentAudit(MY_POLICY, GROUP_B, PDP_TYPE, USER);
68 assertThat(auditManager.getAuditRecords()).hasSize(2);
69 assertEquals(AuditAction.DEPLOYMENT, auditManager.getAuditRecords().get(0).getAction());
70 assertEquals(AuditAction.UNDEPLOYMENT, auditManager.getAuditRecords().get(1).getAction());
72 auditManager.saveRecordsToDb();
74 assertThat(auditManager.getAuditRecords()).isEmpty();
78 public void testSaveRecordsToDb_EmptyList() {
79 assertThat(auditManager.getAuditRecords()).isEmpty();;
80 auditManager.saveRecordsToDb();
82 assertThatCode(() -> auditManager.saveRecordsToDb()).doesNotThrowAnyException();
86 public void testSaveRecordsToDb_Exception() {
87 auditManager.addDeploymentAudit(MY_POLICY, GROUP_A, PDP_TYPE, USER);
89 assertThat(auditManager.getAuditRecords()).hasSize(1);
91 doThrow(PfModelRuntimeException.class).when(dao).createAuditRecords(any());
92 auditManager.saveRecordsToDb();
94 assertThat(auditManager.getAuditRecords()).isNotEmpty();