Skip to content

Commit 34da924

Browse files
authored
#10 add MSSQL Server support (#13)
1 parent df863e5 commit 34da924

10 files changed

Lines changed: 168 additions & 0 deletions

File tree

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,32 @@ dependencies {
237237
}
238238
````
239239

240+
* **Provided properties**:
241+
* `spring.datasource.url`
242+
* `spring.datasource.username`
243+
* `spring.datasource.password`
244+
* `spring.datasource.driver-class-name`
245+
246+
## Microsoft SQL Server
247+
248+
### MSSQL specific:
249+
Make sure to add the following property to your ```application-test.yml``` to accept the MSSQL Server License Agreement:
250+
```yml
251+
test-resources:
252+
containers:
253+
mssql:
254+
accept-license: true
255+
```
256+
257+
* **Module-ID**: mssql
258+
* **Default-Image**: mcr.microsoft.com/mssql/server:2019-CU16-GDR1-ubuntu-20.04
259+
260+
````kotlin
261+
dependencies {
262+
testResourcesImplementation ("io.cloudflight.testresources.springboot:springboot-testresources-jdbc-mssql:0.1.2")
263+
}
264+
````
265+
240266
* **Provided properties**:
241267
* `spring.datasource.url`
242268
* `spring.datasource.username`

settings.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ include("springboot-testresources-client")
99
include("springboot-testresources-minio")
1010
include("springboot-testresources-jdbc")
1111
include("springboot-testresources-jdbc:springboot-testresources-jdbc-mariadb")
12+
include("springboot-testresources-jdbc:springboot-testresources-jdbc-mssql")
1213
include("springboot-testresources-jdbc:springboot-testresources-jdbc-postgres")
1314
include("springboot-testresources-rabbitmq")
1415
include("springboot-testresources-redis")
1516

1617
include("testprojects:jdbc:mariadb")
18+
include("testprojects:jdbc:mssql")
1719
include("testprojects:jdbc:postgres")
1820
include("testprojects:azurite")
1921
include("testprojects:minio")
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
description = "Spring Boot TestResourceProvider for Microsoft SQL Server"
2+
3+
dependencies {
4+
implementation(project(":springboot-testresources-jdbc"))
5+
implementation("org.testcontainers:mssqlserver")
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
io.cloudflight.testresources.springboot.jdbc.mssql.MssqlServerTestResourcesProvider
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package io.cloudflight.testresources.springboot.jdbc.mariadb;
2+
3+
import io.cloudflight.testresources.springboot.jdbc.mssql.MssqlServerTestResourcesProvider;
4+
import org.junit.jupiter.api.Test;
5+
6+
import java.util.Collections;
7+
import java.util.List;
8+
9+
import static org.junit.jupiter.api.Assertions.assertFalse;
10+
import static org.junit.jupiter.api.Assertions.assertTrue;
11+
12+
class MssqlServerTestResourcesProviderTest {
13+
14+
final private MssqlServerTestResourcesProvider provider = new MssqlServerTestResourcesProvider();
15+
16+
@Test
17+
void test() {
18+
List<String> properties = provider.getResolvableProperties(Collections.emptyMap(), Collections.emptyMap());
19+
assertFalse(properties.isEmpty());
20+
}
21+
22+
@Test
23+
void shouldAnswer() {
24+
assertTrue(provider.shouldAnswer("spring.datasource.url", Collections.emptyMap(), Collections.emptyMap()));
25+
}
26+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
plugins {
2+
id("io.micronaut.test-resources")
3+
}
4+
5+
dependencies {
6+
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
7+
implementation("com.microsoft.sqlserver:mssql-jdbc:11.2.3.jre17")
8+
9+
runtimeOnly("org.jetbrains.kotlin:kotlin-reflect")
10+
11+
testImplementation("org.springframework.boot:spring-boot-starter-test")
12+
13+
testRuntimeOnly(project(":springboot-testresources-client"))
14+
testResourcesImplementation(project(":springboot-testresources-jdbc:springboot-testresources-jdbc-mssql"))
15+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package io.cloudflight.testresources.springboot.jdbc.mssql
2+
3+
import jakarta.persistence.Entity
4+
import jakarta.persistence.GeneratedValue
5+
import jakarta.persistence.Id
6+
import org.springframework.boot.autoconfigure.SpringBootApplication
7+
import org.springframework.data.jpa.repository.JpaRepository
8+
9+
@SpringBootApplication
10+
class Application
11+
12+
@Entity
13+
class Person(var name: String) {
14+
15+
@Id
16+
@GeneratedValue
17+
var id: Long = 0
18+
}
19+
20+
interface PersonRepository : JpaRepository<Person, Long>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
test-resources:
2+
containers:
3+
mssql:
4+
accept-license: true
5+
6+
spring:
7+
jpa:
8+
hibernate:
9+
ddl-auto: create-drop
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package io.cloudflight.testresources.springboot.jdbc.mssql
2+
3+
import org.junit.jupiter.api.Test
4+
import org.springframework.beans.factory.annotation.Autowired
5+
import org.springframework.boot.test.context.SpringBootTest
6+
7+
@SpringBootTest
8+
class ApplicationTest(
9+
@Autowired private val repository: PersonRepository
10+
) {
11+
12+
@Test
13+
fun enterData() {
14+
repository.save(Person(name = "Person1"))
15+
}
16+
}

0 commit comments

Comments
 (0)