Skip to content

Commit f89d5d0

Browse files
committed
clean up
1 parent 24676cc commit f89d5d0

File tree

7 files changed

+65
-41
lines changed

7 files changed

+65
-41
lines changed

src/main/kotlin/org/learning/by/example/reactive/kotlin/microservices/KotlinReactiveMS/handlers/ApiHandler.kt

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import org.learning.by.example.reactive.kotlin.microservices.KotlinReactiveMS.ex
44
import org.learning.by.example.reactive.kotlin.microservices.KotlinReactiveMS.extensions.toMono
55
import org.learning.by.example.reactive.kotlin.microservices.KotlinReactiveMS.extensions.withBody
66
import org.learning.by.example.reactive.kotlin.microservices.KotlinReactiveMS.model.GeographicCoordinates
7-
import org.learning.by.example.reactive.kotlin.microservices.KotlinReactiveMS.model.HelloResponse
87
import org.learning.by.example.reactive.kotlin.microservices.KotlinReactiveMS.model.LocationRequest
98
import org.learning.by.example.reactive.kotlin.microservices.KotlinReactiveMS.model.LocationResponse
109
import org.learning.by.example.reactive.kotlin.microservices.KotlinReactiveMS.services.GeoLocationService
@@ -19,8 +18,6 @@ internal class ApiHandler(val geoLocationService: GeoLocationService, val sunris
1918
const val ADDRESS = "address"
2019
}
2120

22-
internal fun getHello(req: ServerRequest) = ok() withBody HelloResponse("world")
23-
2421
internal fun getLocation(request: ServerRequest) =
2522
request.pathVariable(ADDRESS).toMono()
2623
.transform(this::buildResponse)
@@ -32,12 +29,12 @@ internal class ApiHandler(val geoLocationService: GeoLocationService, val sunris
3229
.transform(this::buildResponse)
3330
.transform(this::serverResponse)!!
3431

35-
internal fun buildResponse(address: Mono<String>)
36-
= address.transform(geoLocationService::fromAddress).and(this::sunriseSunset, ::LocationResponse)
32+
internal fun buildResponse(address: Mono<String>) =
33+
address.transform(geoLocationService::fromAddress).and(this::sunriseSunset, ::LocationResponse)
3734

38-
internal fun sunriseSunset(geographicCoordinates: GeographicCoordinates)
39-
= geographicCoordinates.toMono().transform(sunriseSunsetService::fromGeographicCoordinates)
35+
internal fun sunriseSunset(geographicCoordinates: GeographicCoordinates) =
36+
geographicCoordinates.toMono().transform(sunriseSunsetService::fromGeographicCoordinates)
4037

41-
internal fun serverResponse(address: Mono<LocationResponse>): Mono<ServerResponse>
42-
= address.flatMap { ok() withBody it }
38+
internal fun serverResponse(address: Mono<LocationResponse>): Mono<ServerResponse> =
39+
address.flatMap { ok() withBody it }
4340
}

src/main/kotlin/org/learning/by/example/reactive/kotlin/microservices/KotlinReactiveMS/model/HelloResponse.kt

Lines changed: 0 additions & 4 deletions
This file was deleted.

src/main/kotlin/org/learning/by/example/reactive/kotlin/microservices/KotlinReactiveMS/routers/ApiRouter.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,19 @@ import org.springframework.http.MediaType
55
import org.springframework.web.reactive.function.server.router
66

77

