added some testcases and method writeComplex to write the type and value correctly.

Signed-off-by: Christian Amend <christian.amend@sap.com>
This commit is contained in:
Morten Riedel 2016-07-25 16:23:55 +02:00 committed by Christian Amend
parent 44d6f5a171
commit 756ae564e5
2 changed files with 150 additions and 19 deletions

View File

@ -400,8 +400,10 @@ public class ODataJsonSerializer extends AbstractODataSerializer {
protected EdmComplexType resolveComplexType(final ServiceMetadata metadata, final EdmComplexType baseType,
final String derivedTypeName) throws SerializerException {
String fullQualifiedName = baseType.getFullQualifiedName().getFullQualifiedNameAsString();
if (derivedTypeName == null ||
baseType.getFullQualifiedName().getFullQualifiedNameAsString().equals(derivedTypeName)) {
fullQualifiedName.equals(derivedTypeName)) {
return baseType;
}
EdmComplexType derivedType = metadata.getEdm().getComplexType(new FullQualifiedName(derivedTypeName));
@ -611,8 +613,7 @@ public class ODataJsonSerializer extends AbstractODataSerializer {
if (edmProperty.isCollection()) {
writeComplexCollection(metadata, (EdmComplexType) type, property, selectedPaths, json);
} else {
writeComplexValue(metadata, property, (EdmComplexType) type, property.asComplex().getValue(), selectedPaths,
json);
writeComplex(metadata, (EdmComplexType) type, property, selectedPaths, json);
}
} else {
throw new SerializerException("Property type not yet supported!",
@ -625,6 +626,20 @@ public class ODataJsonSerializer extends AbstractODataSerializer {
}
}
private void writeComplex(final ServiceMetadata metadata, final EdmComplexType type,
final Property property, final Set<List<String>> selectedPaths, final JsonGenerator json)
throws IOException, SerializerException{
json.writeStartObject();
String derivedName = property.getType();
final EdmComplexType resolvedType = resolveComplexType(metadata, (EdmComplexType) type, derivedName);
if (!isODataMetadataNone && !resolvedType.equals(type) || isODataMetadataFull) {
json.writeStringField(Constants.JSON_TYPE, "#" + property.getType());
}
writeComplexValue(metadata, resolvedType, property.asComplex().getValue(), selectedPaths,
json);
json.writeEndObject();
}
private void writePrimitiveCollection(final EdmPrimitiveType type, final Property property,
final Boolean isNullable, final Integer maxLength, final Integer precision, final Integer scale,
final Boolean isUnicode, final JsonGenerator json)
@ -662,7 +677,13 @@ public class ODataJsonSerializer extends AbstractODataSerializer {
for (Object value : property.asCollection()) {
switch (property.getValueType()) {
case COLLECTION_COMPLEX:
writeComplexValue(metadata, property, type, ((ComplexValue) value).getValue(), selectedPaths, json);
json.writeStartObject();
if (isODataMetadataFull) {
json.writeStringField(Constants.JSON_TYPE, "#" +
type.getFullQualifiedName().getFullQualifiedNameAsString());
}
writeComplexValue(metadata, type, ((ComplexValue) value).getValue(), selectedPaths, json);
json.writeEndObject();
break;
default:
throw new SerializerException("Property type not yet supported!",
@ -735,27 +756,19 @@ public class ODataJsonSerializer extends AbstractODataSerializer {
}
}
protected void writeComplexValue(final ServiceMetadata metadata, final Property complexProperty,
protected void writeComplexValue(final ServiceMetadata metadata,
final EdmComplexType type, final List<Property> properties,
final Set<List<String>> selectedPaths, final JsonGenerator json)
throws IOException, SerializerException {
json.writeStartObject();
final EdmComplexType resolvedType = resolveComplexType(metadata,
type, complexProperty.getType());
if (!isODataMetadataNone && !resolvedType.equals(type) || isODataMetadataFull) {
json.writeStringField(Constants.JSON_TYPE, "#" + complexProperty.getType());
}
for (final String propertyName : resolvedType.getPropertyNames()) {
for (final String propertyName : type.getPropertyNames()) {
final Property property = findProperty(propertyName, properties);
if (selectedPaths == null || ExpandSelectHelper.isSelected(selectedPaths, propertyName)) {
writeProperty(metadata, (EdmProperty) resolvedType.getProperty(propertyName), property,
writeProperty(metadata, (EdmProperty) type.getProperty(propertyName), property,
selectedPaths == null ? null : ExpandSelectHelper.getReducedSelectedPaths(selectedPaths, propertyName),
json);
}
}
json.writeEndObject();
}
private Property findProperty(final String propertyName, final List<Property> properties) {

View File

@ -938,9 +938,7 @@ public class ODataJsonSerializerTest {
InputStream result = serializer
.entityCollection(metadata, entityType, entitySet,
EntityCollectionSerializerOptions.with()
.contextURL(ContextURL.with().entitySet(edmEntitySet)
.selectList(helper.buildContextURLSelectList(entityType, null, null))
.build())
.contextURL(ContextURL.with().entitySet(edmEntitySet).build())
.build()).getContent();
final String resultString = IOUtils.toString(result);
@ -986,7 +984,127 @@ public class ODataJsonSerializerTest {
resultString);
}
@Test
public void selectComplexNestedCollectionOfComplexWithMetadataFull() throws Exception{
final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESCompCollComp");
final EntityCollection entitySet = data.readAll(edmEntitySet);
InputStream result = serializerFullMetadata
.entityCollection(metadata, edmEntitySet.getEntityType(), entitySet,
EntityCollectionSerializerOptions.with()
.contextURL(ContextURL.with().entitySet(edmEntitySet).build())
.build())
.getContent();
final String resultString = IOUtils.toString(result);
final String expectedResult = "{\"@odata.context\":\"$metadata#ESCompCollComp\","
+ "\"@odata.metadataEtag\":\"W/\\\"metadataETag\\\"\","
+ "\"value\":[{\"@odata.type\":\"#olingo.odata.test1.ETCompCollComp\","
+ "\"@odata.id\":\"ESCompCollComp(32767)\","
+ "\"PropertyInt16@odata.type\":\"#Int16\",\"PropertyInt16\":32767,"
+ "\"PropertyComp\":{"
+ "\"@odata.type\":\"#olingo.odata.test1.CTCompCollComp\","
+ "\"CollPropertyComp@odata.type\":\"#Collection(olingo.odata.test1.CTTwoPrim)\","
+ "\"CollPropertyComp\":["
+ "{"
+ "\"@odata.type\":\"#olingo.odata.test1.CTTwoPrim\","
+ "\"PropertyInt16@odata.type\":\"#Int16\","
+"\"PropertyInt16\":555,"
+"\"PropertyString\":\"1 Test Complex in Complex Property\""
+"},{"
+"\"@odata.type\":\"#olingo.odata.test1.CTTwoPrim\","
+"\"PropertyInt16@odata.type\":\"#Int16\","
+"\"PropertyInt16\":666,"
+"\"PropertyString\":\"2 Test Complex in Complex Property\""
+"},{"
+"\"@odata.type\":\"#olingo.odata.test1.CTTwoPrim\","
+"\"PropertyInt16@odata.type\":\"#Int16\","
+"\"PropertyInt16\":777,"
+"\"PropertyString\":\"3 Test Complex in Complex Property\""
+"}]}},{"
+"\"@odata.type\":\"#olingo.odata.test1.ETCompCollComp\","
+"\"@odata.id\":\"ESCompCollComp(12345)\","
+"\"PropertyInt16@odata.type\":\"#Int16\","
+"\"PropertyInt16\":12345,"
+"\"PropertyComp\":{"
+"\"@odata.type\":\"#olingo.odata.test1.CTCompCollComp\","
+"\"CollPropertyComp@odata.type\":\"#Collection(olingo.odata.test1.CTTwoPrim)\","
+"\"CollPropertyComp\":["
+"{"
+"\"@odata.type\":\"#olingo.odata.test1.CTTwoPrim\","
+"\"PropertyInt16@odata.type\":\"#Int16\","
+"\"PropertyInt16\":888,"
+"\"PropertyString\":\"11 Test Complex in Complex Property\""
+"},{"
+"\"@odata.type\":\"#olingo.odata.test1.CTTwoPrim\","
+"\"PropertyInt16@odata.type\":\"#Int16\","
+"\"PropertyInt16\":999,"
+"\"PropertyString\":\"12 Test Complex in Complex Property\""
+"},{"
+"\"@odata.type\":\"#olingo.odata.test1.CTTwoPrim\","
+"\"PropertyInt16@odata.type\":\"#Int16\","
+"\"PropertyInt16\":0,"
+"\"PropertyString\":\"13 Test Complex in Complex Property\""
+"}]}}]}";
Assert.assertEquals(expectedResult, resultString);
}
@Test
public void selectComplexNestedCollectionOfComplexWithMetadataMinimal() throws Exception{
final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESCompCollComp");
final EntityCollection entitySet = data.readAll(edmEntitySet);
InputStream result = serializer
.entityCollection(metadata, edmEntitySet.getEntityType(), entitySet,
EntityCollectionSerializerOptions.with()
.contextURL(ContextURL.with().entitySet(edmEntitySet).build())
.build())
.getContent();
final String resultString = IOUtils.toString(result);
final String expectedResult = "{\"@odata.context\":\"$metadata#ESCompCollComp\","
+ "\"@odata.metadataEtag\":\"W/\\\"metadataETag\\\"\","
+ "\"value\":[{"
+ "\"PropertyInt16\":32767,"
+ "\"PropertyComp\":{"
+ "\"CollPropertyComp\":["
+ "{"
+"\"PropertyInt16\":555,"
+"\"PropertyString\":\"1 Test Complex in Complex Property\""
+"},{"
+"\"PropertyInt16\":666,"
+"\"PropertyString\":\"2 Test Complex in Complex Property\""
+"},{"
+"\"PropertyInt16\":777,"
+"\"PropertyString\":\"3 Test Complex in Complex Property\""
+"}]}},{"
+"\"PropertyInt16\":12345,"
+"\"PropertyComp\":{"
+"\"CollPropertyComp\":["
+"{"
+"\"PropertyInt16\":888,"
+"\"PropertyString\":\"11 Test Complex in Complex Property\""
+"},{"
+"\"PropertyInt16\":999,"
+"\"PropertyString\":\"12 Test Complex in Complex Property\""
+"},{"
+"\"PropertyInt16\":0,"
+"\"PropertyString\":\"13 Test Complex in Complex Property\""
+"}]}}]}";
Assert.assertEquals(expectedResult, resultString);
}
@Test
public void selectComplexNestedCollectionOfComplexWithMetadataNone() throws Exception{
final String METADATA_TEXT = "@odata.";
final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESCompCollComp");
final EntityCollection entitySet = data.readAll(edmEntitySet);
InputStream result = serializerNoMetadata
.entityCollection(metadata, edmEntitySet.getEntityType(), entitySet,
EntityCollectionSerializerOptions.with()
.contextURL(ContextURL.with().entitySet(edmEntitySet).build())
.build())
.getContent();
final String resultString = IOUtils.toString(result);
Assert.assertEquals(false, resultString.contains(METADATA_TEXT));
}
@Test(expected = SerializerException.class)
public void selectMissingId() throws Exception {
final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESAllPrim");