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 */
018
019package org.fcrepo.kernel.impl;
020
021import static org.mockito.ArgumentMatchers.any;
022import static org.mockito.Mockito.doAnswer;
023import static org.mockito.Mockito.when;
024
025import org.fcrepo.kernel.api.Transaction;
026
027import org.mockito.Mockito;
028
029/**
030 * TX help for testing
031 *
032 * @author pwinckles
033 */
034public class TestTransactionHelper {
035
036    private TestTransactionHelper() {
037
038    }
039
040    /**
041     * Create a mock transaction.
042     * @param transactionId the id of the transaction
043     * @param isShortLived is the transaction short-lived.
044     * @return the mock transaction.
045     */
046    public static Transaction mockTransaction(final String transactionId, final boolean isShortLived) {
047        final var transaction = Mockito.mock(Transaction.class);
048        when(transaction.getId()).thenReturn(transactionId);
049        when(transaction.isShortLived()).thenReturn(isShortLived);
050        when(transaction.isOpenLongRunning()).thenReturn(!isShortLived);
051        when(transaction.isOpen()).thenReturn(true);
052        doAnswer(invocationOnMock -> {
053            invocationOnMock.getArgument(0, Runnable.class).run();
054            return null;
055        }).when(transaction).doInTx(any(Runnable.class));
056        return transaction;
057    }
058
059}