WIP: WebClient
[facebook-errors] / src / test / java / de / juplo / facebook / errors / GraphApiExchangeFilterFunctionIntegrationTest.java
1 package de.juplo.facebook.errors;
2
3
4 import de.juplo.facebook.errors.GraphApiException.Type;
5 import java.time.Duration;
6 import okhttp3.mockwebserver.MockResponse;
7 import okhttp3.mockwebserver.MockWebServer;
8 import org.junit.After;
9 import static org.junit.Assert.assertEquals;
10 import org.junit.Before;
11 import org.junit.Test;
12 import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory;
14 import org.springframework.web.reactive.function.client.WebClient;
15 import reactor.core.publisher.Mono;
16 import reactor.test.StepVerifier;
17
18
19 /**
20  *
21  * @author Kai Moritz
22  */
23 public class GraphApiExchangeFilterFunctionIntegrationTest
24 {
25   private static final Logger LOG =
26       LoggerFactory.getLogger(GraphApiExchangeFilterFunctionIntegrationTest.class);
27
28   private MockWebServer server;
29         private WebClient webClient;
30
31
32         @Before
33         public void setup()
34   {
35     server = new MockWebServer();
36     webClient =
37         WebClient
38             .builder()
39             .baseUrl(server.url("/").toString())
40             .filter(GraphApiExchangeFilterFunction.INSTANCE)
41             .build();
42         }
43
44         @After
45         public void shutdown() throws Exception
46   {
47     this.server.shutdown();
48         }
49
50
51   @Test
52   public void testValidError()
53   {
54     LOG.info("testValidError");
55
56
57     server
58         .enqueue(new MockResponse()
59             .setStatus("400")
60             .setHeader("Content-Type", "application/json")
61             .setBody(
62                 "{\n" +
63                 "  \"error\":\n" +
64                 "  {\n" +
65                 "    \"message\": \"(#613) Calls to stream have exceeded the rate of 600 calls per 600 seconds.\",\n" +
66                 "    \"type\": \"OAuthException\",\n" +
67                 "    \"code\": 613\n" +
68                 "  }\n" +
69                 "}"));
70
71
72     Mono<String> result =
73         webClient
74             .get()
75             .uri("/egal")
76             .retrieve()
77             .bodyToMono(String.class);
78
79     StepVerifier
80         .create(result)
81         .expectErrorSatisfies(throwable ->
82         {
83           assertEquals(RateLimitExceededException.class, throwable.getClass());
84           RateLimitExceededException e = (RateLimitExceededException)throwable;
85           LOG.debug("{}", e.toString());
86           assertEquals(new Integer(613), e.getCode());
87           assertEquals("(#613) Calls to stream have exceeded the rate of 600 calls per 600 seconds.", e.getMessage());
88           assertEquals(Type.OAuthException, e.getType());
89         })
90         .verify(Duration.ofSeconds(3));
91   }
92 }