Skip to content

Commit

Permalink
devonfw#223: still found wiremock tests with fixed port numbers and f…
Browse files Browse the repository at this point in the history
…ixed them to use dynamic port (devonfw#668)
  • Loading branch information
hohwille authored Oct 1, 2024
1 parent a729a9a commit d571df9
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -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/";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

/**
Expand All @@ -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)
Expand All @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 {

/**
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
{
Expand Down Expand Up @@ -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": [
{
Expand Down Expand Up @@ -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": [
{
Expand Down

0 comments on commit d571df9

Please sign in to comment.