use of org.apache.atlas.model.typedef.AtlasEntityDef in project incubator-atlas by apache.
the class AtlasDeleteHandlerV1Test method testDeleteTargetOfRequiredMapReference.
@Test
public void testDeleteTargetOfRequiredMapReference() throws Exception {
// Define type for map value.
AtlasEntityDef mapValueDef = new AtlasEntityDef("RequiredMapValue", "RequiredMapValue_description", "1.0", Collections.<AtlasStructDef.AtlasAttributeDef>emptyList(), Collections.<String>emptySet());
AtlasStructDef.AtlasAttributeDef[] mapOwnerAttributes = new AtlasStructDef.AtlasAttributeDef[] { new AtlasStructDef.AtlasAttributeDef("map", "map<string,RequiredMapValue>", false, AtlasStructDef.AtlasAttributeDef.Cardinality.SINGLE, 1, 1, false, false, Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()) };
AtlasEntityDef mapOwnerDef = new AtlasEntityDef("RequiredMapOwner", "RequiredMapOwner_description", "1.0", Arrays.asList(mapOwnerAttributes), Collections.<String>emptySet());
AtlasTypesDef typesDef = AtlasTypeUtil.getTypesDef(ImmutableList.<AtlasEnumDef>of(), ImmutableList.<AtlasStructDef>of(), ImmutableList.<AtlasClassificationDef>of(), ImmutableList.<AtlasEntityDef>of(mapValueDef, mapOwnerDef));
typeDefStore.createTypesDef(typesDef);
AtlasEntityType mapOwnerType = typeRegistry.getEntityTypeByName("RequiredMapOwner");
AtlasEntityType mapValueType = typeRegistry.getEntityTypeByName("RequiredMapValue");
// Create instances of RequiredMapOwner and RequiredMapValue.
// Set RequiredMapOwner.map with one entry that references RequiredMapValue instance.
AtlasEntity mapOwnerInstance = new AtlasEntity(mapOwnerType.getTypeName());
AtlasEntity mapValueInstance = new AtlasEntity(mapValueType.getTypeName());
mapOwnerInstance.setAttribute("map", Collections.singletonMap("value1", AtlasTypeUtil.getAtlasObjectId(mapValueInstance)));
AtlasEntity.AtlasEntitiesWithExtInfo entities = new AtlasEntity.AtlasEntitiesWithExtInfo();
entities.addReferredEntity(mapValueInstance);
entities.addEntity(mapOwnerInstance);
List<AtlasEntityHeader> createEntitiesResult = entityStore.createOrUpdate(new AtlasEntityStream(entities), false).getCreatedEntities();
Assert.assertEquals(createEntitiesResult.size(), 2);
List<String> guids = metadataService.getEntityList("RequiredMapOwner");
Assert.assertEquals(guids.size(), 1);
String mapOwnerGuid = guids.get(0);
guids = metadataService.getEntityList("RequiredMapValue");
Assert.assertEquals(guids.size(), 1);
String mapValueGuid = guids.get(0);
// Verify MapOwner.map attribute has expected value.
final AtlasEntity.AtlasEntityWithExtInfo mapOwnerInstance1 = entityStore.getById(mapOwnerGuid);
Object object = mapOwnerInstance1.getEntity().getAttribute("map");
Assert.assertNotNull(object);
Assert.assertTrue(object instanceof Map);
Map<String, AtlasObjectId> map = (Map<String, AtlasObjectId>) object;
Assert.assertEquals(map.size(), 1);
AtlasObjectId mapValueInstance1 = map.get("value1");
Assert.assertNotNull(mapValueInstance1);
Assert.assertEquals(mapValueInstance1.getGuid(), mapValueGuid);
String edgeLabel = AtlasGraphUtilsV1.getAttributeEdgeLabel(mapOwnerType, "map");
String mapEntryLabel = edgeLabel + "." + "value1";
AtlasEdgeLabel atlasEdgeLabel = new AtlasEdgeLabel(mapEntryLabel);
AtlasVertex mapOwnerVertex = GraphHelper.getInstance().getVertexForGUID(mapOwnerGuid);
object = mapOwnerVertex.getProperty(atlasEdgeLabel.getQualifiedMapKey(), Object.class);
Assert.assertNotNull(object);
// Verify deleting the target of required map attribute throws a AtlasBaseException.
try {
entityStore.deleteById(mapValueGuid);
Assert.fail(AtlasBaseException.class.getSimpleName() + " was expected but none thrown.");
} catch (Exception e) {
verifyExceptionThrown(e, AtlasBaseException.class);
}
}
use of org.apache.atlas.model.typedef.AtlasEntityDef in project incubator-atlas by apache.
the class AtlasTypeDefGraphStoreTest method testUpdateWithMandatoryFields.
@Test(enabled = false, dependsOnMethods = { "testCreateDept" })
public void testUpdateWithMandatoryFields() {
AtlasTypesDef atlasTypesDef = TestUtilsV2.defineInvalidUpdatedDeptEmployeeTypes();
List<AtlasEnumDef> enumDefsToUpdate = atlasTypesDef.getEnumDefs();
List<AtlasClassificationDef> classificationDefsToUpdate = atlasTypesDef.getClassificationDefs();
List<AtlasStructDef> structDefsToUpdate = atlasTypesDef.getStructDefs();
List<AtlasEntityDef> entityDefsToUpdate = atlasTypesDef.getEntityDefs();
AtlasTypesDef onlyEnums = new AtlasTypesDef(enumDefsToUpdate, Collections.EMPTY_LIST, Collections.EMPTY_LIST, Collections.EMPTY_LIST);
AtlasTypesDef onlyStructs = new AtlasTypesDef(Collections.EMPTY_LIST, structDefsToUpdate, Collections.EMPTY_LIST, Collections.EMPTY_LIST);
AtlasTypesDef onlyClassification = new AtlasTypesDef(Collections.EMPTY_LIST, Collections.EMPTY_LIST, classificationDefsToUpdate, Collections.EMPTY_LIST);
AtlasTypesDef onlyEntities = new AtlasTypesDef(Collections.EMPTY_LIST, Collections.EMPTY_LIST, Collections.EMPTY_LIST, entityDefsToUpdate);
try {
AtlasTypesDef updated = typeDefStore.updateTypesDef(onlyEnums);
assertNotNull(updated);
} catch (AtlasBaseException ignored) {
}
try {
AtlasTypesDef updated = typeDefStore.updateTypesDef(onlyClassification);
assertNotNull(updated);
assertEquals(updated.getClassificationDefs().size(), 0, "Updates should've failed");
} catch (AtlasBaseException ignored) {
}
try {
AtlasTypesDef updated = typeDefStore.updateTypesDef(onlyStructs);
assertNotNull(updated);
assertEquals(updated.getStructDefs().size(), 0, "Updates should've failed");
} catch (AtlasBaseException ignored) {
}
try {
AtlasTypesDef updated = typeDefStore.updateTypesDef(onlyEntities);
assertNotNull(updated);
assertEquals(updated.getEntityDefs().size(), 0, "Updates should've failed");
} catch (AtlasBaseException ignored) {
}
}
use of org.apache.atlas.model.typedef.AtlasEntityDef in project incubator-atlas by apache.
the class AtlasTypeDefGraphStoreTest method testUpdate.
@Test(dependsOnMethods = { "testCreateDept" }, dataProvider = "validUpdateDeptTypes")
public void testUpdate(AtlasTypesDef atlasTypesDef) {
try {
AtlasTypesDef updatedTypesDef = typeDefStore.updateTypesDef(atlasTypesDef);
assertNotNull(updatedTypesDef);
assertEquals(updatedTypesDef.getEnumDefs().size(), atlasTypesDef.getEnumDefs().size(), "EnumDefs update failed");
assertEquals(updatedTypesDef.getClassificationDefs().size(), atlasTypesDef.getClassificationDefs().size(), "ClassificationDef update failed");
assertEquals(updatedTypesDef.getStructDefs().size(), atlasTypesDef.getStructDefs().size(), "StructDef update failed");
assertEquals(updatedTypesDef.getEntityDefs().size(), atlasTypesDef.getEntityDefs().size(), "EntityDef update failed");
// Try another update round by name and GUID
for (AtlasEnumDef enumDef : updatedTypesDef.getEnumDefs()) {
AtlasEnumDef updated = typeDefStore.updateEnumDefByGuid(enumDef.getGuid(), enumDef);
assertNotNull(updated);
}
for (AtlasEnumDef enumDef : atlasTypesDef.getEnumDefs()) {
AtlasEnumDef updated = typeDefStore.updateEnumDefByName(enumDef.getName(), enumDef);
assertNotNull(updated);
}
// Try another update round by name and GUID
for (AtlasClassificationDef classificationDef : updatedTypesDef.getClassificationDefs()) {
AtlasClassificationDef updated = typeDefStore.updateClassificationDefByGuid(classificationDef.getGuid(), classificationDef);
assertNotNull(updated);
}
for (AtlasClassificationDef classificationDef : atlasTypesDef.getClassificationDefs()) {
AtlasClassificationDef updated = typeDefStore.updateClassificationDefByName(classificationDef.getName(), classificationDef);
assertNotNull(updated);
}
// Try another update round by name and GUID
for (AtlasStructDef structDef : updatedTypesDef.getStructDefs()) {
AtlasStructDef updated = typeDefStore.updateStructDefByGuid(structDef.getGuid(), structDef);
assertNotNull(updated);
}
for (AtlasStructDef structDef : atlasTypesDef.getStructDefs()) {
AtlasStructDef updated = typeDefStore.updateStructDefByName(structDef.getName(), structDef);
assertNotNull(updated);
}
// Try another update round by name and GUID
for (AtlasEntityDef entityDef : updatedTypesDef.getEntityDefs()) {
AtlasEntityDef updated = typeDefStore.updateEntityDefByGuid(entityDef.getGuid(), entityDef);
assertNotNull(updated);
}
for (AtlasEntityDef entityDef : atlasTypesDef.getEntityDefs()) {
AtlasEntityDef updated = typeDefStore.updateEntityDefByName(entityDef.getName(), entityDef);
assertNotNull(updated);
}
} catch (AtlasBaseException e) {
fail("TypeDef updates should've succeeded");
}
}
use of org.apache.atlas.model.typedef.AtlasEntityDef in project incubator-atlas by apache.
the class BaseResourceIT method batchCreateTypes.
protected void batchCreateTypes(AtlasTypesDef typesDef) throws AtlasServiceException {
AtlasTypesDef toCreate = new AtlasTypesDef();
for (AtlasEnumDef enumDef : typesDef.getEnumDefs()) {
if (atlasClientV2.typeWithNameExists(enumDef.getName())) {
LOG.warn("Type with name {} already exists. Skipping", enumDef.getName());
} else {
toCreate.getEnumDefs().add(enumDef);
}
}
for (AtlasStructDef structDef : typesDef.getStructDefs()) {
if (atlasClientV2.typeWithNameExists(structDef.getName())) {
LOG.warn("Type with name {} already exists. Skipping", structDef.getName());
} else {
toCreate.getStructDefs().add(structDef);
}
}
for (AtlasEntityDef entityDef : typesDef.getEntityDefs()) {
if (atlasClientV2.typeWithNameExists(entityDef.getName())) {
LOG.warn("Type with name {} already exists. Skipping", entityDef.getName());
} else {
toCreate.getEntityDefs().add(entityDef);
}
}
for (AtlasClassificationDef classificationDef : typesDef.getClassificationDefs()) {
if (atlasClientV2.typeWithNameExists(classificationDef.getName())) {
LOG.warn("Type with name {} already exists. Skipping", classificationDef.getName());
} else {
toCreate.getClassificationDefs().add(classificationDef);
}
}
atlasClientV2.createAtlasTypeDefs(toCreate);
}
use of org.apache.atlas.model.typedef.AtlasEntityDef in project incubator-atlas by apache.
the class DefaultTypeSystem method createType.
private <T extends HierarchicalType> void createType(Collection<AttributeDefinition> attributes, Class<T> type, String name, String description, boolean isTrait) throws ResourceAlreadyExistsException {
try {
List<AtlasStructDef.AtlasAttributeDef> attrDefs = new ArrayList<>();
for (AttributeDefinition attrDefinition : attributes) {
attrDefs.add(TypeConverterUtil.toAtlasAttributeDef(attrDefinition));
}
if (isTrait) {
AtlasClassificationDef classificationDef = new AtlasClassificationDef(name, description, "1.0", attrDefs, ImmutableSet.of(TaxonomyResourceProvider.TAXONOMY_TERM_TYPE));
AtlasTypesDef typesDef = new AtlasTypesDef(ImmutableList.<AtlasEnumDef>of(), ImmutableList.<AtlasStructDef>of(), ImmutableList.of(classificationDef), ImmutableList.<AtlasEntityDef>of());
typeDefStore.createTypesDef(typesDef);
} else {
AtlasEntityDef entityDef = new AtlasEntityDef(name, description, "1.0", attrDefs);
AtlasTypesDef typesDef = new AtlasTypesDef(ImmutableList.<AtlasEnumDef>of(), ImmutableList.<AtlasStructDef>of(), ImmutableList.<AtlasClassificationDef>of(), ImmutableList.of(entityDef));
typeDefStore.createTypesDef(typesDef);
}
} catch (AtlasBaseException e) {
if (e.getAtlasErrorCode() == AtlasErrorCode.TYPE_ALREADY_EXISTS) {
throw new ResourceAlreadyExistsException(String.format("Type '%s' already exists", name));
} else {
throw new CatalogRuntimeException(String.format("Unable to create type '%s' in type system: %s", name, e), e);
}
}
}
Aggregations