Skip to content

Commit b59f324

Browse files
committed
Add testContainers integration tests
1 parent 5f57415 commit b59f324

File tree

4 files changed

+231
-18
lines changed

4 files changed

+231
-18
lines changed

pom.xml

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,19 @@
5656
<dependency>
5757
<groupId>org.junit.jupiter</groupId>
5858
<artifactId>junit-jupiter-engine</artifactId>
59-
<version>5.4.2</version>
59+
<version>5.3.2</version>
60+
<scope>test</scope>
61+
</dependency>
62+
<dependency>
63+
<groupId>org.mockito</groupId>
64+
<artifactId>mockito-core</artifactId>
65+
<version>2.23.4</version>
6066
<scope>test</scope>
6167
</dependency>
6268
<dependency>
6369
<groupId>org.mockito</groupId>
6470
<artifactId>mockito-junit-jupiter</artifactId>
65-
<version>2.27.0</version>
71+
<version>2.23.4</version>
6672
<scope>test</scope>
6773
</dependency>
6874
<dependency>
@@ -90,6 +96,31 @@
9096
<groupId>org.springframework.boot</groupId>
9197
<artifactId>spring-boot-maven-plugin</artifactId>
9298
</plugin>
99+
<plugin>
100+
<groupId>org.apache.maven.plugins</groupId>
101+
<artifactId>maven-surefire-plugin</artifactId>
102+
<version>2.22.0</version>
103+
</plugin>
104+
<plugin>
105+
<groupId>org.apache.maven.plugins</groupId>
106+
<artifactId>maven-failsafe-plugin</artifactId>
107+
<version>3.0.0-M3</version>
108+
<configuration>
109+
<skipTests>false</skipTests>
110+
<rerunFailingTestsCount>3</rerunFailingTestsCount>
111+
<includes>
112+
<include>**/*IT.java</include>
113+
</includes>
114+
</configuration>
115+
<executions>
116+
<execution>
117+
<goals>
118+
<goal>integration-test</goal>
119+
<goal>verify</goal>
120+
</goals>
121+
</execution>
122+
</executions>
123+
</plugin>
93124
</plugins>
94125
</build>
95126

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.madadipouya.elasticsearch.springdata;
2+
3+
import org.testcontainers.elasticsearch.ElasticsearchContainer;
4+
5+
public class BookElasticsearchContainer extends ElasticsearchContainer {
6+
7+
private static final String ELASTIC_SEARCH_DOCKER = "elasticsearch:6.4.3";
8+
9+
private static final String CLUSTER_NAME = "cluster.name";
10+
11+
private static final String ELASTIC_SEARCH = "elasticsearch";
12+
13+
public BookElasticsearchContainer() {
14+
super(ELASTIC_SEARCH_DOCKER);
15+
this.addFixedExposedPort(9200, 9200);
16+
this.addFixedExposedPort(9300, 9300);
17+
this.addEnv(CLUSTER_NAME, ELASTIC_SEARCH);
18+
}
19+
}

src/test/java/com/madadipouya/elasticsearch/springdata/example/TestcontainersEsApplicationTests.java

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
package com.madadipouya.elasticsearch.springdata.example.service.impl;
2+
3+
import com.madadipouya.elasticsearch.springdata.BookElasticsearchContainer;
4+
import com.madadipouya.elasticsearch.springdata.example.model.Book;
5+
import com.madadipouya.elasticsearch.springdata.example.service.BookService;
6+
import com.madadipouya.elasticsearch.springdata.example.service.exception.BookNotFoundException;
7+
import com.madadipouya.elasticsearch.springdata.example.service.exception.DuplicateIsbnException;
8+
import org.junit.jupiter.api.AfterAll;
9+
import org.junit.jupiter.api.BeforeAll;
10+
import org.junit.jupiter.api.BeforeEach;
11+
import org.junit.jupiter.api.Test;
12+
import org.junit.jupiter.api.extension.ExtendWith;
13+
import org.springframework.beans.factory.annotation.Autowired;
14+
import org.springframework.boot.test.context.SpringBootTest;
15+
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
16+
import org.springframework.test.context.junit.jupiter.SpringExtension;
17+
import org.testcontainers.elasticsearch.ElasticsearchContainer;
18+
import org.testcontainers.junit.jupiter.Container;
19+
import org.testcontainers.junit.jupiter.Testcontainers;
20+
21+
import java.util.List;
22+
import java.util.Optional;
23+
24+
import static org.junit.jupiter.api.Assertions.*;
25+
26+
27+
@Testcontainers
28+
@ExtendWith(SpringExtension.class)
29+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
30+
class DefaultBookServiceIT {
31+
32+
@Autowired
33+
private BookService bookService;
34+
35+
@Autowired
36+
ElasticsearchTemplate template;
37+
38+
@Container
39+
private static ElasticsearchContainer elasticsearchContainer = new BookElasticsearchContainer();
40+
41+
@BeforeAll
42+
static void setUp() {
43+
elasticsearchContainer.start();
44+
}
45+
46+
@BeforeEach
47+
void testIsContainerRunning() {
48+
assertTrue(elasticsearchContainer.isRunning());
49+
recreateIndex();
50+
}
51+
52+
@Test
53+
void testGetBookByIsbn() throws DuplicateIsbnException {
54+
bookService.create(createBook("12 rules for life", "Jordan Peterson", 2018, "978-0345816023"));
55+
Optional<Book> result = bookService.getByIsbn("978-0345816023");
56+
assertTrue(result.isPresent());
57+
Book createdBook = result.get();
58+
assertNotNull(createdBook);
59+
assertEquals("12 rules for life", createdBook.getTitle());
60+
assertEquals("Jordan Peterson", createdBook.getAuthorName());
61+
assertEquals(2018, createdBook.getPublicationYear());
62+
assertEquals("978-0345816023", createdBook.getIsbn());
63+
}
64+
65+
@Test
66+
void testGetAllBooks() throws DuplicateIsbnException {
67+
bookService.create(createBook("12 rules for life", "Jordan Peterson", 2018, "978-0345816023"));
68+
bookService.create(createBook("The Cathedral and the Bazaar", "Eric Raymond", 1999, "9780596106386"));
69+
List<Book> books = bookService.getAll();
70+
71+
assertNotNull(books);
72+
assertEquals(2, books.size());
73+
}
74+
75+
@Test
76+
void testFindByAuthor() throws DuplicateIsbnException {
77+
bookService.create(createBook("12 rules for life", "Jordan Peterson", 2018, "978-0345816023"));
78+
bookService.create(createBook("Maps of Meaning", "Jordan Peterson", 1999, "9781280407253"));
79+
80+
List<Book> books = bookService.findByAuthor("Jordan Peterson");
81+
82+
assertNotNull(books);
83+
assertEquals(2, books.size());
84+
}
85+
86+
@Test
87+
void testFindByTitleAndAuthor() throws DuplicateIsbnException {
88+
bookService.create(createBook("12 rules for life", "Jordan Peterson", 2018, "978-0345816023"));
89+
bookService.create(createBook("Rules or not rules?", "Jordan Miller", 2010, "978128000000"));
90+
bookService.create(createBook("Poor economy", "Jordan Miller", 2006, "9781280789000"));
91+
bookService.create(createBook("The Cathedral and the Bazaar", "Eric Raymond", 1999, "9780596106386"));
92+
93+
List<Book> books = bookService.findByTitleAndAuthor("rules", "jordan");
94+
95+
assertNotNull(books);
96+
assertEquals(2, books.size());
97+
}
98+
99+
@Test
100+
void testCreateBook() throws DuplicateIsbnException {
101+
Book createdBook = bookService.create(createBook("12 rules for life", "Jordan Peterson", 2018, "978-0345816023"));
102+
assertNotNull(createdBook);
103+
assertNotNull(createdBook.getId());
104+
assertEquals("12 rules for life", createdBook.getTitle());
105+
assertEquals("Jordan Peterson", createdBook.getAuthorName());
106+
assertEquals(2018, createdBook.getPublicationYear());
107+
assertEquals("978-0345816023", createdBook.getIsbn());
108+
}
109+
110+
@Test
111+
void testCreateBookWithDuplicateISBNThrowsException() throws DuplicateIsbnException {
112+
Book createdBook = bookService.create(createBook("12 rules for life", "Jordan Peterson", 2018, "978-0345816023"));
113+
assertNotNull(createdBook);
114+
assertThrows(DuplicateIsbnException.class, () -> {
115+
bookService.create(createBook("Test title", "Test author", 2010, "978-0345816023"));
116+
});
117+
}
118+
119+
@Test
120+
void testDeleteBookById() throws DuplicateIsbnException {
121+
Book createdBook = bookService.create(createBook("12 rules for life", "Jordan Peterson", 2018, "978-0345816023"));
122+
123+
assertNotNull(createdBook);
124+
assertNotNull(createdBook.getId());
125+
126+
bookService.deleteById(createdBook.getId());
127+
List<Book> books = bookService.findByAuthor("Jordan Peterson");
128+
129+
assertTrue(books.isEmpty());
130+
}
131+
132+
@Test
133+
void testUpdateBook() throws DuplicateIsbnException, BookNotFoundException {
134+
Book bookToUpdate = bookService.create(createBook("12 rules for life", "Jordan Peterson", 2000, "978-0345816023"));
135+
136+
assertNotNull(bookToUpdate);
137+
assertNotNull(bookToUpdate.getId());
138+
139+
bookToUpdate.setPublicationYear(2018);
140+
Book updatedBook = bookService.update(bookToUpdate.getId(), bookToUpdate);
141+
142+
assertNotNull(updatedBook);
143+
assertNotNull(updatedBook.getId());
144+
assertEquals("12 rules for life", updatedBook.getTitle());
145+
assertEquals("Jordan Peterson", updatedBook.getAuthorName());
146+
assertEquals(2018, updatedBook.getPublicationYear());
147+
assertEquals("978-0345816023", updatedBook.getIsbn());
148+
}
149+
150+
@Test
151+
void testUpdateBookThrowsExceptionIfCannotFindBook() {
152+
Book updatedBook = createBook("12 rules for life", "Jordan Peterson", 2000, "978-0345816023");
153+
154+
assertThrows(BookNotFoundException.class, () -> {
155+
bookService.update("1A2B3C", updatedBook);
156+
});
157+
}
158+
159+
private Book createBook(String title, String authorName, int publicationYear, String isbn) {
160+
Book book = new Book();
161+
book.setTitle(title);
162+
book.setAuthorName(authorName);
163+
book.setPublicationYear(publicationYear);
164+
book.setIsbn(isbn);
165+
return book;
166+
}
167+
168+
private void recreateIndex() {
169+
if (template.indexExists(Book.class)) {
170+
template.deleteIndex(Book.class);
171+
template.createIndex(Book.class);
172+
}
173+
}
174+
175+
@AfterAll
176+
static void destroy() {
177+
elasticsearchContainer.stop();
178+
}
179+
}

0 commit comments

Comments
 (0)