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