Skip to content

Commit

Permalink
Adapt to the new conferences àll-events.json format (closes #139)
Browse files Browse the repository at this point in the history
- Declare new fields in conferences JSON parsing.
- Fix handling of conferences JSON dates:
  - the epoch timestamp is now expressed in milliseconds,
  - end date is now optional.
  • Loading branch information
marcwrobel committed May 10, 2023
1 parent 2f7b2a7 commit e80c2d8
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 26 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

### Fixed

- Adapt to the new conferences `àll-events.json` format (#139).

### Deprecated

### Removed
Expand Down
42 changes: 26 additions & 16 deletions src/main/java/com/lescastcodeurs/bot/conferences/Conference.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
package com.lescastcodeurs.bot.conferences;

import static com.lescastcodeurs.bot.internal.StringUtils.isNotBlank;
import static com.lescastcodeurs.bot.internal.StringUtils.isBlank;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.lescastcodeurs.bot.MarkdownSerializable;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Locale;

// Those fields are not used (but we still want this object to exactly reflect the expected JSON
// structure).
@JsonIgnoreProperties({"cfp", "status"})
@SuppressWarnings("java:S6218") // don't care
public record Conference(String name, String hyperlink, String location, long[] date, String misc)
implements MarkdownSerializable {

public static final long MIN_TIMESTAMP = Instant.MIN.getEpochSecond();
public static final long MAX_TIMESTAMP = Instant.MAX.getEpochSecond();
public static final long MIN_TIMESTAMP = Instant.EPOCH.toEpochMilli();
public static final long MAX_TIMESTAMP = Long.MAX_VALUE - 1;
private static final DateTimeFormatter FORMAT = DateTimeFormatter.ofPattern("d MMMM uuuu");

public boolean isValidCandidate(List<String> selectionCriteria, LocalDate date) {
Expand All @@ -28,22 +31,29 @@ public boolean isValidCandidate(List<String> selectionCriteria, LocalDate date)
}

public boolean hasValidData() {
return date != null
&& date.length == 2
&& date[0] >= MIN_TIMESTAMP
&& date[1] <= MAX_TIMESTAMP
&& date[0] <= date[1]
&& isNotBlank(name)
&& isNotBlank(hyperlink)
&& isNotBlank(location);
if (date == null || isBlank(name) || isBlank(hyperlink) || isBlank(location)) {
return false;
}

if (date.length == 2) {
return date[0] >= MIN_TIMESTAMP && date[1] <= MAX_TIMESTAMP && date[0] <= date[1];
} else if (date.length == 1) {
return date[0] >= MIN_TIMESTAMP && date[0] <= MAX_TIMESTAMP;
}

return false;
}

public boolean isOnOrAfter(LocalDate d) {
long end = date[1];
long now = d.toEpochSecond(LocalTime.MIDNIGHT, ZoneOffset.UTC);
long end = endDate();
long now = d.atStartOfDay().toInstant(ZoneOffset.UTC).toEpochMilli();
return end >= now;
}

private long endDate() {
return date.length == 1 ? date[0] : date[1];
}

public boolean matchesAnyOf(List<String> criteria) {
for (String criterion : criteria) {
if (name.contains(criterion) || location.contains(criterion)) {
Expand All @@ -57,7 +67,7 @@ public boolean matchesAnyOf(List<String> criteria) {
@Override
public String markdown(Locale locale) {
LocalDate start = timestampToDate(date[0]);
LocalDate end = timestampToDate(date[1]);
LocalDate end = timestampToDate(endDate());
DateTimeFormatter formatter = FORMAT.withLocale(locale);

String date;
Expand All @@ -73,7 +83,7 @@ public String markdown(Locale locale) {
}

private static LocalDate timestampToDate(long timestamp) {
Instant instant = Instant.ofEpochSecond(timestamp);
Instant instant = Instant.ofEpochMilli(timestamp);
ZonedDateTime dateTime = ZonedDateTime.ofInstant(instant, ZoneOffset.UTC);
return dateTime.toLocalDate();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import static com.lescastcodeurs.bot.conferences.Conference.MAX_TIMESTAMP;
import static com.lescastcodeurs.bot.conferences.Conference.MIN_TIMESTAMP;
import static java.time.LocalTime.MIDNIGHT;
import static java.time.ZoneOffset.UTC;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand All @@ -24,9 +23,9 @@ class ConferenceTest {
private static final String MISC = "CFP ends on 2023-01-01";

private static final LocalDate START = LocalDate.of(2023, 4, 12);
private static final long START_STAMP = START.toEpochSecond(MIDNIGHT, UTC);
private static final long START_STAMP = START.atStartOfDay().toInstant(UTC).toEpochMilli();
private static final LocalDate END = LocalDate.of(2023, 4, 14);
private static final long END_STAMP = END.toEpochSecond(MIDNIGHT, UTC);
private static final long END_STAMP = END.atStartOfDay().toInstant(UTC).toEpochMilli();
private static final long[] DATES = new long[] {START_STAMP, END_STAMP};

@Test
Expand All @@ -53,7 +52,6 @@ void datesMustBeValid(long[] invalidDates) {
private static Stream<Arguments> invalidDates() {
return Stream.of(
of((Object) new long[] {}),
of((Object) new long[] {MIN_TIMESTAMP}),
of((Object) new long[] {MIN_TIMESTAMP, MAX_TIMESTAMP, MAX_TIMESTAMP}),
// Disabled: https://github.com/scraly/developers-conferences-agenda/issues/319
of((Object) new long[] {MAX_TIMESTAMP, MIN_TIMESTAMP}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ void withCorrectJson() {
{
"name": "Devoxx France",
"date": [
2147483647,
2147483647
9223372036854775007,
9223372036854775007
],
"hyperlink": "https://devoxx.fr/",
"location": "France (Paris)",
Expand All @@ -47,8 +47,8 @@ void withCorrectJson() {
{
"name": "Monitorama",
"date": [
2147483647,
2147483647
9223372036854775007,
9223372036854775007
],
"hyperlink": "http://monitorama.com/",
"location": "USA",
Expand All @@ -57,8 +57,8 @@ void withCorrectJson() {
{
"name": "Best Of Web",
"date": [
-2147483648,
-2147483648
0,
0
],
"hyperlink": "http://bestofweb.paris/",
"location": "France",
Expand Down

0 comments on commit e80c2d8

Please sign in to comment.