2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.pap.main.rest;
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.assertj.core.api.Assertions.assertThatCode;
26 import static org.junit.Assert.assertEquals;
27 import static org.mockito.ArgumentMatchers.any;
28 import static org.mockito.Mockito.doThrow;
30 import org.junit.AfterClass;
31 import org.junit.Before;
32 import org.junit.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;
39 public class TestPolicyAuditManager extends ProviderSuper {
41 private static final ToscaConceptIdentifier MY_POLICY = new ToscaConceptIdentifier("myPolicy", "1.0.0");
42 private static final String GROUP_A = "pdpGroup-A";
43 private static final String GROUP_B = "pdpGroup-B";
44 private static final String PDP_TYPE = "typeABC";
45 private static final String USER = "healthcheck";
47 PolicyAuditManager auditManager;
50 * Setup the test variables.
54 public void setUp() throws Exception {
56 auditManager = new PolicyAuditManager(policyAuditService);
60 public static void tearDownAfterClass() {
61 Registry.newRegistry();
65 public void testDeployments() {
66 auditManager.addDeploymentAudit(MY_POLICY, GROUP_A, PDP_TYPE, USER);
67 auditManager.addUndeploymentAudit(MY_POLICY, GROUP_B, PDP_TYPE, USER);
69 assertThat(auditManager.getAuditRecords()).hasSize(2);
70 assertEquals(AuditAction.DEPLOYMENT, auditManager.getAuditRecords().get(0).getAction());
71 assertEquals(AuditAction.UNDEPLOYMENT, auditManager.getAuditRecords().get(1).getAction());
73 auditManager.saveRecordsToDb();
75 assertThat(auditManager.getAuditRecords()).isEmpty();
79 public void testSaveRecordsToDb_EmptyList() {
80 assertThat(auditManager.getAuditRecords()).isEmpty();;
81 auditManager.saveRecordsToDb();
83 assertThatCode(() -> auditManager.saveRecordsToDb()).doesNotThrowAnyException();
87 public void testSaveRecordsToDb_Exception() {
88 auditManager.addDeploymentAudit(MY_POLICY, GROUP_A, PDP_TYPE, USER);
90 assertThat(auditManager.getAuditRecords()).hasSize(1);
92 doThrow(PfModelRuntimeException.class).when(policyAuditService).createAuditRecords(any());
93 auditManager.saveRecordsToDb();
95 assertThat(auditManager.getAuditRecords()).isNotEmpty();