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