@@ -19,6 +19,16 @@ import org.junit.Assert.assertEquals
19
19
import org.junit.Before
20
20
import org.junit.Test
21
21
22
+ private val defaultIpAddressInformation = IpAddressInformationDataModel (
23
+ city = " Paris" ,
24
+ region = " Paris" ,
25
+ country = " France" ,
26
+ geolocation = " 0.0,0.0" ,
27
+ internetServiceProviderName = " Le ISP" ,
28
+ postCode = " 12345" ,
29
+ timeZone = " GMT +1"
30
+ )
31
+
22
32
class ConnectionDetailsRepositoryTest {
23
33
private lateinit var classUnderTest: ConnectionDetailsRepository
24
34
@@ -54,13 +64,9 @@ class ConnectionDetailsRepositoryTest {
54
64
fun `Given unset connection when connectionDetails then returns unset state` () = runBlocking {
55
65
// Given
56
66
val givenState = ConnectionStateDataModel .Unset
57
- every { connectionDataSource.observeIsConnected() } returns flow {
58
- emit(givenState)
59
- }
67
+ givenConnectionState(givenState)
60
68
val expectedState = ConnectionDetailsDomainModel .Unset
61
- every {
62
- connectionDetailsDomainResolver.toDomain(eq(givenState), any(), any())
63
- } returns expectedState
69
+ givenConnectionDetailsDomainResolverMaps(givenState, expectedState)
64
70
65
71
// When
66
72
val actualValue = classUnderTest.connectionDetails().toList()
@@ -74,13 +80,9 @@ class ConnectionDetailsRepositoryTest {
74
80
runBlocking {
75
81
// Given
76
82
val givenState = ConnectionStateDataModel .Disconnected
77
- every { connectionDataSource.observeIsConnected() } returns flow {
78
- emit(givenState)
79
- }
83
+ givenConnectionState(givenState)
80
84
val expectedState = ConnectionDetailsDomainModel .Disconnected
81
- every {
82
- connectionDetailsDomainResolver.toDomain(eq(givenState), any(), any())
83
- } returns expectedState
85
+ givenConnectionDetailsDomainResolverMaps(givenState, expectedState)
84
86
85
87
// When
86
88
val actualValue = classUnderTest.connectionDetails().toList()
@@ -93,11 +95,14 @@ class ConnectionDetailsRepositoryTest {
93
95
fun `Given connected when connectionDetails then returns connected state` () = runBlocking {
94
96
// Given
95
97
val givenState = ConnectionStateDataModel .Connected
96
- every { connectionDataSource.observeIsConnected() } returns flow {
97
- emit(givenState)
98
- }
98
+ givenConnectionState(givenState)
99
+ val givenIpAddress = " 1.2.3.4"
100
+ givenIpAddress(givenIpAddress)
101
+ every {
102
+ ipAddressInformationDataSource.ipAddressInformation(givenIpAddress)
103
+ } returns defaultIpAddressInformation
99
104
val expectedState = ConnectionDetailsDomainModel .Connected (
100
- ipAddress = " 1.2.3.4 " ,
105
+ ipAddress = givenIpAddress ,
101
106
city = " Paris" ,
102
107
region = " Paris" ,
103
108
countryCode = " France" ,
@@ -107,20 +112,7 @@ class ConnectionDetailsRepositoryTest {
107
112
timeZone = " GMT +1"
108
113
)
109
114
110
- val ipAddress = " 1.1.1.1"
111
-
112
- every {
113
- connectionDetailsDomainResolver.toDomain(eq(givenState), any(), any())
114
- } answers { call ->
115
- @Suppress(" UNCHECKED_CAST" )
116
- testIpAddressReadCorrectly(ipAddress, call.invocation.args[1 ] as () -> String )
117
- @Suppress(" UNCHECKED_CAST" )
118
- testConnectionDetailsReadCorrectly(
119
- ipAddress,
120
- call.invocation.args[2 ] as (String ) -> IpAddressInformationDataModel
121
- )
122
- expectedState
123
- }
115
+ givenConnectionDetailsDomainResolverMaps(givenState, expectedState)
124
116
125
117
// When
126
118
val actualValue = classUnderTest.connectionDetails().toList()
@@ -134,15 +126,16 @@ class ConnectionDetailsRepositoryTest {
134
126
runBlocking {
135
127
// Given
136
128
val givenState = ConnectionStateDataModel .Connected
137
- every { connectionDataSource.observeIsConnected() } returns flow {
138
- emit(givenState)
139
- }
129
+ givenConnectionState(givenState)
130
+ val givenIpAddress = " 1.1.1.1 "
131
+ givenIpAddress(givenIpAddress)
140
132
val throwable = Throwable ()
133
+ every {
134
+ ipAddressInformationDataSource.ipAddressInformation(givenIpAddress)
135
+ } throws throwable andThen defaultIpAddressInformation
141
136
142
137
val expectedState2 = ConnectionDetailsDomainModel .Disconnected
143
- every {
144
- connectionDetailsDomainResolver.toDomain(eq(givenState), any(), any())
145
- } throws throwable andThen expectedState2
138
+ givenConnectionDetailsDomainResolverMaps(givenState, expectedState2)
146
139
147
140
val expectedDomainException = UnknownDomainException (throwable)
148
141
every { throwableDomainMapper.toDomain(throwable) } returns expectedDomainException
@@ -155,42 +148,22 @@ class ConnectionDetailsRepositoryTest {
155
148
assertEquals(listOf (expectedState1, expectedState2), actualValue)
156
149
}
157
150
158
- private fun testIpAddressReadCorrectly (
159
- expectedIpAddress : String ,
160
- ipAddressProvider : () -> String
161
- ) {
162
- // Given
163
- every { ipAddressDataSource.ipAddress() } returns expectedIpAddress
164
-
165
- // When
166
- val actualIpAddress = ipAddressProvider()
167
-
168
- // Then
169
- assertEquals(expectedIpAddress, actualIpAddress)
151
+ private fun givenConnectionState (givenState : ConnectionStateDataModel ) {
152
+ every { connectionDataSource.observeIsConnected() } returns flow {
153
+ emit(givenState)
154
+ }
170
155
}
171
156
172
- private fun testConnectionDetailsReadCorrectly (
173
- ipAddress : String ,
174
- ipAddressInformationProvider : ( String ) -> IpAddressInformationDataModel
157
+ private fun givenConnectionDetailsDomainResolverMaps (
158
+ givenState : ConnectionStateDataModel ,
159
+ expectedState : ConnectionDetailsDomainModel
175
160
) {
176
- // Given
177
- val expectedIpAddressInformation = IpAddressInformationDataModel (
178
- city = " Paris" ,
179
- region = " Paris" ,
180
- country = " France" ,
181
- geolocation = " 0.0,0.0" ,
182
- internetServiceProviderName = " Le ISP" ,
183
- postCode = " 12345" ,
184
- timeZone = " GMT +1"
185
- )
186
161
every {
187
- ipAddressInformationDataSource.ipAddressInformation(ipAddress)
188
- } returns expectedIpAddressInformation
189
-
190
- // When
191
- val actualIpAddressInformation = ipAddressInformationProvider(ipAddress)
162
+ connectionDetailsDomainResolver.toDomain(eq(givenState), any(), any())
163
+ } returns expectedState
164
+ }
192
165
193
- // Then
194
- assertEquals(expectedIpAddressInformation, actualIpAddressInformation)
166
+ private fun givenIpAddress ( givenIpAddress : String ) {
167
+ every { ipAddressDataSource.ipAddress() } returns givenIpAddress
195
168
}
196
169
}
0 commit comments