use of org.apache.atlas.model.instance.AtlasEntityHeader in project incubator-atlas by apache.
the class AtlasEntityStoreV1Test method testPartialUpdateAttr.
@Test
public void testPartialUpdateAttr() throws Exception {
//Update optional attribute
init();
AtlasEntity dbEntity = new AtlasEntity(TestUtilsV2.DATABASE_TYPE);
dbEntity.setAttribute("name", RandomStringUtils.randomAlphanumeric(10));
dbEntity.setAttribute("description", "us db");
dbEntity.setAttribute("isReplicated", false);
dbEntity.setAttribute("created", "09081988");
dbEntity.setAttribute("namespace", "db namespace");
dbEntity.setAttribute("cluster", "Fenton_Cluster");
dbEntity.setAttribute("colo", "10001");
EntityStream dbStream = new AtlasEntityStream(new AtlasEntitiesWithExtInfo(dbEntity));
EntityMutationResponse response = entityStore.createOrUpdate(dbStream, false);
AtlasEntityHeader dbHeader = response.getFirstEntityCreated();
AtlasEntity createdDbEntity = getEntityFromStore(dbHeader);
// update the db entity
dbEntity = new AtlasEntity(TestUtilsV2.DATABASE_TYPE);
dbEntity.setGuid(createdDbEntity.getGuid());
// dbEntity.setAttribute("name", createdDbEntity.getAttribute("name"));
// dbEntity.setAttribute("description", "another db"); // required attr
// optional attr
dbEntity.setAttribute("created", "08151947");
// optional attr
dbEntity.setAttribute("isReplicated", true);
dbStream = new AtlasEntityStream(new AtlasEntitiesWithExtInfo(dbEntity));
// fail full update if required attributes are not specified.
try {
entityStore.createOrUpdate(dbStream, false);
} catch (AtlasBaseException ex) {
Assert.assertEquals(ex.getAtlasErrorCode(), AtlasErrorCode.INSTANCE_CRUD_INVALID_PARAMS);
}
// do partial update without providing required attributes
dbStream.reset();
response = entityStore.createOrUpdate(dbStream, true);
dbHeader = response.getFirstEntityPartialUpdated();
AtlasEntity updatedDbEntity = getEntityFromStore(dbHeader);
assertEquals(updatedDbEntity.getAttribute("name"), createdDbEntity.getAttribute("name"));
assertEquals(updatedDbEntity.getAttribute("description"), createdDbEntity.getAttribute("description"));
assertEquals(updatedDbEntity.getAttribute("isReplicated"), true);
assertEquals(updatedDbEntity.getAttribute("created"), "08151947");
assertEquals(updatedDbEntity.getAttribute("namespace"), createdDbEntity.getAttribute("namespace"));
assertEquals(updatedDbEntity.getAttribute("cluster"), createdDbEntity.getAttribute("cluster"));
assertEquals(updatedDbEntity.getAttribute("colo"), createdDbEntity.getAttribute("colo"));
// create a new table type
AtlasEntity tblEntity = new AtlasEntity(TABLE_TYPE);
tblEntity.setAttribute("name", RandomStringUtils.randomAlphanumeric(10));
tblEntity.setAttribute("type", "type");
tblEntity.setAttribute("tableType", "MANAGED");
tblEntity.setAttribute("database", AtlasTypeUtil.getAtlasObjectId(updatedDbEntity));
// create new column entity
AtlasEntity col1 = TestUtilsV2.createColumnEntity(tblEntity);
AtlasEntity col2 = TestUtilsV2.createColumnEntity(tblEntity);
col1.setAttribute(TestUtilsV2.NAME, "col1");
col2.setAttribute(TestUtilsV2.NAME, "col2");
List<AtlasObjectId> columns = new ArrayList<>();
columns.add(AtlasTypeUtil.getAtlasObjectId(col1));
columns.add(AtlasTypeUtil.getAtlasObjectId(col2));
tblEntity.setAttribute(TestUtilsV2.COLUMNS_ATTR_NAME, columns);
AtlasEntitiesWithExtInfo tableEntityInfo = new AtlasEntitiesWithExtInfo(tblEntity);
tableEntityInfo.addReferredEntity(col1.getGuid(), col1);
tableEntityInfo.addReferredEntity(col2.getGuid(), col2);
EntityStream tblStream = new AtlasEntityStream(tableEntityInfo);
response = entityStore.createOrUpdate(tblStream, false);
AtlasEntityHeader tblHeader = response.getFirstEntityCreated();
AtlasEntity createdTblEntity = getEntityFromStore(tblHeader);
columns = (List<AtlasObjectId>) createdTblEntity.getAttribute(TestUtilsV2.COLUMNS_ATTR_NAME);
assertEquals(columns.size(), 2);
// update - add 2 more columns to table
AtlasEntity col3 = TestUtilsV2.createColumnEntity(createdTblEntity);
col3.setAttribute(TestUtilsV2.NAME, "col3");
col3.setAttribute("description", "description col3");
AtlasEntity col4 = TestUtilsV2.createColumnEntity(createdTblEntity);
col4.setAttribute(TestUtilsV2.NAME, "col4");
col4.setAttribute("description", "description col4");
columns.clear();
columns.add(AtlasTypeUtil.getAtlasObjectId(col3));
columns.add(AtlasTypeUtil.getAtlasObjectId(col4));
tblEntity = new AtlasEntity(TABLE_TYPE);
tblEntity.setGuid(createdTblEntity.getGuid());
tblEntity.setAttribute(TestUtilsV2.COLUMNS_ATTR_NAME, columns);
tableEntityInfo = new AtlasEntitiesWithExtInfo(tblEntity);
tableEntityInfo.addReferredEntity(col3.getGuid(), col3);
tableEntityInfo.addReferredEntity(col4.getGuid(), col4);
tblStream = new AtlasEntityStream(tableEntityInfo);
response = entityStore.createOrUpdate(tblStream, true);
tblHeader = response.getFirstEntityPartialUpdated();
AtlasEntity updatedTblEntity = getEntityFromStore(tblHeader);
columns = (List<AtlasObjectId>) updatedTblEntity.getAttribute(TestUtilsV2.COLUMNS_ATTR_NAME);
// deleted columns are included in the attribute; hence use >=
assertTrue(columns.size() >= 2);
}
use of org.apache.atlas.model.instance.AtlasEntityHeader in project incubator-atlas by apache.
the class AtlasDeleteHandlerV1Test method testLowerBoundsIgnoredWhenDeletingCompositeEntitesOwnedByMap.
@Test
public void testLowerBoundsIgnoredWhenDeletingCompositeEntitesOwnedByMap() throws Exception {
// Define MapValueReferencer type with required reference to CompositeMapValue.
AtlasStructDef.AtlasAttributeDef[] mapValueAttributes = new AtlasStructDef.AtlasAttributeDef[] { new AtlasStructDef.AtlasAttributeDef("refToMapValue", "CompositeMapValue", false, AtlasStructDef.AtlasAttributeDef.Cardinality.SINGLE, 1, 1, false, false, Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()) };
AtlasEntityDef mapValueDef = new AtlasEntityDef("MapValueReferencer", "RequiredMapValue_description", "1.0", Arrays.asList(mapValueAttributes), Collections.<String>emptySet());
AtlasStructDef.AtlasAttributeDef[] mapContainerAttributes = new AtlasStructDef.AtlasAttributeDef[] { new AtlasStructDef.AtlasAttributeDef("requiredMap", "map<string,MapValueReferencer>", false, AtlasStructDef.AtlasAttributeDef.Cardinality.SINGLE, 1, 1, false, false, new ArrayList<AtlasStructDef.AtlasConstraintDef>() {
{
add(new AtlasStructDef.AtlasConstraintDef(AtlasStructDef.AtlasConstraintDef.CONSTRAINT_TYPE_OWNED_REF));
}
}) };
AtlasEntityDef mapContainerDef = new AtlasEntityDef("MapValueReferencerContainer", "MapValueReferencerContainer_description", "1.0", Arrays.asList(mapContainerAttributes), Collections.<String>emptySet());
AtlasTypesDef typesDef = AtlasTypeUtil.getTypesDef(ImmutableList.<AtlasEnumDef>of(), ImmutableList.<AtlasStructDef>of(), ImmutableList.<AtlasClassificationDef>of(), ImmutableList.<AtlasEntityDef>of(mapValueDef, mapContainerDef));
typeDefStore.createTypesDef(typesDef);
// Create instances of CompositeMapOwner and CompositeMapValue.
// Set MapOwner.map with one entry that references MapValue instance.
AtlasEntity.AtlasEntityWithExtInfo entityDefinition = createMapOwnerAndValueEntities();
String mapOwnerGuid = entityDefinition.getEntity().getGuid();
// Verify MapOwner.map attribute has expected value.
ITypedReferenceableInstance mapOwnerInstance = metadataService.getEntityDefinition(mapOwnerGuid);
Object object = mapOwnerInstance.get("map");
Assert.assertNotNull(object);
Assert.assertTrue(object instanceof Map);
Map<String, ITypedReferenceableInstance> map = (Map<String, ITypedReferenceableInstance>) object;
Assert.assertEquals(map.size(), 1);
ITypedReferenceableInstance mapValueInstance = map.get("value1");
Assert.assertNotNull(mapValueInstance);
String mapValueGuid = mapValueInstance.getId()._getId();
// Create instance of MapValueReferencerContainer
RequestContextV1.clear();
AtlasEntity mapValueReferencer = new AtlasEntity(mapValueDef.getName());
mapValueReferencer.setAttribute("refToMapValue", new AtlasObjectId(mapValueInstance.getId()._getId(), mapValueInstance.getTypeName()));
AtlasEntity.AtlasEntitiesWithExtInfo entities = new AtlasEntity.AtlasEntitiesWithExtInfo();
entities.addEntity(mapValueReferencer);
List<AtlasEntityHeader> createEntitiesResult = entityStore.createOrUpdate(new AtlasEntityStream(entities), false).getCreatedEntities();
Assert.assertEquals(createEntitiesResult.size(), 1);
// Create instance of MapValueReferencer, and update mapValueReferencerContainer
// to reference it.
AtlasEntity mapValueReferenceContainer = new AtlasEntity(mapContainerDef.getName());
entities = new AtlasEntity.AtlasEntitiesWithExtInfo();
entities.addEntity(mapValueReferenceContainer);
entities.addReferredEntity(mapValueReferencer);
mapValueReferenceContainer.setAttribute("requiredMap", Collections.singletonMap("value1", AtlasTypeUtil.getAtlasObjectId(mapValueReferencer)));
RequestContextV1.clear();
EntityMutationResponse updateEntitiesResult = entityStore.createOrUpdate(new AtlasEntityStream(entities), false);
String mapValueReferencerContainerGuid = updateEntitiesResult.getCreatedEntitiesByTypeName("MapValueReferencerContainer").get(0).getGuid();
String mapValueReferencerGuid = updateEntitiesResult.getUpdatedEntitiesByTypeName("MapValueReferencer").get(0).getGuid();
Assert.assertEquals(updateEntitiesResult.getCreatedEntities().size(), 1);
Assert.assertEquals(updateEntitiesResult.getUpdatedEntities().size(), 1);
Assert.assertEquals(updateEntitiesResult.getUpdatedEntities().get(0).getGuid(), mapValueReferencerGuid);
// Delete map owner and map referencer container. A total of 4 entities should be deleted,
// including the composite entities. The lower bound constraint on MapValueReferencer.refToMapValue
// should not be enforced on the composite MapValueReferencer since it is being deleted.
EntityMutationResponse deleteEntitiesResult = entityStore.deleteByIds(Arrays.asList(mapOwnerGuid, mapValueReferencerContainerGuid));
Assert.assertEquals(deleteEntitiesResult.getDeletedEntities().size(), 4);
Assert.assertTrue(extractGuids(deleteEntitiesResult.getDeletedEntities()).containsAll(Arrays.asList(mapOwnerGuid, mapValueGuid, mapValueReferencerContainerGuid, mapValueReferencerGuid)));
}
use of org.apache.atlas.model.instance.AtlasEntityHeader in project incubator-atlas by apache.
the class AtlasDeleteHandlerV1Test method testDeleteReference.
@Test
public void testDeleteReference() throws Exception {
//Deleting column should update table
final AtlasEntity dbEntity = TestUtilsV2.createDBEntity();
init();
EntityMutationResponse dbCreationResponse = entityStore.createOrUpdate(new AtlasEntityStream(dbEntity), false);
final AtlasEntity tableEntity = TestUtilsV2.createTableEntity(dbEntity);
final AtlasEntity columnEntity = TestUtilsV2.createColumnEntity(tableEntity);
tableEntity.setAttribute(COLUMNS_ATTR_NAME, Arrays.asList(AtlasTypeUtil.getAtlasObjectId(columnEntity)));
AtlasEntity.AtlasEntityWithExtInfo input = new AtlasEntity.AtlasEntityWithExtInfo(tableEntity);
input.addReferredEntity(columnEntity);
init();
EntityMutationResponse tblCreationResponse = entityStore.createOrUpdate(new AtlasEntityStream(input), false);
final AtlasEntityHeader columnCreated = tblCreationResponse.getFirstCreatedEntityByTypeName(COLUMN_TYPE);
final AtlasEntityHeader tableCreated = tblCreationResponse.getFirstCreatedEntityByTypeName(TABLE_TYPE);
init();
EntityMutationResponse deletionResponse = entityStore.deleteById(columnCreated.getGuid());
assertEquals(deletionResponse.getDeletedEntities().size(), 1);
assertEquals(deletionResponse.getDeletedEntities().get(0).getGuid(), columnCreated.getGuid());
assertEquals(deletionResponse.getUpdatedEntities().size(), 1);
assertEquals(deletionResponse.getUpdatedEntities().get(0).getGuid(), tableCreated.getGuid());
assertEntityDeleted(columnCreated.getGuid());
assertColumnForTestDeleteReference(entityStore.getById(tableCreated.getGuid()));
//Deleting table should update process
AtlasEntity process = TestUtilsV2.createProcessEntity(null, Arrays.asList(AtlasTypeUtil.getAtlasObjectId(tableCreated)));
init();
final EntityMutationResponse processCreationResponse = entityStore.createOrUpdate(new AtlasEntityStream(process), false);
init();
entityStore.deleteById(tableCreated.getGuid());
assertEntityDeleted(tableCreated.getGuid());
assertTableForTestDeleteReference(tableCreated.getGuid());
assertProcessForTestDeleteReference(processCreationResponse.getFirstEntityCreated());
}
use of org.apache.atlas.model.instance.AtlasEntityHeader in project incubator-atlas by apache.
the class AtlasEntityStoreV1Test method testTagAssociationAfterRedefinition.
@Test
public void testTagAssociationAfterRedefinition() {
AtlasClassificationDef aTag = new AtlasClassificationDef("testTag");
AtlasAttributeDef attributeDef = new AtlasAttributeDef("testAttribute", "int", true, AtlasAttributeDef.Cardinality.SINGLE, 0, 1, false, true, Collections.<AtlasStructDef.AtlasConstraintDef>emptyList());
aTag.addAttribute(attributeDef);
AtlasTypesDef typesDef = new AtlasTypesDef();
typesDef.setClassificationDefs(Arrays.asList(aTag));
try {
typeDefStore.createTypesDef(typesDef);
} catch (AtlasBaseException e) {
fail("Tag creation should've succeeded");
}
try {
typeDefStore.deleteTypesDef(typesDef);
} catch (AtlasBaseException e) {
fail("Tag deletion should've succeeded");
}
aTag = new AtlasClassificationDef("testTag");
attributeDef = new AtlasAttributeDef("testAttribute", "string", true, AtlasAttributeDef.Cardinality.SINGLE, 0, 1, false, true, Collections.<AtlasStructDef.AtlasConstraintDef>emptyList());
aTag.addAttribute(attributeDef);
typesDef.setClassificationDefs(Arrays.asList(aTag));
try {
typeDefStore.createTypesDef(typesDef);
} catch (AtlasBaseException e) {
fail("Tag re-creation should've succeeded");
}
final AtlasEntity dbEntity = TestUtilsV2.createDBEntity();
try {
EntityMutationResponse response = entityStore.createOrUpdate(new AtlasEntityStream(dbEntity), false);
List<AtlasEntityHeader> createdEntity = response.getCreatedEntities();
assertTrue(CollectionUtils.isNotEmpty(createdEntity));
String guid = createdEntity.get(0).getGuid();
entityStore.addClassification(Arrays.asList(guid), new AtlasClassification(aTag.getName(), "testAttribute", "test-string"));
} catch (AtlasBaseException e) {
fail("DB entity creation should've succeeded");
}
}
use of org.apache.atlas.model.instance.AtlasEntityHeader 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);
}
Aggregations