-
-
Notifications
You must be signed in to change notification settings - Fork 464
/
Copy pathPostRepositoryTest.java
96 lines (82 loc) · 3.66 KB
/
PostRepositoryTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package com.example.demo;
import com.example.demo.domain.Post;
import com.example.demo.repository.MongoConfig;
import com.example.demo.repository.PostRepository;
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.context.ApplicationContextInitializer;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.core.env.MapPropertySource;
import org.springframework.data.domain.Sort;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.testcontainers.containers.MongoDBContainer;
import reactor.core.publisher.Flux;
import reactor.test.StepVerifier;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* @author hantsy
*/
@SpringJUnitConfig(classes = PostRepositoryTest.TestConfig.class)
@ActiveProfiles("test")
@ContextConfiguration(initializers = PostRepositoryTest.TestContainerInitializer.class)
@Slf4j
public class PostRepositoryTest {
static class TestContainerInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
final MongoDBContainer container = new MongoDBContainer("mongo:4");
container.start();
log.info(" container.getFirstMappedPort():: {}", container.getFirstMappedPort());
configurableApplicationContext
.addApplicationListener((ApplicationListener<ContextClosedEvent>) event -> container.stop());
configurableApplicationContext.getEnvironment()
.getPropertySources()
.addFirst(
new MapPropertySource("testproperties",
Map.of("mongo.uri", "mongodb://localhost:" + container.getFirstMappedPort())
)
);
}
}
@Configuration
@ComponentScan(basePackageClasses = {MongoConfig.class, Post.class})
static class TestConfig {
}
@Autowired
PostRepository posts;
@BeforeEach
void setUp() throws InterruptedException {
CountDownLatch latch = new CountDownLatch(1);
this.posts.deleteAll()
.thenMany(
Flux.just("Post one", "Post two")
.flatMap(
title -> this.posts.save(Post.builder().title(title).content("content of " + title).build())
)
)
.doOnComplete(latch::countDown)
.subscribe(p -> log.debug("saved post: {}", p));
latch.await(1000, TimeUnit.MILLISECONDS);
}
@Test
public void testGetAllPosts() {
this.posts.findAll(Sort.by(Sort.Order.asc("title")))
.as(StepVerifier::create)
.consumeNextWith(p -> assertTrue(p.getTitle().contains("one")))
.consumeNextWith(p -> assertTrue(p.getTitle().contains("two")))
.expectComplete()
.verify();
}
}