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,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("/")
Expand All @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -24,7 +25,7 @@ class IntegrationTest {

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

@Test
Expand All @@ -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());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}