Search in sources :

Example 51 with Struct

use of org.apache.atlas.typesystem.Struct in project incubator-atlas by apache.

the class TraitTest method testRandomOrder.

@Test
public void testRandomOrder() throws AtlasException {
    HierarchicalTypeDefinition A = createTraitTypeDef("A", null, createRequiredAttrDef("a", DataTypes.INT_TYPE), createOptionalAttrDef("b", DataTypes.BOOLEAN_TYPE), createOptionalAttrDef("c", DataTypes.BYTE_TYPE), createOptionalAttrDef("d", DataTypes.SHORT_TYPE));
    HierarchicalTypeDefinition B = createTraitTypeDef("B", ImmutableSet.of("A"), createOptionalAttrDef("b", DataTypes.BOOLEAN_TYPE));
    HierarchicalTypeDefinition C = createTraitTypeDef("C", ImmutableSet.of("A"), createOptionalAttrDef("c", DataTypes.BYTE_TYPE));
    HierarchicalTypeDefinition D = createTraitTypeDef("D", ImmutableSet.of("B", "C"), createOptionalAttrDef("d", DataTypes.SHORT_TYPE));
    defineTraits(B, D, A, C);
    TraitType DType = getTypeSystem().getDataType(TraitType.class, "D");
    Struct s1 = new Struct("D");
    s1.set("d", 1);
    s1.set("c", 1);
    s1.set("b", true);
    s1.set("a", 1);
    s1.set("A.B.D.b", true);
    s1.set("A.B.D.c", 2);
    s1.set("A.B.D.d", 2);
    s1.set("A.C.D.a", 3);
    s1.set("A.C.D.b", false);
    s1.set("A.C.D.c", 3);
    s1.set("A.C.D.d", 3);
    ITypedStruct ts = DType.convert(s1, Multiplicity.REQUIRED);
    Assert.assertEquals(ts.toString(), "{\n" + "\td : \t1\n" + "\tb : \ttrue\n" + "\tc : \t1\n" + "\ta : \t1\n" + "\tA.B.D.b : \ttrue\n" + "\tA.B.D.c : \t2\n" + "\tA.B.D.d : \t2\n" + "\tA.C.D.a : \t3\n" + "\tA.C.D.b : \tfalse\n" + "\tA.C.D.c : \t3\n" + "\tA.C.D.d : \t3\n" + "}");
}
Also used : ITypedStruct(org.apache.atlas.typesystem.ITypedStruct) ITypedStruct(org.apache.atlas.typesystem.ITypedStruct) IStruct(org.apache.atlas.typesystem.IStruct) Struct(org.apache.atlas.typesystem.Struct) Test(org.testng.annotations.Test)

Example 52 with Struct

use of org.apache.atlas.typesystem.Struct in project incubator-atlas by apache.

the class SerializationJavaTest method testTrait.

@Test
public void testTrait() throws AtlasException {
    TypeSystem ts = getTypeSystem();
    HierarchicalTypeDefinition<TraitType> securityClearanceTypeDef = createTraitTypeDef("SecurityClearance2", ImmutableSet.<String>of(), createRequiredAttrDef("level", DataTypes.INT_TYPE));
    ts.defineTypes(ImmutableList.<EnumTypeDefinition>of(), ImmutableList.<StructTypeDefinition>of(), ImmutableList.of(securityClearanceTypeDef), ImmutableList.<HierarchicalTypeDefinition<ClassType>>of());
    Struct s = new Struct("SecurityClearance2");
    s.set("level", 1);
    TraitType tType = ts.getDataType(TraitType.class, "SecurityClearance2");
    ITypedInstance t = tType.convert(s, Multiplicity.REQUIRED);
    String jsonStr = Serialization$.MODULE$.toJson(t);
    ITypedInstance t2 = Serialization$.MODULE$.traitFromJson(jsonStr);
    Assert.assertEquals(t.toString(), t2.toString());
}
Also used : ITypedInstance(org.apache.atlas.typesystem.ITypedInstance) TypeSystem(org.apache.atlas.typesystem.types.TypeSystem) TraitType(org.apache.atlas.typesystem.types.TraitType) ClassType(org.apache.atlas.typesystem.types.ClassType) Struct(org.apache.atlas.typesystem.Struct) Test(org.testng.annotations.Test) BaseTest(org.apache.atlas.typesystem.types.BaseTest)

