Skip to content

Commit

Permalink
update boot-data-neo4j-rx to use testcontainers for it tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
hantsy committed Apr 21, 2020
1 parent bcf18a2 commit 9d04105
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 23 deletions.
13 changes: 10 additions & 3 deletions boot-data-neo4j-rx/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
<version>2.2.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
Expand All @@ -17,8 +17,8 @@
<properties>
<java.version>11</java.version>
<maven-failsafe-plugin.version>3.0.0-M4</maven-failsafe-plugin.version>
<spring-data-neo4j-rx.version>1.0.0-beta03</spring-data-neo4j-rx.version>
<testcontainers.version>1.10.7</testcontainers.version>
<spring-data-neo4j-rx.version>1.0.0</spring-data-neo4j-rx.version>
<testcontainers.version>1.13.0</testcontainers.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -52,11 +52,18 @@
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.neo4j.springframework.data</groupId>
<artifactId>spring-data-neo4j-rx-spring-boot-test-autoconfigure</artifactId>
<version>${spring-data-neo4j-rx.version}</version>
<scope>test</scope>
<exclusions>
<exclusion>
<artifactId>neo4j-harness</artifactId>
<groupId>org.neo4j.test</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
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.DataNeo4jTest;
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;
Expand All @@ -15,7 +15,11 @@
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;

Expand All @@ -28,10 +32,41 @@


@ContextConfiguration(initializers = PostRepositoryTest.TestContainerInitializer.class)
@DataNeo4jTest(excludeAutoConfiguration = Neo4jTestHarnessAutoConfiguration.class)
@ReactiveDataNeo4jTest(excludeAutoConfiguration = Neo4jTestHarnessAutoConfiguration.class)
//@Testcontainers
@Slf4j
public class PostRepositoryTest {

// @Container
// private static Neo4jContainer<?> neo4jContainer = new Neo4jContainer<>("neo4j:4.0");
//
// @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);
// }

/**
* Note: This code fragment is from Neo4j Data Rx spring boot test starter.
*
* An initializer that starts a Neo4j test container and sets {@code org.neo4j.driver.uri} to the containers
* bolt uri. It also registers an application listener that stops the container when the context closes.
*/
static class TestContainerInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {

@Override
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
final Neo4jContainer<?> neo4jContainer = new Neo4jContainer<>("neo4j:4.0").withoutAuthentication();
neo4jContainer.start();
configurableApplicationContext
.addApplicationListener((ApplicationListener<ContextClosedEvent>) event -> neo4jContainer.stop());
TestPropertyValues.of("org.neo4j.driver.uri=" + neo4jContainer.getBoltUrl())
.applyTo(configurableApplicationContext.getEnvironment());
}
}


@Autowired
private PostRepository posts;

Expand Down Expand Up @@ -78,23 +113,5 @@ void testAllPosts() {
.verifyComplete();
}

/**
* Note: This code fragment is from Neo4j Data Rx spring boot test starter.
*
* An initializer that starts a Neo4j test container and sets {@code org.neo4j.driver.uri} to the containers
* bolt uri. It also registers an application listener that stops the container when the context closes.
*/
static class TestContainerInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {

@Override
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
final Neo4jContainer<?> neo4jContainer = new Neo4jContainer<>("neo4j:4.0.0").withoutAuthentication();
neo4jContainer.start();
configurableApplicationContext
.addApplicationListener((ApplicationListener<ContextClosedEvent>) event -> neo4jContainer.stop());
TestPropertyValues.of("org.neo4j.driver.uri=" + neo4jContainer.getBoltUrl())
.applyTo(configurableApplicationContext.getEnvironment());
}
}

}
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();
}


}

0 comments on commit 9d04105

Please sign in to comment.