From 19aa42941f6c8ccaec8e70cb8397f931fbe7b99c Mon Sep 17 00:00:00 2001 From: djmil Date: Fri, 21 Jul 2023 12:38:51 +0200 Subject: [PATCH] POST - URI for Location Header - CRUD - create --- .../djmil/cashcard/CashCardController.java | 15 +++++++++++++ .../cashcard/CashcardApplicationTests.java | 22 +++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/src/main/java/djmil/cashcard/CashCardController.java b/src/main/java/djmil/cashcard/CashCardController.java index fb4b57f..c443639 100644 --- a/src/main/java/djmil/cashcard/CashCardController.java +++ b/src/main/java/djmil/cashcard/CashCardController.java @@ -2,10 +2,14 @@ package djmil.cashcard; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.util.UriComponentsBuilder; +import java.net.URI; import java.util.Optional; @RestController @@ -28,4 +32,15 @@ public class CashCardController { return ResponseEntity.notFound().build(); } } + + @PostMapping + private ResponseEntity createCashCard(@RequestBody CashCard newCashCardRequest, UriComponentsBuilder ucb) { + CashCard savedCashCard = cashCardRepository.save(newCashCardRequest); // CRUD - Create + + URI locationOfNewCashCard = ucb + .path("cashcards/{id}") + .buildAndExpand(savedCashCard.id()) + .toUri(); + return ResponseEntity.created(locationOfNewCashCard).build(); + } } diff --git a/src/test/java/djmil/cashcard/CashcardApplicationTests.java b/src/test/java/djmil/cashcard/CashcardApplicationTests.java index 30c8e2b..e4ebcdf 100644 --- a/src/test/java/djmil/cashcard/CashcardApplicationTests.java +++ b/src/test/java/djmil/cashcard/CashcardApplicationTests.java @@ -11,6 +11,8 @@ import org.springframework.http.ResponseEntity; import com.jayway.jsonpath.DocumentContext; import com.jayway.jsonpath.JsonPath; +import java.net.URI; + import static org.assertj.core.api.Assertions.assertThat; @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @@ -45,4 +47,24 @@ class CashcardApplicationTests { assertThat(response.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND); assertThat(response.getBody()).isBlank(); } + + @Test + void shouldCreateANewCashCard() { + CashCard newCashCard = new CashCard(null, 250.00); + ResponseEntity createResponse = restTemplate.postForEntity("/cashcards", newCashCard, Void.class ); + + assertThat(createResponse.getStatusCode()).isEqualTo(HttpStatus.CREATED); + + URI locationOfNewCashCard = createResponse.getHeaders().getLocation(); + ResponseEntity getResponse = restTemplate.getForEntity(locationOfNewCashCard, String.class); + assertThat(getResponse.getStatusCode()).isEqualTo(HttpStatus.OK); + + // Validate created CashCard JSON + DocumentContext documentContext = JsonPath.parse(getResponse.getBody()); + Number id = documentContext.read("$.id"); + Double amount = documentContext.read("$.amount"); + + assertThat(id).isNotNull(); + assertThat(amount).isEqualTo(250.00); + } }