Commit 224eaffe by Madhan Neethiraj

ATLAS-1506: updated AtlasObjectId to support unqiueAttributes to identity the object

parent 2763ff30
......@@ -29,6 +29,7 @@ import javax.xml.bind.annotation.XmlSeeAlso;
import org.apache.atlas.model.PList;
import org.apache.atlas.model.SearchFilter.SortType;
import org.apache.atlas.model.typedef.AtlasBaseTypeDef;
import org.codehaus.jackson.annotate.JsonAutoDetect;
import static org.codehaus.jackson.annotate.JsonAutoDetect.Visibility.PUBLIC_ONLY;
import static org.codehaus.jackson.annotate.JsonAutoDetect.Visibility.NONE;
......@@ -47,29 +48,41 @@ import org.codehaus.jackson.map.annotate.JsonSerialize;
public class AtlasObjectId implements Serializable {
private static final long serialVersionUID = 1L;
public static final String KEY_TYPENAME = "typeName";
public static final String KEY_GUID = "guid";
public static final String KEY_TYPENAME = "typeName";
public static final String KEY_GUID = "guid";
public static final String KEY_UNIQUE_ATTRIBUTES = "uniqueAttributes";
private String typeName;
private String guid;
private String typeName;
private String guid;
private Map<String, Object> uniqueAttributes;
public AtlasObjectId() {
this(null, null);
this(null, null, null);
}
public AtlasObjectId(String typeName) {
this(typeName, null);
this(typeName, null, null);
}
public AtlasObjectId(String typeName, String guid) {
this(typeName, guid, null);
}
public AtlasObjectId(String typeName, Map<String, Object> uniqueAttributes) {
this(typeName, null, uniqueAttributes);
}
public AtlasObjectId(String typeName, String guid, Map<String, Object> uniqueAttributes) {
setTypeName(typeName);
setGuid(guid);
setUniqueAttributes(uniqueAttributes);
}
public AtlasObjectId(AtlasObjectId other) {
if (other != null) {
setTypeName(other.getTypeName());
setGuid(other.getGuid());
setUniqueAttributes(other.getUniqueAttributes());
}
}
......@@ -77,6 +90,7 @@ public class AtlasObjectId implements Serializable {
if (objIdMap != null) {
Object t = objIdMap.get(KEY_TYPENAME);
Object g = objIdMap.get(KEY_GUID);
Object u = objIdMap.get(KEY_UNIQUE_ATTRIBUTES);
if (t != null) {
setTypeName(t.toString());
......@@ -85,6 +99,10 @@ public class AtlasObjectId implements Serializable {
if (g != null) {
setGuid(g.toString());
}
if (u != null && u instanceof Map) {
setUniqueAttributes((Map)u);
}
}
}
......@@ -104,6 +122,14 @@ public class AtlasObjectId implements Serializable {
this.guid = guid;
}
public Map<String, Object> getUniqueAttributes() {
return uniqueAttributes;
}
public void setUniqueAttributes(Map<String, Object> uniqueAttributes) {
this.uniqueAttributes = uniqueAttributes;
}
public StringBuilder toString(StringBuilder sb) {
if (sb == null) {
sb = new StringBuilder();
......@@ -112,6 +138,9 @@ public class AtlasObjectId implements Serializable {
sb.append("AtlasObjectId{");
sb.append("typeName='").append(typeName).append('\'');
sb.append(", guid='").append(guid).append('\'');
sb.append(", uniqueAttributes={");
AtlasBaseTypeDef.dumpObjects(uniqueAttributes, sb);
sb.append('}');
sb.append('}');
return sb;
......@@ -123,12 +152,13 @@ public class AtlasObjectId implements Serializable {
if (o == null || getClass() != o.getClass()) return false;
AtlasObjectId that = (AtlasObjectId) o;
return Objects.equals(typeName, that.typeName) &&
Objects.equals(guid, that.guid);
Objects.equals(guid, that.guid) &&
Objects.equals(uniqueAttributes, that.uniqueAttributes);
}
@Override
public int hashCode() {
return Objects.hash(typeName, guid);
return Objects.hash(typeName, guid, uniqueAttributes);
}
@Override
......
......@@ -27,6 +27,7 @@ import java.util.Map;
import org.apache.atlas.model.TypeCategory;
import org.apache.atlas.model.instance.AtlasObjectId;
import org.apache.atlas.model.typedef.AtlasBaseTypeDef;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
......@@ -526,9 +527,7 @@ public class AtlasBuiltInTypes {
if (obj == null || obj instanceof AtlasObjectId) {
return true;
} else if (obj instanceof Map) {
Map map = (Map)obj;
return map.containsKey(AtlasObjectId.KEY_TYPENAME) && map.containsKey(AtlasObjectId.KEY_GUID);
return isValidMap((Map)obj);
}
return getNormalizedValue(obj) != null;
......@@ -542,7 +541,7 @@ public class AtlasBuiltInTypes {
} else if (obj instanceof Map) {
Map map = (Map) obj;
if (map.containsKey(AtlasObjectId.KEY_TYPENAME) && map.containsKey(AtlasObjectId.KEY_GUID)) {
if (isValidMap(map)) {
return new AtlasObjectId(map);
}
}
......@@ -550,5 +549,21 @@ public class AtlasBuiltInTypes {
return null;
}
private boolean isValidMap(Map map) {
if (map.containsKey(AtlasObjectId.KEY_TYPENAME)) {
if (map.containsKey(AtlasObjectId.KEY_GUID)) {
return true;
} else {
Object uniqueAttributes = map.get(AtlasObjectId.KEY_UNIQUE_ATTRIBUTES);
if (uniqueAttributes instanceof Map && MapUtils.isNotEmpty((Map)uniqueAttributes)) {
return true;
}
}
}
return false;
}
}
}
......@@ -37,27 +37,46 @@ public class TestAtlasObjectIdType {
private final Object[] invalidValues;
{
Map<String, String> objectId1 = new HashMap<>();
Map<Object, Object> objectId2 = new HashMap<>();
Map<Object, Object> objectId3 = new HashMap<>();
Map<Object, Object> objectId4 = new HashMap<>();
Map<Object, Object> objectId5 = new HashMap<>();
objectId1.put(AtlasObjectId.KEY_TYPENAME, "testType");
objectId1.put(AtlasObjectId.KEY_GUID, "guid-1234");
objectId2.put(AtlasObjectId.KEY_TYPENAME, "testType");
objectId2.put(AtlasObjectId.KEY_GUID, 1234);
objectId3.put(AtlasObjectId.KEY_TYPENAME, "testType"); // no guid
objectId4.put(AtlasObjectId.KEY_GUID, "guid-1234"); // no typeName
objectId4.put(AtlasObjectId.KEY_TYPENAME + "-invalid", "testType");
objectId5.put(AtlasObjectId.KEY_GUID + "-invalid", "guid-1234");
Map<String, String> validObj1 = new HashMap<>();
Map<Object, Object> validObj2 = new HashMap<>();
Map<Object, Object> validObj3 = new HashMap<>();
Map<Object, Object> invalidObj1 = new HashMap<>();
Map<Object, Object> invalidObj2 = new HashMap<>();
Map<Object, Object> invalidObj3 = new HashMap<>();
Map<Object, Object> invalidObj4 = new HashMap<>();
Map<Object, Object> invalidObj5 = new HashMap<>();
validObj1.put(AtlasObjectId.KEY_TYPENAME, "testType");
validObj1.put(AtlasObjectId.KEY_GUID, "guid-1234");
validObj2.put(AtlasObjectId.KEY_TYPENAME, "testType");
validObj2.put(AtlasObjectId.KEY_GUID, 1234);
Map<String, Object> uniqAttribs = new HashMap<String, Object>();
uniqAttribs.put("name", "testTypeInstance-1");
validObj3.put(AtlasObjectId.KEY_TYPENAME, "testType");
validObj3.put(AtlasObjectId.KEY_UNIQUE_ATTRIBUTES, uniqAttribs);
invalidObj1.put(AtlasObjectId.KEY_TYPENAME, "testType"); // no guid
invalidObj2.put(AtlasObjectId.KEY_GUID, "guid-1234"); // no typeName or uniqueAttribute
invalidObj2.put(AtlasObjectId.KEY_TYPENAME + "-invalid", "testType");
invalidObj3.put(AtlasObjectId.KEY_GUID + "-invalid", "guid-1234"); // no guid or typename or uniqueAttribute
invalidObj4.put(AtlasObjectId.KEY_TYPENAME, "testType"); // empty uniqueAttribute
invalidObj4.put(AtlasObjectId.KEY_UNIQUE_ATTRIBUTES, new HashMap<String, Object>());
invalidObj5.put(AtlasObjectId.KEY_TYPENAME, "testType"); // non-map uniqueAttribute
invalidObj5.put(AtlasObjectId.KEY_UNIQUE_ATTRIBUTES, new ArrayList<String>());
validValues = new Object[] {
null, objectId1, objectId2, new AtlasObjectId(), new AtlasObjectId("testType", "guid-1234"), };
null, validObj1, validObj2, validObj3, new AtlasObjectId(), new AtlasObjectId("testType", "guid-1234"), };
invalidValues = new Object[] {
objectId3, objectId4, objectId5, Byte.valueOf((byte)1), Short.valueOf((short)1), Integer.valueOf(1),
invalidObj1, invalidObj2, invalidObj3, invalidObj4, invalidObj5,
Byte.valueOf((byte)1), Short.valueOf((short)1), Integer.valueOf(1),
Long.valueOf(1L), Float.valueOf(1), Double.valueOf(1), BigInteger.valueOf(1), BigDecimal.valueOf(1), "1",
"", "12ab", "abcd", "-12ab",
};
......@@ -99,8 +118,14 @@ public class TestAtlasObjectIdType {
} else if (value instanceof Map) {
assertEquals(normalizedValue.getTypeName(), ((Map)value).get(AtlasObjectId.KEY_TYPENAME).toString(),
"value=" + value);
assertEquals(normalizedValue.getGuid(), ((Map)value).get(AtlasObjectId.KEY_GUID).toString(),
"value=" + value);
if (((Map)value).get(AtlasObjectId.KEY_GUID) == null) {
assertEquals(normalizedValue.getGuid(), ((Map)value).get(AtlasObjectId.KEY_GUID), "value=" + value);
} else {
assertEquals(normalizedValue.getGuid().toString(), ((Map) value).get(AtlasObjectId.KEY_GUID).toString(), "value=" + value);
}
assertEquals(normalizedValue.getUniqueAttributes(), ((Map)value).get(AtlasObjectId.KEY_UNIQUE_ATTRIBUTES),
"value=" + value);
}
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment