Skip to content

Commit

Permalink
Merge branch 'main' into validate_before_publishig
Browse files Browse the repository at this point in the history
  • Loading branch information
pld authored Jan 8, 2024
2 parents cb65498 + 387ffbe commit f465b6d
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand All @@ -42,7 +40,8 @@ public class ValidateFhirResourcesCommand implements Runnable {
names = {"-s", "--schema"},
description = "configs schema"
)
private static String configSchema;

static String configSchema;

@Override
public void run() {
Expand Down Expand Up @@ -156,8 +155,12 @@ static int validateConfig(FctFile configFile) throws GenerationException, Valida
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;
}
}
Original file line number Diff line number Diff line change
@@ -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"));
}
}
61 changes: 61 additions & 0 deletions efsity/src/test/resources/profile_config.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
]
}

0 comments on commit f465b6d

Please sign in to comment.