//
// Copyright (c) 2016, 2022 Contributors to the Eclipse Foundation
//
// See the NOTICE file(s) distributed with this work for additional
// information regarding copyright ownership.
//
// Licensed 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.
//

= Running the Microprofile Metrics TCK

Any implementation claiming Microprofile Metrics 5.0 compatibility must pass this test suite.
The TCK uses `junit`.

If an implementation supports non-mandatory features defined in the specification then the optional TCK
must be executed to validate compliance.

The optional TCK currently validates the following non-mandatory features:

- REST stat metrics
- Base metrics

== Dependencies

To enable the tests in your Arquillian based test project you need to add the following dependencies:

[source, xml]
----

<!-- versions should correspond to the value specified in the MicroProfile Parent
pom.xml and MicroProfile Parent TCK-BOM pom.xml used by the specified version of MicroProfile Metrics -->
<properties>
    <microprofile.metrics.version>5.0.0</microprofile.metrics.version>
    <jakartaee.coreprofile.version>10.0.0</jakartaee.coreprofile.version>
    <junit.version>4.13.2</junit.version>
    <arquillian.version>1.7.0.Alpha12</arquillian.version>
    <hamcrest.version>1.3</hamcrest.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.eclipse.microprofile.metrics</groupId>
        <artifactId>microprofile-metrics-api</artifactId>
        <version>${microprofile.metrics.version}</version>
    </dependency>

    <dependency>
        <groupId>org.eclipse.microprofile.metrics</groupId>
        <artifactId>microprofile-metrics-rest-tck</artifactId>
        <version>${microprofile.metrics.version}</version>
    </dependency>

    <dependency>
        <groupId>org.eclipse.microprofile.metrics</groupId>
        <artifactId>microprofile-metrics-api-tck</artifactId>
        <version>${microprofile.metrics.version}</version>
    </dependency>

    <dependency>
        <groupId>jakarta.platform</groupId>
        <artifactId>jakarta.jakartaee-core-api</artifactId>
        <version>${jakartaee.coreprofile.version}</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>${junit.version}</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.hamcrest</groupId>
        <artifactId>hamcrest-all</artifactId>
        <version>${hamcrest.version}</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.jboss.arquillian.junit</groupId>
        <artifactId>arquillian-junit-container</artifactId>
        <version>${arquillian.version}</version>
        <scope>test</scope>
    </dependency>

    <!-- You can replace this with your choice of container -->
    <dependency>
	    <groupId>io.openliberty.arquillian</groupId>
        <artifactId>arquillian-liberty-managed-jakarta</artifactId>
        <version>2.1.0</version>
        <scope>test</scope>
    </dependency>
</dependencies>

----

=== Additional dependencies for Optional TCK

If running the optional TCK module you will need the below *additional* dependencies:

[source, xml]
----

    <dependency>
        <groupId>org.eclipse.microprofile.metrics</groupId>
        <artifactId>microprofile-metrics-optional-tck</artifactId>
        <version>${microprofile.metrics.version}</version>
    </dependency>

----
== Declaring the Tests to run in Apache Maven pom.xml

If you use Apache Maven, then the tests are run via the `maven-surefire-plugin` by adding the following in your pom.xml.
[source, xml]
----
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.19.1</version>
            <configuration>
                <dependenciesToScan>
                    <dependency>org.eclipse.microprofile.metrics:microprofile-metrics-api-tck</dependency>
                    <dependency>org.eclipse.microprofile.metrics:microprofile-metrics-rest-tck</dependency>
                </dependenciesToScan>
                <environmentVariables>
                    <MP_METRICS_TAGS>tier=integration</MP_METRICS_TAGS>
                </environmentVariables>

            </configuration>
        </plugin>
    </plugins>
</build>
----
Note: Be sure to set up your Arquillian.xml as required for your server under test.

If the implementation does not support all base metrics as described in the specification because
they are not available in the used JVM, it is possible to skip the tests related to base metrics
by adding the system property `skip.base.metric.tests=true` to the surefire process.


=== Additional Configuration for Optional TCKs

Add the `<dependency>org.eclipse.microprofile.metrics:microprofile-metrics-optional-tck</dependency>` element to the `<dependenciesToScan>` element:


[source, xml]
----

    <dependenciesToScan>
        <dependency>org.eclipse.microprofile.metrics:microprofile-metrics-optional-tck</dependency>
        <dependency>org.eclipse.microprofile.metrics:microprofile-metrics-api-tck</dependency>
        <dependency>org.eclipse.microprofile.metrics:microprofile-metrics-rest-tck</dependency>
    </dependenciesToScan>

----

== Running the TCK

To run the TCK, ensure that the above dependencies are installed, and run the following command:
----
# Modify the test.url variable to point to your server under test
mvn test -Dtest.url=http://localhost:9080
----

If your server under test has basic authentication enabled, run the following command:
----
# Modify the test.url variable to point to your server under test.  Modify the test.user and test.pwd variables to appropriate values for your server under test.
mvn test -Dtest.url=https://localhost:9443 -Dtest.user=someUser -Dtest.pwd=somePassword
----

If you are testing with the Optional TCK module it will require the tests to send requests to the application deployed on your server. By default the tests will use the port defined in `test.url` as your application port. You can configure a separate application port by defining the `application.port` property.
----
# Modify the application.port with your server's defined application port
mvn test -Dtest.url=https://localhost:9443 -Dtest.user=someUser -Dtest.pwd=somePassword -Dapplication.port=8080
----
