Skip to content

Commit

Permalink
BAH-976 | Fix obs to obs flow sheet bug for form2 ad… (#65)
Browse files Browse the repository at this point in the history
* Siva, Sowmika | MOBN-887 | Fix obs to obs flow sheet bug for form2 add-more

* Siva | BAH-976 | Review suggestions: add test case

* Siva | BAH-976 | Review suggestions: Rename test case
  • Loading branch information
shivarach authored Mar 19, 2020
1 parent 70090be commit 84a9aa6
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ public static List<Obs> filterFormBuilderObs(List<Obs> observations) {
.collect(Collectors.toList()) : Collections.emptyList();
}

public static String getFormNameAlongWithVersion(String formFieldPath) {
public static String getParentFormFieldPath(String formFieldPath) {
return isNotBlank(formFieldPath) && formFieldPath.contains("/")
? formFieldPath.substring(0, formFieldPath.indexOf("/")) : "";
? formFieldPath.substring(0, formFieldPath.lastIndexOf("/")) : "";
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -100,23 +100,30 @@ public void shouldReturnObsWhichHaveFormFieldPath() {
}

@Test
public void shouldReturnFormNameAlongWithVersionForGivenFormFieldPath() {
public void shouldReturnParentFormFieldPathForGivenFormFieldPath() {
String expectedFormNameWithVersion = "FormName.1";
String actualFormNameWithVersion = FormUtil.getFormNameAlongWithVersion("FormName.1/1-0");
String actualFormNameWithVersion = FormUtil.getParentFormFieldPath("FormName.1/1-0");
assertEquals(expectedFormNameWithVersion, actualFormNameWithVersion);
}

@Test
public void shouldReturnParentFormFieldPathForGivenThreeLevelFormFieldPath() {
String expectedFormNameWithVersion = "FormName.1/1-0/2-0";
String actualFormNameWithVersion = FormUtil.getParentFormFieldPath("FormName.1/1-0/2-0/3-0");
assertEquals(expectedFormNameWithVersion, actualFormNameWithVersion);
}

@Test
public void shouldReturnFormNameAlongWithVersionIfGivenFormFieldPathDoesNotHaveSlash() {
String expectedFormNameWithVersion = "";
String actualFormNameWithVersion = FormUtil.getFormNameAlongWithVersion("FormName.1");
String actualFormNameWithVersion = FormUtil.getParentFormFieldPath("FormName.1");
assertEquals(expectedFormNameWithVersion, actualFormNameWithVersion);
}

@Test
public void shouldReturnEmptyStringWhenFormFieldPathIsNull() {
String expectedFormNameWithVersion = "";
String actualFormNameWithVersion = FormUtil.getFormNameAlongWithVersion(null);
String actualFormNameWithVersion = FormUtil.getParentFormFieldPath(null);
assertEquals(expectedFormNameWithVersion, actualFormNameWithVersion);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import java.util.Set;
import java.util.stream.Collectors;

import static org.bahmni.module.bahmnicore.forms2.util.FormUtil.getFormNameAlongWithVersion;
import static org.bahmni.module.bahmnicore.forms2.util.FormUtil.getParentFormFieldPath;

@Component
public class BahmniFormBuilderObsToTabularViewMapper extends BahmniObservationsToTabularViewMapper {
Expand All @@ -31,13 +31,13 @@ public PivotTable constructTable(Set<EncounterTransaction.Concept> concepts,
return pivotTable;
}

public List<PivotRow> getNonEmptyRows(List<PivotRow> rows, String groupByConceptName){
public List<PivotRow> getNonEmptyRows(List<PivotRow> rows, String groupByConceptName) {
return rows.stream().filter(row -> isNonNullRow(groupByConceptName, row)).collect(Collectors.toList());
}

private List<PivotRow> constructRows(Set<EncounterTransaction.Concept> concepts,
Collection<BahmniObservation> bahmniObservations, String groupByConceptName) {
Map<String, List<BahmniObservation>> rowsMapper = getRowsMapper(bahmniObservations);
Map<String, List<BahmniObservation>> rowsMapper = getRowsMapper(bahmniObservations, groupByConceptName);
List<PivotRow> rows = new ArrayList<>();
rowsMapper.forEach((rowIdentifier, rowObservations) -> {
PivotRow row = new PivotRow();
Expand All @@ -49,19 +49,42 @@ private List<PivotRow> constructRows(Set<EncounterTransaction.Concept> concepts,
return rows;
}

private Map<String, List<BahmniObservation>> getRowsMapper(Collection<BahmniObservation> bahmniObservations) {
private Map<String, List<BahmniObservation>> getRowsMapper(Collection<BahmniObservation> bahmniObservations,
String groupByConceptName) {
final Map<String, List<BahmniObservation>> obsRows = prepareMapWithRowIdentifier(bahmniObservations,
groupByConceptName);
for (BahmniObservation observation : bahmniObservations) {
final String currentObsRowIdentifier = getRowIdentifier(observation);
for (String rowIdentifier : obsRows.keySet()) {
if (currentObsRowIdentifier.startsWith(rowIdentifier)) {
obsRows.get(rowIdentifier).add(observation);
break;
}
}
}
return obsRows;
}

// Observation rows are distinguished by encounter uuid and obs parent formFieldPath
private String getRowIdentifier(BahmniObservation bahmniObservation) {
return bahmniObservation.getEncounterUuid() + getParentFormFieldPath(bahmniObservation.getFormFieldPath());
}


private Map<String, List<BahmniObservation>> prepareMapWithRowIdentifier(Collection<BahmniObservation> bahmniObservations,
String groupByConceptName) {
List<BahmniObservation> groupByConceptObservations = getGroupByConceptObservations(bahmniObservations,
groupByConceptName);
Map<String, List<BahmniObservation>> rowsMapper = new LinkedHashMap<>();
bahmniObservations.forEach(bahmniObservation -> {
String rowIdentifier = getRowIdentifier(bahmniObservation);
List<BahmniObservation> bahmniObs;
bahmniObs = rowsMapper.containsKey(rowIdentifier) ? rowsMapper.get(rowIdentifier) : new ArrayList<>();
bahmniObs.add(bahmniObservation);
rowsMapper.put(rowIdentifier, bahmniObs);
});
groupByConceptObservations.forEach(observation
-> rowsMapper.put(getRowIdentifier(observation), new ArrayList<>()));
return rowsMapper;
}

private String getRowIdentifier(BahmniObservation bahmniObservation) {
return bahmniObservation.getEncounterUuid() + getFormNameAlongWithVersion(bahmniObservation.getFormFieldPath());
private List<BahmniObservation> getGroupByConceptObservations(Collection<BahmniObservation> bahmniObservations,
String groupByConceptName) {
return bahmniObservations.stream()
.filter(observation -> observation.getConcept().getName().equals(groupByConceptName))
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -250,4 +250,128 @@ public void shouldReturnPivotTableForMultiSelectObs() {
assertThat(firstRowColumns.get(multiSelectConceptName),
containsInAnyOrder(multiSelectFirstObs, multiSelectSecondObs));
}

@Test
public void shouldReturnPivotTableWithTwoRowsDifferentiatedByEncounterUUIDAndParentFormFieldPathsWhenAddMoreSectionHasAllConceptsIncludingGroupByConcept() {

String groupByConceptName = "id";
String weightConceptName = "weight";

Concept groupByConcept = new Concept();
Concept weightConcept = new Concept();
groupByConcept.setUuid("group-concept-uuid");
groupByConcept.setName(groupByConceptName);
weightConcept.setUuid("weight-concept-uuid");
weightConcept.setName(weightConceptName);

BahmniObservation idObservation = new BahmniObservation();
BahmniObservation weightObservation = new BahmniObservation();
idObservation.setConcept(groupByConcept);
weightObservation.setConcept(weightConcept);
weightObservation.setValue("obs value");
idObservation.setValue("1");
String encounterUuid = "encounter-uuid";
idObservation.setEncounterUuid(encounterUuid);
idObservation.setFormFieldPath("MedicalForm.10/1-0/2-0");
weightObservation.setEncounterUuid(encounterUuid);
weightObservation.setFormFieldPath("MedicalForm.10/1-0/3-0");

BahmniObservation anotherIdObservation = new BahmniObservation();
BahmniObservation anotherWeightObservation = new BahmniObservation();
anotherIdObservation.setConcept(groupByConcept);
anotherWeightObservation.setConcept(weightConcept);
anotherWeightObservation.setValue("another obs value");
anotherIdObservation.setValue(1);
anotherIdObservation.setEncounterUuid(encounterUuid);
anotherIdObservation.setFormFieldPath("MedicalForm.10/1-1/2-0");
anotherWeightObservation.setUuid(encounterUuid);
anotherWeightObservation.setFormFieldPath("MedicalForm.10/1-1/3-0");
anotherIdObservation.setEncounterUuid(encounterUuid);
anotherWeightObservation.setEncounterUuid(encounterUuid);

HashSet<Concept> concepts = new HashSet<>(asList(groupByConcept, weightConcept));
List<BahmniObservation> bahmniObservations = asList(idObservation, weightObservation, anotherIdObservation,
anotherWeightObservation);

PivotTable pivotTable = bahmniFormBuilderObsToTabularViewMapper.constructTable(concepts, bahmniObservations,
groupByConceptName);

assertEquals(2, pivotTable.getHeaders().size());
final List<PivotRow> rows = pivotTable.getRows();
assertEquals(2, rows.size());
assertEquals(2, rows.get(0).getColumns().size());
assertEquals(2, rows.get(1).getColumns().size());

final Map<String, ArrayList<BahmniObservation>> firstColumn = rows.get(0).getColumns();
final Map<String, ArrayList<BahmniObservation>> secondColumn = rows.get(1).getColumns();

final List<Object> actualFirstRow = asList(firstColumn.get(groupByConceptName).get(0),
firstColumn.get(weightConceptName).get(0));

final List<Object> actualSecondRow = asList(secondColumn.get(groupByConceptName).get(0),
secondColumn.get(weightConceptName).get(0));

List expectedFirstRow = asList(idObservation, weightObservation);
List expectedSecondRow = asList(anotherIdObservation, anotherWeightObservation);


assertTrue(expectedFirstRow.containsAll(actualFirstRow)
|| expectedFirstRow.containsAll(actualSecondRow));
assertTrue(expectedSecondRow.containsAll(actualFirstRow)
|| expectedSecondRow.containsAll(actualSecondRow));
}

@Test
public void shouldReturnPivotTableWithOneRowWhenAddMoreSectionHasAllConceptsExceptGroupByConcept() {
String groupByConceptName = "id";
String weightConceptName = "weight";

Concept groupByConcept = new Concept();
Concept weightConcept = new Concept();
groupByConcept.setUuid("group-concept-uuid");
groupByConcept.setName(groupByConceptName);
weightConcept.setUuid("weight-concept-uuid");
weightConcept.setName(weightConceptName);

BahmniObservation idObservation = new BahmniObservation();
BahmniObservation weightObservation = new BahmniObservation();
idObservation.setConcept(groupByConcept);
weightObservation.setConcept(weightConcept);
weightObservation.setValue("obs value");
idObservation.setValue("1");
String encounterUuid = "encounter-uuid";
idObservation.setEncounterUuid(encounterUuid);
idObservation.setFormFieldPath("MedicalForm.10/1-0");
weightObservation.setEncounterUuid(encounterUuid);
weightObservation.setFormFieldPath("MedicalForm.10/2-0/3-0");

BahmniObservation anotherWeightObservation = new BahmniObservation();
anotherWeightObservation.setConcept(weightConcept);
anotherWeightObservation.setValue("another obs value");
anotherWeightObservation.setUuid(encounterUuid);
anotherWeightObservation.setFormFieldPath("MedicalForm.10/2-1/3-0");
anotherWeightObservation.setEncounterUuid(encounterUuid);

HashSet<Concept> concepts = new HashSet<>(asList(groupByConcept, weightConcept));
List<BahmniObservation> bahmniObservations = asList(idObservation, weightObservation,
anotherWeightObservation);

PivotTable pivotTable = bahmniFormBuilderObsToTabularViewMapper.constructTable(concepts, bahmniObservations,
groupByConceptName);

assertEquals(2, pivotTable.getHeaders().size());
final List<PivotRow> rows = pivotTable.getRows();
assertEquals(1, rows.size());
assertEquals(2, rows.get(0).getColumns().size());

final Map<String, ArrayList<BahmniObservation>> columns = rows.get(0).getColumns();

final List<Object> actualRow = asList(columns.get(groupByConceptName).get(0),
columns.get(weightConceptName).get(0), columns.get(weightConceptName).get(1));

List expectedRow = asList(idObservation, weightObservation, anotherWeightObservation);


assertTrue(expectedRow.containsAll(actualRow));
}
}

0 comments on commit 84a9aa6

Please sign in to comment.