From 154d0b2b4637d6b8aa200bf865dce26866e94715 Mon Sep 17 00:00:00 2001 From: Aurora Lahtela <24460436+AuroraLS3@users.noreply.github.com> Date: Sat, 25 Feb 2023 12:17:30 +0200 Subject: [PATCH] Refactored FullSystemExtension to be easier to maintain --- .../delivery/webserver/PublicHtmlTest.java | 19 +++- .../java/extension/FullSystemExtension.java | 88 ++++++++----------- 2 files changed, 54 insertions(+), 53 deletions(-) diff --git a/Plan/common/src/test/java/com/djrapitops/plan/delivery/webserver/PublicHtmlTest.java b/Plan/common/src/test/java/com/djrapitops/plan/delivery/webserver/PublicHtmlTest.java index 7022fa153..552c54573 100644 --- a/Plan/common/src/test/java/com/djrapitops/plan/delivery/webserver/PublicHtmlTest.java +++ b/Plan/common/src/test/java/com/djrapitops/plan/delivery/webserver/PublicHtmlTest.java @@ -22,6 +22,8 @@ import com.djrapitops.plan.settings.config.PlanConfig; import extension.FullSystemExtension; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; @@ -51,7 +53,8 @@ class PublicHtmlTest { system.disable(); } - @ParameterizedTest + @DisplayName("Public Html supports file types") + @ParameterizedTest(name = "Supports file type {0}") @CsvSource({ "avif", "bin", "bmp", "css", "csv", "eot", @@ -77,6 +80,20 @@ class PublicHtmlTest { assertEquals(404, access(address + "/does-not-exist-" + fileName)); } + @Test + @DisplayName("Public Html doesn't support some file types") + void customFileTypeNotSupported(PlanConfig config, DeliveryUtilities deliveryUtilities) throws Exception { + String fileName = "test.mp4"; + Path publicHtmlDirectory = config.getResourceSettings().getPublicHtmlDirectory(); + Path file = publicHtmlDirectory.resolve(fileName); + Files.createDirectories(publicHtmlDirectory); + Files.write(file, new byte[]{1, 1, 1, 1}); + String address = deliveryUtilities.getAddresses().getAccessAddress() + .orElse(deliveryUtilities.getAddresses().getFallbackLocalhostAddress()); + + assertEquals(404, access(address + "/" + fileName)); + } + private int access(String address) throws Exception { HttpURLConnection connection = null; try { diff --git a/Plan/common/src/test/java/extension/FullSystemExtension.java b/Plan/common/src/test/java/extension/FullSystemExtension.java index fb123f450..380d72b96 100644 --- a/Plan/common/src/test/java/extension/FullSystemExtension.java +++ b/Plan/common/src/test/java/extension/FullSystemExtension.java @@ -26,6 +26,7 @@ import com.djrapitops.plan.settings.config.paths.WebserverSettings; import com.djrapitops.plan.storage.database.Database; import com.djrapitops.plan.storage.file.PlanFiles; import com.djrapitops.plan.storage.file.PublicHtmlFiles; +import com.djrapitops.plan.utilities.java.Maps; import javassist.tools.web.Webserver; import org.junit.jupiter.api.extension.*; import utilities.RandomData; @@ -36,6 +37,8 @@ import java.io.IOException; import java.io.UncheckedIOException; import java.nio.file.Files; import java.nio.file.Path; +import java.util.Map; +import java.util.function.Supplier; /** * JUnit 5 extension to construct a full PlanSystem for a test. @@ -49,6 +52,36 @@ public class FullSystemExtension implements ParameterResolver, BeforeAllCallback private Path tempDir; private PlanSystem planSystem; + private final Map parameterResolvers; + + public FullSystemExtension() { + this.parameterResolvers = Maps.builder(Class.class, Supplier.class) + .put(PlanSystem.class, () -> planSystem) + .put(PlanFiles.class, () -> planSystem.getPlanFiles()) + .put(PlanConfig.class, () -> planSystem.getConfigSystem().getConfig()) + .put(ServerUUID.class, () -> planSystem.getServerInfo().getServerUUID()) + .put(PlanPluginComponent.class, () -> { + try { + return component.getComponent(); + } catch (Exception e) { + throw new ParameterResolutionException("Error getting " + PlanPluginComponent.class, e); + } + }) + .put(PlanCommand.class, () -> { + try { + return component.getComponent().planCommand(); + } catch (Exception e) { + throw new ParameterResolutionException("Error getting " + PlanCommand.class, e); + } + }) + .put(Database.class, () -> planSystem.getDatabaseSystem().getDatabase()) + .put(DeliveryUtilities.class, () -> planSystem.getDeliveryUtilities()) + .put(PublicHtmlFiles.class, () -> planSystem.getDeliveryUtilities().getPublicHtmlFiles()) + .put(Webserver.class, () -> planSystem.getWebServerSystem().getWebServer()) + .put(Exporter.class, () -> planSystem.getExportSystem().getExporter()) + .build(); + } + @Override public void beforeAll(ExtensionContext context) throws Exception { tempDir = Files.createTempDirectory("plan-fullsystem-test"); @@ -82,63 +115,14 @@ public class FullSystemExtension implements ParameterResolver, BeforeAllCallback @Override public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) { Class type = parameterContext.getParameter().getType(); - return PlanSystem.class.equals(type) || - PlanFiles.class.equals(type) || - PlanConfig.class.equals(type) || - ServerUUID.class.equals(type) || - PlanPluginComponent.class.equals(type) || - PlanCommand.class.equals(type) || - Database.class.equals(type) || - DeliveryUtilities.class.equals(type) || - PublicHtmlFiles.class.equals(type) || - Webserver.class.equals(type) || - Exporter.class.equals(type); + return parameterResolvers.containsKey(type); } @Override public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) { Class type = parameterContext.getParameter().getType(); - if (PlanSystem.class.equals(type)) { - return planSystem; - } - if (PlanFiles.class.equals(type)) { - return planSystem.getPlanFiles(); - } - if (PlanConfig.class.equals(type)) { - return planSystem.getConfigSystem().getConfig(); - } - if (ServerUUID.class.equals(type)) { - return planSystem.getServerInfo().getServerUUID(); - } - if (PlanPluginComponent.class.equals(type)) { - try { - return component.getComponent(); - } catch (Exception e) { - throw new ParameterResolutionException("Error getting " + type.getName(), e); - } - } - if (PlanCommand.class.equals(type)) { - try { - return component.getComponent().planCommand(); - } catch (Exception e) { - throw new ParameterResolutionException("Error getting " + type.getName(), e); - } - } - if (Database.class.equals(type)) { - return planSystem.getDatabaseSystem().getDatabase(); - } - if (DeliveryUtilities.class.equals(type)) { - return planSystem.getDeliveryUtilities(); - } - if (PublicHtmlFiles.class.equals(type)) { - return planSystem.getDeliveryUtilities().getPublicHtmlFiles(); - } - if (Webserver.class.equals(type)) { - return planSystem.getWebServerSystem().getWebServer(); - } - if (Exporter.class.equals(type)) { - return planSystem.getExportSystem().getExporter(); - } + Supplier supplier = parameterResolvers.get(type); + if (supplier != null) return supplier.get(); throw new ParameterResolutionException("Unsupported parameter type " + type.getName()); } }