From ad6cba6452bf83f9e0503a0f63d3518deb3beedd Mon Sep 17 00:00:00 2001 From: piotr-yuxuan Date: Thu, 28 Sep 2023 10:33:04 +0100 Subject: [PATCH] Simple routes for basic operations --- .../api/ApiController.java | 25 ++++++++++++ ... SpringBootPlaygroundApplicationTest.java} | 2 +- .../api/ApiControllerTest.java | 39 +++++++++++++++++++ 3 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/github/piotryuxuan/springbootplayground/api/ApiController.java rename src/test/java/com/github/piotryuxuan/springbootplayground/{SpringBootPlaygroundApplicationTests.java => SpringBootPlaygroundApplicationTest.java} (81%) create mode 100644 src/test/java/com/github/piotryuxuan/springbootplayground/api/ApiControllerTest.java diff --git a/src/main/java/com/github/piotryuxuan/springbootplayground/api/ApiController.java b/src/main/java/com/github/piotryuxuan/springbootplayground/api/ApiController.java new file mode 100644 index 0000000..abd6806 --- /dev/null +++ b/src/main/java/com/github/piotryuxuan/springbootplayground/api/ApiController.java @@ -0,0 +1,25 @@ +package com.github.piotryuxuan.springbootplayground.api; + +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/api/v1") +public class ApiController { + + @GetMapping("answer") + public ResponseEntity getAnswer() { + int[] array = { 42 }; + return new ResponseEntity<>(array, HttpStatus.OK); + } + + @GetMapping("addition") + public ResponseEntity getAddition(@RequestParam Long a, @RequestParam Long b) { + return new ResponseEntity<>(a + b, HttpStatus.OK); + } + +} diff --git a/src/test/java/com/github/piotryuxuan/springbootplayground/SpringBootPlaygroundApplicationTests.java b/src/test/java/com/github/piotryuxuan/springbootplayground/SpringBootPlaygroundApplicationTest.java similarity index 81% rename from src/test/java/com/github/piotryuxuan/springbootplayground/SpringBootPlaygroundApplicationTests.java rename to src/test/java/com/github/piotryuxuan/springbootplayground/SpringBootPlaygroundApplicationTest.java index 701a4af..111e95c 100644 --- a/src/test/java/com/github/piotryuxuan/springbootplayground/SpringBootPlaygroundApplicationTests.java +++ b/src/test/java/com/github/piotryuxuan/springbootplayground/SpringBootPlaygroundApplicationTest.java @@ -4,7 +4,7 @@ import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest -class SpringBootPlaygroundApplicationTests { +class SpringBootPlaygroundApplicationTest { @Test void contextLoads() { diff --git a/src/test/java/com/github/piotryuxuan/springbootplayground/api/ApiControllerTest.java b/src/test/java/com/github/piotryuxuan/springbootplayground/api/ApiControllerTest.java new file mode 100644 index 0000000..9bbec94 --- /dev/null +++ b/src/test/java/com/github/piotryuxuan/springbootplayground/api/ApiControllerTest.java @@ -0,0 +1,39 @@ +package com.github.piotryuxuan.springbootplayground.api; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; + +@SpringBootTest +@AutoConfigureMockMvc +class ApiControllerTest { + + @Autowired + private MockMvc mockMvc; + + @Test + public void testGetAnswer() throws Exception { + mockMvc.perform(get("/api/v1/answer") + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(content().json("[42]")); + } + + @Test + public void testGetAddition() throws Exception { + mockMvc.perform(get("/api/v1/addition") + .contentType(MediaType.APPLICATION_JSON) + .param("a", "3") + .param("b", "3")) + .andExpect(status().isOk()) + .andExpect(content().json("6")); + } + +}