Skip to content

Commit

Permalink
chore: clean codes
Browse files Browse the repository at this point in the history
  • Loading branch information
hantsy committed Dec 4, 2024
1 parent d8280a3 commit f707f11
Show file tree
Hide file tree
Showing 12 changed files with 159 additions and 179 deletions.
5 changes: 5 additions & 0 deletions boot-data-couchbase/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-testcontainers</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
Expand Down
10 changes: 5 additions & 5 deletions boot-data-couchbase/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# couchbase connection
spring.couchbase.connection-string=couchbase://localhost
spring.couchbase.username=Administrator
spring.couchbase.password=password
# couchbase bucket name
spring.data.couchbase.bucket-name=demo
#spring.couchbase.connection-string=couchbase://localhost
#spring.couchbase.username=Administrator
#spring.couchbase.password=password
## couchbase bucket name
#spring.data.couchbase.bucket-name=demo
# by default, it is disabled, you have to enable auto indexing in Couchbase console.
spring.data.couchbase.autoIndex=true
# logging config
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.data.couchbase.DataCouchbaseTest;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Import;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.data.domain.Sort;
import org.springframework.test.context.ActiveProfiles;
Expand All @@ -25,39 +27,12 @@

import static org.assertj.core.api.Assertions.assertThat;

@SpringBootTest
@ContextConfiguration(initializers = PostRepositoryTest.TestContainerInitializer.class)
@DataCouchbaseTest
@Import(TestcontainersConfiguration.class)
@ActiveProfiles("test")
@Slf4j
class PostRepositoryTest {

static class TestContainerInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
private static final String COUCHBASE_IMAGE_NAME = "couchbase";
private static final String DEFAULT_IMAGE_NAME = "couchbase/server:6";
private static final DockerImageName DEFAULT_IMAGE = DockerImageName.parse(COUCHBASE_IMAGE_NAME)
.asCompatibleSubstituteFor(DEFAULT_IMAGE_NAME);

@Override
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
final CouchbaseContainer couchbaseContainer = new CouchbaseContainer(DEFAULT_IMAGE)
.withCredentials("Administrator", "password")
.withBucket(new BucketDefinition("demo").withPrimaryIndex(true))
.withStartupTimeout(Duration.ofSeconds(60));

couchbaseContainer.start();

configurableApplicationContext
.addApplicationListener((ApplicationListener<ContextClosedEvent>) event -> couchbaseContainer.stop());
TestPropertyValues.of(
"spring.couchbase.connection-string=" + couchbaseContainer.getConnectionString(),
"spring.couchbase.username=" + couchbaseContainer.getUsername(),
"spring.couchbase.password=" + couchbaseContainer.getPassword(),
"spring.data.couchbase.bucket-name=demo"
)
.applyTo(configurableApplicationContext.getEnvironment());
}
}

@Autowired
private PostRepository posts;

Expand All @@ -74,7 +49,8 @@ public void setup() {
)
.subscribe(data -> {
log.debug("saved data: {}", data);
countDownLatch.countDown();;
countDownLatch.countDown();
;
});
countDownLatch.await(1000, java.util.concurrent.TimeUnit.MILLISECONDS);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package com.example.demo;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.data.domain.Sort;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.testcontainers.couchbase.BucketDefinition;
import org.testcontainers.couchbase.CouchbaseContainer;
import org.testcontainers.utility.DockerImageName;
import reactor.test.StepVerifier;

import java.time.Duration;
import java.util.List;
import java.util.concurrent.CountDownLatch;

import static org.assertj.core.api.Assertions.assertThat;

@SpringBootTest
@ContextConfiguration(initializers = PostRepositoryWithContextInitializerTest.TestContainerInitializer.class)
@ActiveProfiles("test")
@Slf4j
class PostRepositoryWithContextInitializerTest {

static class TestContainerInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
private static final String COUCHBASE_IMAGE_NAME = "couchbase";
private static final String DEFAULT_IMAGE_NAME = "couchbase/server:7";
private static final DockerImageName DEFAULT_IMAGE = DockerImageName.parse(COUCHBASE_IMAGE_NAME)
.asCompatibleSubstituteFor(DEFAULT_IMAGE_NAME);

@Override
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
final CouchbaseContainer couchbaseContainer = new CouchbaseContainer(DEFAULT_IMAGE)
.withCredentials("Administrator", "password")
.withBucket(new BucketDefinition("demo")
.withPrimaryIndex(true)
)
.withStartupTimeout(Duration.ofSeconds(60));

couchbaseContainer.start();

configurableApplicationContext
.addApplicationListener((ApplicationListener<ContextClosedEvent>) event -> couchbaseContainer.stop());
TestPropertyValues.of(
"spring.couchbase.connection-string=" + couchbaseContainer.getConnectionString(),
"spring.couchbase.username=" + couchbaseContainer.getUsername(),
"spring.couchbase.password=" + couchbaseContainer.getPassword(),
"spring.data.couchbase.bucket-name=demo"
)
.applyTo(configurableApplicationContext.getEnvironment());
}
}

