@@ -19,6 +19,16 @@ import org.junit.Assert.assertEquals
1919import org.junit.Before
2020import org.junit.Test
2121
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+
2232class ConnectionDetailsRepositoryTest {
2333 private lateinit var classUnderTest: ConnectionDetailsRepository
2434
@@ -54,13 +64,9 @@ class ConnectionDetailsRepositoryTest {
5464 fun `Given unset connection when connectionDetails then returns unset state` () = runBlocking {
5565 // Given
5666 val givenState = ConnectionStateDataModel .Unset
57- every { connectionDataSource.observeIsConnected() } returns flow {
58- emit(givenState)
59- }
67+ givenConnectionState(givenState)
6068 val expectedState = ConnectionDetailsDomainModel .Unset
61- every {
62- connectionDetailsDomainResolver.toDomain(eq(givenState), any(), any())
63- } returns expectedState
69+ givenConnectionDetailsDomainResolverMaps(givenState, expectedState)
6470
6571 // When
6672 val actualValue = classUnderTest.connectionDetails().toList()
@@ -74,13 +80,9 @@ class ConnectionDetailsRepositoryTest {
7480 runBlocking {
7581 // Given
7682 val givenState = ConnectionStateDataModel .Disconnected
77- every { connectionDataSource.observeIsConnected() } returns flow {
78- emit(givenState)
79- }
83+ givenConnectionState(givenState)
8084 val expectedState = ConnectionDetailsDomainModel .Disconnected
81- every {
82- connectionDetailsDomainResolver.toDomain(eq(givenState), any(), any())
83- } returns expectedState
85+ givenConnectionDetailsDomainResolverMaps(givenState, expectedState)
8486
8587 // When
8688 val actualValue = classUnderTest.connectionDetails().toList()
@@ -93,11 +95,14 @@ class ConnectionDetailsRepositoryTest {
9395 fun `Given connected when connectionDetails then returns connected state` () = runBlocking {
9496 // Given
9597 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
99104 val expectedState = ConnectionDetailsDomainModel .Connected (
100- ipAddress = " 1.2.3.4 " ,
105+ ipAddress = givenIpAddress ,
101106 city = " Paris" ,
102107 region = " Paris" ,
103108 countryCode = " France" ,
@@ -107,20 +112,7 @@ class ConnectionDetailsRepositoryTest {
107112 timeZone = " GMT +1"
108113 )
109114
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)
124116
125117 // When
126118 val actualValue = classUnderTest.connectionDetails().toList()
@@ -134,15 +126,16 @@ class ConnectionDetailsRepositoryTest {
134126 runBlocking {
135127 // Given
136128 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)
140132 val throwable = Throwable ()
133+ every {
134+ ipAddressInformationDataSource.ipAddressInformation(givenIpAddress)
135+ } throws throwable andThen defaultIpAddressInformation
141136
142137 val expectedState2 = ConnectionDetailsDomainModel .Disconnected
143- every {
144- connectionDetailsDomainResolver.toDomain(eq(givenState), any(), any())
145- } throws throwable andThen expectedState2
138+ givenConnectionDetailsDomainResolverMaps(givenState, expectedState2)
146139
147140 val expectedDomainException = UnknownDomainException (throwable)
148141 every { throwableDomainMapper.toDomain(throwable) } returns expectedDomainException
@@ -155,42 +148,22 @@ class ConnectionDetailsRepositoryTest {
155148 assertEquals(listOf (expectedState1, expectedState2), actualValue)
156149 }
157150
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+ }
170155 }
171156
172- private fun testConnectionDetailsReadCorrectly (
173- ipAddress : String ,
174- ipAddressInformationProvider : ( String ) -> IpAddressInformationDataModel
157+ private fun givenConnectionDetailsDomainResolverMaps (
158+ givenState : ConnectionStateDataModel ,
159+ expectedState : ConnectionDetailsDomainModel
175160 ) {
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- )
186161 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+ }
192165
193- // Then
194- assertEquals(expectedIpAddressInformation, actualIpAddressInformation)
166+ private fun givenIpAddress ( givenIpAddress : String ) {
167+ every { ipAddressDataSource.ipAddress() } returns givenIpAddress
195168 }
196169}
0 commit comments