Skip to content

Commit

Permalink
Merge branch 'development' into mb-jn-file-library-v2
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthewBemis authored Jan 8, 2025
2 parents fb2df40 + ee2e4e1 commit 6563288
Show file tree
Hide file tree
Showing 26 changed files with 1,958 additions and 138 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public ResponseEntity<Object> listPortalEnvAlerts(String portalShortcode, String
"https://gvascdev.b2clogin.com", // gVASC (demo)
"https://gvascprod.b2clogin.com", // gVASC (prod)
"https://juniperatcpdev.b2clogin.com", // ATCP (demo)
"https://trccproject.b2clogin.com" // tRCC (prod)
},
maxAge = 3600,
methods = {RequestMethod.GET, RequestMethod.OPTIONS})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ public String getSwagger() {
"https://gvascdev.b2clogin.com", // gVASC (demo)
"https://gvascprod.b2clogin.com", // gVASC (prod)
"https://juniperatcpdev.b2clogin.com", // ATCP (demo)
"https://trccproject.b2clogin.com" // tRCC (prod)
},
maxAge = 3600,
methods = {RequestMethod.GET, RequestMethod.OPTIONS})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public SiteMediaController(SiteMediaService siteMediaService) {
"https://hearthive.b2clogin.com", // HeartHive (demo)
"https://gvascdev.b2clogin.com", // gVASC (demo)
"https://gvascprod.b2clogin.com", // gVASC (prod)
"https://juniperatcpdev.b2clogin.com" // ATCP (demo)
"https://juniperatcpdev.b2clogin.com", // ATCP (demo)
"https://trccproject.b2clogin.com" // tRCC (prod)
},
maxAge = 3600,
methods = {RequestMethod.GET, RequestMethod.OPTIONS})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.JsonNodeType;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.commons.lang3.StringUtils;

Expand Down Expand Up @@ -58,18 +59,44 @@ private static List<JsonNode> getSubQuestions(JsonNode parent) {
List<JsonNode> subQuestions = new ArrayList<>();

if (parent.get("type").asText().equals("paneldynamic") && parent.has("templateElements")) {
subQuestions = getPanelDynamicSubQuestions(parent);
}

// keep track of the parent stableid
subQuestions = subQuestions
.stream()
.map(q -> (JsonNode) q.deepCopy())
.map(q -> {
((ObjectNode) q).put("parent", parent.get("name").asText());
return q;
})
.toList();
// get subquestions and add parent stable id to each
subQuestions = getPanelDynamicSubQuestions(parent)
.stream()
.map(q -> (JsonNode) q.deepCopy())
.map(q -> {
((ObjectNode) q).put("parent", parent.get("name").asText());
return q;
})
.toList();
}

if (parent.get("type").asText().equals("checkbox") && parent.has("choices")) {
// not a derived value, so doesn't need parent
subQuestions = getCheckboxOtherSubquestions(parent);
}

return subQuestions;
}

private static List<JsonNode> getCheckboxOtherSubquestions(JsonNode parent) {
List<JsonNode> subQuestions = new ArrayList<>();
if (parent.has("choices")) {
for (JsonNode choice : parent.get("choices")) {
if (choice.has("otherStableId")) {
JsonNode otherQuestion = parent.deepCopy();
((ObjectNode) otherQuestion).put("name", choice.get("otherStableId").asText());
if (choice.has("otherText")) {
((ObjectNode) otherQuestion).put("title", nodeToString(choice.get("otherText")));
} else {
((ObjectNode) otherQuestion).put("title", "Other (" + choice.get("value").asText() + ")");
}
((ObjectNode) otherQuestion).put("type", "text");
((ObjectNode) otherQuestion).remove("choices");
((ObjectNode) otherQuestion).remove("required");
subQuestions.add(otherQuestion);
}
}
}
return subQuestions;
}

