package com.atlassian.stash.internal.build.hook;

import com.atlassian.bitbucket.audit.AuditEntry;
import com.atlassian.bitbucket.audit.AuditEntryBuilder;
import com.atlassian.bitbucket.project.Project;
import com.atlassian.bitbucket.repository.Repository;
import org.junit.Test;

import static org.junit.Assert.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class RequiredBuildChangedEventConverterTest {

    @Test
    public void testConvert() {
        Repository repo = mock(Repository.class);
        Project project = mock(Project.class);
        when(repo.getProject()).thenReturn(project);
        RequiredBuildChangedEventConverter converter = new RequiredBuildChangedEventConverter();
        AuditEntry entry = converter.convert(new RequiredBuildsChangedEvent(this, repo, 0, 1), new AuditEntryBuilder());
        assertContains(entry.getDetails(), "new", "0", "old", "1");
        assertEquals("null/null", entry.getTarget());
    }


    @Test
    public void testConvertNulls() {
        Repository repo = mock(Repository.class);
        Project project = mock(Project.class);
        when(repo.getProject()).thenReturn(project);
        RequiredBuildChangedEventConverter converter = new RequiredBuildChangedEventConverter();
        AuditEntry entry = converter.convert(new RequiredBuildsChangedEvent(this, repo, null, null), new AuditEntryBuilder());
        assertNotNull(entry);
        assertContains(entry.getDetails(), "new", "null", "old");
        assertEquals("null/null", entry.getTarget());
    }

    static void assertContains(String details, String... subStrings) {
        for (String subString : subStrings) {
            assertTrue(details + " doesn't contain " + subString, details.contains(subString));
        }
    }
}

