@Test public void testIsoDateFormatDateOptionalTimeUTC() { DateTimeFormatter formatter = ISODateTimeFormat.dateOptionalTimeParser().withZone(DateTimeZone.UTC); long millis = formatter.parseMillis("1970-01-01T00:00:00Z"); assertThat(millis, equalTo(0l)); millis = formatter.parseMillis("1970-01-01T00:00:00.001Z"); assertThat(millis, equalTo(1l)); millis = formatter.parseMillis("1970-01-01T00:00:00.1Z"); assertThat(millis, equalTo(100l)); millis = formatter.parseMillis("1970-01-01T00:00:00.1"); assertThat(millis, equalTo(100l)); millis = formatter.parseMillis("1970-01-01T00:00:00"); assertThat(millis, equalTo(0l)); millis = formatter.parseMillis("1970-01-01"); assertThat(millis, equalTo(0l)); millis = formatter.parseMillis("1970"); assertThat(millis, equalTo(0l)); try { formatter.parseMillis("1970 kuku"); assert false : "formatting should fail"; } catch (IllegalArgumentException e) { // all is well } // test offset in format millis = formatter.parseMillis("1970-01-01T00:00:00-02:00"); assertThat(millis, equalTo(TimeValue.timeValueHours(2).millis())); }
@Test public void testWriteAndParse() { DateTimeFormatter dateTimeWriter = ISODateTimeFormat.dateTime().withZone(DateTimeZone.UTC); DateTimeFormatter formatter = ISODateTimeFormat.dateOptionalTimeParser().withZone(DateTimeZone.UTC); Date date = new Date(); assertThat( formatter.parseMillis(dateTimeWriter.print(date.getTime())), equalTo(date.getTime())); }
public class StringToReadableInstant implements ETransformer<String, ReadableInstant> { private static final DateTimeFormatter LenientIsoParser = ISODateTimeFormat.dateOptionalTimeParser(); @Override public ReadableInstant from(String from) { return from == null ? null : LenientIsoParser.parseDateTime(from); } }
@Test public void testUpperBound() { MutableDateTime dateTime = new MutableDateTime(3000, 12, 31, 23, 59, 59, 999, DateTimeZone.UTC); DateTimeFormatter formatter = ISODateTimeFormat.dateOptionalTimeParser().withZone(DateTimeZone.UTC); String value = "2000-01-01"; int i = formatter.parseInto(dateTime, value, 0); assertThat(i, equalTo(value.length())); assertThat(dateTime.toString(), equalTo("2000-01-01T23:59:59.999Z")); }
@Test public void testIsoVsCustom() { DateTimeFormatter formatter = ISODateTimeFormat.dateOptionalTimeParser().withZone(DateTimeZone.UTC); long millis = formatter.parseMillis("1970-01-01T00:00:00"); assertThat(millis, equalTo(0l)); formatter = DateTimeFormat.forPattern("yyyy/MM/dd HH:mm:ss").withZone(DateTimeZone.UTC); millis = formatter.parseMillis("1970/01/01 00:00:00"); assertThat(millis, equalTo(0l)); FormatDateTimeFormatter formatter2 = Joda.forPattern("yyyy/MM/dd HH:mm:ss"); millis = formatter2.parser().parseMillis("1970/01/01 00:00:00"); assertThat(millis, equalTo(0l)); }
/** * This test acutally runs the full thread of calling the GetRecordsResponseConverter then calls * the CswInputTransformer. */ @Test public void testUnmarshalGetRecordsResponseFull() { XStream xstream = new XStream(new WstxDriver()); xstream.setClassLoader(this.getClass().getClassLoader()); CswTransformProvider provider = new CswTransformProvider(null, mockInputManager); when(mockInputManager.getTransformerBySchema(anyString())).thenReturn(new CswRecordConverter()); xstream.registerConverter(new GetRecordsResponseConverter(provider)); xstream.alias("GetRecordsResponse", CswRecordCollection.class); String xml = "<csw:GetRecordsResponse xmlns:csw=\"http://www.opengis.net/cat/csw\">\r\n" + " <csw:SearchStatus status=\"subset\" timestamp=\"2013-05-01T02:13:36+0200\"/>\r\n" + " <csw:SearchResults elementSet=\"full\" nextRecord=\"11\" numberOfRecordsMatched=\"479\" numberOfRecordsReturned=\"10\" recordSchema=\"csw:Record\">\r\n" + " <csw:Record xmlns:csw=\"http://www.opengis.net/cat/csw\">\r\n" + " <dc:identifier xmlns:dc=\"http://purl.org/dc/elements/1.1/\">{8C1F6297-EC96-4302-A01E-14988C9149FD}</dc:identifier>\r\n" + " <dc:title xmlns:dc=\"http://purl.org/dc/elements/1.1/\">title 1</dc:title>\r\n" + " <dct:modified xmlns:dct=\"http://purl.org/dc/terms/\">2008-12-15</dct:modified>\r\n" + " <dc:subject xmlns:dc=\"http://purl.org/dc/elements/1.1/\">subject 1</dc:subject>\r\n" + " <dc:subject xmlns:dc=\"http://purl.org/dc/elements/1.1/\">second subject</dc:subject>\r\n" + " <dct:abstract xmlns:dct=\"http://purl.org/dc/terms/\">abstract 1</dct:abstract>\r\n" + " <dc:rights xmlns:dc=\"http://purl.org/dc/elements/1.1/\">copyright 1</dc:rights>\r\n" + " <dc:rights xmlns:dc=\"http://purl.org/dc/elements/1.1/\">copyright 2</dc:rights>\r\n" + " <dc:language xmlns:dc=\"http://purl.org/dc/elements/1.1/\">english</dc:language> \r\n" + " <ows:BoundingBox xmlns:ows=\"http://www.opengis.net/ows\" crs=\"EPSG:RD_New (28992)\">\r\n" + " <ows:LowerCorner>5.121 52.139</ows:LowerCorner>\r\n" + " <ows:UpperCorner>4.468 52.517</ows:UpperCorner>\r\n" + " </ows:BoundingBox> \r\n" + " <dc:type xmlns:dc=\"http://purl.org/dc/elements/1.1/\">dataset</dc:type>\r\n" + " <dc:format xmlns:dc=\"http://purl.org/dc/elements/1.1/\">Shapefile</dc:format> \r\n" + " </csw:Record>\r\n" + " <csw:Record xmlns:csw=\"http://www.opengis.net/cat/csw\">\r\n" + " <dc:identifier xmlns:dc=\"http://purl.org/dc/elements/1.1/\">{23362852-F370-4369-B0B2-BE74B2859614}</dc:identifier>\r\n" + " <dc:title xmlns:dc=\"http://purl.org/dc/elements/1.1/\">mc2 title</dc:title>\r\n" + " <dct:modified xmlns:dct=\"http://purl.org/dc/terms/\">2010-12-15</dct:modified>\r\n" + " <dc:subject xmlns:dc=\"http://purl.org/dc/elements/1.1/\">first subject</dc:subject>\r\n" + " <dc:subject xmlns:dc=\"http://purl.org/dc/elements/1.1/\">subject 2</dc:subject>\r\n" + " <dct:abstract xmlns:dct=\"http://purl.org/dc/terms/\">mc2 abstract</dct:abstract>\r\n" + " <dc:rights xmlns:dc=\"http://purl.org/dc/elements/1.1/\">first copyright</dc:rights>\r\n" + " <dc:rights xmlns:dc=\"http://purl.org/dc/elements/1.1/\">second copyright</dc:rights>\r\n" + " <dc:language xmlns:dc=\"http://purl.org/dc/elements/1.1/\">english</dc:language>\r\n" + " <ows:BoundingBox xmlns:ows=\"http://www.opengis.net/ows\" crs=\"EPSG:RD_New (28992)\">\r\n" + " <ows:LowerCorner>6.121 53.139</ows:LowerCorner>\r\n" + " <ows:UpperCorner>5.468 53.517</ows:UpperCorner>\r\n" + " </ows:BoundingBox>\r\n" + " <dc:type xmlns:dc=\"http://purl.org/dc/elements/1.1/\">dataset 2</dc:type>\r\n" + " <dc:format xmlns:dc=\"http://purl.org/dc/elements/1.1/\">Shapefile 2</dc:format>\r\n" + " </csw:Record>\r\n" + " </csw:SearchResults>\r\n" + "</csw:GetRecordsResponse>"; InputStream inStream = IOUtils.toInputStream(xml); CswRecordCollection cswRecords = (CswRecordCollection) xstream.fromXML(inStream); IOUtils.closeQuietly(inStream); List<Metacard> metacards = cswRecords.getCswRecords(); assertThat(metacards, not(nullValue())); assertThat(metacards.size(), equalTo(2)); // verify first metacard's values Metacard mc = metacards.get(0); assertThat(mc, not(nullValue())); Map<String, Object> expectedValues = new HashMap<String, Object>(); expectedValues.put(Metacard.ID, "{8C1F6297-EC96-4302-A01E-14988C9149FD}"); expectedValues.put( CswRecordMetacardType.CSW_IDENTIFIER, new String[] {"{8C1F6297-EC96-4302-A01E-14988C9149FD}"}); expectedValues.put(Metacard.TITLE, "title 1"); expectedValues.put(CswRecordMetacardType.CSW_TITLE, new String[] {"title 1"}); String expectedModifiedDateStr = "2008-12-15"; DateTimeFormatter dateFormatter = ISODateTimeFormat.dateOptionalTimeParser(); Date expectedModifiedDate = dateFormatter.parseDateTime(expectedModifiedDateStr).toDate(); expectedValues.put(CswRecordMetacardType.CSW_MODIFIED, new String[] {expectedModifiedDateStr}); expectedValues.put(Metacard.MODIFIED, expectedModifiedDate); expectedValues.put( CswRecordMetacardType.CSW_SUBJECT, new String[] {"subject 1", "second subject"}); expectedValues.put(CswRecordMetacardType.CSW_ABSTRACT, new String[] {"abstract 1"}); expectedValues.put( CswRecordMetacardType.CSW_RIGHTS, new String[] {"copyright 1", "copyright 2"}); expectedValues.put(CswRecordMetacardType.CSW_LANGUAGE, new String[] {"english"}); expectedValues.put(CswRecordMetacardType.CSW_TYPE, "dataset"); expectedValues.put(CswRecordMetacardType.CSW_FORMAT, new String[] {"Shapefile"}); expectedValues.put( Metacard.GEOGRAPHY, "POLYGON((52.139 5.121, 52.517 5.121, 52.517 4.468, 52.139 4.468, 52.139 5.121))"); expectedValues.put( CswRecordMetacardType.OWS_BOUNDING_BOX, new String[] { "POLYGON((52.139 5.121, 52.517 5.121, 52.517 4.468, 52.139 4.468, 52.139 5.121))" }); assertMetacard(mc, expectedValues); expectedValues.clear(); // verify second metacard's values mc = metacards.get(1); assertThat(mc, not(nullValue())); expectedValues = new HashMap<String, Object>(); expectedValues.put(Metacard.ID, "{23362852-F370-4369-B0B2-BE74B2859614}"); expectedValues.put( CswRecordMetacardType.CSW_IDENTIFIER, new String[] {"{23362852-F370-4369-B0B2-BE74B2859614}"}); expectedValues.put(Metacard.TITLE, "mc2 title"); expectedValues.put(CswRecordMetacardType.CSW_TITLE, new String[] {"mc2 title"}); expectedModifiedDateStr = "2010-12-15"; dateFormatter = ISODateTimeFormat.dateOptionalTimeParser(); expectedModifiedDate = dateFormatter.parseDateTime(expectedModifiedDateStr).toDate(); expectedValues.put(CswRecordMetacardType.CSW_MODIFIED, new String[] {expectedModifiedDateStr}); expectedValues.put(Metacard.MODIFIED, expectedModifiedDate); expectedValues.put( CswRecordMetacardType.CSW_SUBJECT, new String[] {"first subject", "subject 2"}); expectedValues.put(CswRecordMetacardType.CSW_ABSTRACT, new String[] {"mc2 abstract"}); expectedValues.put( CswRecordMetacardType.CSW_RIGHTS, new String[] {"first copyright", "second copyright"}); expectedValues.put(CswRecordMetacardType.CSW_LANGUAGE, new String[] {"english"}); expectedValues.put(CswRecordMetacardType.CSW_TYPE, "dataset 2"); expectedValues.put(CswRecordMetacardType.CSW_FORMAT, new String[] {"Shapefile 2"}); expectedValues.put( Metacard.GEOGRAPHY, "POLYGON((53.139 6.121, 53.517 6.121, 53.517 5.468, 53.139 5.468, 53.139 6.121))"); expectedValues.put( CswRecordMetacardType.OWS_BOUNDING_BOX, new String[] { "POLYGON((53.139 6.121, 53.517 6.121, 53.517 5.468, 53.139 5.468, 53.139 6.121))" }); assertMetacard(mc, expectedValues); expectedValues.clear(); }
private static long time(String time, DateTimeZone zone) { return ISODateTimeFormat.dateOptionalTimeParser().withZone(zone).parseMillis(time); }
private void assertTimestamp(Map<String, Object> hit) { org.assertj.core.api.Assertions.assertThat(hit).containsKey("@timestamp"); // no exception means everything is cool ISODateTimeFormat.dateOptionalTimeParser().parseDateTime(hit.get("@timestamp").toString()); }
private long utcTimeInMillis(String time) { return ISODateTimeFormat.dateOptionalTimeParser().withZone(DateTimeZone.UTC).parseMillis(time); }