Search in sources :

Example 26 with AtlasEntityWithExtInfo

use of org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo in project incubator-atlas by apache.

the class AtlasEntityStoreV1Test method testSpecialCharacters.

@Test(enabled = false)
public //TODO : Failing in typedef creation
void testSpecialCharacters() throws Exception {
    //Verify that type can be created with reserved characters in typename, attribute name
    final String typeName = "test_type_" + RandomStringUtils.randomAlphanumeric(10);
    String strAttrName = randomStrWithReservedChars();
    String arrayAttrName = randomStrWithReservedChars();
    String mapAttrName = randomStrWithReservedChars();
    AtlasEntityDef typeDefinition = AtlasTypeUtil.createClassTypeDef(typeName, "Special chars test type", ImmutableSet.<String>of(), AtlasTypeUtil.createOptionalAttrDef(strAttrName, "string"), AtlasTypeUtil.createOptionalAttrDef(arrayAttrName, "array<string>"), AtlasTypeUtil.createOptionalAttrDef(mapAttrName, "map<string,string>"));
    AtlasTypesDef atlasTypesDef = new AtlasTypesDef(null, null, null, Arrays.asList(typeDefinition));
    typeDefStore.createTypesDef(atlasTypesDef);
    //verify that entity can be created with reserved characters in string value, array value and map key and value
    AtlasEntity entity = new AtlasEntity();
    entity.setAttribute(strAttrName, randomStrWithReservedChars());
    entity.setAttribute(arrayAttrName, new String[] { randomStrWithReservedChars() });
    entity.setAttribute(mapAttrName, new HashMap<String, String>() {

        {
            put(randomStrWithReservedChars(), randomStrWithReservedChars());
        }
    });
    AtlasEntityWithExtInfo entityWithExtInfo = new AtlasEntityWithExtInfo(entity);
    final EntityMutationResponse response = entityStore.createOrUpdate(new AtlasEntityStream(entityWithExtInfo), false);
    final AtlasEntityHeader firstEntityCreated = response.getFirstEntityCreated();
    validateEntity(entityWithExtInfo, getEntityFromStore(firstEntityCreated));
//Verify that search with reserved characters works - for string attribute
//        String query =
//            String.format("`%s` where `%s` = '%s'", typeName, strAttrName, entity.getAttribute(strAttrName));
//        String responseJson = discoveryService.searchByDSL(query, new QueryParams(1, 0));
//        JSONObject response = new JSONObject(responseJson);
//        assertEquals(response.getJSONArray("rows").length(), 1);
}
Also used : AtlasEntityDef(org.apache.atlas.model.typedef.AtlasEntityDef) AtlasEntityWithExtInfo(org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo) AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) EntityMutationResponse(org.apache.atlas.model.instance.EntityMutationResponse) AtlasEntityHeader(org.apache.atlas.model.instance.AtlasEntityHeader) TestUtils.randomString(org.apache.atlas.TestUtils.randomString) AtlasTypesDef(org.apache.atlas.model.typedef.AtlasTypesDef) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 27 with AtlasEntityWithExtInfo

use of org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo in project incubator-atlas by apache.

the class AtlasEntityStoreV1Test method testUpdateEntityWithMap.

