001/*
002 * The contents of this file are subject to the license and copyright
003 * detailed in the LICENSE and NOTICE files at the root of the source
004 * tree.
005 */
006
007package org.fcrepo.kernel.impl;
008
009import static org.mockito.ArgumentMatchers.any;
010import static org.mockito.Mockito.doAnswer;
011import static org.mockito.Mockito.when;
012
013import org.fcrepo.kernel.api.Transaction;
014
015import org.mockito.Mockito;
016
017/**
018 * TX help for testing
019 *
020 * @author pwinckles
021 */
022public class TestTransactionHelper {
023
024    private TestTransactionHelper() {
025
026    }
027
028    /**
029     * Create a mock transaction.
030     * @param transactionId the id of the transaction
031     * @param isShortLived is the transaction short-lived.
032     * @return the mock transaction.
033     */
034    public static Transaction mockTransaction(final String transactionId, final boolean isShortLived) {
035        final var transaction = Mockito.mock(Transaction.class);
036        when(transaction.getId()).thenReturn(transactionId);
037        when(transaction.isShortLived()).thenReturn(isShortLived);
038        when(transaction.isOpenLongRunning()).thenReturn(!isShortLived);
039        when(transaction.isOpen()).thenReturn(true);
040        doAnswer(invocationOnMock -> {
041            invocationOnMock.getArgument(0, Runnable.class).run();
042            return null;
043        }).when(transaction).doInTx(any(Runnable.class));
044        return transaction;
045    }
046
047}