import de.juplo.facebook.errors.GraphApiException.Type;
-import java.time.Duration;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import org.junit.After;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.springframework.http.HttpStatus;
import org.springframework.http.client.reactive.JettyClientHttpConnector;
import org.springframework.web.reactive.function.client.WebClient;
+import org.springframework.web.reactive.function.client.WebClientResponseException.BadRequest;
+import org.springframework.web.reactive.function.client.WebClientResponseException.InternalServerError;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
@Test
public void testNoError()
{
- //server
- // .enqueue(new MockResponse()
- // .setResponseCode(HttpStatus.CONTINUE.value())
- // .setHeader("Content-Type", "application/json")
- // .setBody("Hallo Welt!"));
- //
- //try
- //{
- // String result = clientTemplate.getForObject(uri, String.class);
- // assertEquals("Hallo Welt!", result);
- //}
- //catch(Exception e)
- //{
- // LOG.debug("{}", e.toString());
- // fail("Unexpected error: " + e);
- //}
+ Mono<String> result;
+
+
+ server
+ .enqueue(new MockResponse()
+ .setResponseCode(HttpStatus.CONTINUE.value())
+ .setHeader("Content-Type", "application/json")
+ .setBody("Hallo Welt!"));
+
+ result =
+ webClient
+ .get()
+ .uri("/egal")
+ .retrieve()
+ .bodyToMono(String.class);
+
+ StepVerifier
+ .create(result)
+ .expectNext("Hallo Welt!")
+ .expectComplete()
+ .verify();
server
.setHeader("Content-Type", "text/plain")
.setBody("Hallo Welt!"));
- try
- {
- String result = clientTemplate.getForObject(uri, String.class);
- assertEquals("Hallo Welt!", result);
- }
- catch(Exception e)
- {
- LOG.debug("{}", e.toString());
- fail("Unexpected error: " + e);
- }
+ result =
+ webClient
+ .get()
+ .uri("/egal")
+ .retrieve()
+ .bodyToMono(String.class);
+
+ StepVerifier
+ .create(result)
+ .expectNext("Hallo Welt!")
+ .expectComplete()
+ .verify();
server
.setHeader("Content-Type", "text/plain")
.setBody("Hallo Welt!"));
- try
- {
- String result = clientTemplate.getForObject(uri, String.class);
- assertEquals("Hallo Welt!", result);
- }
- catch(Exception e)
- {
- LOG.debug("{}", e.toString());
- fail("Unexpected error: " + e);
- }
+ result =
+ webClient
+ .get()
+ .uri("/egal")
+ .retrieve()
+ .bodyToMono(String.class);
+
+ StepVerifier
+ .create(result)
+ .expectNext("Hallo Welt!")
+ .expectComplete()
+ .verify();
server
.setHeader("Content-Type", "text/plain")
.setBody("Hallo Welt!"));
- try
- {
- clientTemplate.getForObject(uri, String.class);
- fail("The parent handler should have raised an exception!");
- }
- catch(HttpServerErrorException e)
- {
- LOG.debug("Expexted error: {}", e.toString());
- }
- catch(Exception e)
- {
- LOG.debug("{}", e.toString());
- fail("Unexpected error: " + e);
- }
+ result =
+ webClient
+ .get()
+ .uri("/egal")
+ .retrieve()
+ .bodyToMono(String.class);
+
+ StepVerifier
+ .create(result)
+ .expectError(InternalServerError.class)
+ .verify();
}
@Test
" }\n" +
"}"));
- try
- {
- clientTemplate.getForObject(uri, String.class);
- fail("The expected exception was not thrown");
- }
- catch(RateLimitExceededException e)
+ Mono<String> result =
+ webClient
+ .get()
+ .uri("/egal")
+ .retrieve()
+ .bodyToMono(String.class);
+
+ StepVerifier.create(result).expectErrorSatisfies(throwable ->
{
+ assertEquals(RateLimitExceededException.class, throwable.getClass());
+ RateLimitExceededException e = (RateLimitExceededException)throwable;
LOG.debug("{}", e.toString());
assertEquals(new Integer(613), e.getCode());
assertEquals("(#613) Calls to stream have exceeded the rate of 600 calls per 600 seconds.", e.getMessage());
assertEquals(Type.OAuthException, e.getType());
- }
+ })
+ .verify();
}
@Test
" }\n" +
"}"));
- try
- {
- clientTemplate.getForObject(uri, String.class);
- fail("The expected exception was not thrown");
- }
- catch(GraphApiException e)
+
+ Mono<String> result =
+ webClient
+ .get()
+ .uri("/egal")
+ .retrieve()
+ .bodyToMono(String.class);
+
+ StepVerifier.create(result).expectErrorSatisfies(throwable ->
{
+ assertEquals(UnmappedErrorException.class, throwable.getClass());
+ UnmappedErrorException e = (UnmappedErrorException)throwable;
LOG.debug("{}", e.toString());
assertEquals(new Integer(999999999), e.getCode());
assertEquals("This error does not exist.", e.getMessage());
fail("unmapped type was resolved by enum: " + type);
}
catch (IllegalArgumentException ee) {}
- }
+ })
+ .verify();
}
@Test
public void testInvlalidError()
{
+ Mono<String> result;
+
+
server
.enqueue(new MockResponse()
.setResponseCode(HttpStatus.BAD_REQUEST.value())
" }\n" +
"}"));
- try
- {
- clientTemplate.getForObject(uri, String.class);
- fail("The parent handler should have raised an exception!");
- }
- catch(HttpClientErrorException e)
- {
- LOG.debug("Expexted error: {}", e.toString());
- }
- catch(Exception e)
- {
- LOG.debug("{}", e.toString());
- fail("Unexpected error: " + e);
- }
+ result =
+ webClient
+ .get()
+ .uri("/egal")
+ .retrieve()
+ .bodyToMono(String.class);
+
+ StepVerifier.create(result).expectError(BadRequest.class).verify();
server
.setHeader("Content-Type", "text/plain")
.setBody("Hallo Welt!"));
- try
- {
- clientTemplate.getForObject(uri, String.class);
- fail("The parent handler should have raised an exception!");
- }
- catch(HttpClientErrorException e)
- {
- LOG.debug("Expexted error: {}", e.toString());
- }
- catch(Exception e)
- {
- LOG.debug("{}", e.toString());
- fail("Unexpected error: " + e);
- }
- }
-
- @Test
- public void testValidError()
- {
- server
- .enqueue(new MockResponse()
- .setResponseCode(HttpStatus.BAD_REQUEST.value())
- .setHeader("Content-Type", "application/json")
- .setBody(
- "{\n" +
- " \"error\":\n" +
- " {\n" +
- " \"message\": \"(#613) Calls to stream have exceeded the rate of 600 calls per 600 seconds.\",\n" +
- " \"type\": \"OAuthException\",\n" +
- " \"code\": 613\n" +
- " }\n" +
- "}"));
-
-
- Mono<String> result =
+ result =
webClient
.get()
.uri("/egal")
.retrieve()
.bodyToMono(String.class);
- StepVerifier
- .create(result)
- .expectErrorSatisfies(throwable ->
- {
- assertEquals(RateLimitExceededException.class, throwable.getClass());
- RateLimitExceededException e = (RateLimitExceededException)throwable;
- LOG.debug("{}", e.toString());
- assertEquals(new Integer(613), e.getCode());
- assertEquals("(#613) Calls to stream have exceeded the rate of 600 calls per 600 seconds.", e.getMessage());
- assertEquals(Type.OAuthException, e.getType());
- })
- .verify(Duration.ofSeconds(3));
+ StepVerifier.create(result).expectError(BadRequest.class).verify();
}
}