From 4be601fd0372de3bd4c03f4265bdc8f573cc627c Mon Sep 17 00:00:00 2001 From: Maximilian Pilz Date: Thu, 7 Mar 2024 09:41:40 +0100 Subject: [PATCH] TestSuite extension mechanism (#146) Make it easier to extend TestSuite with test cases and let TomlConfigParser read in inherited fields as well. # Checklist The following aspects have been respected by the author of this pull request, confirmed by both pull request assignee **and** reviewer: * Adherence to coding conventions * [x] @maximilianpilz * [x] @belagertem * Adherence to javadoc conventions * [x] @maximilianpilz * [x] @belagertem * Changelog update (necessity checked and entry added or not added respectively) * [x] @maximilianpilz * [x] @belagertem * README update (necessity checked and entry added or not added respectively) * [x] @maximilianpilz * [x] @belagertem * config update (necessity checked and entry added or not added respectively) * [x] @maximilianpilz * [x] @belagertem * SDCcc executable ran against a test device (if necessary) * [x] @maximilianpilz * [x] @belagertem --- checkstyle/checkstyle.xml | 1 - pom.xml | 71 ++++++++++++++----- sdccc/pom.xml | 27 ++++--- .../com/draeger/medical/sdccc/TestSuite.java | 55 ++++++++++++-- .../configuration/DefaultTestSuiteConfig.java | 19 +++-- .../configuration/EnabledTestConfig.java | 9 ++- .../medical/sdccc/guice/TomlConfigParser.java | 5 +- 7 files changed, 140 insertions(+), 47 deletions(-) diff --git a/checkstyle/checkstyle.xml b/checkstyle/checkstyle.xml index 0c141703..fbcfcdb9 100644 --- a/checkstyle/checkstyle.xml +++ b/checkstyle/checkstyle.xml @@ -90,7 +90,6 @@ - diff --git a/pom.xml b/pom.xml index 9b4668a4..0e56d54a 100644 --- a/pom.xml +++ b/pom.xml @@ -35,25 +35,6 @@ - - org.codehaus.mojo - exec-maven-plugin - ${execPluginVersion} - - true - ${java.home}/bin/java - - -classpath - - com.draeger.medical.sdccc.TestSuite - -c - ${project.basedir}/../configuration/config.toml - -t - ${project.basedir}/../configuration/test_configuration.toml - - - - org.codehaus.mojo license-maven-plugin @@ -92,6 +73,58 @@ + + org.codehaus.mojo + exec-maven-plugin + ${execPluginVersion} + + true + ${java.home}/bin/java + + -classpath + + com.draeger.medical.sdccc.TestSuite + -c + ${project.basedir}/../configuration/config.toml + -t + ${project.basedir}/../configuration/test_configuration.toml + + + + + + + + exec-sdccc + + + + org.codehaus.mojo + exec-maven-plugin + ${execPluginVersion} + + + 951d1725-bb0b-40db-9d4e-d1947a8318ed + + false + ${java.home}/bin/java + + -classpath + + com.draeger.medical.sdccc.TestSuite + -c + ${project.basedir}/../configuration/config.toml + -t + ${project.basedir}/../configuration/test_configuration.toml + + + + + + + + + diff --git a/sdccc/pom.xml b/sdccc/pom.xml index 210f1fa8..de6ff58d 100644 --- a/sdccc/pom.xml +++ b/sdccc/pom.xml @@ -423,19 +423,26 @@ - - org.codehaus.mojo - exec-maven-plugin - ${execPluginVersion} - - false - ${java.home}/bin/java - - + + exec-sdccc + + + + org.codehaus.mojo + exec-maven-plugin + ${execPluginVersion} + + false + ${java.home}/bin/java + + + + + executable @@ -472,7 +479,7 @@ ${revision}.0 ${revision}${changelist} ${project.name} - 2023 Draegerwerk AG & Co. KGaA + 2024 Draegerwerk AG & Co. KGaA ${revision}.0 ${revision}${changelist} ${project.name} diff --git a/sdccc/src/main/java/com/draeger/medical/sdccc/TestSuite.java b/sdccc/src/main/java/com/draeger/medical/sdccc/TestSuite.java index 7ebaaf12..011b8132 100644 --- a/sdccc/src/main/java/com/draeger/medical/sdccc/TestSuite.java +++ b/sdccc/src/main/java/com/draeger/medical/sdccc/TestSuite.java @@ -105,8 +105,21 @@ public class TestSuite { private final TestClient client; private final boolean testExecutionLogging; + /** + * Used by the main injector to create a TestSuite instance. + * It is also useful for extending the TestSuite class. + * + * @param injector a reference to the injector injecting here + * @param testRunObserver observer for invalidating test runs + * @param sdcTestDirectories directories to search for test cases + * @param testRunDir directory to run the tests in and store artifacts + * @param testExecutionLogging whether logging of test case starts etc. shall be done + * @param messageGenerator utility for generating messages to send + * @param testRunInformation utility where information of the test run is stored + * @param client client to use for direct tests + */ @Inject - TestSuite( + public TestSuite( final Injector injector, final TestRunObserver testRunObserver, @Named(TestSuiteConfig.SDC_TEST_DIRECTORIES) final String[] sdcTestDirectories, @@ -428,7 +441,11 @@ private static Injector createInjector(final Module... override) { .with(override)); } - private static Injector createTestRunInjector(final CommandLineOptions cmdLine, final File testRunDir) + private static Injector createTestRunInjector( + final CommandLineOptions cmdLine, + final File testRunDir, + final Class enabledTestConfigClass, + final String[] sdcTestDirectories) throws IOException { final AbstractConfigurationModule baseConfigModule = new AbstractConfigurationModule() { @@ -509,12 +526,13 @@ protected void defaultConfigure() { baseConfigModule.bind(TestSuiteConfig.CONSUMER_DEVICE_LOCATION_FLOOR, String.class, null); baseConfigModule.bind(TestSuiteConfig.CONSUMER_DEVICE_LOCATION_ROOM, String.class, null); baseConfigModule.bind(TestSuiteConfig.CONSUMER_DEVICE_LOCATION_BED, String.class, null); + baseConfigModule.bind(TestSuiteConfig.SDC_TEST_DIRECTORIES, String[].class, sdcTestDirectories); configModuleParser.parseToml(configFileStream, configModule); } try (final var testConfigFileStream = new FileInputStream(cmdLine.getTestConfigPath().toFile())) { - final var testConfigModuleParser = new TomlConfigParser(EnabledTestConfig.class); + final var testConfigModuleParser = new TomlConfigParser(enabledTestConfigClass); testConfigModuleParser.parseToml(testConfigFileStream, testConfigModule); } @@ -620,12 +638,38 @@ public static void setupSwingTheme() * @param args array of command line arguments */ public static void main(final String[] args) throws IOException { + final var cmdLine = initialize(args); + + runWithArgs(cmdLine, EnabledTestConfig.class, DefaultTestSuiteConfig.DEFAULT_DIRECTORIES); + } + + /** + * Set required system properties and parse the command line arguments. + * + * @param args array of command line arguments + * @return parsed command line arguments + */ + public static CommandLineOptions initialize(final String[] args) { // improve xml interaction performance setSystemProperties(); // parse command line options - final var cmdLine = new CommandLineOptions(args); + return new CommandLineOptions(args); + } + /** + * Run the test suite with the given already parsed command line arguments and the + * specified enabled test config constants. + * + * @param cmdLine parsed command line arguments + * @param enabledTestConfigClass class containing test identifier constants + * @param sdcTestDirectories directories to search for test cases + */ + public static void runWithArgs( + final CommandLineOptions cmdLine, + final Class enabledTestConfigClass, + final String[] sdcTestDirectories) + throws IOException { // setup logging final var testRunDir = TestRunConfig.createTestRunDirectory( cmdLine.getTestRunDirectory().orElse(null)); @@ -648,7 +692,8 @@ public static void main(final String[] args) throws IOException { LOG.warn("Error while setting swing look and feel options.", e); } - final Injector injector = createTestRunInjector(cmdLine, testRunDir); + final Injector injector = + createTestRunInjector(cmdLine, testRunDir, enabledTestConfigClass, sdcTestDirectories); final TriggerOnErrorOrWorseLogAppender triggerOnErrorOrWorseLogAppender = findTriggerOnErrorOrWorseLogAppender(logConfig); diff --git a/sdccc/src/main/java/com/draeger/medical/sdccc/configuration/DefaultTestSuiteConfig.java b/sdccc/src/main/java/com/draeger/medical/sdccc/configuration/DefaultTestSuiteConfig.java index d3c4d01d..820023ae 100644 --- a/sdccc/src/main/java/com/draeger/medical/sdccc/configuration/DefaultTestSuiteConfig.java +++ b/sdccc/src/main/java/com/draeger/medical/sdccc/configuration/DefaultTestSuiteConfig.java @@ -7,6 +7,7 @@ package com.draeger.medical.sdccc.configuration; +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import org.somda.sdc.common.guice.AbstractConfigurationModule; /** @@ -16,6 +17,17 @@ */ public class DefaultTestSuiteConfig extends AbstractConfigurationModule { + @SuppressFBWarnings( + value = {"MS_MUTABLE_ARRAY"}, + justification = "In case this is wrong there will be an error (for test cases that couldn't be found)" + + " and thus this being wrong due to modification will always be noticed.") + public static final String[] DEFAULT_DIRECTORIES = { + "com.draeger.medical.sdccc.tests.biceps", + "com.draeger.medical.sdccc.tests.mdpws", + "com.draeger.medical.sdccc.tests.dpws", + "com.draeger.medical.sdccc.tests.glue", + }; + private static final int BUFFER_SIZE = 100; @Override @@ -80,12 +92,7 @@ void configureTestParameter() { } void configureInternalSettings() { - bind(TestSuiteConfig.SDC_TEST_DIRECTORIES, String[].class, new String[] { - "com.draeger.medical.sdccc.tests.biceps", - "com.draeger.medical.sdccc.tests.mdpws", - "com.draeger.medical.sdccc.tests.dpws", - "com.draeger.medical.sdccc.tests.glue", - }); + bind(TestSuiteConfig.SDC_TEST_DIRECTORIES, String[].class, DEFAULT_DIRECTORIES); } protected void configureCommlogSettings() { diff --git a/sdccc/src/main/java/com/draeger/medical/sdccc/configuration/EnabledTestConfig.java b/sdccc/src/main/java/com/draeger/medical/sdccc/configuration/EnabledTestConfig.java index e4de2a10..05e2f18a 100644 --- a/sdccc/src/main/java/com/draeger/medical/sdccc/configuration/EnabledTestConfig.java +++ b/sdccc/src/main/java/com/draeger/medical/sdccc/configuration/EnabledTestConfig.java @@ -1,6 +1,6 @@ /* * This Source Code Form is subject to the terms of the MIT License. - * Copyright (c) 2023 Draegerwerk AG & Co. KGaA. + * Copyright (c) 2023, 2024 Draegerwerk AG & Co. KGaA. * * SPDX-License-Identifier: MIT */ @@ -11,7 +11,7 @@ * Constants used to map the content of the configuration for enabled * test cases. */ -public final class EnabledTestConfig { +public class EnabledTestConfig { // BICEPS private static final String BICEPS = "BICEPS."; @@ -124,5 +124,8 @@ public final class EnabledTestConfig { public static final String GLUE_R0080 = GLUE + "R0080"; public static final String GLUE_813 = GLUE + "8-1-3"; - private EnabledTestConfig() {} + /** + * Default constructor for extending the constants list. + */ + public EnabledTestConfig() {} } diff --git a/sdccc/src/main/java/com/draeger/medical/sdccc/guice/TomlConfigParser.java b/sdccc/src/main/java/com/draeger/medical/sdccc/guice/TomlConfigParser.java index ceede797..44dbf4fe 100644 --- a/sdccc/src/main/java/com/draeger/medical/sdccc/guice/TomlConfigParser.java +++ b/sdccc/src/main/java/com/draeger/medical/sdccc/guice/TomlConfigParser.java @@ -1,6 +1,6 @@ /* * This Source Code Form is subject to the terms of the MIT License. - * Copyright (c) 2023 Draegerwerk AG & Co. KGaA. + * Copyright (c) 2023, 2024 Draegerwerk AG & Co. KGaA. * * SPDX-License-Identifier: MIT */ @@ -38,13 +38,12 @@ public class TomlConfigParser { * This parser only allows known keys and will throw an exception otherwise. * * @param constantsClass to verify toml keys against - * @throws IOException in case an error occurred during parsing. */ public TomlConfigParser(final Class constantsClass) { // track allowed keys this.allowedKeys = new HashSet<>(); - Arrays.stream(constantsClass.getDeclaredFields()).forEach(field -> { + Arrays.stream(constantsClass.getFields()).forEach(field -> { if (java.lang.reflect.Modifier.isStatic(field.getModifiers())) { try { allowedKeys.add((String) field.get(null));