package com.atlassian.stash.internal.config;

import com.atlassian.config.lifecycle.events.ApplicationStartedEvent;
import com.atlassian.config.lifecycle.events.ApplicationStoppedEvent;
import com.atlassian.config.lifecycle.events.ApplicationStoppingEvent;
import com.atlassian.event.api.EventPublisher;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.isA;
import static org.mockito.Mockito.verify;

@RunWith(MockitoJUnitRunner.class)
public class ConfigLifecycleTest {

    @Mock
    private EventPublisher eventPublisher;
    @InjectMocks
    private ConfigLifecycle lifecycle;

    @Test
    public void testLifecycle() {
        assertTrue("ConfigLifecycle should be auto-startup", lifecycle.isAutoStartup());
        assertFalse("ConfigLifecycle should not be running", lifecycle.isRunning());

        lifecycle.start();
        assertTrue("ConfigLifecycle should be running", lifecycle.isRunning());
        verify(eventPublisher).publish(isA(ApplicationStartedEvent.class));

        lifecycle.stop();
        assertFalse("ConfigLifecycle should be stopped", lifecycle.isRunning());
        verify(eventPublisher).publish(isA(ApplicationStoppingEvent.class));
        verify(eventPublisher).publish(isA(ApplicationStoppedEvent.class));
    }
}