File tree 3 files changed +11
-9
lines changed
main/kotlin/com/rsginer/hackathoon/banks
test/kotlin/com/rsginer/hackathoon/banks/controller
3 files changed +11
-9
lines changed Original file line number Diff line number Diff line change @@ -2,22 +2,24 @@ package com.rsginer.hackathoon.banks.controller
2
2
3
3
import com.rsginer.hackathoon.banks.model.Bank
4
4
import com.rsginer.hackathoon.banks.service.BankService
5
- import org.springframework.web.bind.annotation.GetMapping
6
- import org.springframework.web.bind.annotation.PathVariable
7
- import org.springframework.web.bind.annotation.RequestMapping
8
- import org.springframework.web.bind.annotation.RestController
5
+ import org.springframework.http.HttpStatus
6
+ import org.springframework.http.ResponseEntity
7
+ import org.springframework.web.bind.annotation.*
9
8
10
9
@RestController
11
10
@RequestMapping(" api/banks" )
12
11
class BankController (
13
12
private val bankService : BankService
14
13
) {
15
14
15
+ @ExceptionHandler(NoSuchElementException ::class )
16
+ fun handleNotFound (e : NoSuchElementException ): ResponseEntity <String > =
17
+ ResponseEntity (e.message, HttpStatus .NOT_FOUND )
18
+
16
19
@GetMapping
17
20
fun getBanks (): Collection <Bank > = this .bankService.getBanks()
18
21
19
22
@GetMapping(" /{accountNumber}" )
20
- fun getBankByAccountNumber (@PathVariable accountNumber : String ): Bank {
21
- return this .bankService.getBank(accountNumber)
22
- }
23
+ fun getBankByAccountNumber (@PathVariable accountNumber : String ): Bank = this .bankService.getBank(accountNumber)
24
+
23
25
}
Original file line number Diff line number Diff line change @@ -14,5 +14,6 @@ class MockBankDataSource : BankDataSource {
14
14
)
15
15
16
16
override fun retrieveBanks (): Collection <Bank > = banks
17
- override fun retrieveBank (accountNumber : String ): Bank = banks.first { it.accountNumber == accountNumber }
17
+ override fun retrieveBank (accountNumber : String ): Bank = banks.firstOrNull() { it.accountNumber == accountNumber }
18
+ ? : throw NoSuchElementException (" Could not find a bank with accountNumber $accountNumber " )
18
19
}
Original file line number Diff line number Diff line change @@ -71,7 +71,6 @@ internal class BankControllerTest {
71
71
.andDo { print () }
72
72
.andExpect {
73
73
status { isNotFound() }
74
- content { contentType(MediaType .APPLICATION_JSON ) }
75
74
}
76
75
}
77
76
}
You can’t perform that action at this time.
0 commit comments