Skip to content

Commit

Permalink
fix: handle accept license as string (#240)
Browse files Browse the repository at this point in the history
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)
``
  • Loading branch information
sdelamo authored May 4, 2023
1 parent 86d2afe commit c39758c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ protected MSSQLServerContainer<?> createContainer(DockerImageName imageName, Map
public static MSSQLServerContainer<?> createMSSQLContainer(DockerImageName imageName, String simpleName, Map<String, Object> 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 {
Expand All @@ -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<String, Object> testResourcesConfiguration) {
Object obj = testResourcesConfiguration.get(licenseKey);
if (obj == null) {
return false;
}
if (obj instanceof Boolean b) {
return b;
} else {
return Boolean.parseBoolean(obj.toString());
}
}
}
Original file line number Diff line number Diff line change
@@ -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<String, Object> testResourcesConfiguration = [(licenseKey): value]
when:
boolean accept = MSSQLTestResourceProvider.shouldAcceptLicense(licenseKey, testResourcesConfiguration)

then:
noExceptionThrown()
accept

where:
value << [StringUtils.TRUE, true, Boolean.TRUE]
}


}

0 comments on commit c39758c

Please sign in to comment.