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.Matchers.any; 024import static org.mockito.Mockito.verify; 025import static org.mockito.Mockito.when; 026 027import java.security.Principal; 028 029import javax.jcr.Credentials; 030import javax.servlet.http.HttpServletRequest; 031 032import org.fcrepo.kernel.api.FedoraRepository; 033import org.fcrepo.kernel.api.FedoraSession; 034import org.fcrepo.kernel.api.exception.SessionMissingException; 035import org.fcrepo.kernel.api.services.BatchService; 036import org.fcrepo.kernel.api.services.CredentialsService; 037import org.junit.Before; 038import org.junit.Test; 039import org.junit.runner.RunWith; 040import org.mockito.Mock; 041import org.mockito.runners.MockitoJUnitRunner; 042import org.modeshape.jcr.api.ServletCredentials; 043 044import com.google.common.base.Throwables; 045 046/** 047 * <p>SessionFactoryTest class.</p> 048 * 049 * @author awoods 050 */ 051@RunWith(MockitoJUnitRunner.class) 052public class SessionFactoryTest { 053 054 SessionFactory testObj; 055 056 @Mock 057 private FedoraSession txSession; 058 059 @Mock 060 private FedoraSession mockSession; 061 062 @Mock 063 private FedoraRepository mockRepo; 064 065 @Mock 066 private BatchService mockTxService; 067 068 @Mock 069 private CredentialsService mockCredService; 070 071 @Mock 072 private FedoraSession mockTx; 073 074 @Mock 075 private HttpServletRequest mockRequest; 076 077 @Mock 078 private Principal mockUser; 079 080 @Before 081 public void setUp() { 082 testObj = new SessionFactory(mockRepo, mockTxService); 083 setField(testObj, "credentialsService", mockCredService); 084 testObj.init(); 085 } 086 087 @Test 088 public void testGetSessionWithNullPath() { 089 when(mockRequest.getPathInfo()).thenReturn(null); 090 when(mockRequest.getContextPath()).thenReturn(""); 091 when(mockRepo.login(any(Credentials.class))).thenReturn(mockSession); 092 testObj.getSession(mockRequest); 093 verify(mockRepo).login(any(ServletCredentials.class)); 094 } 095 096 @Test 097 public void testGetSessionUnauthenticated() { 098 testObj.getInternalSession(); 099 verify(mockRepo).login(); 100 } 101 102 @Test 103 public void testCreateSession() { 104 when(mockRequest.getPathInfo()).thenReturn("/some/path"); 105 testObj.createSession(mockRequest); 106 verify(mockRepo).login(any(Credentials.class)); 107 } 108 109 @Test 110 public void testGetSessionFromTransaction() { 111 when(mockRequest.getPathInfo()).thenReturn("/tx:123/some/path"); 112 when(mockTxService.getSession("123", null)).thenReturn(mockTx); 113 final HttpSession session = testObj.getSessionFromTransaction(mockRequest, "123"); 114 assertEquals(mockTx, session.getFedoraSession()); 115 } 116 117 @Test 118 public void testGetSessionThrowException() { 119 when(mockRequest.getPathInfo()).thenReturn("/tx:123/some/path"); 120 when(mockTxService.getSession("123", null)).thenThrow(new SessionMissingException("")); 121 try { 122 testObj.getSession(mockRequest); 123 } catch (final RuntimeException e) { 124 final Throwable rootCause = Throwables.getRootCause(e); 125 assertTrue("TransactionMissionException expected", 126 rootCause instanceof SessionMissingException); 127 } 128 } 129 130 @Test 131 public void testGetEmbeddedIdTx() { 132 when(mockRequest.getPathInfo()).thenReturn("/tx:123/some/path"); 133 final String txId = testObj.getEmbeddedId(mockRequest, SessionFactory.Prefix.TX); 134 assertEquals("txId should be 123", "123", txId); 135 } 136 137}