package com.atlassian.stash.internal.audit;

import com.atlassian.bitbucket.audit.AuditEntry;
import com.atlassian.bitbucket.audit.Channels;
import com.atlassian.bitbucket.audit.Priority;
import com.atlassian.bitbucket.event.project.ProjectAvatarUpdatedEvent;
import com.atlassian.bitbucket.event.project.ProjectCreatedEvent;
import com.atlassian.bitbucket.event.project.ProjectDeletedEvent;
import com.atlassian.bitbucket.event.project.ProjectModifiedEvent;
import com.atlassian.bitbucket.event.audit.AuditEvent;
import com.atlassian.bitbucket.project.Project;
import org.junit.Test;
import org.mockito.InjectMocks;

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

public class ProjectEventListenerTest extends AbstractAuditEventListenerTest {

    @InjectMocks
    private ProjectEventListener listener;
    
    @Test
    public void testOnProjectCreated() throws Exception {
        listener.onProjectCreated(new ProjectCreatedEvent(this, mockProject()));

        AuditEvent event = getAuditEvent();
        assertEquals(null, event.getEntry().getDetails());
        assertEquals("project-key", event.getEntry().getTarget());
        assertEventCommonDetails(event, Priority.HIGH, Channels.PROJECT_UI);
    }

    @Test
    public void testOnProjectAvatarUpdated() throws Exception {
        listener.onProjectAvatarUpdated(new ProjectAvatarUpdatedEvent(this, mockProject()));

        AuditEvent event = getAuditEvent();
        assertEquals(null, event.getEntry().getDetails());
        assertEquals("project-key", event.getEntry().getTarget());
        assertEventCommonDetails(event, Priority.LOW);
    }

    @Test
    public void testOnProjectDeleted() throws Exception {
        listener.onProjectDeleted(new ProjectDeletedEvent(this, mockProject()));

        AuditEvent event = getAuditEvent();
        assertEquals(null, event.getEntry().getDetails());
        assertEquals("project-key", event.getEntry().getTarget());
        assertEventCommonDetails(event, Priority.HIGH);
    }

    @Test
    public void testOnProjectModified() throws Exception {
        Project project = mockProject();
        when(project.getDescription()).thenReturn("old");

        listener.onProjectModified(new ProjectModifiedEvent(this, project, mockProject()));

        AuditEvent event = getAuditEvent();
        AuditEntry entry = event.getEntry();
        assertContains(entry.getDetails(), "old", "null");
        assertEquals("project-key", entry.getTarget());
        assertEventCommonDetails(event, Priority.HIGH, Channels.PROJECT_UI);
    }
}
