/*
 * 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.maven.plugin.surefire;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.DefaultArtifact;
import org.apache.maven.surefire.providerapi.ProviderInfo;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verifyNoMoreInteractions;

/**
 * Testing providerInfo applicable behavior.
 */
@ExtendWith(MockitoExtension.class)
public class AbstractSurefireMojoProvidersInfoTest {

    @Spy
    private AbstractSurefireMojo mojo;

    @Test
    public void dynamicProviderAreAlwaysApplicable() {
        ProviderInfo providerInfo = mojo.new DynamicProviderInfo("test");
        assertThat(providerInfo.isApplicable()).isTrue();
    }

    // surefire-junit-platform

    @Test
    public void jUnitPlatformProviderApplicable() {
        Artifact junitPlatform = mock(Artifact.class);
        ProviderInfo providerInfo = mojo
        .new JUnitPlatformProviderInfo(
                null, junitPlatform, aTestClassPath(), null, null, null, null, null, null, null, null, null);

        assertThat(providerInfo.isApplicable()).isTrue();

        // no interaction with artifact only reference are checked
        verifyNoMoreInteractions(junitPlatform);
    }

    @Test
    public void jUnitPlatformProviderNotApplicable() {
        ProviderInfo providerInfo = mojo
        .new JUnitPlatformProviderInfo(
                null, null, aTestClassPath(), null, null, null, null, null, null, null, null, null);
        assertThat(providerInfo.isApplicable()).isFalse();
    }

    @Test
    public void jUnitPlatformProviderNotApplicableForPlatformRunner() {
        // not applicable if junit-platform-runner is on classpath
        Artifact junitPlatformRunnerArtifact = mock(Artifact.class);
        ProviderInfo providerInfo = mojo
        .new JUnitPlatformProviderInfo(
                junitPlatformRunnerArtifact,
                null,
                aTestClassPath(),
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null);
        assertThat(providerInfo.isApplicable()).isFalse();

        // no interaction with artifact only reference are checked
        verifyNoMoreInteractions(junitPlatformRunnerArtifact);
    }

    // surefire-junit4

    @Test
    public void jUnit4ProviderNullArtifacts() {
        ProviderInfo providerInfo = mojo
        .new JUnitPlatformProviderInfo(
                null, null, aTestClassPath(), null, null, null, null, null, null, null, null, null);
        assertThat(providerInfo.isApplicable()).isFalse();
    }

    @Test
    public void jUnit4ProviderJunit3AsDependencyArtifact() {
        Artifact junit = aArtifact("3.8.1");
        ProviderInfo providerInfo = mojo
        .new JUnitPlatformProviderInfo(
                null, null, aTestClassPath(), junit, null, null, null, null, null, null, null, null);
        assertThat(providerInfo.isApplicable()).isFalse();
    }

    @Test
    public void jUnit4ProviderJunit45AsDependencyArtifact() {
        Artifact junit = aArtifact("3.8.1");
        ProviderInfo providerInfo = mojo
        .new JUnitPlatformProviderInfo(
                null, null, aTestClassPath(), junit, null, null, null, null, null, null, null, null);
        assertThat(providerInfo.isApplicable()).isFalse();
    }

    @Test
    public void jUnit4ProviderJunit47AsDependencyArtifact() {
        Artifact junit = aArtifact("3.8.1");
        ProviderInfo providerInfo = mojo
        .new JUnitPlatformProviderInfo(
                null, null, aTestClassPath(), junit, null, null, null, null, null, null, null, null);
        assertThat(providerInfo.isApplicable()).isFalse();
    }

    @Test
    public void jUnit4ProviderJunit413AsDependencyArtifact() {
        Artifact junit = aArtifact("4.13");
        ProviderInfo providerInfo = mojo
        .new JUnitPlatformProviderInfo(
                null, null, aTestClassPath(), junit, null, null, null, null, null, null, null, null);
        assertThat(providerInfo.isApplicable()).isTrue();
    }

    private TestClassPath aTestClassPath() {
        return new TestClassPath(null, null, null, null);
    }

    private Artifact aArtifact(String version) {
        return new DefaultArtifact("test", "test", version, "test", "jar", "", null);
    }
}
