package com.atlassian.bitbucket.internal.mirroring.mirror.rest;

import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.nullValue;

/**
 * @since 4.6
 */
public class RestConnectInstalledEventTest {

    @Test
    public void testGetTeamIdReturnsNullForRegularUser() {
        assertThat(forUser("userId", "jack").getTeamId(), nullValue());
    }

    @Test
    public void testGetTeamNameReturnsNullForRegularUser() {
        assertThat(forUser("userId", "jack").getTeamName(), nullValue());
    }

    @Test
    public void testGetTeamIdReturnsNullForEmptyEvent() {
        assertThat(new RestConnectInstalledEvent().getTeamId(), nullValue());
    }

    @Test
    public void testGetTeamNameReturnsNullForEmptyEvent() {
        assertThat(new RestConnectInstalledEvent().getTeamName(), nullValue());
    }

    @Test
    public void testGetTeamNameReturnsWhenTeamNameIsEmpty() {
        assertThat(forTeam(null, null).getTeamName(), nullValue());
    }

    @Test
    public void testGetTeamIdReturnsWhenTeamNameIsEmpty() {
        assertThat(forTeam(null, null).getTeamId(), nullValue());
    }

    @Test
    public void testGetTeamIdReturnsTeamId() {
        assertThat(forTeam("teamId", "teamName").getTeamId(), equalTo("teamId"));
    }

    @Test
    public void testGetTeamNameReturnsTeamName() {
        assertThat(forTeam("team", "teamName").getTeamName(), equalTo("teamName"));
    }

    private RestConnectInstalledEvent forTeam(String id, String team) {
        return forPrincipal(id, team, "team");
    }

    private static RestConnectInstalledEvent forUser(String id, String name) {
        return forPrincipal(id, name, "user");
    }

    private static RestConnectInstalledEvent forPrincipal(String id, String name, String type) {

        Map<String, String> user = new HashMap<>();
        if (type != null) {
            user.put("uuid", id);
        }
        if (name != null) {
            user.put("username", name);
        }
        if (type != null) {
            user.put("type", type);
        }

        RestConnectInstalledEvent event = new RestConnectInstalledEvent();
        event.put("principal", user);
        return event;
    }
}