From b1493b67c96753978100cb8cab9daaa9c005cb00 Mon Sep 17 00:00:00 2001 From: moritzLanger <44358321+moritzLanger@users.noreply.github.com> Date: Fri, 5 Jan 2024 09:06:30 +0100 Subject: [PATCH] #127: Increase commandlet test coverage for InstallCommandlet (#137) --- .../ide/commandlet/InstallCommandletTest.java | 112 ++++++++++++++++++ .../__files/java-17.0.6-linux-x64.tgz | Bin 0 -> 350 bytes .../__files/java-17.0.6-windows-x64.zip | Bin 0 -> 447 bytes .../urls/java/java/17.0.6/linux_x64.sha256 | 1 + .../_ide/urls/java/java/17.0.6/linux_x64.urls | 1 + .../_ide/urls/java/java/17.0.6/mac_x64.sha256 | 1 + .../_ide/urls/java/java/17.0.6/mac_x64.urls | 1 + .../_ide/urls/java/java/17.0.6/status.json | 20 ++++ .../urls/java/java/17.0.6/windows_x64.urls | 1 + .../java/java/17.0.6/windows_x64.urls.sha256 | 1 + 10 files changed, 138 insertions(+) create mode 100644 cli/src/test/java/com/devonfw/tools/ide/commandlet/InstallCommandletTest.java create mode 100644 cli/src/test/resources/__files/java-17.0.6-linux-x64.tgz create mode 100644 cli/src/test/resources/__files/java-17.0.6-windows-x64.zip create mode 100644 cli/src/test/resources/ide-projects/_ide/urls/java/java/17.0.6/linux_x64.sha256 create mode 100644 cli/src/test/resources/ide-projects/_ide/urls/java/java/17.0.6/linux_x64.urls create mode 100644 cli/src/test/resources/ide-projects/_ide/urls/java/java/17.0.6/mac_x64.sha256 create mode 100644 cli/src/test/resources/ide-projects/_ide/urls/java/java/17.0.6/mac_x64.urls create mode 100644 cli/src/test/resources/ide-projects/_ide/urls/java/java/17.0.6/status.json create mode 100644 cli/src/test/resources/ide-projects/_ide/urls/java/java/17.0.6/windows_x64.urls create mode 100644 cli/src/test/resources/ide-projects/_ide/urls/java/java/17.0.6/windows_x64.urls.sha256 diff --git a/cli/src/test/java/com/devonfw/tools/ide/commandlet/InstallCommandletTest.java b/cli/src/test/java/com/devonfw/tools/ide/commandlet/InstallCommandletTest.java new file mode 100644 index 000000000..6633aff01 --- /dev/null +++ b/cli/src/test/java/com/devonfw/tools/ide/commandlet/InstallCommandletTest.java @@ -0,0 +1,112 @@ +package com.devonfw.tools.ide.commandlet; + +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; +import static com.github.tomakehurst.wiremock.client.WireMock.get; +import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import com.devonfw.tools.ide.context.AbstractIdeContextTest; +import com.devonfw.tools.ide.context.IdeContext; +import com.github.tomakehurst.wiremock.WireMockServer; +import com.github.tomakehurst.wiremock.core.WireMockConfiguration; + +/** + * Integration test of {@link InstallCommandlet}. + */ + +public class InstallCommandletTest extends AbstractIdeContextTest { + + private static WireMockServer server; + + private static Path resourcePath = Paths.get("src/test/resources"); + + @BeforeAll + static void setUp() throws IOException { + + server = new WireMockServer(WireMockConfiguration.wireMockConfig().port(1111)); + server.start(); + } + + @AfterAll + static void tearDown() throws IOException { + + server.shutdownServer(); + } + + private void mockWebServer() throws IOException { + + Path windowsFilePath = resourcePath.resolve("__files").resolve("java-17.0.6-windows-x64.zip"); + String windowsLength = String.valueOf(Files.size(windowsFilePath)); + server.stubFor( + get(urlPathEqualTo("/installTest/windows")).willReturn(aResponse().withHeader("Content-Type", "application/zip") + .withHeader("Content-Length", windowsLength).withStatus(200).withBodyFile("java-17.0.6-windows-x64.zip"))); + + Path linuxFilePath = resourcePath.resolve("__files").resolve("java-17.0.6-linux-x64.tgz"); + String linuxLength = String.valueOf(Files.size(linuxFilePath)); + server.stubFor( + get(urlPathEqualTo("/installTest/linux")).willReturn(aResponse().withHeader("Content-Type", "application/tgz") + .withHeader("Content-Length", linuxLength).withStatus(200).withBodyFile("java-17.0.6-linux-x64.tgz"))); + + server.stubFor( + get(urlPathEqualTo("/installTest/macOS")).willReturn(aResponse().withHeader("Content-Type", "application/tgz") + .withHeader("Content-Length", linuxLength).withStatus(200).withBodyFile("java-17.0.6-linux-x64.tgz"))); + } + + /** + * Test of {@link InstallCommandlet} run, when Installed Version is null. + */ + @Test + public void testInstallCommandletRunWithVersion() throws IOException { + + // arrange + String path = "workspaces/foo-test/my-git-repo"; + IdeContext context = newContext("basic", path, true); + InstallCommandlet install = context.getCommandletManager().getCommandlet(InstallCommandlet.class); + install.tool.setValueAsString("java"); + mockWebServer(); + // act + install.run(); + // assert + assertTestInstall(context); + } + + /** + * Test of {@link InstallCommandlet} run, when Installed Version is set. + */ + @Test + public void testInstallCommandletRunWithVersionAndVersionIdentifier() throws IOException { + + // arrange + String path = "workspaces/foo-test/my-git-repo"; + IdeContext context = newContext("basic", path, true); + InstallCommandlet install = context.getCommandletManager().getCommandlet(InstallCommandlet.class); + install.tool.setValueAsString("java"); + install.version.setValueAsString("17.0.6"); + mockWebServer(); + + // act + install.run(); + // assert + assertTestInstall(context); + } + + private void assertTestInstall(IdeContext context) { + + assertThat(context.getSoftwarePath().resolve("java")).exists(); + assertThat(context.getSoftwarePath().resolve("java/InstallTest.txt")).hasContent("This is a test file."); + assertThat(context.getSoftwarePath().resolve("java/bin/HelloWorld.txt")).hasContent("Hello World!"); + if(context.getSystemInfo().isWindows()){ + assertThat(context.getSoftwarePath().resolve("java/bin/java.cmd")).exists(); + } else if (context.getSystemInfo().isLinux() || context.getSystemInfo().isMac()) { + assertThat(context.getSoftwarePath().resolve("java/bin/java")).exists(); + } + } +} diff --git a/cli/src/test/resources/__files/java-17.0.6-linux-x64.tgz b/cli/src/test/resources/__files/java-17.0.6-linux-x64.tgz new file mode 100644 index 0000000000000000000000000000000000000000..9a592c991a7398a6ab70ce32d2793383d56293bf GIT binary patch literal 350 zcmV-k0ipgMiwFP!000001MSwaPQx$|24K%ftjtIEq-V!*UVs5uSSrC9N}#EnL~2r0 zHlBm0#SRD2(o{VAo12#t z%`DyM=idpb(eIBmd5QB7QC6*=XCFiE3CTHX;D7lp8=2j|r0mT#y&GiiQ z%oOtT6v{L6Qu51-l|Y6;oH7UK6#4x5AH{%%fG|H0d*&6FB