@Test void testSerialize() { EntityType entityType = mock(EntityType.class); when(entityType.getId()).thenReturn("entity"); Attribute oneToManyAttr = mock(Attribute.class); String oneToManyAttrName = "oneToManyAttr"; when(oneToManyAttr.getName()).thenReturn(oneToManyAttrName); when(oneToManyAttr.getDataType()).thenReturn(ONE_TO_MANY); Attribute manyToOneAttr = mock(Attribute.class); String manyToOneAttrName = "xrefAttr"; when(manyToOneAttr.getName()).thenReturn(manyToOneAttrName); when(manyToOneAttr.getDataType()).thenReturn(XREF); when(entityType.getAtomicAttributes()).thenReturn(newArrayList(oneToManyAttr, manyToOneAttr)); EntityType refEntityType = mock(EntityType.class); when(refEntityType.getId()).thenReturn("refEntity"); String oneToManyEntity0IdValue = "oneToManyEntity0"; String oneToManyEntity0LabelValue = "oneToManyEntityLabel0"; Entity oneToManyEntity0 = mock(Entity.class); when(oneToManyEntity0.getEntityType()).thenReturn(refEntityType); when(oneToManyEntity0.getIdValue()).thenReturn(oneToManyEntity0IdValue); when(oneToManyEntity0.getLabelValue()).thenReturn(oneToManyEntity0LabelValue); String oneToManyEntity1IdValue = "oneToManyEntity1"; String oneToManyEntity1LabelValue = "oneToManyEntityLabel1"; Entity oneToManyEntity1 = mock(Entity.class); when(oneToManyEntity1.getIdValue()).thenReturn(oneToManyEntity1IdValue); when(oneToManyEntity1.getLabelValue()).thenReturn(oneToManyEntity1LabelValue); when(oneToManyEntity1.getEntityType()).thenReturn(refEntityType); List<Entity> oneToManyEntities = newArrayList(oneToManyEntity0, oneToManyEntity1); String manyToOneEntityIdValue = "xrefEntity0"; String manyToOneEntityLabelValue = "xrefEntityLabel0"; Entity manyToOneEntity = mock(Entity.class); when(manyToOneEntity.getEntityType()).thenReturn(refEntityType); when(manyToOneEntity.getIdValue()).thenReturn(manyToOneEntityIdValue); when(manyToOneEntity.getLabelValue()).thenReturn(manyToOneEntityLabelValue); Entity entity = mock(Entity.class); when(entity.getEntityType()).thenReturn(entityType); when(entity.get(oneToManyAttrName)).thenReturn(oneToManyEntities); when(entity.get(manyToOneAttrName)).thenReturn(manyToOneEntity); when(entity.getEntities(oneToManyAttrName)).thenReturn(oneToManyEntities); when(entity.getEntity(manyToOneAttrName)).thenReturn(manyToOneEntity); Type type = mock(Type.class); JsonSerializationContext context = mock(JsonSerializationContext.class); when(context.serialize(oneToManyEntity0IdValue)) .thenReturn(new JsonPrimitive(oneToManyEntity0IdValue)); when(context.serialize(oneToManyEntity0LabelValue)) .thenReturn(new JsonPrimitive(oneToManyEntity0LabelValue)); when(context.serialize(oneToManyEntity1IdValue)) .thenReturn(new JsonPrimitive(oneToManyEntity1IdValue)); when(context.serialize(oneToManyEntity1LabelValue)) .thenReturn(new JsonPrimitive(oneToManyEntity1LabelValue)); when(context.serialize(manyToOneEntityIdValue)) .thenReturn(new JsonPrimitive(manyToOneEntityIdValue)); when(context.serialize(manyToOneEntityLabelValue)) .thenReturn(new JsonPrimitive(manyToOneEntityLabelValue)); String expectedJson = "{\"__entityTypeId\":\"entity\",\"oneToManyAttr\":[{\"__entityTypeId\":\"refEntity\",\"__idValue\":\"oneToManyEntity0\",\"__labelValue\":\"oneToManyEntityLabel0\"},{\"__entityTypeId\":\"refEntity\",\"__idValue\":\"oneToManyEntity1\",\"__labelValue\":\"oneToManyEntityLabel1\"}],\"xrefAttr\":{\"__entityTypeId\":\"refEntity\",\"__idValue\":\"xrefEntity0\",\"__labelValue\":\"xrefEntityLabel0\"}}"; assertEquals(expectedJson, entitySerializer.serialize(entity, type, context).toString()); }
@Override public JsonElement serialize(Entity entity, Type type, JsonSerializationContext context) { JsonObject result = new JsonObject(); result.addProperty("__entityTypeId", entity.getEntityType().getId()); for (Attribute attr : entity.getEntityType().getAtomicAttributes()) { String attributeName = attr.getName(); Object value = entity.get(attributeName); if (value != null) { AttributeType attrType = attr.getDataType(); switch (attrType) { case BOOL: result.addProperty(attributeName, entity.getBoolean(attributeName)); break; case CATEGORICAL: case XREF: case FILE: Entity refEntity = entity.getEntity(attributeName); result.add(attributeName, serializeReference(refEntity, context)); break; case CATEGORICAL_MREF: case MREF: case ONE_TO_MANY: JsonArray jsonArray = new JsonArray(); entity .getEntities(attributeName) .forEach(e -> jsonArray.add(serializeReference(e, context))); result.add(attributeName, jsonArray); break; case DATE: LocalDate localDateValue = entity.getLocalDate(attributeName); result.add(attributeName, context.serialize(localDateValue)); break; case DATE_TIME: Instant instantValue = entity.getInstant(attributeName); result.add(attributeName, context.serialize(instantValue)); break; case DECIMAL: result.addProperty(attributeName, entity.getDouble(attributeName)); break; case INT: result.addProperty(attributeName, entity.getInt(attributeName)); break; case LONG: result.addProperty(attributeName, entity.getLong(attributeName)); break; case ENUM: case HTML: case HYPERLINK: case SCRIPT: case EMAIL: case STRING: case TEXT: result.addProperty(attributeName, value.toString()); break; case COMPOUND: throw new IllegalAttributeTypeException(attrType); default: throw new UnexpectedEnumException(attrType); } } } return result; }
EntitySerializer implements JsonSerializer<Entity> { @Override public JsonElement serialize(Entity entity, Type type, JsonSerializationContext context) { JsonObject result = new JsonObject(); result.addProperty("__entityTypeId", entity.getEntityType().getId()); for (Attribute attr : entity.getEntityType().getAtomicAttributes()) { String attributeName = attr.getName(); Object value = entity.get(attributeName); if (value != null) { AttributeType attrType = attr.getDataType(); switch (attrType) { case BOOL: result.addProperty(attributeName, entity.getBoolean(attributeName)); break; case CATEGORICAL: case XREF: case FILE: Entity refEntity = entity.getEntity(attributeName); result.add(attributeName, serializeReference(refEntity, context)); break; case CATEGORICAL_MREF: case MREF: case ONE_TO_MANY: JsonArray jsonArray = new JsonArray(); entity .getEntities(attributeName) .forEach(e -> jsonArray.add(serializeReference(e, context))); result.add(attributeName, jsonArray); break; case DATE: LocalDate localDateValue = entity.getLocalDate(attributeName); result.add(attributeName, context.serialize(localDateValue)); break; case DATE_TIME: Instant instantValue = entity.getInstant(attributeName); result.add(attributeName, context.serialize(instantValue)); break; case DECIMAL: result.addProperty(attributeName, entity.getDouble(attributeName)); break; case INT: result.addProperty(attributeName, entity.getInt(attributeName)); break; case LONG: result.addProperty(attributeName, entity.getLong(attributeName)); break; case ENUM: case HTML: case HYPERLINK: case SCRIPT: case EMAIL: case STRING: case TEXT: result.addProperty(attributeName, value.toString()); break; case COMPOUND: throw new IllegalAttributeTypeException(attrType); default: throw new UnexpectedEnumException(attrType); } } } return result; } }
EntitySerializer implements JsonSerializer<Entity> { @Override public JsonElement serialize(Entity entity, Type type, JsonSerializationContext context) { JsonObject result = new JsonObject(); result.addProperty("__entityTypeId", entity.getEntityType().getId()); for (Attribute attr : entity.getEntityType().getAtomicAttributes()) { String attributeName = attr.getName(); Object value = entity.get(attributeName); if (value != null) { AttributeType attrType = attr.getDataType(); switch (attrType) { case BOOL: result.addProperty(attributeName, entity.getBoolean(attributeName)); break; case CATEGORICAL: case XREF: case FILE: Entity refEntity = entity.getEntity(attributeName); result.add(attributeName, serializeReference(refEntity, context)); break; case CATEGORICAL_MREF: case MREF: case ONE_TO_MANY: JsonArray jsonArray = new JsonArray(); entity .getEntities(attributeName) .forEach(e -> jsonArray.add(serializeReference(e, context))); result.add(attributeName, jsonArray); break; case DATE: LocalDate localDateValue = entity.getLocalDate(attributeName); result.add(attributeName, context.serialize(localDateValue)); break; case DATE_TIME: Instant instantValue = entity.getInstant(attributeName); result.add(attributeName, context.serialize(instantValue)); break; case DECIMAL: result.addProperty(attributeName, entity.getDouble(attributeName)); break; case INT: result.addProperty(attributeName, entity.getInt(attributeName)); break; case LONG: result.addProperty(attributeName, entity.getLong(attributeName)); break; case ENUM: case HTML: case HYPERLINK: case SCRIPT: case EMAIL: case STRING: case TEXT: result.addProperty(attributeName, value.toString()); break; case COMPOUND: throw new IllegalAttributeTypeException(attrType); default: throw new UnexpectedEnumException(attrType); } } } return result; }  }
EntitySerializer implements JsonSerializer<Entity> { @Override public JsonElement serialize(Entity entity, Type type, JsonSerializationContext context) { JsonObject result = new JsonObject(); result.addProperty("__entityTypeId", entity.getEntityType().getId()); for (Attribute attr : entity.getEntityType().getAtomicAttributes()) { String attributeName = attr.getName(); Object value = entity.get(attributeName); if (value != null) { AttributeType attrType = attr.getDataType(); switch (attrType) { case BOOL: result.addProperty(attributeName, entity.getBoolean(attributeName)); break; case CATEGORICAL: case XREF: case FILE: Entity refEntity = entity.getEntity(attributeName); result.add(attributeName, serializeReference(refEntity, context)); break; case CATEGORICAL_MREF: case MREF: case ONE_TO_MANY: JsonArray jsonArray = new JsonArray(); entity .getEntities(attributeName) .forEach(e -> jsonArray.add(serializeReference(e, context))); result.add(attributeName, jsonArray); break; case DATE: LocalDate localDateValue = entity.getLocalDate(attributeName); result.add(attributeName, context.serialize(localDateValue)); break; case DATE_TIME: Instant instantValue = entity.getInstant(attributeName); result.add(attributeName, context.serialize(instantValue)); break; case DECIMAL: result.addProperty(attributeName, entity.getDouble(attributeName)); break; case INT: result.addProperty(attributeName, entity.getInt(attributeName)); break; case LONG: result.addProperty(attributeName, entity.getLong(attributeName)); break; case ENUM: case HTML: case HYPERLINK: case SCRIPT: case EMAIL: case STRING: case TEXT: result.addProperty(attributeName, value.toString()); break; case COMPOUND: throw new IllegalAttributeTypeException(attrType); default: throw new UnexpectedEnumException(attrType); } } } return result; }  @Override JsonElement serialize(Entity entity, Type type, JsonSerializationContext context); }
EntitySerializer implements JsonSerializer<Entity> { @Override public JsonElement serialize(Entity entity, Type type, JsonSerializationContext context) { JsonObject result = new JsonObject(); result.addProperty("__entityTypeId", entity.getEntityType().getId()); for (Attribute attr : entity.getEntityType().getAtomicAttributes()) { String attributeName = attr.getName(); Object value = entity.get(attributeName); if (value != null) { AttributeType attrType = attr.getDataType(); switch (attrType) { case BOOL: result.addProperty(attributeName, entity.getBoolean(attributeName)); break; case CATEGORICAL: case XREF: case FILE: Entity refEntity = entity.getEntity(attributeName); result.add(attributeName, serializeReference(refEntity, context)); break; case CATEGORICAL_MREF: case MREF: case ONE_TO_MANY: JsonArray jsonArray = new JsonArray(); entity .getEntities(attributeName) .forEach(e -> jsonArray.add(serializeReference(e, context))); result.add(attributeName, jsonArray); break; case DATE: LocalDate localDateValue = entity.getLocalDate(attributeName); result.add(attributeName, context.serialize(localDateValue)); break; case DATE_TIME: Instant instantValue = entity.getInstant(attributeName); result.add(attributeName, context.serialize(instantValue)); break; case DECIMAL: result.addProperty(attributeName, entity.getDouble(attributeName)); break; case INT: result.addProperty(attributeName, entity.getInt(attributeName)); break; case LONG: result.addProperty(attributeName, entity.getLong(attributeName)); break; case ENUM: case HTML: case HYPERLINK: case SCRIPT: case EMAIL: case STRING: case TEXT: result.addProperty(attributeName, value.toString()); break; case COMPOUND: throw new IllegalAttributeTypeException(attrType); default: throw new UnexpectedEnumException(attrType); } } } return result; }  @Override JsonElement serialize(Entity entity, Type type, JsonSerializationContext context);  }