Skip to content

Commit

Permalink
introducing testcontainers in dev mode and increment (#76)
Browse files Browse the repository at this point in the history
* introducing testcontainers in dev mode and increment

* fixes issue with localstack
  • Loading branch information
rajadilipkolli committed Jun 11, 2023
1 parent 7c8a8a8 commit 485d4a3
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 101 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"java.configuration.updateBuildConfiguration": "automatic"
"java.configuration.updateBuildConfiguration": "automatic",
"java.compile.nullAnalysis.mode": "automatic"
}
22 changes: 11 additions & 11 deletions generators/constants.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
module.exports = {
JAVA_VERSION : '17',
SPRING_BOOT_VERSION : '3.1.0-RC2',
SPRING_CLOUD_VERSION : '2022.0.2',
SPRING_CLOUD_AWS_VERSION : '3.0.0',
SPRING_BOOT_VERSION : '3.1.0',
SPRING_CLOUD_VERSION : '2022.0.3',
SPRING_CLOUD_AWS_VERSION : '3.0.1',
SPRING_DEP_MNGMNT_VERSION : '1.1.0',
DEFAULT_APP_VERSION : '0.0.1-SNAPSHOT',
TEST_CONTAINERS_VERSION : '1.18.0',
TEST_CONTAINERS_VERSION : '1.18.3',
SPRINGDOC_OPENAPI_VERSION : '2.1.0',
COMMONS_IO_VERSION : '2.11.0',
COMMONS_IO_VERSION : '2.12.0',
LOGSTASH_LOGBACK_ENCODER : 7.3,

JACOCO_MIN_COVERAGE_REQUIRED:'0.80',
Expand All @@ -21,17 +21,17 @@ module.exports = {

MAVEN_DEPENDENCY_CHECK_PLUGIN_VERSION:'8.2.1',
MAVEN_PROPERTIES_PLUGIN_VERSION:'1.1.0',
MAVEN_SUREFIRE_PLUGIN_VERSION:'3.0.0',
MAVEN_FAILSAFE_PLUGIN_VERSION:'3.0.0',
MAVEN_SUREFIRE_PLUGIN_VERSION:'3.1.2',
MAVEN_FAILSAFE_PLUGIN_VERSION:'3.1.2',
MAVEN_SONAR_PLUGIN_VERSION:'3.9.1.2184',
MAVEN_JACOCO_PLUGIN_VERSION:'0.8.10',
MAVEN_SPOTLESS_PLUGIN_VERSION:'2.36.0',
MAVEN_SPOTLESS_PLUGIN_VERSION:'2.37.0',

KEY_FLYWAY_MIGRATION_COUNTER : 'flywayMigrationCounter',
KEY_LIQUIBASE_MIGRATION_COUNTER : 'liquibaseMigrationCounter',

LOCALSTACK_IMAGE: 'localstack/localstack:2.0',
POSTGRESQL_IMAGE: 'postgres:15.2-alpine',
MARIADB_IMAGE: 'mariadb:10.11',
LOCALSTACK_IMAGE: 'localstack/localstack:2.1.0',
POSTGRESQL_IMAGE: 'postgres:15.3-alpine',
MARIADB_IMAGE: 'mariadb:11.0',
MYSQL_IMAGE: 'mysql:8.0.3',
}
2 changes: 1 addition & 1 deletion generators/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ module.exports = class extends BaseGenerator {
const testJavaTemplates = [
'ApplicationIntegrationTest.java',
'common/AbstractIntegrationTest.java',
'common/DBContainerInitializer.java'
'TestApplication.java'
];
if(configOptions.features.includes("localstack")) {
testJavaTemplates.push('common/LocalStackConfig.java');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ spring.datasource.password=secret

<%_ if (features.includes('localstack')) { _%>
###AWS
spring.cloud.aws.sqs.endpoint=http://localhost:4566
spring.cloud.aws.sqs.region=us-east-1
spring.cloud.aws.s3.endpoint=http://localhost:4566
spring.cloud.aws.s3.region=us-east-1
spring.cloud.aws.endpoint=http://localhost:4566

spring.cloud.aws.credentials.access-key=noop
spring.cloud.aws.credentials.secret-key=noop
Expand Down
41 changes: 41 additions & 0 deletions generators/server/templates/app/src/test/java/TestApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package <%= packageName %>;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.context.annotation.Bean;
<%_ if (databaseType === 'postgresql') { _%>
import org.testcontainers.containers.PostgreSQLContainer;
<%_ } _%>
<%_ if (databaseType === 'mysql') { _%>
import org.testcontainers.containers.MySQLContainer;
<%_ } _%>
<%_ if (databaseType === 'mariadb') { _%>
import org.testcontainers.containers.MariaDBContainer;
<%_ } _%>

@TestConfiguration(proxyBeanMethods = false)
public class TestApplication {

@Bean
@ServiceConnection
<%_ if (databaseType === 'postgresql') { _%>
PostgreSQLContainer<?> postgreSQLContainer() {
return new PostgreSQLContainer<>("<%= POSTGRESQL_IMAGE %>");
}
<%_ } _%>
<%_ if (databaseType === 'mysql') { _%>
MySQLContainer<?> sqlContainer () {
return new MySQLContainer<>("<%= MYSQL_IMAGE %>");
}
<%_ } _%>
<%_ if (databaseType === 'mariadb') { _%>
MariaDBContainer<?> sqlContainer () {
return new MariaDBContainer<>("<%= MARIADB_IMAGE %>");
}
<%_ } _%>

public static void main(String[] args) {
SpringApplication.from(Application::main).with(TestApplication.class).run(args);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,25 @@
import static <%= packageName %>.utils.AppConstants.PROFILE_TEST;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;

import <%= packageName %>.TestApplication;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.ActiveProfiles;
<%_ if (features.includes('localstack')) { _%>
import org.springframework.test.context.ContextConfiguration;
<%_ } _%>
import org.springframework.test.web.servlet.MockMvc;

@ActiveProfiles({PROFILE_TEST})
@SpringBootTest(webEnvironment = RANDOM_PORT)
@ContextConfiguration(initializers = {DBContainerInitializer.class <%_ if (features.includes('localstack')) { _%>,LocalStackConfig.class<%_ } _%>})
<%_ if (features.includes('localstack')) { _%>
@ContextConfiguration(initializers = {LocalStackConfig.class})
<%_ } _%>
@AutoConfigureMockMvc
@Import(TestApplication.class)
public abstract class AbstractIntegrationTest {

@Autowired protected MockMvc mockMvc;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package <%= packageName %>.common;

import static org.testcontainers.containers.localstack.LocalStackContainer.Service.S3;
import static org.testcontainers.containers.localstack.LocalStackContainer.Service.SQS;

import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
Expand All @@ -11,22 +8,17 @@

public class LocalStackConfig
implements ApplicationContextInitializer<ConfigurableApplicationContext> {
static LocalStackContainer localStackContainer;
static final LocalStackContainer localStackContainer = new LocalStackContainer(DockerImageName.parse("<%= LOCALSTACK_IMAGE %>"));

static {
localStackContainer =
new LocalStackContainer(DockerImageName.parse("<%= LOCALSTACK_IMAGE %>"))
.withServices(S3, SQS)
.withExposedPorts(4566);
localStackContainer.start();
}

@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
TestPropertyValues.of(
"spring.cloud.aws.sqs.endpoint="
+ localStackContainer.getEndpointOverride(SQS),
"spring.cloud.aws.sqs.region=" + localStackContainer.getRegion(),
"spring.cloud.aws.endpoint="
+ localStackContainer.getEndpoint(),
"spring.cloud.aws.region.static=" + localStackContainer.getRegion(),
"spring.cloud.aws.credentials.access-key="
+ localStackContainer.getAccessKey(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
<%_ if (features.includes('localstack')) { _%>
spring.cloud.aws.sqs.endpoint=http://localhost:4566
spring.cloud.aws.sqs.region=us-east-1
spring.cloud.aws.s3.endpoint=http://localhost:4566
spring.cloud.aws.s3.region=us-east-1
spring.cloud.aws.endpoint=http://localhost:4566

spring.cloud.aws.credentials.access-key=noop
spring.cloud.aws.credentials.secret-key=noop
Expand Down
2 changes: 1 addition & 1 deletion generators/server/templates/gradle/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ dependencies {
<%_ } _%>

testImplementation "org.springframework.boot:spring-boot-starter-test"
testImplementation "org.springframework.boot:spring-boot-testcontainers"
testImplementation "org.projectlombok:lombok"
testImplementation "org.awaitility:awaitility"
testImplementation "org.testcontainers:junit-jupiter"
Expand All @@ -104,7 +105,6 @@ dependencyManagement {
<%_ if (features.includes("localstack")) { _%>
mavenBom "io.awspring.cloud:spring-cloud-aws-dependencies:${spring_cloud_aws_version}"
<%_ } _%>
mavenBom "org.testcontainers:testcontainers-bom:${testcontainers_version}"
}
}

Expand Down
18 changes: 5 additions & 13 deletions generators/server/templates/maven/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,11 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-testcontainers</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
Expand Down Expand Up @@ -210,12 +215,6 @@
<artifactId>localstack</artifactId>
<scope>test</scope>
</dependency>
<!-- AWS SDK v1 is required by testcontainers-localstack -->
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-core</artifactId>
<scope>test</scope>
</dependency>
<%_ } _%>
</dependencies>

Expand All @@ -237,13 +236,6 @@
<scope>import</scope>
</dependency>
<%_ } _%>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers-bom</artifactId>
<version>${testcontainers.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down

0 comments on commit 485d4a3

Please sign in to comment.