use of org.apache.atlas.model.instance.AtlasStruct in project incubator-atlas by apache.
the class AtlasEntityStoreV1Test method validateAttribute.
private void validateAttribute(AtlasEntityExtInfo entityExtInfo, Object actual, Object expected, AtlasType attributeType, String attrName) throws AtlasBaseException, AtlasException {
switch(attributeType.getTypeCategory()) {
case OBJECT_ID_TYPE:
Assert.assertTrue(actual instanceof AtlasObjectId);
String guid = ((AtlasObjectId) actual).getGuid();
Assert.assertTrue(AtlasTypeUtil.isAssignedGuid(guid), "expected assigned guid. found " + guid);
break;
case PRIMITIVE:
case ENUM:
Assert.assertEquals(actual, expected);
break;
case MAP:
AtlasMapType mapType = (AtlasMapType) attributeType;
AtlasType valueType = mapType.getValueType();
Map actualMap = (Map) actual;
Map expectedMap = (Map) expected;
if (MapUtils.isNotEmpty(expectedMap)) {
Assert.assertTrue(MapUtils.isNotEmpty(actualMap));
// deleted entries are included in the attribute; hence use >=
Assert.assertTrue(actualMap.size() >= expectedMap.size());
for (Object key : expectedMap.keySet()) {
validateAttribute(entityExtInfo, actualMap.get(key), expectedMap.get(key), valueType, attrName);
}
}
break;
case ARRAY:
AtlasArrayType arrType = (AtlasArrayType) attributeType;
AtlasType elemType = arrType.getElementType();
List actualList = (List) actual;
List expectedList = (List) expected;
if (CollectionUtils.isNotEmpty(expectedList)) {
Assert.assertTrue(CollectionUtils.isNotEmpty(actualList));
//actual list could have deleted entities. Hence size may not match.
Assert.assertTrue(actualList.size() >= expectedList.size());
for (int i = 0; i < expectedList.size(); i++) {
validateAttribute(entityExtInfo, actualList.get(i), expectedList.get(i), elemType, attrName);
}
}
break;
case STRUCT:
AtlasStruct expectedStruct = (AtlasStruct) expected;
AtlasStruct actualStruct = (AtlasStruct) actual;
validateEntity(entityExtInfo, actualStruct, expectedStruct);
break;
default:
Assert.fail("Unknown type category");
}
}
use of org.apache.atlas.model.instance.AtlasStruct in project incubator-atlas by apache.
the class AtlasStructType method validateValue.
@Override
public boolean validateValue(Object obj, String objName, List<String> messages) {
boolean ret = true;
if (obj != null) {
if (obj instanceof AtlasStruct) {
AtlasStruct structObj = (AtlasStruct) obj;
for (AtlasAttributeDef attributeDef : structDef.getAttributeDefs()) {
String attrName = attributeDef.getName();
AtlasAttribute attribute = allAttributes.get(attributeDef.getName());
if (attribute != null) {
AtlasType dataType = attribute.getAttributeType();
Object value = structObj.getAttribute(attrName);
String fieldName = objName + "." + attrName;
if (value != null) {
ret = dataType.validateValue(value, fieldName, messages) && ret;
} else if (!attributeDef.getIsOptional()) {
ret = false;
messages.add(fieldName + ": mandatory attribute value missing in type " + getTypeName());
}
}
}
} else if (obj instanceof Map) {
Map attributes = AtlasTypeUtil.toStructAttributes((Map) obj);
for (AtlasAttributeDef attributeDef : structDef.getAttributeDefs()) {
String attrName = attributeDef.getName();
AtlasAttribute attribute = allAttributes.get(attributeDef.getName());
if (attribute != null) {
AtlasType dataType = attribute.getAttributeType();
Object value = attributes.get(attrName);
String fieldName = objName + "." + attrName;
if (value != null) {
ret = dataType.validateValue(value, fieldName, messages) && ret;
} else if (!attributeDef.getIsOptional()) {
ret = false;
messages.add(fieldName + ": mandatory attribute value missing in type " + getTypeName());
}
}
}
} else {
ret = false;
messages.add(objName + "=" + obj + ": invalid value for type " + getTypeName());
}
}
return ret;
}
use of org.apache.atlas.model.instance.AtlasStruct in project incubator-atlas by apache.
the class AtlasStructType method isValidValueForUpdate.
@Override
public boolean isValidValueForUpdate(Object obj) {
if (obj != null) {
Map<String, Object> attributes;
if (obj instanceof AtlasStruct) {
AtlasStruct structObj = (AtlasStruct) obj;
attributes = structObj.getAttributes();
} else if (obj instanceof Map) {
attributes = AtlasTypeUtil.toStructAttributes((Map) obj);
} else {
return false;
}
if (MapUtils.isNotEmpty(attributes)) {
for (Map.Entry<String, Object> e : attributes.entrySet()) {
String attrName = e.getKey();
Object attrValue = e.getValue();
AtlasAttributeDef attrDef = structDef.getAttribute(attrName);
if (attrValue == null || attrDef == null) {
continue;
}
if (!isAssignableValueForUpdate(attrValue, attrDef)) {
return false;
}
}
}
}
return true;
}
use of org.apache.atlas.model.instance.AtlasStruct in project incubator-atlas by apache.
the class TestUtilsV2 method createDeptEg2.
public static AtlasEntitiesWithExtInfo createDeptEg2() {
AtlasEntitiesWithExtInfo entitiesWithExtInfo = new AtlasEntitiesWithExtInfo();
/******* Department - HR *******/
AtlasEntity hrDept = new AtlasEntity(DEPARTMENT_TYPE, "name", "hr");
AtlasObjectId hrDeptId = AtlasTypeUtil.getAtlasObjectId(hrDept);
/******* Address Entities *******/
AtlasStruct janeAddr = new AtlasStruct(ADDRESS_TYPE);
janeAddr.setAttribute("street", "Great America Parkway");
janeAddr.setAttribute("city", "Santa Clara");
AtlasStruct juliusAddr = new AtlasStruct(ADDRESS_TYPE);
juliusAddr.setAttribute("street", "Madison Ave");
juliusAddr.setAttribute("city", "Newtonville");
AtlasStruct maxAddr = new AtlasStruct(ADDRESS_TYPE);
maxAddr.setAttribute("street", "Ripley St");
maxAddr.setAttribute("city", "Newton");
AtlasStruct johnAddr = new AtlasStruct(ADDRESS_TYPE);
johnAddr.setAttribute("street", "Stewart Drive");
johnAddr.setAttribute("city", "Sunnyvale");
/******* Manager - Jane (John and Max subordinates) *******/
AtlasEntity jane = new AtlasEntity(MANAGER_TYPE);
AtlasObjectId janeId = AtlasTypeUtil.getAtlasObjectId(jane);
jane.setAttribute("name", "Jane");
jane.setAttribute("department", hrDeptId);
jane.setAttribute("address", janeAddr);
/******* Manager - Julius (no subordinates) *******/
AtlasEntity julius = new AtlasEntity(MANAGER_TYPE);
AtlasObjectId juliusId = AtlasTypeUtil.getAtlasObjectId(julius);
julius.setAttribute("name", "Julius");
julius.setAttribute("department", hrDeptId);
julius.setAttribute("address", juliusAddr);
julius.setAttribute("subordinates", ImmutableList.of());
/******* Employee - Max (Manager: Jane, Mentor: Julius) *******/
AtlasEntity max = new AtlasEntity(EMPLOYEE_TYPE);
AtlasObjectId maxId = AtlasTypeUtil.getAtlasObjectId(max);
max.setAttribute("name", "Max");
max.setAttribute("department", hrDeptId);
max.setAttribute("address", maxAddr);
max.setAttribute("manager", janeId);
max.setAttribute("mentor", juliusId);
max.setAttribute("birthday", new Date(1979, 3, 15));
max.setAttribute("hasPets", true);
max.setAttribute("age", 36);
max.setAttribute("numberOfCars", 2);
max.setAttribute("houseNumber", 17);
max.setAttribute("carMileage", 13);
max.setAttribute("shares", Long.MAX_VALUE);
max.setAttribute("salary", Double.MAX_VALUE);
max.setAttribute("numberOfStarsEstimate", new BigInteger("1000000000000000000000000000000"));
max.setAttribute("approximationOfPi", new BigDecimal("3.1415926535897932"));
/******* Employee - John (Manager: Jane, Mentor: Max) *******/
AtlasEntity john = new AtlasEntity(EMPLOYEE_TYPE);
AtlasObjectId johnId = AtlasTypeUtil.getAtlasObjectId(john);
john.setAttribute("name", "John");
john.setAttribute("department", hrDeptId);
john.setAttribute("address", johnAddr);
john.setAttribute("manager", janeId);
john.setAttribute("mentor", maxId);
john.setAttribute("birthday", new Date(1950, 5, 15));
john.setAttribute("hasPets", true);
john.setAttribute("numberOfCars", 1);
john.setAttribute("houseNumber", 153);
john.setAttribute("carMileage", 13364);
john.setAttribute("shares", 15000);
john.setAttribute("salary", 123345.678);
john.setAttribute("age", 50);
john.setAttribute("numberOfStarsEstimate", new BigInteger("1000000000000000000000"));
john.setAttribute("approximationOfPi", new BigDecimal("3.141592653589793238462643383279502884197169399375105820974944592307816406286"));
jane.setAttribute("subordinates", ImmutableList.of(johnId, maxId));
hrDept.setAttribute("employees", ImmutableList.of(janeId, juliusId, maxId, johnId));
entitiesWithExtInfo.addEntity(hrDept);
entitiesWithExtInfo.addEntity(jane);
entitiesWithExtInfo.addEntity(julius);
entitiesWithExtInfo.addEntity(max);
entitiesWithExtInfo.addEntity(john);
return entitiesWithExtInfo;
}
use of org.apache.atlas.model.instance.AtlasStruct in project incubator-atlas by apache.
the class EntityGraphMapper method mapStructValue.
private AtlasEdge mapStructValue(AttributeMutationContext ctx, EntityMutationContext context) throws AtlasBaseException {
if (LOG.isDebugEnabled()) {
LOG.debug("==> mapStructValue({})", ctx);
}
AtlasEdge ret = null;
if (ctx.getCurrentEdge() != null) {
AtlasStruct structVal = null;
if (ctx.getValue() instanceof AtlasStruct) {
structVal = (AtlasStruct) ctx.getValue();
} else if (ctx.getValue() instanceof Map) {
structVal = new AtlasStruct(ctx.getAttrType().getTypeName(), (Map) AtlasTypeUtil.toStructAttributes((Map) ctx.getValue()));
}
if (structVal != null) {
updateVertex(structVal, ctx.getCurrentEdge().getInVertex(), context);
}
ret = ctx.getCurrentEdge();
} else if (ctx.getValue() != null) {
String edgeLabel = AtlasGraphUtilsV1.getEdgeLabel(ctx.getVertexProperty());
AtlasStruct structVal = null;
if (ctx.getValue() instanceof AtlasStruct) {
structVal = (AtlasStruct) ctx.getValue();
} else if (ctx.getValue() instanceof Map) {
structVal = new AtlasStruct(ctx.getAttrType().getTypeName(), (Map) AtlasTypeUtil.toStructAttributes((Map) ctx.getValue()));
}
if (structVal != null) {
ret = createVertex(structVal, ctx.getReferringVertex(), edgeLabel, context);
}
}
if (LOG.isDebugEnabled()) {
LOG.debug("<== mapStructValue({})", ctx);
}
return ret;
}
Aggregations