Skip to content

Commit

Permalink
Use custom sql container
Browse files Browse the repository at this point in the history
  • Loading branch information
seraphinandrieux committed Jun 15, 2022
1 parent 0cfffca commit f4398dc
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
public class Mssql {

public static final String MSSQL_DOCKER_IMAGE_NAME = "mcr.microsoft.com/mssql/server";
public static final String MSSQL_TEST_CONTAINER_EXTENSION_FILE = "ReactiveSqlTestContainerExtension.java";
public static final String LICENSE_TEST_CONTAINER_FILE = "container-license-acceptance.txt";

private Mssql() {}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package tech.jhipster.lite.generator.server.springboot.database.mssql.domain;

import static tech.jhipster.lite.common.domain.FileUtils.*;
import static tech.jhipster.lite.common.domain.WordUtils.*;
import static tech.jhipster.lite.generator.project.domain.Constants.*;
import static tech.jhipster.lite.generator.project.domain.DefaultConfig.*;

import tech.jhipster.lite.error.domain.Assert;
import tech.jhipster.lite.error.domain.GeneratorException;
import tech.jhipster.lite.generator.buildtool.generic.domain.BuildToolService;
import tech.jhipster.lite.generator.docker.domain.DockerService;
import tech.jhipster.lite.generator.docker.domain.DockerImages;
import tech.jhipster.lite.generator.project.domain.DatabaseType;
import tech.jhipster.lite.generator.project.domain.Project;
import tech.jhipster.lite.generator.project.domain.ProjectFile;
Expand All @@ -18,24 +19,29 @@
public class MssqlDomainService implements MssqlService {

public static final String SOURCE = "server/sql";
public static final String EXTENSION_TEST_CONTAINER = "server/springboot/core";
public static final String NEEDLE_ANNOTATION_INTEGRATION_TEST = "// jhipster-needle-annotation-it";
public static final String NEEDLE_IMPORT_INTEGRATION_TEST = "// jhipster-needle-import-it";
public static final String EXTENSION_INTEGRATION_TEST = "@ExtendWith(ReactiveSqlTestContainerExtension.class)";
public static final String IMPORT_INTEGRATION_TEST = "import org.junit.jupiter.api.extension.ExtendWith;";

private final BuildToolService buildToolService;
private final SpringBootCommonService springBootCommonService;
private final SQLCommonService sqlCommonService;
private final DockerService dockerService;
private final DockerImages dockerImages;
private final ProjectRepository projectRepository;

public MssqlDomainService(
BuildToolService buildToolService,
SpringBootCommonService springBootCommonService,
SQLCommonService sqlCommonService,
DockerService dockerService,
DockerImages dockerImages,
ProjectRepository projectRepository
) {
this.buildToolService = buildToolService;
this.springBootCommonService = springBootCommonService;
this.sqlCommonService = sqlCommonService;
this.dockerService = dockerService;
this.dockerImages = dockerImages;
this.projectRepository = projectRepository;
}

Expand Down Expand Up @@ -65,14 +71,9 @@ private void addDriver(Project project) {
private void addDockerCompose(Project project) {
project.addDefaultConfig(BASE_NAME);

dockerService
.getImageNameWithVersion(Mssql.getDockerImageName())
.ifPresentOrElse(
imageName -> project.addConfig("dockerImageName", imageName),
() -> {
throw new GeneratorException("Version not found for docker image: " + Mssql.getDockerImageName());
}
);
String dockerImage = dockerImages.get(Mssql.getDockerImageName()).fullName();
project.addConfig("dockerImageName", dockerImage);

sqlCommonService.addDockerComposeTemplate(project, DatabaseType.MSSQL.id());
}

Expand All @@ -94,14 +95,39 @@ private void addProperties(Project project) {
}

private void addTestcontainers(Project project) {
this.sqlCommonService.addTestcontainers(
project,
DatabaseType.MSSQL.id(),
Mssql.springPropertiesForTest(project.getBaseName().orElse("jhipster"))
);
this.projectRepository.add(
ProjectFile.forProject(project).withSource(SOURCE, Mssql.LICENSE_TEST_CONTAINER_FILE).withDestinationFolder(TEST_RESOURCES)
);
String packageNamePath = project.getPackageNamePath().orElse(getPath(PACKAGE_PATH));
String integrationTestPath = getPath(TEST_JAVA, packageNamePath);
String extendWithNeedle = EXTENSION_INTEGRATION_TEST + LF + NEEDLE_ANNOTATION_INTEGRATION_TEST;
String importWithNeedle = IMPORT_INTEGRATION_TEST + LF + NEEDLE_ANNOTATION_INTEGRATION_TEST;
sqlCommonService.addTestcontainers(
project,
DatabaseType.MSSQL.id(),
Mssql.springPropertiesForTest(project.getBaseName().orElse("jhipster"))
);
projectRepository.replaceText(
project,
getPath(integrationTestPath),
INTEGRATION_TEST,
NEEDLE_ANNOTATION_INTEGRATION_TEST,
extendWithNeedle
);
projectRepository.replaceText(
project,
getPath(integrationTestPath),
INTEGRATION_TEST,
NEEDLE_IMPORT_INTEGRATION_TEST,
importWithNeedle
);

projectRepository.add(
ProjectFile.forProject(project).withSource(SOURCE, Mssql.LICENSE_TEST_CONTAINER_FILE).withDestinationFolder(TEST_RESOURCES)
);
projectRepository.template(
ProjectFile
.forProject(project)
.withSource(EXTENSION_TEST_CONTAINER, Mssql.MSSQL_TEST_CONTAINER_EXTENSION_FILE)
.withDestinationFolder(getPath(integrationTestPath))
);
}

private void addHibernateCore(Project project) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import tech.jhipster.lite.generator.buildtool.generic.domain.BuildToolService;
import tech.jhipster.lite.generator.docker.domain.DockerService;
import tech.jhipster.lite.generator.docker.domain.DockerImages;
import tech.jhipster.lite.generator.project.domain.ProjectRepository;
import tech.jhipster.lite.generator.server.springboot.common.domain.SpringBootCommonService;
import tech.jhipster.lite.generator.server.springboot.database.mssql.domain.MssqlDomainService;
Expand All @@ -16,25 +16,25 @@ public class MssqlBeanConfiguration {
private final BuildToolService buildToolService;
private final SpringBootCommonService springBootCommonService;
private final SQLCommonService sqlCommonService;
private final DockerService dockerService;
private final DockerImages dockerImages;
private final ProjectRepository projectRepository;

public MssqlBeanConfiguration(
BuildToolService buildToolService,
SpringBootCommonService springBootCommonService,
SQLCommonService sqlCommonService,
DockerService dockerService,
DockerImages dockerImages,
ProjectRepository projectRepository
) {
this.buildToolService = buildToolService;
this.springBootCommonService = springBootCommonService;
this.sqlCommonService = sqlCommonService;
this.dockerService = dockerService;
this.dockerImages = dockerImages;
this.projectRepository = projectRepository;
}

@Bean
public MssqlService mssqlService() {
return new MssqlDomainService(buildToolService, springBootCommonService, sqlCommonService, dockerService, projectRepository);
return new MssqlDomainService(buildToolService, springBootCommonService, sqlCommonService, dockerImages, projectRepository);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.springframework.boot.test.context.SpringBootTest;
// jhipster-needle-import-it

@DisplayNameGeneration(ReplaceCamelCase.class)
@Retention(RetentionPolicy.RUNTIME)
@SpringBootTest(classes = { {{mainClass}}App.class })
@Target(ElementType.TYPE)
// jhipster-needle-annotation-it
public @interface IntegrationTest {
public String[] properties() default {};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package {{packageName}};

import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.testcontainers.containers.MSSQLServerContainer;
import org.testcontainers.utility.DockerImageName;

import java.util.concurrent.atomic.AtomicBoolean;

public class ReactiveSqlTestContainerExtension implements BeforeAllCallback {
private static AtomicBoolean started = new AtomicBoolean(false);
private static MSSQLServerContainer<?> container = new MSSQLServerContainer<>(DockerImageName.parse("mcr.microsoft.com/mssql/server:latest").asCompatibleSubstituteFor("mcr.microsoft.com/mssql/server"))
.withUrlParam("trustServerCertificate","true")
.acceptLicense();
@Override
public void beforeAll(ExtensionContext extensionContext) throws Exception {
if (!started.get()) {
container.start();
System.setProperty("spring.datasource.url", container.getJdbcUrl());
System.setProperty("spring.datasource.password", container.getPassword());
System.setProperty("spring.datasource.username", container.getUsername());
System.setProperty("spring.datasource.driver-class-name","com.microsoft.sqlserver.jdbc.SQLServerDriver");
started.set(true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import static tech.jhipster.lite.generator.server.springboot.core.domain.SpringBoot.*;

import java.util.List;
import java.util.Optional;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import tech.jhipster.lite.IntegrationTest;
Expand Down Expand Up @@ -56,10 +57,12 @@ void shouldInit() {
"spring.datasource.url=jdbc:sqlserver://localhost:1433;database=jhipster"
);
assertTestContainersWasAdded(project);
assertExtensionForDatabaseContainerWasAdded(project);
assertLoggerInConfig(project);
}

private void assertTestContainersWasAdded(Project project) {
Optional<String> test = Optional.of("test");
assertFileContent(project, POM_XML, "<testcontainers.version>");
assertFileContent(project, POM_XML, "</testcontainers.version>");
assertFileContent(project, POM_XML, testcontainers());
Expand All @@ -77,6 +80,13 @@ private void assertTestContainersWasAdded(Project project) {
assertFileExist(project, "src/test/resources/container-license-acceptance.txt");
}

private void assertExtensionForDatabaseContainerWasAdded(Project project) {
final String projectTestPath = "src/test/java/com/mycompany/myapp";
assertFileExist(project, projectTestPath + "/ReactiveSqlTestContainerExtension.java");
assertFileContent(project, getPath(projectTestPath, "IntegrationTest.java"), "@ExtendWith(ReactiveSqlTestContainerExtension.class)");
assertFileContent(project, getPath(projectTestPath, "IntegrationTest.java"), "import org.junit.jupiter.api.extension.ExtendWith;");
}

private void assertLoggerInConfig(Project project) {
assertFileContent(
project,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
import tech.jhipster.lite.error.domain.MissingMandatoryValueException;
import tech.jhipster.lite.generator.buildtool.generic.domain.BuildToolService;
import tech.jhipster.lite.generator.buildtool.generic.domain.Dependency;
import tech.jhipster.lite.generator.docker.domain.DockerService;
import tech.jhipster.lite.generator.docker.domain.DockerImage;
import tech.jhipster.lite.generator.docker.domain.DockerImages;
import tech.jhipster.lite.generator.project.domain.Project;
import tech.jhipster.lite.generator.project.domain.ProjectFile;
import tech.jhipster.lite.generator.project.domain.ProjectRepository;
Expand All @@ -41,7 +42,7 @@ class MssqlDomainServiceUnitTest {
ProjectRepository projectRepository;

@Mock
DockerService dockerService;
DockerImages dockerImages;

@InjectMocks
MssqlDomainService mssqlDomainService;
Expand All @@ -57,15 +58,14 @@ void shouldNotInitWithoutProject() {
void shouldInit() {
Project project = tmpProjectWithPomXml();

when(dockerService.getImageNameWithVersion("mcr.microsoft.com/mssql/server")).thenReturn(Optional.of("mssql:0.0.0"));
when(dockerImages.get("mcr.microsoft.com/mssql/server")).thenReturn(new DockerImage("mssql", "0.0.0"));

mssqlDomainService.init(project);

verify(buildToolService).addDependency(any(Project.class), any(Dependency.class));

verify(springBootCommonService, times(4)).addLoggerTest(any(Project.class), anyString(), any(Level.class));

verify(sqlCommonService).addTestcontainers(any(Project.class), anyString(), anyMap());
verify(sqlCommonService).addSpringDataJpa(project);
verify(sqlCommonService).addHibernateCore(project);
verify(sqlCommonService).addDockerComposeTemplate(project, "mssqlserver");
Expand All @@ -74,12 +74,10 @@ void shouldInit() {
verify(springBootCommonService, times(7)).addProperties(eq(project), any(), any());
verify(springBootCommonService, times(2)).addLogger(eq(project), any(), any());
verify(sqlCommonService).addLoggers(project);
}

@Test
void shouldThrowExceptionWhenImageVersionNotFound() {
Project project = tmpProject();

assertThatThrownBy(() -> mssqlDomainService.init(project)).isInstanceOf(GeneratorException.class).hasMessageContaining("mssql");
verify(projectRepository).template(any(ProjectFile.class));
verify(projectRepository)
.replaceText(eq(project), any(String.class), eq("IntegrationTest.java"), eq("// jhipster-needle-annotation-it"), any(String.class));
verify(projectRepository).template(any(ProjectFile.class));
verify(projectRepository).add(any(ProjectFile.class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,4 @@ void shouldGetSpringProperties() {
.containsEntry("spring.jpa.properties.hibernate.dialect", "org.hibernate.dialect.SQLServer2012Dialect")
.containsEntry("spring.jpa.properties.hibernate.format_sql", true);
}

@Test
void shouldGetSpringPropertiesForTest() {
Map<String, Object> springProperties = Mssql.springPropertiesForTest("baseName");

assertThat(springProperties)
.containsEntry("spring.datasource.driver-class-name", "org.testcontainers.jdbc.ContainerDatabaseDriver")
.containsEntry(
"spring.datasource.url",
"jdbc:tc:sqlserver:latest://;database=baseName;trustServerCertificate=true?TC_TMPFS=/testtmpfs:rw"
)
.containsEntry("spring.datasource.username", "SA")
.containsEntry("spring.datasource.password", "yourStrong(!)Password")
.containsEntry("spring.datasource.hikari.maximum-pool-size", 2);
}
}

0 comments on commit f4398dc

Please sign in to comment.