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

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;
import org.junit.jupiter.params.provider.EnumSource.Mode;

import java.util.Optional;

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

class IggyAuthorizationExceptionTest {
    @Test
    void constructorCreatesExpectedIggyAuthorizationException() {
        var exception = new IggyAuthorizationException(
                IggyErrorCode.UNAUTHORIZED, 41, "unauthorized", Optional.of("bar"), Optional.of("foo"));

        assertThat(exception).isInstanceOf(IggyAuthorizationException.class);
        assertThat(exception.getErrorCode()).isEqualTo(IggyErrorCode.UNAUTHORIZED);
        assertThat(exception.getRawErrorCode()).isEqualTo(41);
        assertThat(exception.getReason()).isEqualTo("unauthorized");
        assertThat(exception.getField()).isEqualTo(Optional.of("bar"));
        assertThat(exception.getErrorId()).isEqualTo(Optional.of("foo"));
    }

    @ParameterizedTest
    @EnumSource(
            value = IggyErrorCode.class,
            names = {"UNAUTHORIZED"})
    void matchesReturnsTrueForAuthorizationRelatedCodes(IggyErrorCode code) {
        assertThat(IggyAuthorizationException.matches(code)).isTrue();
    }

    @ParameterizedTest
    @EnumSource(
            value = IggyErrorCode.class,
            names = {"UNAUTHORIZED"},
            mode = Mode.EXCLUDE)
    void matchesReturnsFalseForNonAuthorizationRelatedCodes(IggyErrorCode code) {
        assertThat(IggyAuthorizationException.matches(code)).isFalse();
    }
}
