Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update subject.reference #313

Merged
merged 8 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion efsity-cli/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ repositories {

group = "org.smartregister"

version = "2.3.11-DEV-SNAPSHOT"
version = "2.3.12-SNAPSHOT"

description = "fhircore-tooling (efsity)"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -378,12 +383,17 @@ static String populateMode(String questionnaireData, String fhir_base_url, Strin
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");
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);
} else {
FctUtils.printInfo("Error: 'item' not found in the questionnaire response.");
}
return String.valueOf(questionnaire_response);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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}"));
}
}