@Test(dependsOnMethods = "testCreate")
public void testUpdateEntityWithMap() throws Exception {
    AtlasEntity tableEntity = new AtlasEntity(tblEntity.getEntity());
    AtlasEntitiesWithExtInfo entitiesInfo = new AtlasEntitiesWithExtInfo(tableEntity);
    Map<String, AtlasStruct> partsMap = new HashMap<>();
    partsMap.put("part0", new AtlasStruct(TestUtils.PARTITION_STRUCT_TYPE, TestUtilsV2.NAME, "test"));
    tableEntity.setAttribute("partitionsMap", partsMap);
    init();
    EntityMutationResponse response = entityStore.createOrUpdate(new AtlasEntityStream(entitiesInfo), false);
    AtlasEntityHeader tableDefinition1 = response.getFirstUpdatedEntityByTypeName(TABLE_TYPE);
    AtlasEntity updatedTableDef1 = getEntityFromStore(tableDefinition1);
    validateEntity(entitiesInfo, updatedTableDef1);
    Assert.assertTrue(partsMap.get("part0").equals(((Map<String, AtlasStruct>) updatedTableDef1.getAttribute("partitionsMap")).get("part0")));
    //update map - add a map key
    partsMap.put("part1", new AtlasStruct(TestUtils.PARTITION_STRUCT_TYPE, TestUtilsV2.NAME, "test1"));
    tableEntity.setAttribute("partitionsMap", partsMap);
    init();
    response = entityStore.createOrUpdate(new AtlasEntityStream(entitiesInfo), false);
    AtlasEntityHeader tableDefinition2 = response.getFirstUpdatedEntityByTypeName(TABLE_TYPE);
    AtlasEntity updatedTableDef2 = getEntityFromStore(tableDefinition2);
    validateEntity(entitiesInfo, updatedTableDef2);
    assertEquals(((Map<String, AtlasStruct>) updatedTableDef2.getAttribute("partitionsMap")).size(), 2);
    Assert.assertTrue(partsMap.get("part1").equals(((Map<String, AtlasStruct>) updatedTableDef2.getAttribute("partitionsMap")).get("part1")));
    //update map - remove a key and add another key
    partsMap.remove("part0");
    partsMap.put("part2", new AtlasStruct(TestUtils.PARTITION_STRUCT_TYPE, TestUtilsV2.NAME, "test2"));
    tableEntity.setAttribute("partitionsMap", partsMap);
    init();
    response = entityStore.createOrUpdate(new AtlasEntityStream(entitiesInfo), false);
    AtlasEntityHeader tableDefinition3 = response.getFirstUpdatedEntityByTypeName(TABLE_TYPE);
    AtlasEntity updatedTableDef3 = getEntityFromStore(tableDefinition3);
    validateEntity(entitiesInfo, updatedTableDef3);
    assertEquals(((Map<String, AtlasStruct>) updatedTableDef3.getAttribute("partitionsMap")).size(), 2);
    Assert.assertNull(((Map<String, AtlasStruct>) updatedTableDef3.getAttribute("partitionsMap")).get("part0"));
    Assert.assertTrue(partsMap.get("part2").equals(((Map<String, AtlasStruct>) updatedTableDef3.getAttribute("partitionsMap")).get("part2")));
    //update struct value for existing map key
    AtlasStruct partition2 = partsMap.get("part2");
    partition2.setAttribute(TestUtilsV2.NAME, "test2Updated");
    init();
    response = entityStore.createOrUpdate(new AtlasEntityStream(entitiesInfo), false);
    AtlasEntityHeader tableDefinition4 = response.getFirstUpdatedEntityByTypeName(TABLE_TYPE);
    AtlasEntity updatedTableDef4 = getEntityFromStore(tableDefinition4);
    validateEntity(entitiesInfo, updatedTableDef4);
    assertEquals(((Map<String, AtlasStruct>) updatedTableDef4.getAttribute("partitionsMap")).size(), 2);
    Assert.assertNull(((Map<String, AtlasStruct>) updatedTableDef4.getAttribute("partitionsMap")).get("part0"));
    Assert.assertTrue(partsMap.get("part2").equals(((Map<String, AtlasStruct>) updatedTableDef4.getAttribute("partitionsMap")).get("part2")));
    //Test map pointing to a class
    AtlasEntity col0 = new AtlasEntity(TestUtilsV2.COLUMN_TYPE, TestUtilsV2.NAME, "test1");
    col0.setAttribute("type", "string");
    col0.setAttribute("table", AtlasTypeUtil.getAtlasObjectId(tableEntity));
    AtlasEntityWithExtInfo col0WithExtendedInfo = new AtlasEntityWithExtInfo(col0);
    col0WithExtendedInfo.addReferredEntity(tableEntity);
    col0WithExtendedInfo.addReferredEntity(dbEntity.getEntity());
    init();
    entityStore.createOrUpdate(new AtlasEntityStream(col0WithExtendedInfo), false);
    AtlasEntity col1 = new AtlasEntity(TestUtils.COLUMN_TYPE, TestUtilsV2.NAME, "test2");
    col1.setAttribute("type", "string");
    col1.setAttribute("table", AtlasTypeUtil.getAtlasObjectId(tableEntity));
    AtlasEntityWithExtInfo col1WithExtendedInfo = new AtlasEntityWithExtInfo(col1);
    col1WithExtendedInfo.addReferredEntity(tableEntity);
    col1WithExtendedInfo.addReferredEntity(dbEntity.getEntity());
    init();
    entityStore.createOrUpdate(new AtlasEntityStream(col1WithExtendedInfo), false);
    Map<String, AtlasObjectId> columnsMap = new HashMap<String, AtlasObjectId>();
    columnsMap.put("col0", AtlasTypeUtil.getAtlasObjectId(col0));
    columnsMap.put("col1", AtlasTypeUtil.getAtlasObjectId(col1));
    tableEntity.setAttribute(TestUtils.COLUMNS_MAP, columnsMap);
    entitiesInfo.addReferredEntity(col0);
    entitiesInfo.addReferredEntity(col1);
    init();
    response = entityStore.createOrUpdate(new AtlasEntityStream(entitiesInfo), false);
    AtlasEntityHeader tableDefinition5 = response.getFirstUpdatedEntityByTypeName(TABLE_TYPE);
    validateEntity(entitiesInfo, getEntityFromStore(tableDefinition5));
    //Swap elements
    columnsMap.clear();
    columnsMap.put("col0", AtlasTypeUtil.getAtlasObjectId(col1));
    columnsMap.put("col1", AtlasTypeUtil.getAtlasObjectId(col0));
    tableEntity.setAttribute(TestUtils.COLUMNS_MAP, columnsMap);
    init();
    response = entityStore.createOrUpdate(new AtlasEntityStream(entitiesInfo), false);
    AtlasEntityHeader tableDefinition6 = response.getFirstUpdatedEntityByTypeName(TABLE_TYPE);
    validateEntity(entitiesInfo, getEntityFromStore(tableDefinition6));
    Assert.assertEquals(entityStore.getById(col0.getGuid()).getEntity().getStatus(), AtlasEntity.Status.ACTIVE);
    Assert.assertEquals(entityStore.getById(col1.getGuid()).getEntity().getStatus(), AtlasEntity.Status.ACTIVE);
    //Drop the first key and change the class type as well to col0
    columnsMap.clear();
    columnsMap.put("col0", AtlasTypeUtil.getAtlasObjectId(col0));
    init();
    response = entityStore.createOrUpdate(new AtlasEntityStream(entitiesInfo), false);
    AtlasEntityHeader tableDefinition7 = response.getFirstUpdatedEntityByTypeName(TABLE_TYPE);
    validateEntity(entitiesInfo, getEntityFromStore(tableDefinition7));
    //Clear state
    tableEntity.setAttribute(TestUtils.COLUMNS_MAP, null);
    init();
    response = entityStore.createOrUpdate(new AtlasEntityStream(entitiesInfo), false);
    AtlasEntityHeader tableDefinition8 = response.getFirstUpdatedEntityByTypeName(TABLE_TYPE);
    validateEntity(entitiesInfo, getEntityFromStore(tableDefinition8));
}
Also used : AtlasEntityWithExtInfo(org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo) HashMap(java.util.HashMap) EntityMutationResponse(org.apache.atlas.model.instance.EntityMutationResponse) AtlasObjectId(org.apache.atlas.model.instance.AtlasObjectId) TestUtils.randomString(org.apache.atlas.TestUtils.randomString) AtlasStruct(org.apache.atlas.model.instance.AtlasStruct) AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) AtlasEntitiesWithExtInfo(org.apache.atlas.model.instance.AtlasEntity.AtlasEntitiesWithExtInfo) AtlasEntityHeader(org.apache.atlas.model.instance.AtlasEntityHeader) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 28 with AtlasEntityWithExtInfo

