# LocalAssetManager.kt

    /*
     * The MIT License
     *
     * Original work sponsored and donated by The Danish Health Data Authority (http://www.sundhedsdatastyrelsen.dk)
     *
     * Copyright (C) 2023 The Danish Health Data Authority (http://www.sundhedsdatastyrelsen.dk)
     *
     *
     * Permission is hereby granted, free of charge, to any person obtaining a copy of
     * this software and associated documentation files (the "Software"), to deal in
     * the Software without restriction, including without limitation the rights to
     * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
     * of the Software, and to permit persons to whom the Software is furnished to do
     * so, subject to the following conditions:
     *
     *
     * The above copyright notice and this permission notice shall be included in all
     * copies or substantial portions of the Software.
     *
     *
     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
     * SOFTWARE.
     */
    import androidx.annotation.VisibleForTesting
    import dk.nsp.mingraviditet.core.network.appointments.AssetManager
    import java.io.File
    import java.io.InputStream
    import java.util.Properties

    /**
     * This class helps with loading Android `/assets` files, especially when running JVM unit tests.
     * It must remain on the root package for an easier [Class.getResource] with relative paths.
     * @see <a href="https://developer.android.com/reference/tools/gradle-api/7.3/com/android/build/api/dsl/UnitTestOptions">UnitTestOptions</a>
     */
    @VisibleForTesting
    object LocalAssetManager : AssetManager {
        private val config =
            requireNotNull(javaClass.getResource("com/android/tools/test_config.properties")) {
                """
                Missing Android resources properties file.
                Did you forget to enable the feature in the gradle build file?
                android.testOptions.unitTests.isIncludeAndroidResources = true
                """.trimIndent()
            }
        private val properties = Properties().apply { config.openStream().use(::load) }
        private val assets = File(properties["android_merged_assets"].toString())

        override fun open(fileName: String): InputStream = File(assets, fileName).inputStream()
    }