|
| 1 | +package io.cloudflight.testresources.springboot.jdbc.mssql; |
| 2 | + |
| 3 | +import io.cloudflight.testresources.springboot.jdbc.AbstractJdbcTestResourceProvider; |
| 4 | +import org.testcontainers.containers.MSSQLServerContainer; |
| 5 | +import org.testcontainers.utility.DockerImageName; |
| 6 | +import org.testcontainers.utility.LicenseAcceptance; |
| 7 | + |
| 8 | +import java.util.Map; |
| 9 | + |
| 10 | +public class MssqlServerTestResourcesProvider extends AbstractJdbcTestResourceProvider<MSSQLServerContainer<?>> { |
| 11 | + |
| 12 | + private static final String DEFAULT_IMAGE = "mcr.microsoft.com/mssql/server:2019-CU16-GDR1-ubuntu-20.04"; |
| 13 | + private static final String SIMPLE_NAME = "mssql"; |
| 14 | + |
| 15 | + |
| 16 | + @Override |
| 17 | + protected String getSimpleName() { |
| 18 | + return SIMPLE_NAME; |
| 19 | + } |
| 20 | + |
| 21 | + @Override |
| 22 | + protected String getDefaultImageName() { |
| 23 | + return DEFAULT_IMAGE; |
| 24 | + } |
| 25 | + |
| 26 | + @Override |
| 27 | + protected MSSQLServerContainer<?> createContainer(DockerImageName imageName, Map<String, Object> requestedProperties, Map<String, Object> testResourcesConfiguration) { |
| 28 | + return createMSSQLContainer(imageName, getSimpleName(), testResourcesConfiguration); |
| 29 | + } |
| 30 | + |
| 31 | + public static MSSQLServerContainer<?> createMSSQLContainer(DockerImageName imageName, String simpleName, Map<String, Object> testResourcesConfiguration) { |
| 32 | + MSSQLServerContainer<?> container = new MSSQLServerContainer<>(imageName); |
| 33 | + String licenseKey = "containers." + simpleName + ".accept-license"; |
| 34 | + Boolean acceptLicense = (Boolean) testResourcesConfiguration.get(licenseKey); |
| 35 | + if (Boolean.TRUE.equals(acceptLicense)) { |
| 36 | + container.acceptLicense(); |
| 37 | + } else { |
| 38 | + try { |
| 39 | + LicenseAcceptance.assertLicenseAccepted(imageName.toString()); |
| 40 | + } catch (IllegalStateException ex) { |
| 41 | + throw new IllegalStateException("You must set the property 'test-resources." + licenseKey + "' to true in order to use a Microsoft SQL Server test container", ex); |
| 42 | + } |
| 43 | + } |
| 44 | + return container; |
| 45 | + } |
| 46 | + |
| 47 | +} |
0 commit comments