use of com.vmware.photon.controller.model.data.SchemaBuilder in project photon-model by vmware.
the class ResourceOperationSpecJsonTest method testRoundtrip.
@Test
public void testRoundtrip() {
ResourceOperationSpec origSpec = new ResourceOperationSpec();
origSpec.operation = "oper";
origSpec.targetCriteria = "true";
origSpec.adapterReference = URI.create("http://localhost");
origSpec.description = "OP desc";
origSpec.name = "OP name";
origSpec.resourceType = ResourceType.COMPUTE;
origSpec.endpointType = "myEP";
// @formatter:off
origSpec.schema = new SchemaBuilder().withName("schema1").addField("f1").withDataType(SchemaField.DATATYPE_STRING).withLabel("F One").withType(Type.VALUE).done().build();
ExtensionOne origExtensionOne = new ExtensionOne();
origExtensionOne.prop1 = "property one";
origExtensionOne.prop2 = URI.create("urn:prop2:value");
ExtensionTwo origExtensionTwo = new ExtensionTwo();
origExtensionTwo.int1 = 42;
origSpec.extensions = new HashMap<>();
origSpec.extensions.put(EX_ONE, Utils.toJson(origExtensionOne));
origSpec.extensions.put(EX_TWO, Utils.toJson(origExtensionTwo));
// @formatter:on
String json = Utils.toJson(origSpec);
this.logger.info("json: " + json);
ResourceOperationSpec trgtSpec = Utils.fromJson(json, ResourceOperationSpec.class);
Assert.assertNotNull(trgtSpec.schema);
Schema origSchema = origSpec.schema;
Schema trgtSchema = trgtSpec.schema;
Assert.assertEquals(origSchema.name, trgtSchema.name);
Assert.assertEquals(origSchema.fields, trgtSchema.fields);
String jsonExtensionOne = trgtSpec.extensions.get(EX_ONE);
{
Assert.assertNotNull(jsonExtensionOne);
this.logger.info("jsonExtensionOne: " + jsonExtensionOne);
BaseExtension be = Utils.fromJson(jsonExtensionOne, BaseExtension.class);
Assert.assertEquals(ExtensionOne.KIND, be.documentKind);
ExtensionOne trgtExtensionOne = Utils.fromJson(jsonExtensionOne, ExtensionOne.class);
this.logger.info("trgtExtensionOne: " + trgtExtensionOne);
Assert.assertEquals(origExtensionOne.prop1, trgtExtensionOne.prop1);
Assert.assertEquals(origExtensionOne.prop2, trgtExtensionOne.prop2);
}
String jsonExtensionTwo = trgtSpec.extensions.get(EX_TWO);
{
Assert.assertNotNull(jsonExtensionTwo);
this.logger.info("jsonExtensionTwo: " + jsonExtensionTwo);
BaseExtension be = Utils.fromJson(jsonExtensionTwo, BaseExtension.class);
Assert.assertEquals(ExtensionTwo.KIND, be.documentKind);
ExtensionTwo trgtExtensionTwo = Utils.fromJson(jsonExtensionTwo, ExtensionTwo.class);
this.logger.info("trgtExtensionTwo: " + trgtExtensionTwo);
Assert.assertEquals(origExtensionTwo.int1, trgtExtensionTwo.int1);
}
}
Aggregations