diff --git a/src/main/java/djmil/cashcard/CashCardController.java b/src/main/java/djmil/cashcard/CashCardController.java new file mode 100644 index 0000000..c3771f4 --- /dev/null +++ b/src/main/java/djmil/cashcard/CashCardController.java @@ -0,0 +1,22 @@ +package djmil.cashcard; + +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/cashcards") +public class CashCardController { + + @GetMapping("/{requestedId}") + public ResponseEntity findById(@PathVariable Long requestedId) { + if (requestedId.equals(99L)) { + CashCard cashCard = new CashCard(99L, 123.45); + return ResponseEntity.ok(cashCard); + } else { + return ResponseEntity.notFound().build(); + } + } +} diff --git a/src/test/java/djmil/cashcard/CashcardApplicationTests.java b/src/test/java/djmil/cashcard/CashcardApplicationTests.java index efc5249..30c8e2b 100644 --- a/src/test/java/djmil/cashcard/CashcardApplicationTests.java +++ b/src/test/java/djmil/cashcard/CashcardApplicationTests.java @@ -1,13 +1,48 @@ package djmil.cashcard; import org.junit.jupiter.api.Test; -import org.springframework.boot.test.context.SpringBootTest; -@SpringBootTest +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; + +import com.jayway.jsonpath.DocumentContext; +import com.jayway.jsonpath.JsonPath; + +import static org.assertj.core.api.Assertions.assertThat; + +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) class CashcardApplicationTests { @Test void contextLoads() { } + @Autowired + TestRestTemplate restTemplate; + + @Test + void shouldReturnACashCardWhenDataIsSaved() { + ResponseEntity response = restTemplate.getForEntity("/cashcards/99", String.class); + + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); + + DocumentContext documentContext = JsonPath.parse(response.getBody()); + + Number id = documentContext.read("$.id"); + assertThat(id).isEqualTo(99); + + Double amount = documentContext.read("$.amount"); + assertThat(amount).isEqualTo(123.45); + } + + @Test + void shouldNotReturnACashCardWithAnUnknownId() { + ResponseEntity response = restTemplate.getForEntity("/cashcards/1000", String.class); + + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND); + assertThat(response.getBody()).isBlank(); + } }