Example 53 with Struct

use of org.apache.atlas.typesystem.Struct in project incubator-atlas by apache.

the class HiveHookIT method createTrait.

private String createTrait(String guid) throws AtlasServiceException, JSONException {
    //add trait
    //valid type names in v2 must consist of a letter followed by a sequence of letter, number, or _ characters
    String traitName = "PII_Trait" + random();
    atlasClient.createTraitType(traitName);
    Struct traitInstance = new Struct(traitName);
    atlasClient.addTrait(guid, traitInstance);
    return traitName;
}
Also used : Struct(org.apache.atlas.typesystem.Struct)

Example 54 with Struct

use of org.apache.atlas.typesystem.Struct in project incubator-atlas by apache.

the class HiveHookIT method verifyBucketSortingProperties.

private void verifyBucketSortingProperties(Referenceable tableRef, int numBuckets, ImmutableList<String> bucketColNames, ImmutableList<String> sortcolNames) throws Exception {
    Referenceable sdRef = (Referenceable) tableRef.get(HiveMetaStoreBridge.STORAGE_DESC);
    Assert.assertEquals(((scala.math.BigInt) sdRef.get(HiveMetaStoreBridge.STORAGE_NUM_BUCKETS)).intValue(), numBuckets);
    Assert.assertEquals(sdRef.get("bucketCols"), bucketColNames);
    List<Struct> hiveOrderStructList = (List<Struct>) sdRef.get("sortCols");
    Assert.assertNotNull(hiveOrderStructList);
    Assert.assertEquals(hiveOrderStructList.size(), sortcolNames.size());
    for (int i = 0; i < sortcolNames.size(); i++) {
        Assert.assertEquals(hiveOrderStructList.get(i).get("col"), sortcolNames.get(i));
        Assert.assertEquals(((scala.math.BigInt) hiveOrderStructList.get(i).get("order")).intValue(), 1);
    }
}
Also used : Referenceable(org.apache.atlas.typesystem.Referenceable) ImmutableList(com.google.common.collect.ImmutableList) Struct(org.apache.atlas.typesystem.Struct)

Example 55 with Struct

use of org.apache.atlas.typesystem.Struct in project incubator-atlas by apache.

the class GraphBackedMetadataRepositoryDeleteTestBase method testDisconnectUnidirectionalArrayReferenceFromStructAndTraitTypes.

/**
     * Verify deleting entities that are the target of a unidirectional class array reference
     * from a struct or trait instance.
     */
@Test
public void testDisconnectUnidirectionalArrayReferenceFromStructAndTraitTypes() throws Exception {
    // Define class types.
    HierarchicalTypeDefinition<ClassType> structTargetDef = TypesUtil.createClassTypeDef("StructTarget", ImmutableSet.<String>of(), TypesUtil.createOptionalAttrDef("attr1", DataTypes.STRING_TYPE));
    HierarchicalTypeDefinition<ClassType> traitTargetDef = TypesUtil.createClassTypeDef("TraitTarget", ImmutableSet.<String>of(), TypesUtil.createOptionalAttrDef("attr1", DataTypes.STRING_TYPE));
    HierarchicalTypeDefinition<ClassType> structContainerDef = TypesUtil.createClassTypeDef("StructContainer", ImmutableSet.<String>of(), TypesUtil.createOptionalAttrDef("struct", "TestStruct"));
    // Define struct and trait types which have a unidirectional array reference
    // to a class type.
    StructTypeDefinition structDef = TypesUtil.createStructTypeDef("TestStruct", new AttributeDefinition("target", DataTypes.arrayTypeName("StructTarget"), Multiplicity.OPTIONAL, false, null), new AttributeDefinition("nestedStructs", DataTypes.arrayTypeName("NestedStruct"), Multiplicity.OPTIONAL, false, null));
    StructTypeDefinition nestedStructDef = TypesUtil.createStructTypeDef("NestedStruct", TypesUtil.createOptionalAttrDef("attr1", DataTypes.STRING_TYPE));
    HierarchicalTypeDefinition<TraitType> traitDef = TypesUtil.createTraitTypeDef("TestTrait", ImmutableSet.<String>of(), new AttributeDefinition("target", DataTypes.arrayTypeName("TraitTarget"), Multiplicity.OPTIONAL, false, null));
    TypesDef typesDef = TypesUtil.getTypesDef(ImmutableList.<EnumTypeDefinition>of(), ImmutableList.of(structDef, nestedStructDef), ImmutableList.of(traitDef), ImmutableList.of(structTargetDef, traitTargetDef, structContainerDef));
    typeSystem.defineTypes(typesDef);
    // Create instances of class, struct, and trait types.
    Referenceable structTargetEntity = new Referenceable("StructTarget");
    Referenceable traitTargetEntity = new Referenceable("TraitTarget");
    Referenceable structContainerEntity = new Referenceable("StructContainer");
    Struct structInstance = new Struct("TestStruct");
    Struct nestedStructInstance = new Struct("NestedStruct");
    Referenceable traitInstance = new Referenceable("TestTrait");
    structContainerEntity.set("struct", structInstance);
    structInstance.set("target", ImmutableList.of(structTargetEntity));
    structInstance.set("nestedStructs", ImmutableList.of(nestedStructInstance));
    ClassType structTargetType = typeSystem.getDataType(ClassType.class, "StructTarget");
    ClassType traitTargetType = typeSystem.getDataType(ClassType.class, "TraitTarget");
    ClassType structContainerType = typeSystem.getDataType(ClassType.class, "StructContainer");
    ITypedReferenceableInstance structTargetConvertedEntity = structTargetType.convert(structTargetEntity, Multiplicity.REQUIRED);
    ITypedReferenceableInstance traitTargetConvertedEntity = traitTargetType.convert(traitTargetEntity, Multiplicity.REQUIRED);
    ITypedReferenceableInstance structContainerConvertedEntity = structContainerType.convert(structContainerEntity, Multiplicity.REQUIRED);
    List<String> guids = repositoryService.createEntities(structTargetConvertedEntity, traitTargetConvertedEntity, structContainerConvertedEntity).getCreatedEntities();
    Assert.assertEquals(guids.size(), 3);
    guids = repositoryService.getEntityList("StructTarget");
    Assert.assertEquals(guids.size(), 1);
    String structTargetGuid = guids.get(0);
    guids = repositoryService.getEntityList("TraitTarget");
    Assert.assertEquals(guids.size(), 1);
    String traitTargetGuid = guids.get(0);
    guids = repositoryService.getEntityList("StructContainer");
    Assert.assertEquals(guids.size(), 1);
    String structContainerGuid = guids.get(0);
    // Add TestTrait to StructContainer instance
    traitInstance.set("target", ImmutableList.of(new Id(traitTargetGuid, 0, "TraitTarget")));
    TraitType traitType = typeSystem.getDataType(TraitType.class, "TestTrait");
    ITypedStruct convertedTrait = traitType.convert(traitInstance, Multiplicity.REQUIRED);
    repositoryService.addTrait(structContainerGuid, convertedTrait);
    // Verify that the unidirectional references from the struct and trait instances
    // are pointing at the target entities.
    structContainerConvertedEntity = repositoryService.getEntityDefinition(structContainerGuid);
    Object object = structContainerConvertedEntity.get("struct");
    Assert.assertNotNull(object);
    Assert.assertTrue(object instanceof ITypedStruct);
    ITypedStruct struct = (ITypedStruct) object;
    object = struct.get("target");
    Assert.assertNotNull(object);
    Assert.assertTrue(object instanceof List);
    List<ITypedReferenceableInstance> refList = (List<ITypedReferenceableInstance>) object;
    Assert.assertEquals(refList.size(), 1);
    Assert.assertEquals(refList.get(0).getId()._getId(), structTargetGuid);
    IStruct trait = structContainerConvertedEntity.getTrait("TestTrait");
    Assert.assertNotNull(trait);
    object = trait.get("target");
    Assert.assertNotNull(object);
    Assert.assertTrue(object instanceof List);
    refList = (List<ITypedReferenceableInstance>) object;
    Assert.assertEquals(refList.size(), 1);
    Assert.assertEquals(refList.get(0).getId()._getId(), traitTargetGuid);
    // Delete the entities that are targets of the struct and trait instances.
    EntityResult entityResult = deleteEntities(structTargetGuid, traitTargetGuid);
    Assert.assertEquals(entityResult.getDeletedEntities().size(), 2);
    Assert.assertTrue(entityResult.getDeletedEntities().containsAll(Arrays.asList(structTargetGuid, traitTargetGuid)));
    assertEntityDeleted(structTargetGuid);
    assertEntityDeleted(traitTargetGuid);
    assertTestDisconnectUnidirectionalArrayReferenceFromStructAndTraitTypes(structContainerGuid);
    // Delete the entity which contains nested structs and has the TestTrait trait.
    entityResult = deleteEntities(structContainerGuid);
    Assert.assertEquals(entityResult.getDeletedEntities().size(), 1);
    Assert.assertTrue(entityResult.getDeletedEntities().contains(structContainerGuid));
    assertEntityDeleted(structContainerGuid);
    // Verify all TestStruct struct vertices were removed.
    assertVerticesDeleted(getVertices(Constants.ENTITY_TYPE_PROPERTY_KEY, "TestStruct"));
    // Verify all NestedStruct struct vertices were removed.
    assertVerticesDeleted(getVertices(Constants.ENTITY_TYPE_PROPERTY_KEY, "NestedStruct"));
    // Verify all TestTrait trait vertices were removed.
    assertVerticesDeleted(getVertices(Constants.ENTITY_TYPE_PROPERTY_KEY, "TestTrait"));
}
Also used : ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) ITypedStruct(org.apache.atlas.typesystem.ITypedStruct) EntityResult(org.apache.atlas.model.legacy.EntityResult) ITypedStruct(org.apache.atlas.typesystem.ITypedStruct) IStruct(org.apache.atlas.typesystem.IStruct) Struct(org.apache.atlas.typesystem.Struct) TypesDef(org.apache.atlas.typesystem.TypesDef) Referenceable(org.apache.atlas.typesystem.Referenceable) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Id(org.apache.atlas.typesystem.persistence.Id) IStruct(org.apache.atlas.typesystem.IStruct) Test(org.testng.annotations.Test)

Aggregations

Struct (org.apache.atlas.typesystem.Struct)56 Test (org.testng.annotations.Test)36 IStruct (org.apache.atlas.typesystem.IStruct)29 Referenceable (org.apache.atlas.typesystem.Referenceable)25 ITypedStruct (org.apache.atlas.typesystem.ITypedStruct)17 TraitType (org.apache.atlas.typesystem.types.TraitType)12 ArrayList (java.util.ArrayList)9 LinkedList (java.util.LinkedList)9 HashMap (java.util.HashMap)8 Map (java.util.Map)8 Id (org.apache.atlas.typesystem.persistence.Id)7 JSONObject (org.codehaus.jettison.json.JSONObject)7 BeforeTest (org.testng.annotations.BeforeTest)6 AfterTest (org.testng.annotations.AfterTest)5 ImmutableList (com.google.common.collect.ImmutableList)4 List (java.util.List)4 ITypedInstance (org.apache.atlas.typesystem.ITypedInstance)4 ITypedReferenceableInstance (org.apache.atlas.typesystem.ITypedReferenceableInstance)4 TypeSystem (org.apache.atlas.typesystem.types.TypeSystem)4 Date (java.util.Date)3