Skip to content

Commit 2a86bcf

Browse files
committed
Use ZonedDateTime for request event time
1 parent bdebc0f commit 2a86bcf

7 files changed

Lines changed: 43 additions & 13 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ CHANGELOG
99
* Added `Device.getLastSeenDateTime()`, `Device.getLocalDateTime()`,
1010
`Email.getFirstSeenDate()`, and `GeoIp2Location.getLocalDateTime()` methods
1111
that return `java.time` objects rather than strings.
12+
* The request event time is now stored internally as a `ZonedDateTime`. An
13+
`Event.Builder.time(ZonedDateTime)` method was also added to the event
14+
builder.
1215

1316
1.11.0 (2020-02-21)
1417
-------------------

src/main/java/com/maxmind/minfraud/request/Event.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
package com.maxmind.minfraud.request;
22

3+
import com.fasterxml.jackson.annotation.JsonIgnore;
34
import com.fasterxml.jackson.annotation.JsonProperty;
45
import com.maxmind.minfraud.AbstractModel;
56

7+
import java.time.ZoneId;
8+
import java.time.ZonedDateTime;
69
import java.util.Date;
710

811
public final class Event extends AbstractModel {
912

1013
private final String transactionId;
1114
private final String shopId;
12-
private final Date time;
15+
private final ZonedDateTime time;
1316
private final Type type;
1417

1518
private Event(Event.Builder builder) {
@@ -26,7 +29,7 @@ private Event(Event.Builder builder) {
2629
public static final class Builder {
2730
String transactionId;
2831
String shopId;
29-
Date time;
32+
ZonedDateTime time;
3033
Type type;
3134

3235
/**
@@ -58,7 +61,16 @@ public Event.Builder shopId(String id) {
5861
* @return The builder object.
5962
*/
6063
public Event.Builder time(Date date) {
61-
time = new Date(date.getTime());
64+
time = date.toInstant().atZone(ZoneId.systemDefault());
65+
return this;
66+
}
67+
68+
/**
69+
* @param date The date and time the event occurred.
70+
* @return The builder object.
71+
*/
72+
public Event.Builder time(ZonedDateTime date) {
73+
time = date;
6274
return this;
6375
}
6476

@@ -99,8 +111,16 @@ public String getShopId() {
99111
/**
100112
* @return The date and time of the event.
101113
*/
102-
@JsonProperty("time")
114+
@JsonIgnore
103115
public Date getTime() {
116+
return Date.from(time.toInstant());
117+
}
118+
119+
/**
120+
* @return The date and time of the event.
121+
*/
122+
@JsonProperty("time")
123+
public ZonedDateTime getDateTime() {
104124
return time;
105125
}
106126

src/test/java/com/maxmind/minfraud/request/EventTest.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.maxmind.minfraud.request.Event.Type;
55
import org.junit.Test;
66

7+
import java.time.ZonedDateTime;
78
import java.util.Date;
89

910
import static org.junit.Assert.assertEquals;
@@ -23,12 +24,19 @@ public void testShopId() throws Exception {
2324
}
2425

2526
@Test
26-
public void testTime() throws Exception {
27+
public void testTimeWithDate() throws Exception {
2728
Date date = new Date();
2829
Event event = new Builder().time(date).build();
2930
assertEquals(date, event.getTime());
3031
}
3132

33+
@Test
34+
public void testTimeWithZonedDateTime() throws Exception {
35+
ZonedDateTime date = ZonedDateTime.now();
36+
Event event = new Builder().time(date).build();
37+
assertEquals(date, event.getDateTime());
38+
}
39+
3240
@Test
3341
public void testType() throws Exception {
3442
Event event = new Builder().type(Type.ACCOUNT_CREATION).build();

src/test/java/com/maxmind/minfraud/request/RequestTestHelper.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import java.nio.charset.StandardCharsets;
99
import java.nio.file.Files;
1010
import java.nio.file.Paths;
11-
import java.text.SimpleDateFormat;
11+
import java.time.ZonedDateTime;
1212

1313
import static com.github.tomakehurst.wiremock.client.WireMock.*;
1414

@@ -32,7 +32,6 @@ public static Transaction fullTransactionEmailMd5() throws Exception {
3232
}
3333

3434
private static Transaction makeTransaction(Email e) throws Exception {
35-
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
3635
return new Transaction.Builder(
3736
new Device.Builder(InetAddress.getByName("81.2.69.160"))
3837
.userAgent("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.89 Safari/537.36")
@@ -46,7 +45,7 @@ private static Transaction makeTransaction(Email e) throws Exception {
4645
.Builder()
4746
.transactionId("txn3134133")
4847
.shopId("s2123")
49-
.time(dateFormat.parse("2012-04-12T23:20:50.52Z"))
48+
.time(ZonedDateTime.parse("2012-04-12T23:20:50.52Z"))
5049
.type(Event.Type.PURCHASE)
5150
.build()
5251
)

src/test/java/com/maxmind/minfraud/response/DeviceTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public void testDevice() throws Exception {
1919
.put("confidence", 99)
2020
.put("id", "C8D3BE1A-BE26-11E5-8C50-1B575C37265F")
2121
.put("last_seen", "2016-06-08T14:16:38Z")
22-
.put("local_time", "2018-04-05T15:21:00-07:00")
22+
.put("local_time", "2018-04-05T15:21:01-07:00")
2323
.end()
2424
.finish()
2525
);
@@ -28,8 +28,8 @@ public void testDevice() throws Exception {
2828
assertEquals(UUID.fromString("C8D3BE1A-BE26-11E5-8C50-1B575C37265F"), device.getId());
2929
assertEquals("2016-06-08T14:16:38Z", device.getLastSeen());
3030
assertEquals("2016-06-08T14:16:38Z", device.getLastSeenDateTime().toString());
31-
assertEquals("2018-04-05T15:21:00-07:00", device.getLocalTime());
32-
assertEquals("2018-04-05T15:21:00-07:00", device.getLocalDateTime().toString());
31+
assertEquals("2018-04-05T15:21:01-07:00", device.getLocalTime());
32+
assertEquals("2018-04-05T15:21:01-07:00", device.getLocalDateTime().toString());
3333

3434
}
3535
}

src/test/resources/test-data/full-request-email-md5.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"event": {
33
"transaction_id": "txn3134133",
44
"shop_id": "s2123",
5-
"time": "2012-04-12T23:20:50.052+00:00",
5+
"time": "2012-04-12T23:20:50.52Z",
66
"type": "purchase"
77
},
88
"account": {

src/test/resources/test-data/full-request.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"event": {
33
"transaction_id": "txn3134133",
44
"shop_id": "s2123",
5-
"time": "2012-04-12T23:20:50.052+00:00",
5+
"time": "2012-04-12T23:20:50.52Z",
66
"type": "purchase"
77
},
88
"account": {

0 commit comments

Comments
 (0)