<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.vaadin</groupId>
    <artifactId>observability-kit-tests</artifactId>
    <version>5.0-SNAPSHOT</version>
  </parent>
  <artifactId>observability-kit-tests-starter</artifactId>

  <packaging>jar</packaging>
  <name>Test Vaadin Observability Kit Starter on Spring Boot + Prometheus</name>
  <description>
    Integration test that boots a Spring Boot application using observability-kit-starter
    auto-configuration and verifies session / UI / request metrics are exported via the
    Actuator Prometheus endpoint.
  </description>

  <dependencies>
    <!-- Boot starter under test — provides auto-config transitively -->
    <dependency>
      <groupId>com.vaadin</groupId>
      <artifactId>observability-kit-starter</artifactId>
      <version>${project.version}</version>
    </dependency>
    <dependency>
      <groupId>com.vaadin</groupId>
      <artifactId>vaadin-spring</artifactId>
    </dependency>
    <dependency>
      <groupId>com.vaadin</groupId>
      <artifactId>flow-server</artifactId>
      <version>${flow.version}</version>
    </dependency>
    <dependency>
      <groupId>com.vaadin</groupId>
      <artifactId>flow-html-components</artifactId>
      <version>${flow.version}</version>
    </dependency>

    <!-- Spring Boot web (Tomcat) -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-webmvc</artifactId>

    </dependency>
    
    <!-- Actuator + Prometheus registry -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
      <groupId>io.micrometer</groupId>
      <artifactId>micrometer-registry-prometheus</artifactId>
    </dependency>

    <!-- JDBC + embedded H2, to exercise vaadin.db.fetch.rows end to end -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <dependency>
      <groupId>com.h2database</groupId>
      <artifactId>h2</artifactId>
    </dependency>

    <!-- Test dependencies -->
    <dependency>
      <groupId>com.vaadin</groupId>
      <artifactId>observability-kit-tests-common</artifactId>
      <version>${project.version}</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>com.vaadin</groupId>
      <artifactId>vaadin-testbench-core-junit5</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>com.vaadin</groupId>
      <artifactId>flow-html-components-testbench</artifactId>
      <version>${flow.version}</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.seleniumhq.selenium</groupId>
      <artifactId>selenium-java</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.assertj</groupId>
      <artifactId>assertj-core</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-maven-plugin</artifactId>
          <version>${spring.boot.version}</version>
        </plugin>
      </plugins>
    </pluginManagement>

    <plugins>
      <!-- Build the Vaadin frontend bundle so Boot can serve it -->
      <plugin>
        <groupId>com.vaadin</groupId>
        <artifactId>flow-maven-plugin</artifactId>
        <version>${flow.version}</version>
        <executions>
          <execution>
            <goals>
              <goal>prepare-frontend</goal>
              <goal>build-frontend</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <!-- Boot app lifecycle around integration tests -->
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <executions>
          <execution>
            <id>pre-integration-test</id>
            <goals>
              <goal>start</goal>
            </goals>
            <configuration>
              <!-- Enable the JDBC/H2 demo (DatabaseView, vaadin.db.fetch.rows)
                   for the JVM browser ITs. The native build never activates
                   this profile, so its image stays free of the DB stack. -->
              <profiles>
                <profile>db-demo</profile>
              </profiles>
            </configuration>
          </execution>
          <execution>
            <id>post-integration-test</id>
            <goals>
              <goal>stop</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <!-- Run the *IT browser tests against the running Boot app -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-failsafe-plugin</artifactId>
        <executions>
          <execution>
            <goals>
              <goal>integration-test</goal>
              <goal>verify</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

  <profiles>
    <!--
      Native-image smoke test. Builds this Spring Boot + Vaadin app as a GraalVM
      native image so we can verify the starter survives AOT: the
      ServiceLoader-discovered MetricsServiceInitListener must still be
      instantiated, the auto-configuration must still wire the binders, and the
      vaadin.* meters must still reach the Prometheus endpoint in a closed-world
      image.

      This profile only produces the native executable (bound to `package`); it
      deliberately does NOT run the browser ITs, because those run in a normal
      JVM against the *already-running* native server. The native-smoke CI
      workflow builds the image, starts it, then runs BootMetricsIT against it.

      Build the production frontend bundle alongside, so the image boots in
      production mode (no dev server, which native cannot provide):

        mvn -Pproduction,native -DskipTests package \
          -pl observability-kit-tests/observability-kit-tests-starter -am

      Requires a GraalVM JDK with native-image on the PATH.
    -->
    <profile>
      <id>native</id>
      <properties>
        <native-maven-plugin.version>0.10.6</native-maven-plugin.version>
      </properties>
      <build>
        <plugins>
          <!-- Force a production frontend build so the native image does not
               try to start a dev server at runtime. -->
          <plugin>
            <groupId>com.vaadin</groupId>
            <artifactId>flow-maven-plugin</artifactId>
            <configuration>
              <productionMode>true</productionMode>
            </configuration>
          </plugin>
          <!-- Spring AOT: generate the closed-world bean definitions and
               runtime hints consumed by native-image. -->
          <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <executions>
              <execution>
                <id>process-aot</id>
                <goals>
                  <goal>process-aot</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
          <plugin>
            <groupId>org.graalvm.buildtools</groupId>
            <artifactId>native-maven-plugin</artifactId>
            <version>${native-maven-plugin.version}</version>
            <configuration>
              <classesDirectory>${project.build.outputDirectory}</classesDirectory>
              <imageName>observability-kit-tests-starter</imageName>
              <mainClass>com.vaadin.observability.tests.starter.Application</mainClass>
              <!-- Pull in upstream (Spring, Tomcat, Micrometer, ...) reachability
                   metadata so we only have to worry about the kit's own code. -->
              <metadataRepository>
                <enabled>true</enabled>
              </metadataRepository>
            </configuration>
            <executions>
              <execution>
                <id>add-reachability-metadata</id>
                <goals>
                  <goal>add-reachability-metadata</goal>
                </goals>
              </execution>
              <execution>
                <id>build-native-image</id>
                <phase>package</phase>
                <goals>
                  <goal>compile-no-fork</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>

</project>
