forked from hantsy/spring-reactive-sample
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update boot-data-neo4j-rx to use testcontainers for it tests.
- Loading branch information
Showing
3 changed files
with
146 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
boot-data-neo4j-rx/src/test/java/com/example/demo/PostRepositoryWithTestContainersTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package com.example.demo; | ||
|
||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.neo4j.driver.springframework.boot.test.autoconfigure.Neo4jTestHarnessAutoConfiguration; | ||
import org.neo4j.springframework.boot.test.autoconfigure.data.ReactiveDataNeo4jTest; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
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.Example; | ||
import org.springframework.test.context.ContextConfiguration; | ||
import org.springframework.test.context.DynamicPropertyRegistry; | ||
import org.springframework.test.context.DynamicPropertySource; | ||
import org.testcontainers.containers.Neo4jContainer; | ||
import org.testcontainers.junit.jupiter.Container; | ||
import org.testcontainers.junit.jupiter.Testcontainers; | ||
import reactor.core.publisher.Flux; | ||
import reactor.test.StepVerifier; | ||
|
||
import java.io.IOException; | ||
import java.time.Duration; | ||
import java.util.Comparator; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
@ReactiveDataNeo4jTest(excludeAutoConfiguration = Neo4jTestHarnessAutoConfiguration.class) | ||
@Testcontainers | ||
@Slf4j | ||
public class PostRepositoryWithTestContainersTest { | ||
|
||
@Container | ||
private static Neo4jContainer<?> neo4jContainer = new Neo4jContainer<>("neo4j:4.0") | ||
.withStartupTimeout(Duration.ofMinutes(5)); | ||
|
||
@DynamicPropertySource | ||
static void neo4jProperties(DynamicPropertyRegistry registry) { | ||
registry.add("org.neo4j.driver.uri", neo4jContainer::getBoltUrl); | ||
registry.add("org.neo4j.driver.authentication.username", () -> "neo4j"); | ||
registry.add("org.neo4j.driver.authentication.password", neo4jContainer::getAdminPassword); | ||
} | ||
|
||
|
||
|
||
@Autowired | ||
private PostRepository posts; | ||
|
||
@BeforeEach | ||
public void setup() throws IOException { | ||
log.debug("running setup.....,"); | ||
this.posts.deleteAll() | ||
.thenMany(testSaveMethod()) | ||
.log() | ||
.thenMany(testFoundMethod()) | ||
.log() | ||
.blockLast();// to make the tests work | ||
// .subscribe( | ||
// (data) -> log.info("found post:" + data), | ||
// (err) -> log.error("" + err), | ||
// () -> log.info("done") | ||
// ); | ||
} | ||
|
||
private Flux<Post> testSaveMethod() { | ||
var data = Stream.of("Post one", "Post two") | ||
.map(title -> Post.builder().title(title).content("The content of " + title).build()) | ||
.collect(Collectors.toList()); | ||
return Flux.fromIterable(data) | ||
.flatMap(it -> this.posts.save(it)); | ||
} | ||
|
||
private Flux<Post> testFoundMethod() { | ||
return this.posts | ||
.findAll(Example.of(Post.builder().title("Post one").build())); | ||
} | ||
|
||
@AfterEach | ||
void teardown() { | ||
//this.posts.deleteAll(); | ||
} | ||
|
||
@Test | ||
void testAllPosts() { | ||
posts.findAll().sort(Comparator.comparing(post -> post.getTitle())) | ||
.as(StepVerifier::create) | ||
.consumeNextWith(p -> assertEquals("Post one", p.getTitle())) | ||
.consumeNextWith(p -> assertEquals("Post two", p.getTitle())) | ||
.verifyComplete(); | ||
} | ||
|
||
|
||
} |