Added a `@RestController` status-interceptor
authorKai Moritz <kai@juplo.de>
Sun, 9 Apr 2023 13:17:22 +0000 (15:17 +0200)
committerKai Moritz <kai@juplo.de>
Sun, 9 Apr 2023 18:35:57 +0000 (20:35 +0200)
src/main/java/de/juplo/demo/DemoApplication.java
src/main/java/de/juplo/demo/DemoRestController.java [new file with mode: 0644]
src/main/java/de/juplo/demo/GreetingTO.java [new file with mode: 0644]
src/test/java/de/juplo/demo/DemoApplicationTests.java

index 2917409..64e04c8 100644 (file)
@@ -9,20 +9,30 @@ import org.springframework.context.annotation.Bean;
 @SpringBootApplication
 public class DemoApplication
 {
+       @Value("${application.name}")
+       String applicationName;
        @Value("${build.version}")
        String projectVersion;
 
 
        @Bean
-       public BackendVersionInterceptor backendVersionInterceptor()
+       public BackendVersionInterceptor backendVersionInterceptor(
+                       @Value("${build.version}") String projectVersion)
        {
                return new BackendVersionInterceptor(projectVersion);
        }
 
+       @Bean
+       public String from(
+                       @Value("${application.name}") String applicationName,
+                       @Value("${build.version}") String projectVersion)
+       {
+               return applicationName + "@" + projectVersion;
+       }
+
 
        public static void main(String[] args)
        {
                SpringApplication.run(DemoApplication.class, args);
        }
-
 }
diff --git a/src/main/java/de/juplo/demo/DemoRestController.java b/src/main/java/de/juplo/demo/DemoRestController.java
new file mode 100644 (file)
index 0000000..4259a30
--- /dev/null
@@ -0,0 +1,34 @@
+package de.juplo.demo;
+
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.web.bind.annotation.*;
+
+
+@RequiredArgsConstructor
+@RestController
+@Slf4j
+public class DemoRestController
+{
+  public final String PLACEHOLDER_TO = "TO_NAME";
+  public final String PLACEHOLDER_FROM = "FROM_NAME";
+
+
+  private final String from;
+
+
+  @PostMapping("{to}")
+  public GreetingTO greet(
+      @PathVariable String to,
+      @RequestBody String greeting)
+  {
+    String message = greeting
+        .replaceAll(PLACEHOLDER_FROM, from)
+        .replaceAll(PLACEHOLDER_TO, to)
+        .trim();
+
+    log.info("Greeting from {} to {}: {}", from, to, message);
+
+    return GreetingTO.of(message, from, to);
+  }
+}
diff --git a/src/main/java/de/juplo/demo/GreetingTO.java b/src/main/java/de/juplo/demo/GreetingTO.java
new file mode 100644 (file)
index 0000000..c61a30e
--- /dev/null
@@ -0,0 +1,21 @@
+package de.juplo.demo;
+
+
+import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
+import com.fasterxml.jackson.annotation.JsonInclude;
+import lombok.AllArgsConstructor;
+import lombok.Data;
+
+import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_EMPTY;
+
+
+@Data
+@AllArgsConstructor(staticName = "of")
+@JsonIgnoreProperties(ignoreUnknown = true)
+@JsonInclude(NON_EMPTY)
+public class GreetingTO
+{
+  private String greeting;
+  private String from;
+  private String to;
+}
index f2981dc..576544f 100644 (file)
@@ -8,11 +8,14 @@ import org.springframework.boot.test.context.SpringBootTest;
 import org.springframework.test.web.servlet.MockMvc;
 
 import static de.juplo.demo.BackendVersionInterceptor.BACKEND_VERSION;
+import static org.springframework.http.HttpHeaders.ACCEPT;
 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
 
 
-@SpringBootTest
+@SpringBootTest(properties = { "application.name=FOO", "build.version=BAR" })
 @AutoConfigureMockMvc
 class DemoApplicationTests
 {
@@ -50,4 +53,38 @@ class DemoApplicationTests
                                                .header(BACKEND_VERSION, "FOO"))
                                .andExpect(status().isGone());
        }
+
+       @Test
+       void getGreetingIfBackendHeaderIsNotSet(@Autowired MockMvc mvc) throws Exception
+       {
+               mvc
+                               .perform(post("/peter")
+                                               .header(ACCEPT, "application/json")
+                                               .header(BACKEND_VERSION, projectVersion)
+                                               .content("Hello TO_NAME, nice to meet you. I'm FROM_NAME."))
+                               .andExpect(status().isOk())
+                               .andExpect(content().json("{\"greeting\": \"Hello peter, nice to meet you. I'm FOO@BAR.\"}"));
+       }
+
+       @Test
+       void getGreetingIfBackendHeaderDoesMatch(@Autowired MockMvc mvc) throws Exception
+       {
+               mvc
+                               .perform(post("/peter")
+                                               .header(ACCEPT, "application/json")
+                                               .header(BACKEND_VERSION, "FOO")
+                                               .content("Hello TO_NAME, nice to meet you. I'm FROM_NAME."))
+                               .andExpect(status().isGone());
+       }
+
+       @Test
+       void getGreetingIfBackendHeaderDoesNotMatch(@Autowired MockMvc mvc) throws Exception
+       {
+               mvc
+                               .perform(post("/peter")
+                                               .header(ACCEPT, "application/json")
+                                               .content("Hello TO_NAME, nice to meet you. I'm FROM_NAME."))
+                               .andExpect(status().isOk())
+                               .andExpect(content().json("{\"greeting\": \"Hello peter, nice to meet you. I'm FOO@BAR.\"}"));
+       }
 }