Program: 61095
Project: 61095
Buggy source:
private Map<String,Set<Attribute>> prepareAttributes(SyncopeUser user,String password,TargetResource resource) throws PropagationException {
  LOG.debug("String_Node_Str" + "String_Node_Str" + "String_Node_Str",new Object[]{user,resource,user.getAttributes()});
  Set<Attribute> accountAttributes=new HashSet<Attribute>();
  Class castToBeApplied;
  Map<String,Attribute> accountId=new HashMap<String,Attribute>();
  Set objValues;
  UAttr attr;
  UDerAttr derAttr;
  UVirAttr virAttr;
  AbstractSchema schema;
  AbstractDerSchema derSchema;
  AbstractVirSchema virSchema;
  SchemaType schemaType=null;
  List<AbstractAttrValue> values;
  for (  SchemaMapping mapping : resource.getMappings()) {
    LOG.debug("String_Node_Str",mapping.getSourceAttrName());
    schema=null;
    derSchema=null;
    virSchema=null;
    values=null;
    try {
switch (mapping.getSourceMappingType()) {
case UserSchema:
        schema=schemaDAO.find(mapping.getSourceAttrName(),getSourceMappingTypeClass(mapping.getSourceMappingType()));
      schemaType=schema.getType();
    attr=user.getAttribute(mapping.getSourceAttrName());
  values=attr != null ? (schema.isUniqueConstraint() ? Collections.singletonList(attr.getUniqueValue()) : attr.getValues()) : Collections.EMPTY_LIST;
LOG.debug("String_Node_Str",attr + "String_Node_Str" + "String_Node_Str"+ "String_Node_Str",new Object[]{mapping.getSourceAttrName(),mapping.getSourceMappingType(),values});
break;
case UserVirtualSchema:
virSchema=virSchemaDAO.find(mapping.getSourceAttrName(),getSourceMappingTypeClass(mapping.getSourceMappingType()));
schemaType=SchemaType.String;
virAttr=user.getVirtualAttribute(mapping.getSourceAttrName());
values=new ArrayList<AbstractAttrValue>();
AbstractAttrValue abstractValue;
if (virAttr != null && virAttr.getValues() != null) {
for (String value : virAttr.getValues()) {
abstractValue=new UAttrValue();
abstractValue.setStringValue(value);
values.add(abstractValue);
}
}
LOG.debug("String_Node_Str",virAttr + "String_Node_Str" + "String_Node_Str"+ "String_Node_Str",new Object[]{mapping.getSourceAttrName(),mapping.getSourceMappingType(),values});
break;
case UserDerivedSchema:
derSchema=derSchemaDAO.find(mapping.getSourceAttrName(),getSourceMappingTypeClass(mapping.getSourceMappingType()));
schemaType=SchemaType.String;
derAttr=user.getDerivedAttribute(mapping.getSourceAttrName());
if (derAttr != null) {
AbstractAttrValue value=new UAttrValue();
value.setStringValue(derAttr.getValue(user.getAttributes()));
values=Collections.singletonList(value);
}
 else {
values=Collections.EMPTY_LIST;
}
LOG.debug("String_Node_Str",derAttr + "String_Node_Str" + "String_Node_Str"+ "String_Node_Str",new Object[]{mapping.getSourceAttrName(),mapping.getSourceMappingType(),values});
break;
case SyncopeUserId:
case Password:
schema=null;
schemaType=SchemaType.String;
AbstractAttrValue uAttrValue=new UAttrValue();
if (SourceMappingType.SyncopeUserId == mapping.getSourceMappingType()) {
uAttrValue.setStringValue(user.getId().toString());
}
if (SourceMappingType.Password == mapping.getSourceMappingType() && password != null) {
uAttrValue.setStringValue(password);
}
values=Collections.singletonList(uAttrValue);
break;
default :
break;
}
if (LOG.isDebugEnabled()) {
LOG.debug("String_Node_Str" + "String_Node_Str" + mapping.getDestAttrName() + "String_Node_Str"+ mapping.isAccountid()+ "String_Node_Str"+ (mapping.isPassword() || mapping.getSourceMappingType().equals(SourceMappingType.Password))+ "String_Node_Str"+ mapping.getMandatoryCondition()+ "String_Node_Str"+ mapping.getSourceAttrName()+ "String_Node_Str"+ mapping.getSourceMappingType().toString()+ "String_Node_Str"+ schemaType.getClassName()+ "String_Node_Str"+ values);
}
objValues=new HashSet();
for (AbstractAttrValue value : values) {
castToBeApplied=Class.forName(schemaType.getClassName());
if (!FrameworkUtil.isSupportedAttributeType(castToBeApplied)) {
castToBeApplied=String.class;
objValues.add(value.getValueAsString());
}
 else {
objValues.add(value.getValue());
}
}
if (mapping.isAccountid()) {
if (schema != null && schema.isMultivalue()) {
accountId.put(objValues.iterator().next().toString(),AttributeBuilder.build(mapping.getDestAttrName(),objValues));
}
 else {
accountId.put(objValues.iterator().next().toString(),objValues.isEmpty() ? AttributeBuilder.build(mapping.getDestAttrName()) : AttributeBuilder.build(mapping.getDestAttrName(),objValues.iterator().next()));
}
}
if (mapping.isPassword()) {
accountAttributes.add(AttributeBuilder.buildPassword(objValues.iterator().next().toString().toCharArray()));
}
if (!mapping.isPassword() && !mapping.isAccountid()) {
if (schema != null && schema.isMultivalue()) {
accountAttributes.add(AttributeBuilder.build(mapping.getDestAttrName(),objValues));
}
 else {
accountAttributes.add(objValues.isEmpty() ? AttributeBuilder.build(mapping.getDestAttrName()) : AttributeBuilder.build(mapping.getDestAttrName(),objValues.iterator().next()));
}
}
}
 catch (Throwable t) {
LOG.debug("String_Node_Str",mapping.getSourceAttrName(),t);
}
}
if (accountId.isEmpty()) {
throw new PropagationException(resource.getName(),"String_Node_Str");
}
final String key=accountId.keySet().iterator().next();
String evaluatedAccountLink=jexlUtil.evaluateWithAttributes(resource.getAccountLink(),user.getAttributes());
if (evaluatedAccountLink.isEmpty()) {
LOG.debug("String_Node_Str",key);
accountAttributes.add(new Name(key));
}
 else {
LOG.debug("String_Node_Str",evaluatedAccountLink);
accountAttributes.add(new Name(evaluatedAccountLink));
LOG.debug("String_Node_Str");
}
return Collections.singletonMap(key,accountAttributes);
}

