diff --git a/app/build.gradle b/app/build.gradle index 7de7ce6..dc54433 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -59,6 +59,8 @@ dependencies { // TESTING testImplementation 'junit:junit:4.12' + testImplementation "io.mockk:mockk:1.9.3" + testImplementation "com.squareup.okhttp3:mockwebserver:4.2.1" androidTestImplementation 'androidx.test.ext:junit:1.1.1' androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' androidTestImplementation "androidx.room:room-testing:$rootProject.roomVersion" diff --git a/app/src/test/java/it/unisannio/ding/ids/wedroid/app/BoardRepositoryTest.kt b/app/src/test/java/it/unisannio/ding/ids/wedroid/app/BoardRepositoryTest.kt new file mode 100644 index 0000000..3e73d76 --- /dev/null +++ b/app/src/test/java/it/unisannio/ding/ids/wedroid/app/BoardRepositoryTest.kt @@ -0,0 +1,171 @@ +package it.unisannio.ding.ids.wedroid.app + +import io.mockk.coEvery +import io.mockk.every +import io.mockk.mockk +import it.unisannio.ding.ids.wedroid.app.data.dao.BoardDao +import it.unisannio.ding.ids.wedroid.app.data.entity.Board +import it.unisannio.ding.ids.wedroid.app.data.repository.BoardRepository +import it.unisannio.ding.ids.wedroid.app.util.PreferenceReader +import it.unisannio.ding.ids.wedroid.wrapper.api.BoardService +import it.unisannio.ding.ids.wedroid.wrapper.entity.BoardBackgroundColor +import junit.framework.TestCase.* +import kotlinx.coroutines.runBlocking +import okhttp3.mockwebserver.MockResponse +import okhttp3.mockwebserver.MockWebServer +import org.junit.After +import org.junit.Before +import org.junit.Test +import retrofit2.Retrofit +import retrofit2.converter.gson.GsonConverterFactory +import java.net.HttpURLConnection +import java.util.concurrent.CountDownLatch +import java.util.concurrent.TimeUnit +import kotlin.reflect.full.declaredFunctions +import kotlin.reflect.jvm.isAccessible + +class BoardRepositoryTest { + private val reader = mockk() + private val webServer = MockWebServer() + private lateinit var service: BoardService + private lateinit var latch: CountDownLatch + + @Before + fun setUp() { + webServer.start() + service = Retrofit.Builder() + .baseUrl(webServer.url("/")) + .addConverterFactory(GsonConverterFactory.create()) + .build() + .create(BoardService::class.java) + + every { reader.userId } returns "user id" + + + latch = CountDownLatch(1) + } + + @After + fun teardown() { + webServer.close() + } + + @Test + fun insertWithOkOnServer() { + val dao = mockk() + var inInsert = false + + coEvery { + dao.insert(any()) + } answers { + inInsert = true + assertEquals("id", arg(0).id) + assertEquals("title", arg(0).title) + latch.countDown() + } + + webServer.enqueue( + MockResponse() + .setResponseCode(HttpURLConnection.HTTP_OK) + .setBody("{ \"_id\": \"id\" }") + ) + + val repository = BoardRepository( + dao, service, reader + ) + + repository.insertBoard( + "title", true, BoardBackgroundColor.LIMEGREEN + ) + + latch.await(5, TimeUnit.SECONDS) + + assertTrue(inInsert) + } + + @Test + fun notInsertWithErrorOnServer() { + val dao = mockk() + var inInsert = false + + coEvery { + dao.insert(any() as Board) + } answers { + inInsert = true + latch.countDown() + } + + webServer.enqueue( + MockResponse() + .setResponseCode(HttpURLConnection.HTTP_BAD_REQUEST) + ) + + val repository = BoardRepository( + dao, service, reader + ) + + repository.insertBoard( + "title", true, BoardBackgroundColor.LIMEGREEN + ) + + latch.await(3, TimeUnit.SECONDS) + + assertFalse(inInsert) + } + + @Test + fun deleteWithOkOnServer() { + val dao = mockk() + var inDelete = false + + coEvery { + dao.delete(any() as Board) + } answers { + assertEquals("id", arg(0).id) + inDelete = true + latch.countDown() + } + + webServer.enqueue( + MockResponse() + .setResponseCode(HttpURLConnection.HTTP_OK) + ) + + val repository = BoardRepository( + dao, service, reader + ) + + repository.deleteBoard("id") + + latch.await(5, TimeUnit.SECONDS) + assertTrue(inDelete) + } + + @Test + fun notDeleteWithErrorOnServer() { + val dao = mockk() + var inDelete = false + + coEvery { + dao.delete(any() as Board) + } answers { + assertEquals("id", arg(0).id) + inDelete = true + latch.countDown() + } + + webServer.enqueue( + MockResponse() + .setResponseCode(HttpURLConnection.HTTP_BAD_REQUEST) + ) + + val repository = BoardRepository( + dao, service, reader + ) + + repository.deleteBoard("id") + + latch.await(3, TimeUnit.SECONDS) + assertFalse(inDelete) + } +} \ No newline at end of file