Commit 0a208d18 by Harish Butani

add java json serde test

parent 77030a8c
...@@ -22,10 +22,10 @@ import org.apache.jute.compiler.JLong ...@@ -22,10 +22,10 @@ import org.apache.jute.compiler.JLong
import org.apache.hadoop.metadata.types.DataTypes.{MapType, TypeCategory, ArrayType} import org.apache.hadoop.metadata.types.DataTypes.{MapType, TypeCategory, ArrayType}
import org.apache.hadoop.metadata._ import org.apache.hadoop.metadata._
import org.apache.hadoop.metadata.types._ import org.apache.hadoop.metadata.types._
import org.apache.hadoop.metadata.storage.Id import org.apache.hadoop.metadata.storage.{ReferenceableInstance, Id}
import org.json4s.JsonAST.JInt import org.json4s.JsonAST.JInt
import org.json4s._ import org.json4s._
import org.json4s.native.Serialization.{read, write => swrite} import org.json4s.native.Serialization.{write => swrite, _}
import org.json4s.reflect.{ScalaType, Reflector} import org.json4s.reflect.{ScalaType, Reflector}
import java.util.regex.Pattern import java.util.regex.Pattern
import java.util.Date import java.util.Date
...@@ -239,5 +239,17 @@ object Serialization { ...@@ -239,5 +239,17 @@ object Serialization {
JField("version", JInt(version)) :: Nil) => new Id(id.toLong, version.toInt, className) JField("version", JInt(version)) :: Nil) => new Id(id.toLong, version.toInt, className)
} }
def toJson(value : ITypedReferenceableInstance) : String = {
implicit val formats = org.json4s.native.Serialization.formats(NoTypeHints) + new TypedStructSerializer +
new TypedReferenceableInstanceSerializer + new BigDecimalSerializer + new BigIntegerSerializer
writePretty(value)
}
def fromJson(jsonStr : String) : ITypedReferenceableInstance = {
implicit val formats = org.json4s.native.Serialization.formats(NoTypeHints) + new TypedStructSerializer +
new TypedReferenceableInstanceSerializer + new BigDecimalSerializer + new BigIntegerSerializer
read[ReferenceableInstance](jsonStr)
}
} }
/**
* 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.hadoop.metadata.json;
import com.google.common.collect.ImmutableList;
import org.apache.hadoop.metadata.BaseTest;
import org.apache.hadoop.metadata.ITypedReferenceableInstance;
import org.apache.hadoop.metadata.MetadataException;
import org.apache.hadoop.metadata.Referenceable;
import org.apache.hadoop.metadata.types.*;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
public class SerializationJavaTest extends BaseTest {
@Before
public void setup() throws MetadataException {
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 MetadataException {
TypeSystem ts = ms.getTypeSystem();
HierarchicalTypeDefinition<ClassType> deptTypeDef = createClassTypeDef("Department", ImmutableList.<String>of(),
createRequiredAttrDef("name", DataTypes.STRING_TYPE),
new AttributeDefinition("employees",
String.format("array<%s>", "Person"), Multiplicity.COLLECTION, true, "department")
);
HierarchicalTypeDefinition<ClassType> personTypeDef = createClassTypeDef("Person", ImmutableList.<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",
ImmutableList.<String>of("Person"),
new AttributeDefinition("subordinates",
String.format("array<%s>", "Person"), Multiplicity.COLLECTION, false, "manager")
);
HierarchicalTypeDefinition<TraitType> securityClearanceTypeDef = createTraitTypeDef("SecurityClearance",
ImmutableList.<String>of(),
createRequiredAttrDef("level", DataTypes.INT_TYPE)
);
ts.defineTypes(ImmutableList.<StructTypeDefinition>of(),
ImmutableList.<HierarchicalTypeDefinition<TraitType>>of(securityClearanceTypeDef),
ImmutableList.<HierarchicalTypeDefinition<ClassType>>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.<Referenceable>of(john, jane));
jane.set("subordinates", ImmutableList.<Referenceable>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);
}
}
\ No newline at end of file
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