Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ 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.RestController
import org.springframework.web.server.ResponseStatusException

@RestController
@RequestMapping("/")
Expand All @@ -16,7 +18,10 @@ 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) {
throw ResponseStatusException(HttpStatus.BAD_REQUEST, "n must be <= 46")
}

return fibonacciService?.fibonacci(n)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ 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
return when {
n < 0 -> 0
n == 0 -> 0
n == 1 -> 1
else -> fibonacci(n - 1) + fibonacci(n - 2)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,21 @@ class IntegrationTest {
Assertions.assertEquals("1", entity.body)
}

@Test
fun callFibonacciEndpoint2() {
// given

// when
val entity = restTemplate.getForEntity(
"http://localhost:8080/fibonacci?n=46",
String::class.java
)

// then
Assertions.assertEquals(HttpStatus.OK, entity.statusCode)
Assertions.assertEquals("1836311903", entity.body)
}

@Test
@Throws(Exception::class)
fun callFibonacciEndpointWithInvalid() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,22 @@ class FibonacciServiceTest {
}

// TODO - Test with greater numbers and test edge cases

@Test
fun shouldReturn0WhenCallMinus1() {
val result: Int = underTest.fibonacci(-1)
Assertions.assertEquals(0, result)
}

@Test
fun shouldReturn1WhenCall2() {
val result: Int = underTest.fibonacci(2)
Assertions.assertEquals(1, result)
}

@Test
fun shouldReturn2WhenCall3() {
val result: Int = underTest.fibonacci(3)
Assertions.assertEquals(2, result)
}
}