Expand Down Expand Up @@ -118,9 +145,9 @@ public static SurveyQuestionDefinition unmarshalSurveyQuestion(
//For normal elements, we'll store the title in the question_text column
//For HTML elements which don't have a title, we'll store the HTML instead
if (templatedQuestion.has("title")) {
definition.setQuestionText(templatedQuestion.get("title").asText());
definition.setQuestionText(nodeToString(templatedQuestion.get("title")));
} else if (templatedQuestion.has("html")) {
definition.setQuestionText(templatedQuestion.get("html").asText());
definition.setQuestionText(nodeToString(templatedQuestion.get("html")));
}

if (templatedQuestion.has("isRequired")) {
Expand All @@ -134,6 +161,14 @@ public static SurveyQuestionDefinition unmarshalSurveyQuestion(
return definition;
}

private static String nodeToString(JsonNode node) {
if (node.getNodeType().equals(JsonNodeType.STRING)) {
return node.asText();
} else {
return node.toString();
}
}


public record QuestionReference(String surveyStableId, String questionStableId) {
public static QuestionReference fromString(String reference) {
Expand Down
113 changes: 102 additions & 11 deletions core/src/test/java/bio/terra/pearl/core/util/SurveyParseUtilsTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class SurveyParseUtilsTests extends BaseSpringBootTest {
@Autowired
Expand Down Expand Up @@ -54,7 +56,7 @@ public void testUnmarshalQuestionChoices() throws JsonProcessingException {
String expected = """
[{"stableId":"cardiacStentPlacement","text":"Cardiac stent placement"},{"stableId":"cardiacBypassSurgery","text":"Cardiac bypass surgery"},{"stableId":"noneOfThese","text":"None of these"}]""";

Assertions.assertEquals(expected, actual);
assertEquals(expected, actual);
}

@Test
Expand Down Expand Up @@ -101,7 +103,7 @@ public void testGetChildElements() throws JsonProcessingException {

List<JsonNode> actual = SurveyParseUtils.getAllQuestions(questionNode);

Assertions.assertEquals(2, actual.size());
assertEquals(2, actual.size());
}

@Test
Expand Down Expand Up @@ -134,16 +136,16 @@ public void testGetDynamicPanelElements() throws JsonProcessingException {

List<JsonNode> actual = SurveyParseUtils.getAllQuestions(questionNode);

Assertions.assertEquals(3, actual.size());
assertEquals(3, actual.size());

JsonNode panel = actual.get(0);
JsonNode firstName = actual.get(1);
JsonNode lastName = actual.get(2);


Assertions.assertEquals("examplePanel", panel.get("name").asText());
Assertions.assertEquals("firstName", firstName.get("name").asText());
Assertions.assertEquals("lastName", lastName.get("name").asText());
assertEquals("examplePanel", panel.get("name").asText());
assertEquals("firstName", firstName.get("name").asText());
assertEquals("lastName", lastName.get("name").asText());
}

@Test
Expand Down Expand Up @@ -176,7 +178,7 @@ public void testResolvingQuestionTemplate() throws JsonProcessingException {
String expected = """
[{"stableId":"cardiacStentPlacement","text":"Cardiac stent placement"},{"stableId":"cardiacBypassSurgery","text":"Cardiac bypass surgery"},{"stableId":"noneOfThese","text":"None of these"}]""";

Assertions.assertEquals(expected, actual);
assertEquals(expected, actual);
}

@Test
Expand Down Expand Up @@ -211,7 +213,7 @@ public void testParseQuestionChoicesTranslated(TestInfo info) throws JsonProcess
[{"stableId":"cardiacStentPlacement","text":"Cardiac stent placement"},{"stableId":"cardiacBypassSurgery","text":"Cardiac bypass surgery"},{"stableId":"noneOfThese","text":"None of these"}]
""";

Assertions.assertEquals(expected.strip(), actual.strip());
assertEquals(expected.strip(), actual.strip());
}

@Test
Expand All @@ -231,7 +233,7 @@ public void testResolvingQuestionDropdown() throws JsonProcessingException {
String expected = """
[{"stableId":"foo","text":"foo"},{"stableId":"bar","text":"bar"},{"stableId":"baz","text":"baz"}]""";

Assertions.assertEquals(expected, actual);
assertEquals(expected, actual);
}

@Test
Expand Down Expand Up @@ -363,10 +365,99 @@ public void testParseTitlesUnparseableForm() {
"title": {
"default": "The Basics",
"es"[]]]]]]
""";
""";

Map<String, String> parsedTitles = SurveyParseUtils.parseSurveyTitle(unparseableForm, "FallbackName");
assertThat(parsedTitles, equalTo(Map.of("en", "FallbackName")));
}

@Test
public void testParseCheckboxMultiOtherQuestions() throws JsonProcessingException {
String form = """
{
"elements": [
{
"name": "schools",
"type": "checkbox",
"title": "What schools did you attend?",
"renderAs": "checkbox-multiple-other",
"choices": [
{
"text": "MIT",
"value": "mit",
"otherStableId": "schoolsMitDetail",
"otherText": {
"en": "What did you study at MIT?",
"es": "¿Qué estudiaste en MIT?"
},
"otherPlaceHolder": {
"en": "Your major",
"es": "Tu especialidad"
}
},
{
"text": "Harvard",
"value": "harvard",
"otherStableId": "schoolsHarvardDetail",
"otherText": {
"en": "What did you study at Harvard?",
"es": "¿Qué estudiaste en Harvard?"
},
"otherPlaceHolder": {
"en": "Your major",
"es": "Tu especialidad"
}
},
{
"text": "Northeastern",
"value": "northeastern",
"otherStableId": "schoolsNortheasternDetail",
"otherText": {
"en": "What did you study at Northeastern?",
"es": "¿Qué estudiaste en Northeastern?"
},
"otherPlaceHolder": {
"en": "Your major",
"es": "Tu especialidad"
}
},
{
"text": "Boston University",
"value": "bu",
"otherStableId": "schoolsBuDetail",
"otherText": {
"en": "What did you study at Boston University?",
"es": "¿Qué estudiaste en Boston University?"
},
"otherPlaceHolder": {
"en": "Your major",
"es": "Tu especialidad"
}
},
{
"text": "Other",
"value": "other",
"otherStableId": "schoolsOtherDetail"
}
]
}
]
}
""";

List<JsonNode> questions = SurveyParseUtils.getAllQuestions(objectMapper.readTree(form));

assertThat(questions, hasSize(6));

Set<String> stableIds = questions.stream().map(q -> q.get("name").asText()).collect(Collectors.toSet());
assertEquals(Set.of(
"schools",
"schoolsMitDetail",
"schoolsHarvardDetail",
"schoolsNortheasternDetail",
"schoolsBuDetail",
"schoolsOtherDetail"),
stableIds);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,20 @@ public class BaseSeedPopulator {
"adminUsers/cunningh.json", "adminUsers/andrew.json",
"adminUsers/sampath.json");
public static final List<String> LANGUAGE_TEXTS_TO_POPULATE =
Arrays.asList("i18n/en/languageTexts.json", "i18n/es/languageTexts.json", "i18n/dev/languageTexts.json");
Arrays.asList(
"i18n/de/languageTexts.json",
"i18n/dev/languageTexts.json",
"i18n/en/languageTexts.json",
"i18n/es/languageTexts.json",
"i18n/fr/languageTexts.json",
"i18n/hi/languageTexts.json",
"i18n/it/languageTexts.json",
"i18n/ja/languageTexts.json",
"i18n/pl/languageTexts.json",
"i18n/pt/languageTexts.json",
"i18n/ru/languageTexts.json",
"i18n/tr/languageTexts.json",
"i18n/zh/languageTexts.json");

public BaseSeedPopulator(AdminUserPopulator adminUserPopulator, AdminConfigPopulator adminConfigPopulator,
AdminUserService adminUserService, KitTypePopulator kitTypePopulator,
Expand Down
Loading

0 comments on commit 6563288

Please sign in to comment.