/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.apache.gravitino.auth;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;

public class TestKerberosPrincipal {

  @Test
  public void testBasicConstruction() {
    KerberosPrincipal principal = new KerberosPrincipal("john", "admin", "EXAMPLE.COM");

    assertEquals("john", principal.getName());
    assertEquals("john/admin", principal.getPrimaryWithInstance());
    assertTrue(principal.getInstance().isPresent());
    assertEquals("admin", principal.getInstance().get());
    assertTrue(principal.getRealm().isPresent());
    assertEquals("EXAMPLE.COM", principal.getRealm().get());
    assertEquals("john/admin@EXAMPLE.COM", principal.getFullPrincipal());
  }

  @Test
  public void testPrincipalWithoutInstance() {
    KerberosPrincipal principal = new KerberosPrincipal("john", null, "EXAMPLE.COM");

    assertEquals("john", principal.getName());
    assertEquals("john", principal.getPrimaryWithInstance());
    assertFalse(principal.getInstance().isPresent());
    assertTrue(principal.getRealm().isPresent());
    assertEquals("EXAMPLE.COM", principal.getRealm().get());
    assertEquals("john@EXAMPLE.COM", principal.getFullPrincipal());
  }

  @Test
  public void testPrincipalWithoutRealm() {
    KerberosPrincipal principal = new KerberosPrincipal("john", "admin", null);

    assertEquals("john", principal.getName());
    assertEquals("john/admin", principal.getPrimaryWithInstance());
    assertTrue(principal.getInstance().isPresent());
    assertEquals("admin", principal.getInstance().get());
    assertFalse(principal.getRealm().isPresent());
    assertEquals("john/admin", principal.getFullPrincipal());
  }

  @Test
  public void testMinimalPrincipal() {
    KerberosPrincipal principal = new KerberosPrincipal("john", null, null);

    assertEquals("john", principal.getName());
    assertEquals("john", principal.getPrimaryWithInstance());
    assertFalse(principal.getInstance().isPresent());
    assertFalse(principal.getRealm().isPresent());
    assertEquals("john", principal.getFullPrincipal());
  }

  @Test
  public void testNullUsernameThrowsException() {
    assertThrows(IllegalArgumentException.class, () -> new KerberosPrincipal(null, null, null));
  }

  @Test
  public void testEmptyUsernameThrowsException() {
    assertThrows(IllegalArgumentException.class, () -> new KerberosPrincipal("", null, null));
  }

  @Test
  public void testEquality() {
    KerberosPrincipal principal1 = new KerberosPrincipal("john", "admin", "EXAMPLE.COM");
    KerberosPrincipal principal2 = new KerberosPrincipal("john", "admin", "EXAMPLE.COM");
    KerberosPrincipal principal3 = new KerberosPrincipal("john", "other", "EXAMPLE.COM");

    assertEquals(principal1, principal2);
    assertEquals(principal1.hashCode(), principal2.hashCode());
    assertFalse(principal1.equals(principal3));
  }

  @Test
  public void testToString() {
    KerberosPrincipal principal =
        new KerberosPrincipal("HTTP", "server.example.com", "EXAMPLE.COM");
    String toString = principal.toString();

    assertNotNull(toString);
    assertTrue(toString.contains("HTTP/server.example.com@EXAMPLE.COM"));
  }

  @Test
  public void testServicePrincipal() {
    KerberosPrincipal principal =
        new KerberosPrincipal("HTTP", "server.example.com", "EXAMPLE.COM");

    assertEquals("HTTP", principal.getName());
    assertEquals("HTTP/server.example.com", principal.getPrimaryWithInstance());
    assertEquals("server.example.com", principal.getInstance().get());
    assertEquals("EXAMPLE.COM", principal.getRealm().get());
    assertEquals("HTTP/server.example.com@EXAMPLE.COM", principal.getFullPrincipal());
  }
}
