Skip to content

Commit

Permalink
Increased min SDK version as Jackson doesn't see ZonedDateTime class …
Browse files Browse the repository at this point in the history
…with lower min SDK version even with desugaring (eu-digital-green-certificates#172)
  • Loading branch information
oleksandrsarapulovgl authored Aug 2, 2021
1 parent 6f40d1a commit 8dc3441
Show file tree
Hide file tree
Showing 5 changed files with 302 additions and 3 deletions.
5 changes: 5 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,9 @@ dependencies {
// testImplementation Deps.mockito_core
// testImplementation Deps.mockito_kotlin
coreLibraryDesugaring Deps.android_tools_desugar_jdk_libs

androidTestImplementation Deps.test_runner
androidTestImplementation Deps.test_rules
androidTestImplementation Deps.test_ext
androidTestImplementation Deps.appache_commons
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
/*
* ---license-start
* eu-digital-green-certificates / dgca-verifier-app-android
* ---
* Copyright (C) 2021 T-Systems International GmbH and all other contributors
* ---
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ---license-end
*
* Created by osarapulov on 8/2/21 9:21 AM
*/

package dgca.verifier.app.engine.data.source.local

import android.content.Context
import androidx.room.Room
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.fasterxml.jackson.databind.ObjectMapper
import dgca.verifier.app.android.data.local.rules.EngineDatabase
import dgca.verifier.app.android.data.local.rules.RuleWithDescriptionsLocal
import dgca.verifier.app.android.data.local.rules.RulesDao
import dgca.verifier.app.android.data.local.rules.toRuleWithDescriptionLocal
import dgca.verifier.app.engine.data.RuleCertificateType
import dgca.verifier.app.engine.data.source.remote.rules.RuleRemote
import dgca.verifier.app.engine.data.source.remote.rules.toRule
import org.apache.commons.io.IOUtils
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import java.io.IOException
import java.io.InputStream
import java.nio.charset.Charset

@RunWith(AndroidJUnit4::class)
internal class RulesDaoTest {
private lateinit var rulesDao: RulesDao
private lateinit var db: EngineDatabase
private val objectMapper = ObjectMapper().apply { this.findAndRegisterModules() }

companion object {
const val RULE_JSON_FILE_NAME = "rule.json"
const val RULE_WITH_REGION_JSON_FILE_NAME = "rule_with_region.json"
}

private fun fetchRule(fileName: String): RuleRemote {
val ruleExampleIs: InputStream =
javaClass.classLoader!!.getResourceAsStream(fileName)
val ruleJson = IOUtils.toString(ruleExampleIs, Charset.defaultCharset())
return objectMapper.readValue(ruleJson, RuleRemote::class.java)
}


@Before
fun createDb() {
val context = ApplicationProvider.getApplicationContext<Context>()
db = Room.inMemoryDatabaseBuilder(
context, EngineDatabase::class.java
).build()
rulesDao = db.rulesDao()
}

@After
@Throws(IOException::class)
fun closeDb() {
db.close()
}

@Test
@Throws(Exception::class)
fun testInsert() {
val rule = fetchRule(RULE_JSON_FILE_NAME).toRule()
val expected: RuleWithDescriptionsLocal = rule.toRuleWithDescriptionLocal()
rulesDao.insertAll(listOf(expected))

assertTrue(
rulesDao.getRulesWithDescriptionsBy(
rule.countryCode,
rule.validTo.plusDays(1),
rule.type,
rule.ruleCertificateType,
RuleCertificateType.GENERAL
).isEmpty()
)

val actual = rulesDao.getRulesWithDescriptionsBy(
rule.countryCode,
rule.validTo.minusMinutes(1),
rule.type,
rule.ruleCertificateType,
RuleCertificateType.GENERAL
)

assertTrue(actual.size == 1)
assertEquals(expected.rule.copy(ruleId = 1), actual[0].rule)
assertEquals(2, actual[0].descriptions.size)
expected.descriptions.forEachIndexed { index, descriptionLocal ->
assertEquals(
descriptionLocal.copy(
descriptionId = (index + 1).toLong(),
ruleContainerId = 1
), actual[0].descriptions[index]
)
}
}

@Test
@Throws(Exception::class)
fun testInsertRuleWithRegion() {
val ruleRemote = fetchRule(RULE_WITH_REGION_JSON_FILE_NAME).toRule()
val expected: RuleWithDescriptionsLocal = ruleRemote.toRuleWithDescriptionLocal()
rulesDao.insertAll(listOf(expected))

assertTrue(
rulesDao.getRulesWithDescriptionsBy(
ruleRemote.countryCode,
ruleRemote.validTo.plusDays(1),
ruleRemote.type,
ruleRemote.ruleCertificateType,
RuleCertificateType.GENERAL
).isEmpty()
)

val actual = rulesDao.getRulesWithDescriptionsBy(
ruleRemote.countryCode,
ruleRemote.validTo.minusMinutes(1),
ruleRemote.type,
ruleRemote.ruleCertificateType,
RuleCertificateType.GENERAL
)

assertTrue(actual.size == 1)
assertEquals(expected.rule.copy(ruleId = 1), actual[0].rule)
expected.descriptions.forEachIndexed { index, descriptionLocal ->
assertEquals(
descriptionLocal.copy(
descriptionId = (index + 1).toLong(),
ruleContainerId = 1
), actual[0].descriptions[index]
)
}
}

@Test
@Throws(Exception::class)
fun testDelete() {
val ruleRemote = fetchRule(RULE_JSON_FILE_NAME).toRule()
val expected: RuleWithDescriptionsLocal = ruleRemote.toRuleWithDescriptionLocal()
rulesDao.insertAll(listOf(expected))

assertEquals(1, rulesDao.getAll().size)
assertEquals(2, rulesDao.getDescriptionAll().size)

rulesDao.deleteRulesBy(listOf(ruleRemote.identifier))

assertEquals(0, rulesDao.getAll().size)
assertEquals(0, rulesDao.getDescriptionAll().size)
}

@Test
@Throws(Exception::class)
fun testDeleteAllExcept() {
val identifierFirst = "identifierFirst"
val identifierSecond = "identifierSecond"
val ruleFirst = fetchRule(RULE_JSON_FILE_NAME).toRule().copy(identifier = identifierFirst)
val ruleSecond = fetchRule(RULE_JSON_FILE_NAME).toRule().copy(identifier = identifierSecond)
val ruleWithDescriptionsLocalFirst: RuleWithDescriptionsLocal =
ruleFirst.toRuleWithDescriptionLocal()
val ruleWithDescriptionsLocalSecond: RuleWithDescriptionsLocal =
ruleSecond.toRuleWithDescriptionLocal()

rulesDao.insertAll(listOf(ruleWithDescriptionsLocalFirst, ruleWithDescriptionsLocalSecond))

assertEquals(2, rulesDao.getAll().size)

rulesDao.deleteAllExcept(arrayOf(identifierSecond))

val rulesLocalActual = rulesDao.getAll()
assertEquals(1, rulesLocalActual.size)
assertEquals(identifierSecond, rulesLocalActual.first().identifier)
}
}
56 changes: 56 additions & 0 deletions app/src/androidTest/resources/rule.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"Identifier": "GR-SE-0001",
"Type": "Acceptance",
"Country": "SE",
"Version": "1.0.0",
"SchemaVersion": "1.0.0",
"Engine": "CERTLOGIC",
"EngineVersion": "0.7.5",
"CertificateType": "General",
"Description": [
{
"lang": "en",
"desc": "The positive NAA test result (e.g., PCR) must be no older than 6 months."
},
{
"lang": "sv",
"desc": "Ett positivt NAA test resultat (ex PCR) får inte vara äldre än 6 månader"
}
],
"ValidFrom": "2021-07-05T20:27:25Z",
"ValidTo": "2021-07-19T20:27:25Z",
"AffectedFields": [
"r.0",
"r.0.fr"
],
"Logic": {
"if": [
{
"var": "payload.r.0"
},
{
"not-after": [
{
"plusTime": [
{
"var": "external.validationClock"
},
0,
"day"
]
},
{
"plusTime": [
{
"var": "payload.r.0.fr"
},
183,
"day"
]
}
]
},
true
]
}
}
43 changes: 43 additions & 0 deletions app/src/androidTest/resources/rule_with_region.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"Identifier": "VR-DE-1",
"Version": "1.0.0",
"SchemaVersion": "1.0.0",
"Engine": "CERTLOGIC",
"EngineVersion": "1.0.0",
"Type": "Acceptance",
"Country": "DE",
"CertificateType": "Vaccination",
"Description": [
{
"lang": "en",
"desc": "Vaccination must be from June and doses must be 2"
}
],
"ValidFrom": "2021-06-27T07:46:40Z",
"ValidTo": "2021-08-01T07:46:40Z",
"AffectedFields": [
"dt",
"dn"
],
"Logic": {
"and": [
{
">=": [
{
"var": "dt"
},
"2021-06-01T00:00:00Z"
]
},
{
">=": [
{
"var": "dn"
},
2
]
}
]
},
"Region": "Custom Region"
}
6 changes: 3 additions & 3 deletions buildSrc/src/main/java/AppConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@
import org.gradle.api.JavaVersion

object Config {
const val minSdk = 23
const val minSdk = 26
const val compileSdk = 29
const val targetSdk = 29
val javaVersion = JavaVersion.VERSION_1_8

const val versionCode = 18
const val versionName = "1.1.10"
const val versionCode = 19
const val versionName = "1.1.11"

const val androidTestInstrumentation = "androidx.test.runner.AndroidJUnitRunner"
const val proguardConsumerRules = "consumer-rules.pro"
Expand Down

0 comments on commit 8dc3441

Please sign in to comment.