Program: 188645
Project: 188645
Buggy source:
public static DescriptiveMetadataEditBundle getDescriptiveMetadataEditBundle(RodaUser user,IndexedAIP aip,String descriptiveMetadataId,String type,String version) throws GenericException, RequestNotValidException, NotFoundException, AuthorizationDeniedException {
  DescriptiveMetadataEditBundle ret;
  InputStream inputStream=null;
  try {
    Binary binary=RodaCoreFactory.getModelService().retrieveDescriptiveMetadataBinary(aip.getId(),descriptiveMetadataId);
    inputStream=binary.getContent().createInputStream();
    String xml=IOUtils.toString(inputStream,"String_Node_Str");
    SupportedMetadataTypeBundle metadataTypeBundle=null;
    List<SupportedMetadataTypeBundle> supportedMetadataTypeBundles=BrowserHelper.getSupportedMetadata(user,aip,Locale.getDefault());
    for (    SupportedMetadataTypeBundle typeBundle : supportedMetadataTypeBundles) {
      if (typeBundle.getType() != null && typeBundle.getType().equalsIgnoreCase(type)) {
        if (typeBundle.getVersion() == version || (typeBundle.getVersion() != null && typeBundle.getVersion().equalsIgnoreCase(version))) {
          metadataTypeBundle=typeBundle;
          break;
        }
      }
    }
    boolean similar=false;
    Set<MetadataValue> values=null;
    String template=null;
    if (metadataTypeBundle != null) {
      values=metadataTypeBundle.getValues();
      template=metadataTypeBundle.getTemplate();
      for (      MetadataValue mv : values) {
        String xpathRaw=mv.get("String_Node_Str");
        if (xpathRaw != null && xpathRaw.length() > 0) {
          String[] xpaths=xpathRaw.split("String_Node_Str");
          String value;
          List<String> allValues=new ArrayList<>();
          for (          String xpath : xpaths) {
            allValues.addAll(ServerTools.applyXpath(xml,xpath));
          }
          boolean allEqual=allValues.stream().allMatch(s -> s.trim().equals(allValues.get(0).trim()));
          if (allEqual && !allValues.isEmpty()) {
            value=allValues.get(0);
          }
 else {
            value=String.join("String_Node_Str",allValues);
          }
          mv.set("String_Node_Str",value.trim());
        }
      }
      metadataTypeBundle.setValues(values);
      String templateWithValues=getDescriptiveMetadataPreview(metadataTypeBundle);
      try {
        XMLUnit.setIgnoreComments(true);
        XMLUnit.setIgnoreWhitespace(true);
        XMLUnit.setIgnoreAttributeOrder(true);
        XMLUnit.setCompareUnmatched(false);
        Diff xmlDiff=new Diff(xml,templateWithValues);
        xmlDiff.overrideDifferenceListener(new XMLSimilarityIgnoreElements("String_Node_Str"));
        similar=xmlDiff.identical() || xmlDiff.similar();
      }
 catch (      SAXException e) {
      }
    }
    ret=new DescriptiveMetadataEditBundle(descriptiveMetadataId,type,version,xml,template,values,similar);
  }
 catch (  IOException e) {
    throw new GenericException("String_Node_Str" + e.getMessage());
  }
 finally {
    IOUtils.closeQuietly(inputStream);
  }
  return ret;
}

Fixed version (ground truth):
public static DescriptiveMetadataEditBundle getDescriptiveMetadataEditBundle(RodaUser user,IndexedAIP aip,String descriptiveMetadataId,String type,String version) throws GenericException, RequestNotValidException, NotFoundException, AuthorizationDeniedException {
  DescriptiveMetadataEditBundle ret;
  InputStream inputStream=null;
  try {
    Binary binary=RodaCoreFactory.getModelService().retrieveDescriptiveMetadataBinary(aip.getId(),descriptiveMetadataId);
    inputStream=binary.getContent().createInputStream();
    String xml=IOUtils.toString(inputStream,"String_Node_Str");
    SupportedMetadataTypeBundle metadataTypeBundle=null;
    List<SupportedMetadataTypeBundle> supportedMetadataTypeBundles=BrowserHelper.getSupportedMetadata(user,aip,Locale.getDefault());
    for (    SupportedMetadataTypeBundle typeBundle : supportedMetadataTypeBundles) {
      if (typeBundle.getType() != null && typeBundle.getType().equalsIgnoreCase(type)) {
        if (typeBundle.getVersion() == version || (typeBundle.getVersion() != null && typeBundle.getVersion().equalsIgnoreCase(version))) {
          metadataTypeBundle=typeBundle;
          break;
        }
      }
    }
    boolean similar=false;
    Set<MetadataValue> values=null;
    String template=null;
    if (metadataTypeBundle != null) {
      values=metadataTypeBundle.getValues();
      template=metadataTypeBundle.getTemplate();
      if (values != null) {
        for (        MetadataValue mv : values) {
          String xpathRaw=mv.get("String_Node_Str");
          if (xpathRaw != null && xpathRaw.length() > 0) {
            String[] xpaths=xpathRaw.split("String_Node_Str");
            String value;
            List<String> allValues=new ArrayList<>();
            for (            String xpath : xpaths) {
              allValues.addAll(ServerTools.applyXpath(xml,xpath));
            }
            boolean allEqual=allValues.stream().allMatch(s -> s.trim().equals(allValues.get(0).trim()));
            if (allEqual && !allValues.isEmpty()) {
              value=allValues.get(0);
            }
 else {
              value=String.join("String_Node_Str",allValues);
            }
            mv.set("String_Node_Str",value.trim());
          }
        }
        metadataTypeBundle.setValues(values);
        String templateWithValues=getDescriptiveMetadataPreview(metadataTypeBundle);
        try {
          XMLUnit.setIgnoreComments(true);
          XMLUnit.setIgnoreWhitespace(true);
          XMLUnit.setIgnoreAttributeOrder(true);
          XMLUnit.setCompareUnmatched(false);
          Diff xmlDiff=new Diff(xml,templateWithValues);
          xmlDiff.overrideDifferenceListener(new XMLSimilarityIgnoreElements("String_Node_Str"));
          similar=xmlDiff.identical() || xmlDiff.similar();
        }
 catch (        SAXException e) {
        }
      }
    }
    ret=new DescriptiveMetadataEditBundle(descriptiveMetadataId,type,version,xml,template,values,similar);
  }
 catch (  IOException e) {
    throw new GenericException("String_Node_Str" + e.getMessage());
  }
 finally {
    IOUtils.closeQuietly(inputStream);
  }
  return ret;
}