BAEL-6910, Spring Boot 3.1's ConnectionDetails abstraction

This commit is contained in:
parthiv39731 2023-10-02 23:42:50 +05:30
parent e9d8fd0f55
commit 2934a2c1b0
55 changed files with 1281 additions and 8 deletions

View File

@ -94,6 +94,75 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-testcontainers</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>r2dbc-postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-r2dbc</artifactId>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-cassandra</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-neo4j</artifactId>
</dependency>
</dependencies>
<profiles>
@ -189,6 +258,7 @@
<mockserver.version>5.14.0</mockserver.version>
<spring-boot.version>3.1.0</spring-boot.version>
<lombok-mapstruct-binding.version>0.2.0</lombok-mapstruct-binding.version>
<testcontainers.version>1.18.3</testcontainers.version>
</properties>
</project>

View File

@ -0,0 +1,16 @@
package com.baeldung.connectiondetails;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan(basePackages = "com.baeldung.connectiondetails")
public class ConnectionDetailsApplication {
public static void main(String[] args) {
SpringApplication.run(ConnectionDetailsApplication.class, args);
}
}

View File

@ -0,0 +1,107 @@
package com.baeldung.connectiondetails.adapter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class VaultAdapter {
private static final Logger logger = LoggerFactory.getLogger(VaultAdapter.class);
public static String getSecret(String secretKey) {
logger.info("call vault to get the secret of key: " + secretKey);
//Postgres keys
if (secretKey.equalsIgnoreCase("postgres_secret_key")) {
return "postgres";
}
if (secretKey.equalsIgnoreCase("postgres_user_key")) {
return "postgres";
}
if (secretKey.equalsIgnoreCase("postgres_jdbc_url")) {
return "jdbc:postgresql://localhost:15432/postgresdb";
}
//RabbitMQ Server Keys
if (secretKey.equalsIgnoreCase("rabbitmq_username")) {
return "rabbitmquser";
}
if (secretKey.equalsIgnoreCase("rabbitmq_password")) {
return "rabbitmq";
}
if (secretKey.equalsIgnoreCase("rabbitmq_port")) {
return "5672";
}
if (secretKey.equalsIgnoreCase("rabbitmq_host")) {
return "localhost";
}
//Redis Server Keys
if (secretKey.equalsIgnoreCase("redis_username")) {
return null;
}
if (secretKey.equalsIgnoreCase("redis_password")) {
return "redis";
}
if (secretKey.equalsIgnoreCase("redis_port")) {
return "6379";
}
if (secretKey.equalsIgnoreCase("redis_host")) {
return "localhost";
}
//Mongo DB Keys
if (secretKey.equalsIgnoreCase("mongo_connection_string")) {
return "mongodb://localhost:27017/demodb";
}
//r2dbc Keys
if (secretKey.equalsIgnoreCase("r2dbc_postgres_user")) {
return "postgres";
}
if (secretKey.equalsIgnoreCase("r2dbc_postgres_secret")) {
return "postgres";
}
if (secretKey.equalsIgnoreCase("r2dbc_postgres_host")) {
return "localhost";
}
if (secretKey.equalsIgnoreCase("r2dbc_postgres_port")) {
return "15432";
}
if (secretKey.equalsIgnoreCase("r2dbc_postgres_database")) {
return "postgresdb";
}
//Elastic Search Keys
if (secretKey.equalsIgnoreCase("elastic_user")) {
return "elastic";
}
if (secretKey.equalsIgnoreCase("elastic_secret")) {
return "secret";
}
if (secretKey.equalsIgnoreCase("elastic_host")) {
return "localhost";
}
if (secretKey.equalsIgnoreCase("elastic_port1")) {
return "19200";
}
if (secretKey.equalsIgnoreCase("elastic_port2")) {
return "19300";
}
//Cassandra keys
if (secretKey.equalsIgnoreCase("cassandra_user")) {
return "cassandra";
}
if (secretKey.equalsIgnoreCase("cassandra_secret")) {
return "secret";
}
if (secretKey.equalsIgnoreCase("cassandra_host")) {
return "localhost";
}
if (secretKey.equalsIgnoreCase("cassandra_port")) {
return "19042";
}
//Neo4j Keys
if (secretKey.equalsIgnoreCase("neo4j_secret")) {
return "neo4j123";
}
if (secretKey.equalsIgnoreCase("neo4j_uri")) {
return "bolt://localhost:17687";
}
return "secretVal";
}
}

View File

@ -0,0 +1,32 @@
package com.baeldung.connectiondetails.configuration;
import com.baeldung.connectiondetails.adapter.VaultAdapter;
import org.springframework.boot.autoconfigure.cassandra.CassandraConnectionDetails;
import java.util.List;
public class CustomCassandraConnectionDetails implements CassandraConnectionDetails {
@Override
public List<Node> getContactPoints() {
Node node = new Node(
VaultAdapter.getSecret("cassandra_host"),
Integer.parseInt(VaultAdapter.getSecret("cassandra_port"))
);
return List.of(node);
}
@Override
public String getUsername() {
return VaultAdapter.getSecret("cassandra_user");
}
@Override
public String getPassword() {
return VaultAdapter.getSecret("cassandra_secret");
}
@Override
public String getLocalDatacenter() {
return "datacenter-1";
}
}

View File

@ -0,0 +1,17 @@
package com.baeldung.connectiondetails.configuration;
import org.springframework.boot.autoconfigure.cassandra.CassandraConnectionDetails;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Profile;
@Configuration(proxyBeanMethods = false)
@Profile("cassandra")
public class CustomCassandraConnectionDetailsConfiguration {
@Bean
@Primary
public CassandraConnectionDetails getCustomCassandraConnectionDetails() {
return new CustomCassandraConnectionDetails();
}
}

View File

