From 29b053eaa1659010d39877705e7d82a96d0e942a Mon Sep 17 00:00:00 2001 From: alandavis Date: Sat, 6 Aug 2022 14:25:35 +0100 Subject: [PATCH] [skip ci] options in test page --- .../transform/base/TransformController.java | 7 +- .../transform/base/html/OptionLister.java | 71 +++++++ .../base/transform/StreamHandler.java | 3 +- .../src/main/resources/templates/test.html | 176 ++-------------- .../transform/base/http/OptionListerTest.java | 189 ++++++++++++++++++ .../libreoffice/LibreOfficeTest.java | 1 - .../org/alfresco/transform/misc/MiscTest.java | 16 +- .../org/alfresco/transform/tika/TikaTest.java | 17 ++ .../alfresco/transform/registry/Origin.java | 22 -- .../registry/TransformRegistryHelperTest.java | 14 ++ .../registry/TransformRegistryTest.java | 52 +++-- 11 files changed, 367 insertions(+), 201 deletions(-) create mode 100644 engines/base/src/main/java/org/alfresco/transform/base/html/OptionLister.java create mode 100644 engines/base/src/test/java/org/alfresco/transform/base/http/OptionListerTest.java diff --git a/engines/base/src/main/java/org/alfresco/transform/base/TransformController.java b/engines/base/src/main/java/org/alfresco/transform/base/TransformController.java index c1b85991..3692b456 100644 --- a/engines/base/src/main/java/org/alfresco/transform/base/TransformController.java +++ b/engines/base/src/main/java/org/alfresco/transform/base/TransformController.java @@ -26,8 +26,8 @@ */ package org.alfresco.transform.base; +import org.alfresco.transform.base.html.OptionLister; import org.alfresco.transform.base.logging.LogEntry; -import org.alfresco.transform.base.probes.ProbeTransform; import org.alfresco.transform.base.transform.TransformHandler; import org.alfresco.transform.client.model.TransformReply; import org.alfresco.transform.client.model.TransformRequest; @@ -102,6 +102,8 @@ public class TransformController @Autowired TransformHandler transformHandler; @Autowired private String coreVersion; + @Autowired + private OptionLister optionLister; TransformEngine transformEngine; @@ -149,6 +151,9 @@ public class TransformController public String test(Model model) { model.addAttribute("title", transformEngine.getTransformEngineName() + " Test Page"); + TransformConfig transformConfig = ((TransformRegistry) transformRegistry).getTransformConfig(); + transformConfig = setOrClearCoreVersion(transformConfig, 0); + model.addAttribute("transformOptions", optionLister.getOptionNames(transformConfig.getTransformOptions())); return "test"; // display test.html } diff --git a/engines/base/src/main/java/org/alfresco/transform/base/html/OptionLister.java b/engines/base/src/main/java/org/alfresco/transform/base/html/OptionLister.java new file mode 100644 index 00000000..3c66a3d3 --- /dev/null +++ b/engines/base/src/main/java/org/alfresco/transform/base/html/OptionLister.java @@ -0,0 +1,71 @@ +/* + * #%L + * Alfresco Transform Core + * %% + * Copyright (C) 2022 - 2022 Alfresco Software Limited + * %% + * This file is part of the Alfresco software. + * - + * If the software was purchased under a paid Alfresco license, the terms of + * the paid license agreement will prevail. Otherwise, the software is + * provided under the following open source license terms: + * - + * Alfresco is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * - + * Alfresco is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * - + * You should have received a copy of the GNU Lesser General Public License + * along with Alfresco. If not, see . + * #L% + */ +package org.alfresco.transform.base.html; + +import org.alfresco.transform.config.TransformOption; +import org.alfresco.transform.config.TransformOptionGroup; +import org.alfresco.transform.config.TransformOptionValue; +import org.springframework.stereotype.Component; + +import java.util.Map; +import java.util.Set; +import java.util.TreeSet; + +@Component +public class OptionLister +{ + public Set getOptionNames(Map> transformOptionsByName) + { + Set set = new TreeSet<>(); + transformOptionsByName.forEach(((optionName, optionSet) -> { + optionSet.stream().forEach(option -> addToList(set, option)); + })); + return set; + } + + private void addToList(Set set, TransformOption option) + { + if (option instanceof TransformOptionGroup) + { + addGroupToList(set, (TransformOptionGroup)option); + } + else + { + addValueToList(set, (TransformOptionValue)option); + } + } + + private void addGroupToList(Set set, TransformOptionGroup group) + { + group.getTransformOptions().stream().forEach(option -> addToList(set, option)); + } + + private void addValueToList(Set set, TransformOptionValue value) + { + set.add(value.getName()); + } +} diff --git a/engines/base/src/main/java/org/alfresco/transform/base/transform/StreamHandler.java b/engines/base/src/main/java/org/alfresco/transform/base/transform/StreamHandler.java index fd30ab98..29296120 100644 --- a/engines/base/src/main/java/org/alfresco/transform/base/transform/StreamHandler.java +++ b/engines/base/src/main/java/org/alfresco/transform/base/transform/StreamHandler.java @@ -111,9 +111,8 @@ public abstract class StreamHandler { inputStream.close(); } - catch (IOException e) + catch (IOException ignore) { - throw new RuntimeException(e); } } } diff --git a/engines/base/src/main/resources/templates/test.html b/engines/base/src/main/resources/templates/test.html index 61d99c82..66369ffe 100644 --- a/engines/base/src/main/resources/templates/test.html +++ b/engines/base/src/main/resources/templates/test.html @@ -5,7 +5,7 @@
- + - - - - - - - - - - - - - - - + + + + + + + + + +
file
Direct Url
directAccessUrl
sourceMimetype
timeout
diff --git a/engines/base/src/test/java/org/alfresco/transform/base/http/OptionListerTest.java b/engines/base/src/test/java/org/alfresco/transform/base/http/OptionListerTest.java new file mode 100644 index 00000000..5b08644c --- /dev/null +++ b/engines/base/src/test/java/org/alfresco/transform/base/http/OptionListerTest.java @@ -0,0 +1,189 @@ +/* + * #%L + * Alfresco Transform Core + * %% + * Copyright (C) 2022 - 2022 Alfresco Software Limited + * %% + * This file is part of the Alfresco software. + * - + * If the software was purchased under a paid Alfresco license, the terms of + * the paid license agreement will prevail. Otherwise, the software is + * provided under the following open source license terms: + * - + * Alfresco is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * - + * Alfresco is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * - + * You should have received a copy of the GNU Lesser General Public License + * along with Alfresco. If not, see . + * #L% + */ +package org.alfresco.transform.base.http; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; +import org.alfresco.transform.base.html.OptionLister; +import org.alfresco.transform.config.TransformOption; +import org.alfresco.transform.config.TransformOptionGroup; +import org.alfresco.transform.config.TransformOptionValue; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Map; +import java.util.Set; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * Used in the html test page. + */ +public class OptionListerTest +{ + OptionLister optionLister = new OptionLister(); + + @Test + public void emptyListTest() + { + Map> transformOptionsByName = Collections.emptyMap(); + + assertEquals(Collections.emptySet(), optionLister.getOptionNames(transformOptionsByName)); + } + + @Test + public void singleOptionNameWithSingleValue() + { + Map> transformOptionsByName = ImmutableMap.of("Dummy", ImmutableSet.of( + new TransformOptionValue(true, "startPage"))); + + assertEquals(ImmutableSet.of("startPage"), optionLister.getOptionNames(transformOptionsByName)); + } + + @Test + public void whenOptionNameEndsInOptions_stripIt() + { + Map> transformOptionsByName = ImmutableMap.of("DummyOptions", ImmutableSet.of( + new TransformOptionValue(true, "startPage"))); + + assertEquals(ImmutableSet.of("startPage"), optionLister.getOptionNames(transformOptionsByName)); + } + + @Test + public void singleOptionNameWithASingleRequiredValue() + { + Map> transformOptionsByName = ImmutableMap.of("DummyOptions", ImmutableSet.of( + new TransformOptionValue(true, "startPage"))); + + assertEquals(ImmutableSet.of("startPage"), optionLister.getOptionNames(transformOptionsByName)); + } + + @Test + public void singleOptionNameWithACoupleOfValues() + { + Map> transformOptionsByName = ImmutableMap.of("DummyOptions", ImmutableSet.of( + new TransformOptionValue(false, "startPage"), + new TransformOptionValue(true, "endPage"))); + + assertEquals(ImmutableSet.of("startPage", "endPage"), optionLister.getOptionNames(transformOptionsByName)); + } + + @Test + public void sortedValues() + { + Map> transformOptionsByName = ImmutableMap.of("DummyOptions", ImmutableSet.of( + new TransformOptionValue(false, "a"), + new TransformOptionValue(false, "n"), + new TransformOptionValue(false, "k"), + new TransformOptionValue(false, "f"), + new TransformOptionValue(true, "z"))); + + assertEquals(ImmutableList.of("a", "f", "k", "n", "z"), new ArrayList<>(optionLister.getOptionNames(transformOptionsByName))); + } + + @Test + public void multipleOptionNames() + { + Map> transformOptionsByName = ImmutableMap.of("DummyOptions", ImmutableSet.of( + new TransformOptionValue(false, "startPage"), + new TransformOptionValue(true, "endPage")), + "Another", ImmutableSet.of( + new TransformOptionValue(false, "scale")), + "YetAnother", ImmutableSet.of( + new TransformOptionValue(false, "x"), + new TransformOptionValue(false, "y"), + new TransformOptionValue(true, "ratio")) + ); + + assertEquals(ImmutableSet.of( + "startPage", + "endPage", + "scale", + "x", + "y", + "ratio"), + optionLister.getOptionNames(transformOptionsByName)); + } + + @Test + public void multipleOptionNamesWithDuplicates() + { + Map> transformOptionsByName = ImmutableMap.of("DummyOptions", ImmutableSet.of( + new TransformOptionValue(false, "startPage"), + new TransformOptionValue(true, "endPage")), + "Another", ImmutableSet.of( + new TransformOptionValue(false, "scale")), + "YetAnother", ImmutableSet.of( + new TransformOptionValue(false, "x"), + new TransformOptionValue(false, "y"), + new TransformOptionValue(true, "scale")) + ); + + assertEquals(ImmutableSet.of( + "startPage", + "endPage", + "scale", + "x", + "y"), + optionLister.getOptionNames(transformOptionsByName)); + } + + @Test + public void nestedGroups() + { + Map> transformOptionsByName = ImmutableMap.of("DummyOptions", ImmutableSet.of( + new TransformOptionValue(false, "1"), + new TransformOptionValue(true, "2"), + new TransformOptionGroup(false, ImmutableSet.of( + new TransformOptionValue(false, "3.1"), + new TransformOptionValue(true, "3.2"), + new TransformOptionValue(false, "3.3"))), + new TransformOptionGroup(true, ImmutableSet.of( + new TransformOptionValue(false, "4.1"), + new TransformOptionGroup(false, ImmutableSet.of( + new TransformOptionValue(false, "4.2.1"), + new TransformOptionGroup(true, ImmutableSet.of( + new TransformOptionValue(false, "4.2.2.1"))), + new TransformOptionValue(true, "4.2.3"))), + new TransformOptionValue(false, "4.3"))))); + + assertEquals(ImmutableSet.of( + "1", + "2", + "3.1", + "3.2", + "3.3", + "4.1", + "4.2.1", + "4.2.2.1", + "4.2.3", + "4.3"), + optionLister.getOptionNames(transformOptionsByName)); + } +} diff --git a/engines/libreoffice/src/test/java/org/alfresco/transform/libreoffice/LibreOfficeTest.java b/engines/libreoffice/src/test/java/org/alfresco/transform/libreoffice/LibreOfficeTest.java index 8160a94e..1d6fa311 100644 --- a/engines/libreoffice/src/test/java/org/alfresco/transform/libreoffice/LibreOfficeTest.java +++ b/engines/libreoffice/src/test/java/org/alfresco/transform/libreoffice/LibreOfficeTest.java @@ -38,7 +38,6 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.doThrow; -import static org.mockito.Mockito.spy; import static org.mockito.Mockito.when; import static org.springframework.http.HttpHeaders.ACCEPT; import static org.springframework.http.HttpHeaders.CONTENT_DISPOSITION; diff --git a/engines/misc/src/test/java/org/alfresco/transform/misc/MiscTest.java b/engines/misc/src/test/java/org/alfresco/transform/misc/MiscTest.java index 9c291601..0f2daf00 100644 --- a/engines/misc/src/test/java/org/alfresco/transform/misc/MiscTest.java +++ b/engines/misc/src/test/java/org/alfresco/transform/misc/MiscTest.java @@ -26,12 +26,14 @@ */ package org.alfresco.transform.misc; +import com.google.common.collect.ImmutableSet; import org.alfresco.transform.base.AbstractBaseTest; -import org.alfresco.transform.client.model.TransformRequest; +import org.alfresco.transform.base.html.OptionLister; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.text.PDFTextStripper; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mock.web.MockMultipartFile; import org.springframework.test.web.servlet.MvcResult; import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; @@ -63,6 +65,8 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. */ public class MiscTest extends AbstractBaseTest { + @Autowired OptionLister optionLister; + protected final String sourceEncoding = "UTF-8"; protected final String targetEncoding = "UTF-8"; protected final String targetMimetype = MIMETYPE_TEXT_PLAIN; @@ -499,4 +503,14 @@ public class MiscTest extends AbstractBaseTest super.targetMimetype = this.targetMimetype; super.queueTransformRequestUsingDirectAccessUrlTest(); } + + @Test + public void optionListTest() + { + assertEquals(ImmutableSet.of( + "pageLimit", + "targetEncoding", + "extractMapping"), + optionLister.getOptionNames(controller.transformConfig(0).getBody().getTransformOptions())); + } } \ No newline at end of file diff --git a/engines/tika/src/test/java/org/alfresco/transform/tika/TikaTest.java b/engines/tika/src/test/java/org/alfresco/transform/tika/TikaTest.java index 013d90fb..2456cdff 100644 --- a/engines/tika/src/test/java/org/alfresco/transform/tika/TikaTest.java +++ b/engines/tika/src/test/java/org/alfresco/transform/tika/TikaTest.java @@ -26,8 +26,10 @@ */ package org.alfresco.transform.tika; +import com.google.common.collect.ImmutableSet; import org.alfresco.transform.base.AbstractBaseTest; import org.alfresco.transform.base.executors.RuntimeExec; +import org.alfresco.transform.base.html.OptionLister; import org.alfresco.transform.base.model.FileRefEntity; import org.alfresco.transform.base.model.FileRefResponse; import org.alfresco.transform.client.model.TransformReply; @@ -37,6 +39,7 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.Mock; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.Resource; import org.springframework.http.HttpHeaders; @@ -116,6 +119,8 @@ public class TikaTest extends AbstractBaseTest "The quick brown fox jumps over the lazy dogs"; private static final String EXPECTED_CSV_CONTENT_CONTAINS = "\"The\",\"quick\",\"brown\",\"fox\""; + @Autowired OptionLister optionLister; + @Mock private RuntimeExec.ExecutionResult mockExecutionResult; @@ -480,4 +485,16 @@ public class TikaTest extends AbstractBaseTest expectedTargetFileBytes = readTestFile(targetExtension); super.httpTransformRequestUsingDirectAccessUrlTest(); } + + @Test + public void optionListTest() + { + assertEquals(ImmutableSet.of( + "includeContents", + "targetEncoding", + "extractMapping", + "notExtractBookmarksText", + "metadata"), + optionLister.getOptionNames(controller.transformConfig(0).getBody().getTransformOptions())); + } } diff --git a/model/src/main/java/org/alfresco/transform/registry/Origin.java b/model/src/main/java/org/alfresco/transform/registry/Origin.java index 0e35c36b..af462aa0 100644 --- a/model/src/main/java/org/alfresco/transform/registry/Origin.java +++ b/model/src/main/java/org/alfresco/transform/registry/Origin.java @@ -56,27 +56,5 @@ public class Origin { return readFrom; } - - public static Set setOf(Collection> originCollection) - { - Set tSet = new HashSet<>(originCollection.size()); - originCollection.forEach(element -> tSet.add(element.get())); - return tSet; - } - - @Override - public boolean equals(Object o) - { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - Origin origin = (Origin)o; - return t.equals(origin.t); - } - - @Override - public int hashCode() - { - return Objects.hash(t); - } } diff --git a/model/src/test/java/org/alfresco/transform/registry/TransformRegistryHelperTest.java b/model/src/test/java/org/alfresco/transform/registry/TransformRegistryHelperTest.java index 13209b0b..83895f11 100644 --- a/model/src/test/java/org/alfresco/transform/registry/TransformRegistryHelperTest.java +++ b/model/src/test/java/org/alfresco/transform/registry/TransformRegistryHelperTest.java @@ -26,6 +26,7 @@ */ package org.alfresco.transform.registry; +import com.google.common.collect.ImmutableMap; import org.alfresco.transform.common.TransformException; import org.junit.jupiter.api.Test; @@ -34,6 +35,7 @@ import java.util.concurrent.atomic.AtomicInteger; import static java.util.Arrays.asList; import static java.util.Collections.emptySet; +import static org.alfresco.transform.common.RequestParamMap.TIMEOUT; import static org.alfresco.transform.registry.TransformRegistryHelper.retrieveTransformListBySize; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -181,4 +183,16 @@ public class TransformRegistryHelperTest retrieveTransformListBySize(data, "text/plain", null, null, null); }); } + + @Test + public void filterTimeoutTest() + { + // Almost identical to buildTransformListTargetMimeTypeNullErrorTest + TransformCache data = new TransformCache(); + + assertThrows(TransformException.class, () -> + { + retrieveTransformListBySize(data, "text/plain", null, ImmutableMap.of(TIMEOUT, "1234"), null); + }); + } } diff --git a/model/src/test/java/org/alfresco/transform/registry/TransformRegistryTest.java b/model/src/test/java/org/alfresco/transform/registry/TransformRegistryTest.java index 937bb048..83d84a31 100644 --- a/model/src/test/java/org/alfresco/transform/registry/TransformRegistryTest.java +++ b/model/src/test/java/org/alfresco/transform/registry/TransformRegistryTest.java @@ -42,6 +42,8 @@ import java.util.HashSet; import java.util.Map; import java.util.Set; +import com.google.common.collect.ImmutableList; +import org.alfresco.transform.config.CoreFunction; import org.alfresco.transform.config.SupportedSourceAndTarget; import org.alfresco.transform.config.TransformConfig; import org.alfresco.transform.config.TransformOption; @@ -130,7 +132,7 @@ public class TransformRegistryTest } // transformOptionNames are upper case if required. - private void assertIsSupported(final Set actualOptionNames, + private void assertOptionsMatch(final Set actualOptionNames, final Set transformOptionNames, final String unsupportedMsg) { final Map transformOptions = transformOptionNames @@ -384,22 +386,22 @@ public class TransformRegistryTest } @Test - public void testRegistryIsSupportedMethod() + public void testRegistryOptionsMatchMethod() { - assertIsSupported(set("a"), set("a", "B", "c"), "required option B is missing"); - assertIsSupported(emptySet(), set("a", "B", "c"), "required option B is missing"); - assertIsSupported(set("B"), set("a", "B", "c"), null); - assertIsSupported(set("B", "c"), set("a", "B", "c"), null); - assertIsSupported(set("B", "a", "c"), set("a", "B", "c"), null); + assertOptionsMatch(set("a"), set("a", "B", "c"), "required option B is missing"); + assertOptionsMatch(emptySet(), set("a", "B", "c"), "required option B is missing"); + assertOptionsMatch(set("B"), set("a", "B", "c"), null); + assertOptionsMatch(set("B", "c"), set("a", "B", "c"), null); + assertOptionsMatch(set("B", "a", "c"), set("a", "B", "c"), null); - assertIsSupported(set("B", "d"), set("a", "B", "c"), "there is an extra option d"); - assertIsSupported(set("B", "c", "d"), set("a", "B", "c"), "there is an extra option d"); - assertIsSupported(set("d"), set("a", "B", "c"), + assertOptionsMatch(set("B", "d"), set("a", "B", "c"), "there is an extra option d"); + assertOptionsMatch(set("B", "c", "d"), set("a", "B", "c"), "there is an extra option d"); + assertOptionsMatch(set("d"), set("a", "B", "c"), "required option B is missing and there is an extra option d"); - assertIsSupported(set("a"), set("a", "b", "c"), null); - assertIsSupported(emptySet(), set("a", "b", "c"), null); - assertIsSupported(set("a", "b", "c"), set("a", "b", "c"), null); + assertOptionsMatch(set("a"), set("a", "b", "c"), null); + assertOptionsMatch(emptySet(), set("a", "b", "c"), null); + assertOptionsMatch(set("a", "b", "c"), set("a", "b", "c"), null); } @Test @@ -613,4 +615,28 @@ public class TransformRegistryTest } return ImmutableSet.copyOf(elements); } + + @Test + public void testIsSupportedCoreFunction() throws Exception + { + Transformer t1 = newTransformer("transformer1", MSG, GIF, 100, 50); + Transformer t2 = newTransformer("transformer2", MSG, GIF, 200, 60); + Transformer t3 = newTransformer("transformer3", MSG, GIF, 200, 40); + t2.setCoreVersion("1.0"); + t3.setCoreVersion("2.5.7"); + + buildAndPopulateRegistry(new Transformer[] {t1, t2, t3}); + + assertTrue(registry.isSupported(CoreFunction.HTTP, "transformer1")); + assertTrue(registry.isSupported(CoreFunction.HTTP, "transformer2")); + assertTrue(registry.isSupported(CoreFunction.HTTP, "transformer3")); + + assertFalse(registry.isSupported(CoreFunction.ACTIVE_MQ, "transformer1")); + assertTrue(registry.isSupported(CoreFunction.ACTIVE_MQ, "transformer2")); + assertTrue(registry.isSupported(CoreFunction.ACTIVE_MQ, "transformer3")); + + assertFalse(registry.isSupported(CoreFunction.DIRECT_ACCESS_URL, "transformer1")); + assertFalse(registry.isSupported(CoreFunction.DIRECT_ACCESS_URL, "transformer2")); + assertTrue(registry.isSupported(CoreFunction.DIRECT_ACCESS_URL, "transformer3")); + } }