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