@@ -14,6 +14,8 @@ import org.learning.by.example.reactive.kotlin.microservices.KotlinReactiveMS.mo
14
14
import org.learning.by.example.reactive.kotlin.microservices.KotlinReactiveMS.test.*
15
15
import org.learning.by.example.reactive.kotlin.microservices.KotlinReactiveMS.test.tags.UnitTest
16
16
import org.springframework.boot.test.mock.mockito.SpyBean
17
+ import org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR
18
+ import org.springframework.http.HttpStatus.OK
17
19
import reactor.core.publisher.Mono
18
20
import reactor.core.publisher.toMono
19
21
@@ -33,127 +35,146 @@ private class GeoLocationServiceImplTest {
33
35
const val JSON_NOT_FOUND = " /json/GeoLocationResponse_NOT_FOUND.json"
34
36
const val JSON_EMPTY = " /json/GeoLocationResponse_EMPTY.json"
35
37
const val JSON_WRONG_STATUS = " /json/GeoLocationResponse_WRONG_STATUS.json"
36
- val LOCATION_OK = getMonoFromJsonPath(JSON_OK , GeoLocationResponse ::class )
37
- val LOCATION_NOT_FOUND = getMonoFromJsonPath(JSON_NOT_FOUND , GeoLocationResponse ::class )
38
- val LOCATION_EMPTY = getMonoFromJsonPath(JSON_EMPTY , GeoLocationResponse ::class )
39
- val LOCATION_WRONG_STATUS = getMonoFromJsonPath(JSON_WRONG_STATUS , GeoLocationResponse ::class )
38
+ val LOCATION_OK = getEntityFromJsonPath(JSON_OK , GeoLocationResponse ::class )
39
+ val LOCATION_NOT_FOUND = getEntityFromJsonPath(JSON_NOT_FOUND , GeoLocationResponse ::class )
40
+ val LOCATION_EMPTY = getEntityFromJsonPath(JSON_EMPTY , GeoLocationResponse ::class )
41
+ val LOCATION_ERROR = getEntityFromJsonPath(JSON_EMPTY , GeoLocationResponse ::class , INTERNAL_SERVER_ERROR )
42
+ val LOCATION_WRONG_STATUS = getEntityFromJsonPath(JSON_WRONG_STATUS , GeoLocationResponse ::class )
40
43
val LOCATION_EXCEPTION : Mono <GeoLocationResponse > = GetGeoLocationException (BAD_EXCEPTION ).toMono()
41
44
val BIG_EXCEPTION : Mono <GeoLocationResponse > = RuntimeException (BAD_EXCEPTION ).toMono()
42
45
}
43
46
44
47
@SpyBean(GeoLocationService ::class )
45
- lateinit var geoLocationServiceImpl : GeoLocationServiceImpl
48
+ lateinit var serviceImpl : GeoLocationServiceImpl
46
49
47
50
48
51
@Test
49
52
fun getMockingWebClientTest () {
50
- geoLocationServiceImpl .webClient = geoLocationServiceImpl .webClient `with mock response` LOCATION_OK
53
+ serviceImpl .webClient = serviceImpl .webClient mocking LOCATION_OK
51
54
52
55
val geoLocationResponse = GOOGLE_ADDRESS_MONO
53
- .transform(geoLocationServiceImpl::buildUrl)
54
- .transform(geoLocationServiceImpl::get).block()
55
- geoLocationResponse.status `should equal` OK_STATUS
56
+ .transform(serviceImpl::buildUrl)
57
+ .transform(serviceImpl::get).block()
56
58
57
- geoLocationServiceImpl.webClient reset `mock responses`
59
+ geoLocationResponse.statusCode `should equal` OK
60
+
61
+ geoLocationResponse.body.status `should equal` OK_STATUS
62
+
63
+ serviceImpl.webClient reset `mock responses`
64
+ }
65
+
66
+ @Test
67
+ fun getMockingWebClientErrorTest () {
68
+ serviceImpl.webClient = serviceImpl.webClient mocking LOCATION_ERROR
69
+
70
+ val geoLocationResponse = GOOGLE_ADDRESS_MONO
71
+ .transform(serviceImpl::buildUrl)
72
+ .transform(serviceImpl::get)
73
+ .block()
74
+
75
+ geoLocationResponse.statusCode `should be` INTERNAL_SERVER_ERROR
76
+
77
+ serviceImpl.webClient reset `mock responses`
58
78
}
59
79
60
80
@Test
61
81
fun fromAddressTest () {
62
- (geoLocationServiceImpl `will return ` LOCATION_OK ).get(any())
63
82
64
- val geographicCoordinates = geoLocationServiceImpl.fromAddress(GOOGLE_ADDRESS_MONO ).block()
83
+ (serviceImpl `will return ` LOCATION_OK ).get(any())
84
+
85
+ val geographicCoordinates = serviceImpl.fromAddress(GOOGLE_ADDRESS_MONO ).block()
65
86
geographicCoordinates `should equal` GeographicCoordinates (GOOGLE_LAT , GOOGLE_LNG )
66
87
67
- geoLocationServiceImpl reset `mock responses`
88
+ serviceImpl reset `mock responses`
68
89
}
69
90
70
91
@Test
71
92
fun fromAddressNotFoundTest () {
72
93
73
- (geoLocationServiceImpl `will return ` LOCATION_NOT_FOUND ).get(any())
94
+ (serviceImpl `will return ` LOCATION_NOT_FOUND ).get(any())
74
95
75
- val geographicCoordinates: GeographicCoordinates ? = geoLocationServiceImpl .fromAddress(GOOGLE_ADDRESS_MONO )
96
+ val geographicCoordinates: GeographicCoordinates ? = serviceImpl .fromAddress(GOOGLE_ADDRESS_MONO )
76
97
.onErrorResume {
77
98
it `should be instance of` GeoLocationNotFoundException ::class
78
99
Mono .empty()
79
100
}
80
101
.block()
81
102
geographicCoordinates `should be` null
82
103
83
- geoLocationServiceImpl reset `mock responses`
104
+ serviceImpl reset `mock responses`
84
105
}
85
106
86
107
@Test
87
108
fun fromAddressEmptyTest () {
88
- (geoLocationServiceImpl `will return ` LOCATION_EMPTY ).get(any())
109
+ (serviceImpl `will return ` LOCATION_EMPTY ).get(any())
89
110
90
- val geographicCoordinates: GeographicCoordinates ? = geoLocationServiceImpl .fromAddress(GOOGLE_ADDRESS_MONO )
111
+ val geographicCoordinates: GeographicCoordinates ? = serviceImpl .fromAddress(GOOGLE_ADDRESS_MONO )
91
112
.onErrorResume {
92
113
it `should be instance of` GetGeoLocationException ::class
93
114
Mono .empty()
94
115
}
95
116
.block()
96
117
geographicCoordinates `should be` null
97
118
98
- geoLocationServiceImpl reset `mock responses`
119
+ serviceImpl reset `mock responses`
99
120
}
100
121
101
122
102
123
@Test
103
124
fun fromAddressWrongStatusTest () {
104
- (geoLocationServiceImpl `will return ` LOCATION_WRONG_STATUS ).get(any())
125
+ (serviceImpl `will return ` LOCATION_WRONG_STATUS ).get(any())
105
126
106
- val geographicCoordinates: GeographicCoordinates ? = geoLocationServiceImpl .fromAddress(GOOGLE_ADDRESS_MONO )
127
+ val geographicCoordinates: GeographicCoordinates ? = serviceImpl .fromAddress(GOOGLE_ADDRESS_MONO )
107
128
.onErrorResume {
108
129
it `should be instance of` GetGeoLocationException ::class
109
130
Mono .empty()
110
131
}
111
132
.block()
112
133
geographicCoordinates `should be` null
113
134
114
- geoLocationServiceImpl reset `mock responses`
135
+ serviceImpl reset `mock responses`
115
136
}
116
137
117
138
@Test
118
139
fun fromAddressExceptionTest () {
119
- (geoLocationServiceImpl `will return ` LOCATION_EXCEPTION ).get(any())
140
+ (serviceImpl `will return ` LOCATION_EXCEPTION ).get(any())
120
141
121
- val geographicCoordinates: GeographicCoordinates ? = geoLocationServiceImpl .fromAddress(GOOGLE_ADDRESS_MONO )
142
+ val geographicCoordinates: GeographicCoordinates ? = serviceImpl .fromAddress(GOOGLE_ADDRESS_MONO )
122
143
.onErrorResume {
123
144
it `should be instance of` GetGeoLocationException ::class
124
145
Mono .empty()
125
146
}
126
147
.block()
127
148
geographicCoordinates `should be` null
128
149
129
- geoLocationServiceImpl reset `mock responses`
150
+ serviceImpl reset `mock responses`
130
151
}
131
152
132
153
@Test
133
154
fun fromAddressBigExceptionTest () {
134
- (geoLocationServiceImpl `will return ` BIG_EXCEPTION ).get(any())
155
+ (serviceImpl `will return ` BIG_EXCEPTION ).get(any())
135
156
136
- val geographicCoordinates: GeographicCoordinates ? = geoLocationServiceImpl .fromAddress(GOOGLE_ADDRESS_MONO )
157
+ val geographicCoordinates: GeographicCoordinates ? = serviceImpl .fromAddress(GOOGLE_ADDRESS_MONO )
137
158
.onErrorResume {
138
159
it `should be instance of` GetGeoLocationException ::class
139
160
Mono .empty()
140
161
}
141
162
.block()
142
163
geographicCoordinates `should be` null
143
164
144
- geoLocationServiceImpl reset `mock responses`
165
+ serviceImpl reset `mock responses`
145
166
}
146
167
147
168
148
169
@Test
149
170
fun buildUrlTest () {
150
- val url = GOOGLE_ADDRESS_MONO .transform(geoLocationServiceImpl ::buildUrl).block()
151
- url `should equal` geoLocationServiceImpl .endPoint + GOOGLE_ADDRESS_IN_PARAMS
171
+ val url = GOOGLE_ADDRESS_MONO .transform(serviceImpl ::buildUrl).block()
172
+ url `should equal` serviceImpl .endPoint + GOOGLE_ADDRESS_IN_PARAMS
152
173
}
153
174
154
175
@Test
155
176
fun buildUrlEmptyAddressTest () {
156
- val url = " " .toMono().transform(geoLocationServiceImpl ::buildUrl).onErrorResume {
177
+ val url = " " .toMono().transform(serviceImpl ::buildUrl).onErrorResume {
157
178
it `should be instance of` InvalidParametersException ::class
158
179
Mono .empty()
159
180
}.block()
0 commit comments