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("Content-Type", "text/plain")
106 .setBody("Hallo Welt!"));
113 .bodyToMono(String.class);
117 .expectNext("Hallo Welt!")
123 .enqueue(new MockResponse()
124 .setResponseCode(HttpStatus.INTERNAL_SERVER_ERROR.value())
125 .setHeader("Content-Type", "text/plain")
126 .setBody("Hallo Welt!"));
133 .bodyToMono(String.class);
137 .expectError(InternalServerError.class)
142 public void testValidError()
145 .enqueue(new MockResponse()
146 .setResponseCode(HttpStatus.BAD_REQUEST.value())
147 .setHeader("Content-Type", "application/json")
152 " \"message\": \"(#613) Calls to stream have exceeded the rate of 600 calls per 600 seconds.\",\n" +
153 " \"type\": \"OAuthException\",\n" +
158 Mono<String> result =
163 .bodyToMono(String.class);
165 StepVerifier.create(result).expectErrorSatisfies(throwable ->
167 assertEquals(RateLimitExceededException.class, throwable.getClass());
168 RateLimitExceededException e = (RateLimitExceededException)throwable;
169 LOG.debug("{}", e.toString());
170 assertEquals(new Integer(613), e.getCode());
171 assertEquals("(#613) Calls to stream have exceeded the rate of 600 calls per 600 seconds.", e.getMessage());
172 assertEquals(Type.OAuthException, e.getType());
178 public void testUnmappedError()
181 .enqueue(new MockResponse()
182 .setResponseCode(HttpStatus.BAD_REQUEST.value())
183 .setHeader("Content-Type", "application/json")
188 " \"message\": \"This error does not exist.\",\n" +
189 " \"type\": \"NonexistentTypeException\",\n" +
190 " \"code\": 999999999\n" +
195 Mono<String> result =
200 .bodyToMono(String.class);
202 StepVerifier.create(result).expectErrorSatisfies(throwable ->
204 assertEquals(UnmappedErrorException.class, throwable.getClass());
205 UnmappedErrorException e = (UnmappedErrorException)throwable;
206 LOG.debug("{}", e.toString());
207 assertEquals(new Integer(999999999), e.getCode());
208 assertEquals("This error does not exist.", e.getMessage());
211 Type type = e.getType();
212 LOG.error("unknown type: {}", type);
213 fail("unmapped type was resolved by enum: " + type);
215 catch (IllegalArgumentException ee) {}
221 public void testInvlalidError()
227 .enqueue(new MockResponse()
228 .setResponseCode(HttpStatus.BAD_REQUEST.value())
229 .setHeader("Content-Type", "application/json")
234 " \"message\": \"Not a Graph-Api-Exception.\",\n" +
235 " \"type\": \"Whatever\",\n" +
236 " \"code\": \"some string\"\n" +
245 .bodyToMono(String.class);
247 StepVerifier.create(result).expectError(BadRequest.class).verify();
251 .enqueue(new MockResponse()
252 .setResponseCode(HttpStatus.BAD_REQUEST.value())
253 .setHeader("Content-Type", "text/plain")
254 .setBody("Hallo Welt!"));
261 .bodyToMono(String.class);
263 StepVerifier.create(result).expectError(BadRequest.class).verify();