Program: 86793
Project: 86793
Buggy source:
/** 
 * @see {@link EncounterAndObsDataSetEvaluator#evaluate(DataSetDefinition,EvaluationContext)}
 */
@Test @Verifies(value="String_Node_Str",method="String_Node_Str") public void evaluate_shouldEvaluateAnEncounterAndObsDataSetDefinitionAllEncounterObsValuesWithOptionalColumns() throws Exception {
  executeDataSet(XML_ENCOUNTER_DATASET);
  executeDataSet(XML_OBS_GROUP_DATASET);
  executeDataSet(XML_FORM_DATASET);
  Form form=Context.getFormService().getForm(2);
  Encounter encounter1=Context.getEncounterService().getEncounter(13);
  Encounter encounter2=Context.getEncounterService().getEncounter(14);
  encounter1.setForm(form);
  encounter2.setForm(form);
  EncounterAndObsDataSetDefinition d=new EncounterAndObsDataSetDefinition();
  d.setForms(Collections.singletonList(form));
  d.setColumnDisplayFormat(Collections.singletonList(EncounterAndObsDataSetDefinition.ColumnDisplayFormat.ID));
  List<EncounterAndObsDataSetDefinition.ObsOptionalColumn> optionalColumns=new ArrayList<EncounterAndObsDataSetDefinition.ObsOptionalColumn>();
  optionalColumns.add(EncounterAndObsDataSetDefinition.ObsOptionalColumn.ACCESSION_NUMBER);
  optionalColumns.add(EncounterAndObsDataSetDefinition.ObsOptionalColumn.VALUE_MODIFIER);
  optionalColumns.add(EncounterAndObsDataSetDefinition.ObsOptionalColumn.COMMENT);
  d.setOptionalColumns(optionalColumns);
  Integer maxColumnHeaderWidth=d.getMaxColumnHeaderWidth();
  SimpleDataSet result=(SimpleDataSet)Context.getService(DataSetDefinitionService.class).evaluate(d,null);
  for (  DataSetRow row : result.getRows()) {
    Integer encounterId=(Integer)row.getColumnValue("String_Node_Str");
    Encounter e=Context.getEncounterService().getEncounter(encounterId);
    List<String> obsValues=new ArrayList<String>();
    for (    DataSetColumn dsc : row.getColumnValues().keySet()) {
      Object obsValue=row.getColumnValue(dsc);
      if (obsValue != null && obsValue != "String_Node_Str") {
        obsValues.add(obsValue.toString());
      }
    }
    for (    Obs obs : e.getObs()) {
      Concept obsCodedValue=obs.getValueCoded();
      String obsNonCodedValue=obs.getValueAsString(Context.getLocale());
      if ((obsCodedValue != null) || (obsNonCodedValue != null && StringUtils.isNotEmpty(obsNonCodedValue))) {
        if (obsCodedValue != null && obsValues.contains(obsCodedValue.toString())) {
          boolean obsRemoved=obsValues.remove(obsCodedValue.toString());
          Assert.assertTrue(obsRemoved);
        }
 else         if (obsValues.contains(obsNonCodedValue)) {
          boolean obsRemoved=obsValues.remove(obsNonCodedValue);
          Assert.assertTrue(obsRemoved);
        }
 else {
          Assert.assertFalse(true);
        }
        boolean obsGroupRemoved=false;
        if (obs.getObsGroup() != null) {
          obsGroupRemoved=obsValues.remove(obs.getObsGroup().getId().toString());
        }
 else {
          obsGroupRemoved=true;
        }
        boolean obsValueModRemoved=false;
        if (obs.getValueModifier() != null) {
          obsValueModRemoved=obsValues.remove(obs.getValueModifier());
        }
 else {
          obsValueModRemoved=true;
        }
        boolean obsDateRemoved=false;
        if (obs.getObsDatetime() != null) {
          obsDateRemoved=obsValues.remove(Context.getDateFormat().format(obs.getObsDatetime()));
        }
 else {
          obsDateRemoved=true;
        }
        boolean obsAccessionRemoved=false;
        if (obs.getAccessionNumber() != null) {
          obsAccessionRemoved=obsValues.remove(obs.getAccessionNumber());
        }
 else {
          obsAccessionRemoved=true;
        }
        boolean obsCommentRemoved=false;
        if (obs.getComment() != null) {
          obsCommentRemoved=obsValues.remove(obs.getComment());
        }
 else {
          obsCommentRemoved=true;
        }
        Assert.assertTrue(obsDateRemoved && obsGroupRemoved && obsValueModRemoved&& obsAccessionRemoved&& obsCommentRemoved);
      }
    }
    Assert.assertTrue(obsValues.size() == 5);
    Assert.assertEquals(ObjectUtil.trimStringIfNeeded(e.getEncounterId().toString(),maxColumnHeaderWidth),ObjectUtil.trimStringIfNeeded(row.getColumnValue("String_Node_Str").toString(),maxColumnHeaderWidth));
    Assert.assertEquals(ObjectUtil.trimStringIfNeeded(e.getEncounterDatetime().toString(),maxColumnHeaderWidth),ObjectUtil.trimStringIfNeeded(row.getColumnValue("String_Node_Str").toString(),maxColumnHeaderWidth));
    Assert.assertEquals(ObjectUtil.trimStringIfNeeded(e.getLocation().toString(),maxColumnHeaderWidth),ObjectUtil.trimStringIfNeeded(row.getColumnValue("String_Node_Str").toString(),maxColumnHeaderWidth));
    Assert.assertEquals(ObjectUtil.trimStringIfNeeded(e.getProvider().getPersonName().toString(),maxColumnHeaderWidth),ObjectUtil.trimStringIfNeeded(row.getColumnValue("String_Node_Str").toString(),maxColumnHeaderWidth));
    Assert.assertEquals(ObjectUtil.trimStringIfNeeded(e.getPatientId().toString(),maxColumnHeaderWidth),ObjectUtil.trimStringIfNeeded(row.getColumnValue("String_Node_Str").toString(),maxColumnHeaderWidth));
  }
}

