LocalOperationsTest.java revision 42c10b520a70c6b2c67ad812cfda98ac1663f0c5
2852N/A/*
2887N/A * The contents of this file are subject to the terms of the Common Development and
2852N/A * Distribution License (the License). You may not use this file except in compliance with the
2852N/A * License.
2852N/A *
2852N/A * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
2852N/A * specific language governing permission and limitations under the License.
2852N/A *
2852N/A * When distributing Covered Software, include this CDDL Header Notice in each file and include
2852N/A * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
2852N/A * Header, with the fields enclosed by brackets [] replaced by your own identifying
2852N/A * information: "Portions copyright [year] [name of copyright owner]".
2852N/A *
2852N/A * Copyright 2013-2016 ForgeRock AS.
2852N/A */
2852N/Apackage com.iplanet.dpro.session.operations.strategies;
2852N/A
2852N/Aimport static org.assertj.core.api.Assertions.assertThat;
2852N/Aimport static org.mockito.BDDMockito.*;
2852N/A
2852N/Aimport org.forgerock.openam.session.SessionCookies;
2852N/Aimport org.forgerock.openam.session.SessionEventType;
2852N/Aimport org.forgerock.openam.session.authorisation.SessionChangeAuthorizer;
2852N/Aimport org.forgerock.openam.session.service.SessionAccessManager;
2852N/Aimport org.mockito.ArgumentCaptor;
4347N/Aimport org.mockito.Mock;
3175N/Aimport org.mockito.MockitoAnnotations;
2852N/Aimport org.testng.annotations.BeforeMethod;
2852N/Aimport org.testng.annotations.Test;
2852N/A
2852N/Aimport com.iplanet.dpro.session.Session;
4618N/Aimport com.iplanet.dpro.session.SessionException;
4618N/Aimport com.iplanet.dpro.session.SessionID;
4618N/Aimport com.iplanet.dpro.session.service.InternalSession;
4618N/Aimport com.iplanet.dpro.session.service.InternalSessionEvent;
4618N/Aimport com.iplanet.dpro.session.service.MonitoringOperations;
4618N/Aimport com.iplanet.dpro.session.service.SessionAuditor;
4618N/Aimport com.iplanet.dpro.session.service.SessionEventBroker;
4618N/Aimport com.iplanet.dpro.session.service.SessionLogging;
4618N/Aimport com.iplanet.dpro.session.service.SessionNotificationSender;
4618N/Aimport com.iplanet.dpro.session.service.SessionServerConfig;
4618N/Aimport com.iplanet.dpro.session.service.SessionServiceConfig;
4618N/Aimport com.iplanet.dpro.session.share.SessionInfo;
4618N/Aimport com.iplanet.dpro.session.utils.SessionInfoFactory;
4618N/Aimport com.sun.identity.shared.debug.Debug;
4618N/A
4618N/Apublic class LocalOperationsTest {
4618N/A
4168N/A private LocalOperations local;
4618N/A @Mock
4618N/A private Session mockRequester;
4618N/A @Mock
4618N/A private Session mockSession;
4618N/A @Mock
4618N/A private SessionID mockSessionID;
4618N/A @Mock
4618N/A private InternalSession mockInternalSession;
4618N/A @Mock
4618N/A private SessionAccessManager sessionAccessManager;
4618N/A @Mock
4618N/A private SessionInfoFactory sessionInfoFactory;
4618N/A @Mock
4618N/A private SessionServerConfig serverConfig;
4618N/A @Mock
4618N/A private SessionServiceConfig serviceConfig;
4618N/A @Mock
4618N/A private MonitoringOperations monitoringOperations;
4618N/A @Mock
4618N/A private SessionCookies sessionCookies;
4618N/A @Mock
4618N/A private SessionChangeAuthorizer sessionChangeAuthorizer;
4618N/A @Mock
4618N/A private SessionEventBroker sessionEventBroker;
4618N/A
4618N/A @BeforeMethod
4618N/A public void setup() {
4618N/A MockitoAnnotations.initMocks(this);
4618N/A given(mockSession.getID()).willReturn(mockSessionID);
4618N/A given(mockInternalSession.getID()).willReturn(mockSessionID);
4618N/A given(mockInternalSession.getSessionID()).willReturn(mockSessionID);
4618N/A given(sessionAccessManager.getInternalSession(mockSessionID)).willReturn(mockInternalSession);
4618N/A given(sessionAccessManager.removeInternalSession(mockSessionID)).willReturn(mockInternalSession);
4618N/A given(mockSession.getID()).willReturn(mockSessionID);
4618N/A
4618N/A local = new LocalOperations(mock(Debug.class), sessionAccessManager, sessionInfoFactory, serverConfig,
4618N/A mock(SessionNotificationSender.class), mock(SessionLogging.class), mock(SessionAuditor.class),
4618N/A sessionEventBroker, sessionChangeAuthorizer, serviceConfig);
4618N/A }
4618N/A
4618N/A @Test
4618N/A public void shouldNotSetLastAccessTimeWhenResetFlagIsFalse() throws SessionException {
4618N/A // Given
4618N/A boolean flag = false;
4618N/A // When
4618N/A local.refresh(mockSession, flag);
4618N/A // Then
4618N/A verify(mockInternalSession, times(0)).setLatestAccessTime();
4618N/A }
4618N/A
4618N/A @Test
4618N/A public void shouldSetLastAccessTimeWhenResetFlagIsTrue() throws SessionException {
4618N/A // Given
4618N/A boolean flag = true;
4618N/A // When
4618N/A local.refresh(mockSession, flag);
4618N/A // Then
4618N/A verify(mockInternalSession).setLatestAccessTime();
4618N/A }
4618N/A
4618N/A @Test
4618N/A public void shouldReturnSessionInfoOnRefresh() throws SessionException {
4618N/A // Given
4618N/A SessionInfo mockSessionInfo = mock(SessionInfo.class);
4618N/A
4618N/A given(sessionInfoFactory.getSessionInfo(mockInternalSession, mockInternalSession.getSessionID()))
4618N/A .willReturn(mockSessionInfo);
4618N/A // When
4618N/A SessionInfo result = local.refresh(mockSession, true);
4618N/A // Then
4618N/A assertThat(result).isEqualTo(mockSessionInfo);
4618N/A }
4618N/A
4618N/A @Test
4618N/A public void shouldRemoveSessionFromSessionAccessManagerOnLogout() throws Exception {
4618N/A // Given
4618N/A given(mockSession.getSessionID()).willReturn(mockSessionID);
4618N/A given(mockSession.getID()).willReturn(mockSessionID);
4618N/A // When
4618N/A local.logout(mockSession);
4618N/A // Then
4618N/A verify(sessionAccessManager).removeInternalSession(mockSessionID);
4618N/A }
4618N/A
4618N/A @Test
4618N/A public void firesInternalSessionEventWhenLoggingOutSession() throws Exception {
4618N/A // Given
4618N/A given(mockSession.getSessionID()).willReturn(mockSessionID);
4618N/A given(mockSession.getID()).willReturn(mockSessionID);
4618N/A // When
4618N/A local.logout(mockSession);
4618N/A // Then
4618N/A verifyEvent(SessionEventType.LOGOUT);
4618N/A }
4618N/A
4618N/A @Test
4618N/A public void shouldRemoveSessionFromSessionAccessManagerOnDestroy() throws SessionException {
4618N/A // Given
4618N/A given(mockSession.getSessionID()).willReturn(mockSessionID);
4618N/A given(sessionAccessManager.getInternalSession(mockSessionID)).willReturn(mockInternalSession);
4618N/A // When
4618N/A local.destroy(mockRequester, mockSession);
4618N/A // Then
4618N/A verify(sessionAccessManager).removeSessionId(eq(mockSessionID));
4618N/A }
4618N/A
4618N/A @Test
4618N/A public void firesInternalSessionEventWhenDestroyingSession() throws Exception {
4618N/A // Given
4618N/A given(mockSession.getSessionID()).willReturn(mockSessionID);
4618N/A given(sessionAccessManager.getInternalSession(mockSessionID)).willReturn(mockInternalSession);
4618N/A // When
4618N/A local.destroy(mockRequester, mockSession);
4618N/A // Then
4618N/A verifyEvent(SessionEventType.DESTROY);
4618N/A }
4618N/A
4618N/A private void verifyEvent(SessionEventType eventType) {
4618N/A ArgumentCaptor<InternalSessionEvent> eventCaptor = ArgumentCaptor.forClass(InternalSessionEvent.class);
4618N/A verify(sessionEventBroker, times(1)).onEvent(eventCaptor.capture());
4618N/A InternalSessionEvent event = eventCaptor.getValue();
4618N/A assertThat(event.getType()).isEqualTo(eventType);
4618N/A }
4618N/A
4618N/A}
4618N/A