@Autowired
private PostRepository posts;

@SneakyThrows
@BeforeEach
public void setup() {
var countDownLatch = new CountDownLatch(1);
this.posts
.saveAll(
List.of(
Post.builder().title("Post one").content("content of post one").build(),
Post.builder().title("Post two").content("content of post two").build()
)
)
.subscribe(data -> {
log.debug("saved data: {}", data);
countDownLatch.countDown();
;
});
countDownLatch.await(1000, java.util.concurrent.TimeUnit.MILLISECONDS);
}

@Test
void testLoadUsers() {
this.posts.findAll(Sort.by(Sort.Direction.ASC, "title"))
.log()
.as(StepVerifier::create)
.consumeNextWith(user -> {
assertThat(user.getTitle()).isEqualTo("Post one");
//verify data auditing
assertThat(user.getCreatedBy()).isEqualTo("hantsy");
assertThat(user.getCreatedAt()).isNotNull();
})
.consumeNextWith(user -> assertThat(user.getTitle()).isEqualTo("Post two"))
//.expectNextCount(2)
.verifyComplete();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
@Testcontainers
@ActiveProfiles("test")
@Slf4j
class PostRepositoryWithTestcontainresTest {
class PostRepositoryWithTestcontainersTest {


@TestConfiguration
Expand All @@ -39,7 +39,7 @@ static class TestConfig {
}

private static final String COUCHBASE_IMAGE_NAME = "couchbase";
private static final String DEFAULT_IMAGE_NAME = "couchbase/server:6";
private static final String DEFAULT_IMAGE_NAME = "couchbase/server:7";
private static final DockerImageName DEFAULT_IMAGE = DockerImageName.parse(COUCHBASE_IMAGE_NAME)
.asCompatibleSubstituteFor(DEFAULT_IMAGE_NAME);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.example.demo;

import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.testcontainers.couchbase.BucketDefinition;
import org.testcontainers.couchbase.CouchbaseContainer;
import org.testcontainers.utility.DockerImageName;

import java.time.Duration;

@Configuration(proxyBeanMethods = false)
public class TestcontainersConfiguration {
private static final String COUCHBASE_IMAGE_NAME = "couchbase";
private static final String DEFAULT_IMAGE_NAME = "couchbase/server";
private static final DockerImageName DEFAULT_IMAGE = DockerImageName.parse(COUCHBASE_IMAGE_NAME)
.asCompatibleSubstituteFor(DEFAULT_IMAGE_NAME);

@Bean
@ServiceConnection
public CouchbaseContainer couchbaseContainer() {
return new CouchbaseContainer(DEFAULT_IMAGE)
.withCredentials("Administrator", "password")
.withBucket(new BucketDefinition("demo").withPrimaryIndex(true))
.withStartupAttempts(5)
.withStartupTimeout(Duration.ofSeconds(120));
}
}
46 changes: 0 additions & 46 deletions session-data-mongo/nbactions.xml

This file was deleted.

5 changes: 4 additions & 1 deletion session-data-mongo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,16 @@
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/htmlunit-driver -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>htmlunit-driver</artifactId>
<version>4.13.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>net.sourceforge.htmlunit</groupId>
<groupId>org.htmlunit</groupId>
<artifactId>htmlunit</artifactId>
<scope>test</scope>
</dependency>
Expand Down
46 changes: 0 additions & 46 deletions session-data-redis/nbactions.xml

This file was deleted.

5 changes: 4 additions & 1 deletion session-data-redis/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,16 @@
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/htmlunit-driver -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>htmlunit-driver</artifactId>
<version>4.13.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>net.sourceforge.htmlunit</groupId>
<groupId>org.htmlunit</groupId>
<artifactId>htmlunit</artifactId>
<scope>test</scope>
</dependency>
Expand Down
Loading

0 comments on commit f707f11

Please sign in to comment.