Commit 53574720 by Shwetha GS

ATLAS-543 Entity Instance requests should not require ID element for new…

ATLAS-543 Entity Instance requests should not require ID element for new Entities (harishjp via shwethags)
parent 2e90ff1f
......@@ -17,6 +17,7 @@ ATLAS-409 Atlas will not import avro tables with schema read from a file (dosset
ATLAS-379 Create sqoop and falcon metadata addons (venkatnrangan,bvellanki,sowmyaramesh via shwethags)
ALL CHANGES:
ATLAS-543 Entity Instance requests should not require ID element for new Entities (harishjp via shwethags)
ATLAS-681 update committer/ppmc members in the pom.xml (sneethiraj via shwethags)
ATLAS-616 Resolve OOM - Zookeeper throws exceptions when trying to fire DSL queries at Atlas at large scale. (yhemanth via sumasai)
ATLAS-530 Add table information to column class (sumasai)
......
......@@ -31,9 +31,9 @@ import scala.collection.JavaConverters._
object InstanceSerialization {
case class _Id(id : String, version : Int, typeName : String, state : String)
case class _Id(id : String, version : Int, typeName : String, state : Option[String])
case class _Struct(typeName : String, values : Map[String, AnyRef])
case class _Reference(id : _Id,
case class _Reference(id : Option[_Id],
typeName : String,
values : Map[String, AnyRef],
traitNames : List[String],
......@@ -106,7 +106,7 @@ object InstanceSerialization {
i <- id
s <- state
v <- version
} yield _Id(i, v, typNm, s)
} yield _Id(i, v, typNm, Some(s))
}
/**
......@@ -233,7 +233,7 @@ object InstanceSerialization {
values <- valuesMap
traitNms <- traitNames
ts <- traits
} yield _Reference(i, typNm, values, traitNms.toList, ts)
} yield _Reference(Some(i), typNm, values, traitNms.toList, ts)
}
/**
......@@ -258,10 +258,14 @@ object InstanceSerialization {
}
def asJava(v : Any)(implicit format: Formats) : Any = v match {
case i : _Id => new Id(i.id, i.version, i.typeName, i.state)
case i : _Id => new Id(i.id, i.version, i.typeName, i.state.orNull)
case s : _Struct => new Struct(s.typeName, asJava(s.values).asInstanceOf[java.util.Map[String, Object]])
case r : _Reference => {
new Referenceable(new Id(r.id.id, r.id.version, r.id.typeName, r.id.state),
val id = r.id match {
case Some(i) => new Id(i.id, i.version, i.typeName, i.state.orNull)
case None => new Id(r.typeName)
}
new Referenceable(id,
r.typeName,
asJava(r.values).asInstanceOf[java.util.Map[String, Object]],
asJava(r.traitNames).asInstanceOf[java.util.List[String]],
......@@ -280,13 +284,13 @@ object InstanceSerialization {
}
def asScala(v : Any) : Any = v match {
case i : Id => _Id(i._getId(), i.getVersion, i.getClassName, i.getStateAsString)
case i : Id => _Id(i._getId(), i.getVersion, i.getClassName, Some(i.getStateAsString))
case r : IReferenceableInstance => {
val traits = r.getTraits.map { tName =>
val t = r.getTrait(tName).asInstanceOf[IStruct]
(tName -> _Struct(t.getTypeName, asScala(t.getValuesMap).asInstanceOf[Map[String, AnyRef]]))
}.toMap
_Reference(asScala(r.getId).asInstanceOf[_Id],
_Reference(Some(asScala(r.getId).asInstanceOf[_Id]),
r.getTypeName, asScala(r.getValuesMap).asInstanceOf[Map[String, AnyRef]],
asScala(r.getTraits).asInstanceOf[List[String]],
traits.asInstanceOf[Map[String, _Struct]])
......
/*
* 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.ImmutableSet
import org.apache.atlas.typesystem.Referenceable
import org.apache.atlas.typesystem.types.{DataTypes, TypeSystem}
import org.apache.atlas.typesystem.types.utils.TypesUtil
import org.testng.Assert._
import org.testng.annotations.{BeforeClass, Test}
import scala.util.Random
class InstanceSerializationTest {
private var typeName: String = null
@BeforeClass def setup {
typeName = "Random_" + Math.abs(Random.nextInt())
val clsType = TypesUtil.createClassTypeDef(typeName, "Random-description", ImmutableSet.of[String](),
TypesUtil.createRequiredAttrDef("name", DataTypes.STRING_TYPE))
TypeSystem.getInstance().defineClassType(clsType)
}
@Test def testIdentity {
val entity: Referenceable = new Referenceable(typeName)
val json: String = InstanceSerialization.toJson(entity, true)
val entity2: Referenceable = InstanceSerialization.fromJsonReferenceable(json, true)
assertNotNull(entity2)
assertEquals(entity2.getId, entity.getId, "Simple conversion failed")
assertEquals(entity2.getTraits, entity.getTraits, "Traits mismatch")
}
@Test def testMissingStateInId: Unit = {
val entity: Referenceable = new Referenceable(typeName)
val json: String = InstanceSerialization.toJson(entity, true)
val staticJson: String = "{\n" +
" \"jsonClass\":\"org.apache.atlas.typesystem.json.InstanceSerialization$_Reference\",\n" +
" \"id\":{\n" +
" \"jsonClass\":\"org.apache.atlas.typesystem.json.InstanceSerialization$_Id\",\n" +
" \"id\":\"" + entity.getId.id + "\",\n" +
" \"version\":0,\n" +
" \"typeName\":\"" + entity.getTypeName + "\",\n" +
" },\n" +
" \"typeName\":\"" + entity.getTypeName + "\",\n" +
" \"values\":{}\n" +
" \"traitNames\":[]\n" +
" \"traits\":{}\n" +
"}"
val entity2: Referenceable = InstanceSerialization.fromJsonReferenceable(staticJson, true)
assertNotNull(entity2)
assertNotNull(entity2.getId)
assertNotNull(entity2.getId.id) // This creates a new id so the values will not match.
assertEquals(entity2.getId.typeName, entity.getId.typeName)
assertEquals(entity2.getId.version, entity.getId.version)
assertEquals(entity2.getId.state, entity.getId.state)
assertEquals(entity2.getTypeName, entity.getTypeName, "Type name mismatch")
assertEquals(entity2.getValuesMap, entity.getValuesMap, "Values mismatch")
assertEquals(entity2.getTraits, entity.getTraits, "Traits mismatch")
}
@Test def testMissingId: Unit = {
val entity: Referenceable = new Referenceable(typeName)
val json: String = InstanceSerialization.toJson(entity, true)
val staticJson: String = "{\n" +
" \"jsonClass\":\"org.apache.atlas.typesystem.json.InstanceSerialization$_Reference\",\n" +
" \"typeName\":\"" + entity.getTypeName + "\",\n" +
" \"values\":{}\n" +
" \"traitNames\":[],\n" +
" \"traits\":{}\n" +
"}"
val entity2: Referenceable = InstanceSerialization.fromJsonReferenceable(staticJson, true)
assertNotNull(entity2)
assertNotNull(entity2.getId)
assertNotNull(entity2.getId.id) // This creates a new id so the values will not match.
assertEquals(entity2.getId.typeName, entity.getId.typeName)
assertEquals(entity2.getId.version, entity.getId.version)
assertEquals(entity2.getId.state, entity.getId.state)
assertEquals(entity2.getTypeName, entity.getTypeName, "Type name mismatch")
assertEquals(entity2.getValuesMap, entity.getValuesMap, "Values mismatch")
assertEquals(entity2.getTraits, entity.getTraits, "Traits mismatch")
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment