From 89e38d72e203a80cebc470e07e7be07881ee37ac Mon Sep 17 00:00:00 2001 From: Simon Templer Date: Fri, 2 Feb 2024 11:57:50 +0100 Subject: [PATCH] test: add tests for built/published resources --- .github/workflows/check-update.yml | 17 ++++- build.gradle | 11 +++ settings.gradle | 1 + test/build.gradle | 48 +++++++++++++ .../OfflineResourcesTest.java | 71 +++++++++++++++++++ 5 files changed, 146 insertions(+), 2 deletions(-) create mode 100644 test/build.gradle create mode 100644 test/src/test/java/to.wetransform.offlineresources/OfflineResourcesTest.java diff --git a/.github/workflows/check-update.yml b/.github/workflows/check-update.yml index 8d938e8e..cd961b41 100644 --- a/.github/workflows/check-update.yml +++ b/.github/workflows/check-update.yml @@ -23,8 +23,21 @@ jobs: - name: Download resources and build Jars run: | - ./gradlew downloads jars - # TODO also test publishing (e.g. to local maven repo) + ./gradlew downloads jars publishJarsToMavenLocal :test:test + + - name: Publish Test Report + uses: mikepenz/action-junit-report@v3 + if: always() # always run even if the previous step fails + with: + report_paths: "test/build/test-results/**/*.xml" + require_tests: true + + # Workaround for check that is additionally created being associated + # to the wrong workflow/run. Instead not additional check is created. + # See https://github.com/mikepenz/action-junit-report/issues/40 + annotate_only: true + detailed_summary: true + fail_on_failure: true - name: Add git status to summary run: | diff --git a/build.gradle b/build.gradle index ce3b57a0..b0a48df2 100644 --- a/build.gradle +++ b/build.gradle @@ -642,6 +642,17 @@ task('publishJars', type: GradleBuild) { tasks = ['publish'] } +/** + * Task that publishes all Jars to the local maven repository. + */ +task('publishJarsToMavenLocal', type: GradleBuild) { + buildFile = file('publish.gradle') + + startParameter.projectProperties.publications = jarFolder.getAbsolutePath() + + tasks = ['publishToMavenLocal'] +} + /** * Task for creating resource bundles used by hale */ diff --git a/settings.gradle b/settings.gradle index c1c7df50..3f09dafa 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1 +1,2 @@ rootProject.name = 'offline-resources' +include 'test' diff --git a/test/build.gradle b/test/build.gradle new file mode 100644 index 00000000..e62f1f9d --- /dev/null +++ b/test/build.gradle @@ -0,0 +1,48 @@ +plugins { + id 'java' +} + +/* + * Dependencies + */ + +repositories { + mavenLocal() // resources to be tested should come from here + + mavenCentral() + + maven { + url 'https://artifactory.wetransform.to/artifactory/local' + } + + maven { // wetransform internal repository (for offline resources API) + url 'https://artifactory.wetransform.to/artifactory/private' + credentials { + username project.hasProperty('wetfArtifactoryUser') ? wetfArtifactoryUser : '' + password project.hasProperty('wetfArtifactoryPassword') ? wetfArtifactoryPassword : '' + } + } +} + +dependencies { + implementation 'to.wetransform:offline-resources-api:1.0.0' + implementation group: 'org.slf4j', name: 'slf4j-api', version: '1.7.25' + + // Testing + testImplementation 'org.junit.jupiter:junit-jupiter-api:5.10.1' + testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.10.1' + + // Offline resources for testing + // Always test current versions + testRuntimeOnly 'to.wetransform.offline-resources:www.w3.org:CURRENT-SNAPSHOT' + testRuntimeOnly 'to.wetransform.offline-resources:inspire.ec.europa.eu:CURRENT-SNAPSHOT' +} + +test { + useJUnitPlatform() +} + +configurations.all { + // ensure SNAPSHOTs are updated every time if needed + resolutionStrategy.cacheChangingModulesFor 0, 'seconds' +} diff --git a/test/src/test/java/to.wetransform.offlineresources/OfflineResourcesTest.java b/test/src/test/java/to.wetransform.offlineresources/OfflineResourcesTest.java new file mode 100644 index 00000000..8d0872d8 --- /dev/null +++ b/test/src/test/java/to.wetransform.offlineresources/OfflineResourcesTest.java @@ -0,0 +1,71 @@ +package to.wetransform.offlineresources; + +import org.junit.jupiter.api.Test; +import to.wetransform.offlineresources.api.OfflineResources; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.URI; +import java.nio.charset.StandardCharsets; +import java.util.function.Supplier; + +import static org.junit.jupiter.api.Assertions.*; + +public class OfflineResourcesTest { + + @Test + public void testLoadXmlSchema() throws IOException { + testLoadLocation(URI.create( + "https://www.w3.org/2001/XMLSchema.xsd" + )); + } + + @Test + public void testLoadInspireHyP4() throws IOException { + testLoadLocation(URI.create( + "https://inspire.ec.europa.eu/schemas/hy-p/4.0/HydroPhysicalWaters.xsd" + )); + } + + /** + * Test workaround for missing schemas in INSPIRE repository that are compensated with redirects on the INSPIRE website. + * + * See https://github.com/halestudio/hale-platform/blob/1e7e00c9684c9043c98a857338fd1ccfee05541b/modules/resources/build.gradle#L157 + */ + @Test + public void testLoadInspireRedirectWorkaround() throws IOException { + testLoadLocation(URI.create( + "https://inspire.ec.europa.eu/schemas/lc/0.0/LandCover.xsd" + )); + + testLoadLocation(URI.create( + "https://inspire.ec.europa.eu/schemas/wfd/0.0/WaterFrameworkDirective.xsd" + )); + } + + @Test + public void testLoadNonExistentFail() throws IOException { + assertThrows(Exception.class, () -> { + testLoadLocation(URI.create( + "https://example.com/example.xsd" + )); + }); + } + + // helpers + + private void testLoadLocation(URI location) throws IOException { + OfflineResources or = new OfflineResources(); + + Supplier input = or.resolve(location); + + try (InputStream in = input.get(); BufferedReader reader = new BufferedReader( + new InputStreamReader(in, StandardCharsets.UTF_8))) { + // consume content + reader.lines(); + } + } + +}