Search in sources :

Example 6 with AtlasStruct

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");
    }
}
Also used : AtlasStruct(org.apache.atlas.model.instance.AtlasStruct) AtlasArrayType(org.apache.atlas.type.AtlasArrayType) AtlasType(org.apache.atlas.type.AtlasType) AtlasObjectId(org.apache.atlas.model.instance.AtlasObjectId) List(java.util.List) ArrayList(java.util.ArrayList) TestUtils.randomString(org.apache.atlas.TestUtils.randomString) Map(java.util.Map) HashMap(java.util.HashMap) AtlasMapType(org.apache.atlas.type.AtlasMapType)

Example 7 with AtlasStruct

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;
}
Also used : AtlasStruct(org.apache.atlas.model.instance.AtlasStruct) AtlasAttribute(org.apache.atlas.type.AtlasStructType.AtlasAttribute) AtlasAttributeDef(org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef) HashMap(java.util.HashMap) Map(java.util.Map)

Example 8 with AtlasStruct

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;
}
Also used : AtlasStruct(org.apache.atlas.model.instance.AtlasStruct) AtlasAttributeDef(org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef) HashMap(java.util.HashMap) Map(java.util.Map)

Example 9 with AtlasStruct

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;
}
Also used : AtlasStruct(org.apache.atlas.model.instance.AtlasStruct) AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) AtlasEntitiesWithExtInfo(org.apache.atlas.model.instance.AtlasEntity.AtlasEntitiesWithExtInfo) BigInteger(java.math.BigInteger) AtlasObjectId(org.apache.atlas.model.instance.AtlasObjectId) Date(java.util.Date) BigDecimal(java.math.BigDecimal)

Example 10 with AtlasStruct

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;
}
Also used : AtlasStruct(org.apache.atlas.model.instance.AtlasStruct) AtlasEdge(org.apache.atlas.repository.graphdb.AtlasEdge)

Aggregations

AtlasStruct (org.apache.atlas.model.instance.AtlasStruct)21 Map (java.util.Map)9 AtlasEntity (org.apache.atlas.model.instance.AtlasEntity)9 HashMap (java.util.HashMap)8 AtlasObjectId (org.apache.atlas.model.instance.AtlasObjectId)6 Test (org.testng.annotations.Test)5 AtlasEntitiesWithExtInfo (org.apache.atlas.model.instance.AtlasEntity.AtlasEntitiesWithExtInfo)4 EntityMutationResponse (org.apache.atlas.model.instance.EntityMutationResponse)4 BeforeTest (org.testng.annotations.BeforeTest)4 ArrayList (java.util.ArrayList)3 Date (java.util.Date)3 List (java.util.List)3 AtlasBaseException (org.apache.atlas.exception.AtlasBaseException)3 AtlasEntityHeader (org.apache.atlas.model.instance.AtlasEntityHeader)3 AtlasStructType (org.apache.atlas.type.AtlasStructType)3 AtlasAttribute (org.apache.atlas.type.AtlasStructType.AtlasAttribute)3 IStruct (org.apache.atlas.typesystem.IStruct)3 TestUtils.randomString (org.apache.atlas.TestUtils.randomString)2 AtlasEntityWithExtInfo (org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo)2 AtlasAttributeDef (org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef)2