package com.atlassian.stash.internal.web.fragments;

import com.atlassian.stash.internal.integrity.IntegrityCheckReporter;
import com.atlassian.stash.internal.integrity.IntegrityEventKey;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class IntegrityChecksContextProviderTest {

    private static final String KEY = "integrityCheckState";

    @Mock
    private IntegrityCheckReporter integrityCheckReporter;

    @InjectMocks
    private IntegrityChecksContextProvider provider;

    @Before
    public void setup() {
        when(integrityCheckReporter.getLatestEvents(eq(2))).thenReturn(Collections.emptyList());
    }

    @Test
    public void testGetContextMap() throws Exception {
        Map<String, Object> context = provider.getContextMap(ImmutableMap.of());

        assertEquals(0, ((List) context.get(KEY)).size());
    }

    @Test
    public void testGetContextMapWithEvent() throws Exception {
        when(integrityCheckReporter.getLatestEvents(eq(2))).thenReturn(ImmutableList.of(IntegrityEventKey.STARTED));

        Map<String, Object> context = provider.getContextMap(ImmutableMap.of());

        assertEquals("started", ((List) context.get(KEY)).get(0));
    }
}