diff --git a/docs/modules/tkit-quarkus/nav.adoc b/docs/modules/tkit-quarkus/nav.adoc index 4af979b..5a0d7cb 100644 --- a/docs/modules/tkit-quarkus/nav.adoc +++ b/docs/modules/tkit-quarkus/nav.adoc @@ -11,4 +11,5 @@ * xref:tkit-quarkus-rest.adoc[Rest Extension] * xref:tkit-quarkus-rest-context.adoc[Rest Context Extension] * xref:tkit-quarkus-security.adoc[Rest Security] -* xref:tkit-quarkus-security-test.adoc[Rest Security Tests] \ No newline at end of file +* xref:tkit-quarkus-security-test.adoc[Rest Security Tests] +* xref:tkit-quarkus-test.adoc[Tests] \ No newline at end of file diff --git a/docs/modules/tkit-quarkus/pages/includes/tkit-quarkus-test.adoc b/docs/modules/tkit-quarkus/pages/includes/tkit-quarkus-test.adoc new file mode 100644 index 0000000..05d0952 --- /dev/null +++ b/docs/modules/tkit-quarkus/pages/includes/tkit-quarkus-test.adoc @@ -0,0 +1,114 @@ + +:summaryTableId: tkit-quarkus-test +[.configuration-legend] +icon:lock[title=Fixed at build time] Configuration property fixed at build time - All other configuration properties are overridable at runtime +[.configuration-reference.searchable, cols="80,.^10,.^10"] +|=== + +h|[[tkit-quarkus-test_configuration]]link:#tkit-quarkus-test_configuration[Configuration property] + +h|Type +h|Default + +a| [[tkit-quarkus-test_tkit-test-integration-test-enabled]]`link:#tkit-quarkus-test_tkit-test-integration-test-enabled[tkit.test.integration-test-enabled]` + + +[.description] +-- +Disable or enabled integration test flag + +ifdef::add-copy-button-to-env-var[] +Environment variable: env_var_with_copy_button:+++TKIT_TEST_INTEGRATION_TEST_ENABLED+++[] +endif::add-copy-button-to-env-var[] +ifndef::add-copy-button-to-env-var[] +Environment variable: `+++TKIT_TEST_INTEGRATION_TEST_ENABLED+++` +endif::add-copy-button-to-env-var[] +--|boolean +|`false` + + +a| [[tkit-quarkus-test_tkit-test-ci-enabled]]`link:#tkit-quarkus-test_tkit-test-ci-enabled[tkit.test.ci.enabled]` + + +[.description] +-- +Disable or enabled for ci pipeline + +ifdef::add-copy-button-to-env-var[] +Environment variable: env_var_with_copy_button:+++TKIT_TEST_CI_ENABLED+++[] +endif::add-copy-button-to-env-var[] +ifndef::add-copy-button-to-env-var[] +Environment variable: `+++TKIT_TEST_CI_ENABLED+++` +endif::add-copy-button-to-env-var[] +--|boolean +|`true` + + +a| [[tkit-quarkus-test_tkit-test-ci-env-name]]`link:#tkit-quarkus-test_tkit-test-ci-env-name[tkit.test.ci.env-name]` + + +[.description] +-- +The name of environment variable to check if we are running in the pipe. + +ifdef::add-copy-button-to-env-var[] +Environment variable: env_var_with_copy_button:+++TKIT_TEST_CI_ENV_NAME+++[] +endif::add-copy-button-to-env-var[] +ifndef::add-copy-button-to-env-var[] +Environment variable: `+++TKIT_TEST_CI_ENV_NAME+++` +endif::add-copy-button-to-env-var[] +--|string +|`GITLAB_CI` + + +a| [[tkit-quarkus-test_tkit-test-rest-assured-enabled]]`link:#tkit-quarkus-test_tkit-test-rest-assured-enabled[tkit.test.rest-assured.enabled]` + + +[.description] +-- +Disable or enabled rest-assured config. + +ifdef::add-copy-button-to-env-var[] +Environment variable: env_var_with_copy_button:+++TKIT_TEST_REST_ASSURED_ENABLED+++[] +endif::add-copy-button-to-env-var[] +ifndef::add-copy-button-to-env-var[] +Environment variable: `+++TKIT_TEST_REST_ASSURED_ENABLED+++` +endif::add-copy-button-to-env-var[] +--|boolean +|`true` + + +a| [[tkit-quarkus-test_tkit-test-rest-assured-overwrite-uri-enabled]]`link:#tkit-quarkus-test_tkit-test-rest-assured-overwrite-uri-enabled[tkit.test.rest-assured.overwrite-uri.enabled]` + + +[.description] +-- +Disable or enabled URI overwrite + +ifdef::add-copy-button-to-env-var[] +Environment variable: env_var_with_copy_button:+++TKIT_TEST_REST_ASSURED_OVERWRITE_URI_ENABLED+++[] +endif::add-copy-button-to-env-var[] +ifndef::add-copy-button-to-env-var[] +Environment variable: `+++TKIT_TEST_REST_ASSURED_OVERWRITE_URI_ENABLED+++` +endif::add-copy-button-to-env-var[] +--|boolean +|`true` + + +a| [[tkit-quarkus-test_tkit-test-rest-assured-overwrite-uri-uri]]`link:#tkit-quarkus-test_tkit-test-rest-assured-overwrite-uri-uri[tkit.test.rest-assured.overwrite-uri.uri]` + + +[.description] +-- +Overwrite the RestAssured URI with a new value. + +ifdef::add-copy-button-to-env-var[] +Environment variable: env_var_with_copy_button:+++TKIT_TEST_REST_ASSURED_OVERWRITE_URI_URI+++[] +endif::add-copy-button-to-env-var[] +ifndef::add-copy-button-to-env-var[] +Environment variable: `+++TKIT_TEST_REST_ASSURED_OVERWRITE_URI_URI+++` +endif::add-copy-button-to-env-var[] +--|string +|`http://docker` + +|=== \ No newline at end of file diff --git a/docs/modules/tkit-quarkus/pages/tkit-quarkus-test.adoc b/docs/modules/tkit-quarkus/pages/tkit-quarkus-test.adoc new file mode 100644 index 0000000..217a8c5 --- /dev/null +++ b/docs/modules/tkit-quarkus/pages/tkit-quarkus-test.adoc @@ -0,0 +1,52 @@ +== tkit-quarkus-security-test + +include::./includes/attributes.adoc[] + +Enhanced test configuration + +=== Installation + +If you want to use this extension, you need to add the `org.tkit.quarkus.lib:tkit-quarkus-test` extension first to your build file. + +For instance, with Maven, add the following dependency to your POM file: + +[source,xml,subs=attributes+] +---- + + org.tkit.quarkus.lib + tkit-quarkus-test + {project-version} + test + +---- + +=== Usage + +==== Rest-Assured URI overwrite + +To enabled URI overwrite for integration test add `tkit.test.integration-test-enabled` system property to the `maven-failsafe-plugin` plugin. + +[source,xml,subs=attributes+] +---- + + org.apache.maven.plugins + maven-failsafe-plugin + + + + integration-test + verify + + + + true + + + + + +---- + +=== Configuration + +include::includes/tkit-quarkus-test.adoc[leveloffset=+1, opts=optional] \ No newline at end of file diff --git a/docs/pom.xml b/docs/pom.xml index 01742aa..33894f9 100644 --- a/docs/pom.xml +++ b/docs/pom.xml @@ -48,6 +48,11 @@ tkit-quarkus-test-db-import-deployment ${project.version} + + org.tkit.quarkus.lib + tkit-quarkus-test + ${project.version} + @@ -88,6 +93,7 @@ tkit-quarkus-rest-context.adoc tkit-quarkus-security.adoc tkit-quarkus-security-test.adoc + tkit-quarkus-test.adoc false diff --git a/extensions/pom.xml b/extensions/pom.xml index 930f5e8..2db7971 100644 --- a/extensions/pom.xml +++ b/extensions/pom.xml @@ -25,6 +25,7 @@ test-db-import security security-test + test diff --git a/extensions/test/pom.xml b/extensions/test/pom.xml new file mode 100644 index 0000000..fcd673d --- /dev/null +++ b/extensions/test/pom.xml @@ -0,0 +1,50 @@ + + + 4.0.0 + + + org.tkit.quarkus.lib + tkit-quarkus-extensions-parent + 999-SNAPSHOT + ../pom.xml + + + tkit-quarkus-test + jar + + + + io.quarkus + quarkus-jackson + provided + + + io.quarkus + quarkus-junit5 + provided + + + io.rest-assured + rest-assured + provided + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + + + io.quarkus + quarkus-extension-processor + ${quarkus.version} + + + + + + + \ No newline at end of file diff --git a/extensions/test/src/main/java/org/tkit/quarkus/test/TestConfig.java b/extensions/test/src/main/java/org/tkit/quarkus/test/TestConfig.java new file mode 100644 index 0000000..35c01b2 --- /dev/null +++ b/extensions/test/src/main/java/org/tkit/quarkus/test/TestConfig.java @@ -0,0 +1,96 @@ +package org.tkit.quarkus.test; + +import io.quarkus.runtime.annotations.ConfigDocFilename; +import io.quarkus.runtime.annotations.ConfigPhase; +import io.quarkus.runtime.annotations.ConfigRoot; +import io.quarkus.runtime.annotations.StaticInitSafe; +import io.smallrye.config.ConfigMapping; +import io.smallrye.config.WithDefault; +import io.smallrye.config.WithName; + +@StaticInitSafe +@ConfigDocFilename("tkit-quarkus-test.adoc") +@ConfigMapping(prefix = "tkit.test") +@ConfigRoot(phase = ConfigPhase.RUN_TIME) +public interface TestConfig { + + /** + * Disable or enabled integration test flag + */ + @WithName("integration-test-enabled") + @WithDefault("false") + boolean integrationTestEnabled(); + + /** + * CI pipe configuration. + */ + @WithName("ci") + CiConfig ciConfig(); + + /** + * CI pipe configuration. + */ + interface CiConfig { + + /** + * Disable or enabled for ci pipeline + */ + @WithName("enabled") + @WithDefault("true") + boolean enabled(); + + /** + * The name of environment variable to check if we are running in the pipe. + */ + @WithName("env-name") + @WithDefault("GITLAB_CI") + String envName(); + + } + + /** + * Rest assured configuration. + */ + @WithName("rest-assured") + RestAssuredConfig restAssuredConfig(); + + /** + * Rest assured configuration. + */ + interface RestAssuredConfig { + + /** + * Disable or enabled rest-assured config. + */ + @WithName("enabled") + @WithDefault("true") + boolean enabled(); + + /** + * Rest assured overwrite URI configuration. + */ + @WithName("overwrite-uri") + RestAssuredOverwriteUriConfig uriOverwrite(); + + } + + /** + * Rest assured overwrite URI configuration. + */ + interface RestAssuredOverwriteUriConfig { + /** + * Disable or enabled URI overwrite + */ + @WithName("enabled") + @WithDefault("true") + boolean enabled(); + + /** + * Overwrite the RestAssured URI with a new value. + */ + @WithName("uri") + @WithDefault("http://docker") + String uri(); + } + +} diff --git a/extensions/test/src/main/java/org/tkit/quarkus/test/TestConfigUtil.java b/extensions/test/src/main/java/org/tkit/quarkus/test/TestConfigUtil.java new file mode 100644 index 0000000..09d1c3b --- /dev/null +++ b/extensions/test/src/main/java/org/tkit/quarkus/test/TestConfigUtil.java @@ -0,0 +1,20 @@ +package org.tkit.quarkus.test; + +import io.smallrye.config.SmallRyeConfigBuilder; + +public final class TestConfigUtil { + + private final static TestConfig TEST_CONFIG; + + static { + TEST_CONFIG = new SmallRyeConfigBuilder() + .addDefaultSources() + .withMapping(TestConfig.class) + .build() + .getConfigMapping(TestConfig.class); + } + + static public TestConfig config() { + return TEST_CONFIG; + } +} diff --git a/extensions/test/src/main/java/org/tkit/quarkus/test/callbacks/RestAssuredUriTestExecutionCallback.java b/extensions/test/src/main/java/org/tkit/quarkus/test/callbacks/RestAssuredUriTestExecutionCallback.java new file mode 100644 index 0000000..18095de --- /dev/null +++ b/extensions/test/src/main/java/org/tkit/quarkus/test/callbacks/RestAssuredUriTestExecutionCallback.java @@ -0,0 +1,48 @@ +package org.tkit.quarkus.test.callbacks; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.tkit.quarkus.test.TestConfig; +import org.tkit.quarkus.test.TestConfigUtil; + +import io.quarkus.test.junit.callback.QuarkusTestBeforeTestExecutionCallback; +import io.quarkus.test.junit.callback.QuarkusTestMethodContext; +import io.restassured.RestAssured; + +public class RestAssuredUriTestExecutionCallback implements QuarkusTestBeforeTestExecutionCallback { + + private static final Logger log = LoggerFactory.getLogger(RestAssuredUriTestExecutionCallback.class); + + @Override + public void beforeTestExecution(QuarkusTestMethodContext context) { + + TestConfig testConfig = TestConfigUtil.config(); + + if (!testConfig.restAssuredConfig().enabled()) { + log.trace("Rest-Assured configuration is disabled"); + return; + } + + if (!testConfig.restAssuredConfig().uriOverwrite().enabled()) { + log.trace("Rest-Assured URI overwrite is disabled"); + return; + } + + if (!testConfig.integrationTestEnabled()) { + log.trace("Integration tests are not enabled"); + return; + } + + if (testConfig.ciConfig().enabled()) { + log.trace("CI pipeline is enabled"); + if (!Boolean.parseBoolean(System.getenv(testConfig.ciConfig().envName()))) { + log.trace("No CI pipeline environment variable found. Env: {}", testConfig.ciConfig().envName()); + return; + } + } + + var uri = testConfig.restAssuredConfig().uriOverwrite().uri(); + log.debug("Overwriting base URI of RestAssured from '{}' to '{}'", RestAssured.baseURI, uri); + RestAssured.baseURI = uri; + } +} diff --git a/extensions/test/src/main/resources/META-INF/beans.xml b/extensions/test/src/main/resources/META-INF/beans.xml new file mode 100644 index 0000000..e69de29 diff --git a/extensions/test/src/main/resources/META-INF/services/io.quarkus.test.junit.callback.QuarkusTestBeforeTestExecutionCallback b/extensions/test/src/main/resources/META-INF/services/io.quarkus.test.junit.callback.QuarkusTestBeforeTestExecutionCallback new file mode 100644 index 0000000..36bf011 --- /dev/null +++ b/extensions/test/src/main/resources/META-INF/services/io.quarkus.test.junit.callback.QuarkusTestBeforeTestExecutionCallback @@ -0,0 +1 @@ +org.tkit.quarkus.test.callbacks.RestAssuredUriTestExecutionCallback \ No newline at end of file