/*
 * Copyright © 2023 by Data Virtuality GmbH
 */
package org.teiid.query.function;

import org.junit.jupiter.api.Test;
import org.teiid.query.util.CommandContext;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.teiid.query.function.FunctionMethods.*;

public class TestUserCaller {
    @Test
    public void testTrimSecurityDomain() {
        assertNull(trimSecurityDomain(null));
        assertEquals("", trimSecurityDomain(""));
        assertEquals("user1", trimSecurityDomain("user1"));
        assertEquals("user1", trimSecurityDomain("user1@"));
        assertEquals("user1", trimSecurityDomain("user1@test"));
        assertEquals("user1@test", trimSecurityDomain("user1@test@test"));
    }

    @Test
    public void testUser() {
        CommandContext ctx = new CommandContext();
        assertNull(user(ctx));
        ctx.setUserName("");
        assertEquals("", user(ctx));
        ctx.setUserName("user1");
        assertEquals("user1", user(ctx));
        ctx.setUserName("user1@");
        assertEquals("user1", user(ctx));
        ctx.setUserName("user1@test");
        assertEquals("user1", user(ctx));
        ctx.setUserName("user1@test@test");
        assertEquals("user1@test", user(ctx));
    }

    @Test
    public void testCaller() {
        CommandContext ctx = new CommandContext();
        assertNull(caller(ctx));
        ctx.setCallerUser("");
        assertEquals("", caller(ctx));
        ctx.setCallerUser("user1");
        assertEquals("user1", caller(ctx));
        ctx.setCallerUser("user1@");
        assertEquals("user1", caller(ctx));
        ctx.setCallerUser("user1@test");
        assertEquals("user1", caller(ctx));
        ctx.setCallerUser("user1@test@test");
        assertEquals("user1@test", caller(ctx));
    }
}
