Skip to content

Commit

Permalink
#17 in progress - unit test for initializer
Browse files Browse the repository at this point in the history
  • Loading branch information
mjureczko committed Sep 28, 2024
1 parent 76c73c0 commit 20adfdf
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 12 deletions.
6 changes: 0 additions & 6 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -24,6 +24,10 @@ class CustomInitializerForRoute(
}
}

fun isAlreadyCopied(): Boolean {
return File(pathToDestination + markerFile).exists()
}

private fun copyRouteDefinition() {
copy("$routeName.xml", storageHelper.getRouteFile(routeName))
}
Expand All @@ -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()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}

}

0 comments on commit 20adfdf

Please sign in to comment.