From 92411e87c93a4236f89b282a769422aa6310d075 Mon Sep 17 00:00:00 2001 From: Lincoln Simba Date: Mon, 8 Jan 2024 19:22:06 +0300 Subject: [PATCH 1/2] Print config errors instead of exiting (#99) fixes #https://github.com/onaio/fhir-tooling/issues/94 Co-authored-by: Peter Lubell-Doughtie --- .../command/ValidateFhirResourcesCommand.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/efsity/src/main/java/org/smartregister/command/ValidateFhirResourcesCommand.java b/efsity/src/main/java/org/smartregister/command/ValidateFhirResourcesCommand.java index 9a7b1e9c..8869ea86 100644 --- a/efsity/src/main/java/org/smartregister/command/ValidateFhirResourcesCommand.java +++ b/efsity/src/main/java/org/smartregister/command/ValidateFhirResourcesCommand.java @@ -15,8 +15,6 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; -import java.util.Arrays; -import java.util.List; import java.util.Map; import net.jimblackler.jsonschemafriend.*; @@ -156,8 +154,12 @@ int validateConfig(FctFile configFile) throws GenerationException, ValidationExc Schema schema = schemaStore.loadSchema(new File(String.valueOf(Paths.get(configSchema)))); Validator validator = new Validator(); - validator.validateJson(schema, configFile.getContent()); - FctUtils.printToConsole("Config file is valid!"); + try { + validator.validateJson(schema, configFile.getContent()); + FctUtils.printToConsole("Config file is valid!"); + } catch (ValidationException e) { + FctUtils.printError(e.toString()); + } return 0; } } From 387ffbeffb4ee1101a19c4ee58c86d40b00e06e7 Mon Sep 17 00:00:00 2001 From: Lincoln Simba Date: Mon, 8 Jan 2024 19:26:12 +0300 Subject: [PATCH 2/2] Add config tests 1 (#100) * Print config errors instead of exiting fixes #https://github.com/onaio/fhir-tooling/issues/94 * Add config tests Succeeds https://github.com/onaio/fhir-tooling/pull/98 Tests should run against the changes on this PR https://github.com/onaio/fhir-tooling/pull/99 fixes https://github.com/onaio/fhir-tooling/issues/93 --------- Co-authored-by: Peter Lubell-Doughtie --- .../command/ValidateFhirResourcesCommand.java | 10 +-- .../ValidateFhirResourcesCommandTest.java | 53 ++++++++++++++++ efsity/src/test/resources/profile_config.json | 61 +++++++++++++++++++ 3 files changed, 119 insertions(+), 5 deletions(-) create mode 100644 efsity/src/test/java/org/smartregister/command/ValidateFhirResourcesCommandTest.java create mode 100644 efsity/src/test/resources/profile_config.json diff --git a/efsity/src/main/java/org/smartregister/command/ValidateFhirResourcesCommand.java b/efsity/src/main/java/org/smartregister/command/ValidateFhirResourcesCommand.java index 8869ea86..bb6d95cc 100644 --- a/efsity/src/main/java/org/smartregister/command/ValidateFhirResourcesCommand.java +++ b/efsity/src/main/java/org/smartregister/command/ValidateFhirResourcesCommand.java @@ -40,7 +40,7 @@ public class ValidateFhirResourcesCommand implements Runnable { names = {"-s", "--schema"}, description = "configs schema" ) - private String configSchema; + static String configSchema; @Override public void run() { @@ -53,7 +53,7 @@ public void run() { } } - private void validateFhirResources(String inputFilePath) throws IOException, ValidationException, GenerationException { + public static void validateFhirResources(String inputFilePath) throws IOException, ValidationException, GenerationException { long start = System.currentTimeMillis(); @@ -117,7 +117,7 @@ private void validateFhirResources(String inputFilePath) throws IOException, Val } } - private int validateResource(FhirValidator validator, IBaseResource resource) { + private static int validateResource(FhirValidator validator, IBaseResource resource) { ValidationResult result = validator.validateWithResult(resource); @@ -144,12 +144,12 @@ private int validateResource(FhirValidator validator, IBaseResource resource) { } } - boolean isConfigFile(FctFile inputFile){ + static boolean isConfigFile(FctFile inputFile){ JSONObject resource = new JSONObject(inputFile.getContent()); return resource.has("configType"); } - int validateConfig(FctFile configFile) throws GenerationException, ValidationException { + static int validateConfig(FctFile configFile) throws GenerationException, ValidationException { SchemaStore schemaStore = new SchemaStore(); Schema schema = schemaStore.loadSchema(new File(String.valueOf(Paths.get(configSchema)))); diff --git a/efsity/src/test/java/org/smartregister/command/ValidateFhirResourcesCommandTest.java b/efsity/src/test/java/org/smartregister/command/ValidateFhirResourcesCommandTest.java new file mode 100644 index 00000000..34d28ad1 --- /dev/null +++ b/efsity/src/test/java/org/smartregister/command/ValidateFhirResourcesCommandTest.java @@ -0,0 +1,53 @@ +package org.smartregister.command; + +import static org.junit.jupiter.api.Assertions.*; + +import net.jimblackler.jsonschemafriend.GenerationException; +import net.jimblackler.jsonschemafriend.ValidationException; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.smartregister.domain.FctFile; +import org.smartregister.util.FctUtils; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; + +import ca.uhn.fhir.parser.DataFormatException; + +public class ValidateFhirResourcesCommandTest { + + private ValidateFhirResourcesCommand validateFhirResourcesCommand; + + @BeforeEach + void setUp() { + validateFhirResourcesCommand = new ValidateFhirResourcesCommand(); + } + @Test + void testValidateResource() throws IOException, ValidationException, GenerationException { + // valid resource + String input = "src/test/resources/raw_questionnaire.json"; + assertDoesNotThrow(() -> validateFhirResourcesCommand.validateFhirResources(input)); + // invalid resource + String invalidInput = "src/test/resources/fhirConfigsJsonSchema.json"; + assertThrows(DataFormatException.class, () -> validateFhirResourcesCommand.validateFhirResources(invalidInput)); + } + + @Test + void testValidateConfig() throws IOException, ValidationException, GenerationException { + // valid config + String input = "src/test/resources/profile_config.json"; + FctFile inputFile = FctUtils.readFile(input); + validateFhirResourcesCommand.configSchema = "src/test/resources/fhirConfigsJsonSchema.json"; + assertDoesNotThrow(() -> validateFhirResourcesCommand.validateConfig(inputFile)); + + // invalid config + String invalidInput = "src/test/resources/raw_questionnaire.json"; + FctFile invalidInputFile = FctUtils.readFile(invalidInput); + ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream(); + System.setOut(new PrintStream(outputStreamCaptor)); + validateFhirResourcesCommand.validateConfig(invalidInputFile); + System.setOut(System.out); + assertTrue(outputStreamCaptor.toString().contains("Validation error")); + } +} diff --git a/efsity/src/test/resources/profile_config.json b/efsity/src/test/resources/profile_config.json new file mode 100644 index 00000000..c5852900 --- /dev/null +++ b/efsity/src/test/resources/profile_config.json @@ -0,0 +1,61 @@ +{ + "appId": "app_id_name", + "configType": "profile", + "id": "config_id", + "fhirResource": { + "baseResource": { + "resource": "Patient" + }, + "relatedResources": [ + { + "resource": "Condition", + "searchParameter": "subject" + } + ]}, + "rules": [ + { + "name": "patientFirstName", + "condition": "true", + "actions": [ + "data.put('patientFirstName', fhirPath.extractValue(Patient, \"Patient.name[0].select(given)\"))" + ] + }, + { + "name": "patientMiddleName", + "condition": "true", + "actions": [ + "data.put('patientMiddleName', fhirPath.extractValue(Patient, \"Patient.name[0].select(text[0])\"))" + ] + }, + { + "name": "patientLastName", + "condition": "true", + "actions": [ + "data.put('patientLastName', fhirPath.extractValue(Patient, \"Patient.name[0].select(family)\"))" + ] + } + ], + "views": [ + { + "viewType": "COLUMN", + "children": [ + { + "viewType": "CARD", + "padding": 0 + } + ] + } + ], + "overFlowMenuItems": [ + { + "title": "Edit Client Info", + "titleColor": "@{patientTextColor}", + "visible": "true", + "enabled": "@{patientActive}", + "icon": { + "type": "local", + "reference": "ic_user" + } + } + ] +} \ No newline at end of file