Skip to content

Commit

Permalink
#223: dynamic wiremock ports (#487)
Browse files Browse the repository at this point in the history
  • Loading branch information
slskiba authored Aug 1, 2024
1 parent 0fabb7a commit 03e23ec
Show file tree
Hide file tree
Showing 19 changed files with 272 additions and 149 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@

import com.devonfw.tools.ide.context.AbstractIdeContextTest;
import com.devonfw.tools.ide.context.IdeContext;
import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo;
import com.github.tomakehurst.wiremock.junit5.WireMockTest;

/**
* Test of {@link IdeProgressBar}.
*/
@WireMockTest(httpPort = 8080)
@WireMockTest
public class IdeProgressBarTest extends AbstractIdeContextTest {

/**
Expand All @@ -26,7 +27,7 @@ public class IdeProgressBarTest extends AbstractIdeContextTest {
* @param tempDir temporary directory to use.
*/
@Test
public void testProgressBarDownloadWithValidContentLength(@TempDir Path tempDir) {
public void testProgressBarDownloadWithValidContentLength(@TempDir Path tempDir, WireMockRuntimeInfo wmRuntimeInfo) {

int maxLength = 10_000;

Expand All @@ -35,7 +36,7 @@ public void testProgressBarDownloadWithValidContentLength(@TempDir Path tempDir)

IdeContext context = newContext(tempDir);
FileAccess impl = context.getFileAccess();
impl.download("http://localhost:8080/os/windows_x64_url.tgz", tempDir.resolve("windows_x64_url.tgz"));
impl.download(wmRuntimeInfo.getHttpBaseUrl() + "/os/windows_x64_url.tgz", tempDir.resolve("windows_x64_url.tgz"));
assertThat(tempDir.resolve("windows_x64_url.tgz")).exists();
assertProgressBar(context, "Downloading", maxLength);
}
Expand Down
21 changes: 17 additions & 4 deletions cli/src/test/java/com/devonfw/tools/ide/tool/UrlUpdaterMock.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.devonfw.tools.ide.url.model.folder.UrlVersion;
import com.devonfw.tools.ide.url.updater.AbstractUrlUpdater;
import com.devonfw.tools.ide.url.updater.UrlUpdater;
import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo;

/**
* Test mock for {@link UrlUpdater} preparing multiple tool versions and distributions.
Expand All @@ -16,6 +17,18 @@ public class UrlUpdaterMock extends AbstractUrlUpdater {

private static final Set<String> versions = new HashSet<>(Arrays.asList("1.0", "1.1", "1.2"));

WireMockRuntimeInfo wmRuntimeInfo;

/**
* The constructor
*
* @param wmRuntimeInfo the {@link WireMockRuntimeInfo} holding the http url and port of the wiremock server.
*/
public UrlUpdaterMock(WireMockRuntimeInfo wmRuntimeInfo) {
super();
this.wmRuntimeInfo = wmRuntimeInfo;
}

@Override
protected String getTool() {

Expand All @@ -34,10 +47,10 @@ protected Set<String> getVersions() {

@Override
protected void addVersion(UrlVersion urlVersion) {
doAddVersion(urlVersion, "http://localhost:8080/os/windows_x64_url.tgz", WINDOWS, X64, "123");
doAddVersion(urlVersion, "http://localhost:8080/os/linux_x64_url.tgz", LINUX, X64, "123");
doAddVersion(urlVersion, "http://localhost:8080/os/mac_x64_url.tgz", MAC, X64, "123");
doAddVersion(urlVersion, "http://localhost:8080/os/mac_Arm64_url.tgz", MAC, ARM64, "123");
doAddVersion(urlVersion, this.wmRuntimeInfo.getHttpBaseUrl() + "/os/windows_x64_url.tgz", WINDOWS, X64, "123");
doAddVersion(urlVersion, this.wmRuntimeInfo.getHttpBaseUrl() + "/os/linux_x64_url.tgz", LINUX, X64, "123");
doAddVersion(urlVersion, this.wmRuntimeInfo.getHttpBaseUrl() + "/os/mac_x64_url.tgz", MAC, X64, "123");
doAddVersion(urlVersion, this.wmRuntimeInfo.getHttpBaseUrl() + "/os/mac_Arm64_url.tgz", MAC, ARM64, "123");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import com.devonfw.tools.ide.url.model.folder.UrlVersion;
import com.devonfw.tools.ide.url.updater.UrlUpdater;
import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo;

/**
* Test mock for {@link UrlUpdater} using a single tool version and distribution.
Expand All @@ -14,14 +15,24 @@ public class UrlUpdaterMockSingle extends UrlUpdaterMock {

private static final Set<String> versions = new HashSet<>(List.of("1.0"));

/**
* The constructor
*
* @param wmRuntimeInfo the {@link WireMockRuntimeInfo} holding the http url and port of the wiremock server.
*/
public UrlUpdaterMockSingle(WireMockRuntimeInfo wmRuntimeInfo) {

super(wmRuntimeInfo);
}

@Override
protected Set<String> getVersions() {
return versions;
}

@Override
protected void addVersion(UrlVersion urlVersion) {
doAddVersion(urlVersion, "http://localhost:8080/os/windows_x64_url.tgz", WINDOWS, X64, "123");
doAddVersion(urlVersion, wmRuntimeInfo.getHttpBaseUrl() + "/os/windows_x64_url.tgz", WINDOWS, X64, "123");
}

}
25 changes: 13 additions & 12 deletions cli/src/test/java/com/devonfw/tools/ide/tool/UrlUpdaterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,19 @@
import com.devonfw.tools.ide.url.model.file.json.StatusJson;
import com.devonfw.tools.ide.url.model.file.json.UrlStatus;
import com.devonfw.tools.ide.url.model.folder.UrlRepository;
import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo;
import com.github.tomakehurst.wiremock.junit5.WireMockTest;

/**
* Test of {@link com.devonfw.tools.ide.url.updater.UrlUpdater} using wiremock to simulate network downloads.
*/
@WireMockTest(httpPort = 8080)
@WireMockTest
public class UrlUpdaterTest extends AbstractUrlUpdaterTest {

/**
* Test resource location
*/
private final static String testdataRoot = "src/test/resources/integrationtest/UrlUpdaterTest";
private final static String TEST_DATA_ROOT = "src/test/resources/integrationtest/UrlUpdaterTest";

/**
* Tests if the {@link com.devonfw.tools.ide.url.updater.UrlUpdater} can automatically add a missing OS (in this case the linux_x64)
Expand All @@ -36,12 +37,12 @@ public class UrlUpdaterTest extends AbstractUrlUpdaterTest {
* @throws IOException test fails
*/
@Test
public void testUrlUpdaterMissingOsGetsAddedAutomatically(@TempDir Path tempDir) throws IOException {
public void testUrlUpdaterMissingOsGetsAddedAutomatically(@TempDir Path tempDir, WireMockRuntimeInfo wmRuntimeInfo) throws IOException {

stubFor(any(urlMatching("/os/.*")).willReturn(aResponse().withStatus(200).withBody("aBody")));

UrlRepository urlRepository = UrlRepository.load(tempDir);
UrlUpdaterMock updater = new UrlUpdaterMock();
UrlUpdaterMock updater = new UrlUpdaterMock(wmRuntimeInfo);

// when
updater.update(urlRepository);
Expand Down Expand Up @@ -72,17 +73,17 @@ public void testUrlUpdaterMissingOsGetsAddedAutomatically(@TempDir Path tempDir)
}

@Test
public void testUrlUpdaterIsNotUpdatingWhenStatusManualIsTrue(@TempDir Path tempDir) {
public void testUrlUpdaterIsNotUpdatingWhenStatusManualIsTrue(@TempDir Path tempDir, WireMockRuntimeInfo wmRuntimeInfo) {

// arrange
stubFor(any(urlMatching("/os/.*")).willReturn(aResponse().withStatus(200).withBody("aBody")));

UrlRepository urlRepository = UrlRepository.load(tempDir);
UrlUpdaterMockSingle updater = new UrlUpdaterMockSingle();
UrlUpdaterMockSingle updater = new UrlUpdaterMockSingle(wmRuntimeInfo);

// act
updater.update(urlRepository);
Path versionsPath = Path.of(testdataRoot).resolve("mocked").resolve("mocked").resolve("1.0");
Path versionsPath = Path.of(TEST_DATA_ROOT).resolve("mocked").resolve("mocked").resolve("1.0");

// assert
assertThat(versionsPath.resolve("windows_x64.urls")).doesNotExist();
Expand All @@ -100,14 +101,14 @@ public void testUrlUpdaterIsNotUpdatingWhenStatusManualIsTrue(@TempDir Path temp
* @param tempDir Temporary directory
*/
@Test
public void testUrlUpdaterStatusJsonRefreshBugStillExisting(@TempDir Path tempDir) {
public void testUrlUpdaterStatusJsonRefreshBugStillExisting(@TempDir Path tempDir, WireMockRuntimeInfo wmRuntimeInfo) {

stubFor(any(urlMatching("/os/.*")).willReturn(aResponse().withStatus(200).withBody("aBody")));

UrlRepository urlRepository = UrlRepository.load(tempDir);
UrlUpdaterMockSingle updater = new UrlUpdaterMockSingle();
UrlUpdaterMockSingle updater = new UrlUpdaterMockSingle(wmRuntimeInfo);

String statusUrl = "http://localhost:8080/os/windows_x64_url.tgz";
String statusUrl = wmRuntimeInfo.getHttpBaseUrl() + "/os/windows_x64_url.tgz";
String toolName = "mocked";
String editionName = "mocked";
String versionName = "1.0";
Expand Down Expand Up @@ -173,13 +174,13 @@ public void testUrlUpdaterStatusJsonRefreshBugStillExisting(@TempDir Path tempDi
* @param tempDir Temporary directory
*/
@Test
public void testUrlUpdaterWithTextContentTypeWillNotCreateStatusJson(@TempDir Path tempDir) {
public void testUrlUpdaterWithTextContentTypeWillNotCreateStatusJson(@TempDir Path tempDir, WireMockRuntimeInfo wmRuntimeInfo) {

// given
stubFor(any(urlMatching("/os/.*")).willReturn(aResponse().withStatus(200).withHeader("Content-Type", "text/plain").withBody("aBody")));

UrlRepository urlRepository = UrlRepository.load(tempDir);
UrlUpdaterMockSingle updater = new UrlUpdaterMockSingle();
UrlUpdaterMockSingle updater = new UrlUpdaterMockSingle(wmRuntimeInfo);

// when
updater.update(urlRepository);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,45 +11,65 @@
import java.nio.file.Path;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

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 AndroidStudioUrlUpdater}.
*/
@WireMockTest(httpPort = 8080)
@WireMockTest
public class AndroidStudioJsonUrlUpdaterTest extends Assertions {

/**
* Test resource location
*/
private final static String testdataRoot = "src/test/resources/integrationtest/AndroidStudioJsonUrlUpdater";
private final static String TEST_DATA_ROOT = "src/test/resources/integrationtest/AndroidStudioJsonUrlUpdater";

/** This is the SHA256 checksum of aBody (a placeholder body which gets returned by WireMock) */
private static final String EXPECTED_ABODY_CHECKSUM = "de08da1685e537e887fbbe1eb3278fed38aff9da5d112d96115150e8771a0f30";

private static String androidVersionJson;
private static String androidVersionWithoutChecksumJson;

/**
* Creates an android-version and android-version-without-checksum json file based on the given test resource in a temporary directory according to the http
* url and port of the {@link WireMockRuntimeInfo}.
*
* @param wmRuntimeInfo wireMock server on a random port
* @throws IOException
*/
@BeforeAll
public static void setupTestVersionJson(WireMockRuntimeInfo wmRuntimeInfo) throws IOException {
//preparing test data with dynamic port
androidVersionJson = Files.readString(Path.of(TEST_DATA_ROOT).resolve("android-version.json"));
androidVersionJson = androidVersionJson.replaceAll("\\$\\{testbaseurl}", wmRuntimeInfo.getHttpBaseUrl());

androidVersionWithoutChecksumJson = Files.readString(Path.of(TEST_DATA_ROOT).resolve("android-version-without-checksum.json"));
androidVersionWithoutChecksumJson = androidVersionWithoutChecksumJson.replaceAll("\\$\\{testbaseurl}", wmRuntimeInfo.getHttpBaseUrl());
}

/**
* Test of {@link JsonUrlUpdater} for the creation of {@link AndroidStudioUrlUpdater} to download URLs and checksums.
*
* @param tempDir Path to a temporary directory
* @throws IOException test fails
* @param wmRuntimeInfo wireMock server on a random port
*/
@Test
public void testJsonUrlUpdaterCreatesDownloadUrlsAndChecksums(@TempDir Path tempDir) throws IOException {
public void testJsonUrlUpdaterCreatesDownloadUrlsAndChecksums(@TempDir Path tempDir, WireMockRuntimeInfo wmRuntimeInfo) {

// given
stubFor(get(urlMatching("/android-studio-releases-list.*")).willReturn(aResponse().withStatus(200)
.withBody(Files.readAllBytes(Path.of(testdataRoot).resolve("android-version.json")))));
stubFor(get(urlMatching("/android-studio-releases-list.*")).willReturn(aResponse().withStatus(200).withBody(androidVersionJson.getBytes())));

stubFor(any(urlMatching("/edgedl/android/studio/ide-zips.*"))
.willReturn(aResponse().withStatus(200).withBody("aBody")));
stubFor(any(urlMatching("/edgedl/android/studio/ide-zips.*")).willReturn(aResponse().withStatus(200).withBody("aBody")));

UrlRepository urlRepository = UrlRepository.load(tempDir);
AndroidStudioUrlUpdaterMock updater = new AndroidStudioUrlUpdaterMock();
AndroidStudioUrlUpdaterMock updater = new AndroidStudioUrlUpdaterMock(wmRuntimeInfo);

// when
updater.update(urlRepository);
Expand All @@ -73,20 +93,18 @@ public void testJsonUrlUpdaterCreatesDownloadUrlsAndChecksums(@TempDir Path temp
* Test if {@link AndroidStudioUrlUpdater} can handle downloads with missing checksums (generate checksum from download file if no checksum was provided)
*
* @param tempDir Path to a temporary directory
* @throws IOException test fails
* @param wmRuntimeInfo wireMock server on a random port
*/
@Test
public void testJsonUrlUpdaterWithMissingDownloadsDoesNotCreateVersionFolder(@TempDir Path tempDir)
throws IOException {
public void testJsonUrlUpdaterWithMissingDownloadsDoesNotCreateVersionFolder(@TempDir Path tempDir, WireMockRuntimeInfo wmRuntimeInfo) {

// given
stubFor(get(urlMatching("/android-studio-releases-list.*")).willReturn(aResponse().withStatus(200)
.withBody(Files.readAllBytes(Path.of(testdataRoot).resolve("android-version.json")))));
stubFor(get(urlMatching("/android-studio-releases-list.*")).willReturn(aResponse().withStatus(200).withBody(androidVersionJson.getBytes())));

stubFor(get(urlMatching("/edgedl/android/studio/ide-zips.*")).willReturn(aResponse().withStatus(404)));

UrlRepository urlRepository = UrlRepository.load(tempDir);
AndroidStudioUrlUpdaterMock updater = new AndroidStudioUrlUpdaterMock();
AndroidStudioUrlUpdaterMock updater = new AndroidStudioUrlUpdaterMock(wmRuntimeInfo);

// when
updater.update(urlRepository);
Expand All @@ -103,29 +121,26 @@ public void testJsonUrlUpdaterWithMissingDownloadsDoesNotCreateVersionFolder(@Te
* no checksum was provided)
*
* @param tempDir Path to a temporary directory
* @throws IOException test fails
* @param wmRuntimeInfo wireMock server on a random port
*/
@Test
public void testJsonUrlUpdaterWithMissingChecksumGeneratesChecksum(@TempDir Path tempDir) throws IOException {
public void testJsonUrlUpdaterWithMissingChecksumGeneratesChecksum(@TempDir Path tempDir, WireMockRuntimeInfo wmRuntimeInfo) {

// given
stubFor(get(urlMatching("/android-studio-releases-list.*")).willReturn(aResponse().withStatus(200)
.withBody(Files.readAllBytes(Path.of(testdataRoot).resolve("android-version-without-checksum.json")))));
stubFor(get(urlMatching("/android-studio-releases-list.*")).willReturn(aResponse().withStatus(200).withBody(androidVersionWithoutChecksumJson.getBytes())));

stubFor(any(urlMatching("/edgedl/android/studio/ide-zips.*"))
.willReturn(aResponse().withStatus(200).withBody("aBody")));
stubFor(any(urlMatching("/edgedl/android/studio/ide-zips.*")).willReturn(aResponse().withStatus(200).withBody("aBody")));

UrlRepository urlRepository = UrlRepository.load(tempDir);
AndroidStudioUrlUpdaterMock updater = new AndroidStudioUrlUpdaterMock();
AndroidStudioUrlUpdaterMock updater = new AndroidStudioUrlUpdaterMock(wmRuntimeInfo);

// when
updater.update(urlRepository);

Path androidStudioVersionsPath = tempDir.resolve("android-studio").resolve("android-studio").resolve("2023.1.1.2");

// then
assertThat(androidStudioVersionsPath.resolve("windows_x64.urls.sha256")).exists()
.hasContent(EXPECTED_ABODY_CHECKSUM);
assertThat(androidStudioVersionsPath.resolve("windows_x64.urls.sha256")).exists().hasContent(EXPECTED_ABODY_CHECKSUM);

}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
package com.devonfw.tools.ide.tool.androidstudio;

import com.devonfw.tools.ide.url.updater.JsonUrlUpdater;
import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo;

/**
* {@link JsonUrlUpdater} test mock for Android Studio.
*/
public class AndroidStudioUrlUpdaterMock extends AndroidStudioUrlUpdater {

/** The base URL used for WireMock */
private final static String TEST_BASE_URL = "http://localhost:8080";
WireMockRuntimeInfo wmRuntimeInfo;

/**
* The constructor
*
* @param wmRuntimeInfo the {@link WireMockRuntimeInfo} holding the http url and port of the wiremock server.
*/
public AndroidStudioUrlUpdaterMock(WireMockRuntimeInfo wmRuntimeInfo) {
super();
this.wmRuntimeInfo = wmRuntimeInfo;
}

@Override
protected String getVersionBaseUrl() {

return TEST_BASE_URL;
return wmRuntimeInfo.getHttpBaseUrl();
}
}
Loading

0 comments on commit 03e23ec

Please sign in to comment.