Program: 171033
Project: 171033
Buggy source:
@Override @GraphTransaction public AtlasSearchResult searchRelatedEntities(String guid,String relation,String sortByAttributeName,SortOrder sortOrder,boolean excludeDeletedEntities,int limit,int offset) throws AtlasBaseException {
  AtlasSearchResult ret=new AtlasSearchResult(AtlasQueryType.RELATIONSHIP);
  if (StringUtils.isEmpty(guid) || StringUtils.isEmpty(relation)) {
    throw new AtlasBaseException(AtlasErrorCode.INVALID_PARAMETERS,"String_Node_Str" + guid + "String_Node_Str"+ relation+ "String_Node_Str");
  }
  AtlasVertex entityVertex=entityRetriever.getEntityVertex(guid);
  String entityTypeName=GraphHelper.getTypeName(entityVertex);
  AtlasEntityType entityType=typeRegistry.getEntityTypeByName(entityTypeName);
  if (entityType == null) {
    throw new AtlasBaseException(AtlasErrorCode.INVALID_RELATIONSHIP_TYPE,entityTypeName,guid);
  }
  AtlasAttribute attribute=entityType.getAttribute(relation);
  if (attribute != null) {
    if (isRelationshipAttribute(attribute)) {
      relation=EDGE_LABEL_PREFIX + attribute.getQualifiedName();
    }
 else {
      throw new AtlasBaseException(AtlasErrorCode.INVALID_RELATIONSHIP_ATTRIBUTE,relation,attribute.getTypeName());
    }
  }
  if (StringUtils.isEmpty(sortByAttributeName)) {
    sortByAttributeName=DEFAULT_SORT_ATTRIBUTE_NAME;
  }
  AtlasAttribute sortByAttribute=entityType.getAttribute(sortByAttributeName);
  if (sortByAttribute == null) {
    sortByAttributeName=null;
    sortOrder=null;
  }
 else {
    sortByAttributeName=sortByAttribute.getQualifiedName();
    if (sortOrder == null) {
      sortOrder=ASCENDING;
    }
  }
  QueryParams params=validateSearchParams(limit,offset);
  ScriptEngine scriptEngine=graph.getGremlinScriptEngine();
  Bindings bindings=scriptEngine.createBindings();
  Set<String> states=getEntityStates();
  String relatedEntitiesQuery=gremlinQueryProvider.getQuery(RELATIONSHIP_SEARCH);
  if (excludeDeletedEntities) {
    states.remove(DELETED.toString());
  }
  if (sortOrder == ASCENDING) {
    relatedEntitiesQuery+=gremlinQueryProvider.getQuery(RELATIONSHIP_SEARCH_ASCENDING_SORT);
    bindings.put("String_Node_Str",sortByAttributeName);
  }
 else   if (sortOrder == DESCENDING) {
    relatedEntitiesQuery+=gremlinQueryProvider.getQuery(RELATIONSHIP_SEARCH_DESCENDING_SORT);
    bindings.put("String_Node_Str",sortByAttributeName);
  }
  relatedEntitiesQuery+=gremlinQueryProvider.getQuery(TO_RANGE_LIST);
  bindings.put("String_Node_Str",graph);
  bindings.put("String_Node_Str",guid);
  bindings.put("String_Node_Str",relation);
  bindings.put("String_Node_Str",Collections.unmodifiableSet(states));
  bindings.put("String_Node_Str",params.offset());
  bindings.put("String_Node_Str",params.offset() + params.limit());
  try {
    Object result=graph.executeGremlinScript(scriptEngine,bindings,relatedEntitiesQuery,false);
    if (result instanceof List && CollectionUtils.isNotEmpty((List)result)) {
      List<?> queryResult=(List)result;
      Object firstElement=queryResult.get(0);
      if (firstElement instanceof AtlasVertex) {
        List<AtlasVertex> vertices=(List<AtlasVertex>)queryResult;
        List<AtlasEntityHeader> resultList=new ArrayList<>(vertices.size());
        for (        AtlasVertex vertex : vertices) {
          resultList.add(entityRetriever.toAtlasEntityHeader(vertex));
        }
        ret.setEntities(resultList);
      }
    }
    if (ret.getEntities() == null) {
      ret.setEntities(new ArrayList<>());
    }
  }
 catch (  ScriptException e) {
    if (LOG.isDebugEnabled()) {
      LOG.debug("String_Node_Str",relatedEntitiesQuery,e);
    }
    throw new AtlasBaseException(AtlasErrorCode.INTERNAL_ERROR,"String_Node_Str");
  }
 finally {
    graph.releaseGremlinScriptEngine(scriptEngine);
  }
  return ret;
}

