Customized the error-pages by adding a template
[demos/testing] / src / test / java / de / juplo / demo / RestControllerIT.java
index 68ad229..9d7a3f5 100644 (file)
@@ -4,6 +4,8 @@ package de.juplo.demo;
 import org.junit.jupiter.api.DisplayName;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.ExtendWith;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -12,6 +14,7 @@ import org.springframework.boot.test.mock.mockito.MockBean;
 import org.springframework.http.MediaType;
 import org.springframework.test.context.junit.jupiter.SpringExtension;
 import org.springframework.test.web.reactive.server.WebTestClient;
+import org.springframework.web.reactive.function.client.WebClientResponseException;
 import reactor.core.publisher.Mono;
 
 
@@ -29,18 +32,61 @@ public class RestControllerIT
   RemoteContentService service;
 
 
-  @Test
-  @DisplayName("Valid mapping for RestController: /?path=foo")
-  void testUriWithParameter()
+  @DisplayName("Valid mappings for RestController with parameter")
+  @ParameterizedTest()
+  @ValueSource(strings = { "/?path=foo", "?path=foo" })
+  void testUriWithParameter(String uri)
   {
     when(service.getRemoteText("foo")).thenReturn(Mono.just("bar"));
     webClient
         .get()
-        .uri("/?path=foo")
+        .uri(uri)
         .header("Accept", MediaType.TEXT_PLAIN_VALUE)
         .exchange()
         .expectStatus().isOk()
         .expectBody(String.class).isEqualTo("bar");
     verify(service).getRemoteText("foo");
   }
+
+  @DisplayName("Mappings for RestController without a parameter")
+  @ParameterizedTest(name = "{arguments} ==> uri={0}")
+  @ValueSource(strings = { "/", "", "?foo=bar", "/?foo=bar"})
+  void testUriWithoutParameter(String uri)
+  {
+    webClient
+        .get()
+        .uri(uri)
+        .header("Accept", MediaType.TEXT_PLAIN_VALUE)
+        .exchange()
+        .expectStatus().isBadRequest()
+        .expectBody()
+        .jsonPath("status").isEqualTo(400)
+        .jsonPath("error").isEqualTo("Bad Request")
+        .jsonPath("message").isEqualTo("Required String parameter 'path' is not present")
+        .jsonPath("timestamp").exists();
+  }
+
+  @DisplayName("Remote-Server answers with 404: Not Found")
+  @Test()
+  void testResponseNotFound()
+  {
+    when(service.getRemoteText("foo")).thenReturn(Mono.error(exception(404)));
+    webClient
+        .get()
+        .uri("/?path=foo")
+        .header("Accept", MediaType.TEXT_PLAIN_VALUE)
+        .exchange()
+        .expectStatus().isNotFound()
+        .expectBody()
+        .jsonPath("status").isEqualTo(404)
+        .jsonPath("error").isEqualTo("Not Found")
+        .jsonPath("path").isEqualTo("/")
+        .jsonPath("timestamp").exists();
+  }
+
+
+  WebClientResponseException exception(int status)
+  {
+    return WebClientResponseException.create(status, "MESSAGE", null, null, null);
+  }
 }