@ -0,0 +1,29 @@
package com.baeldung.connectiondetails.configuration;
import com.baeldung.connectiondetails.adapter.VaultAdapter;
import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchConnectionDetails;
import java.util.List;
public class CustomElasticsearchConnectionDetails implements ElasticsearchConnectionDetails {
@Override
public List<Node> getNodes() {
Node node1 = new Node(
VaultAdapter.getSecret("elastic_host"),
Integer.parseInt(VaultAdapter.getSecret("elastic_port1")),
Node.Protocol.HTTP
);
return List.of(node1);
}
@Override
public String getUsername() {
return VaultAdapter.getSecret("elastic_user");
}
@Override
public String getPassword() {
return VaultAdapter.getSecret("elastic_secret");
}
}

View File

@ -0,0 +1,17 @@
package com.baeldung.connectiondetails.configuration;
import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchConnectionDetails;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Profile;
@Configuration(proxyBeanMethods = false)
@Profile("elastic")
public class CustomElasticsearchConnectionDetailsConfiguration {
@Bean
@Primary
public ElasticsearchConnectionDetails getCustomElasticConnectionDetails() {
return new CustomElasticsearchConnectionDetails();
}
}

View File

@ -0,0 +1,24 @@
package com.baeldung.connectiondetails.configuration;
import com.baeldung.connectiondetails.adapter.VaultAdapter;
import org.neo4j.driver.AuthToken;
import org.neo4j.driver.AuthTokens;
import org.springframework.boot.autoconfigure.neo4j.Neo4jConnectionDetails;
import java.net.URI;
import java.net.URISyntaxException;
public class CustomNeo4jConnectionDetails implements Neo4jConnectionDetails {
@Override
public URI getUri() {
try {
return new URI(VaultAdapter.getSecret("neo4j_uri"));
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}
@Override
public AuthToken getAuthToken() {
return AuthTokens.basic("neo4j", VaultAdapter.getSecret("neo4j_secret"));
}
}

View File

@ -0,0 +1,17 @@
package com.baeldung.connectiondetails.configuration;
import org.springframework.boot.autoconfigure.neo4j.Neo4jConnectionDetails;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Profile;
@Configuration(proxyBeanMethods = false)
@Profile("neo4j")
public class CustomNeo4jConnectionDetailsConfiguration {
@Bean
@Primary
public Neo4jConnectionDetails getNeo4jConnectionDetails() {
return new CustomNeo4jConnectionDetails();
}
}

View File

@ -0,0 +1,17 @@
package com.baeldung.connectiondetails.configuration;
import org.springframework.boot.autoconfigure.jdbc.JdbcConnectionDetails;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Profile;
@Configuration(proxyBeanMethods = false)
@Profile("jdbc")
public class JdbcConnectionDetailsConfiguration {
@Bean
@Primary
public JdbcConnectionDetails getPostgresConnection() {
return new PostgresConnectionDetails();
}
}

View File

@ -0,0 +1,14 @@
package com.baeldung.connectiondetails.configuration;
import com.baeldung.connectiondetails.adapter.VaultAdapter;
import com.mongodb.ConnectionString;
import org.springframework.boot.autoconfigure.mongo.MongoConnectionDetails;
public class MongoDBConnectionDetails implements MongoConnectionDetails {
@Override
public ConnectionString getConnectionString() {
return new ConnectionString(VaultAdapter.getSecret("mongo_connection_string"));
}
}

View File

@ -0,0 +1,17 @@
package com.baeldung.connectiondetails.configuration;
import org.springframework.boot.autoconfigure.mongo.MongoConnectionDetails;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Profile;
@Configuration(proxyBeanMethods = false)
@Profile("mongo")
public class MongoDBConnectionDetailsConfiguration {
@Bean
@Primary
public MongoConnectionDetails getMongoConnectionDetails() {
return new MongoDBConnectionDetails();
}
}

View File

@ -0,0 +1,21 @@
package com.baeldung.connectiondetails.configuration;
import com.baeldung.connectiondetails.adapter.VaultAdapter;
import org.springframework.boot.autoconfigure.jdbc.JdbcConnectionDetails;
public class PostgresConnectionDetails implements JdbcConnectionDetails {
@Override
public String getUsername() {
return VaultAdapter.getSecret("postgres_user_key");
}
@Override
public String getPassword() {
return VaultAdapter.getSecret("postgres_secret_key");
}
@Override
public String getJdbcUrl() {
return VaultAdapter.getSecret("postgres_jdbc_url");
}
}

View File

@ -0,0 +1,22 @@
package com.baeldung.connectiondetails.configuration;
import com.baeldung.connectiondetails.adapter.VaultAdapter;
import io.r2dbc.spi.ConnectionFactoryOptions;
import org.springframework.boot.autoconfigure.r2dbc.R2dbcConnectionDetails;
public class R2dbcPostgresConnectionDetails implements R2dbcConnectionDetails {
@Override
public ConnectionFactoryOptions getConnectionFactoryOptions() {
ConnectionFactoryOptions options = ConnectionFactoryOptions.builder()
.option(ConnectionFactoryOptions.DRIVER, "postgresql")
.option(ConnectionFactoryOptions.HOST, VaultAdapter.getSecret("r2dbc_postgres_host"))
.option(ConnectionFactoryOptions.PORT, Integer.valueOf(VaultAdapter.getSecret("r2dbc_postgres_port")))
.option(ConnectionFactoryOptions.USER, VaultAdapter.getSecret("r2dbc_postgres_user"))
.option(ConnectionFactoryOptions.PASSWORD, VaultAdapter.getSecret("r2dbc_postgres_secret"))
.option(ConnectionFactoryOptions.DATABASE, VaultAdapter.getSecret("r2dbc_postgres_database"))
.build();
return options;
}
}

View File

@ -0,0 +1,17 @@
package com.baeldung.connectiondetails.configuration;
import org.springframework.boot.autoconfigure.r2dbc.R2dbcConnectionDetails;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Profile;
@Configuration(proxyBeanMethods = false)
@Profile("r2dbc")
public class R2dbcPostgresConnectionDetailsConfiguration {
@Bean
@Primary
public R2dbcConnectionDetails getR2dbcPostgresConnectionDetails() {
return new R2dbcPostgresConnectionDetails();
}
}

View File

@ -0,0 +1,34 @@
package com.baeldung.connectiondetails.configuration;
import com.baeldung.connectiondetails.adapter.VaultAdapter;
import org.springframework.boot.autoconfigure.amqp.RabbitConnectionDetails;
import java.util.List;
public class RabbitMQConnectionDetails implements RabbitConnectionDetails {
@Override
public String getUsername() {
return VaultAdapter.getSecret("rabbitmq_username");
}
@Override
public String getPassword() {
return VaultAdapter.getSecret("rabbitmq_password");
}
@Override
public String getVirtualHost() {
return "/";
}
@Override
public List<Address> getAddresses() {
return List.of(this.getFirstAddress());
}
@Override
public Address getFirstAddress() {
return new Address(VaultAdapter.getSecret("rabbitmq_host"),
Integer.parseInt(VaultAdapter.getSecret("rabbitmq_port")));
}
}

View File

@ -0,0 +1,17 @@
package com.baeldung.connectiondetails.configuration;
import org.springframework.boot.autoconfigure.amqp.RabbitConnectionDetails;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Profile;
@Configuration(proxyBeanMethods = false)
@Profile("rabbitmq")
public class RabbitMQConnectionDetailsConfiguration {
@Primary
@Bean
public RabbitConnectionDetails getRabbitmqConnection() {
return new RabbitMQConnectionDetails();
}
}

View File

@ -0,0 +1,26 @@
package com.baeldung.connectiondetails.configuration;
import com.baeldung.connectiondetails.adapter.VaultAdapter;
import org.springframework.boot.autoconfigure.data.redis.RedisConnectionDetails;
public class RedisCacheConnectionDetails implements RedisConnectionDetails {
@Override
public String getPassword() {
return VaultAdapter.getSecret("redis_password");
}
@Override
public Standalone getStandalone() {
return new Standalone() {
@Override
public String getHost() {
return VaultAdapter.getSecret("redis_host");
}
@Override
public int getPort() {
return Integer.parseInt(VaultAdapter.getSecret("redis_port"));
}
};
}
}

View File

@ -0,0 +1,17 @@
package com.baeldung.connectiondetails.configuration;
import org.springframework.boot.autoconfigure.data.redis.RedisConnectionDetails;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Profile;
@Configuration(proxyBeanMethods = false)
@Profile("redis")
public class RedisConnectionDetailsConfiguration {
@Bean
@Primary
public RedisConnectionDetails getRedisCacheConnection() {
return new RedisCacheConnectionDetails();
}
}

View File

@ -0,0 +1,27 @@
package com.baeldung.connectiondetails.entity.elastic;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
@Document(indexName = "person")
public class Person {
String name;
@Id
String ssn;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSsn() {
return ssn;
}
public void setSsn(String ssn) {
this.ssn = ssn;
}
}

View File

@ -0,0 +1,27 @@
package com.baeldung.connectiondetails.entity.neo4j;
import org.springframework.data.neo4j.core.schema.Id;
import org.springframework.data.neo4j.core.schema.Node;
@Node
public class Person {
@Id
String name;
String zipcode;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getZipcode() {
return zipcode;
}
public void setZipcode(String zipcode) {
this.zipcode = zipcode;
}
}

View File

@ -1,7 +0,0 @@
spring:
docker:
compose:
enabled: true
file: docker-compose.yml
autoconfigure:
exclude: org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration, org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration

View File

@ -19,7 +19,7 @@ spring:
compose:
enabled: false
autoconfigure:
exclude: org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration
exclude: org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration, org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration, org.springframework.boot.autoconfigure.data.r2dbc.R2dbcDataAutoConfiguration, org.springframework.boot.autoconfigure.data.r2dbc.R2dbcRepositoriesAutoConfiguration, org.springframework.boot.autoconfigure.r2dbc.R2dbcAutoConfiguration, org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchDataAutoConfiguration, org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchRepositoriesAutoConfiguration, org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration, org.springframework.boot.autoconfigure.data.cassandra.CassandraDataAutoConfiguration, org.springframework.boot.autoconfigure.data.cassandra.CassandraRepositoriesAutoConfiguration, org.springframework.boot.autoconfigure.data.neo4j.Neo4jDataAutoConfiguration, org.springframework.boot.autoconfigure.data.neo4j.Neo4jRepositoriesAutoConfiguration, org.springframework.boot.autoconfigure.neo4j.Neo4jAutoConfiguration
cors:
allow:

View File

@ -0,0 +1,11 @@
spring.docker.compose.enabled=true
spring.docker.compose.file=./connectiondetails/docker-compose-cassandra.yml
spring.docker.compose.skip.in-tests=false
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
spring.mustache.check-template-location=false
spring.profiles.active=cassandra
spring.cassandra.local-datacenter=dc1
#spring.cassandra.keyspace-name=spring_cassandra
#spring.cassandra.schema-action=CREATE_IF_NOT_EXISTS
spring.data.cassandra.request.timeout=20000 # Set your desired timeout in milliseconds
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration, org.springframework.boot.autoconfigure.data.mongo.MongoRepositoriesAutoConfiguration, org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration, org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration, org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration, org.springframework.boot.autoconfigure.data.r2dbc.R2dbcDataAutoConfiguration, org.springframework.boot.autoconfigure.data.r2dbc.R2dbcRepositoriesAutoConfiguration, org.springframework.boot.autoconfigure.r2dbc.R2dbcAutoConfiguration, org.springframework.boot.autoconfigure.data.neo4j.Neo4jDataAutoConfiguration, org.springframework.boot.autoconfigure.data.neo4j.Neo4jRepositoriesAutoConfiguration, org.springframework.boot.autoconfigure.neo4j.Neo4jAutoConfiguration

View File

@ -0,0 +1,7 @@
spring.docker.compose.enabled=true
spring.docker.compose.file=./connectiondetails/docker-compose-elastic.yml
spring.docker.compose.skip.in-tests=false
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
spring.mustache.check-template-location=false
spring.profiles.active=elastic
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration, org.springframework.boot.autoconfigure.data.mongo.MongoRepositoriesAutoConfiguration, org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration, org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration, org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration, org.springframework.boot.autoconfigure.data.r2dbc.R2dbcDataAutoConfiguration, org.springframework.boot.autoconfigure.data.r2dbc.R2dbcRepositoriesAutoConfiguration, org.springframework.boot.autoconfigure.r2dbc.R2dbcAutoConfiguration, org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration, org.springframework.boot.autoconfigure.data.cassandra.CassandraDataAutoConfiguration, org.springframework.boot.autoconfigure.data.cassandra.CassandraRepositoriesAutoConfiguration, org.springframework.boot.autoconfigure.data.neo4j.Neo4jDataAutoConfiguration, org.springframework.boot.autoconfigure.data.neo4j.Neo4jRepositoriesAutoConfiguration, org.springframework.boot.autoconfigure.neo4j.Neo4jAutoConfiguration

View File

@ -0,0 +1,7 @@
spring.docker.compose.enabled=true
spring.docker.compose.file=./connectiondetails/docker-compose-jdbc.yml
spring.docker.compose.skip.in-tests=false
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
spring.mustache.check-template-location=false
spring.profiles.active=jdbc
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration, org.springframework.boot.autoconfigure.data.mongo.MongoRepositoriesAutoConfiguration, org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration, org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration, org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration, org.springframework.boot.autoconfigure.data.r2dbc.R2dbcDataAutoConfiguration, org.springframework.boot.autoconfigure.data.r2dbc.R2dbcRepositoriesAutoConfiguration, org.springframework.boot.autoconfigure.r2dbc.R2dbcAutoConfiguration, org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchDataAutoConfiguration, org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchRepositoriesAutoConfiguration, org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration, org.springframework.boot.autoconfigure.data.cassandra.CassandraDataAutoConfiguration, org.springframework.boot.autoconfigure.data.cassandra.CassandraRepositoriesAutoConfiguration, org.springframework.boot.autoconfigure.data.neo4j.Neo4jDataAutoConfiguration, org.springframework.boot.autoconfigure.data.neo4j.Neo4jRepositoriesAutoConfiguration, org.springframework.boot.autoconfigure.neo4j.Neo4jAutoConfiguration

View File

@ -0,0 +1,8 @@
spring.docker.compose.enabled=true
spring.docker.compose.file=./connectiondetails/docker-compose-mongo.yml
spring.docker.compose.skip.in-tests=false
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
spring.mustache.check-template-location=false
spring.profiles.active=mongo
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration, org.springframework.boot.autoconfigure.data.mongo.MongoRepositoriesAutoConfiguration, org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration, org.springframework.boot.autoconfigure.data.r2dbc.R2dbcDataAutoConfiguration, org.springframework.boot.autoconfigure.data.r2dbc.R2dbcRepositoriesAutoConfiguration, org.springframework.boot.autoconfigure.r2dbc.R2dbcAutoConfiguration, org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchDataAutoConfiguration, org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchRepositoriesAutoConfiguration, org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration, org.springframework.boot.autoconfigure.data.cassandra.CassandraDataAutoConfiguration, org.springframework.boot.autoconfigure.data.cassandra.CassandraRepositoriesAutoConfiguration, org.springframework.boot.autoconfigure.data.neo4j.Neo4jDataAutoConfiguration, org.springframework.boot.autoconfigure.data.neo4j.Neo4jRepositoriesAutoConfiguration, org.springframework.boot.autoconfigure.neo4j.Neo4jAutoConfiguration
spring.data.mongodb.database=demodb

View File

@ -0,0 +1,7 @@
spring.docker.compose.enabled=true
spring.docker.compose.file=./connectiondetails/docker-compose-neo4j.yml
spring.docker.compose.skip.in-tests=false
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
spring.mustache.check-template-location=false
spring.profiles.active=neo4j
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration, org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration, org.springframework.boot.autoconfigure.data.mongo.MongoRepositoriesAutoConfiguration, org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration, org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration, org.springframework.boot.autoconfigure.data.r2dbc.R2dbcDataAutoConfiguration, org.springframework.boot.autoconfigure.data.r2dbc.R2dbcRepositoriesAutoConfiguration, org.springframework.boot.autoconfigure.r2dbc.R2dbcAutoConfiguration, org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchDataAutoConfiguration, org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchRepositoriesAutoConfiguration, org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration, org.springframework.boot.autoconfigure.data.cassandra.CassandraDataAutoConfiguration, org.springframework.boot.autoconfigure.data.cassandra.CassandraRepositoriesAutoConfiguration

View File

@ -0,0 +1,7 @@
spring.docker.compose.enabled=true
spring.docker.compose.file=./connectiondetails/docker-compose-jdbc.yml
spring.docker.compose.skip.in-tests=false
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
spring.mustache.check-template-location=false
spring.profiles.active=r2dbc
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration, org.springframework.boot.autoconfigure.data.mongo.MongoRepositoriesAutoConfiguration, org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration, org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration, org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration, org.springframework.boot.autoconfigure.data.jdbc.JdbcRepositoriesAutoConfiguration, org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration, org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchDataAutoConfiguration, org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchRepositoriesAutoConfiguration, org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration, org.springframework.boot.autoconfigure.data.cassandra.CassandraDataAutoConfiguration, org.springframework.boot.autoconfigure.data.cassandra.CassandraRepositoriesAutoConfiguration, org.springframework.boot.autoconfigure.data.neo4j.Neo4jDataAutoConfiguration, org.springframework.boot.autoconfigure.data.neo4j.Neo4jRepositoriesAutoConfiguration, org.springframework.boot.autoconfigure.neo4j.Neo4jAutoConfiguration

View File

@ -0,0 +1,7 @@
spring.docker.compose.enabled=true
spring.docker.compose.file=./connectiondetails/docker-compose-rabbitmq.yml
spring.docker.compose.skip.in-tests=false
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
spring.mustache.check-template-location=false
spring.profiles.active=rabbitmq
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration, org.springframework.boot.autoconfigure.data.mongo.MongoRepositoriesAutoConfiguration, org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration, org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration, org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration, org.springframework.boot.autoconfigure.data.r2dbc.R2dbcDataAutoConfiguration, org.springframework.boot.autoconfigure.data.r2dbc.R2dbcRepositoriesAutoConfiguration, org.springframework.boot.autoconfigure.r2dbc.R2dbcAutoConfiguration, org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchDataAutoConfiguration, org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchRepositoriesAutoConfiguration, org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration, org.springframework.boot.autoconfigure.data.cassandra.CassandraDataAutoConfiguration, org.springframework.boot.autoconfigure.data.cassandra.CassandraRepositoriesAutoConfiguration, org.springframework.boot.autoconfigure.data.neo4j.Neo4jDataAutoConfiguration, org.springframework.boot.autoconfigure.data.neo4j.Neo4jRepositoriesAutoConfiguration, org.springframework.boot.autoconfigure.neo4j.Neo4jAutoConfiguration

View File

@ -0,0 +1,7 @@
spring.docker.compose.enabled=true
spring.docker.compose.file=./connectiondetails/docker-compose-redis.yml
spring.docker.compose.skip.in-tests=false
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
spring.mustache.check-template-location=false
spring.profiles.active=redis
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration, org.springframework.boot.autoconfigure.data.mongo.MongoRepositoriesAutoConfiguration, org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration, org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration, org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration, org.springframework.boot.autoconfigure.data.r2dbc.R2dbcDataAutoConfiguration, org.springframework.boot.autoconfigure.data.r2dbc.R2dbcRepositoriesAutoConfiguration, org.springframework.boot.autoconfigure.r2dbc.R2dbcAutoConfiguration, org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchDataAutoConfiguration, org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchRepositoriesAutoConfiguration, org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration, org.springframework.boot.autoconfigure.data.cassandra.CassandraDataAutoConfiguration, org.springframework.boot.autoconfigure.data.cassandra.CassandraRepositoriesAutoConfiguration, org.springframework.boot.autoconfigure.data.neo4j.Neo4jDataAutoConfiguration, org.springframework.boot.autoconfigure.data.neo4j.Neo4jRepositoriesAutoConfiguration, org.springframework.boot.autoconfigure.neo4j.Neo4jAutoConfiguration

View File

@ -0,0 +1,27 @@
@startuml
'https://plantuml.com/class-diagram
interface CassandraConnectionDetails {
+getUsername():String
+getPassword():String
+getContactPoints():List<Node>
+getLocalDatacenter():String
}
class CustomCassandraConnectionDetails {
+getUsername():String
+getPassword():String
+getContactPoints():List<Node>
+getLocalDatacenter():String
}
class VaultAdapter {
+getSecret(String secretKey):String
}
class CustomCassandraConnectionDetailsConfiguration {
+getCustomCassandraConnectionDetails():CassandraConnectionDetails
}
CustomCassandraConnectionDetails -left-> VaultAdapter:uses
CustomCassandraConnectionDetails -right-|> CassandraConnectionDetails : implements
CustomCassandraConnectionDetailsConfiguration -up-> CustomCassandraConnectionDetails:uses
@enduml

View File

@ -0,0 +1,16 @@
@startuml
'https://plantuml.com/sequence-diagram
skinparam sequenceMessageAlign direction
skinparam handwritten true
skinparam sequence {
ParticipantBackgroundColor beige
ParticipantPadding 50
}
autonumber
"Spring Boot" -[#63b175]> "Hashicorp Vault": Secret Request
"Hashicorp Vault" -[#63b175]-> "Spring Boot": Secret Response
"Spring Boot" -[#63b175]> "Remote Service": Connection Request
"Remote Service" -[#63b175]-> "Spring Boot": Connection Response
@enduml

View File

@ -0,0 +1,9 @@
version: '3.8'
services:
cassandra:
image: 'cassandra:latest'
environment:
- 'CASSANDRA_DC=datacenter-1'
- 'CASSANDRA_ENDPOINT_SNITCH=GossipingPropertyFileSnitch'
ports:
- '19042:9042'

View File

@ -0,0 +1,12 @@
version: '3.8'
services:
elasticsearch:
image: 'docker.elastic.co/elasticsearch/elasticsearch:7.17.10'
environment:
- 'ELASTIC_USERNAME=elastic'
- 'ELASTIC_PASSWORD=secret'
- 'discovery.type=single-node'
- 'xpack.security.enabled=false'
ports:
- '19200:9200'
# - '19300:9300'

View File

@ -0,0 +1,10 @@
version: '3.8'
services:
database:
image: 'postgres:13.1-alpine'
ports:
- '15432:5432'
environment:
- 'POSTGRES_USER=postgres'
- 'POSTGRES_DB=postgresdb'
- 'POSTGRES_PASSWORD=postgres'

View File

@ -0,0 +1,10 @@
version: '3.8'
services:
mongodb:
image: 'mongo:latest'
environment:
- 'MONGO_INITDB_DATABASE=demodb'
# - 'MONGO_INITDB_ROOT_PASSWORD=Mongo123'
# - 'MONGO_INITDB_ROOT_USERNAME=mongouser'
ports:
- '27017:27017'

View File

@ -0,0 +1,8 @@
version: '3.8'
services:
neo4j:
image: 'neo4j:latest'
ports:
- '17687:7687'
environment:
NEO4J_AUTH: neo4j/neo4j123

View File

@ -0,0 +1,9 @@
version: '3.8'
services:
rabbitmq:
image: 'rabbitmq:latest'
environment:
- 'RABBITMQ_DEFAULT_PASS=rabbitmq'
- 'RABBITMQ_DEFAULT_USER=rabbitmquser'
ports:
- '5672:5672'

View File

@ -0,0 +1,8 @@
version: '3.8'
services:
redis:
image: 'redis:latest'
ports:
- '6379:6379'
environment:
- 'REDIS_REQUIREPASS=redis'

View File

@ -0,0 +1,39 @@
version: '3.8'
services:
database:
profiles:
- 'jdbc'
image: 'postgres:13.1-alpine'
ports:
- '15432:5432'
environment:
- 'POSTGRES_USER=postgres'
- 'POSTGRES_DB=postgresdb'
- 'POSTGRES_PASSWORD=postgres'
rabbitmq:
profiles:
- 'rabbitmq'
image: 'rabbitmq:latest'
environment:
- 'RABBITMQ_DEFAULT_PASS=rabbitmq'
- 'RABBITMQ_DEFAULT_USER=rabbitmquser'
ports:
- '5672:5672'
redis:
profiles:
- 'redis'
image: 'redis:latest'
ports:
- '6379:6379'
environment:
- 'REDIS_REQUIREPASS=redis'
# mongodb:
# profiles:
# - 'mongo-test'
# image: 'mongo:latest'
# environment:
# - 'MONGO_INITDB_DATABASE=demodb'
# - 'MONGO_INITDB_ROOT_PASSWORD=mongo'
# - 'MONGO_INITDB_ROOT_USERNAME=mongouser'
# ports:
# - '27017:27017'

View File

@ -0,0 +1,25 @@
@startuml
'https://plantuml.com/class-diagram
interface ElasticsearchConnectionDetails {
+getUsername():String
+getPassword():String
+getNodes():List<Node>
}
class CustomElasticsearchConnectionDetails {
+getUsername():String
+getPassword():String
+getNodes():List<Node>
}
class VaultAdapter {
+getSecret(String secretKey):String
}
class CustomElasticsearchConnectionDetailsConfiguration {
+getCustomElasticConnectionDetails():ElasticsearchConnectionDetails
}
CustomElasticsearchConnectionDetails -left-> VaultAdapter:uses
CustomElasticsearchConnectionDetails -right-|> ElasticsearchConnectionDetails : implements
CustomElasticsearchConnectionDetailsConfiguration -up-> CustomElasticsearchConnectionDetails:uses
@enduml

View File

@ -0,0 +1,25 @@
@startuml
'https://plantuml.com/class-diagram
interface JdbcConnectionDetails {
+getUsername():String
+getPassword():String
+getJdbcUrl():String
}
class PostgresConnectionDetails {
+getUsername():String
+getPassword():String
+getJdbcUrl():String
}
class VaultAdapter {
+getSecret(String secretKey):String
}
class JdbcConnectonDetailsConfiguration {
+getPostgresConnection():JdbcConnectionDetails
}
PostgresConnectionDetails -left-> VaultAdapter:uses
PostgresConnectionDetails -right-|> JdbcConnectionDetails : implements
JdbcConnectonDetailsConfiguration -up-> PostgresConnectionDetails:uses
@enduml

View File

@ -0,0 +1,19 @@
@startuml
'https://plantuml.com/class-diagram
interface MongoConnectionDetails {
getConnectionString():ConnectionString
}
class MongoDBConnectionDetails {
getConnectionString():ConnectionString
}
class VaultAdapter {
+getSecret(String secretKey):String
}
class MongoDBConnectionDetailsConfiguration {
+getMongoConnectionDetails() : MongoConnectionDetails
}
MongoDBConnectionDetails -left-> VaultAdapter:uses
MongoDBConnectionDetails -right-|> MongoConnectionDetails : implements
MongoDBConnectionDetailsConfiguration -up-> MongoDBConnectionDetails:uses
@enduml

View File

@ -0,0 +1,21 @@
@startuml
'https://plantuml.com/class-diagram
interface R2dbcConnectionDetails {
+getConnectionFactoryOptions():ConnectionFactoryOptions
}
class R2dbcPostgresConnectionDetails {
+getConnectionFactoryOptions():ConnectionFactoryOptions
}
class VaultAdapter {
+getSecret(String secretKey):String
}
class R2dbcPostgresConnectionDetailsConfiguration {
+getR2dbcPostgresConnectionDetails():R2dbcConnectionDetails
}
R2dbcPostgresConnectionDetails -right-> VaultAdapter:uses
R2dbcPostgresConnectionDetails -up-|> R2dbcConnectionDetails:implements
R2dbcPostgresConnectionDetailsConfiguration -up-> R2dbcPostgresConnectionDetails:uses
@enduml

View File

@ -0,0 +1,27 @@
@startuml
'https://plantuml.com/class-diagram
interface RabbitConnectionDetails {
+getUsername():String
+getPassword():String
+getFirstAddress():Address
+getAddresses():List<Address>
}
class RabbitMQConnectionDetails {
+getUsername():String
+getPassword():String
+getFirstAddress():Address
+getAddresses():List<Address>
}
class VaultAdapter {
+getSecret(String secretKey):String
}
class RabbitMQConnectionDetailsConfiguration {
+getRabbitmqConnection() : RabbitConnectionDetails
}
RabbitMQConnectionDetails -left-> VaultAdapter:uses
RabbitMQConnectionDetails -right-|> RabbitConnectionDetails : implements
RabbitMQConnectionDetailsConfiguration -up-> RabbitMQConnectionDetails:uses
@enduml

View File

@ -0,0 +1,23 @@
@startuml
'https://plantuml.com/class-diagram
interface RedisConnectionDetails {
+getPassword():String
+getStandalone():Standalone
}
class RedisCacheConnectionDetails {
+getPassword():String
+getStandalone():Standalone
}
class VaultAdapter {
+getSecret(String secretKey):String
}
class RedisConnectonDetailsConfiguration {
+getRedisCacheConnection():RedisConnectionDetails
}
RedisCacheConnectionDetails -left-> VaultAdapter:uses
RedisCacheConnectionDetails -right-|> RedisConnectionDetails : implements
RedisConnectonDetailsConfiguration -up-> RedisCacheConnectionDetails:uses
@enduml

View File

@ -0,0 +1,37 @@
package com.baeldung.connectiondetails;
import com.baeldung.connectiondetails.configuration.CustomCassandraConnectionDetailsConfiguration;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.data.cassandra.core.CassandraTemplate;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import static org.junit.jupiter.api.Assertions.assertTrue;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ConnectionDetailsApplication.class)
@Import(CustomCassandraConnectionDetailsConfiguration.class)
@ComponentScan(basePackages = "com.baeldung.connectiondetails")
@TestPropertySource(locations = {"classpath:connectiondetails/application-cassandra.properties"})
@ActiveProfiles("cassandra")
public class CassandraConnectionDetailsIntegrationTest {
private static final Logger logger = LoggerFactory.getLogger(CassandraConnectionDetailsIntegrationTest.class);
@Autowired
private CassandraTemplate cassandraTemplate;
@Test
public void givenHashicorpVault_whenRunQuery_thenSuccess() {
Boolean result = cassandraTemplate.getCqlOperations()
.execute("CREATE KEYSPACE IF NOT EXISTS spring_cassandra"
+ " WITH replication = {'class':'SimpleStrategy', 'replication_factor':3}");
logger.info("the result -" + result);
assertTrue(result);
}
}

View File

@ -0,0 +1,45 @@
package com.baeldung.connectiondetails;
import com.baeldung.connectiondetails.configuration.CustomElasticsearchConnectionDetailsConfiguration;
import com.baeldung.connectiondetails.entity.elastic.Person;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.data.elasticsearch.client.elc.ElasticsearchTemplate;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import static org.junit.jupiter.api.Assertions.assertTrue;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ConnectionDetailsApplication.class)
@Import(CustomElasticsearchConnectionDetailsConfiguration.class)
@ComponentScan(basePackages = "com.baeldung.connectiondetails")
@TestPropertySource(locations = {"classpath:connectiondetails/application-elastic.properties"})
@ActiveProfiles("elastic")
public class ElasticsearchConnectionDetailsIntegrationTest {
private static final Logger logger = LoggerFactory.getLogger(ElasticsearchConnectionDetailsIntegrationTest.class);
@Autowired
private ElasticsearchTemplate elasticsearchTemplate;
@Before
public void prepare() {
if (elasticsearchTemplate.indexOps(Person.class).exists()) {
elasticsearchTemplate.indexOps(Person.class).delete();
}
}
@Test
public void givenHashicorpVault_whenCreateIndexInElastic_thenSuccess() {
Boolean result = elasticsearchTemplate.indexOps(Person.class).create();
logger.info("index created:" + result);
assertTrue(result);
}
}

View File

@ -0,0 +1,38 @@
package com.baeldung.connectiondetails;
import com.baeldung.connectiondetails.configuration.JdbcConnectionDetailsConfiguration;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import java.sql.Date;
import java.time.LocalDate;
import static org.junit.jupiter.api.Assertions.assertEquals;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ConnectionDetailsApplication.class)
@Import(JdbcConnectionDetailsConfiguration.class)
@ComponentScan(basePackages = "com.baeldung.connectiondetails")
@TestPropertySource(locations = {"classpath:connectiondetails/application-jdbc.properties"})
@ActiveProfiles("jdbc")
public class JdbcConnectionDetailsIntegrationTest {
private static final Logger logger = LoggerFactory.getLogger(JdbcConnectionDetailsIntegrationTest.class);
@Autowired
private JdbcTemplate jdbcTemplate;
@Test
public void givenSecretVault_whenIntegrateWithPostgres_thenConnectionSuccessful() {
String sql = "select current_date;";
Date date = jdbcTemplate.queryForObject(sql, Date.class);
assertEquals(LocalDate.now().toString(), date.toString());
}
}

View File

@ -0,0 +1,47 @@
package com.baeldung.connectiondetails;
import com.baeldung.connectiondetails.configuration.MongoDBConnectionDetailsConfiguration;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.data.r2dbc.R2dbcDataAutoConfiguration;
import org.springframework.boot.autoconfigure.data.r2dbc.R2dbcRepositoriesAutoConfiguration;
import org.springframework.boot.autoconfigure.r2dbc.R2dbcAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import static org.junit.jupiter.api.Assertions.assertEquals;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ConnectionDetailsApplication.class)
@Import(MongoDBConnectionDetailsConfiguration.class)
@ComponentScan(basePackages = "com.baeldung.connectiondetails")
@TestPropertySource(locations = {"classpath:connectiondetails/application-mongo.properties"})
@ActiveProfiles("mongo")
public class MongoDBConnectionDetailsIntegrationTest {
@Autowired
private MongoTemplate mongoTemplate;
@Test
public void givenMongoDB_whenExecuteQuery_ReturnResult() throws JSONException {
mongoTemplate.insert("{\"msg\":\"My First Entry in MongoDB\"}", "myDemoCollection");
String result = mongoTemplate.find(new Query(), String.class, "myDemoCollection").get(0);
JSONObject jsonObject = new JSONObject(result);
result = jsonObject.get("msg").toString();
R2dbcAutoConfiguration r2dbcAutoConfiguration;
R2dbcDataAutoConfiguration r2dbcDataAutoConfiguration;
R2dbcRepositoriesAutoConfiguration r2dbcRepositoriesAutoConfiguration;
assertEquals("My First Entry in MongoDB", result);
}
}

View File

@ -0,0 +1,43 @@
package com.baeldung.connectiondetails;
import com.baeldung.connectiondetails.configuration.CustomNeo4jConnectionDetailsConfiguration;
import com.baeldung.connectiondetails.entity.neo4j.Person;
import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.data.neo4j.core.Neo4jTemplate;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import static org.junit.jupiter.api.Assertions.assertEquals;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ConnectionDetailsApplication.class)
@Import(CustomNeo4jConnectionDetailsConfiguration.class)
@ComponentScan(basePackages = "com.baeldung.connectiondetails")
@TestPropertySource(locations = {"classpath:connectiondetails/application-neo4j.properties"})
@ActiveProfiles("neo4j")
public class Neo4jConnectionDetailsIntegrationTest {
@Autowired
private Neo4jTemplate neo4jTemplate;
@After
public void cleanup() {
neo4jTemplate.deleteAll(Person.class);
}
@Test
public void giveHashicorpVault_whenRunQuery_thenSuccess() {
Person person = new Person();
person.setName("James");
person.setZipcode("751003");
Person data = neo4jTemplate.save(person);
assertEquals("James", data.getName());
}
}

