diff --git a/src/main/resources/driven-adapter/kms-reactive/kms-adapter.java.mustache b/src/main/resources/driven-adapter/kms-reactive/kms-adapter.java.mustache index bb8093a1..8954a5d6 100644 --- a/src/main/resources/driven-adapter/kms-reactive/kms-adapter.java.mustache +++ b/src/main/resources/driven-adapter/kms-reactive/kms-adapter.java.mustache @@ -1,6 +1,7 @@ package {{package}}.kms; -import org.springframework.beans.factory.annotation.Value; +import {{package}}.kms.config.model.KmsConnectionProperties; +import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Component; import reactor.core.publisher.Mono; import software.amazon.awssdk.core.SdkBytes; @@ -14,25 +15,21 @@ import software.amazon.awssdk.services.kms.model.EncryptionAlgorithmSpec; import java.util.Base64; @Component +@RequiredArgsConstructor public class KmsAdapter // implements ModelRepository from domain { - private final String keyId; private final KmsAsyncClient kmsAsyncClient; - - public KmsAdapter(@Value("${adapters.aws.kms.keyId}") String keyId, KmsAsyncClient kmsAsyncClient) { - this.keyId = keyId; - this.kmsAsyncClient = kmsAsyncClient; - } + private final KmsConnectionProperties kmsProperties; public Mono decrypt(String secretKey) { - return Mono.fromFuture(kmsAsyncClient.decrypt(getDecryptRequest(secretKey, keyId))) + return Mono.fromFuture(kmsAsyncClient.decrypt(getDecryptRequest(secretKey, kmsProperties.keyId()))) .map(DecryptResponse::plaintext) .map(SdkBytes::asByteArray) .switchIfEmpty(Mono.error(new Throwable("Error decrypt secret"))); } public Mono encrypt(byte[] secretKey) { - return Mono.fromFuture(kmsAsyncClient.encrypt(getEncryptRequest(secretKey, keyId))) + return Mono.fromFuture(kmsAsyncClient.encrypt(getEncryptRequest(secretKey, kmsProperties.keyId()))) .map(EncryptResponse::ciphertextBlob) .map(SdkBytes::asByteArray) .map(Base64.getEncoder()::encodeToString) diff --git a/src/main/resources/driven-adapter/kms-reactive/kms-config.java.mustache b/src/main/resources/driven-adapter/kms-reactive/kms-config.java.mustache index ea1aabf7..f4ea123a 100644 --- a/src/main/resources/driven-adapter/kms-reactive/kms-config.java.mustache +++ b/src/main/resources/driven-adapter/kms-reactive/kms-config.java.mustache @@ -34,7 +34,7 @@ public class KmsConfig { private KmsAsyncClientBuilder getBuilder(KmsConnectionProperties kmsProperties, MetricPublisher publisher) { return KmsAsyncClient.builder() .overrideConfiguration(o -> o.addMetricPublisher(publisher)) - .region(Region.of(kmsProperties.getRegion())); + .region(Region.of(kmsProperties.region())); } {{/metrics}} {{^metrics}} @@ -53,7 +53,7 @@ public class KmsConfig { } private KmsAsyncClientBuilder getBuilder(KmsConnectionProperties kmsProperties) { - return KmsAsyncClient.builder().region(Region.of(kmsProperties.getRegion())); + return KmsAsyncClient.builder().region(Region.of(kmsProperties.region())); } {{/metrics}} diff --git a/src/main/resources/driven-adapter/kms-reactive/kms-connection-properties.java.mustache b/src/main/resources/driven-adapter/kms-reactive/kms-connection-properties.java.mustache index 16e1b354..99b68e3c 100644 --- a/src/main/resources/driven-adapter/kms-reactive/kms-connection-properties.java.mustache +++ b/src/main/resources/driven-adapter/kms-reactive/kms-connection-properties.java.mustache @@ -1,40 +1,16 @@ package {{package}}.kms.config.model; -import org.springframework.context.annotation.Configuration; import org.springframework.boot.context.properties.ConfigurationProperties; -import lombok.AllArgsConstructor; -import lombok.Getter; -import lombok.NoArgsConstructor; -import lombok.Setter; - -{{#lombok}} -@Getter -@Setter -@NoArgsConstructor -@AllArgsConstructor -{{/lombok}} -@Configuration @ConfigurationProperties(prefix = "adapters.aws.kms") -public class KmsConnectionProperties { - - String host; - String region; - String protocol; - Integer port; - String keyId; -{{^lombok}} - public KmsConnectionProperties(String host, String region, String protocol, Integer port, String keyId ) { - this.host = host; - this.region = region; - this.protocol = protocol; - this.port = port; - this.keyId = keyId; - } -{{/lombok}} +public record KmsConnectionProperties( + String host, + String region, + String protocol, + Integer port, + String keyId) { public String getEndpoint() { return String.format("%s://%s:%s", protocol, host, port); } - } diff --git a/src/main/resources/driven-adapter/kms/kms-adapter.java.mustache b/src/main/resources/driven-adapter/kms/kms-adapter.java.mustache index 6c277c9c..187be55e 100644 --- a/src/main/resources/driven-adapter/kms/kms-adapter.java.mustache +++ b/src/main/resources/driven-adapter/kms/kms-adapter.java.mustache @@ -1,6 +1,7 @@ package {{package}}.kms; -import org.springframework.beans.factory.annotation.Value; +import {{package}}.kms.config.model.KmsConnectionProperties; +import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Component; import software.amazon.awssdk.core.SdkBytes; import software.amazon.awssdk.services.kms.KmsAsyncClient; @@ -12,27 +13,26 @@ import java.util.Base64; import java.util.concurrent.ExecutionException; @Component +@RequiredArgsConstructor public class KmsAdapter // implements ModelRepository from domain { - private final String keyId; private final KmsAsyncClient kmsAsyncClient; - - public KmsAdapter(@Value("${adapters.aws.kms.keyId}") String keyId, KmsAsyncClient kmsAsyncClient) { - this.keyId = keyId; - this.kmsAsyncClient = kmsAsyncClient; - } + private final KmsConnectionProperties kmsProperties; public byte[] decrypt(String secretKey) throws ExecutionException, InterruptedException { - return kmsAsyncClient.decrypt(getDecryptRequest(secretKey, keyId)) + return kmsAsyncClient.decrypt(getDecryptRequest(secretKey, kmsProperties.keyId())) .get() .plaintext() .asByteArray(); } public String encrypt(byte[] secretKey) throws ExecutionException, InterruptedException { - return Base64.getEncoder().encodeToString(kmsAsyncClient.encrypt(getEncryptRequest(secretKey, keyId)) + return Base64.getEncoder().encodeToString( + kmsAsyncClient.encrypt(getEncryptRequest(secretKey, kmsProperties.keyId())) .get() - .ciphertextBlob().asByteArray()); + .ciphertextBlob() + .asByteArray() + ); } private DecryptRequest getDecryptRequest(String secretKey, String keyId) { diff --git a/src/main/resources/driven-adapter/kms/kms-config.java.mustache b/src/main/resources/driven-adapter/kms/kms-config.java.mustache index ea1aabf7..f4ea123a 100644 --- a/src/main/resources/driven-adapter/kms/kms-config.java.mustache +++ b/src/main/resources/driven-adapter/kms/kms-config.java.mustache @@ -34,7 +34,7 @@ public class KmsConfig { private KmsAsyncClientBuilder getBuilder(KmsConnectionProperties kmsProperties, MetricPublisher publisher) { return KmsAsyncClient.builder() .overrideConfiguration(o -> o.addMetricPublisher(publisher)) - .region(Region.of(kmsProperties.getRegion())); + .region(Region.of(kmsProperties.region())); } {{/metrics}} {{^metrics}} @@ -53,7 +53,7 @@ public class KmsConfig { } private KmsAsyncClientBuilder getBuilder(KmsConnectionProperties kmsProperties) { - return KmsAsyncClient.builder().region(Region.of(kmsProperties.getRegion())); + return KmsAsyncClient.builder().region(Region.of(kmsProperties.region())); } {{/metrics}} diff --git a/src/main/resources/driven-adapter/kms/kms-connection-properties.java.mustache b/src/main/resources/driven-adapter/kms/kms-connection-properties.java.mustache index 16e1b354..99b68e3c 100644 --- a/src/main/resources/driven-adapter/kms/kms-connection-properties.java.mustache +++ b/src/main/resources/driven-adapter/kms/kms-connection-properties.java.mustache @@ -1,40 +1,16 @@ package {{package}}.kms.config.model; -import org.springframework.context.annotation.Configuration; import org.springframework.boot.context.properties.ConfigurationProperties; -import lombok.AllArgsConstructor; -import lombok.Getter; -import lombok.NoArgsConstructor; -import lombok.Setter; - -{{#lombok}} -@Getter -@Setter -@NoArgsConstructor -@AllArgsConstructor -{{/lombok}} -@Configuration @ConfigurationProperties(prefix = "adapters.aws.kms") -public class KmsConnectionProperties { - - String host; - String region; - String protocol; - Integer port; - String keyId; -{{^lombok}} - public KmsConnectionProperties(String host, String region, String protocol, Integer port, String keyId ) { - this.host = host; - this.region = region; - this.protocol = protocol; - this.port = port; - this.keyId = keyId; - } -{{/lombok}} +public record KmsConnectionProperties( + String host, + String region, + String protocol, + Integer port, + String keyId) { public String getEndpoint() { return String.format("%s://%s:%s", protocol, host, port); } - } diff --git a/src/main/resources/driven-adapter/r2dbc-postgresql/config/postgresql-connection-pool.java.mustache b/src/main/resources/driven-adapter/r2dbc-postgresql/config/postgresql-connection-pool.java.mustache index 283db1cd..118f77d1 100644 --- a/src/main/resources/driven-adapter/r2dbc-postgresql/config/postgresql-connection-pool.java.mustache +++ b/src/main/resources/driven-adapter/r2dbc-postgresql/config/postgresql-connection-pool.java.mustache @@ -1,14 +1,14 @@ package {{package}}.r2dbc.config; -import java.time.Duration; import io.r2dbc.pool.ConnectionPool; import io.r2dbc.pool.ConnectionPoolConfiguration; import io.r2dbc.postgresql.PostgresqlConnectionConfiguration; import io.r2dbc.postgresql.PostgresqlConnectionFactory; - import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import java.time.Duration; + @Configuration public class PostgreSQLConnectionPool { // TODO: change pool connection properties based on your resources. @@ -17,28 +17,15 @@ public class PostgreSQLConnectionPool { public static final int MAX_IDLE_TIME = 30; @Bean - public ConnectionPool getConnectionConfig() { - // TODO: change these properties for yours - PostgresqlConnectionProperties pgProperties = new PostgresqlConnectionProperties(); - pgProperties.setDatabase("postgres"); - pgProperties.setHost("localhost"); - pgProperties.setPort(5432); - pgProperties.setUsername("postgres"); - pgProperties.setPassword("secret"); - pgProperties.setSchema("public"); - - return buildConnectionConfiguration(pgProperties); - } - - private ConnectionPool buildConnectionConfiguration(PostgresqlConnectionProperties properties) { + public ConnectionPool getConnectionConfig(PostgresqlConnectionProperties properties) { PostgresqlConnectionConfiguration dbConfiguration = PostgresqlConnectionConfiguration.builder() - .host(properties.getHost()) - .port(properties.getPort()) - .database(properties.getDatabase()) - .schema(properties.getSchema()) - .username(properties.getUsername()) - .password(properties.getPassword()) - .build(); + .host(properties.host()) + .port(properties.port()) + .database(properties.database()) + .schema(properties.schema()) + .username(properties.username()) + .password(properties.password()) + .build(); ConnectionPoolConfiguration poolConfiguration = ConnectionPoolConfiguration.builder() .connectionFactory(new PostgresqlConnectionFactory(dbConfiguration)) @@ -51,4 +38,4 @@ public class PostgreSQLConnectionPool { return new ConnectionPool(poolConfiguration); } -} +} \ No newline at end of file diff --git a/src/main/resources/driven-adapter/r2dbc-postgresql/config/postgresql-connection-pool.unit.test.java.mustache b/src/main/resources/driven-adapter/r2dbc-postgresql/config/postgresql-connection-pool.unit.test.java.mustache index a03babe1..f22c0a25 100644 --- a/src/main/resources/driven-adapter/r2dbc-postgresql/config/postgresql-connection-pool.unit.test.java.mustache +++ b/src/main/resources/driven-adapter/r2dbc-postgresql/config/postgresql-connection-pool.unit.test.java.mustache @@ -1,15 +1,37 @@ package {{package}}.r2dbc.config; -import {{package}}.r2dbc.config.PostgreSQLConnectionPool; -import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.mockito.Mockito.when; class PostgreSQLConnectionPoolTest { - // TODO: change four you own tests + @InjectMocks + private PostgreSQLConnectionPool connectionPool; + + @Mock + private PostgresqlConnectionProperties properties; + + + @BeforeEach + void setUp() { + MockitoAnnotations.openMocks(this); + + when(properties.host()).thenReturn("localhost"); + when(properties.port()).thenReturn(5432); + when(properties.database()).thenReturn("dbName"); + when(properties.schema()).thenReturn("schema"); + when(properties.username()).thenReturn("username"); + when(properties.password()).thenReturn("password"); + } + @Test - void getConnectionConfig() { - PostgreSQLConnectionPool postgreSQLConnectionPool= new PostgreSQLConnectionPool(); - Assertions.assertNotNull(postgreSQLConnectionPool.getConnectionConfig()); + void getConnectionConfigSuccess() { + assertNotNull(connectionPool.getConnectionConfig(properties)); } } diff --git a/src/main/resources/driven-adapter/r2dbc-postgresql/config/postgresql-connection-properties.java.mustache b/src/main/resources/driven-adapter/r2dbc-postgresql/config/postgresql-connection-properties.java.mustache index bd4685d1..f8cc0e2e 100644 --- a/src/main/resources/driven-adapter/r2dbc-postgresql/config/postgresql-connection-properties.java.mustache +++ b/src/main/resources/driven-adapter/r2dbc-postgresql/config/postgresql-connection-properties.java.mustache @@ -1,83 +1,14 @@ package {{package}}.r2dbc.config; -{{#lombok}} -import lombok.AllArgsConstructor; -import lombok.Getter; -import lombok.NoArgsConstructor; -import lombok.Setter; - -@Getter -@Setter -@NoArgsConstructor -@AllArgsConstructor -{{/lombok}} -public class PostgresqlConnectionProperties { - - private String database; - private String schema; - private String username; - private String password; - private String host; - private Integer port; - - {{^lombok}} - public PostgresqlConnectionProperties(){} - - public PostgresqlConnectionProperties(String database, String schema, String username, String password, String host, Integer port) { - this.database = database; - this.schema = schema; - this.username = username; - this.password = password; - this.host = host; - this.port = port; - } - - public String getDatabase() { - return database; - } - - public void setDatabase(String database) { - this.database = database; - } - - public String getSchema() { - return schema; - } - - public void setSchema(String schema) { - this.schema = schema; - } - - public String getUsername() { - return username; - } - - public void setUsername(String username) { - this.username = username; - } - - public String getPassword() { - return password; - } - - public void setPassword(String password) { - this.password = password; - } - - public String getHost() { - return host; - } - - public void setHost(String host) { - this.host = host; - } - - public Integer getPort() { - return port; - } - - public void setPort(Integer port) { - this.port = port; - } - {{/lombok}} +// TODO: Load properties from the application.yaml file or from secrets manager +// import org.springframework.boot.context.properties.ConfigurationProperties; + +// @ConfigurationProperties(prefix = "adapters.r2dbc") +public record PostgresqlConnectionProperties( + String host, + Integer port, + String database, + String schema, + String username, + String password) { } diff --git a/src/main/resources/driven-adapter/redis/redis-repository/build.gradle.mustache b/src/main/resources/driven-adapter/redis/redis-repository/build.gradle.mustache index 1c9dbcf2..1639c427 100644 --- a/src/main/resources/driven-adapter/redis/redis-repository/build.gradle.mustache +++ b/src/main/resources/driven-adapter/redis/redis-repository/build.gradle.mustache @@ -10,7 +10,6 @@ dependencies { implementation("com.github.bancolombia:vault-sync:{{SECRETS_VERSION}}") {{/include-vaultsecrets}} - implementation 'org.reactivecommons.utils:object-mapper-api:{{REACTIVE_COMMONS_MAPPER_VERSION}}' testImplementation 'org.reactivecommons.utils:object-mapper:{{REACTIVE_COMMONS_MAPPER_VERSION}}' diff --git a/src/main/resources/driven-adapter/s3-reactive/s3-config.java.mustache b/src/main/resources/driven-adapter/s3-reactive/s3-config.java.mustache index 8feaeb53..219b62f9 100644 --- a/src/main/resources/driven-adapter/s3-reactive/s3-config.java.mustache +++ b/src/main/resources/driven-adapter/s3-reactive/s3-config.java.mustache @@ -22,7 +22,7 @@ public class S3Config { @Bean public S3AsyncClient s3AsyncClient(S3ConnectionProperties s3Properties) { return S3AsyncClient.builder() - .region(Region.of(s3Properties.getRegion())) + .region(Region.of(s3Properties.region())) .build(); } @@ -30,9 +30,9 @@ public class S3Config { @Bean public S3AsyncClient localS3AsyncClient(S3ConnectionProperties s3Properties) { return S3AsyncClient.builder() - .region(Region.of(s3Properties.getRegion())) + .region(Region.of(s3Properties.region())) .credentialsProvider(ProfileCredentialsProvider.create("default")) - .endpointOverride(URI.create(s3Properties.getEndpoint())) + .endpointOverride(URI.create(s3Properties.endpoint())) .build(); } {{/metrics}} @@ -42,7 +42,7 @@ public class S3Config { public S3AsyncClient s3AsyncClient(S3ConnectionProperties s3Properties, MetricPublisher publisher) { return S3AsyncClient.builder() .overrideConfiguration(o -> o.addMetricPublisher(publisher)) - .region(Region.of(s3Properties.getRegion())) + .region(Region.of(s3Properties.region())) .build(); } @@ -52,9 +52,9 @@ public class S3Config { MetricPublisher publisher) { return S3AsyncClient.builder() .overrideConfiguration(o -> o.addMetricPublisher(publisher)) - .region(Region.of(s3Properties.getRegion())) + .region(Region.of(s3Properties.region())) .credentialsProvider(ProfileCredentialsProvider.create("default")) - .endpointOverride(URI.create(s3Properties.getEndpoint())) + .endpointOverride(URI.create(s3Properties.endpoint())) .build(); } {{/metrics}} diff --git a/src/main/resources/driven-adapter/s3-reactive/s3-connection-properties.java.mustache b/src/main/resources/driven-adapter/s3-reactive/s3-connection-properties.java.mustache index 22552718..a8b8ab4e 100644 --- a/src/main/resources/driven-adapter/s3-reactive/s3-connection-properties.java.mustache +++ b/src/main/resources/driven-adapter/s3-reactive/s3-connection-properties.java.mustache @@ -1,51 +1,10 @@ package {{package}}.s3.config.model; -{{#lombok}} -import lombok.AllArgsConstructor; -import lombok.Getter; -import lombok.NoArgsConstructor; -import lombok.Setter; -{{/lombok}} import org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.context.annotation.Configuration; -{{#lombok}} -@Getter -@Setter -@NoArgsConstructor -@AllArgsConstructor -{{/lombok}} -@Configuration -@ConfigurationProperties(prefix = "adapter.aws.s3") -public class S3ConnectionProperties { - - private String bucketName; - private String region; - private String endpoint; - -{{^lombok}} - public String getBucketName() { - return bucketName; - } - - public void setBucketName(String bucketName) { - this.bucketName = bucketName; - } - - public String getRegion() { - return region; - } - - public void setRegion(String region) { - this.region = region; - } - - public String getEndpoint() { - return endpoint; - } - - public void setEndpoint(String endpoint) { - this.endpoint = endpoint; - } -{{/lombok}} +@ConfigurationProperties(prefix = "adapters.aws.s3") +public record S3ConnectionProperties( + String endpoint, + String region, + String bucketName) { } diff --git a/src/main/resources/driven-adapter/s3/s3-config.java.mustache b/src/main/resources/driven-adapter/s3/s3-config.java.mustache index 6cbe11e1..e13db909 100644 --- a/src/main/resources/driven-adapter/s3/s3-config.java.mustache +++ b/src/main/resources/driven-adapter/s3/s3-config.java.mustache @@ -23,7 +23,7 @@ public class S3Config { public S3Client s3Client(S3ConnectionProperties s3Properties) { return S3Client.builder() .credentialsProvider(WebIdentityTokenFileCredentialsProvider.create()) - .region(Region.of(s3Properties.getRegion())) + .region(Region.of(s3Properties.region())) .build(); } @@ -31,9 +31,9 @@ public class S3Config { @Bean public S3Client localS3Client(S3ConnectionProperties s3Properties) { return S3Client.builder() - .region(Region.of(s3Properties.getRegion())) + .region(Region.of(s3Properties.region())) .credentialsProvider(ProfileCredentialsProvider.create("default")) - .endpointOverride(URI.create(s3Properties.getEndpoint())) + .endpointOverride(URI.create(s3Properties.endpoint())) .build(); } {{/metrics}} @@ -44,7 +44,7 @@ public class S3Config { return S3Client.builder() .overrideConfiguration(o -> o.addMetricPublisher(publisher)) .credentialsProvider(WebIdentityTokenFileCredentialsProvider.create()) - .region(Region.of(s3Properties.getRegion())) + .region(Region.of(s3Properties.region())) .build(); } @@ -52,7 +52,7 @@ public class S3Config { @Bean public S3Client localS3Client(S3ConnectionProperties s3Properties, MetricPublisher publisher) { return S3Client.builder() - .endpointOverride(URI.create(s3Properties.getEndpoint())) + .endpointOverride(URI.create(s3Properties.endpoint())) .overrideConfiguration(o -> o.addMetricPublisher(publisher)) .credentialsProvider(ProfileCredentialsProvider.create("default")) .build(); diff --git a/src/main/resources/driven-adapter/s3/s3-connection-properties.java.mustache b/src/main/resources/driven-adapter/s3/s3-connection-properties.java.mustache index f7181367..a8b8ab4e 100644 --- a/src/main/resources/driven-adapter/s3/s3-connection-properties.java.mustache +++ b/src/main/resources/driven-adapter/s3/s3-connection-properties.java.mustache @@ -1,52 +1,10 @@ package {{package}}.s3.config.model; -{{#lombok}} -import lombok.AllArgsConstructor; -import lombok.Getter; -import lombok.NoArgsConstructor; -import lombok.Setter; -{{/lombok}} import org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.context.annotation.Configuration; -{{#lombok}} -@Getter -@Setter -@NoArgsConstructor -@AllArgsConstructor -{{/lombok}} -@Configuration -@ConfigurationProperties(prefix = "adapter.aws.s3") -public class S3ConnectionProperties { - - private String bucketName; - private String region; - private String endpoint; - -{{^lombok}} - public String getBucketName() { - return bucketName; - } - - public void setBucketName(String bucketName) { - this.bucketName = bucketName; - } - - public String getRegion() { - return region; - } - - public void setRegion(String region) { - this.region = region; - } - - public String getEndpoint() { - return endpoint; - } - - public void setEndpoint(String endpoint) { - this.endpoint = endpoint; - } -{{/lombok}} - -} \ No newline at end of file +@ConfigurationProperties(prefix = "adapters.aws.s3") +public record S3ConnectionProperties( + String endpoint, + String region, + String bucketName) { +} diff --git a/src/main/resources/driven-adapter/secrets-vault-reactive/secrets-config.java.mustache b/src/main/resources/driven-adapter/secrets-vault-reactive/secrets-config.java.mustache index f456cefc..3aaaae1a 100644 --- a/src/main/resources/driven-adapter/secrets-vault-reactive/secrets-config.java.mustache +++ b/src/main/resources/driven-adapter/secrets-vault-reactive/secrets-config.java.mustache @@ -14,7 +14,7 @@ public class SecretsConfig { @Bean public GenericManagerAsync getSecretManager(@Value("${vault.host:localhost}") String host, @Value("${vault.port:8200}") int port) throws SecretException { - VaultSecretManagerConfigurator configurator = VaultSecretManagerConfigurator.builder() + var configurator = VaultSecretManagerConfigurator.builder() .withProperties(VaultSecretsManagerProperties.builder() .host(host) .port(port) diff --git a/src/main/resources/driven-adapter/secrets-vault/secrets-config.java.mustache b/src/main/resources/driven-adapter/secrets-vault/secrets-config.java.mustache index 5875f807..19cc8dba 100644 --- a/src/main/resources/driven-adapter/secrets-vault/secrets-config.java.mustache +++ b/src/main/resources/driven-adapter/secrets-vault/secrets-config.java.mustache @@ -14,7 +14,7 @@ public class SecretsConfig { @Bean public GenericManager getSecretManager(@Value("${vault.host:localhost}") String host, @Value("${vault.port:8200}") int port) throws SecretException { - VaultSecretManagerConfigurator configurator = VaultSecretManagerConfigurator.builder() + var configurator = VaultSecretManagerConfigurator.builder() .withProperties(VaultSecretsManagerProperties.builder() .host(host) .port(port) diff --git a/src/main/resources/driven-adapter/sqs-reactive/sqs-sender-config.java.mustache b/src/main/resources/driven-adapter/sqs-reactive/sqs-sender-config.java.mustache index 9221a273..5379fa3a 100644 --- a/src/main/resources/driven-adapter/sqs-reactive/sqs-sender-config.java.mustache +++ b/src/main/resources/driven-adapter/sqs-reactive/sqs-sender-config.java.mustache @@ -27,7 +27,7 @@ public class SQSSenderConfig { public SqsAsyncClient configSqs(SQSSenderProperties properties, MetricPublisher publisher) { return SqsAsyncClient.builder() .endpointOverride(resolveEndpoint(properties)) - .region(Region.of(properties.getRegion())) + .region(Region.of(properties.region())) .overrideConfiguration(o -> o.addMetricPublisher(publisher)) .credentialsProvider(getProviderChain()) .build(); @@ -38,7 +38,7 @@ public class SQSSenderConfig { public SqsAsyncClient configSqs(SQSSenderProperties properties) { return SqsAsyncClient.builder() .endpointOverride(resolveEndpoint(properties)) - .region(Region.of(properties.getRegion())) + .region(Region.of(properties.region())) .credentialsProvider(getProviderChain()) .build(); } @@ -56,8 +56,8 @@ public class SQSSenderConfig { } private URI resolveEndpoint(SQSSenderProperties properties) { - if (properties.getEndpoint() != null) { - return URI.create(properties.getEndpoint()); + if (properties.endpoint() != null) { + return URI.create(properties.endpoint()); } return null; } diff --git a/src/main/resources/driven-adapter/sqs-reactive/sqs-sender-properties.java.mustache b/src/main/resources/driven-adapter/sqs-reactive/sqs-sender-properties.java.mustache index 62db5446..821e42a6 100644 --- a/src/main/resources/driven-adapter/sqs-reactive/sqs-sender-properties.java.mustache +++ b/src/main/resources/driven-adapter/sqs-reactive/sqs-sender-properties.java.mustache @@ -1,53 +1,10 @@ package {{package}}.sqs.sender.config; -{{#lombok}} -import lombok.AllArgsConstructor; -import lombok.Getter; -import lombok.NoArgsConstructor; -import lombok.Setter; -{{/lombok}} import org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.context.annotation.Configuration; -{{#lombok}} -@Getter -@Setter -@NoArgsConstructor -@AllArgsConstructor -{{/lombok}} -@Configuration @ConfigurationProperties(prefix = "adapter.sqs") -public class SQSSenderProperties { - private String region; - private String queueUrl; - private String endpoint; -{{^lombok}} - - public SQSSenderProperties() { - } - - public String getRegion() { - return region; - } - - public void setRegion(String region) { - this.region = region; - } - - public String getQueueUrl() { - return queueUrl; - } - - public void setQueueUrl(String queueUrl) { - this.queueUrl = queueUrl; - } - - public String getEndpoint() { - return endpoint; - } - - public void setEndpoint(String endpoint) { - this.endpoint = endpoint; - } -{{/lombok}} +public record SQSSenderProperties( + String region, + String queueUrl, + String endpoint){ } diff --git a/src/main/resources/driven-adapter/sqs-reactive/sqs-sender.java.mustache b/src/main/resources/driven-adapter/sqs-reactive/sqs-sender.java.mustache index 11b0eef2..7a16ce68 100644 --- a/src/main/resources/driven-adapter/sqs-reactive/sqs-sender.java.mustache +++ b/src/main/resources/driven-adapter/sqs-reactive/sqs-sender.java.mustache @@ -39,7 +39,7 @@ public class SQSSender /*implements SomeGateway*/ { private SendMessageRequest buildRequest(String message) { return SendMessageRequest.builder() - .queueUrl(properties.getQueueUrl()) + .queueUrl(properties.queueUrl()) .messageBody(message) .build(); } diff --git a/src/main/resources/driven-adapter/sqs/sqs-sender-config.java.mustache b/src/main/resources/driven-adapter/sqs/sqs-sender-config.java.mustache index 0d89d6da..5379fa3a 100644 --- a/src/main/resources/driven-adapter/sqs/sqs-sender-config.java.mustache +++ b/src/main/resources/driven-adapter/sqs/sqs-sender-config.java.mustache @@ -27,7 +27,7 @@ public class SQSSenderConfig { public SqsAsyncClient configSqs(SQSSenderProperties properties, MetricPublisher publisher) { return SqsAsyncClient.builder() .endpointOverride(resolveEndpoint(properties)) - .region(Region.of(properties.getRegion())) + .region(Region.of(properties.region())) .overrideConfiguration(o -> o.addMetricPublisher(publisher)) .credentialsProvider(getProviderChain()) .build(); @@ -38,7 +38,7 @@ public class SQSSenderConfig { public SqsAsyncClient configSqs(SQSSenderProperties properties) { return SqsAsyncClient.builder() .endpointOverride(resolveEndpoint(properties)) - .region(Region.of(properties.getRegion())) + .region(Region.of(properties.region())) .credentialsProvider(getProviderChain()) .build(); } @@ -54,10 +54,10 @@ public class SQSSenderConfig { .addCredentialsProvider(InstanceProfileCredentialsProvider.create()) .build(); } - + private URI resolveEndpoint(SQSSenderProperties properties) { - if (properties.getEndpoint() != null) { - return URI.create(properties.getEndpoint()); + if (properties.endpoint() != null) { + return URI.create(properties.endpoint()); } return null; } diff --git a/src/main/resources/driven-adapter/sqs/sqs-sender-properties.java.mustache b/src/main/resources/driven-adapter/sqs/sqs-sender-properties.java.mustache index 62db5446..821e42a6 100644 --- a/src/main/resources/driven-adapter/sqs/sqs-sender-properties.java.mustache +++ b/src/main/resources/driven-adapter/sqs/sqs-sender-properties.java.mustache @@ -1,53 +1,10 @@ package {{package}}.sqs.sender.config; -{{#lombok}} -import lombok.AllArgsConstructor; -import lombok.Getter; -import lombok.NoArgsConstructor; -import lombok.Setter; -{{/lombok}} import org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.context.annotation.Configuration; -{{#lombok}} -@Getter -@Setter -@NoArgsConstructor -@AllArgsConstructor -{{/lombok}} -@Configuration @ConfigurationProperties(prefix = "adapter.sqs") -public class SQSSenderProperties { - private String region; - private String queueUrl; - private String endpoint; -{{^lombok}} - - public SQSSenderProperties() { - } - - public String getRegion() { - return region; - } - - public void setRegion(String region) { - this.region = region; - } - - public String getQueueUrl() { - return queueUrl; - } - - public void setQueueUrl(String queueUrl) { - this.queueUrl = queueUrl; - } - - public String getEndpoint() { - return endpoint; - } - - public void setEndpoint(String endpoint) { - this.endpoint = endpoint; - } -{{/lombok}} +public record SQSSenderProperties( + String region, + String queueUrl, + String endpoint){ } diff --git a/src/main/resources/driven-adapter/sqs/sqs-sender.java.mustache b/src/main/resources/driven-adapter/sqs/sqs-sender.java.mustache index fb688f2f..d13b53f4 100644 --- a/src/main/resources/driven-adapter/sqs/sqs-sender.java.mustache +++ b/src/main/resources/driven-adapter/sqs/sqs-sender.java.mustache @@ -51,7 +51,7 @@ public class SQSSender /*implements SomeGateway*/ { private SendMessageRequest buildRequest(String message) { return SendMessageRequest.builder() - .queueUrl(properties.getQueueUrl()) + .queueUrl(properties.queueUrl()) .messageBody(message) .build(); } diff --git a/src/main/resources/entry-point/sqs-reactive/build.gradle.mustache b/src/main/resources/entry-point/sqs-reactive/build.gradle.mustache index 2ba3c87e..51e0f277 100644 --- a/src/main/resources/entry-point/sqs-reactive/build.gradle.mustache +++ b/src/main/resources/entry-point/sqs-reactive/build.gradle.mustache @@ -1,7 +1,7 @@ dependencies { implementation project(':model') implementation project(':usecase') - implementation 'org.springframework.boot:spring-boot' + implementation 'org.springframework.boot:spring-boot-starter' implementation 'software.amazon.awssdk:sqs' implementation 'org.apache.logging.log4j:log4j-api' } diff --git a/src/main/resources/entry-point/sqs-reactive/sqs-config.java.mustache b/src/main/resources/entry-point/sqs-reactive/sqs-config.java.mustache index 294ea57d..6f1ab34e 100644 --- a/src/main/resources/entry-point/sqs-reactive/sqs-config.java.mustache +++ b/src/main/resources/entry-point/sqs-reactive/sqs-config.java.mustache @@ -39,7 +39,7 @@ public class SQSConfig { public SqsAsyncClient configSqs(SQSProperties properties, MetricPublisher publisher) { return SqsAsyncClient.builder() .endpointOverride(resolveEndpoint(properties)) - .region(Region.of(properties.getRegion())) + .region(Region.of(properties.region())) .overrideConfiguration(o -> o.addMetricPublisher(publisher)) .credentialsProvider(getProviderChain()) .build(); @@ -50,7 +50,7 @@ public class SQSConfig { public SqsAsyncClient configSqs(SQSProperties properties) { return SqsAsyncClient.builder() .endpointOverride(resolveEndpoint(properties)) - .region(Region.of(properties.getRegion())) + .region(Region.of(properties.region())) .credentialsProvider(getProviderChain()) .build(); } @@ -67,9 +67,9 @@ public class SQSConfig { .build(); } - private URI resolveEndpoint(SQSProperties properties) { - if (properties.getEndpoint() != null) { - return URI.create(properties.getEndpoint()); + protected URI resolveEndpoint(SQSProperties properties) { + if (properties.endpoint() != null) { + return URI.create(properties.endpoint()); } return null; } diff --git a/src/main/resources/entry-point/sqs-reactive/sqs-configTest.java.mustache b/src/main/resources/entry-point/sqs-reactive/sqs-configTest.java.mustache index fc8be448..b0bd37ef 100644 --- a/src/main/resources/entry-point/sqs-reactive/sqs-configTest.java.mustache +++ b/src/main/resources/entry-point/sqs-reactive/sqs-configTest.java.mustache @@ -2,6 +2,7 @@ package {{package}}.sqs.listener.config; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import reactor.core.publisher.Mono; @@ -10,30 +11,54 @@ import software.amazon.awssdk.metrics.LoggingMetricPublisher; {{/metrics}} import software.amazon.awssdk.services.sqs.SqsAsyncClient; -import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.when; -public class SQSConfigTest { +class SQSConfigTest { + + @InjectMocks + private SQSConfig sqsConfig; + + @Mock + private SqsAsyncClient sqsAsyncClient; @Mock - private SqsAsyncClient asyncClient; + private SQSProperties sqsProperties; @BeforeEach - void setUp(){ + void init() { MockitoAnnotations.openMocks(this); + when(sqsProperties.region()).thenReturn("us-east-1"); + when(sqsProperties.queueUrl()).thenReturn("http://localhost:4566/00000000000/queue-sqs"); + when(sqsProperties.waitTimeSeconds()).thenReturn(20); + when(sqsProperties.maxNumberOfMessages()).thenReturn(10); + when(sqsProperties.numberOfThreads()).thenReturn(1); + } + + @Test + void configSQSListenerIsNotNull() { + assertThat(sqsConfig.sqsListener(sqsAsyncClient, sqsProperties, message -> Mono.empty())).isNotNull(); } @Test - void configTest() { - SQSProperties sqsProperties = new SQSProperties(); - sqsProperties.setNumberOfThreads(1); - sqsProperties.setRegion("Region"); + void configSqsIsNotNull() { {{#metrics}} - LoggingMetricPublisher loggingMetricPublisher = LoggingMetricPublisher.create(); + var loggingMetricPublisher = LoggingMetricPublisher.create(); {{/metrics}} - SQSConfig sqsConfig = new SQSConfig(); + assertThat(sqsConfig.configSqs(sqsProperties{{#metrics}}, loggingMetricPublisher{{/metrics}})).isNotNull(); + } - assertNotNull(sqsConfig.sqsListener(asyncClient, sqsProperties, message -> Mono.empty())); - assertNotNull(sqsConfig.configSqs(sqsProperties{{#metrics}}, loggingMetricPublisher{{/metrics}})); + @Test + void configSqsWhenEndpointIsNotNull() { + {{#metrics}} + var loggingMetricPublisher = LoggingMetricPublisher.create(); + {{/metrics}} + when(sqsProperties.endpoint()).thenReturn("http://localhost:4566"); + assertThat(sqsConfig.configSqs(sqsProperties{{#metrics}}, loggingMetricPublisher{{/metrics}})).isNotNull(); + } + @Test + void resolveEndpointIsNull() { + assertThat(sqsConfig.resolveEndpoint(sqsProperties)).isNull(); } -} \ No newline at end of file +} diff --git a/src/main/resources/entry-point/sqs-reactive/sqs-listener.java.mustache b/src/main/resources/entry-point/sqs-reactive/sqs-listener.java.mustache index 2f263e87..ae28b2ea 100644 --- a/src/main/resources/entry-point/sqs-reactive/sqs-listener.java.mustache +++ b/src/main/resources/entry-point/sqs-reactive/sqs-listener.java.mustache @@ -42,11 +42,11 @@ public class SQSListener { public SQSListener start() { {{#metrics}} - this.operation = "MessageFrom:" + properties.getQueueUrl(); + this.operation = "MessageFrom:" + properties.queueUrl(); {{/metrics}} - ExecutorService service = Executors.newFixedThreadPool(properties.getNumberOfThreads()); + ExecutorService service = Executors.newFixedThreadPool(properties.numberOfThreads()); Flux flow = listenRetryRepeat().publishOn(Schedulers.fromExecutorService(service)); - for (var i = 0; i < properties.getNumberOfThreads(); i++) { + for (var i = 0; i < properties.numberOfThreads(); i++) { flow.subscribe(); } return this; @@ -85,16 +85,16 @@ public class SQSListener { private ReceiveMessageRequest getReceiveMessageRequest() { return ReceiveMessageRequest.builder() - .queueUrl(properties.getQueueUrl()) - .maxNumberOfMessages(properties.getMaxNumberOfMessages()) - .waitTimeSeconds(properties.getWaitTimeSeconds()) - .visibilityTimeout(properties.getVisibilityTimeoutSeconds()) + .queueUrl(properties.queueUrl()) + .maxNumberOfMessages(properties.maxNumberOfMessages()) + .waitTimeSeconds(properties.waitTimeSeconds()) + .visibilityTimeout(properties.visibilityTimeoutSeconds()) .build(); } private DeleteMessageRequest getDeleteMessageRequest(String receiptHandle) { return DeleteMessageRequest.builder() - .queueUrl(properties.getQueueUrl()) + .queueUrl(properties.queueUrl()) .receiptHandle(receiptHandle) .build(); } diff --git a/src/main/resources/entry-point/sqs-reactive/sqs-listenerTest.java.mustache b/src/main/resources/entry-point/sqs-reactive/sqs-listenerTest.java.mustache index 9145bc33..aa30030d 100644 --- a/src/main/resources/entry-point/sqs-reactive/sqs-listenerTest.java.mustache +++ b/src/main/resources/entry-point/sqs-reactive/sqs-listenerTest.java.mustache @@ -1,7 +1,7 @@ package {{package}}.sqs.listener.helper; -import co.com.bancolombia.sqs.listener.SQSProcessor; -import co.com.bancolombia.sqs.listener.config.SQSProperties; +import {{package}}.sqs.listener.SQSProcessor; +import {{package}}.sqs.listener.config.SQSProperties; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.Mock; @@ -10,50 +10,61 @@ import org.springframework.test.util.ReflectionTestUtils; import reactor.core.publisher.Flux; import reactor.test.StepVerifier; import software.amazon.awssdk.services.sqs.SqsAsyncClient; -import software.amazon.awssdk.services.sqs.model.*; +import software.amazon.awssdk.services.sqs.model.DeleteMessageRequest; +import software.amazon.awssdk.services.sqs.model.DeleteMessageResponse; +import software.amazon.awssdk.services.sqs.model.Message; +import software.amazon.awssdk.services.sqs.model.ReceiveMessageRequest; +import software.amazon.awssdk.services.sqs.model.ReceiveMessageResponse; import java.util.concurrent.CompletableFuture; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.when; -public class SQSListenerTest { +class SQSListenerTest { @Mock private SqsAsyncClient asyncClient; + @Mock + private SQSProperties sqsProperties; + @BeforeEach - void setUp(){ + void setUp() { MockitoAnnotations.openMocks(this); - Message message = Message.builder().body("message").build(); - DeleteMessageResponse deleteMessageResponse = DeleteMessageResponse.builder().build(); - ReceiveMessageResponse messageResponse = ReceiveMessageResponse.builder().messages(message).build(); + var sqsProperties = new SQSProperties( + "us-east-1", + "http://localhost:4566", + "http://localhost:4566/00000000000/queueName", + 20, + 30, + 10, + 1 + ); - when(asyncClient.receiveMessage(any(ReceiveMessageRequest.class))).thenReturn(CompletableFuture.completedFuture(messageResponse)); - when(asyncClient.deleteMessage(any(DeleteMessageRequest.class))).thenReturn(CompletableFuture.completedFuture(deleteMessageResponse)); + var message = Message.builder().body("message").build(); + var deleteMessageResponse = DeleteMessageResponse.builder().build(); + var messageResponse = ReceiveMessageResponse.builder().messages(message).build(); + when(asyncClient.receiveMessage(any(ReceiveMessageRequest.class))) + .thenReturn(CompletableFuture.completedFuture(messageResponse)); + when(asyncClient.deleteMessage(any(DeleteMessageRequest.class))) + .thenReturn(CompletableFuture.completedFuture(deleteMessageResponse)); } @Test void listenerTest() { - SQSProperties sqsProperties = new SQSProperties(); - sqsProperties.setNumberOfThreads(1); - sqsProperties.setRegion("Region"); - - SQSListener sqsListener = SQSListener.builder() - .client(asyncClient) - .properties(sqsProperties) - .processor(new SQSProcessor()) - {{#metrics}} - .operation("operation") - {{/metrics}} - .build(); + var sqsListener = SQSListener.builder() + .client(asyncClient) + .properties(sqsProperties) + .processor(new SQSProcessor()) + {{#metrics}} + .operation("operation") + {{/metrics}} + .build(); Flux flow = ReflectionTestUtils.invokeMethod(sqsListener, "listen"); - - StepVerifier.create(flow) - .verifyComplete(); - + StepVerifier.create(flow).verifyComplete(); } -} \ No newline at end of file +} diff --git a/src/main/resources/entry-point/sqs-reactive/sqs-properties.java.mustache b/src/main/resources/entry-point/sqs-reactive/sqs-properties.java.mustache index 73b99eaf..308aff26 100644 --- a/src/main/resources/entry-point/sqs-reactive/sqs-properties.java.mustache +++ b/src/main/resources/entry-point/sqs-reactive/sqs-properties.java.mustache @@ -1,89 +1,14 @@ package {{package}}.sqs.listener.config; -{{#lombok}} -import lombok.AllArgsConstructor; -import lombok.Getter; -import lombok.NoArgsConstructor; -import lombok.Setter; -{{/lombok}} import org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.context.annotation.Configuration; -{{#lombok}} -@Getter -@Setter -@NoArgsConstructor -@AllArgsConstructor -{{/lombok}} -@Configuration @ConfigurationProperties(prefix = "entrypoint.sqs") -public class SQSProperties { - private String region; - private String queueUrl; - private String endpoint; - private int waitTimeSeconds; - private int maxNumberOfMessages; - private int visibilityTimeoutSeconds; - private int numberOfThreads; - {{^lombok}} - - public SQSProperties() { - } - - public String getRegion() { - return region; - } - - public void setRegion(String region) { - this.region = region; - } - - public String getEndpoint() { - return endpoint; - } - - public void setEndpoint(String endpoint) { - this.endpoint = endpoint; - } - - public String getQueueUrl() { - return queueUrl; - } - - public void setQueueUrl(String queueUrl) { - this.queueUrl = queueUrl; - } - - public int getWaitTimeSeconds() { - return waitTimeSeconds; - } - - public void setWaitTimeSeconds(int waitTimeSeconds) { - this.waitTimeSeconds = waitTimeSeconds; - } - - public int getMaxNumberOfMessages() { - return maxNumberOfMessages; - } - - public void setMaxNumberOfMessages(int maxNumberOfMessages) { - this.maxNumberOfMessages = maxNumberOfMessages; - } - - public int getVisibilityTimeoutSeconds() { - return visibilityTimeoutSeconds; - } - - public void setVisibilityTimeoutSeconds(int visibilityTimeoutSeconds) { - this.visibilityTimeoutSeconds = visibilityTimeoutSeconds; - } - - public int getNumberOfThreads() { - return numberOfThreads; - } - - public void setNumberOfThreads(int numberOfThreads) { - this.numberOfThreads = numberOfThreads; - } - {{/lombok}} +public record SQSProperties( + String region, + String endpoint, + String queueUrl, + int waitTimeSeconds, + int visibilityTimeoutSeconds, + int maxNumberOfMessages, + int numberOfThreads) { } diff --git a/src/main/resources/entry-point/sqs/build.gradle.mustache b/src/main/resources/entry-point/sqs/build.gradle.mustache index 590b06d4..42c90aeb 100644 --- a/src/main/resources/entry-point/sqs/build.gradle.mustache +++ b/src/main/resources/entry-point/sqs/build.gradle.mustache @@ -1,7 +1,7 @@ dependencies { implementation project(':model') implementation project(':usecase') - implementation 'org.springframework.boot:spring-boot' + implementation 'org.springframework.boot:spring-boot-starter' implementation 'software.amazon.awssdk:sqs' implementation 'org.apache.logging.log4j:log4j-api' {{#metrics}} diff --git a/src/main/resources/entry-point/sqs/sqs-config.java.mustache b/src/main/resources/entry-point/sqs/sqs-config.java.mustache index 293cd226..d5c59cfa 100644 --- a/src/main/resources/entry-point/sqs/sqs-config.java.mustache +++ b/src/main/resources/entry-point/sqs/sqs-config.java.mustache @@ -38,7 +38,7 @@ public class SQSConfig { public SqsAsyncClient configSqs(SQSProperties properties, MetricPublisher publisher) { return SqsAsyncClient.builder() .endpointOverride(resolveEndpoint(properties)) - .region(Region.of(properties.getRegion())) + .region(Region.of(properties.region())) .overrideConfiguration(o -> o.addMetricPublisher(publisher)) .credentialsProvider(getProviderChain()) .build(); @@ -49,7 +49,7 @@ public class SQSConfig { public SqsAsyncClient configSqs(SQSProperties properties) { return SqsAsyncClient.builder() .endpointOverride(resolveEndpoint(properties)) - .region(Region.of(properties.getRegion())) + .region(Region.of(properties.region())) .credentialsProvider(getProviderChain()) .build(); } @@ -66,9 +66,9 @@ public class SQSConfig { .build(); } - private URI resolveEndpoint(SQSProperties properties) { - if (properties.getEndpoint() != null) { - return URI.create(properties.getEndpoint()); + protected URI resolveEndpoint(SQSProperties properties) { + if (properties.endpoint() != null) { + return URI.create(properties.endpoint()); } return null; } diff --git a/src/main/resources/entry-point/sqs/sqs-configTest.java.mustache b/src/main/resources/entry-point/sqs/sqs-configTest.java.mustache index d6f394ea..e6672521 100644 --- a/src/main/resources/entry-point/sqs/sqs-configTest.java.mustache +++ b/src/main/resources/entry-point/sqs/sqs-configTest.java.mustache @@ -2,6 +2,7 @@ package {{package}}.sqs.listener.config; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.MockitoAnnotations; {{#metrics}} @@ -9,29 +10,46 @@ import software.amazon.awssdk.metrics.LoggingMetricPublisher; {{/metrics}} import software.amazon.awssdk.services.sqs.SqsAsyncClient; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.mockito.Mockito.when; -public class SQSConfigTest { + +class SQSConfigTest { + + @InjectMocks + private SQSConfig sqsConfig; @Mock private SqsAsyncClient asyncClient; + @Mock + private SQSProperties sqsProperties; + @BeforeEach - void setUp(){ + void init() { MockitoAnnotations.openMocks(this); + when(sqsProperties.region()).thenReturn("us-east-1"); + when(sqsProperties.queueUrl()).thenReturn("http://localhost:4566/00000000000/massive-sqs"); + when(sqsProperties.waitTimeSeconds()).thenReturn(20); + when(sqsProperties.maxNumberOfMessages()).thenReturn(10); + when(sqsProperties.numberOfThreads()).thenReturn(1); } @Test void configTest() { - SQSProperties sqsProperties = new SQSProperties(); - sqsProperties.setNumberOfThreads(1); - sqsProperties.setRegion("Region"); {{#metrics}} - LoggingMetricPublisher loggingMetricPublisher = LoggingMetricPublisher.create(); + var loggingMetricPublisher = LoggingMetricPublisher.create(); {{/metrics}} - SQSConfig sqsConfig = new SQSConfig(); - - assertNotNull(sqsConfig.sqsListener(asyncClient, sqsProperties, message -> {})); + when(sqsProperties.endpoint()).thenReturn("http://localhost:4566"); + assertNotNull(sqsConfig.sqsListener(asyncClient, sqsProperties, message -> { + })); assertNotNull(sqsConfig.configSqs(sqsProperties{{#metrics}}, loggingMetricPublisher{{/metrics}})); } -} \ No newline at end of file + + @Test + void resolveEndpointIsNull() { + assertThat(sqsConfig.resolveEndpoint(sqsProperties)).isNull(); + } + +} diff --git a/src/main/resources/entry-point/sqs/sqs-listener.java.mustache b/src/main/resources/entry-point/sqs/sqs-listener.java.mustache index aff1dac1..d8cf9945 100644 --- a/src/main/resources/entry-point/sqs/sqs-listener.java.mustache +++ b/src/main/resources/entry-point/sqs/sqs-listener.java.mustache @@ -47,10 +47,10 @@ public class SQSListener implements Runnable { public SQSListener start() { {{#metrics}} this.timer = Metrics.timer("async_operation_flow_duration", - "operation", "MessageFrom:" + properties.getQueueUrl(), "type", "", "status", ""); + "operation", "MessageFrom:" + properties.queueUrl(), "type", "", "status", ""); {{/metrics}} - ExecutorService service = Executors.newFixedThreadPool(properties.getNumberOfThreads()); - for (int i = 0; i < properties.getNumberOfThreads(); i++) { + ExecutorService service = Executors.newFixedThreadPool(properties.numberOfThreads()); + for (var i = 0; i < properties.numberOfThreads(); i++) { service.submit(this); } return this; @@ -101,16 +101,16 @@ public class SQSListener implements Runnable { private ReceiveMessageRequest getReceiveMessageRequest() { return ReceiveMessageRequest.builder() - .queueUrl(properties.getQueueUrl()) - .maxNumberOfMessages(properties.getMaxNumberOfMessages()) - .waitTimeSeconds(properties.getWaitTimeSeconds()) - .visibilityTimeout(properties.getVisibilityTimeoutSeconds()) + .queueUrl(properties.queueUrl()) + .maxNumberOfMessages(properties.maxNumberOfMessages()) + .waitTimeSeconds(properties.waitTimeSeconds()) + .visibilityTimeout(properties.visibilityTimeoutSeconds()) .build(); } private DeleteMessageRequest getDeleteMessageRequest(String receiptHandle) { return DeleteMessageRequest.builder() - .queueUrl(properties.getQueueUrl()) + .queueUrl(properties.queueUrl()) .receiptHandle(receiptHandle) .build(); } diff --git a/src/main/resources/entry-point/sqs/sqs-listenerTest.java.mustache b/src/main/resources/entry-point/sqs/sqs-listenerTest.java.mustache index 7f01ec83..3332a629 100644 --- a/src/main/resources/entry-point/sqs/sqs-listenerTest.java.mustache +++ b/src/main/resources/entry-point/sqs/sqs-listenerTest.java.mustache @@ -1,7 +1,7 @@ package {{package}}.sqs.listener.helper; -import co.com.bancolombia.sqs.listener.SQSProcessor; -import co.com.bancolombia.sqs.listener.config.SQSProperties; +import {{package}}.sqs.listener.SQSProcessor; +import {{package}}.sqs.listener.config.SQSProperties; {{#metrics}} import io.micrometer.core.instrument.Timer; import io.micrometer.core.instrument.simple.SimpleMeterRegistry; @@ -25,13 +25,22 @@ public class SQSListenerTest { @Mock private SqsAsyncClient asyncClient; + @Mock + private SQSProperties sqsProperties; + @BeforeEach void setUp(){ MockitoAnnotations.openMocks(this); - Message message = Message.builder().body("message").build(); - DeleteMessageResponse deleteMessageResponse = DeleteMessageResponse.builder().build(); - ReceiveMessageResponse messageResponse = ReceiveMessageResponse.builder().messages(message).build(); + when(sqsProperties.region()).thenReturn("us-east-1"); + when(sqsProperties.queueUrl()).thenReturn("http://localhost:4566/00000000000/massive-sqs"); + when(sqsProperties.waitTimeSeconds()).thenReturn(20); + when(sqsProperties.maxNumberOfMessages()).thenReturn(10); + when(sqsProperties.numberOfThreads()).thenReturn(1); + + var message = Message.builder().body("message").build(); + var deleteMessageResponse = DeleteMessageResponse.builder().build(); + var messageResponse = ReceiveMessageResponse.builder().messages(message).build(); when(asyncClient.receiveMessage(any(ReceiveMessageRequest.class))).thenReturn(CompletableFuture.completedFuture(messageResponse)); when(asyncClient.deleteMessage(any(DeleteMessageRequest.class))).thenReturn(CompletableFuture.completedFuture(deleteMessageResponse)); @@ -40,11 +49,7 @@ public class SQSListenerTest { @Test void startTest(){ - SQSProperties sqsProperties = new SQSProperties(); - sqsProperties.setNumberOfThreads(1); - sqsProperties.setRegion("Region"); - - SQSListener sqsListener =SQSListener.builder() + var sqsListener = SQSListener.builder() .client(asyncClient) .properties(sqsProperties) .consumer(System.out::println) @@ -53,20 +58,13 @@ public class SQSListenerTest { {{/metrics}} .build(); - SQSListener listener = sqsListener.start(); - - Assertions.assertNotNull(listener); + Assertions.assertNotNull(sqsListener.start()); } @Test void listenerTest() { - SQSProperties sqsProperties = new SQSProperties(); - sqsProperties.setNumberOfThreads(1); - sqsProperties.setRegion("Region"); - - SQSProcessor sqsProcessor = new SQSProcessor(); - - SQSListener sqsListener =SQSListener.builder() + var sqsProcessor = new SQSProcessor(); + var sqsListener = SQSListener.builder() .client(asyncClient) .properties(sqsProperties) .consumer(sqsProcessor) @@ -76,8 +74,6 @@ public class SQSListenerTest { .build(); ReflectionTestUtils.invokeMethod(sqsListener, "listen"); - Assertions.assertNotNull(sqsListener); - } } \ No newline at end of file diff --git a/src/main/resources/entry-point/sqs/sqs-properties.java.mustache b/src/main/resources/entry-point/sqs/sqs-properties.java.mustache index 73b99eaf..308aff26 100644 --- a/src/main/resources/entry-point/sqs/sqs-properties.java.mustache +++ b/src/main/resources/entry-point/sqs/sqs-properties.java.mustache @@ -1,89 +1,14 @@ package {{package}}.sqs.listener.config; -{{#lombok}} -import lombok.AllArgsConstructor; -import lombok.Getter; -import lombok.NoArgsConstructor; -import lombok.Setter; -{{/lombok}} import org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.context.annotation.Configuration; -{{#lombok}} -@Getter -@Setter -@NoArgsConstructor -@AllArgsConstructor -{{/lombok}} -@Configuration @ConfigurationProperties(prefix = "entrypoint.sqs") -public class SQSProperties { - private String region; - private String queueUrl; - private String endpoint; - private int waitTimeSeconds; - private int maxNumberOfMessages; - private int visibilityTimeoutSeconds; - private int numberOfThreads; - {{^lombok}} - - public SQSProperties() { - } - - public String getRegion() { - return region; - } - - public void setRegion(String region) { - this.region = region; - } - - public String getEndpoint() { - return endpoint; - } - - public void setEndpoint(String endpoint) { - this.endpoint = endpoint; - } - - public String getQueueUrl() { - return queueUrl; - } - - public void setQueueUrl(String queueUrl) { - this.queueUrl = queueUrl; - } - - public int getWaitTimeSeconds() { - return waitTimeSeconds; - } - - public void setWaitTimeSeconds(int waitTimeSeconds) { - this.waitTimeSeconds = waitTimeSeconds; - } - - public int getMaxNumberOfMessages() { - return maxNumberOfMessages; - } - - public void setMaxNumberOfMessages(int maxNumberOfMessages) { - this.maxNumberOfMessages = maxNumberOfMessages; - } - - public int getVisibilityTimeoutSeconds() { - return visibilityTimeoutSeconds; - } - - public void setVisibilityTimeoutSeconds(int visibilityTimeoutSeconds) { - this.visibilityTimeoutSeconds = visibilityTimeoutSeconds; - } - - public int getNumberOfThreads() { - return numberOfThreads; - } - - public void setNumberOfThreads(int numberOfThreads) { - this.numberOfThreads = numberOfThreads; - } - {{/lombok}} +public record SQSProperties( + String region, + String endpoint, + String queueUrl, + int waitTimeSeconds, + int visibilityTimeoutSeconds, + int maxNumberOfMessages, + int numberOfThreads) { } diff --git a/src/main/resources/structure/applications/appservice/main.java.mustache b/src/main/resources/structure/applications/appservice/main.java.mustache index 29666737..4880c44e 100644 --- a/src/main/resources/structure/applications/appservice/main.java.mustache +++ b/src/main/resources/structure/applications/appservice/main.java.mustache @@ -2,8 +2,10 @@ package {{package}}; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.context.properties.ConfigurationPropertiesScan; @SpringBootApplication +@ConfigurationPropertiesScan public class MainApplication { public static void main(String[] args) { SpringApplication.run(MainApplication.class, args);