package io.quarkus.gradle;

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

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.gradle.testkit.runner.BuildResult;
import org.gradle.testkit.runner.GradleRunner;
import org.gradle.testkit.runner.TaskOutcome;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;

import io.quarkus.cli.commands.CreateProject;
import io.quarkus.cli.commands.writer.FileProjectWriter;
import io.quarkus.generators.BuildTool;
import io.quarkus.generators.SourceType;

public class QuarkusPluginFunctionalTest extends QuarkusGradleTestBase {

    private File projectRoot;

    @BeforeEach
    void setUp(@TempDir File projectRoot) {
        this.projectRoot = projectRoot;
    }

    @Test
    public void canRunListExtensions() throws IOException {
        createProject(SourceType.JAVA);

        BuildResult build = GradleRunner.create()
                .forwardOutput()
                .withPluginClasspath()
                .withArguments(arguments("listExtensions"))
                .withProjectDir(projectRoot)
                .build();

        assertThat(build.task(":listExtensions").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
        assertThat(build.getOutput()).contains("Quarkus - Core");
    }

    @ParameterizedTest(name = "Build {0} project")
    @EnumSource(SourceType.class)
    public void canBuild(SourceType sourceType) throws IOException, InterruptedException {
        createProject(sourceType);

        BuildResult build = GradleRunner.create()
                .forwardOutput()
                .withPluginClasspath()
                .withArguments(arguments("build", "--stacktrace"))
                .withProjectDir(projectRoot)
                .build();

        assertThat(build.task(":build").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
        // gradle build should not build the native image
        assertThat(build.task(":buildNative")).isNull();
    }

    private void createProject(SourceType sourceType) throws IOException {
        Map<String, Object> context = new HashMap<>();
        context.put("path", "/greeting");
        assertThat(new CreateProject(new FileProjectWriter(projectRoot))
                .groupId("com.acme.foo")
                .artifactId("foo")
                .version("1.0.0-SNAPSHOT")
                .buildTool(BuildTool.GRADLE)
                .className("org.acme.GreetingResource")
                .sourceType(sourceType)
                .doCreateProject(context))
                        .withFailMessage("Project was not created")
                        .isTrue();
    }
}
@@ -254,16 +254,144 @@
 l.Map;%0A%0A
+import io.quarkus.platform.descriptor.QuarkusPlatformDescriptor;%0Aimport io.quarkus.platform.tools.config.QuarkusPlatformConfig;%0A
 import o
@@ -428,16 +428,16 @@
 Result;%0A
-
 import o
@@ -1669,24 +1669,601 @@
 e%22);%0A    %7D%0A%0A
+    @Test%0A    public void canGenerateConfig() throws IOException %7B%0A        createProject(SourceType.JAVA);%0A%0A        BuildResult build = GradleRunner.create()%0A                .forwardOutput()%0A                .withPluginClasspath()%0A                .withArguments(arguments(%22generateConfig%22))%0A                .withProjectDir(projectRoot)%0A                .build();%0A%0A        assertThat(build.task(%22:generateConfig%22).getOutcome()).isEqualTo(TaskOutcome.SUCCESS);%0A        assertThat(projectRoot.toPath().resolve(%22src/main/resources/application.properties.example%22)).exists();%0A    %7D%0A%0A%0A
     @Paramet
@@ -3099,24 +3099,24 @@
 greeting%22);%0A
-
         asse
@@ -3174,16 +3174,119 @@
 ectRoot)
+,%0A                                     QuarkusPlatformConfig.getGlobalDefault().getPlatformDescriptor()
 )%0A      

Add test for generateConfig