/
BlogServiceTest.java
140 lines (122 loc) · 5.4 KB
/
BlogServiceTest.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
package example.armeria.server.blog.thrift;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.catchThrowable;
import java.util.List;
import org.apache.thrift.TException;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.extension.RegisterExtension;
import com.linecorp.armeria.server.ServerBuilder;
import com.linecorp.armeria.server.thrift.THttpService;
import com.linecorp.armeria.testing.junit5.server.ServerExtension;
import example.armeria.blog.thrift.BlogNotFoundException;
import example.armeria.blog.thrift.BlogPost;
@TestMethodOrder(OrderAnnotation.class)
class BlogServiceTest {
@RegisterExtension
static final ServerExtension server = new ServerExtension() {
@Override
protected void configure(ServerBuilder sb) throws Exception {
sb.service("/thrift", THttpService.builder()
.exceptionHandler(new BlogServiceExceptionHandler())
.addService(new BlogServiceImpl())
.build());
}
};
@Test
@Order(1)
void createBlogPost() throws TException {
final BlogClient client = new BlogClient(server.httpUri(), "/thrift");
final BlogPost response = client.createBlogPost("My first blog", "Hello Armeria!");
assertThat(response.getId()).isGreaterThanOrEqualTo(0);
assertThat(response.getTitle()).isEqualTo("My first blog");
assertThat(response.getContent()).isEqualTo("Hello Armeria!");
System.out.println(response);
}
@Test
@Order(2)
void getBlogPost() throws TException {
final BlogClient client = new BlogClient(server.httpUri(), "/thrift");
final BlogPost blogPost = client.getBlogPost(0);
assertThat(blogPost.getTitle()).isEqualTo("My first blog");
assertThat(blogPost.getContent()).isEqualTo("Hello Armeria!");
System.out.println(blogPost);
}
@Test
@Order(3)
void getInvalidBlogPost() {
final BlogClient client = new BlogClient(server.httpUri(), "/thrift");
final Throwable exception = catchThrowable(() -> {
client.getBlogPost(Integer.MAX_VALUE);
});
assertThat(exception).isInstanceOf(BlogNotFoundException.class)
.extracting("reason")
.asString()
.isEqualTo("The blog post does not exist. ID: " + Integer.MAX_VALUE);
}
@Test
@Order(4)
void listBlogPosts() throws TException {
final BlogClient client = new BlogClient(server.httpUri(), "/thrift");
client.createBlogPost("My second blog", "Armeria is awesome!");
final List<BlogPost> blogs = client.listBlogPosts(false);
assertThat(blogs).hasSize(2);
final BlogPost firstBlog = blogs.get(0);
assertThat(firstBlog.getTitle()).isEqualTo("My first blog");
assertThat(firstBlog.getContent()).isEqualTo("Hello Armeria!");
final BlogPost secondBlog = blogs.get(1);
assertThat(secondBlog.getTitle()).isEqualTo("My second blog");
assertThat(secondBlog.getContent()).isEqualTo("Armeria is awesome!");
System.out.println(blogs);
}
@Test
@Order(5)
void updateBlogPosts() throws TException {
final BlogClient client = new BlogClient(server.httpUri(), "/thrift");
final BlogPost updated = client.updateBlogPost(0, "My first blog", "Hello awesome Armeria!");
assertThat(updated.getId()).isZero();
assertThat(updated.getTitle()).isEqualTo("My first blog");
assertThat(updated.getContent()).isEqualTo("Hello awesome Armeria!");
System.out.println(updated);
}
@Test
@Order(6)
void updateInvalidBlogPost() {
final BlogClient client = new BlogClient(server.httpUri(), "/thrift");
final Throwable exception = catchThrowable(() -> {
final BlogPost updated = client.updateBlogPost(Integer.MAX_VALUE, "My first blog",
"Hello awesome Armeria!");
});
assertThat(exception).isInstanceOf(BlogNotFoundException.class)
.extracting("reason")
.asString()
.isEqualTo("The blog post does not exist. ID: " + Integer.MAX_VALUE);
}
@Test
@Order(7)
void deleteBlogPost() throws TException {
final BlogClient client = new BlogClient(server.httpUri(), "/thrift");
client.deleteBlogPost(1);
final Throwable exception = catchThrowable(() -> {
client.getBlogPost(1);
});
assertThat(exception).isInstanceOf(BlogNotFoundException.class)
.extracting("reason")
.asString()
.isEqualTo("The blog post does not exist. ID: 1");
}
@Test
@Order(8)
void deleteInvalidBlogPost() {
final BlogClient client = new BlogClient(server.httpUri(), "/thrift");
final Throwable exception = catchThrowable(() -> {
client.deleteBlogPost(100);
});
assertThat(exception).isInstanceOf(BlogNotFoundException.class)
.extracting("reason")
.asString()
.isEqualTo("The blog post does not exist. ID: 100");
}
}