From d45e32adcfbf7fcefb33e53c9c524e47e7973935 Mon Sep 17 00:00:00 2001 From: oleksandrsarapulovgl <82441124+oleksandrsarapulovgl@users.noreply.github.com> Date: Thu, 24 Jun 2021 11:32:14 +0300 Subject: [PATCH] Updated validation logic (#6) --- .idea/sonarIssues.xml | 35 ++++++++++++++++++ engine/src/androidTest/resources/rule.json | 32 ++++++++--------- .../app/engine/DefaultCertLogicEngine.kt | 22 +++++++----- .../data/source/remote/DescriptionRemote.kt | 4 +-- .../engine/data/source/remote/RuleRemote.kt | 26 +++++++------- engine/src/test/resources/mock_rule.json | 36 ++++++++----------- engine/src/test/resources/rule.json | 32 ++++++++--------- 7 files changed, 109 insertions(+), 78 deletions(-) diff --git a/.idea/sonarIssues.xml b/.idea/sonarIssues.xml index e37887f..d062084 100644 --- a/.idea/sonarIssues.xml +++ b/.idea/sonarIssues.xml @@ -428,11 +428,31 @@ + + + + + + + + + + + + + + + + + + + + @@ -458,6 +478,11 @@ + + + + + @@ -558,6 +583,16 @@ + + + + + + + + + + diff --git a/engine/src/androidTest/resources/rule.json b/engine/src/androidTest/resources/rule.json index 1936383..aae1af9 100644 --- a/engine/src/androidTest/resources/rule.json +++ b/engine/src/androidTest/resources/rule.json @@ -1,20 +1,20 @@ { - "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 ]} + "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 ]} ] } } \ No newline at end of file diff --git a/engine/src/main/java/dgca/verifier/app/engine/DefaultCertLogicEngine.kt b/engine/src/main/java/dgca/verifier/app/engine/DefaultCertLogicEngine.kt index 66f64de..11f70db 100644 --- a/engine/src/main/java/dgca/verifier/app/engine/DefaultCertLogicEngine.kt +++ b/engine/src/main/java/dgca/verifier/app/engine/DefaultCertLogicEngine.kt @@ -35,6 +35,7 @@ class DefaultCertLogicEngine(private val jsonLogicValidator: JsonLogicValidator) companion object { private const val EXTERNAL_KEY = "external" private const val HCERT_KEY = "hcert" + private const val DESCRIPTION = "description" } init { @@ -66,23 +67,26 @@ class DefaultCertLogicEngine(private val jsonLogicValidator: JsonLogicValidator) val validationResults = mutableListOf() val schemaJsonNode = objectMapper.readValue(schemaJson) val dataJsonNode = prepareData(externalParameter, payload) - rules.forEach { it -> - val isValid = jsonLogicValidator.isDataValid(it.logic, dataJsonNode) + rules.forEach { rule -> + val isValid = jsonLogicValidator.isDataValid(rule.logic, dataJsonNode) val res = when { isValid -> Result.PASSED else -> Result.FAIL } val cur = StringBuilder() - it.affectedString.forEach { affectedField -> - cur.append( - "${ - schemaJsonNode.findValue(affectedField).findValue("description").asText() - }: ${dataJsonNode.findValue(affectedField).asText()}\n" - ) + rule.affectedString.forEach { affectedField -> + val description = + schemaJsonNode.findValue(affectedField)?.findValue(DESCRIPTION)?.asText() + val currentState = dataJsonNode.findValue(affectedField)?.asText() + if (description?.isNotBlank() == true && currentState?.isNotBlank() == true) { + cur.append( + "$description: $currentState\n" + ) + } } validationResults.add( ValidationResult( - it, + rule, res, cur.toString(), null diff --git a/engine/src/main/java/dgca/verifier/app/engine/data/source/remote/DescriptionRemote.kt b/engine/src/main/java/dgca/verifier/app/engine/data/source/remote/DescriptionRemote.kt index d519f89..08900e0 100644 --- a/engine/src/main/java/dgca/verifier/app/engine/data/source/remote/DescriptionRemote.kt +++ b/engine/src/main/java/dgca/verifier/app/engine/data/source/remote/DescriptionRemote.kt @@ -24,6 +24,6 @@ import com.fasterxml.jackson.annotation.JsonProperty * Created by osarapulov on 11.06.21 11:00 */ data class DescriptionRemote( - @JsonProperty("LANG") val lang: String, - @JsonProperty("DESC") val desc: String + @JsonProperty("lang") val lang: String, + @JsonProperty("desc") val desc: String ) \ No newline at end of file diff --git a/engine/src/main/java/dgca/verifier/app/engine/data/source/remote/RuleRemote.kt b/engine/src/main/java/dgca/verifier/app/engine/data/source/remote/RuleRemote.kt index 3c1e649..550d4df 100644 --- a/engine/src/main/java/dgca/verifier/app/engine/data/source/remote/RuleRemote.kt +++ b/engine/src/main/java/dgca/verifier/app/engine/data/source/remote/RuleRemote.kt @@ -26,30 +26,30 @@ import java.time.ZonedDateTime * Created by osarapulov on 11.06.21 11:03 */ data class RuleRemote( - @JsonProperty("IDENTIFIER") + @JsonProperty("Identifier") val identifier: String, - @JsonProperty("TYPE") + @JsonProperty("Type") val type: String, - @JsonProperty("VERSION") + @JsonProperty("Version") val version: String, - @JsonProperty("SCHEMAVERSION") + @JsonProperty("SchemaVersion") val schemaVersion: String, - @JsonProperty("ENGINE") + @JsonProperty("Engine") val engine: String, - @JsonProperty("ENGINEVERSION") + @JsonProperty("EngineVersion") val engineVersion: String, - @JsonProperty("CERTIFICATETYPE") + @JsonProperty("CertificateType") val certificateType: String, - @JsonProperty("DESCRIPTION") + @JsonProperty("Description") val descriptions: List, - @JsonProperty("VALIDFROM") + @JsonProperty("ValidFrom") val validFrom: ZonedDateTime, - @JsonProperty("VALIDTO") + @JsonProperty("ValidTo") val validTo: ZonedDateTime, - @JsonProperty("AFFECTEDFIELDS") + @JsonProperty("AffectedFields") val affectedString: List, - @JsonProperty("LOGIC") + @JsonProperty("Logic") val logic: JsonNode, - @JsonProperty("COUNTRY") + @JsonProperty("Country") val countryCode: String ) \ No newline at end of file diff --git a/engine/src/test/resources/mock_rule.json b/engine/src/test/resources/mock_rule.json index bc76bb5..d5bc61d 100644 --- a/engine/src/test/resources/mock_rule.json +++ b/engine/src/test/resources/mock_rule.json @@ -1,25 +1,17 @@ { - "IDENTIFIER": "GR-CZ-0001", - "VERSION": "1.0.0", - "SCHEMAVERSION": "1.0.0", - "ENGINE": "CERTLOGIC", - "ENGINEVERSION": "2.0.1", - "TYPE": "Acceptance", - "CERTIFICATETYPE": "Vaccination", - "COUNTRY": "at", - "DESCRIPTION": [ - { - "LANG": "en", - "DESC": "The Field “Doses” MUST contain number 2 OR 2/2." - } - ], - "VALIDFROM": "2021-05-27T07:46:40Z", - "VALIDTO": "2030-06-01T07:46:40Z", - "AFFECTEDFIELDS": [ - "dt", - "sd" - ], - "LOGIC": { + "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": [ { ">": [ @@ -41,4 +33,4 @@ } ] } -} \ No newline at end of file +} diff --git a/engine/src/test/resources/rule.json b/engine/src/test/resources/rule.json index 1936383..aae1af9 100644 --- a/engine/src/test/resources/rule.json +++ b/engine/src/test/resources/rule.json @@ -1,20 +1,20 @@ { - "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 ]} + "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 ]} ] } } \ No newline at end of file