[OLINGO-235] reader option to switch off validation of facets Change-Id: Ie35a7b5ce4e72f0d21a546e35768a36718635f06
Signed-off-by: Stephan Klevenz <[email protected]> Project: http://git-wip-us.apache.org/repos/asf/olingo-odata2/repo Commit: http://git-wip-us.apache.org/repos/asf/olingo-odata2/commit/2211ce41 Tree: http://git-wip-us.apache.org/repos/asf/olingo-odata2/tree/2211ce41 Diff: http://git-wip-us.apache.org/repos/asf/olingo-odata2/diff/2211ce41 Branch: refs/heads/Olingo-129_PocJpaDataStore Commit: 2211ce410c4bd2c45181037b931244ad6504efd9 Parents: e1b6ead Author: Klaus Straubinger <[email protected]> Authored: Fri Apr 11 14:03:03 2014 +0200 Committer: Stephan Klevenz <[email protected]> Committed: Wed Apr 16 16:28:30 2014 +0200 ---------------------------------------------------------------------- .../api/ep/EntityProviderReadProperties.java | 19 +++- .../core/ep/consumer/JsonEntryConsumer.java | 14 ++- .../core/ep/consumer/JsonPropertyConsumer.java | 33 +++--- .../core/ep/consumer/XmlEntityConsumer.java | 5 +- .../core/ep/consumer/XmlEntryConsumer.java | 22 ++-- .../core/ep/consumer/XmlPropertyConsumer.java | 32 +++--- .../ep/consumer/JsonPropertyConsumerTest.java | 53 ++++++++-- .../ep/consumer/XmlPropertyConsumerTest.java | 103 ++++++++++++++----- .../odata2/fit/basic/ServiceResolutionTest.java | 18 ++++ 9 files changed, 220 insertions(+), 79 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/2211ce41/odata2-lib/odata-api/src/main/java/org/apache/olingo/odata2/api/ep/EntityProviderReadProperties.java ---------------------------------------------------------------------- diff --git a/odata2-lib/odata-api/src/main/java/org/apache/olingo/odata2/api/ep/EntityProviderReadProperties.java b/odata2-lib/odata-api/src/main/java/org/apache/olingo/odata2/api/ep/EntityProviderReadProperties.java index a012487..3df4758 100644 --- a/odata2-lib/odata-api/src/main/java/org/apache/olingo/odata2/api/ep/EntityProviderReadProperties.java +++ b/odata2-lib/odata-api/src/main/java/org/apache/olingo/odata2/api/ep/EntityProviderReadProperties.java @@ -31,10 +31,10 @@ import org.apache.olingo.odata2.api.ep.callback.OnReadInlineContent; * <ul> * <li>the <code>mergeSemantic</code></li> * <li>the <code>callback for inlined navigation properties</code></li> - * <li>and the <code>type mappings</code></li> + * <li>the <code>type mappings</code></li> + * <li>and <code>validatingFacets</code></li> * </ul> * </p> - * */ public class EntityProviderReadProperties { /** Callback which is necessary if entity contains inlined navigation properties. */ @@ -51,6 +51,9 @@ public class EntityProviderReadProperties { * Supported mappings are documented in {@link org.apache.olingo.odata2.api.edm.EdmSimpleType}. */ final private Map<String, Object> typeMappings; + /** whether the constraints expressed in properties' facets are validated */ + private boolean validatingFacets = true; + final private Map<String, String> validatedPrefix2NamespaceUri; private EntityProviderReadProperties() { @@ -90,8 +93,12 @@ public class EntityProviderReadProperties { return merge; } + public boolean isValidatingFacets() { + return validatingFacets; + } + /** - * + * Builder for {@link EntityProviderReadProperties}. */ public static class EntityProviderReadPropertiesBuilder { private final EntityProviderReadProperties properties = new EntityProviderReadProperties(); @@ -103,6 +110,7 @@ public class EntityProviderReadProperties { properties.callback = propertiesFrom.callback; addValidatedPrefixes(propertiesFrom.validatedPrefix2NamespaceUri); addTypeMappings(propertiesFrom.typeMappings); + properties.validatingFacets = propertiesFrom.validatingFacets; } /** @@ -134,6 +142,11 @@ public class EntityProviderReadProperties { return this; } + public EntityProviderReadPropertiesBuilder isValidatingFacets(final boolean validatingFacets) { + properties.validatingFacets = validatingFacets; + return this; + } + public EntityProviderReadProperties build() { return properties; } http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/2211ce41/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/consumer/JsonEntryConsumer.java ---------------------------------------------------------------------- diff --git a/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/consumer/JsonEntryConsumer.java b/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/consumer/JsonEntryConsumer.java index 11350b1..5bae8e7 100644 --- a/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/consumer/JsonEntryConsumer.java +++ b/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/consumer/JsonEntryConsumer.java @@ -168,8 +168,8 @@ public class JsonEntryConsumer { ensureODataEntryExists(); EntityPropertyInfo propertyInfo = eia.getPropertyInfo(name); if (propertyInfo != null) { - JsonPropertyConsumer jpc = new JsonPropertyConsumer(); - Object propertyValue = jpc.readPropertyValue(reader, propertyInfo, typeMappings.get(name)); + Object propertyValue = new JsonPropertyConsumer() + .readPropertyValue(reader, propertyInfo, typeMappings.get(name), readProperties); if (properties.containsKey(name)) { throw new EntityProviderException(EntityProviderException.DOUBLE_PROPERTY.addContent(name)); } @@ -303,7 +303,10 @@ public class JsonEntryConsumer { try { if (callback == null) { inlineReadProperties = - EntityProviderReadProperties.init().mergeSemantic(readProperties.getMergeSemantic()).build(); + EntityProviderReadProperties.init() + .mergeSemantic(readProperties.getMergeSemantic()) + .isValidatingFacets(readProperties.isValidatingFacets()) + .build(); } else { inlineReadProperties = callback.receiveReadProperties(readProperties, navigationProperty); @@ -348,7 +351,10 @@ public class JsonEntryConsumer { EntityProviderReadProperties inlineReadProperties; if (callback == null) { inlineReadProperties = - EntityProviderReadProperties.init().mergeSemantic(readProperties.getMergeSemantic()).build(); + EntityProviderReadProperties.init() + .mergeSemantic(readProperties.getMergeSemantic()) + .isValidatingFacets(readProperties.isValidatingFacets()) + .build(); } else { try { inlineReadProperties = callback.receiveReadProperties(readProperties, navigationProperty); http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/2211ce41/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/consumer/JsonPropertyConsumer.java ---------------------------------------------------------------------- diff --git a/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/consumer/JsonPropertyConsumer.java b/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/consumer/JsonPropertyConsumer.java index af1262d..1c3ef59 100644 --- a/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/consumer/JsonPropertyConsumer.java +++ b/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/consumer/JsonPropertyConsumer.java @@ -24,6 +24,7 @@ import java.util.Map; import org.apache.olingo.odata2.api.edm.Edm; import org.apache.olingo.odata2.api.edm.EdmException; +import org.apache.olingo.odata2.api.edm.EdmFacets; import org.apache.olingo.odata2.api.edm.EdmLiteralKind; import org.apache.olingo.odata2.api.edm.EdmProperty; import org.apache.olingo.odata2.api.edm.EdmSimpleType; @@ -39,7 +40,7 @@ import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonToken; /** - * + * JSON property consumer. */ public class JsonPropertyConsumer { @@ -55,10 +56,10 @@ public class JsonPropertyConsumer { if (FormatJson.D.equals(nextName)) { reader.beginObject(); nextName = reader.nextName(); - handleName(reader, typeMappings, entityPropertyInfo, result, nextName); + handleName(reader, typeMappings, entityPropertyInfo, readProperties, result, nextName); reader.endObject(); } else { - handleName(reader, typeMappings, entityPropertyInfo, result, nextName); + handleName(reader, typeMappings, entityPropertyInfo, readProperties, result, nextName); } reader.endObject(); @@ -78,8 +79,8 @@ public class JsonPropertyConsumer { } private void handleName(final JsonReader reader, final Map<String, Object> typeMappings, - final EntityPropertyInfo entityPropertyInfo, final Map<String, Object> result, final String nextName) - throws EntityProviderException { + final EntityPropertyInfo entityPropertyInfo, final EntityProviderReadProperties readProperties, + final Map<String, Object> result, final String nextName) throws EntityProviderException { if (!entityPropertyInfo.getName().equals(nextName)) { throw new EntityProviderException(EntityProviderException.ILLEGAL_ARGUMENT.addContent(nextName)); } @@ -87,16 +88,16 @@ public class JsonPropertyConsumer { if (typeMappings != null) { mapping = typeMappings.get(nextName); } - Object propertyValue = readPropertyValue(reader, entityPropertyInfo, mapping); + Object propertyValue = readPropertyValue(reader, entityPropertyInfo, mapping, readProperties); result.put(nextName, propertyValue); } protected Object readPropertyValue(final JsonReader reader, final EntityPropertyInfo entityPropertyInfo, - final Object typeMapping) throws EntityProviderException { + final Object typeMapping, final EntityProviderReadProperties readProperties) throws EntityProviderException { try { return entityPropertyInfo.isComplex() ? - readComplexProperty(reader, (EntityComplexPropertyInfo) entityPropertyInfo, typeMapping) : - readSimpleProperty(reader, entityPropertyInfo, typeMapping); + readComplexProperty(reader, (EntityComplexPropertyInfo) entityPropertyInfo, typeMapping, readProperties) : + readSimpleProperty(reader, entityPropertyInfo, typeMapping, readProperties); } catch (final EdmException e) { throw new EntityProviderException(EntityProviderException.EXCEPTION_OCCURRED.addContent(e.getClass() .getSimpleName()), e); @@ -107,7 +108,8 @@ public class JsonPropertyConsumer { } private Object readSimpleProperty(final JsonReader reader, final EntityPropertyInfo entityPropertyInfo, - final Object typeMapping) throws EdmException, EntityProviderException, IOException { + final Object typeMapping, final EntityProviderReadProperties readProperties) + throws EdmException, EntityProviderException, IOException { final EdmSimpleType type = (EdmSimpleType) entityPropertyInfo.getType(); Object value = null; final JsonToken tokenType = reader.peek(); @@ -148,15 +150,18 @@ public class JsonPropertyConsumer { } final Class<?> typeMappingClass = typeMapping == null ? type.getDefaultType() : (Class<?>) typeMapping; - return type.valueOfString((String) value, EdmLiteralKind.JSON, entityPropertyInfo.getFacets(), typeMappingClass); + final EdmFacets facets = readProperties == null || readProperties.isValidatingFacets() ? + entityPropertyInfo.getFacets() : null; + return type.valueOfString((String) value, EdmLiteralKind.JSON, facets, typeMappingClass); } @SuppressWarnings("unchecked") private Object readComplexProperty(final JsonReader reader, final EntityComplexPropertyInfo complexPropertyInfo, - final Object typeMapping) throws EdmException, EntityProviderException, IOException { + final Object typeMapping, final EntityProviderReadProperties readProperties) + throws EdmException, EntityProviderException, IOException { if (reader.peek().equals(JsonToken.NULL)) { reader.nextNull(); - if (complexPropertyInfo.isMandatory()) { + if ((readProperties == null || readProperties.isValidatingFacets()) && complexPropertyInfo.isMandatory()) { throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY_VALUE.addContent(complexPropertyInfo .getName())); } @@ -200,7 +205,7 @@ public class JsonPropertyConsumer { if (childPropertyInfo == null) { throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY.addContent(childName)); } - Object childData = readPropertyValue(reader, childPropertyInfo, mapping.get(childName)); + Object childData = readPropertyValue(reader, childPropertyInfo, mapping.get(childName), readProperties); if (data.containsKey(childName)) { throw new EntityProviderException(EntityProviderException.DOUBLE_PROPERTY.addContent(childName)); } http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/2211ce41/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/consumer/XmlEntityConsumer.java ---------------------------------------------------------------------- diff --git a/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/consumer/XmlEntityConsumer.java b/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/consumer/XmlEntityConsumer.java index c7fca98..5d73862 100644 --- a/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/consumer/XmlEntityConsumer.java +++ b/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/consumer/XmlEntityConsumer.java @@ -115,9 +115,8 @@ public class XmlEntityConsumer { try { reader = XmlHelper.createStreamReader(content); - Map<String, Object> result = - xec.readProperty(reader, edmProperty, properties.getMergeSemantic(), properties.getTypeMappings()); - return result; + return xec.readProperty(reader, edmProperty, properties.getMergeSemantic(), properties.getTypeMappings(), + properties); } catch (EntityProviderException e) { cachedException = e; throw cachedException; http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/2211ce41/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/consumer/XmlEntryConsumer.java ---------------------------------------------------------------------- diff --git a/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/consumer/XmlEntryConsumer.java b/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/consumer/XmlEntryConsumer.java index 6b9cd5f..a6111d1 100644 --- a/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/consumer/XmlEntryConsumer.java +++ b/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/consumer/XmlEntryConsumer.java @@ -130,17 +130,18 @@ public class XmlEntryConsumer { } else if (FormatXml.ATOM_LINK.equals(currentHandledStartTagName)) { readLink(reader, eia, readProperties); } else if (FormatXml.ATOM_CONTENT.equals(currentHandledStartTagName)) { - readContent(reader, eia); + readContent(reader, eia, readProperties); } else if (FormatXml.M_PROPERTIES.equals(currentHandledStartTagName)) { - readProperties(reader, eia); + readProperties(reader, eia, readProperties); } else if (!readProperties.getMergeSemantic()) { - readCustomElement(reader, currentHandledStartTagName, eia); + readCustomElement(reader, currentHandledStartTagName, eia, readProperties); } else { skipStartedTag(reader); } } - private void readCustomElement(final XMLStreamReader reader, final String tagName, final EntityInfoAggregator eia) + private void readCustomElement(final XMLStreamReader reader, final String tagName, final EntityInfoAggregator eia, + final EntityProviderReadProperties readProperties) throws EdmException, EntityProviderException, XMLStreamException { EntityPropertyInfo targetPathInfo = eia.getTargetPathInfo(tagName); NamespaceContext nsctx = reader.getNamespaceContext(); @@ -165,7 +166,8 @@ public class XmlEntryConsumer { final EntityPropertyInfo propertyInfo = getValidatedPropertyInfo(eia, tagName); final Class<?> typeMapping = typeMappings.getMappingClass(propertyInfo.getName()); final EdmSimpleType type = (EdmSimpleType) propertyInfo.getType(); - final Object value = type.valueOfString(text, EdmLiteralKind.DEFAULT, propertyInfo.getFacets(), + final Object value = type.valueOfString(text, EdmLiteralKind.DEFAULT, + readProperties == null || readProperties.isValidatingFacets() ? propertyInfo.getFacets() : null, typeMapping == null ? type.getDefaultType() : typeMapping); properties.put(tagName, value); } @@ -524,7 +526,8 @@ public class XmlEntryConsumer { } } - private void readContent(final XMLStreamReader reader, final EntityInfoAggregator eia) + private void readContent(final XMLStreamReader reader, final EntityInfoAggregator eia, + final EntityProviderReadProperties readProperties) throws EntityProviderException, XMLStreamException, EdmException { reader.require(XMLStreamConstants.START_ELEMENT, Edm.NAMESPACE_ATOM_2005, FormatXml.ATOM_CONTENT); @@ -534,7 +537,7 @@ public class XmlEntryConsumer { reader.nextTag(); if (reader.isStartElement() && reader.getLocalName().equals(FormatXml.M_PROPERTIES)) { - readProperties(reader, eia); + readProperties(reader, eia, readProperties); } else if (reader.isEndElement()) { reader.require(XMLStreamConstants.END_ELEMENT, Edm.NAMESPACE_ATOM_2005, FormatXml.ATOM_CONTENT); } else { @@ -553,7 +556,8 @@ public class XmlEntryConsumer { reader.require(XMLStreamConstants.END_ELEMENT, Edm.NAMESPACE_ATOM_2005, FormatXml.ATOM_ID); } - private void readProperties(final XMLStreamReader reader, final EntityInfoAggregator entitySet) + private void readProperties(final XMLStreamReader reader, final EntityInfoAggregator entitySet, + final EntityProviderReadProperties readProperties) throws XMLStreamException, EdmException, EntityProviderException { // validate namespace reader.require(XMLStreamConstants.START_ELEMENT, Edm.NAMESPACE_M_2007_08, FormatXml.M_PROPERTIES); @@ -580,7 +584,7 @@ public class XmlEntryConsumer { throw new EntityProviderException(EntityProviderException.DOUBLE_PROPERTY.addContent(closeTag)); } property = getValidatedPropertyInfo(entitySet, closeTag); - final Object value = xpc.readStartedElement(reader, property, typeMappings); + final Object value = xpc.readStartedElement(reader, property, typeMappings, readProperties); properties.put(closeTag, value); closeTag = null; } http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/2211ce41/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/consumer/XmlPropertyConsumer.java ---------------------------------------------------------------------- diff --git a/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/consumer/XmlPropertyConsumer.java b/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/consumer/XmlPropertyConsumer.java index 28cacef..3887333 100644 --- a/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/consumer/XmlPropertyConsumer.java +++ b/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/consumer/XmlPropertyConsumer.java @@ -33,6 +33,7 @@ import org.apache.olingo.odata2.api.edm.EdmProperty; import org.apache.olingo.odata2.api.edm.EdmSimpleType; import org.apache.olingo.odata2.api.edm.EdmSimpleTypeException; import org.apache.olingo.odata2.api.ep.EntityProviderException; +import org.apache.olingo.odata2.api.ep.EntityProviderReadProperties; import org.apache.olingo.odata2.core.ep.aggregator.EntityComplexPropertyInfo; import org.apache.olingo.odata2.core.ep.aggregator.EntityInfoAggregator; import org.apache.olingo.odata2.core.ep.aggregator.EntityPropertyInfo; @@ -40,27 +41,27 @@ import org.apache.olingo.odata2.core.ep.aggregator.EntityTypeMapping; import org.apache.olingo.odata2.core.ep.util.FormatXml; /** - * + * XML property consumer. */ public class XmlPropertyConsumer { protected static final String TRUE = "true"; protected static final String FALSE = "false"; - public Map<String, Object> - readProperty(final XMLStreamReader reader, final EdmProperty property, final boolean merge) - throws EntityProviderException { - return readProperty(reader, property, merge, null); + public Map<String, Object> readProperty(final XMLStreamReader reader, final EdmProperty property, + final boolean merge, final EntityProviderReadProperties readProperties) throws EntityProviderException { + return readProperty(reader, property, merge, null, readProperties); } public Map<String, Object> readProperty(final XMLStreamReader reader, final EdmProperty property, - final boolean merge, final Map<String, Object> typeMappings) throws EntityProviderException { + final boolean merge, final Map<String, Object> typeMappings, final EntityProviderReadProperties readProperties) + throws EntityProviderException { EntityPropertyInfo eia = EntityInfoAggregator.create(property); try { reader.next(); - Object value = readStartedElement(reader, eia, EntityTypeMapping.create(typeMappings)); + Object value = readStartedElement(reader, eia, EntityTypeMapping.create(typeMappings), readProperties); if (eia.isComplex() && merge) { mergeWithDefaultValues(value, eia); @@ -110,7 +111,8 @@ public class XmlPropertyConsumer { } protected Object readStartedElement(final XMLStreamReader reader, final EntityPropertyInfo propertyInfo, - final EntityTypeMapping typeMappings) throws EntityProviderException, EdmException { + final EntityTypeMapping typeMappings, final EntityProviderReadProperties readProperties) + throws EntityProviderException, EdmException { final String name = propertyInfo.getName(); Object result = null; @@ -123,7 +125,7 @@ public class XmlPropertyConsumer { } if (TRUE.equals(nullAttribute)) { - if (propertyInfo.isMandatory()) { + if ((readProperties == null || readProperties.isValidatingFacets()) && propertyInfo.isMandatory()) { throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY_VALUE.addContent(name)); } reader.nextTag(); @@ -147,13 +149,14 @@ public class XmlPropertyConsumer { if (childProperty == null) { throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY.addContent(childName)); } - final Object value = readStartedElement(reader, childProperty, typeMappings.getEntityTypeMapping(name)); + final Object value = readStartedElement(reader, childProperty, typeMappings.getEntityTypeMapping(name), + readProperties); name2Value.put(childName, value); reader.nextTag(); } result = name2Value; } else { - result = convert(propertyInfo, reader.getElementText(), typeMappings.getMappingClass(name)); + result = convert(propertyInfo, reader.getElementText(), typeMappings.getMappingClass(name), readProperties); } reader.require(XMLStreamConstants.END_ELEMENT, Edm.NAMESPACE_D_2007_08, name); @@ -164,10 +167,11 @@ public class XmlPropertyConsumer { } } - private Object convert(final EntityPropertyInfo property, final String value, final Class<?> typeMapping) - throws EdmSimpleTypeException { + private Object convert(final EntityPropertyInfo property, final String value, final Class<?> typeMapping, + final EntityProviderReadProperties readProperties) throws EdmSimpleTypeException { final EdmSimpleType type = (EdmSimpleType) property.getType(); - return type.valueOfString(value, EdmLiteralKind.DEFAULT, property.getFacets(), + return type.valueOfString(value, EdmLiteralKind.DEFAULT, + readProperties == null || readProperties.isValidatingFacets() ? property.getFacets() : null, typeMapping == null ? type.getDefaultType() : typeMapping); } } http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/2211ce41/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/ep/consumer/JsonPropertyConsumerTest.java ---------------------------------------------------------------------- diff --git a/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/ep/consumer/JsonPropertyConsumerTest.java b/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/ep/consumer/JsonPropertyConsumerTest.java index b52df8b..7d51314 100644 --- a/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/ep/consumer/JsonPropertyConsumerTest.java +++ b/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/ep/consumer/JsonPropertyConsumerTest.java @@ -186,7 +186,7 @@ public class JsonPropertyConsumerTest extends BaseTest { reader.nextName(); JsonPropertyConsumer jpc = new JsonPropertyConsumer(); - Object value = jpc.readPropertyValue(reader, entityPropertyInfo, null); + Object value = jpc.readPropertyValue(reader, entityPropertyInfo, null, null); assertEquals("Team 1", value); } @@ -208,6 +208,30 @@ public class JsonPropertyConsumerTest extends BaseTest { assertEquals(propertyValue, resultMap.get("Name")); } + @Test(expected = EntityProviderException.class) + public void simplePropertyViolatingValidation() throws Exception { + EdmProperty property = (EdmProperty) MockFacade.getMockEdm().getEntityType("RefScenario", "Room") + .getProperty("Name"); + EdmFacets facets = mock(EdmFacets.class); + when(facets.getMaxLength()).thenReturn(10); + when(property.getFacets()).thenReturn(facets); + new JsonPropertyConsumer().readPropertyStandalone(prepareReader("{\"Name\":\"TooLongName\"}"), property, null); + } + + @Test + public void simplePropertyIgnoringValidation() throws Exception { + EdmProperty property = (EdmProperty) MockFacade.getMockEdm().getEntityType("RefScenario", "Room") + .getProperty("Name"); + EdmFacets facets = mock(EdmFacets.class); + when(facets.getMaxLength()).thenReturn(10); + when(property.getFacets()).thenReturn(facets); + final EntityProviderReadProperties readProperties = mock(EntityProviderReadProperties.class); + final Map<String, Object> resultMap = new JsonPropertyConsumer() + .readPropertyStandalone(prepareReader("{\"Name\":\"TooLongName\"}"), property, readProperties); + assertTrue(resultMap.containsKey("Name")); + assertEquals("TooLongName", resultMap.get("Name")); + } + @Test public void simplePropertyNull() throws Exception { JsonReader reader = prepareReader("{\"Name\":null}"); @@ -405,7 +429,7 @@ public class JsonPropertyConsumerTest extends BaseTest { JsonPropertyConsumer jpc = new JsonPropertyConsumer(); @SuppressWarnings("unchecked") - Map<String, Object> result = (Map<String, Object>) jpc.readPropertyValue(reader, entityPropertyInfo, null); + Map<String, Object> result = (Map<String, Object>) jpc.readPropertyValue(reader, entityPropertyInfo, null, null); assertEquals(2, result.size()); assertEquals("Heidelberg", result.get("CityName")); @@ -422,7 +446,7 @@ public class JsonPropertyConsumerTest extends BaseTest { JsonPropertyConsumer jpc = new JsonPropertyConsumer(); @SuppressWarnings("unchecked") - Map<String, Object> result = (Map<String, Object>) jpc.readPropertyValue(reader, entityPropertyInfo, null); + Map<String, Object> result = (Map<String, Object>) jpc.readPropertyValue(reader, entityPropertyInfo, null, null); assertEquals(2, result.size()); assertEquals("Heidelberg", result.get("CityName")); @@ -443,7 +467,7 @@ public class JsonPropertyConsumerTest extends BaseTest { JsonPropertyConsumer jpc = new JsonPropertyConsumer(); @SuppressWarnings("unchecked") - Map<String, Object> result = (Map<String, Object>) jpc.readPropertyValue(reader, entityPropertyInfo, null); + Map<String, Object> result = (Map<String, Object>) jpc.readPropertyValue(reader, entityPropertyInfo, null, null); assertEquals(2, result.size()); assertEquals("Germany", result.get("Country")); @@ -560,6 +584,23 @@ public class JsonPropertyConsumerTest extends BaseTest { } @Test + public void complexPropertyNullValueNotAllowedButNotValidated() throws Exception { + final EdmProperty property = (EdmProperty) MockFacade.getMockEdm().getDefaultEntityContainer() + .getEntitySet("Employees").getEntityType().getProperty("Location"); + EdmFacets facets = mock(EdmFacets.class); + when(facets.isNullable()).thenReturn(false); + when(property.getFacets()).thenReturn(facets); + final EntityProviderReadProperties readProperties = mock(EntityProviderReadProperties.class); + + final Map<String, Object> propertyData = new JsonPropertyConsumer() + .readPropertyStandalone(prepareReader("{\"Location\":null}"), property, readProperties); + assertNotNull(propertyData); + assertEquals(1, propertyData.size()); + assertTrue(propertyData.containsKey("Location")); + assertNull(propertyData.get("Location")); + } + + @Test public void complexPropertyEmpty() throws Exception { final String cityProperty = "{\"d\":{\"City\":{}}}"; JsonReader reader = prepareReader(cityProperty); @@ -584,7 +625,7 @@ public class JsonPropertyConsumerTest extends BaseTest { (EdmProperty) MockFacade.getMockEdm().getComplexType("RefScenario", "c_Location").getProperty("City"); EntityComplexPropertyInfo entityPropertyInfo = (EntityComplexPropertyInfo) EntityInfoAggregator.create(property); - new JsonPropertyConsumer().readPropertyValue(reader, entityPropertyInfo, null); + new JsonPropertyConsumer().readPropertyValue(reader, entityPropertyInfo, null, null); } @Test(expected = EntityProviderException.class) @@ -596,7 +637,7 @@ public class JsonPropertyConsumerTest extends BaseTest { (EdmProperty) MockFacade.getMockEdm().getComplexType("RefScenario", "c_Location").getProperty("City"); EntityComplexPropertyInfo entityPropertyInfo = (EntityComplexPropertyInfo) EntityInfoAggregator.create(property); - new JsonPropertyConsumer().readPropertyValue(reader, entityPropertyInfo, null); + new JsonPropertyConsumer().readPropertyValue(reader, entityPropertyInfo, null, null); } private JsonReader prepareReader(final String json) throws UnsupportedEncodingException { http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/2211ce41/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/ep/consumer/XmlPropertyConsumerTest.java ---------------------------------------------------------------------- diff --git a/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/ep/consumer/XmlPropertyConsumerTest.java b/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/ep/consumer/XmlPropertyConsumerTest.java index e1e1f9d..c33b4ea 100644 --- a/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/ep/consumer/XmlPropertyConsumerTest.java +++ b/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/ep/consumer/XmlPropertyConsumerTest.java @@ -19,6 +19,7 @@ package org.apache.olingo.odata2.core.ep.consumer; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; @@ -38,11 +39,12 @@ import org.apache.olingo.odata2.api.edm.EdmProperty; import org.apache.olingo.odata2.api.edm.EdmSimpleTypeException; import org.apache.olingo.odata2.api.edm.EdmSimpleTypeKind; import org.apache.olingo.odata2.api.ep.EntityProviderException; +import org.apache.olingo.odata2.api.ep.EntityProviderReadProperties; import org.apache.olingo.odata2.testutil.mock.MockFacade; import org.junit.Test; /** - * + * Tests consuming XML properties. */ public class XmlPropertyConsumerTest extends AbstractXmlConsumerTest { @@ -57,7 +59,7 @@ public class XmlPropertyConsumerTest extends AbstractXmlConsumerTest { final EdmProperty property = (EdmProperty) MockFacade.getMockEdm().getEntityType("RefScenario", "Employee").getProperty("Age"); - Map<String, Object> resultMap = new XmlPropertyConsumer().readProperty(reader, property, false); + Map<String, Object> resultMap = new XmlPropertyConsumer().readProperty(reader, property, false, null); assertEquals(Integer.valueOf(67), resultMap.get("Age")); } @@ -70,7 +72,8 @@ public class XmlPropertyConsumerTest extends AbstractXmlConsumerTest { (EdmProperty) MockFacade.getMockEdm().getEntityType("RefScenario", "Employee").getProperty("Age"); Map<String, Object> typeMappings = createTypeMappings("Age", Long.class); - Map<String, Object> resultMap = new XmlPropertyConsumer().readProperty(reader, property, false, typeMappings); + Map<String, Object> resultMap = new XmlPropertyConsumer().readProperty(reader, property, false, typeMappings, + null); assertEquals(Long.valueOf(67), resultMap.get("Age")); } @@ -83,7 +86,8 @@ public class XmlPropertyConsumerTest extends AbstractXmlConsumerTest { (EdmProperty) MockFacade.getMockEdm().getEntityType("RefScenario", "Employee").getProperty("Age"); Map<String, Object> typeMappings = null; - Map<String, Object> resultMap = new XmlPropertyConsumer().readProperty(reader, property, false, typeMappings); + Map<String, Object> resultMap = new XmlPropertyConsumer().readProperty(reader, property, false, typeMappings, + null); assertEquals(Integer.valueOf(67), resultMap.get("Age")); } @@ -96,7 +100,8 @@ public class XmlPropertyConsumerTest extends AbstractXmlConsumerTest { (EdmProperty) MockFacade.getMockEdm().getEntityType("RefScenario", "Employee").getProperty("Age"); Map<String, Object> typeMappings = new HashMap<String, Object>(); - Map<String, Object> resultMap = new XmlPropertyConsumer().readProperty(reader, property, false, typeMappings); + Map<String, Object> resultMap = new XmlPropertyConsumer().readProperty(reader, property, false, typeMappings, + null); assertEquals(Integer.valueOf(67), resultMap.get("Age")); } @@ -108,7 +113,7 @@ public class XmlPropertyConsumerTest extends AbstractXmlConsumerTest { final EdmProperty property = (EdmProperty) MockFacade.getMockEdm().getEntityType("RefScenario", "Employee").getProperty("EmployeeName"); - Map<String, Object> resultMap = new XmlPropertyConsumer().readProperty(reader, property, false); + Map<String, Object> resultMap = new XmlPropertyConsumer().readProperty(reader, property, false, null); assertEquals("Max Mustermann", resultMap.get("EmployeeName")); } @@ -120,7 +125,7 @@ public class XmlPropertyConsumerTest extends AbstractXmlConsumerTest { final EdmProperty property = (EdmProperty) MockFacade.getMockEdm().getEntityType("RefScenario", "Employee").getProperty("EmployeeName"); - final Map<String, Object> resultMap = new XmlPropertyConsumer().readProperty(reader, property, false); + final Map<String, Object> resultMap = new XmlPropertyConsumer().readProperty(reader, property, false, null); assertTrue(resultMap.containsKey("EmployeeName")); assertEquals("", resultMap.get("EmployeeName")); @@ -134,7 +139,7 @@ public class XmlPropertyConsumerTest extends AbstractXmlConsumerTest { final EdmProperty property = (EdmProperty) MockFacade.getMockEdm().getEntityType("RefScenario", "Employee").getProperty("EntryDate"); - final Map<String, Object> resultMap = new XmlPropertyConsumer().readProperty(reader, property, false); + final Map<String, Object> resultMap = new XmlPropertyConsumer().readProperty(reader, property, false, null); assertTrue(resultMap.containsKey("EntryDate")); assertNull(resultMap.get("EntryDate")); @@ -148,7 +153,7 @@ public class XmlPropertyConsumerTest extends AbstractXmlConsumerTest { final EdmProperty property = (EdmProperty) MockFacade.getMockEdm().getEntityType("RefScenario", "Employee").getProperty("EntryDate"); - final Map<String, Object> resultMap = new XmlPropertyConsumer().readProperty(reader, property, false); + final Map<String, Object> resultMap = new XmlPropertyConsumer().readProperty(reader, property, false, null); assertEquals(86400000L, ((Calendar) resultMap.get("EntryDate")).getTimeInMillis()); } @@ -160,7 +165,7 @@ public class XmlPropertyConsumerTest extends AbstractXmlConsumerTest { final EdmProperty property = (EdmProperty) MockFacade.getMockEdm().getEntityType("RefScenario", "Employee").getProperty("Age"); - new XmlPropertyConsumer().readProperty(reader, property, false); + new XmlPropertyConsumer().readProperty(reader, property, false, null); } @Test(expected = EntityProviderException.class) @@ -171,7 +176,7 @@ public class XmlPropertyConsumerTest extends AbstractXmlConsumerTest { final EdmProperty property = (EdmProperty) MockFacade.getMockEdm().getEntityType("RefScenario", "Employee").getProperty("Age"); - new XmlPropertyConsumer().readProperty(reader, property, false); + new XmlPropertyConsumer().readProperty(reader, property, false, null); } @Test(expected = EntityProviderException.class) @@ -185,7 +190,35 @@ public class XmlPropertyConsumerTest extends AbstractXmlConsumerTest { when(facets.isNullable()).thenReturn(false); when(property.getFacets()).thenReturn(facets); - new XmlPropertyConsumer().readProperty(reader, property, false); + new XmlPropertyConsumer().readProperty(reader, property, false, null); + } + + @Test(expected = EntityProviderException.class) + public void violatedValidation() throws Exception { + final String xml = "<Name xmlns=\"" + Edm.NAMESPACE_D_2007_08 + "\">TooLongName</Name>"; + EdmProperty property = (EdmProperty) MockFacade.getMockEdm().getEntityType("RefScenario", "Team") + .getProperty("Name"); + EdmFacets facets = mock(EdmFacets.class); + when(facets.getMaxLength()).thenReturn(10); + when(property.getFacets()).thenReturn(facets); + + new XmlPropertyConsumer().readProperty(createReaderForTest(xml, true), property, false, null); + } + + @Test + public void ignoringValidation() throws Exception { + final String xml = "<Name xmlns=\"" + Edm.NAMESPACE_D_2007_08 + "\">TooLongName</Name>"; + EdmProperty property = (EdmProperty) MockFacade.getMockEdm().getEntityType("RefScenario", "Team") + .getProperty("Name"); + EdmFacets facets = mock(EdmFacets.class); + when(facets.getMaxLength()).thenReturn(10); + when(property.getFacets()).thenReturn(facets); + final EntityProviderReadProperties readProperties = mock(EntityProviderReadProperties.class); + + final Map<String, Object> resultMap = new XmlPropertyConsumer() + .readProperty(createReaderForTest(xml, true), property, false, readProperties); + assertTrue(resultMap.containsKey("Name")); + assertEquals("TooLongName", resultMap.get("Name")); } @Test @@ -204,7 +237,7 @@ public class XmlPropertyConsumerTest extends AbstractXmlConsumerTest { final EdmProperty property = (EdmProperty) MockFacade.getMockEdm().getEntityType("RefScenario", "Employee").getProperty("Location"); - Map<String, Object> resultMap = new XmlPropertyConsumer().readProperty(reader, property, false); + Map<String, Object> resultMap = new XmlPropertyConsumer().readProperty(reader, property, false, null); Map<String, Object> locationMap = (Map<String, Object>) resultMap.get("Location"); assertEquals("Germany", locationMap.get("Country")); @@ -232,7 +265,7 @@ public class XmlPropertyConsumerTest extends AbstractXmlConsumerTest { final EdmProperty property = (EdmProperty) MockFacade.getMockEdm().getEntityType("RefScenario", "Employee").getProperty("Location"); - Map<String, Object> resultMap = new XmlPropertyConsumer().readProperty(reader, property, false); + Map<String, Object> resultMap = new XmlPropertyConsumer().readProperty(reader, property, false, null); Map<String, Object> locationMap = (Map<String, Object>) resultMap.get("Location"); assertEquals("Germany", locationMap.get("Country")); @@ -258,7 +291,8 @@ public class XmlPropertyConsumerTest extends AbstractXmlConsumerTest { try { Map<String, Object> resultMap = new XmlPropertyConsumer().readProperty(reader, property, false, - createTypeMappings("Location", createTypeMappings("City", createTypeMappings("PostalCode", Integer.class)))); + createTypeMappings("Location", createTypeMappings("City", createTypeMappings("PostalCode", Integer.class))), + null); assertNotNull(resultMap); } catch (EntityProviderException e) { assertTrue(e.getCause() instanceof EdmSimpleTypeException); @@ -293,7 +327,7 @@ public class XmlPropertyConsumerTest extends AbstractXmlConsumerTest { createTypeMappings("City", createTypeMappings("CityName", String.class, "PostalCode", Long.class))); Map<String, Object> resultMap = - new XmlPropertyConsumer().readProperty(reader, locationComplexProperty, false, typeMappings); + new XmlPropertyConsumer().readProperty(reader, locationComplexProperty, false, typeMappings, null); // verify Map<String, Object> locationMap = (Map<String, Object>) resultMap.get("Location"); @@ -320,7 +354,7 @@ public class XmlPropertyConsumerTest extends AbstractXmlConsumerTest { final EdmProperty property = (EdmProperty) MockFacade.getMockEdm().getEntityType("RefScenario", "Employee").getProperty("Location"); - Object prop = new XmlPropertyConsumer().readProperty(reader, property, false); + Object prop = new XmlPropertyConsumer().readProperty(reader, property, false, null); Map<String, Object> resultMap = (Map<String, Object>) prop; Map<String, Object> locationMap = (Map<String, Object>) resultMap.get("Location"); @@ -345,7 +379,7 @@ public class XmlPropertyConsumerTest extends AbstractXmlConsumerTest { final EdmProperty property = (EdmProperty) MockFacade.getMockEdm().getEntityType("RefScenario", "Employee").getProperty("Location"); - new XmlPropertyConsumer().readProperty(reader, property, false); + new XmlPropertyConsumer().readProperty(reader, property, false, null); } @Test(expected = EntityProviderException.class) @@ -363,7 +397,7 @@ public class XmlPropertyConsumerTest extends AbstractXmlConsumerTest { final EdmProperty property = (EdmProperty) MockFacade.getMockEdm().getEntityType("RefScenario", "Employee").getProperty("Location"); - new XmlPropertyConsumer().readProperty(reader, property, false); + new XmlPropertyConsumer().readProperty(reader, property, false, null); } @Test(expected = EntityProviderException.class) @@ -381,7 +415,7 @@ public class XmlPropertyConsumerTest extends AbstractXmlConsumerTest { final EdmProperty property = (EdmProperty) MockFacade.getMockEdm().getEntityType("RefScenario", "Employee").getProperty("Location"); - new XmlPropertyConsumer().readProperty(reader, property, false); + new XmlPropertyConsumer().readProperty(reader, property, false, null); } @Test(expected = EntityProviderException.class) @@ -399,7 +433,7 @@ public class XmlPropertyConsumerTest extends AbstractXmlConsumerTest { final EdmProperty property = (EdmProperty) MockFacade.getMockEdm().getEntityType("RefScenario", "Employee").getProperty("Location"); - new XmlPropertyConsumer().readProperty(reader, property, false); + new XmlPropertyConsumer().readProperty(reader, property, false, null); } @Test @@ -418,7 +452,7 @@ public class XmlPropertyConsumerTest extends AbstractXmlConsumerTest { final EdmProperty property = (EdmProperty) MockFacade.getMockEdm().getEntityType("RefScenario", "Employee").getProperty("Location"); - Map<String, Object> resultMap = new XmlPropertyConsumer().readProperty(reader, property, false); + Map<String, Object> resultMap = new XmlPropertyConsumer().readProperty(reader, property, false, null); Map<String, Object> locationMap = (Map<String, Object>) resultMap.get("Location"); assertEquals("Germany", locationMap.get("Country")); @@ -435,7 +469,7 @@ public class XmlPropertyConsumerTest extends AbstractXmlConsumerTest { final EdmProperty property = (EdmProperty) MockFacade.getMockEdm().getEntityType("RefScenario", "Employee").getProperty("Location"); - final Map<String, Object> resultMap = new XmlPropertyConsumer().readProperty(reader, property, false); + final Map<String, Object> resultMap = new XmlPropertyConsumer().readProperty(reader, property, false, null); assertTrue(resultMap.containsKey("Location")); assertNull(resultMap.get("Location")); @@ -452,7 +486,24 @@ public class XmlPropertyConsumerTest extends AbstractXmlConsumerTest { when(facets.isNullable()).thenReturn(false); when(property.getFacets()).thenReturn(facets); - new XmlPropertyConsumer().readProperty(reader, property, false); + new XmlPropertyConsumer().readProperty(reader, property, false, null); + } + + @Test + public void complexPropertyNullValueNotAllowedButNotValidated() throws Exception { + final String xml = "<Location xmlns=\"" + Edm.NAMESPACE_D_2007_08 + + "\" m:null=\"true\" xmlns:m=\"" + Edm.NAMESPACE_M_2007_08 + "\" />"; + EdmProperty property = (EdmProperty) MockFacade.getMockEdm().getEntityType("RefScenario", "Employee") + .getProperty("Location"); + EdmFacets facets = mock(EdmFacets.class); + when(facets.isNullable()).thenReturn(false); + when(property.getFacets()).thenReturn(facets); + final EntityProviderReadProperties readProperties = mock(EntityProviderReadProperties.class); + + final Map<String, Object> resultMap = new XmlPropertyConsumer() + .readProperty(createReaderForTest(xml, true), property, false, readProperties); + assertFalse(resultMap.isEmpty()); + assertNull(resultMap.get("Location")); } @Test(expected = EntityProviderException.class) @@ -465,7 +516,7 @@ public class XmlPropertyConsumerTest extends AbstractXmlConsumerTest { final EdmProperty property = (EdmProperty) MockFacade.getMockEdm().getEntityType("RefScenario", "Employee").getProperty("Location"); - new XmlPropertyConsumer().readProperty(reader, property, false); + new XmlPropertyConsumer().readProperty(reader, property, false, null); } @Test @@ -475,7 +526,7 @@ public class XmlPropertyConsumerTest extends AbstractXmlConsumerTest { final EdmProperty property = (EdmProperty) MockFacade.getMockEdm().getEntityType("RefScenario", "Employee").getProperty("Location"); - final Map<String, Object> resultMap = new XmlPropertyConsumer().readProperty(reader, property, false); + final Map<String, Object> resultMap = new XmlPropertyConsumer().readProperty(reader, property, false, null); assertNotNull(resultMap.get("Location")); @SuppressWarnings("unchecked") http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/2211ce41/odata2-lib/odata-fit/src/test/java/org/apache/olingo/odata2/fit/basic/ServiceResolutionTest.java ---------------------------------------------------------------------- diff --git a/odata2-lib/odata-fit/src/test/java/org/apache/olingo/odata2/fit/basic/ServiceResolutionTest.java b/odata2-lib/odata-fit/src/test/java/org/apache/olingo/odata2/fit/basic/ServiceResolutionTest.java index 621701e..8ee0883 100644 --- a/odata2-lib/odata-fit/src/test/java/org/apache/olingo/odata2/fit/basic/ServiceResolutionTest.java +++ b/odata2-lib/odata-fit/src/test/java/org/apache/olingo/odata2/fit/basic/ServiceResolutionTest.java @@ -255,6 +255,24 @@ public class ServiceResolutionTest extends BaseTest { } @Test + public void testMetadataUriWithMatrixParameter() throws ClientProtocolException, IOException, ODataException, + URISyntaxException { + server.setPathSplit(3); + startServer(); + + final String endpoint = server.getEndpoint().toString(); + final HttpGet get = new HttpGet(URI.create(endpoint + "aaa/bbb;n=2,3;m=1/ccc/$metadata")); + final HttpResponse response = httpClient.execute(get); + + assertEquals(HttpStatusCodes.OK.getStatusCode(), response.getStatusLine().getStatusCode()); + + final ODataContext ctx = service.getProcessor().getContext(); + assertNotNull(ctx); + assertEquals(endpoint + "aaa/bbb;n=2,3;m=1/ccc/", ctx.getPathInfo().getServiceRoot().toASCIIString()); + assertEquals("$metadata", ctx.getPathInfo().getODataSegments().get(0).getPath()); + } + + @Test public void testBaseUriWithEncoding() throws ClientProtocolException, IOException, ODataException, URISyntaxException { server.setPathSplit(3);
