Commit aa67f8ae by Suma Shivaprasad

ATLAS-1547 Add tests for DeleteHandlerV1 (sumasai via mneethiraj)

parent c7540b38
...@@ -320,7 +320,7 @@ ...@@ -320,7 +320,7 @@
} }
], ],
"isIndexable": false, "isIndexable": false,
"isOptional": false, "isOptional": true,
"isUnique": false "isUnique": false
}, },
{ {
...@@ -483,7 +483,7 @@ ...@@ -483,7 +483,7 @@
} }
], ],
"isIndexable": false, "isIndexable": false,
"isOptional": false, "isOptional": true,
"isUnique": false "isUnique": false
} }
] ]
......
...@@ -33,6 +33,8 @@ import org.apache.atlas.model.typedef.AtlasEntityDef; ...@@ -33,6 +33,8 @@ import org.apache.atlas.model.typedef.AtlasEntityDef;
import org.codehaus.jackson.annotate.JsonAutoDetect; import org.codehaus.jackson.annotate.JsonAutoDetect;
import static org.codehaus.jackson.annotate.JsonAutoDetect.Visibility.PUBLIC_ONLY; import static org.codehaus.jackson.annotate.JsonAutoDetect.Visibility.PUBLIC_ONLY;
import static org.codehaus.jackson.annotate.JsonAutoDetect.Visibility.NONE; import static org.codehaus.jackson.annotate.JsonAutoDetect.Visibility.NONE;
import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.annotate.JsonIgnoreProperties; import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.map.annotate.JsonSerialize; import org.codehaus.jackson.map.annotate.JsonSerialize;
...@@ -189,4 +191,10 @@ public class AtlasEntityHeader extends AtlasStruct implements Serializable { ...@@ -189,4 +191,10 @@ public class AtlasEntityHeader extends AtlasStruct implements Serializable {
super(list, startIndex, pageSize, totalCount, sortType, sortBy); super(list, startIndex, pageSize, totalCount, sortType, sortBy);
} }
} }
@JsonIgnore
public AtlasObjectId getAtlasObjectId() {
return new AtlasObjectId(getGuid(), getTypeName());
}
} }
...@@ -104,7 +104,7 @@ public class AtlasStruct implements Serializable { ...@@ -104,7 +104,7 @@ public class AtlasStruct implements Serializable {
public boolean hasAttribute(String name) { public boolean hasAttribute(String name) {
Map<String, Object> a = this.attributes; Map<String, Object> a = this.attributes;
return a != null ? a.containsKey(name) : null; return a != null ? a.containsKey(name) : false;
} }
public Object getAttribute(String name) { public Object getAttribute(String name) {
......
...@@ -54,7 +54,7 @@ public class EntityMutationResponse { ...@@ -54,7 +54,7 @@ public class EntityMutationResponse {
this.entitiesMutated = opVsEntityMap; this.entitiesMutated = opVsEntityMap;
} }
public Map<EntityMutations.EntityOperation, List<AtlasEntityHeader>> getEntitiesMutated() { public Map<EntityMutations.EntityOperation, List<AtlasEntityHeader>> getMutatedEntities() {
return entitiesMutated; return entitiesMutated;
} }
...@@ -69,6 +69,27 @@ public class EntityMutationResponse { ...@@ -69,6 +69,27 @@ public class EntityMutationResponse {
return null; return null;
} }
public List<AtlasEntityHeader> getCreatedEntities() {
if ( entitiesMutated != null) {
return entitiesMutated.get(EntityMutations.EntityOperation.CREATE);
}
return null;
}
public List<AtlasEntityHeader> getUpdatedEntities() {
if ( entitiesMutated != null) {
return entitiesMutated.get(EntityMutations.EntityOperation.UPDATE);
}
return null;
}
public List<AtlasEntityHeader> getDeletedEntities() {
if ( entitiesMutated != null) {
return entitiesMutated.get(EntityMutations.EntityOperation.DELETE);
}
return null;
}
@JsonIgnore @JsonIgnore
public AtlasEntityHeader getFirstEntityCreated() { public AtlasEntityHeader getFirstEntityCreated() {
final List<AtlasEntityHeader> entitiesByOperation = getEntitiesByOperation(EntityMutations.EntityOperation.CREATE); final List<AtlasEntityHeader> entitiesByOperation = getEntitiesByOperation(EntityMutations.EntityOperation.CREATE);
...@@ -91,30 +112,42 @@ public class EntityMutationResponse { ...@@ -91,30 +112,42 @@ public class EntityMutationResponse {
@JsonIgnore @JsonIgnore
public AtlasEntityHeader getFirstCreatedEntityByTypeName(String typeName) { public AtlasEntityHeader getFirstCreatedEntityByTypeName(String typeName) {
final List<AtlasEntityHeader> entitiesByOperation = getEntitiesByOperation(EntityMutations.EntityOperation.CREATE); return getFirstEntityByType(getEntitiesByOperation(EntityMutations.EntityOperation.CREATE), typeName);
if ( entitiesByOperation != null && entitiesByOperation.size() > 0) { }
for (AtlasEntityHeader header : entitiesByOperation) {
if ( header.getTypeName().equals(typeName)) {
return header;
}
}
}
return null; @JsonIgnore
public AtlasEntityHeader getFirstDeletedEntityByTypeName(String typeName) {
return getFirstEntityByType(getEntitiesByOperation(EntityMutations.EntityOperation.DELETE), typeName);
} }
@JsonIgnore @JsonIgnore
public AtlasEntityHeader getFirstUpdatedEntityByTypeName(String typeName) { public List<AtlasEntityHeader> getCreatedEntitiesByTypeName(String typeName) {
final List<AtlasEntityHeader> entitiesByOperation = getEntitiesByOperation(EntityMutations.EntityOperation.UPDATE); return getEntitiesByType(getEntitiesByOperation(EntityMutations.EntityOperation.CREATE), typeName);
if ( entitiesByOperation != null && entitiesByOperation.size() > 0) { }
for (AtlasEntityHeader header : entitiesByOperation) {
if ( header.getTypeName().equals(typeName)) {
return header;
}
}
}
return null; @JsonIgnore
public AtlasEntityHeader getCreatedEntityByTypeNameAndAttribute(String typeName, String attrName, String attrVal) {
return getEntityByTypeAndUniqueAttribute(getEntitiesByOperation(EntityMutations.EntityOperation.CREATE), typeName, attrName, attrVal);
}
@JsonIgnore
public AtlasEntityHeader getUpdatedEntityByTypeNameAndAttribute(String typeName, String attrName, String attrVal) {
return getEntityByTypeAndUniqueAttribute(getEntitiesByOperation(EntityMutations.EntityOperation.UPDATE), typeName, attrName, attrVal);
}
@JsonIgnore
public List<AtlasEntityHeader> getUpdatedEntitiesByTypeName(String typeName) {
return getEntitiesByType(getEntitiesByOperation(EntityMutations.EntityOperation.UPDATE), typeName);
}
@JsonIgnore
public List<AtlasEntityHeader> getDeletedEntitiesByTypeName(String typeName) {
return getEntitiesByType(getEntitiesByOperation(EntityMutations.EntityOperation.DELETE), typeName);
}
@JsonIgnore
public AtlasEntityHeader getFirstUpdatedEntityByTypeName(String typeName) {
return getFirstEntityByType(getEntitiesByOperation(EntityMutations.EntityOperation.UPDATE), typeName);
} }
public void addEntity(EntityMutations.EntityOperation op, AtlasEntityHeader header) { public void addEntity(EntityMutations.EntityOperation op, AtlasEntityHeader header) {
...@@ -162,6 +195,42 @@ public class EntityMutationResponse { ...@@ -162,6 +195,42 @@ public class EntityMutationResponse {
return toString(new StringBuilder()).toString(); return toString(new StringBuilder()).toString();
} }
private AtlasEntityHeader getFirstEntityByType(List<AtlasEntityHeader> entitiesByOperation, String typeName) {
if ( entitiesByOperation != null && entitiesByOperation.size() > 0) {
for (AtlasEntityHeader header : entitiesByOperation) {
if ( header.getTypeName().equals(typeName)) {
return header;
}
}
}
return null;
}
private List<AtlasEntityHeader> getEntitiesByType(List<AtlasEntityHeader> entitiesByOperation, String typeName) {
List<AtlasEntityHeader> ret = new ArrayList<>();
if ( entitiesByOperation != null && entitiesByOperation.size() > 0) {
for (AtlasEntityHeader header : entitiesByOperation) {
if ( header.getTypeName().equals(typeName)) {
ret.add(header);
}
}
}
return ret;
}
private AtlasEntityHeader getEntityByTypeAndUniqueAttribute(List<AtlasEntityHeader> entitiesByOperation, String typeName, String attrName, String attrVal) {
if (entitiesByOperation != null && entitiesByOperation.size() > 0) {
for (AtlasEntityHeader header : entitiesByOperation) {
if (header.getTypeName().equals(typeName)) {
if (attrVal != null && attrVal.equals(header.getAttribute(attrName))) {
return header;
}
}
}
}
return null;
}
public void setGuidAssignments(Map<String,String> guidAssignments) { public void setGuidAssignments(Map<String,String> guidAssignments) {
this.guidAssignments = guidAssignments; this.guidAssignments = guidAssignments;
} }
......
...@@ -390,8 +390,6 @@ public class AtlasStructDef extends AtlasBaseTypeDef implements Serializable { ...@@ -390,8 +390,6 @@ public class AtlasStructDef extends AtlasBaseTypeDef implements Serializable {
cDefs = new ArrayList<>(); cDefs = new ArrayList<>();
this.constraints = cDefs; this.constraints = cDefs;
} else {
cDefs = new ArrayList<>(cDefs);
} }
cDefs.add(constraintDef); cDefs.add(constraintDef);
......
...@@ -540,8 +540,7 @@ public final class GraphHelper { ...@@ -540,8 +540,7 @@ public final class GraphHelper {
* *
* @return propertyValue to AtlasVertex map with the result. * @return propertyValue to AtlasVertex map with the result.
*/ */
public Map<String, AtlasVertex> getVerticesForPropertyValues(String property, List<String> values) public Map<String, AtlasVertex> getVerticesForPropertyValues(String property, List<String> values) {
throws RepositoryException {
if(values.isEmpty()) { if(values.isEmpty()) {
return Collections.emptyMap(); return Collections.emptyMap();
...@@ -581,8 +580,7 @@ public final class GraphHelper { ...@@ -581,8 +580,7 @@ public final class GraphHelper {
* *
* @return GUID to AtlasVertex map with the result. * @return GUID to AtlasVertex map with the result.
*/ */
public Map<String, AtlasVertex> getVerticesForGUIDs(List<String> guids) public Map<String, AtlasVertex> getVerticesForGUIDs(List<String> guids) {
throws RepositoryException {
return getVerticesForPropertyValues(Constants.GUID_PROPERTY_KEY, guids); return getVerticesForPropertyValues(Constants.GUID_PROPERTY_KEY, guids);
} }
......
...@@ -21,24 +21,35 @@ package org.apache.atlas.repository.store.graph.v1; ...@@ -21,24 +21,35 @@ package org.apache.atlas.repository.store.graph.v1;
import com.google.inject.Inject; import com.google.inject.Inject;
import com.google.inject.Singleton; import com.google.inject.Singleton;
import org.apache.atlas.AtlasErrorCode; import org.apache.atlas.AtlasErrorCode;
import org.apache.atlas.AtlasException;
import org.apache.atlas.GraphTransaction; import org.apache.atlas.GraphTransaction;
import org.apache.atlas.RequestContext;
import org.apache.atlas.RequestContextV1; import org.apache.atlas.RequestContextV1;
import org.apache.atlas.exception.AtlasBaseException; import org.apache.atlas.exception.AtlasBaseException;
import org.apache.atlas.model.instance.AtlasClassification; import org.apache.atlas.model.instance.AtlasClassification;
import org.apache.atlas.model.instance.AtlasEntity; import org.apache.atlas.model.instance.AtlasEntity;
import org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo; import org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo;
import org.apache.atlas.model.instance.AtlasEntity.AtlasEntitiesWithExtInfo; import org.apache.atlas.model.instance.AtlasEntity.AtlasEntitiesWithExtInfo;
import org.apache.atlas.model.instance.AtlasObjectId;
import org.apache.atlas.model.instance.EntityMutationResponse; import org.apache.atlas.model.instance.EntityMutationResponse;
import org.apache.atlas.model.instance.EntityMutations;
import org.apache.atlas.repository.graph.GraphHelper;
import org.apache.atlas.repository.graphdb.AtlasVertex; import org.apache.atlas.repository.graphdb.AtlasVertex;
import org.apache.atlas.repository.store.graph.AtlasEntityStore; import org.apache.atlas.repository.store.graph.AtlasEntityStore;
import org.apache.atlas.repository.store.graph.EntityGraphDiscovery; import org.apache.atlas.repository.store.graph.EntityGraphDiscovery;
import org.apache.atlas.repository.store.graph.EntityGraphDiscoveryContext; import org.apache.atlas.repository.store.graph.EntityGraphDiscoveryContext;
import org.apache.atlas.type.AtlasEntityType; import org.apache.atlas.type.AtlasEntityType;
import org.apache.atlas.type.AtlasTypeRegistry; import org.apache.atlas.type.AtlasTypeRegistry;
import org.apache.commons.lang.StringUtils; import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
...@@ -47,7 +58,6 @@ import java.util.Map; ...@@ -47,7 +58,6 @@ import java.util.Map;
public class AtlasEntityStoreV1 implements AtlasEntityStore { public class AtlasEntityStoreV1 implements AtlasEntityStore {
private static final Logger LOG = LoggerFactory.getLogger(AtlasEntityStoreV1.class); private static final Logger LOG = LoggerFactory.getLogger(AtlasEntityStoreV1.class);
private final DeleteHandlerV1 deleteHandler; private final DeleteHandlerV1 deleteHandler;
private final AtlasTypeRegistry typeRegistry; private final AtlasTypeRegistry typeRegistry;
...@@ -101,6 +111,7 @@ public class AtlasEntityStoreV1 implements AtlasEntityStore { ...@@ -101,6 +111,7 @@ public class AtlasEntityStoreV1 implements AtlasEntityStore {
@GraphTransaction @GraphTransaction
public AtlasEntityWithExtInfo getByUniqueAttributes(AtlasEntityType entityType, Map<String, Object> uniqAttributes) public AtlasEntityWithExtInfo getByUniqueAttributes(AtlasEntityType entityType, Map<String, Object> uniqAttributes)
throws AtlasBaseException { throws AtlasBaseException {
if (LOG.isDebugEnabled()) { if (LOG.isDebugEnabled()) {
LOG.debug("==> getByUniqueAttribute({}, {})", entityType.getTypeName(), uniqAttributes); LOG.debug("==> getByUniqueAttribute({}, {})", entityType.getTypeName(), uniqAttributes);
} }
...@@ -113,7 +124,7 @@ public class AtlasEntityStoreV1 implements AtlasEntityStore { ...@@ -113,7 +124,7 @@ public class AtlasEntityStoreV1 implements AtlasEntityStore {
if (ret == null) { if (ret == null) {
throw new AtlasBaseException(AtlasErrorCode.INSTANCE_BY_UNIQUE_ATTRIBUTE_NOT_FOUND, entityType.getTypeName(), throw new AtlasBaseException(AtlasErrorCode.INSTANCE_BY_UNIQUE_ATTRIBUTE_NOT_FOUND, entityType.getTypeName(),
uniqAttributes.toString()); uniqAttributes.toString());
} }
if (LOG.isDebugEnabled()) { if (LOG.isDebugEnabled()) {
...@@ -157,23 +168,73 @@ public class AtlasEntityStoreV1 implements AtlasEntityStore { ...@@ -157,23 +168,73 @@ public class AtlasEntityStoreV1 implements AtlasEntityStore {
throw new AtlasBaseException(AtlasErrorCode.INTERNAL_ERROR, "updateByUniqueAttributes() not implemented yet"); throw new AtlasBaseException(AtlasErrorCode.INTERNAL_ERROR, "updateByUniqueAttributes() not implemented yet");
} }
@Override
@GraphTransaction @GraphTransaction
public EntityMutationResponse deleteById(String guid) throws AtlasBaseException { public EntityMutationResponse deleteById(final String guid) throws AtlasBaseException {
throw new AtlasBaseException(AtlasErrorCode.INTERNAL_ERROR, "deleteById() not implemented yet");
if (StringUtils.isEmpty(guid)) {
throw new AtlasBaseException(AtlasErrorCode.INSTANCE_GUID_NOT_FOUND, guid);
}
// Retrieve vertices for requested guids.
AtlasVertex vertex = AtlasGraphUtilsV1.findByGuid(guid);
if (LOG.isDebugEnabled()) {
if (vertex == null) {
// Entity does not exist - treat as non-error, since the caller
// wanted to delete the entity and it's already gone.
LOG.debug("Deletion request ignored for non-existent entity with guid " + guid);
}
}
Collection<AtlasVertex> deletionCandidates = new ArrayList<AtlasVertex>();
deletionCandidates.add(vertex);
return deleteVertices(deletionCandidates);
} }
@Override @Override
@GraphTransaction @GraphTransaction
public EntityMutationResponse deleteByUniqueAttributes(AtlasEntityType entityType, Map<String, Object> uniqAttributes) public EntityMutationResponse deleteByIds(final List<String> guids) throws AtlasBaseException {
throws AtlasBaseException { if (CollectionUtils.isEmpty(guids)) {
throw new AtlasBaseException(AtlasErrorCode.INTERNAL_ERROR, "deleteByUniqueAttributes() not implemented yet"); throw new AtlasBaseException(AtlasErrorCode.INSTANCE_GUID_NOT_FOUND, guids);
}
Collection<AtlasVertex> deletionCandidates = new ArrayList<>();
for (String guid : guids) {
// Retrieve vertices for requested guids.
AtlasVertex vertex = AtlasGraphUtilsV1.findByGuid(guid);
if (LOG.isDebugEnabled()) {
if (vertex == null) {
// Entity does not exist - treat as non-error, since the caller
// wanted to delete the entity and it's already gone.
LOG.debug("Deletion request ignored for non-existent entity with guid " + guid);
}
}
deletionCandidates.add(vertex);
}
if (deletionCandidates.isEmpty()) {
LOG.info("No deletion candidate entities were found for guids %s", guids);
}
return deleteVertices(deletionCandidates);
} }
@Override @Override
@GraphTransaction @GraphTransaction
public EntityMutationResponse deleteByIds(List<String> guids) throws AtlasBaseException { public EntityMutationResponse deleteByUniqueAttributes(AtlasEntityType entityType, Map<String, Object> uniqAttributes)
throw new AtlasBaseException(AtlasErrorCode.INTERNAL_ERROR, "deleteByIds() not implemented yet"); throws AtlasBaseException {
if (MapUtils.isEmpty(uniqAttributes)) {
throw new AtlasBaseException(AtlasErrorCode.INSTANCE_BY_UNIQUE_ATTRIBUTE_NOT_FOUND, uniqAttributes.toString());
}
final AtlasVertex vertex = AtlasGraphUtilsV1.findByUniqueAttributes(entityType, uniqAttributes);
Collection<AtlasVertex> deletionCandidates = new ArrayList<>();
deletionCandidates.add(vertex);
return deleteVertices(deletionCandidates);
} }
@Override @Override
...@@ -216,8 +277,6 @@ public class AtlasEntityStoreV1 implements AtlasEntityStore { ...@@ -216,8 +277,6 @@ public class AtlasEntityStoreV1 implements AtlasEntityStore {
} }
context.addUpdated(guid, entity, entityType, vertex); context.addUpdated(guid, entity, entityType, vertex);
RequestContextV1.get().recordEntityUpdate(entity.getAtlasObjectId());
} }
} else { } else {
AtlasEntityType entityType = typeRegistry.getEntityTypeByName(entity.getTypeName()); AtlasEntityType entityType = typeRegistry.getEntityTypeByName(entity.getTypeName());
...@@ -233,10 +292,24 @@ public class AtlasEntityStoreV1 implements AtlasEntityStore { ...@@ -233,10 +292,24 @@ public class AtlasEntityStoreV1 implements AtlasEntityStore {
context.addCreated(guid, entity, entityType, vertex); context.addCreated(guid, entity, entityType, vertex);
RequestContextV1.get().recordEntityCreate(entity.getAtlasObjectId());
} }
} }
return context; return context;
} }
private EntityMutationResponse deleteVertices(Collection<AtlasVertex> deletionCandidates) throws AtlasBaseException {
EntityMutationResponse response = new EntityMutationResponse();
deleteHandler.deleteEntities(deletionCandidates);
RequestContextV1 req = RequestContextV1.get();
for (AtlasObjectId id : req.getDeletedEntityIds()) {
response.addEntity(EntityMutations.EntityOperation.DELETE, EntityGraphMapper.constructHeader(id));
}
for (AtlasObjectId id : req.getUpdatedEntityIds()) {
response.addEntity(EntityMutations.EntityOperation.UPDATE, EntityGraphMapper.constructHeader(id));
}
return response;
}
} }
...@@ -139,6 +139,10 @@ public class EntityGraphMapper { ...@@ -139,6 +139,10 @@ public class EntityGraphMapper {
resp.addEntity(DELETE, constructHeader(id)); resp.addEntity(DELETE, constructHeader(id));
} }
for (AtlasObjectId id : req.getUpdatedEntityIds()) {
resp.addEntity(UPDATE, constructHeader(id));
}
return resp; return resp;
} }
...@@ -744,7 +748,7 @@ public class EntityGraphMapper { ...@@ -744,7 +748,7 @@ public class EntityGraphMapper {
return header; return header;
} }
private AtlasEntityHeader constructHeader(AtlasObjectId id) { public static AtlasEntityHeader constructHeader(AtlasObjectId id) {
AtlasEntityHeader entity = new AtlasEntityHeader(id.getTypeName()); AtlasEntityHeader entity = new AtlasEntityHeader(id.getTypeName());
entity.setGuid(id.getGuid()); entity.setGuid(id.getGuid());
......
...@@ -28,7 +28,7 @@ public class HardDeleteHandlerV1 extends DeleteHandlerV1 { ...@@ -28,7 +28,7 @@ public class HardDeleteHandlerV1 extends DeleteHandlerV1 {
@Inject @Inject
public HardDeleteHandlerV1(AtlasTypeRegistry typeRegistry) { public HardDeleteHandlerV1(AtlasTypeRegistry typeRegistry) {
super(typeRegistry, false, true); super(typeRegistry, true, false);
} }
@Override @Override
......
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
package org.apache.atlas.repository.store.graph.v1; package org.apache.atlas.repository.store.graph.v1;
import com.google.inject.Inject; import com.google.inject.Inject;
import org.apache.atlas.RequestContextV1;
import org.apache.atlas.exception.AtlasBaseException; import org.apache.atlas.exception.AtlasBaseException;
import org.apache.atlas.model.instance.AtlasEntity; import org.apache.atlas.model.instance.AtlasEntity;
import org.apache.atlas.repository.graph.GraphHelper; import org.apache.atlas.repository.graph.GraphHelper;
...@@ -46,10 +47,10 @@ public class SoftDeleteHandlerV1 extends DeleteHandlerV1 { ...@@ -46,10 +47,10 @@ public class SoftDeleteHandlerV1 extends DeleteHandlerV1 {
} else { } else {
AtlasEntity.Status state = AtlasGraphUtilsV1.getState(instanceVertex); AtlasEntity.Status state = AtlasGraphUtilsV1.getState(instanceVertex);
if (state != AtlasEntity.Status.DELETED) { if (state != AtlasEntity.Status.DELETED) {
GraphHelper.setProperty(instanceVertex, STATE_PROPERTY_KEY, Id.EntityState.DELETED.name()); GraphHelper.setProperty(instanceVertex, STATE_PROPERTY_KEY, AtlasEntity.Status.DELETED.name());
GraphHelper.setProperty(instanceVertex, MODIFICATION_TIMESTAMP_PROPERTY_KEY, GraphHelper.setProperty(instanceVertex, MODIFICATION_TIMESTAMP_PROPERTY_KEY,
RequestContext.get().getRequestTime()); RequestContextV1.get().getRequestTime());
GraphHelper.setProperty(instanceVertex, MODIFIED_BY_KEY, RequestContext.get().getUser()); GraphHelper.setProperty(instanceVertex, MODIFIED_BY_KEY, RequestContextV1.get().getUser());
} }
} }
} }
...@@ -59,12 +60,12 @@ public class SoftDeleteHandlerV1 extends DeleteHandlerV1 { ...@@ -59,12 +60,12 @@ public class SoftDeleteHandlerV1 extends DeleteHandlerV1 {
if (force) { if (force) {
graphHelper.removeEdge(edge); graphHelper.removeEdge(edge);
} else { } else {
Id.EntityState state = GraphHelper.getState(edge); AtlasEntity.Status state = AtlasGraphUtilsV1.getState(edge);
if (state != Id.EntityState.DELETED) { if (state != AtlasEntity.Status.DELETED) {
GraphHelper.setProperty(edge, STATE_PROPERTY_KEY, Id.EntityState.DELETED.name()); GraphHelper.setProperty(edge, STATE_PROPERTY_KEY, AtlasEntity.Status.DELETED.name());
GraphHelper GraphHelper
.setProperty(edge, MODIFICATION_TIMESTAMP_PROPERTY_KEY, RequestContext.get().getRequestTime()); .setProperty(edge, MODIFICATION_TIMESTAMP_PROPERTY_KEY, RequestContextV1.get().getRequestTime());
GraphHelper.setProperty(edge, MODIFIED_BY_KEY, RequestContext.get().getUser()); GraphHelper.setProperty(edge, MODIFIED_BY_KEY, RequestContextV1.get().getUser());
} }
} }
} }
......
...@@ -119,13 +119,11 @@ public class AtlasEntityStoreV1Test { ...@@ -119,13 +119,11 @@ public class AtlasEntityStoreV1Test {
@AfterClass @AfterClass
public void clear() { public void clear() {
AtlasGraphProvider.cleanup(); AtlasGraphProvider.cleanup();
TestUtils.resetRequestContext();
} }
@BeforeTest @BeforeTest
public void init() throws Exception { public void init() throws Exception {
entityStore = new AtlasEntityStoreV1(deleteHandler, typeRegistry); entityStore = new AtlasEntityStoreV1(deleteHandler, typeRegistry);
RequestContextV1.clear(); RequestContextV1.clear();
} }
...@@ -138,7 +136,7 @@ public class AtlasEntityStoreV1Test { ...@@ -138,7 +136,7 @@ public class AtlasEntityStoreV1Test {
AtlasEntityHeader dept1 = response.getFirstCreatedEntityByTypeName(TestUtilsV2.DEPARTMENT_TYPE); AtlasEntityHeader dept1 = response.getFirstCreatedEntityByTypeName(TestUtilsV2.DEPARTMENT_TYPE);
validateEntity(deptEntity, getEntityFromStore(dept1), deptEntity.getEntities().get(0)); validateEntity(deptEntity, getEntityFromStore(dept1), deptEntity.getEntities().get(0));
final Map<EntityOperation, List<AtlasEntityHeader>> entitiesMutated = response.getEntitiesMutated(); final Map<EntityOperation, List<AtlasEntityHeader>> entitiesMutated = response.getMutatedEntities();
List<AtlasEntityHeader> entitiesCreated = entitiesMutated.get(EntityOperation.CREATE); List<AtlasEntityHeader> entitiesCreated = entitiesMutated.get(EntityOperation.CREATE);
Assert.assertTrue(entitiesCreated.size() >= deptEntity.getEntities().size()); Assert.assertTrue(entitiesCreated.size() >= deptEntity.getEntities().size());
......
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.atlas.repository.store.graph.v1;
import org.apache.atlas.AtlasClient;
import org.apache.atlas.AtlasException;
import org.apache.atlas.model.instance.AtlasEntity;
import org.apache.atlas.model.instance.AtlasEntityHeader;
import org.apache.atlas.model.instance.AtlasObjectId;
import org.apache.atlas.repository.Constants;
import org.apache.atlas.repository.graph.GraphHelper;
import org.apache.atlas.repository.graphdb.AtlasVertex;
import org.apache.atlas.services.MetadataService;
import org.apache.atlas.type.AtlasTypeRegistry;
import org.apache.atlas.typesystem.IStruct;
import org.apache.atlas.typesystem.ITypedReferenceableInstance;
import org.apache.atlas.typesystem.ITypedStruct;
import org.apache.atlas.typesystem.persistence.Id;
import org.testng.Assert;
import javax.inject.Inject;
import java.util.List;
import java.util.Map;
import static org.apache.atlas.TestUtils.COLUMNS_ATTR_NAME;
import static org.apache.atlas.TestUtils.NAME;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
public class SoftDeleteHandlerV1Test extends AtlasDeleteHandlerV1Test {
@Inject
MetadataService metadataService;
@Override
DeleteHandlerV1 getDeleteHandler(final AtlasTypeRegistry typeRegistry) {
return new SoftDeleteHandlerV1(typeRegistry);
}
@Override
protected void assertDeletedColumn(final ITypedReferenceableInstance tableInstance) throws AtlasException {
}
@Override
protected void assertTestDeleteEntities(final ITypedReferenceableInstance tableInstance) throws Exception {
}
@Override
protected void assertTableForTestDeleteReference(final String tableId) throws Exception {
//TODO - Fix after GET is ready
ITypedReferenceableInstance table = metadataService.getEntityDefinition(tableId);
assertNotNull(table.get(NAME));
assertNotNull(table.get("description"));
assertNotNull(table.get("type"));
assertNotNull(table.get("tableType"));
assertNotNull(table.get("created"));
Id dbId = (Id) table.get("database");
assertNotNull(dbId);
ITypedReferenceableInstance db = metadataService.getEntityDefinition(dbId.getId()._getId());
assertNotNull(db);
assertEquals(db.getId().getState(), Id.EntityState.ACTIVE);
}
@Override
protected void assertColumnForTestDeleteReference(final AtlasEntity tableInstance) throws AtlasException {
List<AtlasObjectId> columns = (List<AtlasObjectId>) tableInstance.getAttribute(COLUMNS_ATTR_NAME);
assertEquals(columns.size(), 1);
//TODO - Enable after GET is ready
ITypedReferenceableInstance colInst = metadataService.getEntityDefinition(columns.get(0).getGuid());
assertEquals(colInst.getId().getState(), Id.EntityState.DELETED);
}
@Override
protected void assertProcessForTestDeleteReference(final AtlasEntityHeader processInstance) throws Exception {
//
ITypedReferenceableInstance process = metadataService.getEntityDefinition(processInstance.getGuid());
List<ITypedReferenceableInstance> outputs =
(List<ITypedReferenceableInstance>) process.get(AtlasClient.PROCESS_ATTRIBUTE_OUTPUTS);
List<ITypedReferenceableInstance> expectedOutputs =
(List<ITypedReferenceableInstance>) process.get(AtlasClient.PROCESS_ATTRIBUTE_OUTPUTS);
assertEquals(outputs.size(), expectedOutputs.size());
}
@Override
protected void assertEntityDeleted(final String id) throws Exception {
// ITypedReferenceableInstance entity = metadataService.getEntityDefinition(id);
// assertEquals(entity.getId().getState(), Id.EntityState.DELETED);
final AtlasEntity.AtlasEntityWithExtInfo byId = entityStore.getById(id);
assertEquals(byId.getEntity().getStatus(), AtlasEntity.Status.DELETED);
}
@Override
protected void assertTestUpdateEntity_MultiplicityOneNonCompositeReference(final String janeGuid) throws Exception {
// Verify Jane's subordinates reference cardinality is still 2.
ITypedReferenceableInstance jane = metadataService.getEntityDefinition(janeGuid);
List<ITypedReferenceableInstance> subordinates = (List<ITypedReferenceableInstance>) jane.get("subordinates");
Assert.assertEquals(subordinates.size(), 2);
}
@Override
protected void assertJohnForTestDisconnectBidirectionalReferences(final ITypedReferenceableInstance john, final String janeGuid) throws Exception {
Id mgr = (Id) john.get("manager");
assertNotNull(mgr);
assertEquals(mgr._getId(), janeGuid);
assertEquals(mgr.getState(), Id.EntityState.DELETED);
}
@Override
protected void assertMaxForTestDisconnectBidirectionalReferences(final Map<String, String> nameGuidMap) throws Exception {
// Verify that the Department.employees reference to the deleted employee
// was disconnected.
ITypedReferenceableInstance hrDept = metadataService.getEntityDefinition(nameGuidMap.get("hr"));
List<ITypedReferenceableInstance> employees = (List<ITypedReferenceableInstance>) hrDept.get("employees");
Assert.assertEquals(employees.size(), 4);
String maxGuid = nameGuidMap.get("Max");
for (ITypedReferenceableInstance employee : employees) {
if (employee.getId()._getId().equals(maxGuid)) {
assertEquals(employee.getId().getState(), Id.EntityState.DELETED);
}
}
// Verify that the Manager.subordinates still references deleted employee
ITypedReferenceableInstance jane = metadataService.getEntityDefinition(nameGuidMap.get("Jane"));
List<ITypedReferenceableInstance> subordinates = (List<ITypedReferenceableInstance>) jane.get("subordinates");
assertEquals(subordinates.size(), 2);
for (ITypedReferenceableInstance subordinate : subordinates) {
if (subordinate.getId()._getId().equals(maxGuid)) {
assertEquals(subordinate.getId().getState(), Id.EntityState.DELETED);
}
}
// Verify that max's Person.mentor unidirectional reference to john was disconnected.
ITypedReferenceableInstance john = metadataService.getEntityDefinition(nameGuidMap.get("John"));
Id mentor = (Id) john.get("mentor");
assertEquals(mentor._getId(), maxGuid);
assertEquals(mentor.getState(), Id.EntityState.DELETED);
}
@Override
protected void assertTestDisconnectUnidirectionalArrayReferenceFromClassType(final List<ITypedReferenceableInstance> columns, final String columnGuid) {
Assert.assertEquals(columns.size(), 3);
for (ITypedReferenceableInstance column : columns) {
if (column.getId()._getId().equals(columnGuid)) {
assertEquals(column.getId().getState(), Id.EntityState.DELETED);
} else {
assertEquals(column.getId().getState(), Id.EntityState.ACTIVE);
}
}
}
@Override
protected void assertTestDisconnectUnidirectionalArrayReferenceFromStructAndTraitTypes(final String structContainerGuid) throws Exception {
// Verify that the unidirectional references from the struct and trait instances
// to the deleted entities were not disconnected.
ITypedReferenceableInstance structContainerConvertedEntity =
metadataService.getEntityDefinition(structContainerGuid);
ITypedStruct struct = (ITypedStruct) structContainerConvertedEntity.get("struct");
assertNotNull(struct.get("target"));
IStruct trait = structContainerConvertedEntity.getTrait("TestTrait");
assertNotNull(trait);
assertNotNull(trait.get("target"));
}
@Override
protected void assertVerticesDeleted(List<AtlasVertex> vertices) {
for (AtlasVertex vertex : vertices) {
assertEquals(GraphHelper.getSingleValuedProperty(vertex, Constants.STATE_PROPERTY_KEY, String.class), Id.EntityState.DELETED.name());
}
}
}
...@@ -115,8 +115,8 @@ public class RequestContextV1 { ...@@ -115,8 +115,8 @@ public class RequestContextV1 {
return requestTime; return requestTime;
} }
public boolean isDeletedEntity(String entityGuid) { public boolean isDeletedEntity(AtlasObjectId entityId) {
return deletedEntityIds.contains(entityGuid); return deletedEntityIds.contains(entityId);
} }
public static Metrics getMetrics() { public static Metrics getMetrics() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment