/*
 * 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.iggy.identifier;

import org.apache.iggy.exception.IggyInvalidArgumentException;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

abstract class BaseIdentifierTest<T extends Identifier> {
    protected abstract T ofName(String name);

    protected abstract T ofId(Long id);

    @Test
    void ofNameReturnsIdentifierWithName() {
        var identifier = ofName("name");

        assertThat(identifier.getName()).isEqualTo("name");
        assertThat(identifier.getId()).isNull();
    }

    @Test
    void ofNameThrowsIggyInvalidArgumentExceptionWhenGivenNull() {
        assertThatThrownBy(() -> ofName(null)).isInstanceOf(IggyInvalidArgumentException.class);
    }

    @Test
    void ofIdReturnsIdentifierWithId() {
        var identifier = ofId(123L);

        assertThat(identifier.getId()).isEqualTo(123L);
        assertThat(identifier.getName()).isNull();
    }

    @Test
    void ofIdThrowsIggyInvalidArgumentExceptionWhenGivenNull() {
        assertThatThrownBy(() -> ofId(null)).isInstanceOf(IggyInvalidArgumentException.class);
    }

    @Test
    void toStringReturnsNameWhenIdentifierCreatedWithName() {
        var identifier = ofName("name");

        assertThat(identifier.toString()).isEqualTo("name");
    }

    @Test
    void toStringReturnsIdWhenIdentifierCreatedWithId() {
        var identifier = ofId(123L);

        assertThat(identifier.toString()).isEqualTo("123");
    }

    @Test
    void getKindReturns1WhenIdentifierCreatedWithId() {
        var identifier = ofId(123L);

        assertThat(identifier.getKind()).isEqualTo(1);
    }

    @Test
    void getKindReturns2WhenIdentifierCreatedWithName() {
        var identifier = ofName("name");

        assertThat(identifier.getKind()).isEqualTo(2);
    }

    @Test
    void getIdReturnsIdWhenIdentifierCreatedWithId() {
        var identifier = ofId(123L);

        assertThat(identifier.getId()).isEqualTo(123L);
    }

    @Test
    void getIdReturnsNullWhenIdentifierCreatedWithName() {
        var identifier = ofName("name");

        assertThat(identifier.getId()).isNull();
    }

    @Test
    void getNameReturnsNameWhenIdentifierCreatedWithName() {
        var identifier = ofName("name");

        assertThat(identifier.getName()).isEqualTo("name");
    }

    @Test
    void getNameReturnsNullWhenIdentifierCreatedWithId() {
        var identifier = ofId(123L);

        assertThat(identifier.getName()).isNull();
    }

    @Test
    void getSizeReturns6WhenIdentifierCreatedWithId() {
        var identifier = ofId(123456789L);

        assertThat(identifier.getSize()).isEqualTo(6);
    }

    @ParameterizedTest
    @CsvSource({"foo, 5", "a, 3", "abcdefghijklmnopqrstuvwxyz, 28"})
    void getSizeReturnsExpectedSizeWhenIdentifierCreatedWithName(String name, int expectedSize) {
        var identifier = ofName(name);

        assertThat(identifier.getSize()).isEqualTo(expectedSize);
    }
}
