/*
 * Copyright Debezium Authors.
 *
 * Licensed under the Apache Software License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
 */
package io.debezium.connector.oracle.junit;

import org.junit.jupiter.api.extension.ConditionEvaluationResult;
import org.junit.jupiter.api.extension.ExecutionCondition;
import org.junit.jupiter.api.extension.ExtensionContext;

import io.debezium.connector.oracle.util.TestHelper;
import io.debezium.junit.AnnotationBasedExtension;

/**
 * JUnit 5 extension that skips a test based on the {@link SkipWhenAdapterNameIs} annotation on
 * either a test method or test class.
 *
 * @author Chris Cranford
 */
public class SkipTestDependingOnAdapterNameExtension extends AnnotationBasedExtension implements ExecutionCondition {

    private static final String adapterName = determineAdapterName();

    @Override
    public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
        SkipWhenAdapterNameIs skipWhenAdpterName = hasAnnotation(context, SkipWhenAdapterNameIs.class);
        if (skipWhenAdpterName != null && skipWhenAdpterName.value().isEqualTo(adapterName)) {
            String reasonForSkipping = "Adapter name is " + skipWhenAdpterName.value() + System.lineSeparator() + skipWhenAdpterName.reason();
            return ConditionEvaluationResult.disabled(reasonForSkipping);
        }

        SkipWhenAdapterNameIsNot skipWhenAdapterNameNot = hasAnnotation(context, SkipWhenAdapterNameIsNot.class);
        if (skipWhenAdapterNameNot != null && skipWhenAdapterNameNot.value().isNotEqualTo(adapterName)) {
            String reasonForSkipping = "Adapter name is not " + skipWhenAdapterNameNot.value() + System.lineSeparator() + skipWhenAdapterNameNot.reason();
            return ConditionEvaluationResult.disabled(reasonForSkipping);
        }

        return ConditionEvaluationResult.enabled("Adapter name is compatible");
    }

    public static String determineAdapterName() {
        return TestHelper.adapter().getValue();
    }
}
