diff --git a/cli/src/test/java/com/devonfw/tools/ide/tool/java/JavaUrlUpdaterMock.java b/cli/src/test/java/com/devonfw/tools/ide/tool/java/JavaUrlUpdaterMock.java index 2d36a96e3..a69777927 100644 --- a/cli/src/test/java/com/devonfw/tools/ide/tool/java/JavaUrlUpdaterMock.java +++ b/cli/src/test/java/com/devonfw/tools/ide/tool/java/JavaUrlUpdaterMock.java @@ -1,21 +1,29 @@ package com.devonfw.tools.ide.tool.java; +import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo; + /** * Mock of {@link JavaUrlUpdater} to allow integration testing with wiremock. */ public class JavaUrlUpdaterMock extends JavaUrlUpdater { - private final static String TEST_URL = "http://localhost:8080/"; + private final String baseUrl; + + JavaUrlUpdaterMock(WireMockRuntimeInfo wireMockRuntimeInfo) { + + super(); + this.baseUrl = wireMockRuntimeInfo.getHttpBaseUrl(); + } @Override protected String getMirror() { - return TEST_URL + "downloads/"; + return this.baseUrl + "/downloads/"; } @Override protected String doGetVersionUrl() { - return TEST_URL + "versions/"; + return this.baseUrl + "/versions/"; } } diff --git a/cli/src/test/java/com/devonfw/tools/ide/tool/java/JavaUrlUpdaterTest.java b/cli/src/test/java/com/devonfw/tools/ide/tool/java/JavaUrlUpdaterTest.java index 1fac6eb55..bd89e5285 100644 --- a/cli/src/test/java/com/devonfw/tools/ide/tool/java/JavaUrlUpdaterTest.java +++ b/cli/src/test/java/com/devonfw/tools/ide/tool/java/JavaUrlUpdaterTest.java @@ -17,12 +17,13 @@ import com.devonfw.tools.ide.tool.npm.NpmUrlUpdater; import com.devonfw.tools.ide.url.model.folder.UrlRepository; import com.devonfw.tools.ide.url.updater.JsonUrlUpdater; +import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo; import com.github.tomakehurst.wiremock.junit5.WireMockTest; /** * Test class for integrations of the {@link NpmUrlUpdater} */ -@WireMockTest(httpPort = 8080) +@WireMockTest public class JavaUrlUpdaterTest extends Assertions { /** @@ -37,10 +38,11 @@ public class JavaUrlUpdaterTest extends Assertions { * Test of {@link JsonUrlUpdater} for the creation of {@link JavaUrlUpdater} download URLs and checksums. * * @param tempDir Path to a temporary directory + * @param wmRuntimeInfo the {@link WireMockRuntimeInfo}. * @throws IOException test fails */ @Test - public void testJavaUrlUpdaterCreatesDownloadUrlsAndChecksums(@TempDir Path tempDir) throws IOException { + public void testJavaUrlUpdaterCreatesDownloadUrlsAndChecksums(@TempDir Path tempDir, WireMockRuntimeInfo wmRuntimeInfo) throws IOException { // given stubFor(get(urlMatching("/versions/")).willReturn(aResponse().withStatus(200) @@ -49,8 +51,7 @@ public void testJavaUrlUpdaterCreatesDownloadUrlsAndChecksums(@TempDir Path temp stubFor(any(urlMatching("/downloads/.*")).willReturn(aResponse().withStatus(200).withBody("aBody"))); UrlRepository urlRepository = UrlRepository.load(tempDir); - JavaUrlUpdaterMock updater = new JavaUrlUpdaterMock(); - + JavaUrlUpdaterMock updater = new JavaUrlUpdaterMock(wmRuntimeInfo); // when updater.update(urlRepository); diff --git a/cli/src/test/java/com/devonfw/tools/ide/tool/npm/NpmJsonUrlUpdaterTest.java b/cli/src/test/java/com/devonfw/tools/ide/tool/npm/NpmJsonUrlUpdaterTest.java index c1a36b687..0c740bc98 100644 --- a/cli/src/test/java/com/devonfw/tools/ide/tool/npm/NpmJsonUrlUpdaterTest.java +++ b/cli/src/test/java/com/devonfw/tools/ide/tool/npm/NpmJsonUrlUpdaterTest.java @@ -7,6 +7,7 @@ import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching; import java.io.IOException; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; @@ -16,12 +17,13 @@ import com.devonfw.tools.ide.url.model.folder.UrlRepository; import com.devonfw.tools.ide.url.updater.JsonUrlUpdater; +import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo; import com.github.tomakehurst.wiremock.junit5.WireMockTest; /** * Test class for integrations of the {@link NpmUrlUpdater} */ -@WireMockTest(httpPort = 8080) +@WireMockTest public class NpmJsonUrlUpdaterTest extends Assertions { /** @@ -36,50 +38,57 @@ public class NpmJsonUrlUpdaterTest extends Assertions { * Test of {@link JsonUrlUpdater} for the creation of {@link NpmUrlUpdater} download URLs and checksums. * * @param tempDir Path to a temporary directory + * @param wmRuntimeInfo the {@link WireMockRuntimeInfo}. * @throws IOException test fails */ @Test - public void testNpmJsonUrlUpdaterCreatesDownloadUrlsAndChecksums(@TempDir Path tempDir) throws IOException { + public void testNpmJsonUrlUpdaterCreatesDownloadUrlsAndChecksums(@TempDir Path tempDir, WireMockRuntimeInfo wmRuntimeInfo) throws IOException { // given stubFor(get(urlMatching("/npm")).willReturn( - aResponse().withStatus(200).withBody(Files.readAllBytes(Path.of(TEST_DATA_ROOT).resolve("npm-version.json"))))); + aResponse().withStatus(200).withBody(getJsonBody(wmRuntimeInfo)))); stubFor(any(urlMatching("/npm/-/npm-[1-9.]*.tgz")).willReturn(aResponse().withStatus(200).withBody("aBody"))); UrlRepository urlRepository = UrlRepository.load(tempDir); - NpmUrlUpdaterMock updater = new NpmUrlUpdaterMock(); + NpmUrlUpdaterMock updater = new NpmUrlUpdaterMock(wmRuntimeInfo); // when updater.update(urlRepository); - Path NpmVersionsPath = tempDir.resolve("npm").resolve("npm").resolve("1.2.32"); + Path npmVersionsPath = tempDir.resolve("npm").resolve("npm").resolve("1.2.32"); // then - assertThat(NpmVersionsPath.resolve("status.json")).exists(); - assertThat(NpmVersionsPath.resolve("urls")).exists(); - assertThat(NpmVersionsPath.resolve("urls.sha256")).exists(); + assertThat(npmVersionsPath.resolve("status.json")).exists(); + assertThat(npmVersionsPath.resolve("urls")).exists(); + assertThat(npmVersionsPath.resolve("urls.sha256")).exists(); } + private static byte[] getJsonBody(WireMockRuntimeInfo wmRuntimeInfo) throws IOException { + Path jsonFile = Path.of(TEST_DATA_ROOT).resolve("npm-version.json"); + return Files.readString(jsonFile).replace("${testbaseurl}", wmRuntimeInfo.getHttpBaseUrl()).getBytes(StandardCharsets.UTF_8); + } + /** * Test if the {@link JsonUrlUpdater} for {@link NpmUrlUpdater} for a non-existent version does successfully not create a download folder. * * @param tempDir Path to a temporary directory + * @param wmRuntimeInfo the {@link WireMockRuntimeInfo}. * @throws IOException test fails */ @Test - public void testNpmJsonUrlUpdaterWithMissingDownloadsDoesNotCreateVersionFolder(@TempDir Path tempDir) + public void testNpmJsonUrlUpdaterWithMissingDownloadsDoesNotCreateVersionFolder(@TempDir Path tempDir, WireMockRuntimeInfo wmRuntimeInfo) throws IOException { // given stubFor(get(urlMatching("/npm")).willReturn( - aResponse().withStatus(200).withBody(Files.readAllBytes(Path.of(TEST_DATA_ROOT).resolve("npm-version.json"))))); + aResponse().withStatus(200).withBody(getJsonBody(wmRuntimeInfo)))); stubFor(any(urlMatching("/npm/-/npm-[1-9.]*.tgz")).willReturn(aResponse().withStatus(200).withBody("aBody"))); UrlRepository urlRepository = UrlRepository.load(tempDir); - NpmUrlUpdaterMock updater = new NpmUrlUpdaterMock(); + NpmUrlUpdaterMock updater = new NpmUrlUpdaterMock(wmRuntimeInfo); // when updater.update(urlRepository); @@ -95,19 +104,20 @@ public void testNpmJsonUrlUpdaterWithMissingDownloadsDoesNotCreateVersionFolder( * Test if the {@link JsonUrlUpdater} for {@link NpmUrlUpdater} can handle filtering of versions. * * @param tempDir Path to a temporary directory + * @param wmRuntimeInfo the {@link WireMockRuntimeInfo}. * @throws IOException test fails */ @Test - public void testNpmJsonUrlUpdaterFilteredVersionCreateVersionFolder(@TempDir Path tempDir) throws IOException { + public void testNpmJsonUrlUpdaterFilteredVersionCreateVersionFolder(@TempDir Path tempDir, WireMockRuntimeInfo wmRuntimeInfo) throws IOException { // given stubFor(get(urlMatching("/npm")).willReturn( - aResponse().withStatus(200).withBody(Files.readAllBytes(Path.of(TEST_DATA_ROOT).resolve("npm-version.json"))))); + aResponse().withStatus(200).withBody(getJsonBody(wmRuntimeInfo)))); stubFor(any(urlMatching("/npm/-/npm-[1-9.]*.tgz")).willReturn(aResponse().withStatus(200).withBody("aBody"))); UrlRepository urlRepository = UrlRepository.load(tempDir); - NpmUrlUpdaterMock updater = new NpmUrlUpdaterMock(); + NpmUrlUpdaterMock updater = new NpmUrlUpdaterMock(wmRuntimeInfo); // when updater.update(urlRepository); @@ -124,19 +134,20 @@ public void testNpmJsonUrlUpdaterFilteredVersionCreateVersionFolder(@TempDir Pat * checksum was provided) * * @param tempDir Path to a temporary directory + * @param wmRuntimeInfo the {@link WireMockRuntimeInfo}. * @throws IOException test fails */ @Test - public void testNpmJsonUrlUpdaterGeneratesChecksum(@TempDir Path tempDir) throws IOException { + public void testNpmJsonUrlUpdaterGeneratesChecksum(@TempDir Path tempDir, WireMockRuntimeInfo wmRuntimeInfo) throws IOException { // given stubFor(get(urlMatching("/npm")).willReturn( - aResponse().withStatus(200).withBody(Files.readAllBytes(Path.of(TEST_DATA_ROOT).resolve("npm-version.json"))))); + aResponse().withStatus(200).withBody(getJsonBody(wmRuntimeInfo)))); stubFor(any(urlMatching("/npm/-/npm-[1-9.]*.tgz")).willReturn(aResponse().withStatus(200).withBody("aBody"))); UrlRepository urlRepository = UrlRepository.load(tempDir); - NpmUrlUpdaterMock updater = new NpmUrlUpdaterMock(); + NpmUrlUpdaterMock updater = new NpmUrlUpdaterMock(wmRuntimeInfo); // when updater.update(urlRepository); diff --git a/cli/src/test/java/com/devonfw/tools/ide/tool/npm/NpmUrlUpdaterMock.java b/cli/src/test/java/com/devonfw/tools/ide/tool/npm/NpmUrlUpdaterMock.java index 0ce3a8a8a..c54b024af 100644 --- a/cli/src/test/java/com/devonfw/tools/ide/tool/npm/NpmUrlUpdaterMock.java +++ b/cli/src/test/java/com/devonfw/tools/ide/tool/npm/NpmUrlUpdaterMock.java @@ -1,15 +1,22 @@ package com.devonfw.tools.ide.tool.npm; +import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo; + /** * Mock of {@link NpmUrlUpdater} to allow integration testing with wiremock. */ public class NpmUrlUpdaterMock extends NpmUrlUpdater { - private final static String TEST_URL = "http://localhost:8080/"; + private final String baseUrl; + + NpmUrlUpdaterMock(WireMockRuntimeInfo wireMockRuntimeInfo) { + super(); + this.baseUrl = wireMockRuntimeInfo.getHttpBaseUrl() + "/"; + } @Override protected String getBaseUrl() { - return TEST_URL; + return this.baseUrl; } } diff --git a/cli/src/test/resources/integrationtest/NpmJsonUrlUpdater/npm-version.json b/cli/src/test/resources/integrationtest/NpmJsonUrlUpdater/npm-version.json index 9bd7b0e0c..2a7404c04 100644 --- a/cli/src/test/resources/integrationtest/NpmJsonUrlUpdater/npm-version.json +++ b/cli/src/test/resources/integrationtest/NpmJsonUrlUpdater/npm-version.json @@ -36,7 +36,7 @@ ], "dist": { "shasum": "", - "tarball": "http://localhost:8080/npm/-/npm-1.1.25.tgz", + "tarball": "${testbaseurl}/npm/-/npm-1.1.25.tgz", "integrity": "", "signatures": [ { @@ -126,7 +126,7 @@ ], "dist": { "shasum": "", - "tarball": "http://localhost:8080/npm/-/npm-1.2.32.tgz", + "tarball": "${testbaseurl}/npm/-/npm-1.2.32.tgz", "integrity": "", "signatures": [ { @@ -214,7 +214,7 @@ ], "dist": { "shasum": "", - "tarball": "http://localhost:8080/npm/-/npm-2.0.0-beta.0.tgz", + "tarball": "${testbaseurl}/npm/-/npm-2.0.0-beta.0.tgz", "integrity": "", "signatures": [ {