http://git-wip-us.apache.org/repos/asf/atlas/blob/0877e47c/repository/src/test/java/org/apache/atlas/repository/store/graph/v1/AtlasRelationshipStoreV1Test.java ---------------------------------------------------------------------- diff --git a/repository/src/test/java/org/apache/atlas/repository/store/graph/v1/AtlasRelationshipStoreV1Test.java b/repository/src/test/java/org/apache/atlas/repository/store/graph/v1/AtlasRelationshipStoreV1Test.java deleted file mode 100644 index 8c58a92..0000000 --- a/repository/src/test/java/org/apache/atlas/repository/store/graph/v1/AtlasRelationshipStoreV1Test.java +++ /dev/null @@ -1,623 +0,0 @@ -/** - * 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 - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * 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 com.google.common.collect.ImmutableList; -import org.apache.atlas.RequestContextV1; -import org.apache.atlas.TestModules; -import org.apache.atlas.TestUtilsV2; -import org.apache.atlas.exception.AtlasBaseException; -import org.apache.atlas.model.instance.AtlasEntity; -import org.apache.atlas.model.instance.AtlasEntity.AtlasEntitiesWithExtInfo; -import org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo; -import org.apache.atlas.model.instance.AtlasEntityHeader; -import org.apache.atlas.model.instance.AtlasObjectId; -import org.apache.atlas.model.instance.AtlasRelatedObjectId; -import org.apache.atlas.model.instance.EntityMutationResponse; -import org.apache.atlas.model.typedef.AtlasTypesDef; -import org.apache.atlas.repository.graph.AtlasGraphProvider; -import org.apache.atlas.repository.graph.GraphBackedSearchIndexer; -import org.apache.atlas.repository.store.bootstrap.AtlasTypeDefStoreInitializer; -import org.apache.atlas.repository.store.graph.AtlasEntityStore; -import org.apache.atlas.repository.store.graph.AtlasRelationshipStore; -import org.apache.atlas.store.AtlasTypeDefStore; -import org.apache.atlas.type.AtlasEntityType; -import org.apache.atlas.type.AtlasTypeRegistry; -import org.apache.commons.collections.CollectionUtils; -import org.testng.annotations.AfterClass; -import org.testng.annotations.BeforeClass; -import org.testng.annotations.BeforeTest; -import org.testng.annotations.Guice; -import org.testng.annotations.Test; - -import javax.inject.Inject; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import static org.apache.atlas.TestRelationshipUtilsV2.EMPLOYEE_TYPE; -import static org.apache.atlas.TestRelationshipUtilsV2.getDepartmentEmployeeInstances; -import static org.apache.atlas.TestRelationshipUtilsV2.getDepartmentEmployeeTypes; -import static org.apache.atlas.TestRelationshipUtilsV2.getInverseReferenceTestTypes; -import static org.apache.atlas.TestUtils.NAME; -import static org.apache.atlas.type.AtlasTypeUtil.getAtlasObjectId; -import static org.mockito.Mockito.mock; -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertNotNull; -import static org.testng.Assert.assertNull; -import static org.testng.Assert.assertTrue; - -@Guice(modules = TestModules.TestOnlyModule.class) -public abstract class AtlasRelationshipStoreV1Test { - - @Inject - AtlasTypeRegistry typeRegistry; - - @Inject - AtlasTypeDefStore typeDefStore; - - @Inject - DeleteHandlerV1 deleteHandler; - - @Inject - EntityGraphMapper graphMapper; - - AtlasEntityStore entityStore; - AtlasRelationshipStore relationshipStore; - AtlasEntityChangeNotifier mockChangeNotifier = mock(AtlasEntityChangeNotifier.class); - - protected Map<String, AtlasObjectId> employeeNameIdMap = new HashMap<>(); - - @BeforeClass - public void setUp() throws Exception { - new GraphBackedSearchIndexer(typeRegistry); - - // create employee relationship types - AtlasTypesDef employeeTypes = getDepartmentEmployeeTypes(); - typeDefStore.createTypesDef(employeeTypes); - - AtlasEntitiesWithExtInfo employeeInstances = getDepartmentEmployeeInstances(); - EntityMutationResponse response = entityStore.createOrUpdate(new AtlasEntityStream(employeeInstances), false); - - for (AtlasEntityHeader entityHeader : response.getCreatedEntities()) { - employeeNameIdMap.put((String) entityHeader.getAttribute(NAME), getAtlasObjectId(entityHeader)); - } - - init(); - AtlasTypesDef typesDef = getInverseReferenceTestTypes(); - - AtlasTypesDef typesToCreate = AtlasTypeDefStoreInitializer.getTypesToCreate(typesDef, typeRegistry); - - if (!typesToCreate.isEmpty()) { - typeDefStore.createTypesDef(typesToCreate); - } - } - - @BeforeTest - public void init() throws Exception { - entityStore = new AtlasEntityStoreV1(deleteHandler, typeRegistry, mockChangeNotifier, graphMapper); - relationshipStore = new AtlasRelationshipStoreV1(typeRegistry); - - RequestContextV1.clear(); - RequestContextV1.get().setUser(TestUtilsV2.TEST_USER); - } - - @AfterClass - public void clear() { - AtlasGraphProvider.cleanup(); - } - - @Test - public void testDepartmentEmployeeEntitiesUsingRelationship() throws Exception { - AtlasObjectId hrId = employeeNameIdMap.get("hr"); - AtlasObjectId maxId = employeeNameIdMap.get("Max"); - AtlasObjectId johnId = employeeNameIdMap.get("John"); - AtlasObjectId juliusId = employeeNameIdMap.get("Julius"); - AtlasObjectId janeId = employeeNameIdMap.get("Jane"); - AtlasObjectId mikeId = employeeNameIdMap.get("Mike"); - - AtlasEntity hrDept = getEntityFromStore(hrId.getGuid()); - AtlasEntity max = getEntityFromStore(maxId.getGuid()); - AtlasEntity john = getEntityFromStore(johnId.getGuid()); - AtlasEntity julius = getEntityFromStore(juliusId.getGuid()); - AtlasEntity jane = getEntityFromStore(janeId.getGuid()); - AtlasEntity mike = getEntityFromStore(mikeId.getGuid()); - - // Department relationship attributes - List<AtlasObjectId> deptEmployees = toAtlasObjectIds(hrDept.getRelationshipAttribute("employees")); - assertNotNull(deptEmployees); - assertEquals(deptEmployees.size(), 5); - assertObjectIdsContains(deptEmployees, maxId); - assertObjectIdsContains(deptEmployees, johnId); - assertObjectIdsContains(deptEmployees, juliusId); - assertObjectIdsContains(deptEmployees, janeId); - assertObjectIdsContains(deptEmployees, mikeId); - - // Max employee validation - AtlasObjectId maxDepartmentId = toAtlasObjectId(max.getRelationshipAttribute("department")); - assertNotNull(maxDepartmentId); - assertObjectIdEquals(maxDepartmentId, hrId); - - AtlasObjectId maxManagerId = toAtlasObjectId(max.getRelationshipAttribute("manager")); - assertNotNull(maxManagerId); - assertObjectIdEquals(maxManagerId, janeId); - - List<AtlasObjectId> maxMentorsId = toAtlasObjectIds(max.getRelationshipAttribute("mentors")); - assertNotNull(maxMentorsId); - assertEquals(maxMentorsId.size(), 1); - assertObjectIdEquals(maxMentorsId.get(0), juliusId); - - List<AtlasObjectId> maxMenteesId = toAtlasObjectIds(max.getRelationshipAttribute("mentees")); - assertNotNull(maxMenteesId); - assertEquals(maxMenteesId.size(), 1); - assertObjectIdEquals(maxMenteesId.get(0), johnId); - - List<AtlasObjectId> maxFriendsIds = toAtlasObjectIds(max.getRelationshipAttribute("friends")); - assertNotNull(maxFriendsIds); - assertEquals(maxFriendsIds.size(), 2); - assertObjectIdsContains(maxFriendsIds, mikeId); - assertObjectIdsContains(maxFriendsIds, johnId); - - // John Employee validation - AtlasObjectId johnDepartmentId = toAtlasObjectId(john.getRelationshipAttribute("department")); - assertNotNull(johnDepartmentId); - assertObjectIdEquals(johnDepartmentId, hrId); - - AtlasObjectId johnManagerId = toAtlasObjectId(john.getRelationshipAttribute("manager")); - assertNotNull(johnManagerId); - assertObjectIdEquals(johnManagerId, janeId); - - List<AtlasObjectId> johnMentorIds = toAtlasObjectIds(john.getRelationshipAttribute("mentors")); - assertNotNull(johnMentorIds); - assertEquals(johnMentorIds.size(), 2); - assertObjectIdsContains(johnMentorIds, maxId); - assertObjectIdsContains(johnMentorIds, juliusId); - - List<AtlasObjectId> johnMenteesId = toAtlasObjectIds(john.getRelationshipAttribute("mentees")); - assertEmpty(johnMenteesId); - - List<AtlasObjectId> johnFriendsIds = toAtlasObjectIds(john.getRelationshipAttribute("friends")); - assertNotNull(johnFriendsIds); - assertEquals(johnFriendsIds.size(), 2); - assertObjectIdsContains(johnFriendsIds, mikeId); - assertObjectIdsContains(johnFriendsIds, maxId); - - // Mike Employee validation - AtlasObjectId mikeDepartmentId = toAtlasObjectId(mike.getRelationshipAttribute("department")); - assertNotNull(mikeDepartmentId); - assertObjectIdEquals(mikeDepartmentId, hrId); - - AtlasObjectId mikeManagerId = toAtlasObjectId(mike.getRelationshipAttribute("manager")); - assertNotNull(mikeManagerId); - assertObjectIdEquals(mikeManagerId, juliusId); - - List<AtlasObjectId> mikeMentorIds = toAtlasObjectIds(mike.getRelationshipAttribute("mentors")); - assertEmpty(mikeMentorIds); - - List<AtlasObjectId> mikeMenteesId = toAtlasObjectIds(mike.getRelationshipAttribute("mentees")); - assertEmpty(mikeMenteesId); - - List<AtlasObjectId> mikeFriendsIds = toAtlasObjectIds(mike.getRelationshipAttribute("friends")); - assertNotNull(mikeFriendsIds); - assertEquals(mikeFriendsIds.size(), 2); - assertObjectIdsContains(mikeFriendsIds, maxId); - assertObjectIdsContains(mikeFriendsIds, johnId); - - // Jane Manager validation - AtlasObjectId janeDepartmentId = toAtlasObjectId(jane.getRelationshipAttribute("department")); - assertNotNull(janeDepartmentId); - assertObjectIdEquals(janeDepartmentId, hrId); - - AtlasObjectId janeManagerId = toAtlasObjectId(jane.getRelationshipAttribute("manager")); - assertNull(janeManagerId); - - List<AtlasObjectId> janeMentorIds = toAtlasObjectIds(jane.getRelationshipAttribute("mentors")); - assertEmpty(janeMentorIds); - - List<AtlasObjectId> janeMenteesId = toAtlasObjectIds(jane.getRelationshipAttribute("mentees")); - assertEmpty(janeMenteesId); - - List<AtlasObjectId> janeSubordinateIds = toAtlasObjectIds(jane.getRelationshipAttribute("subordinates")); - assertNotNull(janeSubordinateIds); - assertEquals(janeSubordinateIds.size(), 2); - assertObjectIdsContains(janeSubordinateIds, maxId); - assertObjectIdsContains(janeSubordinateIds, johnId); - - List<AtlasObjectId> janeFriendsIds = toAtlasObjectIds(jane.getRelationshipAttribute("friends")); - assertEmpty(janeFriendsIds); - - AtlasObjectId janeSiblingId = toAtlasObjectId(jane.getRelationshipAttribute("sibling")); - assertNotNull(janeSiblingId); - assertObjectIdEquals(janeSiblingId, juliusId); - - // Julius Manager validation - AtlasObjectId juliusDepartmentId = toAtlasObjectId(julius.getRelationshipAttribute("department")); - assertNotNull(juliusDepartmentId); - assertObjectIdEquals(juliusDepartmentId, hrId); - - AtlasObjectId juliusManagerId = toAtlasObjectId(julius.getRelationshipAttribute("manager")); - assertNull(juliusManagerId); - - List<AtlasObjectId> juliusMentorIds = toAtlasObjectIds(julius.getRelationshipAttribute("mentors")); - assertEmpty(juliusMentorIds); - - List<AtlasObjectId> juliusMenteesId = toAtlasObjectIds(julius.getRelationshipAttribute("mentees")); - assertNotNull(juliusMenteesId); - assertEquals(juliusMenteesId.size(), 2); - assertObjectIdsContains(juliusMenteesId, maxId); - assertObjectIdsContains(juliusMenteesId, johnId); - - List<AtlasObjectId> juliusSubordinateIds = toAtlasObjectIds(julius.getRelationshipAttribute("subordinates")); - assertNotNull(juliusSubordinateIds); - assertEquals(juliusSubordinateIds.size(), 1); - assertObjectIdsContains(juliusSubordinateIds, mikeId); - - List<AtlasObjectId> juliusFriendsIds = toAtlasObjectIds(julius.getRelationshipAttribute("friends")); - assertEmpty(juliusFriendsIds); - - AtlasObjectId juliusSiblingId = toAtlasObjectId(julius.getRelationshipAttribute("sibling")); - assertNotNull(juliusSiblingId); - assertObjectIdEquals(juliusSiblingId, janeId); - } - - // Seeing intermittent failures with janus profile, disabling it until its fixed. - @Test(enabled = false) - public void testRelationshipAttributeUpdate_NonComposite_OneToMany() throws Exception { - AtlasObjectId maxId = employeeNameIdMap.get("Max"); - AtlasObjectId juliusId = employeeNameIdMap.get("Julius"); - AtlasObjectId janeId = employeeNameIdMap.get("Jane"); - AtlasObjectId mikeId = employeeNameIdMap.get("Mike"); - AtlasObjectId johnId = employeeNameIdMap.get("John"); - - // Change Max's Employee.manager reference to Julius and apply the change as a partial update. - // This should also update Julius to add Max to the inverse Manager.subordinates reference. - AtlasEntity maxEntityForUpdate = new AtlasEntity(EMPLOYEE_TYPE); - maxEntityForUpdate.setRelationshipAttribute("manager", juliusId); - - AtlasEntityType employeeType = typeRegistry.getEntityTypeByName(EMPLOYEE_TYPE); - Map<String, Object> uniqAttributes = Collections.<String, Object>singletonMap("name", "Max"); - EntityMutationResponse updateResponse = entityStore.updateByUniqueAttributes(employeeType, uniqAttributes , new AtlasEntityWithExtInfo(maxEntityForUpdate)); - - List<AtlasEntityHeader> partialUpdatedEntities = updateResponse.getPartialUpdatedEntities(); - assertEquals(partialUpdatedEntities.size(), 3); - // 3 entities should have been updated: - // * Max to change the Employee.manager reference - // * Julius to add Max to Manager.subordinates - // * Jane to remove Max from Manager.subordinates - - AtlasEntitiesWithExtInfo updatedEntities = entityStore.getByIds(ImmutableList.of(maxId.getGuid(), juliusId.getGuid(), janeId.getGuid())); - - // Max's manager updated as Julius - AtlasEntity maxEntity = updatedEntities.getEntity(maxId.getGuid()); - verifyRelationshipAttributeValue(maxEntity, "manager", juliusId.getGuid()); - - // Max added to the subordinate list of Julius, existing subordinate is Mike - AtlasEntity juliusEntity = updatedEntities.getEntity(juliusId.getGuid()); - verifyRelationshipAttributeList(juliusEntity, "subordinates", ImmutableList.of(maxId, mikeId)); - - // Max removed from the subordinate list of Julius - AtlasEntity janeEntity = updatedEntities.getEntity(janeId.getGuid()); - - // Jane's subordinates list includes John and Max for soft delete - // Jane's subordinates list includes only John for hard delete - verifyRelationshipAttributeUpdate_NonComposite_OneToMany(janeEntity); - - // Remove Mike from Max's friends list - // Max's current friends: [Mike, John] - // Max's updated friends: [Julius, John] - maxEntityForUpdate = new AtlasEntity(EMPLOYEE_TYPE); - maxEntityForUpdate.setRelationshipAttribute("friends", ImmutableList.of(johnId, juliusId)); - - init(); - updateResponse = entityStore.updateByUniqueAttributes(employeeType, uniqAttributes , new AtlasEntityWithExtInfo(maxEntityForUpdate)); - - partialUpdatedEntities = updateResponse.getPartialUpdatedEntities(); - assertEquals(partialUpdatedEntities.size(), 3); - // 3 entities should have been updated: - // * Max added Julius and removed Mike from Employee.friends - // * Mike removed Max from Employee.friends - // * Julius added Max in Employee.friends - - updatedEntities = entityStore.getByIds(ImmutableList.of(maxId.getGuid(), mikeId.getGuid(), johnId.getGuid(), juliusId.getGuid())); - - maxEntity = updatedEntities.getEntity(maxId.getGuid()); - juliusEntity = updatedEntities.getEntity(juliusId.getGuid()); - AtlasEntity mikeEntity = updatedEntities.getEntity(mikeId.getGuid()); - AtlasEntity johnEntity = updatedEntities.getEntity(johnId.getGuid()); - - verifyRelationshipAttributeUpdate_ManyToMany_Friends(maxEntity, juliusEntity, mikeEntity, johnEntity); - - // Remove Julius from Jane's sibling and add Mike as new sibling - AtlasEntity juliusEntityForUpdate = new AtlasEntity(EMPLOYEE_TYPE); - juliusEntityForUpdate.setRelationshipAttribute("sibling", mikeId); - - init(); - updateResponse = entityStore.updateByUniqueAttributes(employeeType, Collections.<String, Object>singletonMap("name", "Julius") , new AtlasEntityWithExtInfo(juliusEntityForUpdate)); - partialUpdatedEntities = updateResponse.getPartialUpdatedEntities(); - assertEquals(partialUpdatedEntities.size(), 3); - - updatedEntities = entityStore.getByIds(ImmutableList.of(juliusId.getGuid(), janeId.getGuid(), mikeId.getGuid())); - - juliusEntity = updatedEntities.getEntity(juliusId.getGuid()); - janeEntity = updatedEntities.getEntity(janeId.getGuid()); - mikeEntity = updatedEntities.getEntity(mikeId.getGuid()); - - verifyRelationshipAttributeUpdate_OneToOne_Sibling(juliusEntity, janeEntity, mikeEntity); - } - - @Test - public void testRelationshipAttributeUpdate_NonComposite_ManyToOne() throws Exception { - AtlasEntity a1 = new AtlasEntity("A"); - a1.setAttribute(NAME, "a1_name"); - - AtlasEntity a2 = new AtlasEntity("A"); - a2.setAttribute(NAME, "a2_name"); - - AtlasEntity a3 = new AtlasEntity("A"); - a3.setAttribute(NAME, "a3_name"); - - AtlasEntity b = new AtlasEntity("B"); - b.setAttribute(NAME, "b_name"); - - AtlasEntitiesWithExtInfo entitiesWithExtInfo = new AtlasEntitiesWithExtInfo(); - entitiesWithExtInfo.addEntity(a1); - entitiesWithExtInfo.addEntity(a2); - entitiesWithExtInfo.addEntity(a3); - entitiesWithExtInfo.addEntity(b); - entityStore.createOrUpdate(new AtlasEntityStream(entitiesWithExtInfo) , false); - - AtlasEntity bPartialUpdate = new AtlasEntity("B"); - bPartialUpdate.setRelationshipAttribute("manyA", ImmutableList.of(getAtlasObjectId(a1), getAtlasObjectId(a2))); - - init(); - EntityMutationResponse response = entityStore.updateByUniqueAttributes(typeRegistry.getEntityTypeByName("B"), - Collections.singletonMap(NAME, b.getAttribute(NAME)), - new AtlasEntityWithExtInfo(bPartialUpdate)); - // Verify 3 entities were updated: - // * set b.manyA reference to a1 and a2 - // * set inverse a1.oneB reference to b - // * set inverse a2.oneB reference to b - assertEquals(response.getPartialUpdatedEntities().size(), 3); - AtlasEntitiesWithExtInfo updatedEntities = entityStore.getByIds(ImmutableList.of(a1.getGuid(), a2.getGuid(), b.getGuid())); - - AtlasEntity a1Entity = updatedEntities.getEntity(a1.getGuid()); - verifyRelationshipAttributeValue(a1Entity, "oneB", b.getGuid()); - - AtlasEntity a2Entity = updatedEntities.getEntity(a2.getGuid()); - verifyRelationshipAttributeValue(a2Entity, "oneB", b.getGuid()); - - AtlasEntity bEntity = updatedEntities.getEntity(b.getGuid()); - verifyRelationshipAttributeList(bEntity, "manyA", ImmutableList.of(getAtlasObjectId(a1), getAtlasObjectId(a2))); - - - bPartialUpdate.setRelationshipAttribute("manyA", ImmutableList.of(getAtlasObjectId(a3))); - init(); - response = entityStore.updateByUniqueAttributes(typeRegistry.getEntityTypeByName("B"), - Collections.singletonMap(NAME, b.getAttribute(NAME)), - new AtlasEntityWithExtInfo(bPartialUpdate)); - // Verify 4 entities were updated: - // * set b.manyA reference to a3 - // * set inverse a3.oneB reference to b - // * disconnect inverse a1.oneB reference to b - // * disconnect inverse a2.oneB reference to b - assertEquals(response.getPartialUpdatedEntities().size(), 4); - init(); - - updatedEntities = entityStore.getByIds(ImmutableList.of(a1.getGuid(), a2.getGuid(), a3.getGuid(), b.getGuid())); - a1Entity = updatedEntities.getEntity(a1.getGuid()); - a2Entity = updatedEntities.getEntity(a2.getGuid()); - bEntity = updatedEntities.getEntity(b.getGuid()); - - AtlasEntity a3Entity = updatedEntities.getEntity(a3.getGuid()); - verifyRelationshipAttributeValue(a3Entity, "oneB", b.getGuid()); - - verifyRelationshipAttributeUpdate_NonComposite_ManyToOne(a1Entity, a2Entity, a3Entity, bEntity); - } - - @Test - public void testRelationshipAttributeUpdate_NonComposite_OneToOne() throws Exception { - AtlasEntity a1 = new AtlasEntity("A"); - a1.setAttribute(NAME, "a1_name"); - - AtlasEntity a2 = new AtlasEntity("A"); - a2.setAttribute(NAME, "a2_name"); - - AtlasEntity b = new AtlasEntity("B"); - b.setAttribute(NAME, "b_name"); - - AtlasEntitiesWithExtInfo entitiesWithExtInfo = new AtlasEntitiesWithExtInfo(); - entitiesWithExtInfo.addEntity(a1); - entitiesWithExtInfo.addEntity(a2); - entitiesWithExtInfo.addEntity(b); - - EntityMutationResponse response = entityStore.createOrUpdate(new AtlasEntityStream(entitiesWithExtInfo) , false); - - AtlasEntity partialUpdateB = new AtlasEntity("B"); - partialUpdateB.setRelationshipAttribute("a", getAtlasObjectId(a1)); - - init(); - AtlasEntityType bType = typeRegistry.getEntityTypeByName("B"); - - response = entityStore.updateByUniqueAttributes(bType, Collections.singletonMap(NAME, b.getAttribute(NAME)), new AtlasEntityWithExtInfo(partialUpdateB)); - List<AtlasEntityHeader> partialUpdatedEntitiesHeader = response.getPartialUpdatedEntities(); - // Verify 2 entities were updated: - // * set b.a reference to a1 - // * set inverse a1.b reference to b - assertEquals(partialUpdatedEntitiesHeader.size(), 2); - AtlasEntitiesWithExtInfo partialUpdatedEntities = entityStore.getByIds(ImmutableList.of(a1.getGuid(), b.getGuid())); - - AtlasEntity a1Entity = partialUpdatedEntities.getEntity(a1.getGuid()); - verifyRelationshipAttributeValue(a1Entity, "b", b.getGuid()); - - AtlasEntity bEntity = partialUpdatedEntities.getEntity(b.getGuid()); - verifyRelationshipAttributeValue(bEntity, "a", a1.getGuid()); - - init(); - - // Update b.a to reference a2. - partialUpdateB.setRelationshipAttribute("a", getAtlasObjectId(a2)); - response = entityStore.updateByUniqueAttributes(bType, Collections.<String, Object>singletonMap(NAME, b.getAttribute(NAME)), new AtlasEntityWithExtInfo(partialUpdateB)); - partialUpdatedEntitiesHeader = response.getPartialUpdatedEntities(); - // Verify 3 entities were updated: - // * set b.a reference to a2 - // * set a2.b reference to b - // * disconnect a1.b reference - assertEquals(partialUpdatedEntitiesHeader.size(), 3); - partialUpdatedEntities = entityStore.getByIds(ImmutableList.of(a1.getGuid(), a2.getGuid(), b.getGuid())); - - bEntity = partialUpdatedEntities.getEntity(b.getGuid()); - verifyRelationshipAttributeValue(bEntity, "a", a2.getGuid()); - - AtlasEntity a2Entity = partialUpdatedEntities.getEntity(a2.getGuid()); - verifyRelationshipAttributeValue(a2Entity, "b", b.getGuid()); - - a1Entity = partialUpdatedEntities.getEntity(a1.getGuid()); - verifyRelationshipAttributeUpdate_NonComposite_OneToOne(a1Entity, bEntity); - } - - @Test - public void testRelationshipAttributeUpdate_NonComposite_ManyToMany() throws Exception { - AtlasEntity a1 = new AtlasEntity("A"); - a1.setAttribute(NAME, "a1_name"); - - AtlasEntity a2 = new AtlasEntity("A"); - a2.setAttribute(NAME, "a2_name"); - - AtlasEntity a3 = new AtlasEntity("A"); - a3.setAttribute(NAME, "a3_name"); - - AtlasEntity b1 = new AtlasEntity("B"); - b1.setAttribute(NAME, "b1_name"); - - AtlasEntity b2 = new AtlasEntity("B"); - b2.setAttribute(NAME, "b2_name"); - - AtlasEntitiesWithExtInfo entitiesWithExtInfo = new AtlasEntitiesWithExtInfo(); - entitiesWithExtInfo.addEntity(a1); - entitiesWithExtInfo.addEntity(a2); - entitiesWithExtInfo.addEntity(a3); - entitiesWithExtInfo.addEntity(b1); - entitiesWithExtInfo.addEntity(b2); - entityStore.createOrUpdate(new AtlasEntityStream(entitiesWithExtInfo) , false); - - AtlasEntity b1PartialUpdate = new AtlasEntity("B"); - b1PartialUpdate.setRelationshipAttribute("manyToManyA", ImmutableList.of(getAtlasObjectId(a1), getAtlasObjectId(a2))); - - init(); - EntityMutationResponse response = entityStore.updateByUniqueAttributes(typeRegistry.getEntityTypeByName("B"), - Collections.singletonMap(NAME, b1.getAttribute(NAME)), - new AtlasEntityWithExtInfo(b1PartialUpdate)); - - List<AtlasEntityHeader> updatedEntityHeaders = response.getPartialUpdatedEntities(); - assertEquals(updatedEntityHeaders.size(), 3); - - AtlasEntitiesWithExtInfo updatedEntities = entityStore.getByIds(ImmutableList.of(a1.getGuid(), a2.getGuid(), b1.getGuid())); - - AtlasEntity b1Entity = updatedEntities.getEntity(b1.getGuid()); - verifyRelationshipAttributeList(b1Entity, "manyToManyA", ImmutableList.of(getAtlasObjectId(a1), getAtlasObjectId(a2))); - - AtlasEntity a1Entity = updatedEntities.getEntity(a1.getGuid()); - verifyRelationshipAttributeList(a1Entity, "manyB", ImmutableList.of(getAtlasObjectId(b1))); - - AtlasEntity a2Entity = updatedEntities.getEntity(a2.getGuid()); - verifyRelationshipAttributeList(a2Entity, "manyB", ImmutableList.of(getAtlasObjectId(b1))); - } - - protected abstract void verifyRelationshipAttributeUpdate_NonComposite_OneToOne(AtlasEntity a1, AtlasEntity b); - - protected abstract void verifyRelationshipAttributeUpdate_NonComposite_OneToMany(AtlasEntity entity) throws Exception; - - protected abstract void verifyRelationshipAttributeUpdate_NonComposite_ManyToOne(AtlasEntity a1, AtlasEntity a2, AtlasEntity a3, AtlasEntity b); - - protected abstract void verifyRelationshipAttributeUpdate_ManyToMany_Friends(AtlasEntity e1, AtlasEntity e2, AtlasEntity e3, AtlasEntity e4) throws Exception; - - protected abstract void verifyRelationshipAttributeUpdate_OneToOne_Sibling(AtlasEntity e1, AtlasEntity e2, AtlasEntity e3) throws Exception; - - protected static void assertObjectIdsContains(List<AtlasObjectId> objectIds, AtlasObjectId objectId) { - assertTrue(CollectionUtils.isNotEmpty(objectIds)); - assertTrue(objectIds.contains(objectId)); - } - - protected static void assertObjectIdEquals(AtlasObjectId objId1, AtlasObjectId objId2) { - assertTrue(objId1.equals(objId2)); - } - - private static void assertEmpty(List collection) { - assertTrue(collection != null && collection.isEmpty()); - } - - protected static List<AtlasObjectId> toAtlasObjectIds(Object object) { - List<AtlasObjectId> ret = new ArrayList<>(); - - if (object instanceof List) { - List<?> objectIds = (List) object; - - if (CollectionUtils.isNotEmpty(objectIds)) { - for (Object obj : objectIds) { - if (obj instanceof AtlasRelatedObjectId) { - AtlasRelatedObjectId relatedObjectId = (AtlasRelatedObjectId) obj; - ret.add(new AtlasObjectId(relatedObjectId.getGuid(), relatedObjectId.getTypeName(), relatedObjectId.getUniqueAttributes())); - } - } - } - } - - return ret; - } - - protected static AtlasObjectId toAtlasObjectId(Object object) { - if (object instanceof AtlasRelatedObjectId) { - AtlasRelatedObjectId relatedObjectId = (AtlasRelatedObjectId) object; - return new AtlasObjectId(relatedObjectId.getGuid(), relatedObjectId.getTypeName(), relatedObjectId.getUniqueAttributes()); - } - - return null; - } - - private AtlasEntity getEntityFromStore(String guid) throws AtlasBaseException { - AtlasEntityWithExtInfo entity = guid != null ? entityStore.getById(guid) : null; - - return entity != null ? entity.getEntity() : null; - } - - protected static void verifyRelationshipAttributeList(AtlasEntity entity, String relationshipAttrName, List<AtlasObjectId> expectedValues) { - Object refValue = entity.getRelationshipAttribute(relationshipAttrName); - assertTrue(refValue instanceof List); - - List<AtlasObjectId> refList = toAtlasObjectIds(refValue); - assertEquals(refList.size(), expectedValues.size()); - - if (expectedValues.size() > 0) { - assertTrue(refList.containsAll(expectedValues)); - } - } - - protected static void verifyRelationshipAttributeValue(AtlasEntity entity, String relationshipAttrName, String expectedGuid) { - Object refValue = entity.getRelationshipAttribute(relationshipAttrName); - if (expectedGuid == null) { - assertNull(refValue); - } - else { - assertTrue(refValue instanceof AtlasObjectId); - AtlasObjectId referencedObjectId = (AtlasObjectId) refValue; - assertEquals(referencedObjectId.getGuid(), expectedGuid); - } - } -} \ No newline at end of file
http://git-wip-us.apache.org/repos/asf/atlas/blob/0877e47c/repository/src/test/java/org/apache/atlas/repository/store/graph/v1/HardDeleteHandlerV1Test.java ---------------------------------------------------------------------- diff --git a/repository/src/test/java/org/apache/atlas/repository/store/graph/v1/HardDeleteHandlerV1Test.java b/repository/src/test/java/org/apache/atlas/repository/store/graph/v1/HardDeleteHandlerV1Test.java deleted file mode 100644 index 407ebff..0000000 --- a/repository/src/test/java/org/apache/atlas/repository/store/graph/v1/HardDeleteHandlerV1Test.java +++ /dev/null @@ -1,177 +0,0 @@ -/** - * 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.TestModules; -import org.apache.atlas.TestUtils; -import org.apache.atlas.exception.AtlasBaseException; -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.typesystem.IStruct; -import org.apache.atlas.typesystem.ITypedReferenceableInstance; -import org.apache.atlas.typesystem.ITypedStruct; -import org.testng.Assert; -import org.testng.annotations.Guice; - -import java.util.List; -import java.util.Map; - -import static org.apache.atlas.TestUtils.COLUMNS_ATTR_NAME; -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertFalse; -import static org.testng.Assert.assertNull; -import static org.testng.Assert.fail; -import static org.testng.AssertJUnit.assertNotNull; - -@Guice(modules = TestModules.HardDeleteModule.class) -public class HardDeleteHandlerV1Test extends AtlasDeleteHandlerV1Test { - - - @Override - protected void assertTableForTestDeleteReference(String tableId) { - //entity is deleted. So, no assertions - } - - @Override - protected void assertColumnForTestDeleteReference(final AtlasEntity.AtlasEntityWithExtInfo tableInstance) throws AtlasBaseException { - List<AtlasObjectId> columns = (List<AtlasObjectId>) tableInstance.getEntity().getAttribute(COLUMNS_ATTR_NAME); - assertNull(columns); - } - - @Override - protected void assertProcessForTestDeleteReference(final AtlasEntityHeader processInstance) throws Exception { - //assert that outputs is empty - ITypedReferenceableInstance newProcess = - metadataService.getEntityDefinition(processInstance.getGuid()); - assertNull(newProcess.get(AtlasClient.PROCESS_ATTRIBUTE_OUTPUTS)); - } - - @Override - protected void assertEntityDeleted(String id) throws Exception { - try { - entityStore.getById(id); - fail("Expected EntityNotFoundException"); - } catch (AtlasBaseException e) { - // expected - } - } - - @Override - protected void assertDeletedColumn(final AtlasEntity.AtlasEntityWithExtInfo tableInstance) throws AtlasException { - final List<AtlasObjectId> columns = (List<AtlasObjectId>) tableInstance.getEntity().getAttribute(COLUMNS_ATTR_NAME); - Assert.assertEquals(columns.size(), 2); - } - - @Override - protected void assertTestDeleteEntities(AtlasEntity.AtlasEntityWithExtInfo tableInstance) { - int vertexCount = getVertices(Constants.ENTITY_TYPE_PROPERTY_KEY, TestUtils.TABLE_TYPE).size(); - assertEquals(vertexCount, 0); - - vertexCount = getVertices(Constants.ENTITY_TYPE_PROPERTY_KEY, TestUtils.COLUMN_TYPE).size(); - assertEquals(vertexCount, 0); - } - - @Override - protected void assertVerticesDeleted(List<AtlasVertex> vertices) { - assertEquals(vertices.size(), 0); - } - - @Override - protected void assertTestUpdateEntity_MultiplicityOneNonCompositeReference(String janeGuid) throws Exception { - // Verify that max is no longer a subordinate of jane. - ITypedReferenceableInstance jane = metadataService.getEntityDefinition(janeGuid); - List<ITypedReferenceableInstance> subordinates = (List<ITypedReferenceableInstance>) jane.get("subordinates"); - Assert.assertEquals(subordinates.size(), 1); - } - - @Override - protected void assertJohnForTestDisconnectBidirectionalReferences(final AtlasEntity.AtlasEntityWithExtInfo john, final String janeGuid) throws Exception { - assertNull(john.getEntity().getAttribute("manager")); - } - - @Override - protected void assertMaxForTestDisconnectBidirectionalReferences(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(), 3); - String maxGuid = nameGuidMap.get("Max"); - for (ITypedReferenceableInstance employee : employees) { - Assert.assertNotEquals(employee.getId()._getId(), maxGuid); - } - - // Verify that the Manager.subordinates reference to the deleted employee - // Max was disconnected. - ITypedReferenceableInstance jane = metadataService.getEntityDefinition(nameGuidMap.get("Jane")); - List<ITypedReferenceableInstance> subordinates = (List<ITypedReferenceableInstance>) jane.get("subordinates"); - assertEquals(subordinates.size(), 1); - - // Verify that max's Person.mentor unidirectional reference to john was disconnected. - ITypedReferenceableInstance john = metadataService.getEntityDefinition(nameGuidMap.get("John")); - assertNull(john.get("mentor")); - } - - @Override - protected void assertTestDisconnectUnidirectionalArrayReferenceFromClassType( - List<AtlasObjectId> columns, String columnGuid) { - assertEquals(columns.size(), 2); - for (AtlasObjectId column : columns) { - assertFalse(column.getGuid().equals(columnGuid)); - } - } - - protected void assertTestDisconnectMapReferenceFromClassType(final String mapOwnerGuid) throws Exception { - // Verify map references from mapOwner were disconnected. - AtlasEntity.AtlasEntityWithExtInfo mapOwnerInstance = entityStore.getById(mapOwnerGuid); - Map<String, AtlasObjectId> map = - (Map<String, AtlasObjectId>) mapOwnerInstance.getEntity().getAttribute("map"); - Assert.assertNull(map); - Map<String, AtlasObjectId> biMap = - (Map<String, AtlasObjectId>) mapOwnerInstance.getEntity().getAttribute("biMap"); - Assert.assertNull(biMap); - - AtlasVertex mapOwnerVertex = GraphHelper.getInstance().getVertexForGUID(mapOwnerGuid); - Object object = mapOwnerVertex.getProperty("MapOwner.map.value1", String.class); - assertNull(object); - object = mapOwnerVertex.getProperty("MapOwner.biMap.value1", String.class); - assertNull(object); - } - - @Override - protected void assertTestDisconnectUnidirectionalArrayReferenceFromStructAndTraitTypes(String structContainerGuid) - throws Exception { - // Verify that the unidirectional references from the struct and trait instances - // to the deleted entities were disconnected. - ITypedReferenceableInstance structContainerConvertedEntity = - metadataService.getEntityDefinition(structContainerGuid); - ITypedStruct struct = (ITypedStruct) structContainerConvertedEntity.get("struct"); - assertNull(struct.get("target")); - IStruct trait = structContainerConvertedEntity.getTrait("TestTrait"); - assertNotNull(trait); - assertNull(trait.get("target")); - } -} http://git-wip-us.apache.org/repos/asf/atlas/blob/0877e47c/repository/src/test/java/org/apache/atlas/repository/store/graph/v1/InverseReferenceUpdateHardDeleteV1Test.java ---------------------------------------------------------------------- diff --git a/repository/src/test/java/org/apache/atlas/repository/store/graph/v1/InverseReferenceUpdateHardDeleteV1Test.java b/repository/src/test/java/org/apache/atlas/repository/store/graph/v1/InverseReferenceUpdateHardDeleteV1Test.java deleted file mode 100644 index d54adeb..0000000 --- a/repository/src/test/java/org/apache/atlas/repository/store/graph/v1/InverseReferenceUpdateHardDeleteV1Test.java +++ /dev/null @@ -1,75 +0,0 @@ -/** - * 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 - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * 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 com.google.common.collect.ImmutableList; -import org.apache.atlas.TestModules; -import org.apache.atlas.model.instance.AtlasEntity; -import org.apache.atlas.model.instance.AtlasObjectId; -import org.apache.atlas.type.AtlasTypeUtil; -import org.testng.annotations.Guice; - -import java.util.Map; - -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertTrue; - -/** - * Inverse reference update test with {@link HardDeleteHandlerV1} - */ -@Guice(modules = TestModules.HardDeleteModule.class) -public class InverseReferenceUpdateHardDeleteV1Test extends InverseReferenceUpdateV1Test { - - @Override - protected void verify_testInverseReferenceAutoUpdate_NonComposite_OneToMany(AtlasEntity jane) throws Exception { - - // Max should have been removed from the subordinates list, leaving only John. - verifyReferenceList(jane, "subordinates", ImmutableList.of(nameIdMap.get("John"))); - } - - @Override - protected void verify_testInverseReferenceAutoUpdate_NonCompositeManyToOne(AtlasEntity a1, AtlasEntity a2, AtlasEntity a3, AtlasEntity b) { - - verifyReferenceValue(a1, "oneB", null); - - verifyReferenceValue(a2, "oneB", null); - - verifyReferenceList(b, "manyA", ImmutableList.of(AtlasTypeUtil.getAtlasObjectId(a3))); - } - - @Override - protected void verify_testInverseReferenceAutoUpdate_NonComposite_OneToOne(AtlasEntity a1, AtlasEntity b) { - - verifyReferenceValue(a1, "b", null); - } - - @Override - protected void verify_testInverseReferenceAutoUpdate_Map(AtlasEntity a1, AtlasEntity b1, - AtlasEntity b2, AtlasEntity b3) { - - Object value = a1.getAttribute("mapToB"); - assertTrue(value instanceof Map); - Map<String, AtlasObjectId> refMap = (Map<String, AtlasObjectId>) value; - assertEquals(refMap.size(), 1); - AtlasObjectId referencedEntityId = refMap.get("b3"); - assertEquals(referencedEntityId, AtlasTypeUtil.getAtlasObjectId(b3)); - verifyReferenceValue(b1, "mappedFromA", null); - verifyReferenceValue(b2, "mappedFromA", null); - } - -} http://git-wip-us.apache.org/repos/asf/atlas/blob/0877e47c/repository/src/test/java/org/apache/atlas/repository/store/graph/v1/InverseReferenceUpdateSoftDeleteV1Test.java ---------------------------------------------------------------------- diff --git a/repository/src/test/java/org/apache/atlas/repository/store/graph/v1/InverseReferenceUpdateSoftDeleteV1Test.java b/repository/src/test/java/org/apache/atlas/repository/store/graph/v1/InverseReferenceUpdateSoftDeleteV1Test.java deleted file mode 100644 index 884ab54..0000000 --- a/repository/src/test/java/org/apache/atlas/repository/store/graph/v1/InverseReferenceUpdateSoftDeleteV1Test.java +++ /dev/null @@ -1,78 +0,0 @@ -/** - * 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 - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * 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 com.google.common.collect.ImmutableList; -import org.apache.atlas.TestModules; -import org.apache.atlas.model.instance.AtlasEntity; -import org.apache.atlas.model.instance.AtlasObjectId; -import org.apache.atlas.type.AtlasTypeUtil; -import org.testng.annotations.Guice; - -import java.util.Map; - -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertTrue; - - -/** - * Inverse reference update test with {@link SoftDeleteHandlerV1} - */ -@Guice(modules = TestModules.SoftDeleteModule.class) -public class InverseReferenceUpdateSoftDeleteV1Test extends InverseReferenceUpdateV1Test { - - @Override - protected void verify_testInverseReferenceAutoUpdate_NonComposite_OneToMany(AtlasEntity jane) - throws Exception { - - // Max is still in the subordinates list, as the edge still exists with state DELETED - verifyReferenceList(jane, "subordinates", ImmutableList.of(nameIdMap.get("John"), nameIdMap.get("Max"))); - } - - @Override - protected void verify_testInverseReferenceAutoUpdate_NonCompositeManyToOne(AtlasEntity a1, - AtlasEntity a2, AtlasEntity a3, AtlasEntity b) { - - verifyReferenceValue(a1, "oneB", b.getGuid()); - - verifyReferenceValue(a2, "oneB", b.getGuid()); - - verifyReferenceList(b, "manyA", ImmutableList.of(AtlasTypeUtil.getAtlasObjectId(a1), AtlasTypeUtil.getAtlasObjectId(a2), AtlasTypeUtil.getAtlasObjectId(a3))); - } - - @Override - protected void verify_testInverseReferenceAutoUpdate_NonComposite_OneToOne(AtlasEntity a1, AtlasEntity b) { - - verifyReferenceValue(a1, "b", b.getGuid()); - } - - @Override - protected void verify_testInverseReferenceAutoUpdate_Map(AtlasEntity a1, AtlasEntity b1, - AtlasEntity b2, AtlasEntity b3) { - - Object value = a1.getAttribute("mapToB"); - assertTrue(value instanceof Map); - Map<String, AtlasObjectId> refMap = (Map<String, AtlasObjectId>) value; - assertEquals(refMap.size(), 3); - AtlasObjectId referencedEntityId = refMap.get("b3"); - assertEquals(referencedEntityId, AtlasTypeUtil.getAtlasObjectId(b3)); - verifyReferenceValue(b1, "mappedFromA", a1.getGuid()); - verifyReferenceValue(b2, "mappedFromA", a1.getGuid()); - } - -} http://git-wip-us.apache.org/repos/asf/atlas/blob/0877e47c/repository/src/test/java/org/apache/atlas/repository/store/graph/v1/InverseReferenceUpdateV1Test.java ---------------------------------------------------------------------- diff --git a/repository/src/test/java/org/apache/atlas/repository/store/graph/v1/InverseReferenceUpdateV1Test.java b/repository/src/test/java/org/apache/atlas/repository/store/graph/v1/InverseReferenceUpdateV1Test.java deleted file mode 100644 index 6475639..0000000 --- a/repository/src/test/java/org/apache/atlas/repository/store/graph/v1/InverseReferenceUpdateV1Test.java +++ /dev/null @@ -1,380 +0,0 @@ -/** - * 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 - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * 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 com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; -import org.apache.atlas.RequestContextV1; -import org.apache.atlas.TestUtils; -import org.apache.atlas.TestUtilsV2; -import org.apache.atlas.model.instance.AtlasEntity; -import org.apache.atlas.model.instance.AtlasEntity.AtlasEntitiesWithExtInfo; -import org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo; -import org.apache.atlas.model.instance.AtlasEntityHeader; -import org.apache.atlas.model.instance.AtlasObjectId; -import org.apache.atlas.model.instance.EntityMutationResponse; -import org.apache.atlas.model.typedef.AtlasTypesDef; -import org.apache.atlas.repository.graph.AtlasGraphProvider; -import org.apache.atlas.repository.store.bootstrap.AtlasTypeDefStoreInitializer; -import org.apache.atlas.repository.store.graph.AtlasEntityStore; -import org.apache.atlas.services.MetadataService; -import org.apache.atlas.store.AtlasTypeDefStore; -import org.apache.atlas.type.AtlasEntityType; -import org.apache.atlas.type.AtlasTypeRegistry; -import org.apache.atlas.type.AtlasTypeUtil; -import org.testng.annotations.AfterClass; -import org.testng.annotations.BeforeClass; -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.Test; - -import javax.inject.Inject; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import static org.apache.atlas.TestUtils.NAME; -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertNull; -import static org.testng.Assert.assertTrue; - -/** - * Test automatic inverse reference updating in V1 (V2?) code path. - * - */ -public abstract class InverseReferenceUpdateV1Test { - @Inject - AtlasTypeRegistry typeRegistry; - - @Inject - AtlasTypeDefStore typeDefStore; - - @Inject - AtlasEntityStore entityStore; - - @Inject - MetadataService metadataService; - - private AtlasEntitiesWithExtInfo deptEntity; - - protected Map<String, AtlasObjectId> nameIdMap = new HashMap<>(); - - @BeforeClass - public void setUp() throws Exception { - RequestContextV1.clear(); - RequestContextV1.get().setUser(TestUtilsV2.TEST_USER); - - metadataService = TestUtils.addSessionCleanupWrapper(metadataService); - - AtlasTypesDef[] testTypesDefs = new AtlasTypesDef[] { TestUtilsV2.defineDeptEmployeeTypes(), - TestUtilsV2.defineInverseReferenceTestTypes() - }; - - for (AtlasTypesDef typesDef : testTypesDefs) { - AtlasTypesDef typesToCreate = AtlasTypeDefStoreInitializer.getTypesToCreate(typesDef, typeRegistry); - - if (!typesToCreate.isEmpty()) { - typeDefStore.createTypesDef(typesToCreate); - } - } - - deptEntity = TestUtilsV2.createDeptEg2(); - init(); - EntityMutationResponse response = entityStore.createOrUpdate(new AtlasEntityStream(deptEntity), false); - for (AtlasEntityHeader entityHeader : response.getCreatedEntities()) { - nameIdMap.put((String)entityHeader.getAttribute(NAME), AtlasTypeUtil.getAtlasObjectId(entityHeader)); - } - } - - @AfterClass - public void clear() { - AtlasGraphProvider.cleanup(); - } - - @BeforeMethod - public void init() throws Exception { - RequestContextV1.clear(); - RequestContextV1.get().setUser(TestUtilsV2.TEST_USER); - } - - @Test - public void testInverseReferenceAutoUpdate_NonComposite_OneToMany() throws Exception { - AtlasObjectId juliusId = nameIdMap.get("Julius"); - - // Change Max's Employee.manager reference to Julius and apply the change as a partial update. - // This should also update Julius to add Max to the inverse Manager.subordinates reference. - AtlasEntity maxEntityForUpdate = new AtlasEntity(TestUtilsV2.EMPLOYEE_TYPE); - maxEntityForUpdate.setAttribute("manager", juliusId); - AtlasEntityType employeeType = typeRegistry.getEntityTypeByName(TestUtilsV2.EMPLOYEE_TYPE); - Map<String, Object> uniqAttributes = Collections.<String, Object>singletonMap("name", "Max"); - EntityMutationResponse updateResponse = entityStore.updateByUniqueAttributes(employeeType, uniqAttributes , new AtlasEntityWithExtInfo(maxEntityForUpdate)); - List<AtlasEntityHeader> partialUpdatedEntities = updateResponse.getPartialUpdatedEntities(); - // 3 entities should have been updated: - // * Max to change the Employee.manager reference - // * Julius to add Max to Manager.subordinates - // * Jane to remove Max from Manager.subordinates - assertEquals(partialUpdatedEntities.size(), 3); - - AtlasObjectId maxId = nameIdMap.get("Max"); - String janeGuid = nameIdMap.get("Jane").getGuid(); - AtlasEntitiesWithExtInfo storedEntities = entityStore.getByIds(ImmutableList.of(maxId.getGuid(), juliusId.getGuid(), janeGuid)); - AtlasEntity storedEntity = storedEntities.getEntity(maxId.getGuid()); - verifyReferenceValue(storedEntity, "manager", juliusId.getGuid()); - storedEntity = storedEntities.getEntity(juliusId.getGuid()); - verifyReferenceList(storedEntity, "subordinates", ImmutableList.of(maxId)); - storedEntity = storedEntities.getEntity(janeGuid); - verify_testInverseReferenceAutoUpdate_NonComposite_OneToMany(storedEntity); - } - - protected abstract void verify_testInverseReferenceAutoUpdate_NonComposite_OneToMany(AtlasEntity jane) throws Exception; - - @Test - public void testInverseReferenceAutoUpdate_NonCompositeManyToOne() throws Exception { - AtlasEntityType bType = typeRegistry.getEntityTypeByName("B"); - AtlasEntity a1 = new AtlasEntity("A"); - a1.setAttribute(NAME, TestUtils.randomString()); - AtlasEntity a2 = new AtlasEntity("A"); - a2.setAttribute(NAME, TestUtils.randomString()); - AtlasEntity a3 = new AtlasEntity("A"); - a3.setAttribute(NAME, TestUtils.randomString()); - AtlasEntity b = new AtlasEntity("B"); - - b.setAttribute(NAME, TestUtils.randomString()); - AtlasEntitiesWithExtInfo atlasEntitiesWithExtInfo = new AtlasEntitiesWithExtInfo(); - atlasEntitiesWithExtInfo.addEntity(a1); - atlasEntitiesWithExtInfo.addEntity(a2); - atlasEntitiesWithExtInfo.addEntity(a3); - atlasEntitiesWithExtInfo.addEntity(b); - AtlasEntityStream entityStream = new AtlasEntityStream(atlasEntitiesWithExtInfo); - EntityMutationResponse response = entityStore.createOrUpdate(entityStream , false); - - AtlasEntity bForPartialUpdate = new AtlasEntity("B"); - bForPartialUpdate.setAttribute("manyA", ImmutableList.of(AtlasTypeUtil.getAtlasObjectId(a1), AtlasTypeUtil.getAtlasObjectId(a2))); - init(); - response = entityStore.updateByUniqueAttributes(bType, Collections.<String, Object>singletonMap(NAME, b.getAttribute(NAME)), new AtlasEntityWithExtInfo(bForPartialUpdate)); - List<AtlasEntityHeader> partialUpdatedEntities = response.getPartialUpdatedEntities(); - // Verify 3 entities were updated: - // * set b.manyA reference to a1 and a2 - // * set inverse a1.oneB reference to b - // * set inverse a2.oneB reference to b - assertEquals(partialUpdatedEntities.size(), 3); - AtlasEntitiesWithExtInfo storedEntities = entityStore.getByIds(ImmutableList.of(a1.getGuid(), a2.getGuid(), b.getGuid())); - AtlasEntity storedEntity = storedEntities.getEntity(a1.getGuid()); - verifyReferenceValue(storedEntity, "oneB", b.getGuid()); - - storedEntity = storedEntities.getEntity(a2.getGuid()); - verifyReferenceValue(storedEntity, "oneB", b.getGuid()); - - storedEntity = storedEntities.getEntity(b.getGuid()); - verifyReferenceList(storedEntity, "manyA", ImmutableList.of(AtlasTypeUtil.getAtlasObjectId(a1), AtlasTypeUtil.getAtlasObjectId(a2))); - - bForPartialUpdate.setAttribute("manyA", ImmutableList.of(AtlasTypeUtil.getAtlasObjectId(a3))); - init(); - response = entityStore.updateByUniqueAttributes(bType, Collections.<String, Object>singletonMap(NAME, b.getAttribute(NAME)), new AtlasEntityWithExtInfo(bForPartialUpdate)); - partialUpdatedEntities = response.getPartialUpdatedEntities(); - // Verify 4 entities were updated: - // * set b.manyA reference to a3 - // * set inverse a3.oneB reference to b - // * disconnect inverse a1.oneB reference to b - // * disconnect inverse a2.oneB reference to b - assertEquals(partialUpdatedEntities.size(), 4); - - init(); - storedEntities = entityStore.getByIds(ImmutableList.of(a1.getGuid(), a2.getGuid(), a3.getGuid(), b.getGuid())); - verifyReferenceValue(storedEntities.getEntity(a3.getGuid()), "oneB", b.getGuid()); - - verify_testInverseReferenceAutoUpdate_NonCompositeManyToOne(storedEntities.getEntity(a1.getGuid()), storedEntities.getEntity(a2.getGuid()), - storedEntities.getEntity(a3.getGuid()), storedEntities.getEntity(b.getGuid())); - } - - protected abstract void verify_testInverseReferenceAutoUpdate_NonCompositeManyToOne(AtlasEntity a1, AtlasEntity a2, AtlasEntity a3, AtlasEntity b); - - @Test - public void testInverseReferenceAutoUpdate_NonComposite_OneToOne() throws Exception { - AtlasEntityType bType = typeRegistry.getEntityTypeByName("B"); - AtlasEntity a1 = new AtlasEntity("A"); - a1.setAttribute(NAME, TestUtils.randomString()); - AtlasEntity a2 = new AtlasEntity("A"); - a2.setAttribute(NAME, TestUtils.randomString()); - AtlasEntity b = new AtlasEntity("B"); - b.setAttribute(NAME, TestUtils.randomString()); - AtlasEntitiesWithExtInfo atlasEntitiesWithExtInfo = new AtlasEntitiesWithExtInfo(); - atlasEntitiesWithExtInfo.addEntity(a1); - atlasEntitiesWithExtInfo.addEntity(a2); - atlasEntitiesWithExtInfo.addEntity(b); - AtlasEntityStream entityStream = new AtlasEntityStream(atlasEntitiesWithExtInfo); - EntityMutationResponse response = entityStore.createOrUpdate(entityStream , false); - - AtlasEntity bForPartialUpdate = new AtlasEntity("B"); - bForPartialUpdate.setAttribute("a", AtlasTypeUtil.getAtlasObjectId(a1)); - init(); - response = entityStore.updateByUniqueAttributes(bType, Collections.<String, Object>singletonMap(NAME, b.getAttribute(NAME)), new AtlasEntityWithExtInfo(bForPartialUpdate)); - List<AtlasEntityHeader> partialUpdatedEntities = response.getPartialUpdatedEntities(); - // Verify 2 entities were updated: - // * set b.a reference to a1 - // * set inverse a1.b reference to b - assertEquals(partialUpdatedEntities.size(), 2); - AtlasEntitiesWithExtInfo storedEntities = entityStore.getByIds(ImmutableList.of(a1.getGuid(), b.getGuid())); - AtlasEntity storedEntity = storedEntities.getEntity(a1.getGuid()); - verifyReferenceValue(storedEntity, "b", b.getGuid()); - storedEntity = storedEntities.getEntity(b.getGuid()); - verifyReferenceValue(storedEntity, "a", a1.getGuid()); - - // Update b.a to reference a2. - bForPartialUpdate.setAttribute("a", AtlasTypeUtil.getAtlasObjectId(a2)); - init(); - response = entityStore.updateByUniqueAttributes(bType, Collections.<String, Object>singletonMap(NAME, b.getAttribute(NAME)), new AtlasEntityWithExtInfo(bForPartialUpdate)); - partialUpdatedEntities = response.getPartialUpdatedEntities(); - // Verify 3 entities were updated: - // * set b.a reference to a2 - // * set a2.b reference to b - // * disconnect a1.b reference - assertEquals(partialUpdatedEntities.size(), 3); - storedEntities = entityStore.getByIds(ImmutableList.of(a1.getGuid(), a2.getGuid(), b.getGuid())); - storedEntity = storedEntities.getEntity(a2.getGuid()); - verifyReferenceValue(storedEntity, "b", b.getGuid()); - storedEntity = storedEntities.getEntity(b.getGuid()); - verifyReferenceValue(storedEntity, "a", a2.getGuid()); - storedEntity = storedEntities.getEntity(a1.getGuid()); - Object refValue = storedEntity.getAttribute("b"); - verify_testInverseReferenceAutoUpdate_NonComposite_OneToOne(storedEntities.getEntity(a1.getGuid()), storedEntities.getEntity(b.getGuid())); - } - - protected abstract void verify_testInverseReferenceAutoUpdate_NonComposite_OneToOne(AtlasEntity a1, AtlasEntity b); - - @Test - public void testInverseReferenceAutoUpdate_NonComposite_ManyToMany() throws Exception { - AtlasEntityType bType = typeRegistry.getEntityTypeByName("B"); - AtlasEntity a1 = new AtlasEntity("A"); - a1.setAttribute(NAME, TestUtils.randomString()); - AtlasEntity a2 = new AtlasEntity("A"); - a2.setAttribute(NAME, TestUtils.randomString()); - AtlasEntity a3 = new AtlasEntity("A"); - a3.setAttribute(NAME, TestUtils.randomString()); - AtlasEntity b1 = new AtlasEntity("B"); - b1.setAttribute(NAME, TestUtils.randomString()); - AtlasEntity b2 = new AtlasEntity("B"); - b2.setAttribute(NAME, TestUtils.randomString()); - AtlasEntitiesWithExtInfo atlasEntitiesWithExtInfo = new AtlasEntitiesWithExtInfo(); - atlasEntitiesWithExtInfo.addEntity(a1); - atlasEntitiesWithExtInfo.addEntity(a2); - atlasEntitiesWithExtInfo.addEntity(a3); - atlasEntitiesWithExtInfo.addEntity(b1); - atlasEntitiesWithExtInfo.addEntity(b2); - AtlasEntityStream entityStream = new AtlasEntityStream(atlasEntitiesWithExtInfo); - EntityMutationResponse response = entityStore.createOrUpdate(entityStream , false); - - AtlasEntity b1ForPartialUpdate = new AtlasEntity("B"); - b1ForPartialUpdate.setAttribute("manyToManyA", ImmutableList.of(AtlasTypeUtil.getAtlasObjectId(a1), AtlasTypeUtil.getAtlasObjectId(a2))); - init(); - response = entityStore.updateByUniqueAttributes(bType, Collections.<String, Object>singletonMap(NAME, b1.getAttribute(NAME)), new AtlasEntityWithExtInfo(b1ForPartialUpdate)); - List<AtlasEntityHeader> partialUpdatedEntities = response.getPartialUpdatedEntities(); - assertEquals(partialUpdatedEntities.size(), 3); - AtlasEntitiesWithExtInfo storedEntities = entityStore.getByIds(ImmutableList.of(a1.getGuid(), a2.getGuid(), b1.getGuid())); - AtlasEntity storedEntity = storedEntities.getEntity(b1.getGuid()); - verifyReferenceList(storedEntity, "manyToManyA", ImmutableList.of(AtlasTypeUtil.getAtlasObjectId(a1), AtlasTypeUtil.getAtlasObjectId(a2))); - storedEntity = storedEntities.getEntity(a1.getGuid()); - verifyReferenceList(storedEntity, "manyB", ImmutableList.of(AtlasTypeUtil.getAtlasObjectId(b1))); - storedEntity = storedEntities.getEntity(a2.getGuid()); - verifyReferenceList(storedEntity, "manyB", ImmutableList.of(AtlasTypeUtil.getAtlasObjectId(b1))); - } - - @Test - public void testInverseReferenceAutoUpdate_Map() throws Exception { - AtlasEntity a1 = new AtlasEntity("A"); - a1.setAttribute(NAME, TestUtils.randomString()); - AtlasEntity b1 = new AtlasEntity("B"); - b1.setAttribute(NAME, TestUtils.randomString()); - AtlasEntity b2 = new AtlasEntity("B"); - b2.setAttribute(NAME, TestUtils.randomString()); - AtlasEntity b3 = new AtlasEntity("B"); - b3.setAttribute(NAME, TestUtils.randomString()); - AtlasEntitiesWithExtInfo atlasEntitiesWithExtInfo = new AtlasEntitiesWithExtInfo(); - atlasEntitiesWithExtInfo.addEntity(a1); - atlasEntitiesWithExtInfo.addEntity(b1); - atlasEntitiesWithExtInfo.addEntity(b2); - atlasEntitiesWithExtInfo.addEntity(b3); - AtlasEntityStream entityStream = new AtlasEntityStream(atlasEntitiesWithExtInfo); - EntityMutationResponse response = entityStore.createOrUpdate(entityStream , false); - - AtlasEntityType aType = typeRegistry.getEntityTypeByName("A"); - AtlasEntity aForPartialUpdate = new AtlasEntity("A"); - aForPartialUpdate.setAttribute("mapToB", ImmutableMap.<String, AtlasObjectId>of("b1", AtlasTypeUtil.getAtlasObjectId(b1), "b2", AtlasTypeUtil.getAtlasObjectId(b2))); - init(); - response = entityStore.updateByUniqueAttributes(aType, Collections.<String, Object>singletonMap(NAME, a1.getAttribute(NAME)), new AtlasEntityWithExtInfo(aForPartialUpdate)); - List<AtlasEntityHeader> partialUpdatedEntities = response.getPartialUpdatedEntities(); - // Verify 3 entities were updated: - // * set a1.mapToB to "b1"->b1, "b2"->b2 - // * set b1.mappedFromA to a1 - // * set b2.mappedFromA to a1 - assertEquals(partialUpdatedEntities.size(), 3); - AtlasEntitiesWithExtInfo storedEntities = entityStore.getByIds(ImmutableList.of(a1.getGuid(), b2.getGuid(), b1.getGuid())); - AtlasEntity storedEntity = storedEntities.getEntity(a1.getGuid()); - Object value = storedEntity.getAttribute("mapToB"); - assertTrue(value instanceof Map); - Map<String, AtlasObjectId> refMap = (Map<String, AtlasObjectId>) value; - assertEquals(refMap.size(), 2); - AtlasObjectId referencedEntityId = refMap.get("b1"); - assertEquals(referencedEntityId, AtlasTypeUtil.getAtlasObjectId(b1)); - referencedEntityId = refMap.get("b2"); - assertEquals(referencedEntityId, AtlasTypeUtil.getAtlasObjectId(b2)); - storedEntity = storedEntities.getEntity(b1.getGuid()); - verifyReferenceValue(storedEntity, "mappedFromA", a1.getGuid()); - storedEntity = storedEntities.getEntity(b2.getGuid()); - verifyReferenceValue(storedEntity, "mappedFromA", a1.getGuid()); - - aForPartialUpdate.setAttribute("mapToB", ImmutableMap.<String, AtlasObjectId>of("b3", AtlasTypeUtil.getAtlasObjectId(b3))); - init(); - response = entityStore.updateByUniqueAttributes(aType, Collections.<String, Object>singletonMap(NAME, a1.getAttribute(NAME)), new AtlasEntityWithExtInfo(aForPartialUpdate)); - partialUpdatedEntities = response.getPartialUpdatedEntities(); - // Verify 4 entities were updated: - // * set a1.mapToB to "b3"->b3 - // * set b3.mappedFromA to a1 - // * disconnect b1.mappedFromA - // * disconnect b2.mappedFromA - assertEquals(partialUpdatedEntities.size(), 4); - storedEntities = entityStore.getByIds(ImmutableList.of(a1.getGuid(), b2.getGuid(), b1.getGuid(), b3.getGuid())); - AtlasEntity storedB3 = storedEntities.getEntity(b3.getGuid()); - verifyReferenceValue(storedB3, "mappedFromA", a1.getGuid()); - verify_testInverseReferenceAutoUpdate_Map(storedEntities.getEntity(a1.getGuid()), storedEntities.getEntity(b1.getGuid()), storedEntities.getEntity(b2.getGuid()), storedB3); - } - - protected abstract void verify_testInverseReferenceAutoUpdate_Map(AtlasEntity a1, AtlasEntity b1, AtlasEntity b2, AtlasEntity b3); - - protected void verifyReferenceValue(AtlasEntity entity, String refName, String expectedGuid) { - Object refValue = entity.getAttribute(refName); - if (expectedGuid == null) { - assertNull(refValue); - } - else { - assertTrue(refValue instanceof AtlasObjectId); - AtlasObjectId referencedObjectId = (AtlasObjectId) refValue; - assertEquals(referencedObjectId.getGuid(), expectedGuid); - } - } - - protected void verifyReferenceList(AtlasEntity entity, String refName, List<AtlasObjectId> expectedValues) { - Object refValue = entity.getAttribute(refName); - assertTrue(refValue instanceof List); - List<AtlasObjectId> refList = (List<AtlasObjectId>) refValue; - assertEquals(refList.size(), expectedValues.size()); - if (expectedValues.size() > 0) { - assertTrue(refList.containsAll(expectedValues)); - } - } -} http://git-wip-us.apache.org/repos/asf/atlas/blob/0877e47c/repository/src/test/java/org/apache/atlas/repository/store/graph/v1/SoftDeleteHandlerV1Test.java ---------------------------------------------------------------------- diff --git a/repository/src/test/java/org/apache/atlas/repository/store/graph/v1/SoftDeleteHandlerV1Test.java b/repository/src/test/java/org/apache/atlas/repository/store/graph/v1/SoftDeleteHandlerV1Test.java deleted file mode 100644 index 3f1ed03..0000000 --- a/repository/src/test/java/org/apache/atlas/repository/store/graph/v1/SoftDeleteHandlerV1Test.java +++ /dev/null @@ -1,215 +0,0 @@ -/** - * 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.TestModules; -import org.apache.atlas.exception.AtlasBaseException; -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.typesystem.IReferenceableInstance; -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 org.testng.annotations.Guice; - -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; - -@Guice(modules = TestModules.SoftDeleteModule.class) -public class SoftDeleteHandlerV1Test extends AtlasDeleteHandlerV1Test { - - @Inject - MetadataService metadataService; - - @Override - protected void assertDeletedColumn(final AtlasEntity.AtlasEntityWithExtInfo tableInstance) throws AtlasBaseException { - final List<AtlasObjectId> columns = (List<AtlasObjectId>) tableInstance.getEntity().getAttribute(COLUMNS_ATTR_NAME); - Assert.assertEquals(columns.size(), 3); - - final AtlasEntity.AtlasEntityWithExtInfo colDeleted = entityStore.getById(columns.get(0).getGuid()); - assertEquals(colDeleted.getEntity().getStatus(), AtlasEntity.Status.DELETED); - } - - @Override - protected void assertTestDeleteEntities(final AtlasEntity.AtlasEntityWithExtInfo tableInstance) throws Exception { - //Assert that the deleted table can be fully constructed back - List<IReferenceableInstance> columns = (List<IReferenceableInstance>) tableInstance.getEntity().getAttribute(COLUMNS_ATTR_NAME); - assertEquals(columns.size(), 3); - assertNotNull(tableInstance.getEntity().getAttribute("database")); - } - - @Override - protected void assertTableForTestDeleteReference(final String tableId) throws Exception { - - 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.AtlasEntityWithExtInfo tableInstance) throws AtlasBaseException { - List<AtlasObjectId> columns = (List<AtlasObjectId>) tableInstance.getEntity().getAttribute(COLUMNS_ATTR_NAME); - assertEquals(columns.size(), 1); - - final AtlasEntity.AtlasEntityWithExtInfo byId = entityStore.getById(columns.get(0).getGuid()); - assertEquals(byId.getEntity().getStatus(), AtlasEntity.Status.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 { - 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 AtlasEntity.AtlasEntityWithExtInfo john, final String janeGuid) throws Exception { - AtlasObjectId mgr = (AtlasObjectId) john.getEntity().getAttribute("manager"); - assertNotNull(mgr); - assertEquals(mgr.getGuid(), janeGuid); - - - final AtlasEntity.AtlasEntityWithExtInfo mgrEntity = entityStore.getById(mgr.getGuid()); - assertEquals(mgrEntity.getEntity().getStatus(), AtlasEntity.Status.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<AtlasObjectId> columns, final String columnGuid) throws AtlasBaseException { - Assert.assertEquals(columns.size(), 3); - for (AtlasObjectId column : columns) { - AtlasEntity.AtlasEntityWithExtInfo columnEntity = entityStore.getById(column.getGuid()); - if (column.getGuid().equals(columnGuid)) { - assertEquals(columnEntity.getEntity().getStatus(), AtlasEntity.Status.DELETED); - } else { - assertEquals(columnEntity.getEntity().getStatus(), AtlasEntity.Status.ACTIVE); - } - } - } - - @Override - protected void assertTestDisconnectMapReferenceFromClassType(final String mapOwnerGuid) throws Exception { - AtlasEntity.AtlasEntityWithExtInfo mapOwnerInstance = entityStore.getById(mapOwnerGuid); - Map<String, AtlasObjectId> map = - (Map<String, AtlasObjectId>) mapOwnerInstance.getEntity().getAttribute("map"); - assertNotNull(map); - assertEquals(map.size(), 1); - Map<String, AtlasObjectId> biMap = - (Map<String, AtlasObjectId>) mapOwnerInstance.getEntity().getAttribute("biMap"); - assertNotNull(biMap); - assertEquals(biMap.size(), 1); - } - - @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()); - } - } -}