Fixed version (ground truth):
@Override @GraphTransaction public AtlasSearchResult searchRelatedEntities(String guid,String relation,String sortByAttributeName,SortOrder sortOrder,boolean excludeDeletedEntities,int limit,int offset) throws AtlasBaseException {
  AtlasSearchResult ret=new AtlasSearchResult(AtlasQueryType.RELATIONSHIP);
  if (StringUtils.isEmpty(guid) || StringUtils.isEmpty(relation)) {
    throw new AtlasBaseException(AtlasErrorCode.INVALID_PARAMETERS,"String_Node_Str" + guid + "String_Node_Str"+ relation+ "String_Node_Str");
  }
  AtlasVertex entityVertex=entityRetriever.getEntityVertex(guid);
  String entityTypeName=GraphHelper.getTypeName(entityVertex);
  AtlasEntityType entityType=typeRegistry.getEntityTypeByName(entityTypeName);
  if (entityType == null) {
    throw new AtlasBaseException(AtlasErrorCode.INVALID_RELATIONSHIP_TYPE,entityTypeName,guid);
  }
  AtlasAttribute attribute=entityType.getAttribute(relation);
  if (attribute != null) {
    if (isRelationshipAttribute(attribute)) {
      relation=EDGE_LABEL_PREFIX + attribute.getQualifiedName();
    }
 else {
      throw new AtlasBaseException(AtlasErrorCode.INVALID_RELATIONSHIP_ATTRIBUTE,relation,attribute.getTypeName());
    }
  }
  if (StringUtils.isEmpty(sortByAttributeName)) {
    sortByAttributeName=DEFAULT_SORT_ATTRIBUTE_NAME;
  }
  AtlasAttribute sortByAttribute=entityType.getAttribute(sortByAttributeName);
  if (sortByAttribute == null) {
    sortByAttributeName=null;
    sortOrder=null;
  }
 else {
    sortByAttributeName=sortByAttribute.getQualifiedName();
    if (sortOrder == null) {
      sortOrder=ASCENDING;
    }
  }
  QueryParams params=QueryParams.getNormalizedParams(limit,offset);
  ScriptEngine scriptEngine=graph.getGremlinScriptEngine();
  Bindings bindings=scriptEngine.createBindings();
  Set<String> states=getEntityStates();
  String relatedEntitiesQuery=gremlinQueryProvider.getQuery(RELATIONSHIP_SEARCH);
  if (excludeDeletedEntities) {
    states.remove(DELETED.toString());
  }
  if (sortOrder == ASCENDING) {
    relatedEntitiesQuery+=gremlinQueryProvider.getQuery(RELATIONSHIP_SEARCH_ASCENDING_SORT);
    bindings.put("String_Node_Str",sortByAttributeName);
  }
 else   if (sortOrder == DESCENDING) {
    relatedEntitiesQuery+=gremlinQueryProvider.getQuery(RELATIONSHIP_SEARCH_DESCENDING_SORT);
    bindings.put("String_Node_Str",sortByAttributeName);
  }
  relatedEntitiesQuery+=gremlinQueryProvider.getQuery(TO_RANGE_LIST);
  bindings.put("String_Node_Str",graph);
  bindings.put("String_Node_Str",guid);
  bindings.put("String_Node_Str",relation);
  bindings.put("String_Node_Str",Collections.unmodifiableSet(states));
  bindings.put("String_Node_Str",params.offset());
  bindings.put("String_Node_Str",params.offset() + params.limit());
  try {
    Object result=graph.executeGremlinScript(scriptEngine,bindings,relatedEntitiesQuery,false);
    if (result instanceof List && CollectionUtils.isNotEmpty((List)result)) {
      List<?> queryResult=(List)result;
      Object firstElement=queryResult.get(0);
      if (firstElement instanceof AtlasVertex) {
        List<AtlasVertex> vertices=(List<AtlasVertex>)queryResult;
        List<AtlasEntityHeader> resultList=new ArrayList<>(vertices.size());
        for (        AtlasVertex vertex : vertices) {
          resultList.add(entityRetriever.toAtlasEntityHeader(vertex));
        }
        ret.setEntities(resultList);
      }
    }
    if (ret.getEntities() == null) {
      ret.setEntities(new ArrayList<>());
    }
  }
 catch (  ScriptException e) {
    if (LOG.isDebugEnabled()) {
      LOG.debug("String_Node_Str",relatedEntitiesQuery,e);
    }
    throw new AtlasBaseException(AtlasErrorCode.INTERNAL_ERROR,"String_Node_Str");
  }
 finally {
    graph.releaseGremlinScriptEngine(scriptEngine);
  }
  return ret;
}