001/* 002 * Licensed to DuraSpace under one or more contributor license agreements. 003 * See the NOTICE file distributed with this work for additional information 004 * regarding copyright ownership. 005 * 006 * DuraSpace licenses this file to you under the Apache License, 007 * Version 2.0 (the "License"); you may not use this file except in 008 * compliance with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.fcrepo.http.commons.session; 019 020import static org.fcrepo.http.commons.test.util.TestHelpers.setField; 021import static org.junit.Assert.assertEquals; 022import static org.junit.Assert.assertTrue; 023import static org.mockito.ArgumentMatchers.any; 024import static org.mockito.ArgumentMatchers.isNull; 025import static org.mockito.Mockito.verify; 026import static org.mockito.Mockito.when; 027 028import javax.jcr.Credentials; 029import javax.servlet.http.HttpServletRequest; 030 031import org.fcrepo.kernel.api.FedoraRepository; 032import org.fcrepo.kernel.api.FedoraSession; 033import org.fcrepo.kernel.api.exception.SessionMissingException; 034import org.fcrepo.kernel.api.services.BatchService; 035import org.fcrepo.kernel.api.services.CredentialsService; 036import org.junit.Before; 037import org.junit.Test; 038import org.junit.runner.RunWith; 039import org.mockito.Mock; 040import org.mockito.junit.MockitoJUnitRunner; 041import com.google.common.base.Throwables; 042 043/** 044 * <p>SessionFactoryTest class.</p> 045 * 046 * @author awoods 047 */ 048@RunWith(MockitoJUnitRunner.Silent.class) 049public class SessionFactoryTest { 050 051 private SessionFactory testObj; 052 053 @Mock 054 private FedoraSession mockSession; 055 056 @Mock 057 private FedoraRepository mockRepo; 058 059 @Mock 060 private BatchService mockTxService; 061 062 @Mock 063 private CredentialsService mockCredService; 064 065 @Mock 066 private FedoraSession mockTx; 067 068 @Mock 069 private HttpServletRequest mockRequest; 070 071 @Before 072 public void setUp() { 073 testObj = new SessionFactory(mockRepo, mockTxService); 074 setField(testObj, "credentialsService", mockCredService); 075 testObj.init(); 076 } 077 078 @Test 079 public void testGetSessionWithNullPath() { 080 when(mockRequest.getPathInfo()).thenReturn(null); 081 when(mockRequest.getContextPath()).thenReturn(""); 082 when(mockRepo.login(any(Credentials.class))).thenReturn(mockSession); 083 testObj.getSession(mockRequest); 084 verify(mockRepo).login(isNull()); 085 } 086 087 @Test 088 public void testGetSessionUnauthenticated() { 089 testObj.getInternalSession(); 090 verify(mockRepo).login(); 091 } 092 093 @Test 094 public void testCreateSession() { 095 when(mockRequest.getPathInfo()).thenReturn("/some/path"); 096 testObj.createSession(mockRequest); 097 verify(mockRepo).login(isNull()); 098 } 099 100 @Test 101 public void testGetSessionFromTransaction() { 102 when(mockRequest.getPathInfo()).thenReturn("/tx:123/some/path"); 103 when(mockTxService.getSession("123", null)).thenReturn(mockTx); 104 final HttpSession session = testObj.getSessionFromTransaction(mockRequest, "123"); 105 assertEquals(mockTx, session.getFedoraSession()); 106 } 107 108 @Test 109 public void testGetSessionThrowException() { 110 when(mockRequest.getPathInfo()).thenReturn("/tx:123/some/path"); 111 when(mockTxService.getSession("123", null)).thenThrow(new SessionMissingException("")); 112 try { 113 testObj.getSession(mockRequest); 114 } catch (final RuntimeException e) { 115 final Throwable rootCause = Throwables.getRootCause(e); 116 assertTrue("TransactionMissionException expected", 117 rootCause instanceof SessionMissingException); 118 } 119 } 120 121 @Test 122 public void testGetEmbeddedIdTx() { 123 when(mockRequest.getPathInfo()).thenReturn("/tx:123/some/path"); 124 final String txId = testObj.getEmbeddedId(mockRequest, SessionFactory.Prefix.TX); 125 assertEquals("txId should be 123", "123", txId); 126 } 127 128}