http://git-wip-us.apache.org/repos/asf/atlas/blob/435fe3fb/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 index 6475639..2c21638 100644 --- 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 @@ -20,7 +20,6 @@ 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; @@ -32,7 +31,6 @@ 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; @@ -48,11 +46,12 @@ 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; +import static org.apache.atlas.TestUtilsV2.NAME; + /** * Test automatic inverse reference updating in V1 (V2?) code path. * @@ -67,9 +66,6 @@ public abstract class InverseReferenceUpdateV1Test { @Inject AtlasEntityStore entityStore; - @Inject - MetadataService metadataService; - private AtlasEntitiesWithExtInfo deptEntity; protected Map<String, AtlasObjectId> nameIdMap = new HashMap<>(); @@ -79,8 +75,6 @@ public abstract class InverseReferenceUpdateV1Test { RequestContextV1.clear(); RequestContextV1.get().setUser(TestUtilsV2.TEST_USER); - metadataService = TestUtils.addSessionCleanupWrapper(metadataService); - AtlasTypesDef[] testTypesDefs = new AtlasTypesDef[] { TestUtilsV2.defineDeptEmployeeTypes(), TestUtilsV2.defineInverseReferenceTestTypes() }; @@ -147,14 +141,14 @@ public abstract class InverseReferenceUpdateV1Test { public void testInverseReferenceAutoUpdate_NonCompositeManyToOne() throws Exception { AtlasEntityType bType = typeRegistry.getEntityTypeByName("B"); AtlasEntity a1 = new AtlasEntity("A"); - a1.setAttribute(NAME, TestUtils.randomString()); + a1.setAttribute(NAME, TestUtilsV2.randomString()); AtlasEntity a2 = new AtlasEntity("A"); - a2.setAttribute(NAME, TestUtils.randomString()); + a2.setAttribute(NAME, TestUtilsV2.randomString()); AtlasEntity a3 = new AtlasEntity("A"); - a3.setAttribute(NAME, TestUtils.randomString()); + a3.setAttribute(NAME, TestUtilsV2.randomString()); AtlasEntity b = new AtlasEntity("B"); - b.setAttribute(NAME, TestUtils.randomString()); + b.setAttribute(NAME, TestUtilsV2.randomString()); AtlasEntitiesWithExtInfo atlasEntitiesWithExtInfo = new AtlasEntitiesWithExtInfo(); atlasEntitiesWithExtInfo.addEntity(a1); atlasEntitiesWithExtInfo.addEntity(a2); @@ -208,11 +202,11 @@ public abstract class InverseReferenceUpdateV1Test { public void testInverseReferenceAutoUpdate_NonComposite_OneToOne() throws Exception { AtlasEntityType bType = typeRegistry.getEntityTypeByName("B"); AtlasEntity a1 = new AtlasEntity("A"); - a1.setAttribute(NAME, TestUtils.randomString()); + a1.setAttribute(NAME, TestUtilsV2.randomString()); AtlasEntity a2 = new AtlasEntity("A"); - a2.setAttribute(NAME, TestUtils.randomString()); + a2.setAttribute(NAME, TestUtilsV2.randomString()); AtlasEntity b = new AtlasEntity("B"); - b.setAttribute(NAME, TestUtils.randomString()); + b.setAttribute(NAME, TestUtilsV2.randomString()); AtlasEntitiesWithExtInfo atlasEntitiesWithExtInfo = new AtlasEntitiesWithExtInfo(); atlasEntitiesWithExtInfo.addEntity(a1); atlasEntitiesWithExtInfo.addEntity(a2); @@ -261,15 +255,15 @@ public abstract class InverseReferenceUpdateV1Test { public void testInverseReferenceAutoUpdate_NonComposite_ManyToMany() throws Exception { AtlasEntityType bType = typeRegistry.getEntityTypeByName("B"); AtlasEntity a1 = new AtlasEntity("A"); - a1.setAttribute(NAME, TestUtils.randomString()); + a1.setAttribute(NAME, TestUtilsV2.randomString()); AtlasEntity a2 = new AtlasEntity("A"); - a2.setAttribute(NAME, TestUtils.randomString()); + a2.setAttribute(NAME, TestUtilsV2.randomString()); AtlasEntity a3 = new AtlasEntity("A"); - a3.setAttribute(NAME, TestUtils.randomString()); + a3.setAttribute(NAME, TestUtilsV2.randomString()); AtlasEntity b1 = new AtlasEntity("B"); - b1.setAttribute(NAME, TestUtils.randomString()); + b1.setAttribute(NAME, TestUtilsV2.randomString()); AtlasEntity b2 = new AtlasEntity("B"); - b2.setAttribute(NAME, TestUtils.randomString()); + b2.setAttribute(NAME, TestUtilsV2.randomString()); AtlasEntitiesWithExtInfo atlasEntitiesWithExtInfo = new AtlasEntitiesWithExtInfo(); atlasEntitiesWithExtInfo.addEntity(a1); atlasEntitiesWithExtInfo.addEntity(a2); @@ -297,13 +291,13 @@ public abstract class InverseReferenceUpdateV1Test { @Test public void testInverseReferenceAutoUpdate_Map() throws Exception { AtlasEntity a1 = new AtlasEntity("A"); - a1.setAttribute(NAME, TestUtils.randomString()); + a1.setAttribute(NAME, TestUtilsV2.randomString()); AtlasEntity b1 = new AtlasEntity("B"); - b1.setAttribute(NAME, TestUtils.randomString()); + b1.setAttribute(NAME, TestUtilsV2.randomString()); AtlasEntity b2 = new AtlasEntity("B"); - b2.setAttribute(NAME, TestUtils.randomString()); + b2.setAttribute(NAME, TestUtilsV2.randomString()); AtlasEntity b3 = new AtlasEntity("B"); - b3.setAttribute(NAME, TestUtils.randomString()); + b3.setAttribute(NAME, TestUtilsV2.randomString()); AtlasEntitiesWithExtInfo atlasEntitiesWithExtInfo = new AtlasEntitiesWithExtInfo(); atlasEntitiesWithExtInfo.addEntity(a1); atlasEntitiesWithExtInfo.addEntity(b1);
http://git-wip-us.apache.org/repos/asf/atlas/blob/435fe3fb/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()); - } - } -} http://git-wip-us.apache.org/repos/asf/atlas/blob/435fe3fb/repository/src/test/java/org/apache/atlas/repository/typestore/GraphBackedTypeStoreTest.java ---------------------------------------------------------------------- diff --git a/repository/src/test/java/org/apache/atlas/repository/typestore/GraphBackedTypeStoreTest.java b/repository/src/test/java/org/apache/atlas/repository/typestore/GraphBackedTypeStoreTest.java deleted file mode 100755 index 6e3dabb..0000000 --- a/repository/src/test/java/org/apache/atlas/repository/typestore/GraphBackedTypeStoreTest.java +++ /dev/null @@ -1,256 +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.typestore; - -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableSet; -import org.apache.atlas.AtlasException; -import org.apache.atlas.TestModules; -import org.apache.atlas.TestUtils; -import org.apache.atlas.repository.RepositoryException; -import org.apache.atlas.repository.graph.GraphHelper; -import org.apache.atlas.repository.graphdb.AtlasEdge; -import org.apache.atlas.repository.graphdb.AtlasEdgeDirection; -import org.apache.atlas.repository.graphdb.AtlasGraph; -import org.apache.atlas.repository.graphdb.AtlasVertex; -import org.apache.atlas.typesystem.TypesDef; -import org.apache.atlas.typesystem.types.*; -import org.apache.atlas.typesystem.types.utils.TypesUtil; -import org.testng.Assert; -import org.testng.annotations.AfterClass; -import org.testng.annotations.BeforeClass; -import org.testng.annotations.Guice; -import org.testng.annotations.Test; - -import javax.inject.Inject; -import java.util.Collections; -import java.util.Iterator; -import java.util.List; -import java.util.Map; - -import static org.apache.atlas.typesystem.types.utils.TypesUtil.createClassTypeDef; -import static org.apache.atlas.typesystem.types.utils.TypesUtil.createOptionalAttrDef; -import static org.apache.atlas.typesystem.types.utils.TypesUtil.createRequiredAttrDef; -import static org.apache.atlas.typesystem.types.utils.TypesUtil.createStructTypeDef; - -@Guice(modules = TestModules.TestOnlyModule.class) -public class GraphBackedTypeStoreTest { - - private static final String DESCRIPTION = "_description"; - - @Inject - private ITypeStore typeStore; - - private TypeSystem ts; - - @BeforeClass - public void setUp() throws Exception { - ts = TypeSystem.getInstance(); - ts.reset(); - TestUtils.defineDeptEmployeeTypes(ts); - } - - @AfterClass - public void tearDown() throws Exception { - ts.reset(); -// AtlasGraphProvider.cleanup(); - } - - - @Test - public void testStore() throws AtlasException { - ImmutableList<String> typeNames = ts.getTypeNames(); - typeStore.store(ts, typeNames); - dumpGraph(); - } - - @Test(dependsOnMethods = "testStore") - public void testRestoreType() throws Exception { - TypesDef typesDef = typeStore.restoreType("Manager"); - verifyRestoredClassType(typesDef, "Manager"); - } - - private void dumpGraph() { - AtlasGraph<?, ?> graph = TestUtils.getGraph(); - for (AtlasVertex<?,?> v : graph.getVertices()) { - System.out.println("****v = " + GraphHelper.vertexString(v)); - for (AtlasEdge<?,?> e : v.getEdges(AtlasEdgeDirection.OUT)) { - System.out.println("****e = " + GraphHelper.edgeString(e)); - } - } - } - - @Test(dependsOnMethods = "testStore") - public void testRestore() throws Exception { - TypesDef types = typeStore.restore(); - - //validate enum - List<EnumTypeDefinition> enumTypes = types.enumTypesAsJavaList(); - Assert.assertEquals(1, enumTypes.size()); - EnumTypeDefinition orgLevel = enumTypes.get(0); - Assert.assertEquals(orgLevel.name, "OrgLevel"); - Assert.assertEquals(orgLevel.description, "OrgLevel"+DESCRIPTION); - Assert.assertEquals(orgLevel.enumValues.length, 2); - EnumValue enumValue = orgLevel.enumValues[0]; - Assert.assertEquals(enumValue.value, "L1"); - Assert.assertEquals(enumValue.ordinal, 1); - - //validate class - List<StructTypeDefinition> structTypes = types.structTypesAsJavaList(); - Assert.assertEquals(1, structTypes.size()); - - verifyRestoredClassType(types, "Manager"); - - //validate trait - List<HierarchicalTypeDefinition<TraitType>> traitTypes = types.traitTypesAsJavaList(); - Assert.assertEquals(1, traitTypes.size()); - HierarchicalTypeDefinition<TraitType> trait = traitTypes.get(0); - Assert.assertEquals("SecurityClearance", trait.typeName); - Assert.assertEquals(trait.typeName+DESCRIPTION, trait.typeDescription); - Assert.assertEquals(1, trait.attributeDefinitions.length); - AttributeDefinition attribute = trait.attributeDefinitions[0]; - Assert.assertEquals("level", attribute.name); - Assert.assertEquals(DataTypes.INT_TYPE.getName(), attribute.dataTypeName); - - //validate the new types - ts.reset(); - ts.defineTypes(types); - } - - @Test - public void testTypeWithSpecialChars() throws AtlasException { - HierarchicalTypeDefinition<ClassType> specialTypeDef1 = createClassTypeDef("SpecialTypeDef1", "Typedef with special character", - ImmutableSet.<String>of(), createRequiredAttrDef("attribute$", DataTypes.STRING_TYPE)); - - HierarchicalTypeDefinition<ClassType> specialTypeDef2 = createClassTypeDef("SpecialTypeDef2", "Typedef with special character", - ImmutableSet.<String>of(), createRequiredAttrDef("attribute%", DataTypes.STRING_TYPE)); - - HierarchicalTypeDefinition<ClassType> specialTypeDef3 = createClassTypeDef("SpecialTypeDef3", "Typedef with special character", - ImmutableSet.<String>of(), createRequiredAttrDef("attribute{", DataTypes.STRING_TYPE)); - - HierarchicalTypeDefinition<ClassType> specialTypeDef4 = createClassTypeDef("SpecialTypeDef4", "Typedef with special character", - ImmutableSet.<String>of(), createRequiredAttrDef("attribute}", DataTypes.STRING_TYPE)); - - HierarchicalTypeDefinition<ClassType> specialTypeDef5 = createClassTypeDef("SpecialTypeDef5", "Typedef with special character", - ImmutableSet.<String>of(), createRequiredAttrDef("attribute$%{}", DataTypes.STRING_TYPE)); - - TypesDef typesDef = TypesUtil.getTypesDef(ImmutableList.<EnumTypeDefinition>of(), - ImmutableList.<StructTypeDefinition>of(), - ImmutableList.<HierarchicalTypeDefinition<TraitType>>of(), - ImmutableList.of(specialTypeDef1, specialTypeDef2, specialTypeDef3, specialTypeDef4, specialTypeDef5)); - - Map<String, IDataType> createdTypes = ts.defineTypes(typesDef); - typeStore.store(ts, ImmutableList.copyOf(createdTypes.keySet())); - - //Validate the updated types - TypesDef types = typeStore.restore(); - ts.reset(); - ts.defineTypes(types); - } - - @Test(dependsOnMethods = "testStore") - public void testTypeUpdate() throws Exception { - //Add enum value - String _description = "_description_updated"; - EnumTypeDefinition orgLevelEnum = new EnumTypeDefinition("OrgLevel", "OrgLevel"+_description, new EnumValue("L1", 1), - new EnumValue("L2", 2), new EnumValue("L3", 3)); - - //Add attribute - StructTypeDefinition addressDetails = - createStructTypeDef("Address", createRequiredAttrDef("street", DataTypes.STRING_TYPE), - createRequiredAttrDef("city", DataTypes.STRING_TYPE), - createOptionalAttrDef("state", DataTypes.STRING_TYPE)); - - HierarchicalTypeDefinition<ClassType> deptTypeDef = createClassTypeDef("Department", "Department"+_description, - ImmutableSet.<String>of(), createRequiredAttrDef("name", DataTypes.STRING_TYPE), - new AttributeDefinition("employees", String.format("array<%s>", "Person"), Multiplicity.OPTIONAL, - true, "department"), - new AttributeDefinition("positions", String.format("map<%s,%s>", DataTypes.STRING_TYPE.getName(), "Person"), Multiplicity.OPTIONAL, false, null)); - TypesDef typesDef = TypesUtil.getTypesDef(ImmutableList.of(orgLevelEnum), ImmutableList.of(addressDetails), - ImmutableList.<HierarchicalTypeDefinition<TraitType>>of(), - ImmutableList.of(deptTypeDef)); - - Map<String, IDataType> typesAdded = ts.updateTypes(typesDef); - typeStore.store(ts, ImmutableList.copyOf(typesAdded.keySet())); - - verifyEdges(); - - //Validate the updated types - TypesDef types = typeStore.restore(); - ts.reset(); - ts.defineTypes(types); - - //Assert new enum value - EnumType orgLevel = ts.getDataType(EnumType.class, orgLevelEnum.name); - Assert.assertEquals(orgLevel.name, orgLevelEnum.name); - Assert.assertEquals(orgLevel.description, orgLevelEnum.description); - Assert.assertEquals(orgLevel.values().size(), orgLevelEnum.enumValues.length); - Assert.assertEquals(orgLevel.fromValue("L3").ordinal, 3); - - //Assert new attribute - StructType addressType = ts.getDataType(StructType.class, addressDetails.typeName); - Assert.assertEquals(addressType.numFields, 3); - Assert.assertEquals(addressType.fieldMapping.fields.get("state").dataType(), DataTypes.STRING_TYPE); - - //Updating the definition again shouldn't add another edge - typesDef = TypesUtil.getTypesDef(ImmutableList.<EnumTypeDefinition>of(), - ImmutableList.<StructTypeDefinition>of(), - ImmutableList.<HierarchicalTypeDefinition<TraitType>>of(), - ImmutableList.of(deptTypeDef)); - typesAdded = ts.updateTypes(typesDef); - typeStore.store(ts, ImmutableList.copyOf(typesAdded.keySet())); - verifyEdges(); - } - - private void verifyEdges() throws RepositoryException { - // ATLAS-474: verify that type update did not write duplicate edges to the type store. - if (typeStore instanceof GraphBackedTypeStore) { - GraphBackedTypeStore gbTypeStore = (GraphBackedTypeStore) typeStore; - AtlasVertex typeVertex = gbTypeStore.findVertices(Collections.singletonList("Department")).get("Department"); - int edgeCount = countOutgoingEdges(typeVertex, gbTypeStore.getEdgeLabel("Department", "employees")); - Assert.assertEquals(edgeCount, 1, "Should only be 1 edge for employees attribute on Department type AtlasVertex"); - } - } - - private int countOutgoingEdges(AtlasVertex typeVertex, String edgeLabel) { - - Iterator<AtlasEdge> outGoingEdgesByLabel = GraphHelper.getInstance().getOutGoingEdgesByLabel(typeVertex, edgeLabel); - int edgeCount = 0; - for (; outGoingEdgesByLabel.hasNext();) { - outGoingEdgesByLabel.next(); - edgeCount++; - } - return edgeCount; - } - - private void verifyRestoredClassType(TypesDef types, String typeName) throws AtlasException { - boolean clsTypeFound = false; - List<HierarchicalTypeDefinition<ClassType>> classTypes = types.classTypesAsJavaList(); - for (HierarchicalTypeDefinition<ClassType> classType : classTypes) { - if (classType.typeName.equals(typeName)) { - ClassType expectedType = ts.getDataType(ClassType.class, classType.typeName); - Assert.assertEquals(expectedType.immediateAttrs.size(), classType.attributeDefinitions.length); - Assert.assertEquals(expectedType.superTypes.size(), classType.superTypes.size()); - Assert.assertEquals(classType.typeDescription, classType.typeName+DESCRIPTION); - clsTypeFound = true; - } - } - Assert.assertTrue(clsTypeFound, typeName + " type not restored"); - } -} http://git-wip-us.apache.org/repos/asf/atlas/blob/435fe3fb/repository/src/test/java/org/apache/atlas/repository/typestore/StoreBackedTypeCacheTest.java ---------------------------------------------------------------------- diff --git a/repository/src/test/java/org/apache/atlas/repository/typestore/StoreBackedTypeCacheTest.java b/repository/src/test/java/org/apache/atlas/repository/typestore/StoreBackedTypeCacheTest.java deleted file mode 100644 index 2ea63ff..0000000 --- a/repository/src/test/java/org/apache/atlas/repository/typestore/StoreBackedTypeCacheTest.java +++ /dev/null @@ -1,169 +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.typestore; - -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableSet; -import org.apache.atlas.AtlasException; -import org.apache.atlas.TestModules; -import org.apache.atlas.TestUtils; -import org.apache.atlas.typesystem.types.AttributeInfo; -import org.apache.atlas.typesystem.types.ClassType; -import org.apache.atlas.typesystem.types.DataTypes.TypeCategory; -import org.apache.atlas.typesystem.types.HierarchicalType; -import org.apache.atlas.typesystem.types.IDataType; -import org.apache.atlas.typesystem.types.TraitType; -import org.apache.atlas.typesystem.types.TypeSystem; -import org.apache.atlas.typesystem.types.TypeUtils; -import org.testng.Assert; -import org.testng.annotations.AfterClass; -import org.testng.annotations.BeforeClass; -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.Guice; -import org.testng.annotations.Test; - -import javax.inject.Inject; -import java.util.HashMap; -import java.util.Map; - - -/** - * Unit test for {@link StoreBackedTypeCache} - */ -@Guice(modules = TestModules.TestOnlyModule.class) -public class StoreBackedTypeCacheTest { - - @Inject - private ITypeStore typeStore; - - @Inject - private StoreBackedTypeCache typeCache; - - private TypeSystem ts; - - private Map<String, ClassType> classTypesToTest = new HashMap<>(); - - @Inject - public StoreBackedTypeCacheTest() { - } - - @BeforeClass - public void setUp() throws Exception { - //force graph to be initialized up front - TestUtils.getGraph(); - - ts = TypeSystem.getInstance(); - ts.reset(); - ts.setTypeCache(typeCache); - - // Populate the type store for testing. - TestUtils.defineDeptEmployeeTypes(ts); - TestUtils.createHiveTypes(ts); - ImmutableList<String> typeNames = ts.getTypeNames(); - typeStore.store(ts, typeNames); - - ClassType type = ts.getDataType(ClassType.class, "Manager"); - classTypesToTest.put("Manager", type); - type = ts.getDataType(ClassType.class, TestUtils.TABLE_TYPE); - classTypesToTest.put(TestUtils.TABLE_TYPE, type); - } - - @AfterClass - public void tearDown() throws Exception { - ts.reset(); -// AtlasGraphProvider.cleanup(); - } - - @BeforeMethod - public void setupTestMethod() throws Exception { - typeCache.clear(); - } - - @Test - public void testGetClassType() throws Exception { - for (Map.Entry<String, ClassType> typeEntry : classTypesToTest.entrySet()) { - // Not cached yet - Assert.assertFalse(typeCache.isCachedInMemory(typeEntry.getKey())); - - IDataType dataType = ts.getDataType(IDataType.class, typeEntry.getKey()); - // Verify the type is now cached. - Assert.assertTrue(typeCache.isCachedInMemory(typeEntry.getKey())); - - Assert.assertTrue(dataType instanceof ClassType); - ClassType cachedType = (ClassType)dataType; - // Verify that get() also loaded and cached any dependencies of this type from the type store. - verifyHierarchicalType(cachedType, typeEntry.getValue()); - } - } - - @Test - public void testGetTraitType() throws Exception { - ImmutableList<String> traitNames = ts.getTypeNamesByCategory(TypeCategory.TRAIT); - for (String traitTypeName : traitNames) { - // Not cached yet - Assert.assertFalse(typeCache.isCachedInMemory(traitTypeName)); - - IDataType dataType = typeCache.get(traitTypeName); - // Verify the type is now cached. - Assert.assertTrue(typeCache.isCachedInMemory(traitTypeName)); - - Assert.assertTrue(dataType instanceof TraitType); - TraitType cachedType = (TraitType)dataType; - // Verify that get() also loaded and cached any dependencies of this type from the type store. - verifyHierarchicalType(cachedType, ts.getDataType(TraitType.class, traitTypeName)); - } - } - - private <T extends HierarchicalType> void verifyHierarchicalType(T dataType, T expectedDataType) throws AtlasException { - Assert.assertEquals(dataType.numFields, expectedDataType.numFields); - Assert.assertEquals(dataType.immediateAttrs.size(), expectedDataType.immediateAttrs.size()); - Assert.assertEquals(dataType.fieldMapping().fields.size(), expectedDataType.fieldMapping().fields.size()); - ImmutableSet<String> superTypes = dataType.superTypes; - Assert.assertEquals(superTypes.size(), expectedDataType.superTypes.size()); - - // Verify that any attribute and super types were also cached. - for (String superTypeName : superTypes) { - Assert.assertTrue(typeCache.has(superTypeName)); - } - for (AttributeInfo attrInfo : dataType.fieldMapping().fields.values()) { - switch (attrInfo.dataType().getTypeCategory()) { - case CLASS: - case STRUCT: - case ENUM: - Assert.assertTrue(typeCache.has(attrInfo.dataType().getName()), attrInfo.dataType().getName() + " should be cached"); - break; - case ARRAY: - String elementTypeName = TypeUtils.parseAsArrayType(attrInfo.dataType().getName()); - if (!ts.getCoreTypes().contains(elementTypeName)) { - Assert.assertTrue(typeCache.has(elementTypeName), elementTypeName + " should be cached"); - } - break; - case MAP: - String[] mapTypeNames = TypeUtils.parseAsMapType(attrInfo.dataType().getName()); - for (String typeName : mapTypeNames) { - if (!ts.getCoreTypes().contains(typeName)) { - Assert.assertTrue(typeCache.has(typeName), typeName + " should be cached"); - } - } - break; - default: - break; - } - } - } -}