Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[OLINGO-1501] Fix error when OData client receives EdmDateTime or EdmDateTimeOffset values with precision greater than milliseconds #54

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static EdmDateTime getInstance() {

@Override
public Class<?> getDefaultType() {
return Calendar.class;
return Timestamp.class;
}

@Override
Expand All @@ -73,6 +73,8 @@ protected <T> T internalValueOfString(final String value, final EdmLiteralKind l
dateTimeValue.clear();
dateTimeValue.setTimeInMillis(millis);
return returnType.cast(dateTimeValue);
} else if (returnType.isAssignableFrom(Timestamp.class)) {
return returnType.cast(new Timestamp(millis));
} else {
throw new EdmSimpleTypeException(EdmSimpleTypeException.VALUE_TYPE_NOT_SUPPORTED.addContent(returnType));
}
Expand Down Expand Up @@ -170,11 +172,7 @@ protected <T> String internalValueToString(final T value, final EdmLiteralKind l
}

if (literalKind == EdmLiteralKind.JSON) {
if (value instanceof Timestamp && ((Timestamp) value).getNanos() % (1000 * 1000) != 0) {
throw new EdmSimpleTypeException(EdmSimpleTypeException.VALUE_ILLEGAL_CONTENT.addContent(value));
} else {
return "/Date(" + timeInMillis + ")/";
}
return "/Date(" + timeInMillis + ")/";
}

Calendar dateTimeValue = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static EdmDateTimeOffset getInstance() {

@Override
public Class<?> getDefaultType() {
return Calendar.class;
return Timestamp.class;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -436,8 +436,8 @@ public void defaultType() throws Exception {
assertEquals(byte[].class, EdmSimpleTypeKind.Binary.getEdmSimpleTypeInstance().getDefaultType());
assertEquals(Boolean.class, EdmSimpleTypeKind.Boolean.getEdmSimpleTypeInstance().getDefaultType());
assertEquals(Short.class, EdmSimpleTypeKind.Byte.getEdmSimpleTypeInstance().getDefaultType());
assertEquals(Calendar.class, EdmSimpleTypeKind.DateTime.getEdmSimpleTypeInstance().getDefaultType());
assertEquals(Calendar.class, EdmSimpleTypeKind.DateTimeOffset.getEdmSimpleTypeInstance().getDefaultType());
assertEquals(Timestamp.class, EdmSimpleTypeKind.DateTime.getEdmSimpleTypeInstance().getDefaultType());
assertEquals(Timestamp.class, EdmSimpleTypeKind.DateTimeOffset.getEdmSimpleTypeInstance().getDefaultType());
assertEquals(BigDecimal.class, EdmSimpleTypeKind.Decimal.getEdmSimpleTypeInstance().getDefaultType());
assertEquals(Double.class, EdmSimpleTypeKind.Double.getEdmSimpleTypeInstance().getDefaultType());
assertEquals(UUID.class, EdmSimpleTypeKind.Guid.getEdmSimpleTypeInstance().getDefaultType());
Expand Down Expand Up @@ -596,8 +596,7 @@ public void valueToStringDateTime() throws Exception {
getPrecisionScaleFacets(9, null)));
expectErrorInValueToString(instance, timestamp, EdmLiteralKind.DEFAULT, getPrecisionScaleFacets(8, null),
EdmSimpleTypeException.VALUE_FACETS_NOT_MATCHED);
expectErrorInValueToString(instance, timestamp, EdmLiteralKind.JSON, null,
EdmSimpleTypeException.VALUE_ILLEGAL_CONTENT);
assertEquals("/Date(0)/", instance.valueToString(timestamp, EdmLiteralKind.JSON, null));

dateTime.add(Calendar.MILLISECOND, -100);
expectErrorInValueToString(instance, dateTime, EdmLiteralKind.DEFAULT, getPrecisionScaleFacets(0, null),
Expand Down Expand Up @@ -1190,8 +1189,6 @@ public void valueOfStringDateTime() throws Exception {
getPrecisionScaleFacets(0, null), EdmSimpleTypeException.LITERAL_FACETS_NOT_MATCHED);
expectErrorInValueOfString(instance, "2012-02-29T23:32:02.98700", EdmLiteralKind.DEFAULT,
getPrecisionScaleFacets(2, null), EdmSimpleTypeException.LITERAL_FACETS_NOT_MATCHED);
expectErrorInValueOfString(instance, "2012-02-29T23:32:02.9876", EdmLiteralKind.DEFAULT, null,
EdmSimpleTypeException.LITERAL_ILLEGAL_CONTENT);
expectErrorInValueOfString(instance, "2012-02-29T23:32:02.", EdmLiteralKind.DEFAULT, null,
EdmSimpleTypeException.LITERAL_ILLEGAL_CONTENT);
expectErrorInValueOfString(instance, "2012-02-29T23:32:02.0000000000", EdmLiteralKind.DEFAULT, null,
Expand Down Expand Up @@ -1293,8 +1290,6 @@ public void valueOfStringDateTimeOffset() throws Exception {

expectErrorInValueOfString(instance, "2012-02-29T23:32:02.9Z", EdmLiteralKind.DEFAULT,
getPrecisionScaleFacets(0, null), EdmSimpleTypeException.LITERAL_FACETS_NOT_MATCHED);
expectErrorInValueOfString(instance, "2012-02-29T23:32:02.9876Z", EdmLiteralKind.DEFAULT, null,
EdmSimpleTypeException.LITERAL_ILLEGAL_CONTENT);
expectErrorInValueOfString(instance, "datetime'2012-02-29T23:32:02'", EdmLiteralKind.URI, null,
EdmSimpleTypeException.LITERAL_ILLEGAL_CONTENT);
expectErrorInValueOfString(instance, "2012-02-29T23:32:02X", EdmLiteralKind.DEFAULT, null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@
import static org.junit.Assert.fail;

import java.io.InputStream;
import java.util.Calendar;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

import org.apache.olingo.odata2.api.edm.EdmEntitySet;
import org.apache.olingo.odata2.api.ep.EntityProviderException;
Expand Down Expand Up @@ -184,9 +183,8 @@ public void readSimpleEmployeeEntry() throws Exception {
assertEquals("69124", city.get("PostalCode"));
assertEquals("Heidelberg", city.get("CityName"));
assertEquals(Integer.valueOf(52), properties.get("Age"));
Calendar entryDate = (Calendar) properties.get("EntryDate");
assertEquals(915148800000L, entryDate.getTimeInMillis());
assertEquals(TimeZone.getTimeZone("GMT"), entryDate.getTimeZone());
Timestamp entryDate = (Timestamp) properties.get("EntryDate");
assertEquals(915148800000L, entryDate.getTime());
assertEquals("Employees('1')/$value", properties.get("ImageUrl"));

List<String> associationUris = result.getMetadata().getAssociationUris("ne_Manager");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
Expand Down Expand Up @@ -126,15 +127,15 @@ public void allStringSimplePropertyKinds() throws Exception {
JsonReader reader = prepareReader(simplePropertyJson);
when(edmProperty.getType()).thenReturn(EdmSimpleTypeKind.DateTime.getEdmSimpleTypeInstance());
Map<String, Object> resultMap = execute(edmProperty, reader);
Calendar entryDate = (Calendar) resultMap.get("Name");
assertEquals(Long.valueOf(915148800000l), Long.valueOf(entryDate.getTimeInMillis()));
Timestamp entryDate = (Timestamp) resultMap.get("Name");
assertEquals(Long.valueOf(915148800000l), Long.valueOf(entryDate.getTime()));
// DateTimeOffset
simplePropertyJson = "{\"d\":{\"Name\":\"\\/Date(915148800000)\\/\"}}";
reader = prepareReader(simplePropertyJson);
when(edmProperty.getType()).thenReturn(EdmSimpleTypeKind.DateTime.getEdmSimpleTypeInstance());
resultMap = execute(edmProperty, reader);
entryDate = (Calendar) resultMap.get("Name");
assertEquals(Long.valueOf(915148800000l), Long.valueOf(entryDate.getTimeInMillis()));
entryDate = (Timestamp) resultMap.get("Name");
assertEquals(Long.valueOf(915148800000l), Long.valueOf(entryDate.getTime()));
// Decimal
simplePropertyJson = "{\"d\":{\"Name\":\"123456789\"}}";
reader = prepareReader(simplePropertyJson);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,11 @@
import static org.junit.Assert.assertTrue;

import java.io.InputStream;
import java.util.Calendar;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;
import java.util.Map.Entry;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand Down Expand Up @@ -1855,9 +1854,8 @@ public void testReadFeed() throws Exception {
assertEquals("69124", city.get("PostalCode"));
assertEquals("Heidelberg", city.get("CityName"));
assertEquals(Integer.valueOf(52), properties.get("Age"));
Calendar entryDate = (Calendar) properties.get("EntryDate");
assertEquals(915148800000L, entryDate.getTimeInMillis());
assertEquals(TimeZone.getTimeZone("GMT"), entryDate.getTimeZone());
Timestamp entryDate = (Timestamp) properties.get("EntryDate");
assertEquals(915148800000L, entryDate.getTime());
assertEquals("Employees('1')/$value", properties.get("ImageUrl"));
}

Expand Down Expand Up @@ -1901,9 +1899,8 @@ public void testReadFeedWithInlineCountAndNextLink() throws Exception {
assertEquals("69124", city.get("PostalCode"));
assertEquals("Heidelberg", city.get("CityName"));
assertEquals(Integer.valueOf(52), properties.get("Age"));
Calendar entryDate = (Calendar) properties.get("EntryDate");
assertEquals(915148800000L, entryDate.getTimeInMillis());
assertEquals(TimeZone.getTimeZone("GMT"), entryDate.getTimeZone());
Timestamp entryDate = (Timestamp) properties.get("EntryDate");
assertEquals(915148800000L, entryDate.getTime());
assertEquals("Employees('1')/$value", properties.get("ImageUrl"));
}

Expand Down Expand Up @@ -1936,9 +1933,8 @@ public void testReadEntry() throws Exception {
assertEquals("69124", city.get("PostalCode"));
assertEquals("Heidelberg", city.get("CityName"));
assertEquals(Integer.valueOf(52), properties.get("Age"));
Calendar entryDate = (Calendar) properties.get("EntryDate");
assertEquals(915148800000L, entryDate.getTimeInMillis());
assertEquals(TimeZone.getTimeZone("GMT"), entryDate.getTimeZone());
Timestamp entryDate = (Timestamp) properties.get("EntryDate");
assertEquals(915148800000L, entryDate.getTime());
assertEquals("/SAP/PUBLIC/BC/NWDEMO_MODEL/IMAGES/Employee_1.png", properties.get("ImageUrl"));
}

Expand Down Expand Up @@ -1972,9 +1968,8 @@ public void testReadEntryWithLargeProperty() throws Exception {
assertEquals("69124", city.get("PostalCode"));
assertEquals("Heidelberg", city.get("CityName"));
assertEquals(Integer.valueOf(52), properties.get("Age"));
Calendar entryDate = (Calendar) properties.get("EntryDate");
assertEquals(915148800000L, entryDate.getTimeInMillis());
assertEquals(TimeZone.getTimeZone("GMT"), entryDate.getTimeZone());
Timestamp entryDate = (Timestamp) properties.get("EntryDate");
assertEquals(915148800000L, entryDate.getTime());
assertEquals("/SAP/PUBLIC/BC/NWDEMO_MODEL/IMAGES/Employee_1.png", properties.get("ImageUrl"));
}

Expand Down Expand Up @@ -2011,9 +2006,8 @@ public void testReadEntryMissingKeyProperty() throws Exception {
assertEquals("69124", city.get("PostalCode"));
assertEquals("Heidelberg", city.get("CityName"));
assertEquals(Integer.valueOf(52), properties.get("Age"));
Calendar entryDate = (Calendar) properties.get("EntryDate");
assertEquals(915148800000L, entryDate.getTimeInMillis());
assertEquals(TimeZone.getTimeZone("GMT"), entryDate.getTimeZone());
Timestamp entryDate = (Timestamp) properties.get("EntryDate");
assertEquals(915148800000L, entryDate.getTime());
assertEquals("/SAP/PUBLIC/BC/NWDEMO_MODEL/IMAGES/Employee_1.png", properties.get("ImageUrl"));
}

Expand Down Expand Up @@ -2105,9 +2099,8 @@ public void testReadEntryWithMerge() throws Exception {
assertEquals(2, city.size());
assertEquals("69124", city.get("PostalCode"));
assertEquals("Heidelberg", city.get("CityName"));
Calendar entryDate = (Calendar) properties.get("EntryDate");
assertEquals(915148800000L, entryDate.getTimeInMillis());
assertEquals(TimeZone.getTimeZone("GMT"), entryDate.getTimeZone());
Timestamp entryDate = (Timestamp) properties.get("EntryDate");
assertEquals(915148800000L, entryDate.getTime());
assertEquals("/SAP/PUBLIC/BC/NWDEMO_MODEL/IMAGES/Employee_1.png", properties.get("ImageUrl"));
}

Expand Down Expand Up @@ -2178,9 +2171,8 @@ public void testReadEntryRequest() throws Exception {
assertEquals("69124", city.get("PostalCode"));
assertEquals("Heidelberg", city.get("CityName"));
assertEquals(Integer.valueOf(52), properties.get("Age"));
Calendar entryDate = (Calendar) properties.get("EntryDate");
assertEquals(915148800000L, entryDate.getTimeInMillis());
assertEquals(TimeZone.getTimeZone("GMT"), entryDate.getTimeZone());
Timestamp entryDate = (Timestamp) properties.get("EntryDate");
assertEquals(915148800000L, entryDate.getTime());
assertEquals("/SAP/PUBLIC/BC/NWDEMO_MODEL/IMAGES/Employee_1.png", properties.get("ImageUrl"));
}

Expand Down Expand Up @@ -2211,9 +2203,8 @@ public void testReadEntryRequestNullMapping() throws Exception {
assertEquals("69124", city.get("PostalCode"));
assertEquals("Heidelberg", city.get("CityName"));
assertEquals(Integer.valueOf(52), properties.get("Age"));
Calendar entryDate = (Calendar) properties.get("EntryDate");
assertEquals(915148800000L, entryDate.getTimeInMillis());
assertEquals(TimeZone.getTimeZone("GMT"), entryDate.getTimeZone());
Timestamp entryDate = (Timestamp) properties.get("EntryDate");
assertEquals(915148800000L, entryDate.getTime());
assertEquals("/SAP/PUBLIC/BC/NWDEMO_MODEL/IMAGES/Employee_1.png", properties.get("ImageUrl"));
}

Expand Down Expand Up @@ -2243,9 +2234,8 @@ public void testReadEntryRequestEmptyMapping() throws Exception {
assertEquals("69124", city.get("PostalCode"));
assertEquals("Heidelberg", city.get("CityName"));
assertEquals(Integer.valueOf(52), properties.get("Age"));
Calendar entryDate = (Calendar) properties.get("EntryDate");
assertEquals(915148800000L, entryDate.getTimeInMillis());
assertEquals(TimeZone.getTimeZone("GMT"), entryDate.getTimeZone());
Timestamp entryDate = (Timestamp) properties.get("EntryDate");
assertEquals(915148800000L, entryDate.getTime());
assertEquals("/SAP/PUBLIC/BC/NWDEMO_MODEL/IMAGES/Employee_1.png", properties.get("ImageUrl"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import static org.mockito.Mockito.when;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
Expand Down Expand Up @@ -172,7 +173,7 @@ public void readStringPropertyNullFalse() throws Exception {

final Map<String, Object> resultMap = new XmlPropertyConsumer().readProperty(reader, property, null);

assertEquals(86400000L, ((Calendar) resultMap.get("EntryDate")).getTimeInMillis());
assertEquals(86400000L, ((Timestamp) resultMap.get("EntryDate")).getTime());
}

@Test(expected = EntityProviderException.class)
Expand Down