package com.atlassian.stash.internal.maintenance;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InOrder;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import static org.mockito.Mockito.*;

@RunWith(MockitoJUnitRunner.class)
public class MaintenanceModePhaseTest {

    @Mock
    private JohnsonMaintenanceEvent event;
    @Mock
    private MaintenanceModeHelper maintenanceModeHelper;
    private MaintenanceModePhase phase;
    @Mock
    private MaintenanceTask step;

    @Before
    public void setUp() {
        phase = new MaintenanceModePhase.Builder(maintenanceModeHelper)
                .event(event)
                .add(step, 100)
                .build();
    }

    @Test
    public void testRun() throws Exception {
        try {
            phase.run();
        } finally { //Done in a finally block to allow testRunStepException to reuse it
            InOrder ordered = inOrder(maintenanceModeHelper, step);
            ordered.verify(maintenanceModeHelper).lock(same(event));
            ordered.verify(step).run();
            ordered.verify(maintenanceModeHelper).unlock(same(event));
        }
    }

    @Test(expected = UnsupportedOperationException.class)
    public void testRunStepException() throws Exception {
        doThrow(new UnsupportedOperationException()).when(step).run();

        testRun();
    }
}