Fixed version (ground truth):
private Map<String,Set<Attribute>> prepareAttributes(SyncopeUser user,String password,TargetResource resource) throws PropagationException {
  LOG.debug("String_Node_Str" + "String_Node_Str" + "String_Node_Str",new Object[]{user,resource,user.getAttributes()});
  Set<Attribute> accountAttributes=new HashSet<Attribute>();
  Class castToBeApplied;
  Map<String,Attribute> accountId=new HashMap<String,Attribute>();
  Set objValues;
  UAttr attr;
  UDerAttr derAttr;
  UVirAttr virAttr;
  AbstractSchema schema;
  AbstractDerSchema derSchema;
  AbstractVirSchema virSchema;
  SchemaType schemaType=null;
  List<AbstractAttrValue> values;
  for (  SchemaMapping mapping : resource.getMappings()) {
    LOG.debug("String_Node_Str",mapping.getSourceAttrName());
    schema=null;
    derSchema=null;
    virSchema=null;
    values=null;
    try {
switch (mapping.getSourceMappingType()) {
case UserSchema:
        schema=schemaDAO.find(mapping.getSourceAttrName(),getSourceMappingTypeClass(mapping.getSourceMappingType()));
      schemaType=schema.getType();
    attr=user.getAttribute(mapping.getSourceAttrName());
  values=attr != null ? (schema.isUniqueConstraint() ? Collections.singletonList(attr.getUniqueValue()) : attr.getValues()) : Collections.EMPTY_LIST;
LOG.debug("String_Node_Str",attr + "String_Node_Str" + "String_Node_Str"+ "String_Node_Str",new Object[]{mapping.getSourceAttrName(),mapping.getSourceMappingType(),values});
break;
case UserVirtualSchema:
virSchema=virSchemaDAO.find(mapping.getSourceAttrName(),getSourceMappingTypeClass(mapping.getSourceMappingType()));
schemaType=SchemaType.String;
virAttr=user.getVirtualAttribute(mapping.getSourceAttrName());
values=new ArrayList<AbstractAttrValue>();
AbstractAttrValue abstractValue;
if (virAttr != null && virAttr.getValues() != null) {
for (String value : virAttr.getValues()) {
abstractValue=new UAttrValue();
abstractValue.setStringValue(value);
values.add(abstractValue);
}
}
LOG.debug("String_Node_Str",virAttr + "String_Node_Str" + "String_Node_Str"+ "String_Node_Str",new Object[]{mapping.getSourceAttrName(),mapping.getSourceMappingType(),values});
break;
case UserDerivedSchema:
derSchema=derSchemaDAO.find(mapping.getSourceAttrName(),getSourceMappingTypeClass(mapping.getSourceMappingType()));
schemaType=SchemaType.String;
derAttr=user.getDerivedAttribute(mapping.getSourceAttrName());
if (derAttr != null) {
AbstractAttrValue value=new UAttrValue();
value.setStringValue(derAttr.getValue(user.getAttributes()));
values=Collections.singletonList(value);
}
 else {
values=Collections.EMPTY_LIST;
}
LOG.debug("String_Node_Str",derAttr + "String_Node_Str" + "String_Node_Str"+ "String_Node_Str",new Object[]{mapping.getSourceAttrName(),mapping.getSourceMappingType(),values});
break;
case SyncopeUserId:
case Password:
schema=null;
schemaType=SchemaType.String;
AbstractAttrValue uAttrValue=new UAttrValue();
if (SourceMappingType.SyncopeUserId == mapping.getSourceMappingType()) {
uAttrValue.setStringValue(user.getId().toString());
}
if (SourceMappingType.Password == mapping.getSourceMappingType() && password != null) {
uAttrValue.setStringValue(password);
}
values=Collections.singletonList(uAttrValue);
break;
default :
break;
}
if (LOG.isDebugEnabled()) {
LOG.debug("String_Node_Str" + "String_Node_Str" + mapping.getDestAttrName() + "String_Node_Str"+ mapping.isAccountid()+ "String_Node_Str"+ (mapping.isPassword() || mapping.getSourceMappingType().equals(SourceMappingType.Password))+ "String_Node_Str"+ mapping.getMandatoryCondition()+ "String_Node_Str"+ mapping.getSourceAttrName()+ "String_Node_Str"+ mapping.getSourceMappingType().toString()+ "String_Node_Str"+ schemaType.getClassName()+ "String_Node_Str"+ values);
}
objValues=new HashSet();
for (AbstractAttrValue value : values) {
castToBeApplied=Class.forName(schemaType.getClassName());
if (!FrameworkUtil.isSupportedAttributeType(castToBeApplied)) {
castToBeApplied=String.class;
objValues.add(value.getValueAsString());
}
 else {
objValues.add(value.getValue());
}
}
if (mapping.isAccountid()) {
if (schema != null && schema.isMultivalue()) {
accountId.put(objValues.iterator().next().toString(),AttributeBuilder.build(mapping.getDestAttrName(),objValues));
}
 else {
accountId.put(objValues.iterator().next().toString(),objValues.isEmpty() ? AttributeBuilder.build(mapping.getDestAttrName()) : AttributeBuilder.build(mapping.getDestAttrName(),objValues.iterator().next()));
}
}
if (mapping.isPassword()) {
accountAttributes.add(AttributeBuilder.buildPassword(objValues.iterator().next().toString().toCharArray()));
}
if (!mapping.isPassword() && !mapping.isAccountid()) {
if (schema != null && schema.isMultivalue()) {
accountAttributes.add(AttributeBuilder.build(mapping.getDestAttrName(),objValues));
}
 else {
accountAttributes.add(objValues.isEmpty() ? AttributeBuilder.build(mapping.getDestAttrName()) : AttributeBuilder.build(mapping.getDestAttrName(),objValues.iterator().next()));
}
}
}
 catch (Throwable t) {
LOG.debug("String_Node_Str",mapping.getSourceAttrName(),t);
}
}
if (accountId.isEmpty()) {
throw new PropagationException(resource.getName(),"String_Node_Str");
}
final String key=accountId.keySet().iterator().next();
String evaluatedAccountLink=evaluateAccountLink(user,resource.getAccountLink());
if (evaluatedAccountLink.isEmpty()) {
LOG.debug("String_Node_Str",key);
accountAttributes.add(new Name(key));
}
 else {
LOG.debug("String_Node_Str",evaluatedAccountLink);
accountAttributes.add(new Name(evaluatedAccountLink));
LOG.debug("String_Node_Str");
}
return Collections.singletonMap(key,accountAttributes);
}