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