package com.atlassian.bitbucket.user;

import org.junit.Test;

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

public class ApplicationUserEqualityTest {

    @Test
    public void testEquality() {
        ApplicationUser nameIsAnother = createUser("another");
        ApplicationUser nameIsEmpty = createUser("");
        ApplicationUser nameIsNull = createUser(null);
        ApplicationUser nameIsUsername = createUser("username");

        assertTrue("Nulls should equal", ApplicationUserEquality.equals(null, null));
        assertFalse("Null object should never equal a non-null one", ApplicationUserEquality.equals(nameIsUsername, null));
        assertFalse("Null object should never equal a non-null one", ApplicationUserEquality.equals(null, nameIsUsername));
        assertTrue("Users with the same username should be equal", ApplicationUserEquality.equals(nameIsUsername, nameIsUsername));
        assertTrue("Users with the same username should be equal", ApplicationUserEquality.equals(nameIsEmpty, nameIsEmpty));
        assertTrue("Users with the same username should be equal", ApplicationUserEquality.equals(nameIsNull, nameIsNull));
        assertFalse("Users with different usernames should not be equal", ApplicationUserEquality.equals(nameIsUsername, nameIsAnother));
        assertFalse("Users with different usernames should not be equal", ApplicationUserEquality.equals(nameIsNull, nameIsUsername));
    }

    @Test
    public void testEqualsIsNotCaseSensitive() {
        assertTrue("Users with usernames differing by case should be equal", ApplicationUserEquality.equals(createUser("USERNAME"), createUser("username")));
    }

    @Test
    public void testHashCodeIsNotCaseSensitive() {
        assertEquals("Users with usernames differing by case should yield the same hashcode",
                ApplicationUserEquality.hashCode(createUser("USERNAME")),
                ApplicationUserEquality.hashCode(createUser("username")));
    }

    private static ApplicationUser createUser(String value) {
        ApplicationUser user = mock(ApplicationUser.class);
        when(user.getName()).thenReturn(value);

        return user;
    }
}