View File

@ -0,0 +1,40 @@
package com.baeldung.connectiondetails;
import com.baeldung.connectiondetails.configuration.R2dbcPostgresConnectionDetailsConfiguration;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Import;
import org.springframework.data.r2dbc.core.R2dbcEntityTemplate;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ConnectionDetailsApplication.class)
@Import(R2dbcPostgresConnectionDetailsConfiguration.class)
@TestPropertySource(locations = {"classpath:connectiondetails/application-r2dbc.properties"})
@ActiveProfiles("r2dbc")
public class R2dbcConnectionDetailsIntegrationTest {
Logger logger = LoggerFactory.getLogger(R2dbcConnectionDetailsIntegrationTest.class);
@Autowired
private R2dbcEntityTemplate r2dbcEntityTemplate;
@Test
public void givenHashicorpVault_whenQueryPostgresReactive_thenSuccess() {
String sql = "select * from information_schema.tables";
List<String> result = r2dbcEntityTemplate.getDatabaseClient().sql(sql).fetch().all()
.map(r -> {
return "hello " + r.get("table_name").toString();
}).collectList().block();
logger.info("count ------" + result.size());
}
}

View File

@ -0,0 +1,62 @@
package com.baeldung.connectiondetails;
import com.baeldung.connectiondetails.configuration.RabbitMQConnectionDetailsConfiguration;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import static org.junit.jupiter.api.Assertions.assertEquals;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ConnectionDetailsApplication.class)
@Import(RabbitMQConnectionDetailsConfiguration.class)
@TestPropertySource(locations = {"classpath:connectiondetails/application-rabbitmq.properties"})
@ActiveProfiles("rabbitmq")
public class RabbitmqConnectionDetailsIntegrationTest {
private static final Logger logger = LoggerFactory.getLogger(RabbitmqConnectionDetailsIntegrationTest.class);
@Autowired
private RabbitTemplate rabbitTemplate;
@Autowired
private CachingConnectionFactory connectionFactory;
private final static String queueName = "Test_Queue";
RabbitAdmin rabbitAdmin;
@Before
public void setup() {
logger.info("create new queue");
rabbitAdmin = new RabbitAdmin(connectionFactory);
logger.info("creating queue: " + rabbitAdmin.declareQueue(new Queue(queueName)));
connectionFactory.destroy();
}
@After
public void cleanup() {
logger.info("delete queue");
this.rabbitAdmin.deleteQueue(queueName, false, true);
}
@Test
public void givenRabbitmq_whenPublishMessage_thenSuccess() {
logger.info("sending message to queue " + queueName);
final String MSG = "this is a test message";
this.rabbitTemplate.convertAndSend(queueName, MSG);
assertEquals(MSG, this.rabbitTemplate.receiveAndConvert(queueName));
}
}

View File

@ -0,0 +1,34 @@
package com.baeldung.connectiondetails;
import com.baeldung.connectiondetails.configuration.RedisConnectionDetailsConfiguration;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Import;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import static org.junit.jupiter.api.Assertions.assertEquals;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ConnectionDetailsApplication.class)
@Import(RedisConnectionDetailsConfiguration.class)
@TestPropertySource(locations = {"classpath:connectiondetails/application-redis.properties"})
@ActiveProfiles("redis")
public class RedisCacheConnnectionDetailsIntegrationTest {
private static final Logger logger = LoggerFactory.getLogger(RedisCacheConnnectionDetailsIntegrationTest.class);
@Autowired
RedisTemplate<String, String> redisTemplate;
@Test
public void giveRedisCache_whenKeyVal_storeInCache() {
redisTemplate.opsForValue().set("City", "New York");
assertEquals("New York", redisTemplate.opsForValue().get("City"));
}
}