From 7dfbaec83aa4a3992815ff520d1c48ccd38e54ba Mon Sep 17 00:00:00 2001 From: Sharon Akinyi <79141719+sharon2719@users.noreply.github.com> Date: Mon, 13 Jan 2025 18:25:37 +0300 Subject: [PATCH] Update subject.reference (#313) * Update subject.reference * Update snapshot * Add test and run spotless * Run spotless * Updated changes --- efsity-cli/build.gradle.kts | 2 +- ...QuestionnaireResponseGeneratorCommand.java | 21 +++-- ...tionnaireResponseGeneratorCommandTest.java | 93 +++++++++++++++++++ 3 files changed, 107 insertions(+), 9 deletions(-) diff --git a/efsity-cli/build.gradle.kts b/efsity-cli/build.gradle.kts index dec09a24..cdf95454 100644 --- a/efsity-cli/build.gradle.kts +++ b/efsity-cli/build.gradle.kts @@ -18,7 +18,7 @@ repositories { group = "org.smartregister" -version = "2.3.11-DEV-SNAPSHOT" +version = "2.3.12-SNAPSHOT" description = "fhircore-tooling (efsity)" diff --git a/efsity-cli/src/main/java/org/smartregister/command/QuestionnaireResponseGeneratorCommand.java b/efsity-cli/src/main/java/org/smartregister/command/QuestionnaireResponseGeneratorCommand.java index 893d6c77..121e7fd6 100644 --- a/efsity-cli/src/main/java/org/smartregister/command/QuestionnaireResponseGeneratorCommand.java +++ b/efsity-cli/src/main/java/org/smartregister/command/QuestionnaireResponseGeneratorCommand.java @@ -360,7 +360,12 @@ static String populateMode(String questionnaireData, String fhir_base_url, Strin } JSONObject subject = new JSONObject(); subject.put("name", "subject"); - subject.put("valueString", UUID.randomUUID().toString()); + String referenceValue = + resourceType + "/" + UUID.randomUUID(); // Using resourceType dynamically + JSONObject reference = new JSONObject(); + reference.put("reference", referenceValue); + subject.put("valueReference", reference); + JSONArray arr = new JSONArray(); arr.put(subject); JSONObject params = new JSONObject(); @@ -373,17 +378,17 @@ static String populateMode(String questionnaireData, String fhir_base_url, Strin List result = HttpClient.postRequest(params.toString(), populate_endpoint, null); JSONObject questionnaire_response = new JSONObject(result.get(1)); - FctUtils.printInfo("Debug: questionnaire_response before line 379: " + questionnaire_response); + FctUtils.printError("Debug: response from questionnaireResponse: " + questionnaire_response); if (questionnaire_response.has("contained")) { questionnaire_response.remove("contained"); } - JSONArray response = (JSONArray) questionnaire_response.get("item"); - JSONArray questions = resource.getJSONArray("item"); - FctUtils.printInfo("Debug: questionnaire_response before line 379: " + questionnaire_response); - - JSONArray response_with_answers = getAnswers(questions, response, extras); - questionnaire_response.put("item", response_with_answers); + if (questionnaire_response.has("item")) { + JSONArray response = (JSONArray) questionnaire_response.get("item"); + JSONArray questions = resource.getJSONArray("item"); + JSONArray response_with_answers = getAnswers(questions, response, extras); + questionnaire_response.put("item", response_with_answers); + } return String.valueOf(questionnaire_response); } diff --git a/efsity-cli/src/test/java/org/smartregister/command/QuestionnaireResponseGeneratorCommandTest.java b/efsity-cli/src/test/java/org/smartregister/command/QuestionnaireResponseGeneratorCommandTest.java index 14ec85bd..cf029eca 100644 --- a/efsity-cli/src/test/java/org/smartregister/command/QuestionnaireResponseGeneratorCommandTest.java +++ b/efsity-cli/src/test/java/org/smartregister/command/QuestionnaireResponseGeneratorCommandTest.java @@ -2,6 +2,8 @@ import static org.junit.jupiter.api.Assertions.*; +import java.time.LocalDate; +import java.time.LocalDateTime; import org.json.JSONArray; import org.json.JSONObject; import org.junit.jupiter.api.Test; @@ -69,4 +71,95 @@ void testGenerateAnswer_noExtras() { assertNotNull(result); assertTrue(result.has("valueInteger")); } + + @Test + void testGenerateRandomDate() { + LocalDate randomDate = QuestionnaireResponseGeneratorCommand.generateRandomDate(); + assertNotNull(randomDate); + assertTrue( + randomDate.isAfter(LocalDate.of(1959, 12, 31)) + && randomDate.isBefore(LocalDate.of(2024, 1, 1))); + } + + @Test + void testGenerateRandomDateTime() { + LocalDateTime randomDateTime = QuestionnaireResponseGeneratorCommand.generateRandomDateTime(); + assertNotNull(randomDateTime); + assertTrue( + randomDateTime.isAfter(LocalDateTime.of(1999, 12, 31, 23, 59)) + && randomDateTime.isBefore(LocalDateTime.of(2025, 1, 1, 0, 0))); + } + + @Test + void testGenerateChoiceValue_validOption() { + JSONArray questions = new JSONArray(); + JSONObject question = new JSONObject(); + question.put("type", "choice"); + question.put("linkId", "exampleLinkId"); + JSONArray answerOptions = new JSONArray(); + JSONObject option = new JSONObject(); + JSONObject coding = new JSONObject(); + coding.put("code", "12345"); + coding.put("display", "Option 1"); + option.put("valueCoding", coding); + answerOptions.put(option); + question.put("answerOption", answerOptions); + questions.put(question); + + JSONObject result = + QuestionnaireResponseGeneratorCommand.generateChoiceValue(questions, "exampleLinkId"); + assertNotNull(result); + assertEquals("12345", result.getString("code")); + assertEquals("Option 1", result.getString("display")); + } + + @Test + void testGenerateChoiceValue_noAnswerOptions() { + JSONArray questions = new JSONArray(); + JSONObject question = new JSONObject(); + question.put("type", "choice"); + question.put("linkId", "exampleLinkId"); + questions.put(question); + + JSONObject result = + QuestionnaireResponseGeneratorCommand.generateChoiceValue(questions, "exampleLinkId"); + assertNull(result); + } + + @Test + void testGenerateQuantityValue_withExtensions() { + JSONArray questions = new JSONArray(); + JSONObject question = new JSONObject(); + question.put("type", "quantity"); + question.put("linkId", "exampleLinkId"); + + JSONArray extensions = new JSONArray(); + JSONObject minValue = new JSONObject(); + minValue.put("url", "http://example.com/minValue"); + minValue.put("valueInteger", 50); + JSONObject maxValue = new JSONObject(); + maxValue.put("url", "http://example.com/maxValue"); + maxValue.put("valueInteger", 500); + extensions.put(minValue); + extensions.put(maxValue); + question.put("extension", extensions); + + questions.put(question); + + JSONObject result = + QuestionnaireResponseGeneratorCommand.generateQuantityValue(questions, "exampleLinkId"); + assertNotNull(result); + assertTrue(result.getInt("value") >= 50 && result.getInt("value") <= 500); + assertEquals("cm", result.getString("unit")); + } + + @Test + void testGenerateReferenceValue() { + JSONObject reference = QuestionnaireResponseGeneratorCommand.generateReferenceValue(); + assertNotNull(reference); + assertTrue( + reference + .getString("reference") + .matches("(Patient|Practitioner|Location|Immunization)/[a-f0-9-]{36}")); + } }