AuthenticationModuleEventAuditorTest.java revision c1218d78f656be3cbe77704bc80bb83b82fdc277
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsync/*
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsync * The contents of this file are subject to the terms of the Common Development and
0d39579b024b5bdc9ec28616c9da099866766cdcvboxsync * Distribution License (the License). You may not use this file except in compliance with the
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsync * License.
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsync *
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsync * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsync * specific language governing permission and limitations under the License.
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsync *
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsync * When distributing Covered Software, include this CDDL Header Notice in each file and include
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsync * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsync * Header, with the fields enclosed by brackets [] replaced by your own identifying
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsync * information: "Portions copyright [year] [name of copyright owner]".
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsync *
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsync * Copyright 2015 ForgeRock AS.
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsync */
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsyncpackage com.sun.identity.authentication.audit;
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsync
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsyncimport static org.mockito.Mockito.*;
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsyncimport org.forgerock.openam.audit.AuditConstants.*;
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsync
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsyncimport com.sun.identity.authentication.service.LoginState;
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsyncimport org.forgerock.audit.events.AuditEvent;
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsyncimport org.forgerock.openam.audit.AuditEventFactory;
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsyncimport org.forgerock.openam.audit.AuditEventPublisher;
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsyncimport org.forgerock.openam.audit.model.AuthenticationAuditEntry;
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsyncimport org.mockito.Mock;
4d1226da58f42754f7b947e5ab65fb391de20946vboxsyncimport org.mockito.MockitoAnnotations;
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsyncimport org.testng.annotations.BeforeMethod;
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsyncimport org.testng.annotations.Test;
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsync
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsyncimport java.security.Principal;
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsync
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsyncpublic class AuthenticationModuleEventAuditorTest {
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsync
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsync private AuthenticationModuleEventAuditor auditor;
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsync
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsync @Mock
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsync private AuditEventPublisher eventPublisher;
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsync
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsync @Mock
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsync private AuditEventFactory eventFactory;
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsync
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsync @Mock
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsync private LoginState emptyState;
4d1226da58f42754f7b947e5ab65fb391de20946vboxsync
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsync @Mock
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsync private Principal emptyPrincipal;
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsync
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsync @Mock
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsync private AuthenticationAuditEntry emptyAuditEntryDetail;
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsync
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsync @BeforeMethod
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsync public void setupMocks() {
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsync MockitoAnnotations.initMocks(this);
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsync
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsync when(eventPublisher.isAuditing(anyString(), anyString(), any(EventName.class))).thenReturn(true);
4d1226da58f42754f7b947e5ab65fb391de20946vboxsync when(eventFactory.authenticationEvent(anyString())).thenCallRealMethod();
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsync auditor = new AuthenticationModuleEventAuditor(eventPublisher, eventFactory);
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsync }
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsync
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsync @Test
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsync public void shouldNotFailAuditModuleSuccess() {
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsync // given
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsync
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsync // when
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsync auditor.auditModuleSuccess(null, null, null);
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsync auditor.auditModuleSuccess(emptyState, emptyPrincipal, emptyAuditEntryDetail);
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsync
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsync // then
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsync verify(eventPublisher, times(2)).tryPublish(anyString(), any(AuditEvent.class));
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsync // no exceptions expected
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsync }
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsync
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsync @Test
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsync public void shouldNotFailAuditModuleFailure() {
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsync // given
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsync
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsync // when
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsync auditor.auditModuleFailure(null, null, null);
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsync auditor.auditModuleFailure(emptyState, emptyPrincipal, emptyAuditEntryDetail);
4d1226da58f42754f7b947e5ab65fb391de20946vboxsync
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsync // then
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsync verify(eventPublisher, times(2)).tryPublish(anyString(), any(AuditEvent.class));
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsync // no exceptions expected
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsync }
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsync}
6ebc06c9cd87d26f64680c2b58e6805b6b504728vboxsync