use of org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo in project incubator-atlas by apache.

the class EntityResource method getEntityDefinitionByAttribute.

/**
     * Fetch the complete definition of an entity given its qualified name.
     *
     * @param entityType
     * @param attribute
     * @param value
     */
public Response getEntityDefinitionByAttribute(String entityType, String attribute, String value) {
    try {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Fetching entity definition for type={}, qualified name={}", entityType, value);
        }
        entityType = ParamChecker.notEmpty(entityType, "Entity type cannot be null");
        attribute = ParamChecker.notEmpty(attribute, "attribute name cannot be null");
        value = ParamChecker.notEmpty(value, "attribute value cannot be null");
        Map<String, Object> attributes = new HashMap<>();
        attributes.put(attribute, value);
        AtlasEntityWithExtInfo entityInfo;
        try {
            entityInfo = entitiesStore.getByUniqueAttributes(getEntityType(entityType), attributes);
        } catch (AtlasBaseException e) {
            LOG.error("Cannot find entity with type: {}, attribute: {} and value: {}", entityType, attribute, value);
            throw toWebApplicationException(e);
        }
        String entityDefinition = null;
        if (entityInfo != null) {
            AtlasEntity entity = entityInfo.getEntity();
            final ITypedReferenceableInstance instance = restAdapters.getITypedReferenceable(entity);
            entityDefinition = InstanceSerialization.toJson(instance, true);
        }
        JSONObject response = new JSONObject();
        response.put(AtlasClient.REQUEST_ID, Servlets.getRequestId());
        Response.Status status = Response.Status.NOT_FOUND;
        if (entityDefinition != null) {
            response.put(AtlasClient.DEFINITION, new JSONObject(entityDefinition));
            status = Response.Status.OK;
        } else {
            response.put(AtlasClient.ERROR, Servlets.escapeJsonString(String.format("An entity with type={%s}, " + "qualifiedName={%s} does not exist", entityType, value)));
        }
        return Response.status(status).entity(response).build();
    } catch (AtlasBaseException e) {
        LOG.error("Unable to get instance definition for type={}, qualifiedName={}", entityType, value, e);
        throw toWebApplicationException(e);
    } catch (IllegalArgumentException e) {
        LOG.error("Bad type={}, qualifiedName={}", entityType, value, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.BAD_REQUEST));
    } catch (WebApplicationException e) {
        LOG.error("Unable to get instance definition for type={}, qualifiedName={}", entityType, value, e);
        throw e;
    } catch (Throwable e) {
        LOG.error("Unable to get instance definition for type={}, qualifiedName={}", entityType, value, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR));
    }
}
Also used : AtlasEntityWithExtInfo(org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo) HashMap(java.util.HashMap) ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) Response(javax.ws.rs.core.Response) EntityMutationResponse(org.apache.atlas.model.instance.EntityMutationResponse) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) JSONObject(org.codehaus.jettison.json.JSONObject) AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) JSONObject(org.codehaus.jettison.json.JSONObject)