Fixed version (ground truth):
/** 
 * @see {@link EncounterAndObsDataSetEvaluator#evaluate(DataSetDefinition,EvaluationContext)}
 */
@Test @Verifies(value="String_Node_Str",method="String_Node_Str") public void evaluate_shouldEvaluateAnEncounterAndObsDataSetDefinitionAllEncounterObsValuesWithOptionalColumns() throws Exception {
  executeDataSet(XML_ENCOUNTER_DATASET);
  executeDataSet(XML_OBS_GROUP_DATASET);
  executeDataSet(XML_FORM_DATASET);
  Form form=Context.getFormService().getForm(2);
  Encounter encounter1=Context.getEncounterService().getEncounter(13);
  Encounter encounter2=Context.getEncounterService().getEncounter(14);
  encounter1.setForm(form);
  encounter2.setForm(form);
  EncounterAndObsDataSetDefinition d=new EncounterAndObsDataSetDefinition();
  d.setForms(Collections.singletonList(form));
  d.setColumnDisplayFormat(Collections.singletonList(EncounterAndObsDataSetDefinition.ColumnDisplayFormat.ID));
  List<EncounterAndObsDataSetDefinition.ObsOptionalColumn> optionalColumns=new ArrayList<EncounterAndObsDataSetDefinition.ObsOptionalColumn>();
  optionalColumns.add(EncounterAndObsDataSetDefinition.ObsOptionalColumn.ACCESSION_NUMBER);
  optionalColumns.add(EncounterAndObsDataSetDefinition.ObsOptionalColumn.VALUE_MODIFIER);
  optionalColumns.add(EncounterAndObsDataSetDefinition.ObsOptionalColumn.COMMENT);
  d.setOptionalColumns(optionalColumns);
  Integer maxColumnHeaderWidth=d.getMaxColumnHeaderWidth();
  SimpleDataSet result=(SimpleDataSet)Context.getService(DataSetDefinitionService.class).evaluate(d,null);
  for (  DataSetRow row : result.getRows()) {
    Integer encounterId=(Integer)row.getColumnValue("String_Node_Str");
    Encounter e=Context.getEncounterService().getEncounter(encounterId);
    List<String> obsValues=new ArrayList<String>();
    for (    DataSetColumn dsc : row.getColumnValues().keySet()) {
      Object obsValue=row.getColumnValue(dsc);
      if (obsValue != null && obsValue != "String_Node_Str") {
        obsValues.add(obsValue.toString());
      }
    }
    for (    Obs obs : e.getObs()) {
      Concept obsCodedValue=obs.getValueCoded();
      String obsNonCodedValue=obs.getValueAsString(Context.getLocale());
      if ((obsCodedValue != null) || (obsNonCodedValue != null && StringUtils.isNotEmpty(obsNonCodedValue))) {
        if (obsCodedValue != null && obsValues.contains(obsCodedValue.toString())) {
          boolean obsRemoved=obsValues.remove(obsCodedValue.toString());
          Assert.assertTrue(obsRemoved);
        }
 else         if (obsValues.contains(obsNonCodedValue)) {
          boolean obsRemoved=obsValues.remove(obsNonCodedValue);
          Assert.assertTrue(obsRemoved);
        }
 else {
          Assert.assertFalse(true);
        }
        boolean obsGroupRemoved=false;
        if (obs.getObsGroup() != null) {
          obsGroupRemoved=obsValues.remove(obs.getObsGroup().getId().toString());
        }
 else {
          obsGroupRemoved=true;
        }
        boolean obsValueModRemoved=false;
        if (obs.getValueModifier() != null) {
          obsValueModRemoved=obsValues.remove(obs.getValueModifier());
        }
 else {
          obsValueModRemoved=true;
        }
        boolean obsDateRemoved=false;
        if (obs.getObsDatetime() != null) {
          obsDateRemoved=obsValues.remove(obs.getObsDatetime().toString());
        }
 else {
          obsDateRemoved=true;
        }
        boolean obsAccessionRemoved=false;
        if (obs.getAccessionNumber() != null) {
          obsAccessionRemoved=obsValues.remove(obs.getAccessionNumber());
        }
 else {
          obsAccessionRemoved=true;
        }
        boolean obsCommentRemoved=false;
        if (obs.getComment() != null) {
          obsCommentRemoved=obsValues.remove(obs.getComment());
        }
 else {
          obsCommentRemoved=true;
        }
        Assert.assertTrue(obsDateRemoved && obsGroupRemoved && obsValueModRemoved&& obsAccessionRemoved&& obsCommentRemoved);
      }
    }
    Assert.assertTrue(obsValues.size() == 5);
    Assert.assertEquals(ObjectUtil.trimStringIfNeeded(e.getEncounterId().toString(),maxColumnHeaderWidth),ObjectUtil.trimStringIfNeeded(row.getColumnValue("String_Node_Str").toString(),maxColumnHeaderWidth));
    Assert.assertEquals(ObjectUtil.trimStringIfNeeded(e.getEncounterDatetime().toString(),maxColumnHeaderWidth),ObjectUtil.trimStringIfNeeded(row.getColumnValue("String_Node_Str").toString(),maxColumnHeaderWidth));
    Assert.assertEquals(ObjectUtil.trimStringIfNeeded(e.getLocation().toString(),maxColumnHeaderWidth),ObjectUtil.trimStringIfNeeded(row.getColumnValue("String_Node_Str").toString(),maxColumnHeaderWidth));
    Assert.assertEquals(ObjectUtil.trimStringIfNeeded(e.getProvider().getPersonName().toString(),maxColumnHeaderWidth),ObjectUtil.trimStringIfNeeded(row.getColumnValue("String_Node_Str").toString(),maxColumnHeaderWidth));
    Assert.assertEquals(ObjectUtil.trimStringIfNeeded(e.getPatientId().toString(),maxColumnHeaderWidth),ObjectUtil.trimStringIfNeeded(row.getColumnValue("String_Node_Str").toString(),maxColumnHeaderWidth));
  }
}