Skip to content

Commit 83c83d2

Browse files
committed
adding main router
1 parent 219dca9 commit 83c83d2

File tree

3 files changed

+85
-4
lines changed

3 files changed

+85
-4
lines changed

src/main/kotlin/org/learning/by/example/reactive/kotlin/microservices/KotlinReactiveMS/application/ApplicationConfig.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package org.learning.by.example.reactive.kotlin.microservices.KotlinReactiveMS.a
33
import org.learning.by.example.reactive.kotlin.microservices.KotlinReactiveMS.handlers.ApiHandler
44
import org.learning.by.example.reactive.kotlin.microservices.KotlinReactiveMS.handlers.ErrorHandler
55
import org.learning.by.example.reactive.kotlin.microservices.KotlinReactiveMS.routers.ApiRouter
6+
import org.learning.by.example.reactive.kotlin.microservices.KotlinReactiveMS.routers.MainRouter
67
import org.learning.by.example.reactive.kotlin.microservices.KotlinReactiveMS.routers.StaticRouter
78
import org.learning.by.example.reactive.kotlin.microservices.KotlinReactiveMS.services.GeoLocationService
89
import org.learning.by.example.reactive.kotlin.microservices.KotlinReactiveMS.services.GeoLocationServiceImpl
@@ -25,16 +26,16 @@ internal class ApplicationConfig {
2526
internal fun errorHandler() = ErrorHandler()
2627

2728
@Bean
28-
internal fun staticRouter() = StaticRouter()
29+
internal fun apiRouter(apiHandler: ApiHandler, errorHandler: ErrorHandler) = ApiRouter(apiHandler, errorHandler)
2930

3031
@Bean
31-
internal fun staticRouterFunction(staticRouter: StaticRouter) = staticRouter.doRoute()
32+
internal fun staticRouter() = StaticRouter()
3233

3334
@Bean
34-
internal fun apiRouter(apiHandler: ApiHandler, errorHandler: ErrorHandler) = ApiRouter(apiHandler, errorHandler)
35+
internal fun mainRouter(apiRouter: ApiRouter, staticRouter: StaticRouter) = MainRouter(apiRouter, staticRouter)
3536

3637
@Bean
37-
internal fun apiRouterFunction(apiRouter: ApiRouter) = apiRouter.doRoute()
38+
internal fun mainRouterFunction(mainRouter: MainRouter) = mainRouter.doRoute()
3839

3940
@Bean
4041
internal fun geoLocationService(@Value("\${GeoLocationServiceImpl.endPoint}") endPoint: String): GeoLocationService
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.learning.by.example.reactive.kotlin.microservices.KotlinReactiveMS.routers
2+
3+
import org.springframework.web.reactive.function.server.router
4+
5+
internal class MainRouter(val apiRouter: ApiRouter, val staticRouter: StaticRouter) {
6+
7+
fun doRoute() = router {
8+
routes.addAll(arrayListOf(apiRouter.doRoute(), staticRouter.doRoute()))
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package org.learning.by.example.reactive.kotlin.microservices.KotlinReactiveMS.routers
2+
3+
import com.natpryce.hamkrest.assertion.assert
4+
import com.natpryce.hamkrest.isEmptyString
5+
import com.nhaarman.mockito_kotlin.any
6+
import com.nhaarman.mockito_kotlin.doReturn
7+
import com.nhaarman.mockito_kotlin.reset
8+
import com.nhaarman.mockito_kotlin.whenever
9+
import org.junit.jupiter.api.BeforeEach
10+
import org.junit.jupiter.api.DisplayName
11+
import org.junit.jupiter.api.Test
12+
import org.learning.by.example.reactive.kotlin.microservices.KotlinReactiveMS.extensions.toMono
13+
import org.learning.by.example.reactive.kotlin.microservices.KotlinReactiveMS.model.GeographicCoordinates
14+
import org.learning.by.example.reactive.kotlin.microservices.KotlinReactiveMS.model.LocationResponse
15+
import org.learning.by.example.reactive.kotlin.microservices.KotlinReactiveMS.model.SunriseSunset
16+
import org.learning.by.example.reactive.kotlin.microservices.KotlinReactiveMS.services.GeoLocationService
17+
import org.learning.by.example.reactive.kotlin.microservices.KotlinReactiveMS.services.SunriseSunsetService
18+
import org.learning.by.example.reactive.kotlin.microservices.KotlinReactiveMS.test.BasicIntegrationTest
19+
import org.learning.by.example.reactive.kotlin.microservices.KotlinReactiveMS.test.isNull
20+
import org.learning.by.example.reactive.kotlin.microservices.KotlinReactiveMS.test.tags.IntegrationTest
21+
import org.springframework.beans.factory.annotation.Autowired
22+
import org.springframework.boot.test.mock.mockito.SpyBean
23+
24+
@IntegrationTest
25+
@DisplayName("MainRouter Integration Tests")
26+
internal class MainRouterTest : BasicIntegrationTest() {
27+
28+
companion object {
29+
const val GOOGLE_ADDRESS = "1600 Amphitheatre Parkway, Mountain View, CA"
30+
const val API_LOCATION = "/api/location"
31+
const val SUNRISE_TIME = "2017-05-21T12:53:56+00:00"
32+
const val SUNSET_TIME = "2017-05-22T03:16:05+00:00"
33+
const val GOOGLE_LAT = 37.4224082
34+
const val GOOGLE_LNG = -122.0856086
35+
private val GOOGLE_LOCATION = GeographicCoordinates(GOOGLE_LAT, GOOGLE_LNG).toMono()
36+
private val SUNRISE_SUNSET = SunriseSunset(SUNRISE_TIME, SUNSET_TIME).toMono()
37+
private val STATIC_PATH = "/index.html"
38+
}
39+
40+
@Autowired
41+
lateinit var mainRouter : MainRouter
42+
43+
@SpyBean
44+
lateinit private var geoLocationService: GeoLocationService
45+
46+
@SpyBean
47+
lateinit private var sunriseSunsetService: SunriseSunsetService
48+
49+
@BeforeEach
50+
fun setup() = bindToRouterFunction(mainRouter.doRoute())
51+
52+
@Test
53+
fun staticRouterTest() {
54+
val html : String = get(STATIC_PATH)
55+
assert.that(html, !isEmptyString)
56+
}
57+
58+
@Test
59+
fun apiRouterTest() {
60+
doReturn(GOOGLE_LOCATION).whenever(geoLocationService).fromAddress(any())
61+
doReturn(SUNRISE_SUNSET).whenever(sunriseSunsetService).fromGeographicCoordinates(any())
62+
63+
val locationResponse : LocationResponse = get(url = "${API_LOCATION}/${GOOGLE_ADDRESS}")
64+
assert.that(locationResponse.geographicCoordinates, !isNull())
65+
66+
reset(geoLocationService)
67+
reset(sunriseSunsetService)
68+
}
69+
70+
}

0 commit comments

Comments
 (0)