Example 29 with AtlasEntityWithExtInfo

use of org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo in project incubator-atlas by apache.

the class TestUtilsV2 method createTableEntityV2.

public static AtlasEntityWithExtInfo createTableEntityV2(AtlasEntity dbEntity) {
    AtlasEntity tblEntity = new AtlasEntity(TABLE_TYPE);
    tblEntity.setAttribute(NAME, RandomStringUtils.randomAlphanumeric(10));
    tblEntity.setAttribute("description", "random table");
    tblEntity.setAttribute("type", "type");
    tblEntity.setAttribute("tableType", "MANAGED");
    tblEntity.setAttribute("database", AtlasTypeUtil.getAtlasObjectId(dbEntity));
    tblEntity.setAttribute("created", new Date());
    final AtlasStruct partitionStruct = new AtlasStruct("partition_struct_type", "name", "part0");
    tblEntity.setAttribute("partitions", new ArrayList<AtlasStruct>() {

        {
            add(partitionStruct);
        }
    });
    tblEntity.setAttribute("parametersMap", new java.util.HashMap<String, String>() {

        {
            put("key1", "value1");
        }
    });
    AtlasEntityWithExtInfo ret = new AtlasEntityWithExtInfo(tblEntity);
    ret.addReferredEntity(dbEntity);
    return ret;
}
Also used : AtlasStruct(org.apache.atlas.model.instance.AtlasStruct) AtlasEntityWithExtInfo(org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo) AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) Date(java.util.Date)

Example 30 with AtlasEntityWithExtInfo

use of org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo in project incubator-atlas by apache.

the class TestUtilsV2 method createDBEntityV2.

public static AtlasEntityWithExtInfo createDBEntityV2() {
    AtlasEntity dbEntity = new AtlasEntity(DATABASE_TYPE);
    dbEntity.setAttribute(NAME, RandomStringUtils.randomAlphanumeric(10));
    dbEntity.setAttribute("description", "us db");
    return new AtlasEntityWithExtInfo(dbEntity);
}
Also used : AtlasEntityWithExtInfo(org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo) AtlasEntity(org.apache.atlas.model.instance.AtlasEntity)

Aggregations

AtlasEntityWithExtInfo (org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo)34 AtlasEntity (org.apache.atlas.model.instance.AtlasEntity)24 EntityMutationResponse (org.apache.atlas.model.instance.EntityMutationResponse)18 Test (org.testng.annotations.Test)17 AtlasEntitiesWithExtInfo (org.apache.atlas.model.instance.AtlasEntity.AtlasEntitiesWithExtInfo)8 AtlasEntityHeader (org.apache.atlas.model.instance.AtlasEntityHeader)8 HashMap (java.util.HashMap)6 AtlasBaseException (org.apache.atlas.exception.AtlasBaseException)6 AtlasEntityType (org.apache.atlas.type.AtlasEntityType)6 AtlasObjectId (org.apache.atlas.model.instance.AtlasObjectId)5 GraphTransaction (org.apache.atlas.annotation.GraphTransaction)4 BeforeTest (org.testng.annotations.BeforeTest)4 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 AtlasServiceException (org.apache.atlas.AtlasServiceException)2 TestUtils.randomString (org.apache.atlas.TestUtils.randomString)2 AtlasClassification (org.apache.atlas.model.instance.AtlasClassification)2 AtlasStruct (org.apache.atlas.model.instance.AtlasStruct)2 AtlasEntityDef (org.apache.atlas.model.typedef.AtlasEntityDef)2 AtlasTypesDef (org.apache.atlas.model.typedef.AtlasTypesDef)2