package io.quarkus.bootstrap.resolver.maven.workspace;

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

import org.apache.maven.model.Model;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

public class ModelUtilsTest {

    @Test
    void resolveVersion_literal() {
        assertThat(ModelUtils.resolveVersion("1.0.0", new Model())).isEqualTo("1.0.0");
    }

    @ParameterizedTest
    @ValueSource(strings = { "${revision}", "${sha1}", "${changelist}" })
    void resolveVersion_notResolvable(String rawVersion) {
        assertThat(ModelUtils.resolveVersion(rawVersion, new Model())).isNull();
    }

    @ParameterizedTest
    @ValueSource(strings = { "revision", "sha1", "changelist" })
    void resolveVersion_resolvable(String ciFriendlyPropertyName) {
        var model = new Model();
        model.getProperties().put(ciFriendlyPropertyName, "1.0.0");

        assertThat(ModelUtils.resolveVersion("${" + ciFriendlyPropertyName + "}", model)).isEqualTo("1.0.0");
    }

    @Test
    void resolveVersion_allCiFriendlyPropertyNames() {
        var model = new Model();
        model.getProperties().put("revision", "1");
        model.getProperties().put("sha1", "2");
        model.getProperties().put("changelist", "3");

        assertThat(ModelUtils.resolveVersion("${revision}.${sha1}.${changelist}", model)).isEqualTo("1.2.3");
    }

    @Test
    void resolveVersion_illegalPlaceholder() {
        var model = new Model();
        model.getProperties().put("revision", "${main.project.version}");
        model.getProperties().put("main.project.version", "1.6.0-SNAPSHOT");

        assertThatThrownBy(() -> ModelUtils.resolveVersion("${revision}", model))
                .isExactlyInstanceOf(IllegalArgumentException.class)
                .hasMessageContainingAll("revision", "main.project.version");
    }
}

@@ -1515,32 +1515,113 @@
    %7D%0A%0A    @Test%0A
+    // better error message than %22named capturing group is missing trailing '%7D'%22%0A
     void resolve
@@ -1646,16 +1646,32 @@
 ceholder
+_missingTrailing
 () %7B%0A   
@@ -2057,16 +2057,18 @@
 sion%22, %22
+$%7B
 main.pro
@@ -2079,16 +2079,655 @@
 .version
+%7D%22);%0A    %7D%0A%0A    @Test%0A    // better error message than %22No group with name %7Bversion%7D%22%0A    // see also https://github.com/quarkusio/quarkus/issues/22171%0A    void resolveVersion_illegalPlaceholder_noGroup() %7B%0A        var model = new Model();%0A        model.getProperties().put(%22revision%22, %22$%7Bversion%7D.$%7Bbuild%7D%22);%0A        model.getProperties().put(%22version%22, %221.0.0%22);%0A        model.getProperties().put(%22build%22, %220-SNAPSHOT%22);%0A%0A        assertThatThrownBy(() -%3E ModelUtils.resolveVersion(%22$%7Brevision%7D%22, model))%0A                .isExactlyInstanceOf(IllegalArgumentException.class)%0A                .hasMessageContainingAll(%22revision%22, %22$%7Bversion%7D
 %22);%0A    

Extend ModelUtilsTest with Maven CI friendly version case from #22171