8-
internal class ApiRouter(val handler : ApiHandler) {
8+
internal class ApiRouter(val handler: ApiHandler) {
99

1010
private companion object {
1111
const private val API_PATH = "/api"
12-
const private val HELLO_PATH = "/hello"
12+
const val LOCATION_PATH = "/location"
13+
const val ADDRESS_ARG = "/{address}"
14+
const val LOCATION_WITH_ADDRESS_PATH = "$LOCATION_PATH$ADDRESS_ARG"
1315
}
1416

1517
fun doRoute() = router {
1618
(accept(MediaType.APPLICATION_JSON_UTF8) and API_PATH).nest {
17-
GET(HELLO_PATH, handler::getHello)
19+
GET(LOCATION_WITH_ADDRESS_PATH, handler::getLocation)
20+
POST(LOCATION_PATH, handler::postLocation)
1821
}
1922
}
2023
}
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package org.learning.by.example.reactive.kotlin.microservices.KotlinReactiveMS.application
22

33
import com.natpryce.hamkrest.assertion.assert
4-
import com.natpryce.hamkrest.equalTo
54
import org.junit.jupiter.api.BeforeEach
65
import org.junit.jupiter.api.DisplayName
76
import org.junit.jupiter.api.Test
8-
import org.learning.by.example.reactive.kotlin.microservices.KotlinReactiveMS.model.HelloResponse
7+
import org.learning.by.example.reactive.kotlin.microservices.KotlinReactiveMS.model.LocationRequest
8+
import org.learning.by.example.reactive.kotlin.microservices.KotlinReactiveMS.model.LocationResponse
99
import org.learning.by.example.reactive.kotlin.microservices.KotlinReactiveMS.test.BasicIntegrationTest
10+
import org.learning.by.example.reactive.kotlin.microservices.KotlinReactiveMS.test.isNull
1011
import org.learning.by.example.reactive.kotlin.microservices.KotlinReactiveMS.test.tags.SystemTest
1112
import org.springframework.boot.web.server.LocalServerPort
1213

@@ -15,15 +16,28 @@ import org.springframework.boot.web.server.LocalServerPort
1516
@DisplayName("KotlinReactiveMsApplication System Tests")
1617
private class KotlinReactiveMsApplicationTests : BasicIntegrationTest() {
1718

19+
companion object {
20+
const val GOOGLE_ADDRESS = "1600 Amphitheatre Parkway, Mountain View, CA"
21+
const val API_LOCATION = "/api/location"
22+
}
23+
1824
@LocalServerPort
1925
var port: Int = 0
2026

2127
@BeforeEach
2228
fun setup() = bindToPort(port)
2329

2430
@Test
25-
fun apiGet() {
26-
val helloResponse = get(url = "/api/hello", type = HelloResponse::class)
27-
assert.that(helloResponse.hello, equalTo("world"))
31+
fun getLocation() {
32+
val locationResponse = get(url = "${API_LOCATION}/${GOOGLE_ADDRESS}", type = LocationResponse::class)
33+
assert.that(locationResponse.geographicCoordinates, !isNull())
34+
}
35+
36+
@Test
37+
fun postLocation() {
38+
val locationResponse = post(url = API_LOCATION, value = LocationRequest(GOOGLE_ADDRESS),
39+
type = LocationResponse::class)
40+
41+
assert.that(locationResponse.geographicCoordinates, !isNull())
2842
}
2943
}

src/test/kotlin/org/learning/by/example/reactive/kotlin/microservices/KotlinReactiveMS/handlers/ApiHandlerTests.kt

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ import com.nhaarman.mockito_kotlin.*
66
import org.junit.jupiter.api.DisplayName
77
import org.junit.jupiter.api.Test
88
import org.learning.by.example.reactive.kotlin.microservices.KotlinReactiveMS.extensions.toMono
9-
import org.learning.by.example.reactive.kotlin.microservices.KotlinReactiveMS.model.*
9+
import org.learning.by.example.reactive.kotlin.microservices.KotlinReactiveMS.model.GeographicCoordinates
10+
import org.learning.by.example.reactive.kotlin.microservices.KotlinReactiveMS.model.LocationRequest
11+
import org.learning.by.example.reactive.kotlin.microservices.KotlinReactiveMS.model.LocationResponse
12+
import org.learning.by.example.reactive.kotlin.microservices.KotlinReactiveMS.model.SunriseSunset
1013
import org.learning.by.example.reactive.kotlin.microservices.KotlinReactiveMS.services.GeoLocationService
1114
import org.learning.by.example.reactive.kotlin.microservices.KotlinReactiveMS.services.SunriseSunsetService
1215
import org.learning.by.example.reactive.kotlin.microservices.KotlinReactiveMS.test.BasicIntegrationTest
@@ -47,20 +50,6 @@ private class ApiHandlerTests : BasicIntegrationTest() {
4750
@SpyBean
4851
lateinit private var sunriseSunsetService: SunriseSunsetService
4952

50-
@Test
51-
fun apiHandlerTest() {
52-
53-
val serverRequest = mock<ServerRequest>()
54-
val serverResponseMono = apiHandler.getHello(serverRequest)
55-
56-
serverResponseMono.subscribe {
57-
assert.that(it.statusCode(), equalTo(HttpStatus.OK))
58-
59-
val helloResponse: HelloResponse = it.extractEntity()
60-
assert.that(helloResponse.hello, equalTo("world"))
61-
}
62-
}
63-
6453
@Test
6554
fun combineTest() {
6655
GOOGLE_LOCATION_MONO.and(SUNRISE_SUNSET, ::LocationResponse)
Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package org.learning.by.example.reactive.kotlin.microservices.KotlinReactiveMS.router
22

33
import com.natpryce.hamkrest.assertion.assert
4-
import com.natpryce.hamkrest.equalTo
54
import org.junit.jupiter.api.BeforeEach
65
import org.junit.jupiter.api.DisplayName
76
import org.junit.jupiter.api.Test
8-
import org.learning.by.example.reactive.kotlin.microservices.KotlinReactiveMS.model.HelloResponse
7+
import org.learning.by.example.reactive.kotlin.microservices.KotlinReactiveMS.model.LocationRequest
8+
import org.learning.by.example.reactive.kotlin.microservices.KotlinReactiveMS.model.LocationResponse
99
import org.learning.by.example.reactive.kotlin.microservices.KotlinReactiveMS.routers.ApiRouter
1010
import org.learning.by.example.reactive.kotlin.microservices.KotlinReactiveMS.test.BasicIntegrationTest
11+
import org.learning.by.example.reactive.kotlin.microservices.KotlinReactiveMS.test.isNull
1112
import org.learning.by.example.reactive.kotlin.microservices.KotlinReactiveMS.test.tags.IntegrationTest
1213
import org.springframework.beans.factory.annotation.Autowired
1314

@@ -18,12 +19,25 @@ private class ApiRouterTest : BasicIntegrationTest() {
1819
@Autowired
1920
lateinit var apiRouter: ApiRouter
2021

22+
companion object {
23+
const val GOOGLE_ADDRESS = "1600 Amphitheatre Parkway, Mountain View, CA"
24+
const val API_LOCATION = "/api/location"
25+
}
26+
2127
@BeforeEach
2228
fun setup() = bindToRouterFunction(apiRouter.doRoute())
2329

2430
@Test
25-
fun getHello() {
26-
val helloResponse = get(url = "/api/hello", type = HelloResponse::class)
27-
assert.that(helloResponse.hello, equalTo("world"))
31+
fun getLocation() {
32+
val locationResponse = get(url = "$API_LOCATION/$GOOGLE_ADDRESS", type = LocationResponse::class)
33+
assert.that(locationResponse.geographicCoordinates, !isNull())
34+
}
35+
36+
@Test
37+
fun postLocation() {
38+
val locationResponse = post(url = API_LOCATION, value = LocationRequest(GOOGLE_ADDRESS),
39+
type = LocationResponse::class)
40+
41+
assert.that(locationResponse.geographicCoordinates, !isNull())
2842
}
2943
}

src/test/kotlin/org/learning/by/example/reactive/kotlin/microservices/KotlinReactiveMS/test/BasicIntegrationTest.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package org.learning.by.example.reactive.kotlin.microservices.KotlinReactiveMS.t
33
import org.springframework.http.HttpStatus
44
import org.springframework.http.MediaType
55
import org.springframework.test.web.reactive.server.WebTestClient
6+
import org.springframework.web.reactive.function.BodyInserters
67
import org.springframework.web.reactive.function.server.RouterFunction
78
import kotlin.reflect.KClass
89

@@ -26,4 +27,14 @@ internal abstract class BasicIntegrationTest {
2627
?.expectStatus()?.isEqualTo(httpStatus)
2728
?.expectBody(type.java)
2829
?.returnResult()?.responseBody!!
30+
31+
fun <T : Any> post(url: String, value: Any = Unit ,httpStatus: HttpStatus = HttpStatus.OK, type: KClass<T>) =
32+
webTestClient.post()
33+
?.uri(url)
34+
?.body(BodyInserters.fromObject(value))
35+
?.accept(MediaType.APPLICATION_JSON_UTF8)
36+
?.exchange()
37+
?.expectStatus()?.isEqualTo(httpStatus)
38+
?.expectBody(type.java)
39+
?.returnResult()?.responseBody!!
2940
}

0 commit comments

Comments
 (0)