[OLINGO-530] Additional Tests for deserialization

This commit is contained in:
Christian Amend 2015-01-16 17:35:18 +01:00
parent 121c7d7129
commit 2591c86c82
3 changed files with 129 additions and 14 deletions

View File

@ -33,7 +33,7 @@ public class DeserializerException extends ODataTranslatedException {
/** parameter: format */ UNSUPPORTED_FORMAT,
JSON_SYNTAX_EXCEPTION,
/** parameter: propertyName */ INVALID_NULL_PROPERTY,
TREE_NOT_EMPTY,
/** parameter: keyName */ UNKOWN_CONTENT,
/** parameter: propertyName */ INVALID_VALUE_FOR_PROPERTY,
/** parameter: propertyName */ INVALID_TYPE_FOR_PROPERTY,
VALUE_ARRAY_NOT_PRESENT,

View File

@ -104,7 +104,7 @@ public class ODataJsonDeserializer implements ODataDeserializer {
if (tree.size() != 0) {
throw new DeserializerException("Tree should be empty but still has content left.",
DeserializerException.MessageKeys.TREE_NOT_EMPTY);
DeserializerException.MessageKeys.UNKOWN_CONTENT);
}
return entitySet;
}
@ -242,7 +242,7 @@ public class ODataJsonDeserializer implements ODataDeserializer {
if (field.getKey().endsWith(Constants.JSON_NAVIGATION_LINK)
|| field.getKey().endsWith(Constants.JSON_ASSOCIATION_LINK) || field.getKey().endsWith(Constants.JSON_TYPE)) {
//navigation links, association links and type information have to be ignored in requests.
// navigation links, association links and type information have to be ignored in requests.
toRemove.add(field.getKey());
}
}
@ -251,8 +251,9 @@ public class ODataJsonDeserializer implements ODataDeserializer {
tree.remove(toRemove);
if (tree.size() != 0) {
throw new DeserializerException("Tree should be empty but still has content left.",
DeserializerException.MessageKeys.TREE_NOT_EMPTY);
final String unkownField = tree.fieldNames().next();
throw new DeserializerException("Tree should be empty but still has content left: " + unkownField,
DeserializerException.MessageKeys.UNKOWN_CONTENT, unkownField);
}
return entity;
@ -371,12 +372,26 @@ public class ODataJsonDeserializer implements ODataDeserializer {
valueArray.add(value);
// If navigationProperties are present we have to consume them and create a LinkedComplexValue Object
// TODO: Complex Type Navigation Deserialization
// read and add all annotations
// TODO: read Complex Type Annotations
final List<String> toRemove = new ArrayList<String>();
Iterator<Entry<String, JsonNode>> fieldsIterator = arrayElement.fields();
// TODO: Add custom annotation support
while (fieldsIterator.hasNext()) {
Map.Entry<String, JsonNode> field = (Map.Entry<String, JsonNode>) fieldsIterator.next();
if (field.getKey().endsWith(Constants.JSON_NAVIGATION_LINK)
|| field.getKey().endsWith(Constants.JSON_ASSOCIATION_LINK)
|| field.getKey().endsWith(Constants.JSON_TYPE)) {
// navigation links, association links and type information have to be ignored in requests.
toRemove.add(field.getKey());
}
}
// remove here to avoid iterator issues.
((ObjectNode) arrayElement).remove(toRemove);
// Afterwards the node must be empty
if (arrayElement.size() != 0) {
throw new DeserializerException("Tree should be empty but still has content left.",
DeserializerException.MessageKeys.TREE_NOT_EMPTY);
final String unkownField = arrayElement.fieldNames().next();
throw new DeserializerException("Tree should be empty but still has content left: " + unkownField,
DeserializerException.MessageKeys.UNKOWN_CONTENT, unkownField);
}
}
property.setValue(ValueType.COLLECTION_COMPLEX, valueArray);
@ -406,12 +421,26 @@ public class ODataJsonDeserializer implements ODataDeserializer {
// read and add all navigation properties
// TODO: Complex Type Navigation Deserialization
// read and add all annotations
// TODO: read Complex Type Annotations
final List<String> toRemove = new ArrayList<String>();
Iterator<Entry<String, JsonNode>> fieldsIterator = jsonNode.fields();
// TODO: Add custom annotation support
while (fieldsIterator.hasNext()) {
Map.Entry<String, JsonNode> field = (Map.Entry<String, JsonNode>) fieldsIterator.next();
if (field.getKey().endsWith(Constants.JSON_NAVIGATION_LINK)
|| field.getKey().endsWith(Constants.JSON_ASSOCIATION_LINK)
|| field.getKey().endsWith(Constants.JSON_TYPE)) {
// navigation links, association links and type information have to be ignored in requests.
toRemove.add(field.getKey());
}
}
// remove here to avoid iterator issues.
((ObjectNode) jsonNode).remove(toRemove);
// Afterwards the node must be empty
if (jsonNode.size() != 0) {
throw new DeserializerException("Tree should be empty but still has content left.",
DeserializerException.MessageKeys.TREE_NOT_EMPTY);
final String unkownField = jsonNode.fieldNames().next();
throw new DeserializerException("Tree should be empty but still has content left: " + unkownField,
DeserializerException.MessageKeys.UNKOWN_CONTENT, unkownField);
}
break;
default:

View File

@ -265,7 +265,7 @@ public class ODataJsonDeserializerEntityTest extends AbstractODataDeserializerTe
}
@Test
public void ingoreSomeAnnotations() throws Exception {
public void ingoreSomeAnnotationsInEntityTypes() throws Exception {
// We have to ignore @odata.navigation, @odata.association and @odata.type annotations on server side
String entityString =
"{\"PropertyInt16\":32767,"
@ -279,6 +279,25 @@ public class ODataJsonDeserializerEntityTest extends AbstractODataDeserializerTe
deserializer.entity(stream, edm.getEntityType(new FullQualifiedName("Namespace1_Alias", "ETAllPrim")));
}
@Test
public void ingoreSomeAnnotationsInComplexTypes() throws Exception {
final String entityString = "{"
+ "\"PropertyInt16\":32767,"
+ "\"CollPropertyString\":"
+ "[\"Employee1@company.example\",\"Employee2@company.example\",\"Employee3@company.example\"],"
+ "\"PropertyComp\":{\"PropertyInt16\":111,\"Navigation@odata.navigationLink\": 12," +
"\"Association@odata.associationLink\": 12,\"PropertyString@odata.type\": 12,\"PropertyString\":\"TEST A\"},"
+ "\"CollPropertyComp\":["
+ "{\"PropertyInt16\":123,\"PropertyString\":\"TEST 1\"},"
+ "{\"PropertyInt16\":456,\"Navigation@odata.navigationLink\": 12," +
"\"Association@odata.associationLink\": 12,\"PropertyString@odata.type\": 12,\"PropertyString\":\"TEST 2\"},"
+ "{\"PropertyInt16\":789,\"PropertyString\":\"TEST 3\"}]}";
InputStream stream = new ByteArrayInputStream(entityString.getBytes());
ODataDeserializer deserializer = OData.newInstance().createDeserializer(ODataFormat.JSON);
deserializer.entity(stream, edm.getEntityType(new FullQualifiedName("Namespace1_Alias", "ETMixPrimCollComp")));
}
// ---------------------------------- Negative Tests -----------------------------------------------------------
@Test(expected = DeserializerException.class)
@ -457,4 +476,71 @@ public class ODataJsonDeserializerEntityTest extends AbstractODataDeserializerTe
}
}
@Test(expected = DeserializerException.class)
public void unkownContentInEntity() throws Exception {
final String entityString = "{"
+ "\"PropertyInt16\":32767,"
+ "\"unknown\": 12,"
+ "\"CollPropertyString\":"
+ "[\"Employee1@company.example\",\"Employee2@company.example\",\"Employee3@company.example\"],"
+ "\"PropertyComp\":{\"PropertyInt16\":111,\"PropertyString\":\"TEST A\"},"
+ "\"CollPropertyComp\":["
+ "{\"PropertyInt16\":123,\"PropertyString\":\"TEST 1\"},"
+ "{\"PropertyInt16\":456,\"PropertyString\":\"TEST 2\"},"
+ "{\"PropertyInt16\":789,\"PropertyString\":\"TEST 3\"}]}";
InputStream stream = new ByteArrayInputStream(entityString.getBytes());
try {
ODataDeserializer deserializer = OData.newInstance().createDeserializer(ODataFormat.JSON);
deserializer.entity(stream, edm.getEntityType(new FullQualifiedName("Namespace1_Alias", "ETMixPrimCollComp")));
} catch (DeserializerException e) {
assertEquals(DeserializerException.MessageKeys.UNKOWN_CONTENT, e.getMessageKey());
throw e;
}
}
@Test(expected = DeserializerException.class)
public void unkownContentInComplexProperty() throws Exception {
final String entityString = "{"
+ "\"PropertyInt16\":32767,"
+ "\"CollPropertyString\":"
+ "[\"Employee1@company.example\",\"Employee2@company.example\",\"Employee3@company.example\"],"
+ "\"PropertyComp\":{\"PropertyInt16\":111,\"unknown\": 12,\"PropertyString\":\"TEST A\"},"
+ "\"CollPropertyComp\":["
+ "{\"PropertyInt16\":123,\"PropertyString\":\"TEST 1\"},"
+ "{\"PropertyInt16\":456,\"PropertyString\":\"TEST 2\"},"
+ "{\"PropertyInt16\":789,\"PropertyString\":\"TEST 3\"}]}";
InputStream stream = new ByteArrayInputStream(entityString.getBytes());
try {
ODataDeserializer deserializer = OData.newInstance().createDeserializer(ODataFormat.JSON);
deserializer.entity(stream, edm.getEntityType(new FullQualifiedName("Namespace1_Alias", "ETMixPrimCollComp")));
} catch (DeserializerException e) {
assertEquals(DeserializerException.MessageKeys.UNKOWN_CONTENT, e.getMessageKey());
throw e;
}
}
@Test(expected = DeserializerException.class)
public void unkownContentInComplexCollectionProperty() throws Exception {
final String entityString = "{"
+ "\"PropertyInt16\":32767,"
+ "\"CollPropertyString\":"
+ "[\"Employee1@company.example\",\"Employee2@company.example\",\"Employee3@company.example\"],"
+ "\"PropertyComp\":{\"PropertyInt16\":111,\"PropertyString\":\"TEST A\"},"
+ "\"CollPropertyComp\":["
+ "{\"PropertyInt16\":123,\"PropertyString\":\"TEST 1\"},"
+ "{\"PropertyInt16\":456,\"unknown\": 12,\"PropertyString\":\"TEST 2\"},"
+ "{\"PropertyInt16\":789,\"PropertyString\":\"TEST 3\"}]}";
InputStream stream = new ByteArrayInputStream(entityString.getBytes());
try {
ODataDeserializer deserializer = OData.newInstance().createDeserializer(ODataFormat.JSON);
deserializer.entity(stream, edm.getEntityType(new FullQualifiedName("Namespace1_Alias", "ETMixPrimCollComp")));
} catch (DeserializerException e) {
assertEquals(DeserializerException.MessageKeys.UNKOWN_CONTENT, e.getMessageKey());
throw e;
}
}
}