From c39758cde7f2e7b68d0a4edcda192768bbbd8dc3 Mon Sep 17 00:00:00 2001 From: Sergio del Amo Date: Thu, 4 May 2023 11:44:01 +0200 Subject: [PATCH] fix: handle accept license as string (#240) solve issues found in starter ``` ERROR i.m.http.server.RouteExecutor - Unexpected error occurred: class java.lang.String cannot be cast to class java.lang.Boolean (java.lang.String and java.lang.Boolean are in module java.base of loader 'bootstrap') java.lang.ClassCastException: class java.lang.String cannot be cast to class java.lang.Boolean (java.lang.String and java.lang.Boolean are in module java.base of loader 'bootstrap') at io.micronaut.testresources.mssql.MSSQLTestResourceProvider.createMSSQLContainer(MSSQLTestResourceProvider.java:50) `` --- .../mssql/MSSQLTestResourceProvider.java | 20 ++++++++++++++-- .../MSSQLTestResourceProviderSpec.groovy | 23 +++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 test-resources-jdbc/test-resources-jdbc-mssql/src/test/groovy/io/micronaut/testresources/mssql/MSSQLTestResourceProviderSpec.groovy diff --git a/test-resources-jdbc/test-resources-jdbc-mssql/src/main/java/io/micronaut/testresources/mssql/MSSQLTestResourceProvider.java b/test-resources-jdbc/test-resources-jdbc-mssql/src/main/java/io/micronaut/testresources/mssql/MSSQLTestResourceProvider.java index 62679449f..b6b967e3e 100644 --- a/test-resources-jdbc/test-resources-jdbc-mssql/src/main/java/io/micronaut/testresources/mssql/MSSQLTestResourceProvider.java +++ b/test-resources-jdbc/test-resources-jdbc-mssql/src/main/java/io/micronaut/testresources/mssql/MSSQLTestResourceProvider.java @@ -47,8 +47,7 @@ protected MSSQLServerContainer createContainer(DockerImageName imageName, Map public static MSSQLServerContainer createMSSQLContainer(DockerImageName imageName, String simpleName, Map testResourcesConfiguration) { MSSQLServerContainer container = new MSSQLServerContainer<>(imageName); String licenseKey = "containers." + simpleName + ".accept-license"; - Boolean acceptLicense = (Boolean) testResourcesConfiguration.get(licenseKey); - if (Boolean.TRUE.equals(acceptLicense)) { + if (shouldAcceptLicense(licenseKey, testResourcesConfiguration)) { container.acceptLicense(); } else { try { @@ -60,4 +59,21 @@ public static MSSQLServerContainer createMSSQLContainer(DockerImageName image return container; } + /** + * + * @param licenseKey License Key + * @param testResourcesConfiguration Test Resources Configuration + * @return {@code false} if no value found in Test Resources Configuration for the license key, otherwise it returns the object if it is a boolean, or it parses the value to a boolean using {@link Boolean#parseBoolean(String)}. + */ + public static boolean shouldAcceptLicense(String licenseKey, Map testResourcesConfiguration) { + Object obj = testResourcesConfiguration.get(licenseKey); + if (obj == null) { + return false; + } + if (obj instanceof Boolean b) { + return b; + } else { + return Boolean.parseBoolean(obj.toString()); + } + } } diff --git a/test-resources-jdbc/test-resources-jdbc-mssql/src/test/groovy/io/micronaut/testresources/mssql/MSSQLTestResourceProviderSpec.groovy b/test-resources-jdbc/test-resources-jdbc-mssql/src/test/groovy/io/micronaut/testresources/mssql/MSSQLTestResourceProviderSpec.groovy new file mode 100644 index 000000000..973a4de0c --- /dev/null +++ b/test-resources-jdbc/test-resources-jdbc-mssql/src/test/groovy/io/micronaut/testresources/mssql/MSSQLTestResourceProviderSpec.groovy @@ -0,0 +1,23 @@ +package io.micronaut.testresources.mssql + +import io.micronaut.core.util.StringUtils; +import spock.lang.Specification; + +class MSSQLTestResourceProviderSpec extends Specification { + + void "verify String is parsed into boolean for the value of accept-license"(Object value) { + String licenseKey = "containers.mssql.accept-license" + Map testResourcesConfiguration = [(licenseKey): value] + when: + boolean accept = MSSQLTestResourceProvider.shouldAcceptLicense(licenseKey, testResourcesConfiguration) + + then: + noExceptionThrown() + accept + + where: + value << [StringUtils.TRUE, true, Boolean.TRUE] + } + + +}