GraphBackedMetadataRepository implements MetadataRepository { @Override @GraphTransaction public void deleteTrait(String guid, String traitNameToBeDeleted) throws TraitNotFoundException, EntityNotFoundException, RepositoryException { LOG.debug("Deleting trait={} from entity={}", traitNameToBeDeleted, guid); GraphTransactionInterceptor.lockObjectAndReleasePostCommit(guid); AtlasVertex instanceVertex = graphHelper.getVertexForGUID(guid); List<String> traitNames = GraphHelper.getTraitNames(instanceVertex); if (!traitNames.contains(traitNameToBeDeleted)) { throw new TraitNotFoundException( "Could not find trait=" + traitNameToBeDeleted + " in the repository for entity: " + guid); } try { final String entityTypeName = GraphHelper.getTypeName(instanceVertex); String relationshipLabel = GraphHelper.getTraitLabel(entityTypeName, traitNameToBeDeleted); AtlasEdge edge = graphHelper.getEdgeForLabel(instanceVertex, relationshipLabel); if(edge != null) { deleteHandler.deleteEdgeReference(edge, DataTypes.TypeCategory.TRAIT, false, true); } traitNames.remove(traitNameToBeDeleted); updateTraits(instanceVertex, traitNames); } catch (Exception e) { throw new RepositoryException(e); } } @Inject  GraphBackedMetadataRepository(DeleteHandler deleteHandler, AtlasGraph atlasGraph); GraphToTypedInstanceMapper getGraphToInstanceMapper(); @Override String getTypeAttributeName(); @Override String getStateAttributeName(); @Override String getSuperTypeAttributeName(); String getIdAttributeName(); @Override String getVersionAttributeName(); @Override String getTraitLabel(IDataType<?> dataType, String traitName); @Override String getFieldNameInVertex(IDataType<?> dataType, AttributeInfo aInfo); String getFieldNameInVertex(IDataType<?> dataType, String attrName); @Override String getEdgeLabel(IDataType<?> dataType, AttributeInfo aInfo); @Override @GraphTransaction CreateUpdateEntitiesResult createEntities(ITypedReferenceableInstance... entities); @Override @GraphTransaction ITypedReferenceableInstance getEntityDefinition(String guid); @Override @GraphTransaction List<ITypedReferenceableInstance> getEntityDefinitions(String... guids); @Override @GraphTransaction ITypedReferenceableInstance getEntityDefinition(String entityType, String attribute, Object value); @Override @GraphTransaction List<String> getEntityList(String entityType); @Override @GraphTransaction List<String> getTraitNames(String guid); @Override @GraphTransaction void addTrait(List<String> entityGuids, ITypedStruct traitInstance); @Override @GraphTransaction void addTrait(String guid, ITypedStruct traitInstance); @Override @GraphTransaction void deleteTrait(String guid, String traitNameToBeDeleted); @Override @GraphTransaction CreateUpdateEntitiesResult updateEntities(ITypedReferenceableInstance... entitiesUpdated); @Override @GraphTransaction CreateUpdateEntitiesResult updatePartial(ITypedReferenceableInstance entity); @Override @GraphTransaction EntityResult deleteEntities(List<String> guids); AtlasGraph getGraph();  }
@Test(dependsOnMethods = "testAddTrait") public void testDeleteTrait() throws Exception { final String aGUID = getGUID(); AtlasVertex AtlasVertex = GraphHelper.getInstance().getVertexForGUID(aGUID); Long modificationTimestampPreUpdate = GraphHelper.getSingleValuedProperty(AtlasVertex, Constants.MODIFICATION_TIMESTAMP_PROPERTY_KEY, Long.class); Assert.assertNotNull(modificationTimestampPreUpdate); List<String> traitNames = repositoryService.getTraitNames(aGUID); Assert.assertEquals(traitNames.size(), 3); Assert.assertTrue(traitNames.contains(TestUtils.PII)); Assert.assertTrue(traitNames.contains(TestUtils.CLASSIFICATION)); Assert.assertTrue(traitNames.contains("P_I_I")); repositoryService.deleteTrait(aGUID, TestUtils.PII); traitNames = repositoryService.getTraitNames(aGUID); Assert.assertEquals(traitNames.size(), 2); Assert.assertTrue(traitNames.contains(TestUtils.CLASSIFICATION)); Assert.assertFalse(traitNames.contains(TestUtils.PII)); GraphHelper.getInstance().getVertexForGUID(aGUID); Long modificationTimestampPostUpdate = GraphHelper.getSingleValuedProperty(AtlasVertex, Constants.MODIFICATION_TIMESTAMP_PROPERTY_KEY, Long.class); Assert.assertNotNull(modificationTimestampPostUpdate); Assert.assertTrue(modificationTimestampPostUpdate > modificationTimestampPreUpdate); }
@Test(expectedExceptions = EntityNotFoundException.class) public void testDeleteTraitForNonExistentEntity() throws Exception { repositoryService.deleteTrait(UUID.randomUUID().toString(), TestUtils.PII); Assert.fail(); }
@Test(expectedExceptions = TraitNotFoundException.class) public void testDeleteTraitForNonExistentTrait() throws Exception { final String aGUID = getGUID(); repositoryService.deleteTrait(aGUID, "PCI"); Assert.fail(); }