-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBookControllerTests.java
167 lines (148 loc) · 5.57 KB
/
BookControllerTests.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package com.example.demo;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.test.autoconfigure.graphql.GraphQlTest;
import org.springframework.graphql.test.tester.GraphQlTester;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
@GraphQlTest(BookController.class)
@ImportAutoConfiguration(GraphQLValidationConfig.class)
public class BookControllerTests {
@Autowired
private GraphQlTester graphQlTester;
@Test
void testAddBookMutation() {
String mutation = """
mutation {
addBook(createBookInput: { title: "War of the Words", author: "H.G. Wells" }) {
id
title
author
}
}
""";
graphQlTester.document(mutation)
.execute()
.path("addBook")
.entity(Book.class)
.satisfies(book -> {
assertThat(book.getId()).isNotNull();
assertThat(book.getTitle()).isEqualTo("War of the Words");
assertThat(book.getAuthor()).isEqualTo("H.G. Wells");
});
String query = """
query {
getBooks {
id
title
author
}
}
""";
graphQlTester.document(query)
.execute()
.path("getBooks")
.entityList(Book.class)
.hasSize(1) // Assuming the addBookMutation test has run and added a book
.satisfies(books -> {
assert books.get(0).getTitle().equals("War of the Words");
assert books.get(0).getAuthor().equals("H.G. Wells");
});
}
@Test
public void testAddBookWithShortTitle() {
String mutation = """
mutation {
addBook(createBookInput: { title: "1984", author: "George Orwell" }) {
id
title
author
}
}
""";
graphQlTester.document(mutation)
.execute()
.errors()
.satisfy(errors -> {
assertThat(errors.size()).isEqualTo(1);
assertThat(errors.get(0).getMessage()).isEqualTo("/addBook/createBookInput/title size must be between 5 and 50");
});
}
@Test
public void testSubscribeToBooks() {
// First, add a book to ensure there is data to subscribe to
String addBookMutation = """
mutation {
addBook(createBookInput: { title: "Some book title", author: "Some Author" }) {
id
title
author
}
}
""";
graphQlTester.document(addBookMutation)
.execute()
.path("addBook")
.entity(Book.class)
.satisfies(book -> {
assert book.getTitle().equals("Some book title");
assert book.getAuthor().equals("Some Author");
});
// Now, test the subscription
String subscription = """
subscription {
subscribeToBooks(authorFilter: "Some Author") {
id
title
author
}
}
""";
graphQlTester.document(subscription)
.executeSubscription()
.toFlux("subscribeToBooks", Book.class)
.take(1)
.doOnNext(book -> {
assert book.getTitle().equals("Some book title");
assert book.getAuthor().equals("Some Author");
})
.blockLast();
}
@Test
public void testSubscribeToBooksWithShortFilter() {
// First, add a book to ensure there is data to subscribe to
String addBookMutation = """
mutation {
addBook(createBookInput: { title: "Some book title", author: "Some Author" }) {
id
title
author
}
}
""";
graphQlTester.document(addBookMutation)
.execute()
.path("addBook")
.entity(Book.class)
.satisfies(book -> {
assert book.getTitle().equals("Some book title");
assert book.getAuthor().equals("Some Author");
});
String subscription = """
subscription {
subscribeToBooks(authorFilter: "Some") {
id
title
author
}
}
""";
graphQlTester.document(subscription)
.execute()
.errors()
.satisfy(errors -> {
assertThat(errors.size()).isEqualTo(1);
assertThat(errors.get(0).getMessage()).contains("/subscribeToBooks/authorFilter size must be between 5 and 50");
});
}
}