Skip to content

Commit

Permalink
Simple routes for basic operations
Browse files Browse the repository at this point in the history
  • Loading branch information
piotr-yuxuan committed Sep 28, 2023
1 parent 633c493 commit ad6cba6
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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<int[]> getAnswer() {
int[] array = { 42 };
return new ResponseEntity<>(array, HttpStatus.OK);
}

@GetMapping("addition")
public ResponseEntity<Long> getAddition(@RequestParam Long a, @RequestParam Long b) {
return new ResponseEntity<>(a + b, HttpStatus.OK);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class SpringBootPlaygroundApplicationTests {
class SpringBootPlaygroundApplicationTest {

@Test
void contextLoads() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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"));
}

}

0 comments on commit ad6cba6

Please sign in to comment.