1 package de.juplo.facebook.errors;
4 import de.juplo.facebook.errors.GraphApiException.Type;
5 import okhttp3.mockwebserver.MockResponse;
6 import okhttp3.mockwebserver.MockWebServer;
7 import org.junit.After;
8 import static org.junit.Assert.assertEquals;
9 import static org.junit.Assert.fail;
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.HttpStatus;
15 import org.springframework.http.client.reactive.JettyClientHttpConnector;
16 import org.springframework.web.reactive.function.client.WebClient;
17 import org.springframework.web.reactive.function.client.WebClientResponseException.BadRequest;
18 import org.springframework.web.reactive.function.client.WebClientResponseException.InternalServerError;
19 import reactor.core.publisher.Mono;
20 import reactor.test.StepVerifier;
27 public class GraphApiExchangeFilterFunctionIntegrationTest
29 private static final Logger LOG =
30 LoggerFactory.getLogger(GraphApiExchangeFilterFunctionIntegrationTest.class);
32 private MockWebServer server;
33 private WebClient webClient;
39 server = new MockWebServer();
43 .clientConnector(new JettyClientHttpConnector())
44 .baseUrl(server.url("/").toString())
45 .filter(new GraphApiExchangeFilterFunction())
50 public void shutdown() throws Exception
52 this.server.shutdown();
57 public void testNoError()
63 // .enqueue(new MockResponse()
64 // .setResponseCode(HttpStatus.CONTINUE.value())
65 // .setHeader("Content-Type", "application/json")
66 // .setBody("Hallo Welt!"));
73 // .bodyToMono(String.class);
77 // .expectNext("Hallo Welt!")
83 .enqueue(new MockResponse()
84 .setResponseCode(HttpStatus.OK.value())
85 .setHeader("Content-Type", "text/plain")
86 .setBody("Hallo Welt!"));
93 .bodyToMono(String.class);
97 .expectNext("Hallo Welt!")
103 .enqueue(new MockResponse()
104 .setResponseCode(HttpStatus.TEMPORARY_REDIRECT.value())
105 .setHeader("Location", server.url("/woanders"))
106 .setHeader("Content-Type", "text/plain")
107 .setBody("Jetzt doch woanders..."));
109 .enqueue(new MockResponse()
110 .setResponseCode(HttpStatus.OK.value())
111 .setHeader("Content-Type", "text/plain")
112 .setBody("Hallo Welt!"));
119 .bodyToMono(String.class);
123 .expectNext("Hallo Welt!")
129 .enqueue(new MockResponse()
130 .setResponseCode(HttpStatus.INTERNAL_SERVER_ERROR.value())
131 .setHeader("Content-Type", "text/plain")
132 .setBody("Hallo Welt!"));
139 .bodyToMono(String.class);
143 .expectError(InternalServerError.class)
148 public void testValidError()
151 .enqueue(new MockResponse()
152 .setResponseCode(HttpStatus.BAD_REQUEST.value())
153 .setHeader("Content-Type", "application/json")
158 " \"message\": \"(#613) Calls to stream have exceeded the rate of 600 calls per 600 seconds.\",\n" +
159 " \"type\": \"OAuthException\",\n" +
164 Mono<String> result =
169 .bodyToMono(String.class);
171 StepVerifier.create(result).expectErrorSatisfies(throwable ->
173 assertEquals(RateLimitExceededException.class, throwable.getClass());
174 RateLimitExceededException e = (RateLimitExceededException)throwable;
175 LOG.debug("{}", e.toString());
176 assertEquals(new Integer(613), e.getCode());
177 assertEquals("(#613) Calls to stream have exceeded the rate of 600 calls per 600 seconds.", e.getMessage());
178 assertEquals(Type.OAuthException, e.getType());
184 public void testUnmappedError()
187 .enqueue(new MockResponse()
188 .setResponseCode(HttpStatus.BAD_REQUEST.value())
189 .setHeader("Content-Type", "application/json")
194 " \"message\": \"This error does not exist.\",\n" +
195 " \"type\": \"NonexistentTypeException\",\n" +
196 " \"code\": 999999999\n" +
201 Mono<String> result =
206 .bodyToMono(String.class);
208 StepVerifier.create(result).expectErrorSatisfies(throwable ->
210 assertEquals(UnmappedErrorException.class, throwable.getClass());
211 UnmappedErrorException e = (UnmappedErrorException)throwable;
212 LOG.debug("{}", e.toString());
213 assertEquals(new Integer(999999999), e.getCode());
214 assertEquals("This error does not exist.", e.getMessage());
217 Type type = e.getType();
218 LOG.error("unknown type: {}", type);
219 fail("unmapped type was resolved by enum: " + type);
221 catch (IllegalArgumentException ee) {}
227 public void testInvlalidError()
233 .enqueue(new MockResponse()
234 .setResponseCode(HttpStatus.BAD_REQUEST.value())
235 .setHeader("Content-Type", "application/json")
240 " \"message\": \"Not a Graph-Api-Exception.\",\n" +
241 " \"type\": \"Whatever\",\n" +
242 " \"code\": \"some string\"\n" +
251 .bodyToMono(String.class);
253 StepVerifier.create(result).expectError(BadRequest.class).verify();
257 .enqueue(new MockResponse()
258 .setResponseCode(HttpStatus.BAD_REQUEST.value())
259 .setHeader("Content-Type", "text/plain")
260 .setBody("Hallo Welt!"));
267 .bodyToMono(String.class);
269 StepVerifier.create(result).expectError(BadRequest.class).verify();