Program: 188375
Project: 188375
Buggy source:
public static DescriptiveMetadataEditBundle retrieveDescriptiveMetadataEditBundle(User user,IndexedAIP aip,IndexedRepresentation representation,String descriptiveMetadataId,String type,String version,final Locale locale) throws GenericException, RequestNotValidException, NotFoundException, AuthorizationDeniedException {
  DescriptiveMetadataEditBundle ret;
  InputStream inputStream=null;
  try {
    String representationId=representation != null ? representation.getId() : null;
    Binary binary=RodaCoreFactory.getModelService().retrieveDescriptiveMetadataBinary(aip.getId(),representationId,descriptiveMetadataId);
    inputStream=binary.getContent().createInputStream();
    String xml=IOUtils.toString(inputStream,"String_Node_Str");
    SupportedMetadataTypeBundle metadataTypeBundle=null;
    List<SupportedMetadataTypeBundle> supportedMetadataTypeBundles=BrowserHelper.retrieveSupportedMetadata(user,aip,representation,locale);
    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=retrieveDescriptiveMetadataPreview(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) {
          LOGGER.warn("String_Node_Str",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 retrieveDescriptiveMetadataEditBundle(User user,IndexedAIP aip,IndexedRepresentation representation,String descriptiveMetadataId,String type,String version,final Locale locale) throws GenericException, RequestNotValidException, NotFoundException, AuthorizationDeniedException {
  DescriptiveMetadataEditBundle ret;
  InputStream inputStream=null;
  try {
    String representationId=representation != null ? representation.getId() : null;
    Binary binary=RodaCoreFactory.getModelService().retrieveDescriptiveMetadataBinary(aip.getId(),representationId,descriptiveMetadataId);
    inputStream=binary.getContent().createInputStream();
    String xml=IOUtils.toString(inputStream,"String_Node_Str");
    SupportedMetadataTypeBundle metadataTypeBundle=null;
    List<SupportedMetadataTypeBundle> supportedMetadataTypeBundles=BrowserHelper.retrieveSupportedMetadata(user,aip,representation,locale);
    for (    SupportedMetadataTypeBundle typeBundle : supportedMetadataTypeBundles) {
      if (typeBundle.getType() != null && typeBundle.getType().equalsIgnoreCase(type)) {
        if (typeBundle.getVersion().equals(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=retrieveDescriptiveMetadataPreview(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) {
          LOGGER.warn("String_Node_Str",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;
}