Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 565be57

Browse files
finish PostRepository tests and implementation for Coroutines
1 parent a0b5172 commit 565be57

File tree

5 files changed

+154
-14
lines changed

5 files changed

+154
-14
lines changed

‎libraries/data/src/main/java/com/smarttoolfactory/data/repository/PostRepository.kt‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ import com.smarttoolfactory.data.model.PostEntity
99
*/
1010
interface PostRepository {
1111

12-
suspend fun getPostEntitiesFromLocal(): List<PostEntity>
13-
1412
suspend fun fetchEntitiesFromRemote(): List<PostEntity>
1513

14+
suspend fun getPostEntitiesFromLocal(): List<PostEntity>
15+
1616
suspend fun savePostEntities(postEntities: List<PostEntity>)
1717

1818
suspend fun deletePostEntities()

‎libraries/data/src/main/java/com/smarttoolfactory/data/repository/PostRepositoryImpl.kt‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import com.smarttoolfactory.data.mapper.DTOtoEntityMapper
44
import com.smarttoolfactory.data.model.PostEntity
55
import com.smarttoolfactory.data.source.LocalPostDataSourceCoroutines
66
import com.smarttoolfactory.data.source.RemotePostDataSourceCoroutines
7+
import javax.inject.Inject
78

89
/**
910
* Repository for persistence layer. Local Data source acts as Single Source of Truth
@@ -20,26 +21,27 @@ import com.smarttoolfactory.data.source.RemotePostDataSourceCoroutines
2021
* this [PostRepository] as dependency.
2122
*
2223
*/
23-
class PostRepositoryCoroutinesImpl(
24+
class PostRepositoryCoroutinesImpl @Inject constructor(
2425
private val localPostDataSource: LocalPostDataSourceCoroutines,
2526
private val remotePostDataSource: RemotePostDataSourceCoroutines,
2627
private val mapper: DTOtoEntityMapper
2728
) : PostRepository {
2829

29-
override suspend fun getPostEntitiesFromLocal(): List<PostEntity> {
30-
TODO("Not yet implemented")
30+
override suspend fun fetchEntitiesFromRemote(): List<PostEntity> {
31+
val postDTOList = remotePostDataSource.getPostDTOs()
32+
return mapper.map(postDTOList)
3133
}
3234

33-
override suspend fun fetchEntitiesFromRemote(): List<PostEntity> {
34-
TODO("Not yet implemented")
35+
override suspend fun getPostEntitiesFromLocal(): List<PostEntity> {
36+
return localPostDataSource.getPostEntities()
3537
}
3638

3739
override suspend fun savePostEntities(postEntities: List<PostEntity>) {
38-
TODO("Not yet implemented")
40+
localPostDataSource.saveEntities(postEntities)
3941
}
4042

4143
override suspend fun deletePostEntities() {
42-
TODO("Not yet implemented")
44+
localPostDataSource.deletePostEntities()
4345
}
4446
}
4547

Lines changed: 139 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,155 @@
11
package com.smarttoolfactory.data.repository
22

3+
import com.google.common.truth.Truth
4+
import com.smarttoolfactory.data.mapper.DTOtoEntityMapper
5+
import com.smarttoolfactory.data.model.PostDTO
6+
import com.smarttoolfactory.data.model.PostEntity
7+
import com.smarttoolfactory.data.source.LocalPostDataSourceCoroutines
8+
import com.smarttoolfactory.data.source.RemotePostDataSourceCoroutines
9+
import com.smarttoolfactory.test_utils.RESPONSE_JSON_PATH
10+
import com.smarttoolfactory.test_utils.util.convertFromJsonToListOf
11+
import com.smarttoolfactory.test_utils.util.getResourceAsText
12+
import io.mockk.clearMocks
13+
import io.mockk.coEvery
14+
import io.mockk.coVerify
15+
import io.mockk.coVerifyOrder
16+
import io.mockk.every
17+
import io.mockk.just
18+
import io.mockk.mockk
19+
import io.mockk.runs
20+
import io.mockk.verify
21+
import kotlinx.coroutines.test.runBlockingTest
322
import org.junit.jupiter.api.AfterEach
423
import org.junit.jupiter.api.BeforeEach
24+
import org.junit.jupiter.api.Test
525
import org.junit.jupiter.api.TestInstance
626

727
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
8-
internal class PostRepositoryCoroutinesTest {
28+
class PostRepositoryCoroutinesTest {
29+
30+
companion object {
31+
32+
val postDTOList =
33+
convertFromJsonToListOf<PostDTO>(getResourceAsText(RESPONSE_JSON_PATH))!!
34+
35+
val postEntityList =
36+
convertFromJsonToListOf<PostEntity>(getResourceAsText(RESPONSE_JSON_PATH))!!
37+
}
38+
39+
private lateinit var repository: PostRepositoryCoroutinesImpl
40+
41+
private val localPostDataSource: LocalPostDataSourceCoroutines = mockk()
42+
private val remotePostDataSource: RemotePostDataSourceCoroutines = mockk()
43+
private val mapper: DTOtoEntityMapper = mockk()
44+
45+
@Test
46+
fun `given network error occurred, should throw Exception`() = runBlockingTest {
47+
48+
// GIVEN
49+
coEvery { remotePostDataSource.getPostDTOs() } throws Exception("Network Exception")
50+
every { mapper.map(postDTOList) } returns postEntityList
51+
52+
// WHEN
53+
val expected = try {
54+
remotePostDataSource.getPostDTOs()
55+
} catch (e: Exception) {
56+
e
57+
}
58+
59+
// THEN
60+
Truth.assertThat(expected).isInstanceOf(Exception::class.java)
61+
coVerify { remotePostDataSource.getPostDTOs() }
62+
verify(exactly = 0) { mapper.map(postDTOList) }
63+
}
64+
65+
@Test
66+
fun `given PostDTO list returned from remote source, should return postEntity list`() =
67+
runBlockingTest {
68+
// GIVEN
69+
val actual = postEntityList
70+
coEvery { remotePostDataSource.getPostDTOs() } returns postDTOList
71+
every { mapper.map(postDTOList) } returns postEntityList
72+
73+
// WHEN
74+
val expected = repository.fetchEntitiesFromRemote()
75+
76+
// THEN
77+
Truth.assertThat(expected).isEqualTo(actual)
78+
// Check for order of call either
79+
coVerifyOrder {
80+
remotePostDataSource.getPostDTOs()
81+
mapper.map(postDTOList)
82+
}
83+
}
84+
85+
@Test
86+
fun `given DB is empty should return an empty list`() = runBlockingTest {
87+
88+
// GIVEN
89+
val expected = listOf<PostEntity>()
90+
coEvery { localPostDataSource.getPostEntities() } returns expected
91+
92+
// WHEN
93+
val actual = repository.getPostEntitiesFromLocal()
94+
95+
// THEN
96+
Truth.assertThat(actual).isEmpty()
97+
coVerify(exactly = 1) { localPostDataSource.getPostEntities() }
98+
}
99+
100+
@Test
101+
fun `given DB is populated should return data list`() = runBlockingTest {
102+
103+
// GIVEN
104+
coEvery { localPostDataSource.getPostEntities() } returns postEntityList
105+
106+
// WHEN
107+
val actual = repository.getPostEntitiesFromLocal()
108+
109+
// THEN
110+
Truth.assertThat(actual)
111+
.containsExactlyElementsIn(postEntityList)
112+
coVerify(exactly = 1) { localPostDataSource.getPostEntities() }
113+
}
114+
115+
@Test
116+
fun `given entities, should save entities to DB`() = runBlockingTest {
117+
118+
// GIVEN
119+
val idList = postEntityList.map {
120+
it.id.toLong()
121+
}
122+
123+
coEvery { localPostDataSource.saveEntities(postEntityList) } returns idList
124+
125+
// WHEN
126+
repository.savePostEntities(postEntityList)
127+
128+
// THEN
129+
coVerify(exactly = 1) { localPostDataSource.saveEntities(postEntityList) }
130+
}
131+
132+
@Test
133+
fun `given no error should delete entities`() = runBlockingTest {
134+
135+
// GIVEN
136+
coEvery { localPostDataSource.deletePostEntities() } just runs
137+
138+
// WHEN
139+
repository.deletePostEntities()
140+
141+
// THEN
142+
coVerify(exactly = 1) { localPostDataSource.deletePostEntities() }
143+
}
9144

10145
@BeforeEach
11146
fun setUp() {
147+
repository =
148+
PostRepositoryCoroutinesImpl(localPostDataSource, remotePostDataSource, mapper)
12149
}
13150

14151
@AfterEach
15152
fun tearDown() {
153+
clearMocks(localPostDataSource, remotePostDataSource, mapper)
16154
}
17155
}

‎libraries/data/src/test/java/com/smarttoolfactory/data/source/PostDataSourceCoroutinesTest.kt‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import org.junit.jupiter.api.TestInstance
2424
class PostDataSourceCoroutinesTest {
2525

2626
companion object {
27-
val PostDTOList =
27+
val postDTOList =
2828
convertFromJsonToListOf<PostDTO>(getResourceAsText(RESPONSE_JSON_PATH))!!
2929

3030
val postEntityList =
@@ -61,7 +61,7 @@ class PostDataSourceCoroutinesTest {
6161
fun `given Http 200, should return DTO list`() = runBlockingTest {
6262

6363
// GIVEN
64-
val actual = PostDTOList
64+
val actual = postDTOList
6565
coEvery { postApi.getPosts() } returns actual
6666

6767
// WHEN

‎libraries/data/src/test/java/com/smarttoolfactory/data/source/PostDataSourceRxJava3Test.kt‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import org.junit.jupiter.api.TestInstance
2424
class PostDataSourceRxJava3Test {
2525

2626
companion object {
27-
val PostDTOList =
27+
val postDTOList =
2828
convertFromJsonToListOf<PostDTO>(getResourceAsText(RESPONSE_JSON_PATH))!!
2929

3030
val postEntityList =
@@ -60,7 +60,7 @@ class PostDataSourceRxJava3Test {
6060
fun `given Http 200, should return DTO list`() {
6161

6262
// GIVEN
63-
val actual = PostDTOList
63+
val actual = postDTOList
6464
every { postApi.getPostsSingle() } returns Single.just(actual)
6565

6666
// WHEN

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /