Skip to content

Commit e4dff3e

Browse files
committed
handle date conversion
1 parent b5a5dbf commit e4dff3e

14 files changed

Lines changed: 113 additions & 46 deletions

File tree

src/main/java/com/auth0/client/mgmt/StatsEntity.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package com.auth0.client.mgmt;
22

3-
import com.auth0.utils.Asserts;
43
import com.auth0.json.mgmt.DailyStats;
54
import com.auth0.net.CustomRequest;
65
import com.auth0.net.Request;
6+
import com.auth0.utils.Asserts;
77
import com.fasterxml.jackson.core.type.TypeReference;
88
import okhttp3.HttpUrl;
99
import okhttp3.OkHttpClient;
1010

11+
import java.text.SimpleDateFormat;
12+
import java.util.Date;
1113
import java.util.List;
1214

1315
/**
@@ -46,20 +48,24 @@ public Request<Integer> getActiveUsersCount() {
4648
* Request the Daily Stats for a given period. A token with scope read:stats is needed.
4749
* See https://auth0.com/docs/api/management/v2#!/Stats/get_daily
4850
*
49-
* @param dateFrom the first day of the period (inclusive). Must have YYYYMMDD format.
50-
* @param dateTo the last day of the period (inclusive). Must have YYYYMMDD format.
51+
* @param from the first day of the period (inclusive). Time is not taken into account.
52+
* @param to the last day of the period (inclusive). Time is not taken into account.
5153
* @return a Request to execute.
5254
*/
53-
public Request<List<DailyStats>> getDailyStats(String dateFrom, String dateTo) {
54-
Asserts.assertNotNull(dateFrom, "date from");
55-
Asserts.assertNotNull(dateTo, "date to");
55+
public Request<List<DailyStats>> getDailyStats(Date from, Date to) {
56+
Asserts.assertNotNull(from, "date from");
57+
Asserts.assertNotNull(to, "date to");
5658

59+
String dateFrom = formatDate(from);
60+
String dateTo = formatDate(to);
5761
String url = HttpUrl.parse(baseUrl)
5862
.newBuilder()
5963
.addPathSegment("api")
6064
.addPathSegment("v2")
6165
.addPathSegment("stats")
6266
.addPathSegment("daily")
67+
.addQueryParameter("from", dateFrom)
68+
.addQueryParameter("to", dateTo)
6369
.build()
6470
.toString();
6571

@@ -69,4 +75,9 @@ public Request<List<DailyStats>> getDailyStats(String dateFrom, String dateTo) {
6975
return request;
7076
}
7177

78+
protected String formatDate(Date date) {
79+
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
80+
return sdf.format(date);
81+
}
82+
7283
}

src/main/java/com/auth0/client/mgmt/TenantsEntity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import java.util.Map;
1313

1414
/**
15-
* Class that provides an implementation of the Tenants methods of the Management API as defined in https://auth0.com/docs/api/management/v2#!/Tenants
15+
* Class that provides an implementation of the Tenant Settings methods of the Management API as defined in https://auth0.com/docs/api/management/v2#!/Tenants
1616
*/
1717
@SuppressWarnings("WeakerAccess")
1818
public class TenantsEntity extends BaseManagementEntity {

src/main/java/com/auth0/json/mgmt/DailyStats.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package com.auth0.json.mgmt;
22

3+
import com.fasterxml.jackson.annotation.JsonFormat;
34
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
45
import com.fasterxml.jackson.annotation.JsonInclude;
56
import com.fasterxml.jackson.annotation.JsonProperty;
67

8+
import java.util.Date;
9+
710
/**
811
* Class that represents an Auth0 Daily Stats object. Related to the {@link com.auth0.client.mgmt.StatsEntity()} entity.
912
*/
@@ -14,8 +17,9 @@ public class DailyStats {
1417

1518
@JsonProperty("logins")
1619
private Integer logins;
20+
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
1721
@JsonProperty("date")
18-
private String date;
22+
private Date date;
1923

2024
/**
2125
* Getter for the amount of logins on the date
@@ -32,8 +36,9 @@ public Integer getLogins() {
3236
*
3337
* @return the date to which the stats belong
3438
*/
39+
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
3540
@JsonProperty("date")
36-
public String getDate() {
41+
public Date getDate() {
3742
return date;
3843
}
3944
}

src/main/java/com/auth0/json/mgmt/guardian/Enrollment.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package com.auth0.json.mgmt.guardian;
22

3+
import com.fasterxml.jackson.annotation.JsonFormat;
34
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
45
import com.fasterxml.jackson.annotation.JsonInclude;
56
import com.fasterxml.jackson.annotation.JsonProperty;
67

8+
import java.util.Date;
9+
710
/**
811
* Class that represents an Auth0 Guardian Enrollment object. Related to the {@link com.auth0.client.mgmt.GuardianEntity()} entity.
912
*/
@@ -26,10 +29,12 @@ public class Enrollment {
2629
private String phoneNumber;
2730
@JsonProperty("auth_method")
2831
private String authMethod;
32+
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
2933
@JsonProperty("enrolled_at")
30-
private String enrolledAt;
34+
private Date enrolledAt;
35+
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
3136
@JsonProperty("last_auth")
32-
private String lastAuth;
37+
private Date lastAuth;
3338

3439
/**
3540
* Getter for the enrollment ID
@@ -106,8 +111,9 @@ public String getAuthMethod() {
106111
*
107112
* @return the enrolled at.
108113
*/
114+
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
109115
@JsonProperty("enrolled_at")
110-
public String getEnrolledAt() {
116+
public Date getEnrolledAt() {
111117
return enrolledAt;
112118
}
113119

@@ -116,8 +122,9 @@ public String getEnrolledAt() {
116122
*
117123
* @return the last authentication.
118124
*/
125+
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
119126
@JsonProperty("last_auth")
120-
public String getLastAuth() {
127+
public Date getLastAuth() {
121128
return lastAuth;
122129
}
123130
}

src/main/java/com/auth0/json/mgmt/logevents/LogEvent.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package com.auth0.json.mgmt.logevents;
22

3+
import com.fasterxml.jackson.annotation.JsonFormat;
34
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
45
import com.fasterxml.jackson.annotation.JsonInclude;
56
import com.fasterxml.jackson.annotation.JsonProperty;
67

8+
import java.util.Date;
79
import java.util.Map;
810

911
/**
@@ -16,8 +18,9 @@ public class LogEvent {
1618

1719
@JsonProperty("_id")
1820
private String id;
21+
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
1922
@JsonProperty("date")
20-
private String date;
23+
private Date date;
2124
@JsonProperty("type")
2225
private String type;
2326
@JsonProperty("client_id")
@@ -48,8 +51,9 @@ public String getId() {
4851
*
4952
* @return the date.
5053
*/
54+
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
5155
@JsonProperty("date")
52-
public String getDate() {
56+
public Date getDate() {
5357
return date;
5458
}
5559

@@ -89,7 +93,7 @@ public String getClientName() {
8993
* @return the IP address.
9094
*/
9195
@JsonProperty("ip")
92-
public String getIp() {
96+
public String getIP() {
9397
return ip;
9498
}
9599

src/main/java/com/auth0/json/mgmt/users/User.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package com.auth0.json.mgmt.users;
22

3-
import com.fasterxml.jackson.annotation.JsonCreator;
4-
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
5-
import com.fasterxml.jackson.annotation.JsonInclude;
6-
import com.fasterxml.jackson.annotation.JsonProperty;
3+
import com.fasterxml.jackson.annotation.*;
74

5+
import java.util.Date;
86
import java.util.List;
97
import java.util.Map;
108

@@ -50,10 +48,12 @@ public class User {
5048
private String givenName;
5149
@JsonProperty("family_name")
5250
private String familyName;
51+
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
5352
@JsonProperty("created_at")
54-
private String createdAt;
53+
private Date createdAt;
54+
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
5555
@JsonProperty("updated_at")
56-
private String updatedAt;
56+
private Date updatedAt;
5757
@JsonProperty("identities")
5858
private List<Identity> identities;
5959
@JsonProperty("app_metadata")
@@ -64,8 +64,9 @@ public class User {
6464
private List<String> multifactor;
6565
@JsonProperty("last_ip")
6666
private String lastIp;
67+
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
6768
@JsonProperty("last_login")
68-
private String lastLogin;
69+
private Date lastLogin;
6970
@JsonProperty("logins_count")
7071
private Integer loginsCount;
7172
@JsonProperty("blocked")
@@ -311,8 +312,9 @@ public void setFamilyName(String familyName) {
311312
*
312313
* @return the created at.
313314
*/
315+
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
314316
@JsonProperty("created_at")
315-
public String getCreatedAt() {
317+
public Date getCreatedAt() {
316318
return createdAt;
317319
}
318320

@@ -321,8 +323,9 @@ public String getCreatedAt() {
321323
*
322324
* @return the updated at.
323325
*/
326+
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
324327
@JsonProperty("updated_at")
325-
public String getUpdatedAt() {
328+
public Date getUpdatedAt() {
326329
return updatedAt;
327330
}
328331

@@ -401,8 +404,9 @@ public String getLastIP() {
401404
*
402405
* @return the last login.
403406
*/
407+
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
404408
@JsonProperty("last_login")
405-
public String getLastLogin() {
409+
public Date getLastLogin() {
406410
return lastLogin;
407411
}
408412

src/test/java/com/auth0/client/mgmt/StatsEntityTest.java

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
import okhttp3.mockwebserver.RecordedRequest;
66
import org.junit.Test;
77

8+
import java.util.Calendar;
9+
import java.util.Date;
810
import java.util.List;
911

1012
import static com.auth0.client.MockServer.*;
11-
import static com.auth0.client.RecordedRequestMatcher.hasHeader;
12-
import static com.auth0.client.RecordedRequestMatcher.hasMethodAndPath;
13+
import static com.auth0.client.RecordedRequestMatcher.*;
1314
import static org.hamcrest.Matchers.*;
1415
import static org.junit.Assert.assertThat;
1516

@@ -35,19 +36,27 @@ public void shouldGetActiveUsersCount() throws Exception {
3536
public void shouldThrowOnGetDailyStatsWithNullDateFrom() throws Exception {
3637
exception.expect(IllegalArgumentException.class);
3738
exception.expectMessage("'date from' cannot be null!");
38-
api.stats().getDailyStats(null, "20161011");
39+
api.stats().getDailyStats(null, new Date());
3940
}
4041

4142
@Test
4243
public void shouldThrowOnGetDailyStatsWithNullDateTo() throws Exception {
4344
exception.expect(IllegalArgumentException.class);
4445
exception.expectMessage("'date to' cannot be null!");
45-
api.stats().getDailyStats("20161011", null);
46+
api.stats().getDailyStats(new Date(), null);
4647
}
4748

49+
@SuppressWarnings("deprecation")
4850
@Test
4951
public void shouldGetDailyStats() throws Exception {
50-
Request<List<DailyStats>> request = api.stats().getDailyStats("20161011", "20161011");
52+
Calendar calendar = Calendar.getInstance();
53+
calendar.set(Calendar.YEAR, 2016);
54+
calendar.set(Calendar.MONTH, Calendar.OCTOBER);
55+
calendar.set(Calendar.DATE, 11);
56+
Date dateFrom = calendar.getTime();
57+
calendar.set(Calendar.DATE, 12);
58+
Date dateTo = calendar.getTime();
59+
Request<List<DailyStats>> request = api.stats().getDailyStats(dateFrom, dateTo);
5160
assertThat(request, is(notNullValue()));
5261

5362
server.jsonResponse(MGMT_DAILY_STATS_LIST, 200);
@@ -57,13 +66,15 @@ public void shouldGetDailyStats() throws Exception {
5766
assertThat(recordedRequest, hasMethodAndPath("GET", "/api/v2/stats/daily"));
5867
assertThat(recordedRequest, hasHeader("Content-Type", "application/json"));
5968
assertThat(recordedRequest, hasHeader("Authorization", "Bearer apiToken"));
69+
assertThat(recordedRequest, hasQueryParameter("from", "20161011"));
70+
assertThat(recordedRequest, hasQueryParameter("to", "20161012"));
6071

6172
assertThat(response, is(notNullValue()));
6273
}
6374

6475
@Test
6576
public void shouldReturnEmptyDailyStats() throws Exception {
66-
Request<List<DailyStats>> request = api.stats().getDailyStats("20161011", "20161011");
77+
Request<List<DailyStats>> request = api.stats().getDailyStats(new Date(), new Date());
6778
assertThat(request, is(notNullValue()));
6879

6980
server.jsonResponse(MGMT_EMPTY_LIST, 200);
@@ -72,4 +83,16 @@ public void shouldReturnEmptyDailyStats() throws Exception {
7283
assertThat(response, is(notNullValue()));
7384
assertThat(response, is(emptyCollectionOf(DailyStats.class)));
7485
}
86+
87+
@Test
88+
public void shouldFormatDateToYYYYMMDD() throws Exception {
89+
Calendar calendar = Calendar.getInstance();
90+
calendar.set(Calendar.YEAR, 2010);
91+
//Calendar.MONTH starts at 0 being January
92+
calendar.set(Calendar.MONTH, Calendar.JANUARY);
93+
calendar.set(Calendar.DATE, 22);
94+
95+
assertThat(api.stats().formatDate(calendar.getTime()), is("20100122"));
96+
97+
}
7598
}

src/test/java/com/auth0/json/JsonTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,15 @@
55
import com.fasterxml.jackson.databind.ObjectMapper;
66

77
import java.io.IOException;
8+
import java.text.ParseException;
9+
import java.text.SimpleDateFormat;
10+
import java.util.Date;
11+
import java.util.TimeZone;
812

913
public class JsonTest<T> {
1014

15+
private static final String DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
16+
1117
private ObjectMapper mapper;
1218

1319
public JsonTest() {
@@ -25,4 +31,10 @@ public T fromJSON(String json, Class<T> tClazz) throws IOException {
2531
public T fromJSON(String json, TypeReference<T> tReference) throws IOException {
2632
return mapper.readValue(json, tReference);
2733
}
34+
35+
protected Date parseJSONDate(String dateString) throws ParseException {
36+
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
37+
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
38+
return sdf.parse(dateString);
39+
}
2840
}

src/test/java/com/auth0/json/mgmt/DailyStatsTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.junit.Test;
55

66
import static org.hamcrest.MatcherAssert.assertThat;
7+
import static org.hamcrest.Matchers.equalTo;
78
import static org.hamcrest.Matchers.is;
89
import static org.hamcrest.Matchers.notNullValue;
910

@@ -15,7 +16,7 @@ public void shouldDeserialize() throws Exception {
1516
DailyStats stats = fromJSON(json, DailyStats.class);
1617

1718
assertThat(stats, is(notNullValue()));
18-
assertThat(stats.getDate(), is("2017-01-18T17:45:08.328Z"));
19+
assertThat(stats.getDate(), is(equalTo(parseJSONDate("2017-01-18T17:45:08.328Z"))));
1920
assertThat(stats.getLogins(), is(123));
2021
}
2122

0 commit comments

Comments
 (0)