From 20adfdf052150713c827bcd9fdcae89cfef7f863 Mon Sep 17 00:00:00 2001 From: Marian Jureczko Date: Sat, 28 Sep 2024 14:43:22 +0200 Subject: [PATCH] #17 in progress - unit test for initializer --- app/build.gradle | 6 -- .../screen/main/CustomInitializerForRoute.kt | 12 +-- .../main/CustomInitializerForRouteTest.kt | 96 +++++++++++++++++++ 3 files changed, 102 insertions(+), 12 deletions(-) create mode 100644 app/src/testCustom/java/pl/marianjureczko/poszukiwacz/screen/main/CustomInitializerForRouteTest.kt diff --git a/app/build.gradle b/app/build.gradle index fbbee05..fcea369 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -141,7 +141,6 @@ dependencies { implementation "androidx.navigation:navigation-compose:2.4.2" implementation "androidx.window:window-java:1.1.0" implementation "com.google.dagger:hilt-android:${hilt_version}" - implementation 'com.google.ar:core:1.45.0' kapt "com.google.dagger:hilt-android-compiler:${hilt_version}" implementation 'androidx.hilt:hilt-navigation-compose:1.1.0-alpha01' implementation 'com.google.accompanist:accompanist-permissions:0.30.1' @@ -153,11 +152,6 @@ dependencies { implementation 'org.apache.commons:commons-math3:3.6.1' implementation 'com.google.android.gms:play-services-location:21.1.0' -// required for minifyEnabled true - implementation "org.conscrypt:conscrypt-android:2.5.2" - implementation "org.bouncycastle:bcpkix-jdk15to18:1.70" - implementation "org.openjsse:openjsse:1.1.7" - testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.8.2' testRuntimeOnly group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.8.2' testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-params', version: '5.8.2' diff --git a/app/src/custom/java/pl/marianjureczko/poszukiwacz/screen/main/CustomInitializerForRoute.kt b/app/src/custom/java/pl/marianjureczko/poszukiwacz/screen/main/CustomInitializerForRoute.kt index e303273..106b261 100644 --- a/app/src/custom/java/pl/marianjureczko/poszukiwacz/screen/main/CustomInitializerForRoute.kt +++ b/app/src/custom/java/pl/marianjureczko/poszukiwacz/screen/main/CustomInitializerForRoute.kt @@ -7,8 +7,8 @@ import java.io.FileOutputStream //TODO t: add unit test class CustomInitializerForRoute( - val storageHelper: StorageHelper, - val assetManager: AssetManager + private val storageHelper: StorageHelper, + private val assetManager: AssetManager ) { companion object { const val routeName = "custom" @@ -24,6 +24,10 @@ class CustomInitializerForRoute( } } + fun isAlreadyCopied(): Boolean { + return File(pathToDestination + markerFile).exists() + } + private fun copyRouteDefinition() { copy("$routeName.xml", storageHelper.getRouteFile(routeName)) } @@ -47,10 +51,6 @@ class CustomInitializerForRoute( storageHelper.save(route) } - private fun isAlreadyCopied(): Boolean { - return File(pathToDestination + markerFile).exists() - } - private fun markIsCopied() { File(pathToDestination + markerFile).createNewFile() } diff --git a/app/src/testCustom/java/pl/marianjureczko/poszukiwacz/screen/main/CustomInitializerForRouteTest.kt b/app/src/testCustom/java/pl/marianjureczko/poszukiwacz/screen/main/CustomInitializerForRouteTest.kt new file mode 100644 index 0000000..7629105 --- /dev/null +++ b/app/src/testCustom/java/pl/marianjureczko/poszukiwacz/screen/main/CustomInitializerForRouteTest.kt @@ -0,0 +1,96 @@ +package pl.marianjureczko.poszukiwacz.screen.main + +import android.content.res.AssetManager +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.AfterEach +import org.junit.jupiter.api.Assertions.* + +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.extension.ExtendWith +import org.mockito.BDDMockito.given +import org.mockito.BDDMockito.then +import org.mockito.BDDMockito.times +import org.mockito.Mock +import org.mockito.junit.jupiter.MockitoExtension +import pl.marianjureczko.poszukiwacz.model.Route +import pl.marianjureczko.poszukiwacz.model.TreasureDescription +import pl.marianjureczko.poszukiwacz.shared.StorageHelper +import pl.marianjureczko.poszukiwacz.shared.XmlHelper +import java.io.File + +@ExtendWith(MockitoExtension::class) +class CustomInitializerForRouteTest { + + @Mock + lateinit var storage: StorageHelper + + @Mock + lateinit var assetManager: AssetManager + + private val pathToDestination = "." + private val destinationRoute = "$pathToDestination/custom.xml" + private val treasure = TreasureDescription( + 1, + 50.501055, + 18.180191, + "k01001", + "tip_01.m4a", + "tip_01.jpg", + "kalinowice_01.mp4", + "en_01.srt" + ) + private val route = Route("custom", mutableListOf(treasure.copy())) + + @AfterEach + fun removeFiles() { + tryToRemove(destinationRoute) + tryToRemove(treasure.photoFileName) + tryToRemove(treasure.tipFileName) + tryToRemove(treasure.movieFileName) + tryToRemove(treasure.subtitlesFileName) + tryToRemove("${pathToDestination}/copied_marker.txt") + } + + @Test + fun copyRouteToLocalStorage() { + //given + given(storage.pathToRoutesDir()).willReturn(pathToDestination) + given(storage.getRouteFile(CustomInitializerForRoute.routeName)).willReturn(File(destinationRoute)) + given(assetManager.open("${CustomInitializerForRoute.routeName}.xml")) + .willReturn(XmlHelper().writeToString(route).byteInputStream()) + given(storage.loadRoute(CustomInitializerForRoute.routeName)).willReturn(route) + given(assetManager.open(treasure.photoFileName!!)).willReturn(treasure.photoFileName!!.byteInputStream()) + given(assetManager.open(treasure.tipFileName!!)).willReturn(treasure.tipFileName!!.byteInputStream()) + given(assetManager.open(treasure.movieFileName!!)).willReturn(treasure.movieFileName!!.byteInputStream()) + given(assetManager.open(treasure.subtitlesFileName!!)).willReturn(treasure.subtitlesFileName!!.byteInputStream()) + val initializer = CustomInitializerForRoute(storage, assetManager) + + //when + initializer.copyRouteToLocalStorage() + + //then + then(storage).should(times(1)).save(route) + //all paths in the saved route should be prefixed with pathToDestination + val actualTreasure = route.treasures[0] + val prefix = pathToDestination + "/" + assertThat(actualTreasure.photoFileName).isEqualTo(prefix + treasure.photoFileName) + assertThat(actualTreasure.tipFileName).isEqualTo(prefix + treasure.tipFileName) + assertThat(actualTreasure.movieFileName).isEqualTo(prefix + treasure.movieFileName) + assertThat(actualTreasure.subtitlesFileName).isEqualTo(prefix + treasure.subtitlesFileName) + + assertThat(File(treasure.photoFileName!!)).hasContent(treasure.photoFileName) + assertThat(File(treasure.tipFileName!!)).hasContent(treasure.tipFileName) + assertThat(File(treasure.movieFileName!!)).hasContent(treasure.movieFileName) + assertThat(File(treasure.subtitlesFileName!!)).hasContent(treasure.subtitlesFileName) + assertThat(initializer.isAlreadyCopied()).isTrue() + } + + private fun tryToRemove(path: String?) { + try { + File(path).delete() + } catch (ex: Exception) { + System.err.println(ex); + } + } + +} \ No newline at end of file