From fa85126b31798e6a71b550c39c4ebf8bd2375555 Mon Sep 17 00:00:00 2001 From: hallgato Date: Mon, 25 Sep 2023 12:47:14 +0200 Subject: [PATCH] new tests and fibonacci logic --- .../controller/FibonacciController.kt | 5 +++ .../service/FibonacciService.kt | 4 +- .../integration/IntegrationTest.kt | 38 ++++++++++++++++++- .../service/FibonacciServiceTest.kt | 17 ++++++++- 4 files changed, 60 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/hu/obuda/devops/fibonaccirestapi/controller/FibonacciController.kt b/src/main/kotlin/hu/obuda/devops/fibonaccirestapi/controller/FibonacciController.kt index 35c9112..cc23824 100644 --- a/src/main/kotlin/hu/obuda/devops/fibonaccirestapi/controller/FibonacciController.kt +++ b/src/main/kotlin/hu/obuda/devops/fibonaccirestapi/controller/FibonacciController.kt @@ -2,10 +2,14 @@ package hu.obuda.devops.fibonaccirestapi.controller import hu.obuda.devops.fibonaccirestapi.service.FibonacciService import org.springframework.beans.factory.annotation.Autowired +import org.springframework.http.HttpStatus 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.ResponseStatus import org.springframework.web.bind.annotation.RestController +import org.springframework.web.client.HttpClientErrorException.BadRequest +import org.springframework.web.server.ResponseStatusException @RestController @RequestMapping("/") @@ -17,6 +21,7 @@ class FibonacciController { @GetMapping(value = ["fibonacci"]) open fun fibonacci(@RequestParam n: Int): Int? { // TODO - If n is greater than 46 then return BAD REQUEST use HttpStatus + if (n>46 || n<0) throw ResponseStatusException(HttpStatus.BAD_REQUEST); return fibonacciService?.fibonacci(n) } } \ No newline at end of file diff --git a/src/main/kotlin/hu/obuda/devops/fibonaccirestapi/service/FibonacciService.kt b/src/main/kotlin/hu/obuda/devops/fibonaccirestapi/service/FibonacciService.kt index 137941a..85f9625 100644 --- a/src/main/kotlin/hu/obuda/devops/fibonaccirestapi/service/FibonacciService.kt +++ b/src/main/kotlin/hu/obuda/devops/fibonaccirestapi/service/FibonacciService.kt @@ -5,7 +5,7 @@ import org.springframework.stereotype.Service @Service class FibonacciService { fun fibonacci(n: Int): Int { - return if (n == 1) 0 - else 0 // TODO instead of this logic implement fibonacci + if(n <=1 ) return n; + return fibonacci(n-1)+ fibonacci(n-2); } } \ No newline at end of file diff --git a/src/test/kotlin/hu/obuda/devops/fibonaccirestapi/integration/IntegrationTest.kt b/src/test/kotlin/hu/obuda/devops/fibonaccirestapi/integration/IntegrationTest.kt index f531ec4..ba5c67e 100644 --- a/src/test/kotlin/hu/obuda/devops/fibonaccirestapi/integration/IntegrationTest.kt +++ b/src/test/kotlin/hu/obuda/devops/fibonaccirestapi/integration/IntegrationTest.kt @@ -6,6 +6,7 @@ import org.springframework.boot.test.context.SpringBootTest import org.springframework.http.HttpStatus import org.springframework.web.client.RestClientException import org.springframework.web.client.RestTemplate +import org.springframework.web.server.ResponseStatusException @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) class IntegrationTest { @@ -24,7 +25,7 @@ class IntegrationTest { // then Assertions.assertEquals(HttpStatus.OK, entity.statusCode) - Assertions.assertEquals("0", entity.body) + Assertions.assertEquals("1", entity.body) } @Test @@ -46,4 +47,39 @@ class IntegrationTest { Assertions.assertNotNull(thrown) } + @Test fun callFibonacciEndpointWithInput0() + { + // when + val entity = restTemplate.getForEntity( + "http://localhost:8080/fibonacci?n=0", + String::class.java + ) + // then + Assertions.assertEquals(HttpStatus.OK, entity.getStatusCode()); + Assertions.assertEquals("0", entity.getBody()); + } + @Test fun callFibonacciEndpointWithInput10() + { + // when + val entity = restTemplate.getForEntity( + "http://localhost:8080/fibonacci?n=6", + String::class.java + ) + // then + Assertions.assertEquals(HttpStatus.OK, entity.getStatusCode()); + Assertions.assertEquals("8", entity.getBody()); + } + + @Test fun callFibonacciEndpointWithNull() + { + // when + val entity = restTemplate.getForEntity( + "http://localhost:8080/fibonacci?n=0", + String::class.java + ) + // then + Assertions.assertEquals(HttpStatus.OK, entity.getStatusCode()); + Assertions.assertEquals("0", entity.getBody()); + } + } \ No newline at end of file diff --git a/src/test/kotlin/hu/obuda/devops/fibonaccirestapi/service/FibonacciServiceTest.kt b/src/test/kotlin/hu/obuda/devops/fibonaccirestapi/service/FibonacciServiceTest.kt index 1266580..e8afc93 100644 --- a/src/test/kotlin/hu/obuda/devops/fibonaccirestapi/service/FibonacciServiceTest.kt +++ b/src/test/kotlin/hu/obuda/devops/fibonaccirestapi/service/FibonacciServiceTest.kt @@ -15,8 +15,23 @@ class FibonacciServiceTest { // when val result: Int = underTest.fibonacci(1) // then - Assertions.assertEquals(0, result) + Assertions.assertEquals(1, result) + } + fun shouldReturn0WhenCall0() { + // given + + // when + val result: Int = underTest.fibonacci(1) + // then + Assertions.assertEquals(1, result) } // TODO - Test with greater numbers and test edge cases + @Test + fun inCase3() { + val result: Int = underTest.fibonacci(3); + + Assertions.assertEquals(4, result); + } + } \ No newline at end of file