|
16 | 16 |
|
17 | 17 | package com.linecorp.bot.client; |
18 | 18 |
|
19 | | -import static org.assertj.core.api.Assertions.assertThat; |
20 | | -import static org.hamcrest.CoreMatchers.isA; |
| 19 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
21 | 20 |
|
| 21 | +import java.util.Arrays; |
| 22 | +import java.util.Collection; |
22 | 23 | import java.util.concurrent.ExecutionException; |
23 | 24 |
|
24 | | -import org.hamcrest.CustomTypeSafeMatcher; |
25 | | -import org.junit.Rule; |
26 | 25 | import org.junit.Test; |
27 | | -import org.junit.rules.ExpectedException; |
| 26 | +import org.junit.runner.RunWith; |
| 27 | +import org.junit.runners.Parameterized; |
| 28 | +import org.junit.runners.Parameterized.Parameters; |
28 | 29 |
|
29 | 30 | import com.linecorp.bot.client.exception.BadRequestException; |
30 | 31 | import com.linecorp.bot.client.exception.ForbiddenException; |
31 | | -import com.linecorp.bot.client.exception.LineMessagingException; |
32 | 32 | import com.linecorp.bot.client.exception.LineServerException; |
33 | 33 | import com.linecorp.bot.client.exception.NotFoundException; |
34 | 34 | import com.linecorp.bot.client.exception.TooManyRequestsException; |
35 | 35 | import com.linecorp.bot.client.exception.UnauthorizedException; |
36 | 36 | import com.linecorp.bot.model.error.ErrorResponse; |
37 | 37 |
|
| 38 | +@RunWith(Parameterized.class) |
38 | 39 | public class LineMessagingClientImplWiremockTest extends AbstractWiremockTest { |
39 | 40 | static final ErrorResponse ERROR_RESPONSE = |
40 | 41 | new ErrorResponse(null, "Error Message", null); |
41 | | - |
42 | | - @Rule |
43 | | - public final ExpectedException expectedException = ExpectedException.none(); |
44 | | - |
45 | | - @Test(timeout = ASYNC_TEST_TIMEOUT) |
46 | | - public void status400BadRequestTest() throws Exception { |
47 | | - // Mocking |
48 | | - mocking(400, ERROR_RESPONSE); |
49 | | - |
50 | | - // Expect |
51 | | - expectedException.expect(ExecutionException.class); |
52 | | - expectedException.expectCause(isA(BadRequestException.class)); |
53 | | - expectedException.expectCause(errorResponseIs(ERROR_RESPONSE)); |
54 | | - |
55 | | - // Do |
56 | | - lineBlobClient.getMessageContent("TOKEN").get(); |
57 | | - } |
58 | | - |
59 | | - @Test(timeout = ASYNC_TEST_TIMEOUT) |
60 | | - public void status401UnauthorizedTest() throws Exception { |
61 | | - // Mocking |
62 | | - mocking(401, ERROR_RESPONSE); |
63 | | - |
64 | | - // Expect |
65 | | - expectedException.expect(ExecutionException.class); |
66 | | - expectedException.expectCause(isA(UnauthorizedException.class)); |
67 | | - expectedException.expectCause(errorResponseIs(ERROR_RESPONSE)); |
68 | | - |
69 | | - // Do |
70 | | - lineBlobClient.getMessageContent("TOKEN").get(); |
71 | | - } |
72 | | - |
73 | | - @Test(timeout = ASYNC_TEST_TIMEOUT) |
74 | | - public void status403ForbiddenTest() throws Exception { |
75 | | - // Mocking |
76 | | - mocking(403, ERROR_RESPONSE); |
77 | | - |
78 | | - // Expect |
79 | | - expectedException.expect(ExecutionException.class); |
80 | | - expectedException.expectCause(isA(ForbiddenException.class)); |
81 | | - expectedException.expectCause(errorResponseIs(ERROR_RESPONSE)); |
82 | | - |
83 | | - // Do |
84 | | - lineBlobClient.getMessageContent("TOKEN").get(); |
85 | | - } |
86 | | - |
87 | | - @Test(timeout = ASYNC_TEST_TIMEOUT) |
88 | | - public void status404NotFoundTest() throws Exception { |
89 | | - // Mocking |
90 | | - mocking(404, ERROR_RESPONSE); |
91 | | - |
92 | | - // Expect |
93 | | - expectedException.expect(ExecutionException.class); |
94 | | - expectedException.expectCause(isA(NotFoundException.class)); |
95 | | - expectedException.expectCause(errorResponseIs(ERROR_RESPONSE)); |
96 | | - |
97 | | - // Do |
98 | | - lineBlobClient.getMessageContent("TOKEN").get(); |
| 42 | + private static final String ERROR_MESSAGE |
| 43 | + = "Error Message : ErrorResponse(requestId=null, message=Error Message, details=[])"; |
| 44 | + |
| 45 | + @Parameters(name = "{index}: status={0}, expected={1}") |
| 46 | + public static Collection<Object[]> data() { |
| 47 | + return Arrays.asList(new Object[][] { |
| 48 | + { 400, BadRequestException.class }, |
| 49 | + { 401, UnauthorizedException.class }, |
| 50 | + { 403, ForbiddenException.class }, |
| 51 | + { 404, NotFoundException.class }, |
| 52 | + { 429, TooManyRequestsException.class }, |
| 53 | + { 500, LineServerException.class } |
| 54 | + }); |
99 | 55 | } |
100 | 56 |
|
101 | | - @Test(timeout = ASYNC_TEST_TIMEOUT) |
102 | | - public void status429TooManyRequestsTest() throws Exception { |
103 | | - // Mocking |
104 | | - mocking(429, ERROR_RESPONSE); |
| 57 | + private final int statusCode; |
| 58 | + private final Class<? extends Throwable> expectedClass; |
105 | 59 |
|
106 | | - // Expect |
107 | | - expectedException.expect(ExecutionException.class); |
108 | | - expectedException.expectCause(isA(TooManyRequestsException.class)); |
109 | | - expectedException.expectCause(errorResponseIs(ERROR_RESPONSE)); |
110 | | - |
111 | | - // Do |
112 | | - lineBlobClient.getMessageContent("TOKEN").get(); |
| 60 | + public LineMessagingClientImplWiremockTest(int statusCode, Class<? extends Throwable> expectedClass) { |
| 61 | + this.statusCode = statusCode; |
| 62 | + this.expectedClass = expectedClass; |
113 | 63 | } |
114 | 64 |
|
115 | 65 | @Test(timeout = ASYNC_TEST_TIMEOUT) |
116 | | - public void status500InternalServerErrorTest() throws Exception { |
| 66 | + public void statusCodeHandlerTest() throws Exception { |
117 | 67 | // Mocking |
118 | | - mocking(500, ERROR_RESPONSE); |
119 | | - |
120 | | - // Expect |
121 | | - expectedException.expect(ExecutionException.class); |
122 | | - expectedException.expectCause(isA(LineServerException.class)); |
123 | | - expectedException.expectCause(errorResponseIs(ERROR_RESPONSE)); |
| 68 | + mocking(statusCode, ERROR_RESPONSE); |
124 | 69 |
|
125 | 70 | // Do |
126 | | - lineBlobClient.getMessageContent("TOKEN").get(); |
127 | | - } |
128 | | - |
129 | | - private CustomTypeSafeMatcher<LineMessagingException> errorResponseIs(final ErrorResponse errorResponse) { |
130 | | - return new CustomTypeSafeMatcher<LineMessagingException>("Error Response") { |
131 | | - @Override |
132 | | - protected boolean matchesSafely(LineMessagingException item) { |
133 | | - assertThat(item.getErrorResponse()) |
134 | | - .isEqualTo(errorResponse); |
135 | | - |
136 | | - return true; |
137 | | - } |
138 | | - }; |
| 71 | + assertThatThrownBy(() -> lineBlobClient.getMessageContent("TOKEN").get()) |
| 72 | + .isInstanceOf(ExecutionException.class) |
| 73 | + .hasRootCauseInstanceOf(expectedClass) |
| 74 | + .hasRootCauseMessage(ERROR_MESSAGE); |
139 | 75 | } |
140 | 76 | } |
0 commit comments