http://git-wip-us.apache.org/repos/asf/atlas/blob/435fe3fb/typesystem/src/main/scala/org/apache/atlas/typesystem/json/TypesSerialization.scala ---------------------------------------------------------------------- diff --git a/typesystem/src/main/scala/org/apache/atlas/typesystem/json/TypesSerialization.scala b/typesystem/src/main/scala/org/apache/atlas/typesystem/json/TypesSerialization.scala deleted file mode 100755 index 4478a44..0000000 --- a/typesystem/src/main/scala/org/apache/atlas/typesystem/json/TypesSerialization.scala +++ /dev/null @@ -1,270 +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.typesystem.json - -import java.text.SimpleDateFormat -import com.google.common.collect.ImmutableList -import org.apache.atlas.AtlasException -import org.apache.atlas.AtlasConstants -import org.apache.atlas.typesystem.TypesDef -import org.apache.atlas.typesystem.types.DataTypes.{ArrayType, MapType, TypeCategory} -import org.apache.atlas.typesystem.types._ -import org.json4s.JsonAST.JString -import org.json4s._ -import org.json4s.native.Serialization._ -import com.google.common.collect.ImmutableSet - - -/** - * Module for serializing to/from Json. - * - * @example {{{ - * val j = TypesSerialization.toJson(typeSystem, "Employee", "Person", "Department", "SecurityClearance") - * - * val typesDef = TypesSerialization.fromJson(jsonStr) - * typesDef.enumTypes.foreach( typeSystem.defineEnumType(_)) - - typeSystem.defineTypes(ImmutableList.copyOf(typesDef.structTypes.toArray), - ImmutableList.copyOf(typesDef.traitTypes.toArray), - ImmutableList.copyOf(typesDef.classTypes.toArray) - ) - * }}} - * - * @todo doesn't traverse includes directives. Includes are parsed into - * [[org.apache.atlas.tools.thrift.IncludeDef IncludeDef]] structures - * but are not traversed. - * @todo mixing in [[scala.util.parsing.combinator.PackratParsers PackratParsers]] is a placeholder. Need to - * change specific grammar rules to `lazy val` and `Parser[Elem]` to `PackratParser[Elem]`. Will do based on - * performance analysis. - * @todo Error reporting - */ -object TypesSerialization { - - def toJsonValue(typ: IDataType[_])(implicit formats: Formats): JValue = { - typ.getTypeCategory match { - case TypeCategory.CLASS => { - Extraction.decompose(convertClassTypeToHierarchicalTypeDefinition(typ.asInstanceOf[ClassType])) - } - case TypeCategory.STRUCT => { - Extraction.decompose(convertStructTypeToStructDef(typ.asInstanceOf[StructType])) - } - case TypeCategory.TRAIT => { - Extraction.decompose(convertTraitTypeToHierarchicalTypeDefinition(typ.asInstanceOf[TraitType])) - } - case TypeCategory.ENUM => { - Extraction.decompose(convertEnumTypeToEnumTypeDef(typ.asInstanceOf[EnumType])) - } - case _ => JString(s"${typ.getName}") - } - } - - def toJson(ts: TypeSystem, typName: String): String = { - toJson(ts, List(typName): _*) - } - - def toJson(ts: TypeSystem, typNames: String*): String = { - toJson(ts, (typ: IDataType[_]) => typNames.contains(typ.getName)) - } - - import scala.collection.JavaConversions._ - - def toJson(ts: TypeSystem, typNames: java.util.List[String]): String = { - toJson(ts, typNames.toIndexedSeq: _*) - } - - val _formats = new DefaultFormats { - override val dateFormatter = TypeSystem.getInstance().getDateFormat.asInstanceOf[SimpleDateFormat] - override val typeHints = NoTypeHints - } - - def toJson(ts: TypeSystem, export: IDataType[_] => Boolean): String = { - implicit val formats = _formats + new MultiplicitySerializer - - val typsDef = convertToTypesDef(ts, export) - - writePretty(typsDef) - } - - def fromJson(jsonStr: String): TypesDef = { - implicit val formats = _formats + new MultiplicitySerializer - - read[TypesDef](jsonStr) - } - - def toJson(typesDef : TypesDef) : String = { - implicit val formats = _formats + new MultiplicitySerializer - writePretty(typesDef) - - } - - def toJson(enumTypeDefinition: EnumTypeDefinition) : String = { - toJson(new TypesDef(enumTypeDefinition)) - } - - def toJson(structTypeDefinition: StructTypeDefinition) : String = { - toJson(new TypesDef(structTypeDefinition)) - } - - def toJson(typDef: HierarchicalTypeDefinition[_], isTrait : Boolean) : String = { - toJson(new TypesDef(typDef, isTrait)) - } - - private def convertAttributeInfoToAttributeDef(aInfo: AttributeInfo) = { - new AttributeDefinition(aInfo.name, aInfo.dataType().getName, aInfo.multiplicity, - aInfo.isComposite, aInfo.isUnique, aInfo.isIndexable, aInfo.reverseAttributeName) - } - - private def convertEnumTypeToEnumTypeDef(et: EnumType) = { - val eVals: Seq[EnumValue] = et.valueMap.values().toSeq - new EnumTypeDefinition(et.name, et.description, et.version, eVals: _*) - } - - private def convertStructTypeToStructDef(st: StructType): StructTypeDefinition = { - - val aDefs: Iterable[AttributeDefinition] = - st.fieldMapping.fields.values().map(convertAttributeInfoToAttributeDef(_)) - new StructTypeDefinition(st.name, st.description, st.version, aDefs.toArray) - } - - private def convertTraitTypeToHierarchicalTypeDefinition(tt: TraitType): HierarchicalTypeDefinition[TraitType] = { - - val aDefs: Iterable[AttributeDefinition] = - tt.immediateAttrs.map(convertAttributeInfoToAttributeDef(_)) - new HierarchicalTypeDefinition[TraitType](classOf[TraitType], tt.name, tt.description, tt.version, tt.superTypes, aDefs.toArray) - } - - private def convertClassTypeToHierarchicalTypeDefinition(tt: ClassType): HierarchicalTypeDefinition[ClassType] = { - - val aDefs: Iterable[AttributeDefinition] = - tt.immediateAttrs.map(convertAttributeInfoToAttributeDef(_)) - new HierarchicalTypeDefinition[ClassType](classOf[ClassType], tt.name, tt.description, tt.version, tt.superTypes, aDefs.toArray) - } - - def convertToTypesDef(ts: TypeSystem, export: IDataType[_] => Boolean): TypesDef = { - - var enumTypes: Seq[EnumTypeDefinition] = Nil - var structTypes: Seq[StructTypeDefinition] = Nil - var traitTypes: Seq[HierarchicalTypeDefinition[TraitType]] = Nil - var classTypes: Seq[HierarchicalTypeDefinition[ClassType]] = Nil - - def toTyp(nm: String) = ts.getDataType(classOf[IDataType[_]], nm) - - val typs: Iterable[IDataType[_]] = ts.getTypeNames.map(toTyp(_)).filter { (typ: IDataType[_]) => - !(ts.getCoreTypes.contains(typ.getName)) && export(typ) - } - - typs.foreach { - case typ: ArrayType => () - case typ: MapType => () - case typ: EnumType => enumTypes = enumTypes :+ convertEnumTypeToEnumTypeDef(typ) - case typ: StructType => structTypes = structTypes :+ convertStructTypeToStructDef(typ) - case typ: TraitType => traitTypes = traitTypes :+ convertTraitTypeToHierarchicalTypeDefinition(typ) - case typ: ClassType => classTypes = classTypes :+ convertClassTypeToHierarchicalTypeDefinition(typ) - } - - TypesDef(enumTypes, structTypes, traitTypes, classTypes) - } - -} - -class MultiplicitySerializer extends CustomSerializer[Multiplicity](format => ( { - case JString(m) => m match { - case "optional" => Multiplicity.OPTIONAL - case "required" => Multiplicity.REQUIRED - case "collection" => Multiplicity.COLLECTION - case "set" => Multiplicity.SET - } -}, { - case m: Multiplicity => JString(m match { - case Multiplicity.OPTIONAL => "optional" - case Multiplicity.REQUIRED => "required" - case Multiplicity.COLLECTION => "collection" - case Multiplicity.SET => "set" - } - - ) -} - )) - -trait TypeHelpers { - def requiredAttr(name: String, dataType: IDataType[_]) = - new AttributeDefinition(name, dataType.getName, Multiplicity.REQUIRED, false, null) - - def optionalAttr(name: String, dataTypeName: String) = - new AttributeDefinition(name, dataTypeName, Multiplicity.OPTIONAL, false, null) - - - def optionalAttr(name: String, dataType: IDataType[_]) = - new AttributeDefinition(name, dataType.getName, Multiplicity.OPTIONAL, false, null) - - def structDef(name: String, attrs: AttributeDefinition*): - StructTypeDefinition = { - structDef(name, None, attrs:_*) - } - - def structDef(name: String, description: Option[String], attrs: AttributeDefinition*) = { - new StructTypeDefinition(name, description.getOrElse(null), attrs.toArray) - } - - def defineTraits(ts: TypeSystem, tDefs: HierarchicalTypeDefinition[TraitType]*) = { - ts.defineTraitTypes(tDefs: _*) - } - - def createTraitTypeDef(name: String, superTypes: Seq[String], attrDefs: AttributeDefinition*): - HierarchicalTypeDefinition[TraitType] = { - createTraitTypeDef(name, None, superTypes, attrDefs:_*) - } - - def createTraitTypeDef(name: String, description: Option[String], superTypes: Seq[String], attrDefs: AttributeDefinition*): - HierarchicalTypeDefinition[TraitType] = { - createTraitTypeDef(name, None, AtlasConstants.DEFAULT_TYPE_VERSION, superTypes, attrDefs:_*) - } - - def createTraitTypeDef(name: String, description: Option[String], version: String,superTypes: Seq[String], attrDefs: AttributeDefinition*): - HierarchicalTypeDefinition[TraitType] = { - val sts = ImmutableSet.copyOf(superTypes.toArray) - return new HierarchicalTypeDefinition[TraitType](classOf[TraitType], name, description.getOrElse(null), - sts, attrDefs.toArray) - } - - def createClassTypeDef(name: String, superTypes: Seq[String], attrDefs: AttributeDefinition*): - HierarchicalTypeDefinition[ClassType] = { - createClassTypeDef( name, None, superTypes, attrDefs:_*) - } - - def createClassTypeDef(name: String, description: Option[String], superTypes: Seq[String], attrDefs: AttributeDefinition*): - HierarchicalTypeDefinition[ClassType] = { - createClassTypeDef( name, None, None, superTypes, attrDefs:_*) - } - - def createClassTypeDef(name: String, description: Option[String], version: Option[String], superTypes: Seq[String], attrDefs: AttributeDefinition*): - HierarchicalTypeDefinition[ClassType] = { - val sts = ImmutableSet.copyOf(superTypes.toArray) - return new HierarchicalTypeDefinition[ClassType](classOf[ClassType], name, description.getOrElse(null), AtlasConstants.DEFAULT_TYPE_VERSION, sts, attrDefs.toArray) - } - - - @throws(classOf[AtlasException]) - def defineClassType(ts: TypeSystem, classDef: HierarchicalTypeDefinition[ClassType]): ClassType = { - ts.defineTypes(ImmutableList.of[EnumTypeDefinition], ImmutableList.of[StructTypeDefinition], - ImmutableList.of[HierarchicalTypeDefinition[TraitType]], - ImmutableList.of[HierarchicalTypeDefinition[ClassType]](classDef)) - return ts.getDataType(classOf[ClassType], classDef.typeName) - } -}
http://git-wip-us.apache.org/repos/asf/atlas/blob/435fe3fb/typesystem/src/test/java/org/apache/atlas/ApplicationPropertiesTest.java ---------------------------------------------------------------------- diff --git a/typesystem/src/test/java/org/apache/atlas/ApplicationPropertiesTest.java b/typesystem/src/test/java/org/apache/atlas/ApplicationPropertiesTest.java deleted file mode 100644 index 25a19cf..0000000 --- a/typesystem/src/test/java/org/apache/atlas/ApplicationPropertiesTest.java +++ /dev/null @@ -1,83 +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; - -import org.apache.atlas.typesystem.types.TypeSystem; -import org.apache.commons.configuration.Configuration; -import org.testng.Assert; -import org.testng.annotations.Test; - -import static org.testng.Assert.assertEquals; - -public class ApplicationPropertiesTest { - - @Test - public void testVariables() throws Exception { - Configuration properties = ApplicationProperties.get(ApplicationProperties.APPLICATION_PROPERTIES); - - //plain property without variables - assertEquals(properties.getString("atlas.service"), "atlas"); - - //property containing system property - String data = System.getProperty("user.dir") + "/target/data"; - assertEquals(properties.getString("atlas.data"), data); - - //property referencing other property - assertEquals(properties.getString("atlas.graph.data"), data + "/graph"); - - //invalid system property - not substituted - assertEquals(properties.getString("atlas.db"), "${atlasdb}"); - } - - @Test - //variable substitutions should work with subset configuration as well - public void testSubset() throws Exception { - Configuration configuration = ApplicationProperties.get(ApplicationProperties.APPLICATION_PROPERTIES); - Configuration subConfiguration = configuration.subset("atlas"); - - assertEquals(subConfiguration.getString("service"), "atlas"); - String data = System.getProperty("user.dir") + "/target/data"; - assertEquals(subConfiguration.getString("data"), data); - assertEquals(subConfiguration.getString("graph.data"), data + "/graph"); - } - - @Test - public void testGetClass() throws Exception { - Configuration configuration = ApplicationProperties.get(); - - //read from atlas-application.properties - Class cls = ApplicationProperties.getClass(configuration, "atlas.TypeSystem.impl", - ApplicationProperties.class.getName(), TypeSystem.class); - assertEquals(cls.getName(), TypeSystem.class.getName()); - - //default value - cls = ApplicationProperties.getClass(configuration, "atlas.TypeSystem2.impl", - TypeSystem.class.getName(), TypeSystem.class); - assertEquals(cls.getName(), TypeSystem.class.getName()); - - //incompatible assignTo class, should throw AtlasException - try { - cls = ApplicationProperties.getClass(configuration, "atlas.TypeSystem.impl", - ApplicationProperties.class.getName(), ApplicationProperties.class); - Assert.fail(AtlasException.class.getSimpleName() + " was expected but none thrown."); - } - catch (AtlasException e) { - // good - } - } -} http://git-wip-us.apache.org/repos/asf/atlas/blob/435fe3fb/typesystem/src/test/java/org/apache/atlas/typesystem/json/SerializationJavaTest.java ---------------------------------------------------------------------- diff --git a/typesystem/src/test/java/org/apache/atlas/typesystem/json/SerializationJavaTest.java b/typesystem/src/test/java/org/apache/atlas/typesystem/json/SerializationJavaTest.java deleted file mode 100755 index 5ee019c..0000000 --- a/typesystem/src/test/java/org/apache/atlas/typesystem/json/SerializationJavaTest.java +++ /dev/null @@ -1,166 +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.typesystem.json; - -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableSet; - -import org.apache.atlas.AtlasException; -import org.apache.atlas.typesystem.ITypedInstance; -import org.apache.atlas.typesystem.ITypedReferenceableInstance; -import org.apache.atlas.typesystem.Referenceable; -import org.apache.atlas.typesystem.Struct; -import org.apache.atlas.typesystem.types.AttributeDefinition; -import org.apache.atlas.typesystem.types.BaseTest; -import org.apache.atlas.typesystem.types.ClassType; -import org.apache.atlas.typesystem.types.DataTypes; -import org.apache.atlas.typesystem.types.EnumTypeDefinition; -import org.apache.atlas.typesystem.types.HierarchicalTypeDefinition; -import org.apache.atlas.typesystem.types.Multiplicity; -import org.apache.atlas.typesystem.types.StructTypeDefinition; -import org.apache.atlas.typesystem.types.TraitType; -import org.apache.atlas.typesystem.types.TypeSystem; -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.Test; -import org.testng.Assert; - -import static org.apache.atlas.typesystem.types.utils.TypesUtil.createClassTypeDef; -import static org.apache.atlas.typesystem.types.utils.TypesUtil.createRequiredAttrDef; -import static org.apache.atlas.typesystem.types.utils.TypesUtil.createTraitTypeDef; - -public class SerializationJavaTest extends BaseTest { - - - @BeforeMethod - public void setup() throws Exception { - super.setup(); - } - - /* - * Class Hierarchy is: - * Department(name : String, employees : Array[Person]) - * Person(name : String, department : Department, manager : Manager) - * Manager(subordinates : Array[Person]) extends Person - * - * Persons can have SecurityClearance(level : Int) clearance. - */ - @Test - public void test1() throws AtlasException { - - TypeSystem ts = getTypeSystem(); - - HierarchicalTypeDefinition<ClassType> deptTypeDef = createClassTypeDef("Department", ImmutableSet.<String>of(), - createRequiredAttrDef("name", DataTypes.STRING_TYPE), - new AttributeDefinition("employees", String.format("array<%s>", "Person"), Multiplicity.COLLECTION, - true, "department")); - HierarchicalTypeDefinition<ClassType> personTypeDef = createClassTypeDef("Person", ImmutableSet.<String>of(), - createRequiredAttrDef("name", DataTypes.STRING_TYPE), - new AttributeDefinition("department", "Department", Multiplicity.REQUIRED, false, "employees"), - new AttributeDefinition("manager", "Manager", Multiplicity.OPTIONAL, false, "subordinates")); - HierarchicalTypeDefinition<ClassType> managerTypeDef = - createClassTypeDef("Manager", ImmutableSet.of("Person"), - new AttributeDefinition("subordinates", String.format("array<%s>", "Person"), - Multiplicity.COLLECTION, false, "manager")); - - HierarchicalTypeDefinition<TraitType> securityClearanceTypeDef = - createTraitTypeDef("SecurityClearance", ImmutableSet.<String>of(), - createRequiredAttrDef("level", DataTypes.INT_TYPE)); - - ts.defineTypes(ImmutableList.<EnumTypeDefinition>of(), ImmutableList.<StructTypeDefinition>of(), - ImmutableList.of(securityClearanceTypeDef), - ImmutableList.of(deptTypeDef, personTypeDef, managerTypeDef)); - - Referenceable hrDept = new Referenceable("Department"); - Referenceable john = new Referenceable("Person"); - Referenceable jane = new Referenceable("Manager", "SecurityClearance"); - - hrDept.set("name", "hr"); - john.set("name", "John"); - john.set("department", hrDept); - jane.set("name", "Jane"); - jane.set("department", hrDept); - - john.set("manager", jane); - - hrDept.set("employees", ImmutableList.of(john, jane)); - - jane.set("subordinates", ImmutableList.of(john)); - - jane.getTrait("SecurityClearance").set("level", 1); - - ClassType deptType = ts.getDataType(ClassType.class, "Department"); - ITypedReferenceableInstance hrDept2 = deptType.convert(hrDept, Multiplicity.REQUIRED); - - String hrDeptStr = hrDept2.toString(); - - Assert.assertEquals(hrDeptStr, "{\n" + - "\tid : (type: Department, id: <unassigned>)\n" + - "\tname : \thr\n" + - "\temployees : \t[{\n" + - "\tid : (type: Person, id: <unassigned>)\n" + - "\tname : \tJohn\n" + - "\tdepartment : (type: Department, id: <unassigned>)\n" + - "\tmanager : (type: Manager, id: <unassigned>)\n" + - "}, {\n" + - "\tid : (type: Manager, id: <unassigned>)\n" + - "\tsubordinates : \t[{\n" + - "\tid : (type: Person, id: <unassigned>)\n" + - "\tname : \tJohn\n" + - "\tdepartment : (type: Department, id: <unassigned>)\n" + - "\tmanager : (type: Manager, id: <unassigned>)\n" + - "}]\n" + - "\tname : \tJane\n" + - "\tdepartment : (type: Department, id: <unassigned>)\n" + - "\tmanager : <null>\n" + - "\n" + - "\tSecurityClearance : \t{\n" + - "\t\tlevel : \t\t1\n" + - "\t}}]\n" + - "}"); - - String jsonStr = Serialization$.MODULE$.toJson(hrDept2); - //System.out.println(jsonStr); - - hrDept2 = Serialization$.MODULE$.fromJson(jsonStr); - Assert.assertEquals(hrDept2.toString(), hrDeptStr); - - } - - @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()); - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/atlas/blob/435fe3fb/typesystem/src/test/java/org/apache/atlas/typesystem/types/BaseTest.java ---------------------------------------------------------------------- diff --git a/typesystem/src/test/java/org/apache/atlas/typesystem/types/BaseTest.java b/typesystem/src/test/java/org/apache/atlas/typesystem/types/BaseTest.java deleted file mode 100755 index 95c99e4..0000000 --- a/typesystem/src/test/java/org/apache/atlas/typesystem/types/BaseTest.java +++ /dev/null @@ -1,190 +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.typesystem.types; - -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableSet; -import com.google.common.collect.Lists; -import com.google.common.collect.Maps; - -import org.apache.atlas.AtlasException; -import org.apache.atlas.typesystem.Referenceable; -import org.apache.atlas.typesystem.Struct; -import org.apache.atlas.typesystem.types.utils.TypesUtil; -import org.apache.commons.lang.RandomStringUtils; -import org.testng.annotations.BeforeMethod; - -import java.math.BigDecimal; -import java.math.BigInteger; -import java.util.Date; -import java.util.Map; - -public abstract class BaseTest { - - public static final String STRUCT_TYPE_1 = "t1"; - public static final String STRUCT_TYPE_2 = "t2"; - public static final String STRUCT_TYPE_3 = "t3"; - public static final String TEST_DATE = "2014-12-11T02:35:58.440Z"; - - public static Struct createStruct() throws AtlasException { - StructType structType = TypeSystem.getInstance().getDataType(StructType.class, STRUCT_TYPE_1); - Struct s = new Struct(structType.getName()); - s.set("a", 1); - s.set("b", true); - s.set("c", (byte) 1); - s.set("d", (short) 2); - s.set("e", 1); - s.set("f", 1); - s.set("g", 1L); - s.set("h", 1.0f); - s.set("i", 1.0); - s.set("j", BigInteger.valueOf(1L)); - s.set("k", new BigDecimal(1)); - s.set("l", new Date(1418265358440L)); - s.set("m", Lists.asList(1, new Integer[]{1})); - s.set("n", Lists.asList(BigDecimal.valueOf(1.1), new BigDecimal[]{BigDecimal.valueOf(1.1)})); - Map<String, Double> hm = Maps.newHashMap(); - hm.put("a", 1.0); - hm.put("b", 2.0); - s.set("o", hm); - s.set("p", ""); - s.setNull("q"); - Map<String, String> hm2 = Maps.newHashMap(); - hm2.put("a", ""); - s.set("r", hm2); - return s; - } - - protected final TypeSystem getTypeSystem() { - return TypeSystem.getInstance(); - } - - @BeforeMethod - public void setup() throws Exception { - TypeSystem ts = TypeSystem.getInstance(); - ts.reset(); - - StructType structType = - ts.defineStructType(STRUCT_TYPE_1, true, TypesUtil.createRequiredAttrDef("a", DataTypes.INT_TYPE), - TypesUtil.createOptionalAttrDef("b", DataTypes.BOOLEAN_TYPE), - TypesUtil.createOptionalAttrDef("c", DataTypes.BYTE_TYPE), - TypesUtil.createOptionalAttrDef("d", DataTypes.SHORT_TYPE), - TypesUtil.createOptionalAttrDef("e", DataTypes.INT_TYPE), - TypesUtil.createOptionalAttrDef("f", DataTypes.INT_TYPE), - TypesUtil.createOptionalAttrDef("g", DataTypes.LONG_TYPE), - TypesUtil.createOptionalAttrDef("h", DataTypes.FLOAT_TYPE), - TypesUtil.createOptionalAttrDef("i", DataTypes.DOUBLE_TYPE), - TypesUtil.createOptionalAttrDef("j", DataTypes.BIGINTEGER_TYPE), - TypesUtil.createOptionalAttrDef("k", DataTypes.BIGDECIMAL_TYPE), - TypesUtil.createOptionalAttrDef("l", DataTypes.DATE_TYPE), - TypesUtil.createOptionalAttrDef("m", ts.defineArrayType(DataTypes.INT_TYPE)), - TypesUtil.createOptionalAttrDef("n", ts.defineArrayType(DataTypes.BIGDECIMAL_TYPE)), - TypesUtil.createOptionalAttrDef("o", ts.defineMapType(DataTypes.STRING_TYPE, DataTypes.DOUBLE_TYPE)), - TypesUtil.createOptionalAttrDef("p", DataTypes.STRING_TYPE), - TypesUtil.createOptionalAttrDef("q", DataTypes.STRING_TYPE), - TypesUtil.createOptionalAttrDef("r", ts.defineMapType(DataTypes.STRING_TYPE, DataTypes.STRING_TYPE))); - System.out.println("defined structType = " + structType); - - StructType recursiveStructType = - ts.defineStructType(STRUCT_TYPE_2, true, TypesUtil.createRequiredAttrDef("a", DataTypes.INT_TYPE), - TypesUtil.createOptionalAttrDef("s", STRUCT_TYPE_2)); - System.out.println("defined recursiveStructType = " + recursiveStructType); - - StructType invalidStructType = - ts.defineStructType(STRUCT_TYPE_3, true, TypesUtil.createRequiredAttrDef("a",DataTypes.STRING_TYPE)); - System.out.println("defined structType = " + invalidStructType); - } - - protected Map<String, IDataType> defineTraits(HierarchicalTypeDefinition<TraitType>... tDefs) - throws AtlasException { - return getTypeSystem().defineTraitTypes(tDefs); - } - - protected Map<String, IDataType> defineClasses(HierarchicalTypeDefinition<ClassType>... classDefs) - throws AtlasException { - return getTypeSystem().defineClassTypes(classDefs); - } - - /* - * Class Hierarchy is: - * Department(name : String, employees : Array[Person]) - * Person(name : String, department : Department, manager : Manager) - * Manager(subordinates : Array[Person]) extends Person - * - * Persons can have SecurityClearance(level : Int) clearance. - */ - protected void defineDeptEmployeeTypes(TypeSystem ts) throws AtlasException { - - HierarchicalTypeDefinition<ClassType> deptTypeDef = TypesUtil - .createClassTypeDef("Department", ImmutableSet.<String>of(), - TypesUtil.createRequiredAttrDef("name", DataTypes.STRING_TYPE), - new AttributeDefinition("employees", String.format("array<%s>", "Person"), - Multiplicity.COLLECTION, true, "department")); - HierarchicalTypeDefinition<ClassType> personTypeDef = TypesUtil - .createClassTypeDef("Person", ImmutableSet.<String>of(), - TypesUtil.createRequiredAttrDef("name", DataTypes.STRING_TYPE), - new AttributeDefinition("department", "Department", Multiplicity.REQUIRED, false, "employees"), - new AttributeDefinition("manager", "Manager", Multiplicity.OPTIONAL, false, "subordinates")); - HierarchicalTypeDefinition<ClassType> managerTypeDef = TypesUtil - .createClassTypeDef("Manager", ImmutableSet.of("Person"), - new AttributeDefinition("subordinates", String.format("array<%s>", "Person"), - Multiplicity.COLLECTION, false, "manager")); - - HierarchicalTypeDefinition<TraitType> securityClearanceTypeDef = TypesUtil - .createTraitTypeDef("SecurityClearance", ImmutableSet.<String>of(), - TypesUtil.createRequiredAttrDef("level", DataTypes.INT_TYPE)); - - ts.defineTypes(ImmutableList.<EnumTypeDefinition>of(), ImmutableList.<StructTypeDefinition>of(), - ImmutableList.of(securityClearanceTypeDef), - ImmutableList.of(deptTypeDef, personTypeDef, managerTypeDef)); - - ImmutableList.of(ts.getDataType(HierarchicalType.class, "SecurityClearance"), - ts.getDataType(ClassType.class, "Department"), ts.getDataType(ClassType.class, "Person"), - ts.getDataType(ClassType.class, "Manager")); - } - - protected Referenceable createDeptEg1(TypeSystem ts) throws AtlasException { - Referenceable hrDept = new Referenceable("Department"); - Referenceable john = new Referenceable("Person"); - Referenceable jane = new Referenceable("Manager", "SecurityClearance"); - - hrDept.set("name", "hr"); - john.set("name", "John"); - john.set("department", hrDept); - jane.set("name", "Jane"); - jane.set("department", hrDept); - - john.set("manager", jane); - - hrDept.set("employees", ImmutableList.of(john, jane)); - - jane.set("subordinates", ImmutableList.of(john)); - - jane.getTrait("SecurityClearance").set("level", 1); - - ClassType deptType = ts.getDataType(ClassType.class, "Department"); - deptType.convert(hrDept, Multiplicity.REQUIRED); - - return hrDept; - } - - protected String newName() { - return RandomStringUtils.randomAlphanumeric(10); - } -} http://git-wip-us.apache.org/repos/asf/atlas/blob/435fe3fb/typesystem/src/test/java/org/apache/atlas/typesystem/types/ClassTest.java ---------------------------------------------------------------------- diff --git a/typesystem/src/test/java/org/apache/atlas/typesystem/types/ClassTest.java b/typesystem/src/test/java/org/apache/atlas/typesystem/types/ClassTest.java deleted file mode 100755 index daecdd7..0000000 --- a/typesystem/src/test/java/org/apache/atlas/typesystem/types/ClassTest.java +++ /dev/null @@ -1,124 +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.typesystem.types; - -import org.apache.atlas.AtlasException; -import org.apache.atlas.typesystem.ITypedReferenceableInstance; -import org.apache.atlas.typesystem.Referenceable; -import org.apache.atlas.typesystem.TypesDef; -import org.apache.atlas.typesystem.json.TypesSerialization; -import org.apache.atlas.typesystem.types.utils.TypesUtil; -import org.testng.Assert; -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.Test; - -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableSet; - -public class ClassTest extends HierarchicalTypeTest<ClassType> { - - @BeforeMethod - public void setup() throws Exception { - super.setup(); - } - - @Test - public void test1() throws AtlasException { - - TypeSystem ts = getTypeSystem(); - - defineDeptEmployeeTypes(ts); - Referenceable hrDept = createDeptEg1(ts); - ClassType deptType = ts.getDataType(ClassType.class, "Department"); - ITypedReferenceableInstance hrDept2 = deptType.convert(hrDept, Multiplicity.REQUIRED); - - - Assert.assertEquals(hrDept2.toString(), "{\n" + - "\tid : (type: Department, id: <unassigned>)\n" + - "\tname : \thr\n" + - "\temployees : \t[{\n" + - "\tid : (type: Person, id: <unassigned>)\n" + - "\tname : \tJohn\n" + - "\tdepartment : (type: Department, id: <unassigned>)\n" + - "\tmanager : (type: Manager, id: <unassigned>)\n" + - "}, {\n" + - "\tid : (type: Manager, id: <unassigned>)\n" + - "\tsubordinates : \t[{\n" + - "\tid : (type: Person, id: <unassigned>)\n" + - "\tname : \tJohn\n" + - "\tdepartment : (type: Department, id: <unassigned>)\n" + - "\tmanager : (type: Manager, id: <unassigned>)\n" + - "}]\n" + - "\tname : \tJane\n" + - "\tdepartment : (type: Department, id: <unassigned>)\n" + - "\tmanager : <null>\n" + - "\n" + - "\tSecurityClearance : \t{\n" + - "\t\tlevel : \t\t1\n" + - "\t}}]\n" + - "}"); - } - - - @Test - public void testSerDeWithoutDescription() throws Exception { - HierarchicalTypeDefinition<ClassType> clsType = TypesUtil - .createClassTypeDef("Random", ImmutableSet.<String>of(), - TypesUtil.createRequiredAttrDef("name", DataTypes.STRING_TYPE)); - - TypesDef typesDef = getTypesDef(clsType); - String json = TypesSerialization.toJson(typesDef); - System.out.println("json " + json); - TypesSerialization.fromJson(json); - } - - @Test - public void testSerDeWithDescription() throws Exception { - HierarchicalTypeDefinition<ClassType> clsType = TypesUtil - .createClassTypeDef("Random", "Random-description", ImmutableSet.<String>of(), - TypesUtil.createRequiredAttrDef("name", DataTypes.STRING_TYPE)); - TypesDef typesDef = getTypesDef(clsType); - String json = TypesSerialization.toJson(typesDef); - System.out.println("json " + json); - TypesSerialization.fromJson(json); - } - @Override - protected HierarchicalTypeDefinition<ClassType> getTypeDefinition(String name, AttributeDefinition... attributes) { - return new HierarchicalTypeDefinition(ClassType.class, name, null, null, attributes); - } - - @Override - protected HierarchicalTypeDefinition<ClassType> getTypeDefinition(String name, ImmutableSet<String> superTypes, - AttributeDefinition... attributes) { - return new HierarchicalTypeDefinition(ClassType.class, name, null, superTypes, attributes); - } - - @Override - protected TypesDef getTypesDef(StructTypeDefinition typeDefinition) { - return TypesUtil.getTypesDef(ImmutableList.<EnumTypeDefinition>of(), ImmutableList.<StructTypeDefinition>of(), - ImmutableList.<HierarchicalTypeDefinition<TraitType>>of(), - ImmutableList.of((HierarchicalTypeDefinition<ClassType>) typeDefinition)); - } - - @Override - protected TypesDef getTypesDef(HierarchicalTypeDefinition<ClassType>... typeDefinitions) { - return TypesUtil.getTypesDef(ImmutableList.<EnumTypeDefinition>of(), ImmutableList.<StructTypeDefinition>of(), - ImmutableList.<HierarchicalTypeDefinition<TraitType>>of(), ImmutableList.copyOf(typeDefinitions)); - } -} http://git-wip-us.apache.org/repos/asf/atlas/blob/435fe3fb/typesystem/src/test/java/org/apache/atlas/typesystem/types/EnumTest.java ---------------------------------------------------------------------- diff --git a/typesystem/src/test/java/org/apache/atlas/typesystem/types/EnumTest.java b/typesystem/src/test/java/org/apache/atlas/typesystem/types/EnumTest.java deleted file mode 100755 index 2307192..0000000 --- a/typesystem/src/test/java/org/apache/atlas/typesystem/types/EnumTest.java +++ /dev/null @@ -1,245 +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.typesystem.types; - -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableSet; -import com.google.common.collect.Lists; -import com.google.common.collect.Maps; - -import org.apache.atlas.AtlasException; -import org.apache.atlas.typesystem.IReferenceableInstance; -import org.apache.atlas.typesystem.ITypedReferenceableInstance; -import org.apache.atlas.typesystem.ITypedStruct; -import org.apache.atlas.typesystem.Referenceable; -import org.apache.atlas.typesystem.Struct; -import org.apache.atlas.typesystem.TypesDef; -import org.apache.atlas.typesystem.types.utils.TypesUtil; -import org.testng.Assert; -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.Test; - -import java.math.BigDecimal; -import java.math.BigInteger; -import java.util.Date; -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; - -public class EnumTest extends BaseTest { - - @BeforeMethod - public void setup() throws Exception { - super.setup(); - } - - void defineEnums(TypeSystem ts) throws AtlasException { - ts.defineEnumType("HiveObjectType", new EnumValue("GLOBAL", 1), new EnumValue("DATABASE", 2), - new EnumValue("TABLE", 3), new EnumValue("PARTITION", 4), new EnumValue("COLUMN", 5)); - - ts.defineEnumType("PrincipalType", new EnumValue("USER", 1), new EnumValue("ROLE", 2), - new EnumValue("GROUP", 3)); - - ts.defineEnumType("TxnState", new EnumValue("COMMITTED", 1), new EnumValue("ABORTED", 2), - new EnumValue("OPEN", 3)); - - ts.defineEnumType("LockLevel", new EnumValue("DB", 1), new EnumValue("TABLE", 2), - new EnumValue("PARTITION", 3)); - } - - @Test - public void testTypeUpdate() throws Exception { - TypeSystem ts = getTypeSystem(); - EnumTypeDefinition etd = new EnumTypeDefinition(newName(), new EnumValue("A", 1)); - TypesDef typesDef = getTypesDef(etd); - ts.defineTypes(typesDef); - - //Allow adding new enum - etd = new EnumTypeDefinition(etd.name, new EnumValue("A", 1), new EnumValue("B", 2)); - typesDef = getTypesDef(etd); - ts.updateTypes(typesDef); - - //Don't allow deleting enum - etd = new EnumTypeDefinition(etd.name, new EnumValue("A", 1)); - typesDef = getTypesDef(etd); - try { - ts.updateTypes(typesDef); - Assert.fail("Expected TypeUpdateException"); - } catch (TypeUpdateException e) { - //assert that type is not updated when validation fails - EnumType enumType = ts.getDataType(EnumType.class, etd.name); - Assert.assertEquals(enumType.values().size(), 2); - } - - //Don't allow changing ordinal of existing enum value - etd = new EnumTypeDefinition(etd.name, new EnumValue("A", 2)); - typesDef = getTypesDef(etd); - try { - ts.updateTypes(typesDef); - Assert.fail("Expected TypeUpdateException"); - } catch (TypeUpdateException e) { - //expected - } - } - - private TypesDef getTypesDef(EnumTypeDefinition enumTypeDefinition) { - return TypesUtil.getTypesDef(ImmutableList.of(enumTypeDefinition), ImmutableList.<StructTypeDefinition>of(), - ImmutableList.<HierarchicalTypeDefinition<TraitType>>of(), - ImmutableList.<HierarchicalTypeDefinition<ClassType>>of()); - } - - protected void fillStruct(Struct s) throws AtlasException { - s.set("a", 1); - s.set("b", true); - s.set("c", (byte) 1); - s.set("d", (short) 2); - s.set("e", 1); - s.set("f", 1); - s.set("g", 1L); - s.set("h", 1.0f); - s.set("i", 1.0); - s.set("j", BigInteger.valueOf(1L)); - s.set("k", new BigDecimal(1)); - s.set("l", new Date(1418265358440L)); - s.set("m", Lists.asList(1, new Integer[]{1})); - s.set("n", Lists.asList(BigDecimal.valueOf(1.1), new BigDecimal[]{BigDecimal.valueOf(1.1)})); - Map<String, Double> hm = Maps.newHashMap(); - hm.put("a", 1.0); - hm.put("b", 2.0); - s.set("o", hm); - s.set("enum1", "GLOBAL"); - s.set("enum2", 1); - s.set("enum3", "COMMITTED"); - s.set("enum4", 3); - } - - protected Struct createStructWithEnum(String typeName) throws AtlasException { - Struct s = new Struct(typeName); - fillStruct(s); - return s; - } - - protected Referenceable createInstanceWithEnum(String typeName) throws AtlasException { - Referenceable r = new Referenceable(typeName); - fillStruct(r); - return r; - } - - protected ClassType defineClassTypeWithEnum(TypeSystem ts) throws AtlasException { - return ts.defineClassType( - createClassTypeDef("t4", ImmutableSet.<String>of(), createRequiredAttrDef("a", DataTypes.INT_TYPE), - createOptionalAttrDef("b", DataTypes.BOOLEAN_TYPE), - createOptionalAttrDef("c", DataTypes.BYTE_TYPE), - createOptionalAttrDef("d", DataTypes.SHORT_TYPE), - createOptionalAttrDef("enum1", ts.getDataType(EnumType.class, "HiveObjectType")), - createOptionalAttrDef("e", DataTypes.INT_TYPE), createOptionalAttrDef("f", DataTypes.INT_TYPE), - createOptionalAttrDef("g", DataTypes.LONG_TYPE), - createOptionalAttrDef("enum2", ts.getDataType(EnumType.class, "PrincipalType")), - createOptionalAttrDef("h", DataTypes.FLOAT_TYPE), - createOptionalAttrDef("i", DataTypes.DOUBLE_TYPE), - createOptionalAttrDef("j", DataTypes.BIGINTEGER_TYPE), - createOptionalAttrDef("k", DataTypes.BIGDECIMAL_TYPE), - createOptionalAttrDef("enum3", ts.getDataType(EnumType.class, "TxnState")), - createOptionalAttrDef("l", DataTypes.DATE_TYPE), - createOptionalAttrDef("m", ts.defineArrayType(DataTypes.INT_TYPE)), - createOptionalAttrDef("n", ts.defineArrayType(DataTypes.BIGDECIMAL_TYPE)), - createOptionalAttrDef("o", ts.defineMapType(DataTypes.STRING_TYPE, DataTypes.DOUBLE_TYPE)), - createOptionalAttrDef("enum4", ts.getDataType(EnumType.class, "LockLevel")))); - } - - @Test - public void testStruct() throws AtlasException { - TypeSystem ts = getTypeSystem(); - defineEnums(ts); - StructType structType = ts.defineStructType("ts", true, createRequiredAttrDef("a", DataTypes.INT_TYPE), - createOptionalAttrDef("b", DataTypes.BOOLEAN_TYPE), createOptionalAttrDef("c", DataTypes.BYTE_TYPE), - createOptionalAttrDef("d", DataTypes.SHORT_TYPE), - createOptionalAttrDef("enum1", ts.getDataType(EnumType.class, "HiveObjectType")), - createOptionalAttrDef("e", DataTypes.INT_TYPE), createOptionalAttrDef("f", DataTypes.INT_TYPE), - createOptionalAttrDef("g", DataTypes.LONG_TYPE), - createOptionalAttrDef("enum2", ts.getDataType(EnumType.class, "PrincipalType")), - createOptionalAttrDef("h", DataTypes.FLOAT_TYPE), createOptionalAttrDef("i", DataTypes.DOUBLE_TYPE), - createOptionalAttrDef("j", DataTypes.BIGINTEGER_TYPE), - createOptionalAttrDef("k", DataTypes.BIGDECIMAL_TYPE), - createOptionalAttrDef("enum3", ts.getDataType(EnumType.class, "TxnState")), - createOptionalAttrDef("l", DataTypes.DATE_TYPE), - createOptionalAttrDef("m", ts.defineArrayType(DataTypes.INT_TYPE)), - createOptionalAttrDef("n", ts.defineArrayType(DataTypes.BIGDECIMAL_TYPE)), - createOptionalAttrDef("o", ts.defineMapType(DataTypes.STRING_TYPE, DataTypes.DOUBLE_TYPE)), - createOptionalAttrDef("enum4", ts.getDataType(EnumType.class, "LockLevel"))); - - Struct s = createStructWithEnum("ts"); - ITypedStruct typedS = structType.convert(s, Multiplicity.REQUIRED); - Assert.assertEquals(typedS.toString(), "{\n" + - "\ta : \t1\n" + - "\tb : \ttrue\n" + - "\tc : \t1\n" + - "\td : \t2\n" + - "\tenum1 : \tGLOBAL\n" + - "\te : \t1\n" + - "\tf : \t1\n" + - "\tg : \t1\n" + - "\tenum2 : \tUSER\n" + - "\th : \t1.0\n" + - "\ti : \t1.0\n" + - "\tj : \t1\n" + - "\tk : \t1\n" + - "\tenum3 : \tCOMMITTED\n" + - "\tl : \t" + TEST_DATE + "\n" + - "\tm : \t[1, 1]\n" + - "\tn : \t[1.1, 1.1]\n" + - "\to : \t{a=1.0, b=2.0}\n" + - "\tenum4 : \tPARTITION\n" + - "}"); - } - - @Test - public void testClass() throws AtlasException { - TypeSystem ts = getTypeSystem(); - defineEnums(ts); - ClassType clsType = defineClassTypeWithEnum(ts); - - IReferenceableInstance r = createInstanceWithEnum("t4"); - ITypedReferenceableInstance typedR = clsType.convert(r, Multiplicity.REQUIRED); - Assert.assertEquals(typedR.toString(), "{\n" + - "\tid : (type: t4, id: <unassigned>)\n" + - "\ta : \t1\n" + - "\tb : \ttrue\n" + - "\tc : \t1\n" + - "\td : \t2\n" + - "\tenum1 : \tGLOBAL\n" + - "\te : \t1\n" + - "\tf : \t1\n" + - "\tg : \t1\n" + - "\tenum2 : \tUSER\n" + - "\th : \t1.0\n" + - "\ti : \t1.0\n" + - "\tj : \t1\n" + - "\tk : \t1\n" + - "\tenum3 : \tCOMMITTED\n" + - "\tl : \t" + TEST_DATE + "\n" + - "\tm : \t[1, 1]\n" + - "\tn : \t[1.1, 1.1]\n" + - "\to : \t{a=1.0, b=2.0}\n" + - "\tenum4 : \tPARTITION\n" + - "}"); - } -} http://git-wip-us.apache.org/repos/asf/atlas/blob/435fe3fb/typesystem/src/test/java/org/apache/atlas/typesystem/types/FieldMappingTest.java ---------------------------------------------------------------------- diff --git a/typesystem/src/test/java/org/apache/atlas/typesystem/types/FieldMappingTest.java b/typesystem/src/test/java/org/apache/atlas/typesystem/types/FieldMappingTest.java deleted file mode 100644 index 0259ade..0000000 --- a/typesystem/src/test/java/org/apache/atlas/typesystem/types/FieldMappingTest.java +++ /dev/null @@ -1,151 +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.typesystem.types; - -import java.util.HashSet; - -import org.apache.atlas.typesystem.IReferenceableInstance; -import org.apache.atlas.typesystem.ITypedReferenceableInstance; -import org.apache.atlas.typesystem.ITypedStruct; -import org.apache.atlas.typesystem.TypesDef; -import org.apache.atlas.typesystem.types.AttributeDefinition; -import org.apache.atlas.typesystem.types.ClassType; -import org.apache.atlas.typesystem.types.EnumTypeDefinition; -import org.apache.atlas.typesystem.types.HierarchicalTypeDefinition; -import org.apache.atlas.typesystem.types.Multiplicity; -import org.apache.atlas.typesystem.types.StructTypeDefinition; -import org.apache.atlas.typesystem.types.TraitType; -import org.apache.atlas.typesystem.types.TypeSystem; -import org.apache.atlas.typesystem.types.utils.TypesUtil; -import org.testng.Assert; -import org.testng.annotations.BeforeTest; -import org.testng.annotations.Test; - -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableSet; - - -/** - * Unit test for {@link FieldMapping} - * - */ -public class FieldMappingTest { - - @BeforeTest - public void beforeTest() throws Exception { - TypeSystem typeSystem = TypeSystem.getInstance(); - typeSystem.reset(); - } - - @Test - public void testOutputReferenceableInstance() throws Exception { - // ATLAS-645: verify that FieldMapping.output(IReferenceableInstance) - // does not infinitely recurse when ITypedReferenceableInstance's reference each other. - HierarchicalTypeDefinition<ClassType> valueDef = TypesUtil.createClassTypeDef("Value", - ImmutableSet.<String>of(), - new AttributeDefinition("owner", "Owner", Multiplicity.OPTIONAL, false, null)); - - // Define class type with reference, where the value is a class reference to Value. - HierarchicalTypeDefinition<ClassType> ownerDef = TypesUtil.createClassTypeDef("Owner", - ImmutableSet.<String>of(), - new AttributeDefinition("value", "Value", Multiplicity.OPTIONAL, false, null)); - TypesDef typesDef = TypesUtil.getTypesDef(ImmutableList.<EnumTypeDefinition>of(), - ImmutableList.<StructTypeDefinition>of(), ImmutableList.<HierarchicalTypeDefinition<TraitType>>of(), - ImmutableList.of(ownerDef, valueDef)); - - TypeSystem typeSystem = TypeSystem.getInstance(); - typeSystem.defineTypes(typesDef); - ClassType ownerType = typeSystem.getDataType(ClassType.class, "Owner"); - - // Prior to fix for ATLAS-645, this call would throw a StackOverflowError - try { - ownerType.toString(); - } - catch (StackOverflowError e) { - Assert.fail("Infinite recursion in ClassType.toString() caused StackOverflowError"); - } - - ClassType valueType = typeSystem.getDataType(ClassType.class, "Value"); - - // Create instances of Owner and Value that reference each other. - ITypedReferenceableInstance ownerInstance = ownerType.createInstance(); - ITypedReferenceableInstance valueInstance = valueType.createInstance(); - // Set Owner.value reference to Value instance. - ownerInstance.set("value", valueInstance); - // Set Value.owner reference on Owner instance. - valueInstance.set("owner", ownerInstance); - - // Prior to fix for ATLAS-645, this call would throw a StackOverflowError - try { - ownerInstance.fieldMapping().output(ownerInstance, new StringBuilder(), "", new HashSet<IReferenceableInstance>()); - } - catch (StackOverflowError e) { - Assert.fail("Infinite recursion in FieldMapping.output() caused StackOverflowError"); - } - } - - @Test - public void testOutputStruct() throws Exception { - // ATLAS-645: verify that FieldMapping.output(IStruct) does not infinitely recurse - // when an IStruct and ITypedReferenceableInstance reference each other. - HierarchicalTypeDefinition<ClassType> valueDef = TypesUtil.createClassTypeDef("Value", - ImmutableSet.<String>of(), - new AttributeDefinition("owner", "Owner", Multiplicity.OPTIONAL, false, null)); - - - // Define struct type with reference, where the value is a class reference to Value. - StructTypeDefinition ownerDef = TypesUtil.createStructTypeDef("Owner", - new AttributeDefinition("value", "Value", Multiplicity.OPTIONAL, false, null)); - - TypesDef typesDef = TypesUtil.getTypesDef(ImmutableList.<EnumTypeDefinition>of(), - ImmutableList.of(ownerDef), ImmutableList.<HierarchicalTypeDefinition<TraitType>>of(), - ImmutableList.of(valueDef)); - - TypeSystem typeSystem = TypeSystem.getInstance(); - typeSystem.reset(); - typeSystem.defineTypes(typesDef); - StructType ownerType = typeSystem.getDataType(StructType.class, "Owner"); - ClassType valueType = typeSystem.getDataType(ClassType.class, "Value"); - - // Prior to fix for ATLAS-645, this call would throw a StackOverflowError - try { - ownerType.toString(); - } - catch (StackOverflowError e) { - Assert.fail("Infinite recursion in StructType.toString() caused StackOverflowError"); - } - - - // Create instances of Owner and Value that reference each other. - ITypedStruct ownerInstance = ownerType.createInstance(); - ITypedReferenceableInstance valueInstance = valueType.createInstance(); - // Set Owner.value reference to Value instance. - ownerInstance.set("value", valueInstance); - // Set Value.owner reference on Owner instance. - valueInstance.set("owner", ownerInstance); - - // Prior to fix for ATLAS-645, this call would throw a StackOverflowError - try { - ownerInstance.fieldMapping().output(ownerInstance, new StringBuilder(), "", null); - } - catch (StackOverflowError e) { - Assert.fail("Infinite recursion in FieldMapping.output() caused StackOverflowError"); - } - - } -} http://git-wip-us.apache.org/repos/asf/atlas/blob/435fe3fb/typesystem/src/test/java/org/apache/atlas/typesystem/types/HierarchicalTypeDependencySorterTest.java ---------------------------------------------------------------------- diff --git a/typesystem/src/test/java/org/apache/atlas/typesystem/types/HierarchicalTypeDependencySorterTest.java b/typesystem/src/test/java/org/apache/atlas/typesystem/types/HierarchicalTypeDependencySorterTest.java deleted file mode 100644 index 19bdccf..0000000 --- a/typesystem/src/test/java/org/apache/atlas/typesystem/types/HierarchicalTypeDependencySorterTest.java +++ /dev/null @@ -1,81 +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.typesystem.types; - -import java.util.Arrays; -import java.util.List; - -import org.apache.atlas.AtlasException; -import org.testng.Assert; -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.Test; - -import com.google.common.collect.ImmutableSet; - - -public class HierarchicalTypeDependencySorterTest { - - @BeforeMethod - public void setup() throws Exception { - TypeSystem ts = TypeSystem.getInstance(); - ts.reset(); - } - - @SuppressWarnings("rawtypes") - @Test - public void testSimpleModel() throws AtlasException { - TypeSystem ts = TypeSystem.getInstance(); - HierarchicalType a = new ClassType(ts, "a", null, ImmutableSet.<String>of(), 0); - HierarchicalType b = new ClassType(ts, "B", null, ImmutableSet.of("a"), 0); - HierarchicalType c = new ClassType(ts, "C", null, ImmutableSet.of("B"), 0); - - List<HierarchicalType> unsortedTypes = Arrays.asList(c, a, b); - List<HierarchicalType> sortedTypes = HierarchicalTypeDependencySorter.sortTypes(unsortedTypes); - Assert.assertEquals(sortedTypes.size(), 3); - Assert.assertEquals(sortedTypes.indexOf(a), 0); - Assert.assertEquals(sortedTypes.indexOf(b), 1); - Assert.assertEquals(sortedTypes.indexOf(c), 2); - } - - @SuppressWarnings("rawtypes") - @Test - public void testLargerModel() throws Exception { - TypeSystem ts = TypeSystem.getInstance(); - HierarchicalType testObjectType = new ClassType(ts, "TestObject", null, ImmutableSet.<String>of(), 0); - HierarchicalType testDataSetType = new ClassType(ts, "TestDataSet", null, ImmutableSet.of("TestObject"), 0); - HierarchicalType testColumnType = new ClassType(ts, "TestColumn", null, ImmutableSet.of("TestObject"), 0); - HierarchicalType testRelationalDataSetType = new ClassType(ts, "TestRelationalDataSet", null, ImmutableSet.of("TestDataSet"), 0); - HierarchicalType testTableType = new ClassType(ts, "Table", null, ImmutableSet.of("TestDataSet"), 0); - HierarchicalType testDataFileType = new ClassType(ts, "TestDataFile", null, ImmutableSet.of("TestRelationalDataSet"), 0); - HierarchicalType testDocumentType = new ClassType(ts, "TestDocument", null, ImmutableSet.of("TestDataSet"), 0); - HierarchicalType testAnnotationType = new ClassType(ts, "TestAnnotation", null, ImmutableSet.<String>of(), 0); - HierarchicalType myNewAnnotationType = new ClassType(ts, "MyNewAnnotation", null, ImmutableSet.of("TestAnnotation"), 0); - - List<HierarchicalType> unsortedTypes = Arrays.asList(testTableType, testColumnType, myNewAnnotationType, testDataSetType, - testDataFileType, testAnnotationType, testRelationalDataSetType, testObjectType, testDocumentType); - List<HierarchicalType> sortedTypes = HierarchicalTypeDependencySorter.sortTypes(unsortedTypes); - // Verify that super types were sorted before their subtypes. - Assert.assertTrue(sortedTypes.indexOf(testObjectType) < sortedTypes.indexOf(testDataSetType)); - Assert.assertTrue(sortedTypes.indexOf(testObjectType) < sortedTypes.indexOf(testColumnType)); - Assert.assertTrue(sortedTypes.indexOf(testDataSetType) < sortedTypes.indexOf(testRelationalDataSetType)); - Assert.assertTrue(sortedTypes.indexOf(testDataSetType) < sortedTypes.indexOf(testDocumentType)); - Assert.assertTrue(sortedTypes.indexOf(testDataSetType) < sortedTypes.indexOf(testTableType)); - Assert.assertTrue(sortedTypes.indexOf(testRelationalDataSetType) < sortedTypes.indexOf(testDataFileType)); - Assert.assertTrue(sortedTypes.indexOf(testAnnotationType) < sortedTypes.indexOf(myNewAnnotationType)); - } -} http://git-wip-us.apache.org/repos/asf/atlas/blob/435fe3fb/typesystem/src/test/java/org/apache/atlas/typesystem/types/HierarchicalTypeTest.java ---------------------------------------------------------------------- diff --git a/typesystem/src/test/java/org/apache/atlas/typesystem/types/HierarchicalTypeTest.java b/typesystem/src/test/java/org/apache/atlas/typesystem/types/HierarchicalTypeTest.java deleted file mode 100644 index 9f63f52..0000000 --- a/typesystem/src/test/java/org/apache/atlas/typesystem/types/HierarchicalTypeTest.java +++ /dev/null @@ -1,92 +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.typesystem.types; - -import com.google.common.collect.ImmutableSet; - -import org.apache.atlas.typesystem.TypesDef; -import org.apache.atlas.typesystem.types.utils.TypesUtil; -import org.testng.Assert; -import org.testng.annotations.Test; - -public abstract class HierarchicalTypeTest<T extends HierarchicalType> extends TypeUpdateBaseTest { - @Test - public void testTypeUpdate() throws Exception { - testTypeUpdateForAttributes(); - - //Test super types - HierarchicalTypeDefinition baseSuperType = - getTypeDefinition(newName(), TypesUtil.createOptionalAttrDef("s", DataTypes.INT_TYPE)); - - HierarchicalTypeDefinition classType = getTypeDefinition(newName(), ImmutableSet.of(baseSuperType.typeName), - TypesUtil.createRequiredAttrDef("a", DataTypes.INT_TYPE)); - TypeSystem ts = getTypeSystem(); - ts.defineTypes(getTypesDef(baseSuperType, classType)); - - //Add super type with optional attribute - HierarchicalTypeDefinition superType = - getTypeDefinition(newName(), TypesUtil.createOptionalAttrDef("s", DataTypes.INT_TYPE)); - classType = getTypeDefinition(classType.typeName, ImmutableSet.of(superType.typeName, baseSuperType.typeName), - TypesUtil.createRequiredAttrDef("a", DataTypes.INT_TYPE)); - try { - ts.updateTypes(getTypesDef(superType, classType)); - Assert.fail("Expected TypeUpdateException"); - } catch (TypeUpdateException e) { - //expected - } - - //Add super type with required attribute should fail - HierarchicalTypeDefinition superTypeRequired = - getTypeDefinition(newName(), TypesUtil.createRequiredAttrDef("s", DataTypes.INT_TYPE)); - classType = getTypeDefinition(classType.typeName, - ImmutableSet.of(superTypeRequired.typeName, baseSuperType.typeName), - TypesUtil.createRequiredAttrDef("a", DataTypes.INT_TYPE)); - try { - ts.updateTypes(getTypesDef(superTypeRequired, classType)); - Assert.fail("Expected TypeUpdateException"); - } catch (TypeUpdateException e) { - //expected - } - - //Deleting super type should fail - classType = getTypeDefinition(classType.typeName, TypesUtil.createRequiredAttrDef("a", DataTypes.INT_TYPE)); - try { - ts.updateTypes(getTypesDef(classType)); - Assert.fail("Expected TypeUpdateException"); - } catch (TypeUpdateException e) { - //expected - } - } - - @Override - protected abstract HierarchicalTypeDefinition<T> getTypeDefinition(String name, AttributeDefinition... attributes); - - protected abstract HierarchicalTypeDefinition<T> getTypeDefinition(String name, ImmutableSet<String> superTypes, - AttributeDefinition... attributes); - - @Override - protected abstract TypesDef getTypesDef(StructTypeDefinition typeDefinition); - - protected abstract TypesDef getTypesDef(HierarchicalTypeDefinition<T>... typeDefinitions); - - @Override - protected int getNumberOfFields(TypeSystem ts, String typeName) throws Exception { - return ts.getDataType(HierarchicalType.class, typeName).numFields; - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/atlas/blob/435fe3fb/typesystem/src/test/java/org/apache/atlas/typesystem/types/StructTest.java ---------------------------------------------------------------------- diff --git a/typesystem/src/test/java/org/apache/atlas/typesystem/types/StructTest.java b/typesystem/src/test/java/org/apache/atlas/typesystem/types/StructTest.java deleted file mode 100755 index 3a1675e..0000000 --- a/typesystem/src/test/java/org/apache/atlas/typesystem/types/StructTest.java +++ /dev/null @@ -1,126 +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.typesystem.types; - -import com.google.common.collect.ImmutableList; -import org.apache.atlas.AtlasException; -import org.apache.atlas.typesystem.ITypedStruct; -import org.apache.atlas.typesystem.Struct; -import org.apache.atlas.typesystem.TypesDef; -import org.apache.atlas.typesystem.types.utils.TypesUtil; -import org.testng.Assert; -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.Test; - -import static org.testng.Assert.assertTrue; - -public class StructTest extends TypeUpdateBaseTest { - - StructType structType; - StructType recursiveStructType; - StructType invalidStructType; - - @BeforeMethod - public void setup() throws Exception { - super.setup(); - structType = getTypeSystem().getDataType(StructType.class, STRUCT_TYPE_1); - recursiveStructType = getTypeSystem().getDataType(StructType.class, STRUCT_TYPE_2); - invalidStructType = getTypeSystem().getDataType(StructType.class, STRUCT_TYPE_3); - } - - @Test - public void test1() throws AtlasException { - Struct s = createStruct(); - ITypedStruct ts = structType.convert(s, Multiplicity.REQUIRED); - Assert.assertEquals(ts.toString(), "{\n" + - "\ta : \t1\n" + - "\tb : \ttrue\n" + - "\tc : \t1\n" + - "\td : \t2\n" + - "\te : \t1\n" + - "\tf : \t1\n" + - "\tg : \t1\n" + - "\th : \t1.0\n" + - "\ti : \t1.0\n" + - "\tj : \t1\n" + - "\tk : \t1\n" + - "\tl : \t" + TEST_DATE + "\n" + - "\tm : \t[1, 1]\n" + - "\tn : \t[1.1, 1.1]\n" + - "\to : \t{a=1.0, b=2.0}\n" + - "\tp : \t\n" + - "\tq : \t<null>\n"+ - "\tr : \t{a=}\n" + - "}"); - } - - @Test - public void testStructWithEmptyString() throws AtlasException{ - try { - assertTrue(getTypeSystem().getTypeNames().contains("t3")); - Struct s = new Struct(invalidStructType.getName()); - s.set("a", ""); - ITypedStruct ts = invalidStructType.convert(s, Multiplicity.REQUIRED); - } - catch (AtlasException e){ - String err = "org.apache.atlas.typesystem.types.ValueConversionException: Cannot convert value 'org.apache.atlas.typesystem.Struct@1ba02' to datatype t3"; - Assert.assertEquals(e.toString(), err); - } - } - - @Test - public void testRecursive() throws AtlasException { - Struct s1 = new Struct(recursiveStructType.getName()); - s1.set("a", 1); - Struct s2 = new Struct(recursiveStructType.getName()); - s2.set("a", 1); - s2.set("s", s1); - ITypedStruct ts = recursiveStructType.convert(s2, Multiplicity.REQUIRED); - Assert.assertEquals(ts.toString(), "{\n" + - "\ta : \t1\n" + - "\ts : \t{\n" + - "\t\ta : \t\t1\n" + - "\t\ts : <null>\n" + - "\n" + - "\t}\n" + - "}"); - } - - @Test - public void testTypeUpdate() throws Exception { - testTypeUpdateForAttributes(); - } - - @Override - protected int getNumberOfFields(TypeSystem ts, String typeName) throws Exception { - return ts.getDataType(StructType.class, typeName).numFields; - } - - @Override - protected StructTypeDefinition getTypeDefinition(String name, AttributeDefinition... attributes) { - return new StructTypeDefinition(name, attributes); - } - - @Override - protected TypesDef getTypesDef(StructTypeDefinition typeDefinition) { - return TypesUtil.getTypesDef(ImmutableList.<EnumTypeDefinition>of(), ImmutableList.of(typeDefinition), - ImmutableList.<HierarchicalTypeDefinition<TraitType>>of(), - ImmutableList.<HierarchicalTypeDefinition<ClassType>>of()); - } -} http://git-wip-us.apache.org/repos/asf/atlas/blob/435fe3fb/typesystem/src/test/java/org/apache/atlas/typesystem/types/TraitTest.java ---------------------------------------------------------------------- diff --git a/typesystem/src/test/java/org/apache/atlas/typesystem/types/TraitTest.java b/typesystem/src/test/java/org/apache/atlas/typesystem/types/TraitTest.java deleted file mode 100755 index 7c39213..0000000 --- a/typesystem/src/test/java/org/apache/atlas/typesystem/types/TraitTest.java +++ /dev/null @@ -1,247 +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.typesystem.types; - -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableSet; - -import org.apache.atlas.AtlasException; -import org.apache.atlas.typesystem.IStruct; -import org.apache.atlas.typesystem.ITypedStruct; -import org.apache.atlas.typesystem.Struct; -import org.apache.atlas.typesystem.TypesDef; -import org.apache.atlas.typesystem.types.utils.TypesUtil; -import org.testng.Assert; -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.Test; - -import java.util.HashMap; -import java.util.Map; - -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.createTraitTypeDef; - -public class TraitTest extends HierarchicalTypeTest<TraitType> { - - - @BeforeMethod - public void setup() throws Exception { - super.setup(); - } - - /* - * Type Hierarchy is: - * A(a,b,c,d) - * B(b) extends A - * C(c) extends A - * D(d) extends B,C - * - * - There are a total of 11 fields in an instance of D - * - an attribute that is hidden by a SubType can referenced by prefixing it with the - * complete Path. - * For e.g. the 'b' attribute in A (that is a superType for B) is hidden the 'b' attribute - * in B. - * So it is available by the name 'A.B.D.b' - * - * - Another way to set attributes is to cast. Casting a 'D' instance of 'B' makes the 'A.B.D - * .b' attribute - * available as 'A.B.b'. Casting one more time to an 'A' makes the 'A.B.b' attribute - * available as 'b'. - */ - @Test - public void test1() 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(A, B, C, D); - - TraitType DType = getTypeSystem().getDataType(TraitType.class, "D"); - - // for(String aName : DType.fieldMapping().fields.keySet()) { - // System.out.println(String.format("nameToQualifiedName.put(\"%s\", \"%s\");", aName, DType - // .getQualifiedName(aName))); - // } - - Map<String, String> nameToQualifiedName = new HashMap(); - { - nameToQualifiedName.put("d", "D.d"); - nameToQualifiedName.put("b", "B.b"); - nameToQualifiedName.put("c", "C.c"); - nameToQualifiedName.put("a", "A.a"); - nameToQualifiedName.put("A.B.D.b", "A.B.D.b"); - nameToQualifiedName.put("A.B.D.c", "A.B.D.c"); - nameToQualifiedName.put("A.B.D.d", "A.B.D.d"); - nameToQualifiedName.put("A.C.D.a", "A.C.D.a"); - nameToQualifiedName.put("A.C.D.b", "A.C.D.b"); - nameToQualifiedName.put("A.C.D.c", "A.C.D.c"); - nameToQualifiedName.put("A.C.D.d", "A.C.D.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" + - "}"); - - /* - * cast to B and set the 'b' attribute on A. - */ - TraitType BType = getTypeSystem().getDataType(TraitType.class, "B"); - IStruct s2 = DType.castAs(ts, "B"); - s2.set("A.B.b", false); - - Assert.assertEquals(ts.toString(), "{\n" + - "\td : \t1\n" + - "\tb : \ttrue\n" + - "\tc : \t1\n" + - "\ta : \t1\n" + - "\tA.B.D.b : \tfalse\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" + - "}"); - - /* - * cast again to A and set the 'b' attribute on A. - */ - TraitType AType = getTypeSystem().getDataType(TraitType.class, "A"); - IStruct s3 = BType.castAs(s2, "A"); - s3.set("b", true); - 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" + - "}"); - } - - @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" + - "}"); - } - - @Override - protected HierarchicalTypeDefinition<TraitType> getTypeDefinition(String name, AttributeDefinition... attributes) { - return new HierarchicalTypeDefinition(TraitType.class, name, null, null, attributes); - } - - @Override - protected HierarchicalTypeDefinition<TraitType> getTypeDefinition(String name, ImmutableSet<String> superTypes, - AttributeDefinition... attributes) { - return new HierarchicalTypeDefinition(TraitType.class, name, null, superTypes, attributes); - } - - @Override - protected TypesDef getTypesDef(StructTypeDefinition typeDefinition) { - return TypesUtil.getTypesDef(ImmutableList.<EnumTypeDefinition>of(), ImmutableList.<StructTypeDefinition>of(), - ImmutableList.of((HierarchicalTypeDefinition<TraitType>) typeDefinition), - ImmutableList.<HierarchicalTypeDefinition<ClassType>>of()); - } - - @Override - protected TypesDef getTypesDef(HierarchicalTypeDefinition<TraitType>... typeDefinitions) { - return TypesUtil.getTypesDef(ImmutableList.<EnumTypeDefinition>of(), ImmutableList.<StructTypeDefinition>of(), - ImmutableList.copyOf(typeDefinitions), ImmutableList.<HierarchicalTypeDefinition<ClassType>>of()); - } -} -