Skip to content

Commit d73af94

Browse files
author
Enes Açıkoğlu
committed
test data and controller tests added
1 parent 4241008 commit d73af94

File tree

7 files changed

+109
-9
lines changed

7 files changed

+109
-9
lines changed

src/main/kotlin/com/cengenes/kotlin/api/KotlinSpringBootApiApplication.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import org.springframework.boot.autoconfigure.SpringBootApplication
44
import org.springframework.boot.runApplication
55

66
@SpringBootApplication
7-
class KotlinSpringBootApiApplication {
7+
class KotlinSpringBootApiApplication
88

9+
object KotlinMain {
10+
@JvmStatic
911
fun main(args: Array<String>) {
1012
runApplication<KotlinSpringBootApiApplication>(*args)
1113
}
12-
}
14+
}

src/main/kotlin/com/cengenes/kotlin/api/controller/HotelController.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,20 @@ import com.cengenes.kotlin.api.entity.Hotel
44
import com.cengenes.kotlin.api.model.request.CheckInRequest
55
import com.cengenes.kotlin.api.service.HotelServiceManager
66
import org.springframework.web.bind.annotation.*
7+
import javax.validation.Valid
78

89
@RestController
910
@RequestMapping("/hotels")
1011
class HotelController(var hotelServiceManager: HotelServiceManager) {
1112

1213
@GetMapping
13-
fun getAllHotels(): MutableIterable<Hotel> = this.hotelServiceManager.findAll()
14+
fun getAllHotels(): MutableList<Hotel> = this.hotelServiceManager.findAll()
1415

1516
@GetMapping("/{name}")
1617
fun findByHotelName(@PathVariable(value = "name") name: String): Hotel = this.hotelServiceManager.findByName(name)
1718

1819
@PostMapping("checkIn")
19-
fun checkIn(@RequestBody checkInRequest: CheckInRequest): Boolean {
20+
fun checkIn(@Valid @RequestBody checkInRequest: CheckInRequest): Boolean {
2021
return hotelServiceManager.checkIn(checkInRequest)
2122
}
2223
}

src/main/kotlin/com/cengenes/kotlin/api/model/request/CheckInRequest.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package com.cengenes.kotlin.api.model.request
22

33
import java.io.Serializable
4+
import javax.validation.constraints.NotNull
45

5-
data class CheckInRequest(var hotelName: String, var numberOfGuest: Long) : Serializable {
6+
data class CheckInRequest(@NotNull var hotelName: String, @NotNull var numberOfGuest: Long) : Serializable {
67

78
override fun toString(): String {
89
return "CheckInRequest(hotelName='$hotelName', numberOfGuest=$numberOfGuest)"

src/main/resources/db/changelog/version/1.0.0.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,10 @@
2424
</column>
2525
</createTable>
2626
</changeSet>
27+
<changeSet id="17-06-2018 10:50" author="enes.acikoglu">
28+
<sql>
29+
INSERT INTO hotel (id, hotel_name,classification,total_room_count, free_room_count) VALUES(nextval('seq_hotel'), 'Hotel California',101, 200,200);
30+
INSERT INTO hotel (id, hotel_name,classification,total_room_count, free_room_count) VALUES(nextval('seq_hotel'), 'Ns Hilton',102, 400,400);
31+
</sql>
32+
</changeSet>
2733
</databaseChangeLog>

src/test/kotlin/com/cengenes/kotlin/api/KotlinSpringBootApiApplicationIT.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ import org.springframework.test.context.junit4.SpringRunner
77
@RunWith(SpringRunner::class)
88
class KotlinSpringBootApiApplicationIT {
99
@Test
10-
fun it_should_test_application_running_properly()=KotlinSpringBootApiApplication().main(arrayOf("test"))
10+
fun it_should_test_application_running_properly()=KotlinMain.main(arrayOf("test"))
1111
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package com.cengenes.kotlin.api.controller
2+
3+
import com.cengenes.kotlin.api.entity.Hotel
4+
import com.cengenes.kotlin.api.model.request.CheckInRequest
5+
import org.assertj.core.api.Assertions.assertThat
6+
import org.junit.Test
7+
import org.junit.runner.RunWith
8+
import org.springframework.beans.factory.annotation.Autowired
9+
import org.springframework.boot.test.context.SpringBootTest
10+
import org.springframework.boot.test.web.client.TestRestTemplate
11+
import org.springframework.boot.test.web.client.getForEntity
12+
import org.springframework.core.ParameterizedTypeReference
13+
import org.springframework.http.HttpEntity
14+
import org.springframework.http.HttpMethod
15+
import org.springframework.http.HttpStatus
16+
import org.springframework.http.ResponseEntity
17+
import org.springframework.test.context.junit4.SpringRunner
18+
19+
@RunWith(SpringRunner::class)
20+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
21+
class HotelControllerIT {
22+
23+
@Autowired
24+
private lateinit var restTemplate: TestRestTemplate
25+
26+
27+
@Test
28+
fun it_should_get_hotel_by_name() {
29+
//Given
30+
var hotelName = "Hotel California"
31+
32+
//When
33+
val hotelResponse: ResponseEntity<Hotel> = restTemplate.getForEntity(url = "/hotels/" + hotelName, uriVariables = Hotel::class)
34+
35+
//Then
36+
assertThat(hotelResponse).isNotNull
37+
assertThat(hotelResponse.statusCode).isEqualTo(HttpStatus.OK)
38+
assertThat(hotelResponse.body?.name).isEqualTo("Hotel California")
39+
assertThat(hotelResponse.body?.classification).isEqualTo(101L)
40+
assertThat(hotelResponse.body?.totalRoomCount).isEqualTo(200L)
41+
assertThat(hotelResponse.body?.freeRoomCount).isEqualTo(200L)
42+
}
43+
44+
@Test
45+
fun it_should_get_all_hotels() {
46+
//Given
47+
48+
//When
49+
val hotelResponse: ResponseEntity<MutableList<Hotel>> = restTemplate.exchange("/hotels", HttpMethod.GET, HttpEntity.EMPTY, object : ParameterizedTypeReference<MutableList<Hotel>>() {})
50+
51+
//Then
52+
assertThat(hotelResponse).isNotNull
53+
assertThat(hotelResponse.statusCode).isEqualTo(HttpStatus.OK)
54+
assertThat(hotelResponse.body?.get(0)?.name).isEqualTo("Hotel California")
55+
assertThat(hotelResponse.body?.get(0)?.classification).isEqualTo(101L)
56+
assertThat(hotelResponse.body?.get(0)?.totalRoomCount).isEqualTo(200L)
57+
assertThat(hotelResponse.body?.get(0)?.freeRoomCount).isEqualTo(200L)
58+
assertThat(hotelResponse.body?.get(1)?.name).isEqualTo("Ns Hilton")
59+
assertThat(hotelResponse.body?.get(1)?.classification).isEqualTo(102L)
60+
assertThat(hotelResponse.body?.get(1)?.totalRoomCount).isEqualTo(400L)
61+
assertThat(hotelResponse.body?.get(1)?.freeRoomCount).isEqualTo(400L)
62+
}
63+
64+
@Test
65+
fun it_should_checkin_succesfully_when_free_room_count_is_available() {
66+
//Given
67+
val checkInRequest = CheckInRequest("Hotel California", 199L)
68+
69+
//When
70+
val response = restTemplate.postForEntity("/hotels/checkIn", checkInRequest, Boolean::class.java)
71+
72+
//Then
73+
assertThat(response).isNotNull
74+
assertThat(response.body).isTrue()
75+
76+
}
77+
78+
@Test
79+
fun it_should_checkin_failed_when_free_room_count_is_not_available() {
80+
//Given
81+
val checkInRequest = CheckInRequest("Hotel California", 201L)
82+
83+
//When
84+
val response = restTemplate.postForEntity("/hotels/checkIn", checkInRequest, Boolean::class.java)
85+
86+
//Then
87+
assertThat(response).isNotNull
88+
assertThat(response.body).isFalse()
89+
}
90+
}

src/test/kotlin/com/cengenes/kotlin/api/repository/HotelRepositoryTest.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,17 @@ class HotelRepositoryTest {
2222
@Test
2323
fun it_should_find_hotel_by_name() {
2424
//Given
25-
val hotel = Hotel("Hotel California",99L,250L)
25+
val hotel = Hotel("Sultanahmet Inn Hotel",99L,250L)
2626
testEntityManager.persistAndFlush(hotel)
2727

2828
//When
29-
val optionalHotel = hotelRepository.findByName("Hotel California")
29+
val optionalHotel = hotelRepository.findByName("Sultanahmet Inn Hotel")
3030

3131
//Then
3232
assertThat(optionalHotel).isPresent
3333
val expectedHotel = optionalHotel.get()
3434
assertThat(expectedHotel.id).isNotNull()
35-
assertThat(expectedHotel.name).isEqualTo("Hotel California")
35+
assertThat(expectedHotel.name).isEqualTo("Sultanahmet Inn Hotel")
3636
assertThat(expectedHotel.classification).isEqualTo(99L)
3737
assertThat(expectedHotel.totalRoomCount).isEqualTo(250L)
3838
assertThat(expectedHotel.freeRoomCount).isEqualTo(250L)

0 commit comments

Comments
 (0)