diff --git a/src/main/java/com/switcherapi/client/service/remote/ClientRemoteService.java b/src/main/java/com/switcherapi/client/service/remote/ClientRemoteService.java index 8739ebf..ab21b7b 100644 --- a/src/main/java/com/switcherapi/client/service/remote/ClientRemoteService.java +++ b/src/main/java/com/switcherapi/client/service/remote/ClientRemoteService.java @@ -17,7 +17,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.Date; +import java.time.Instant; import java.util.Objects; import java.util.Optional; import java.util.Set; @@ -147,7 +147,7 @@ private void setSilentModeExpiration() throws SwitcherInvalidDateTimeArgumentExc final AuthResponse response = new AuthResponse(); response.setToken(ContextKey.SILENT_MODE.getParam()); - response.setExp(SwitcherUtils.addTimeDuration(addValue, new Date()).getTime()/1000); + response.setExp(SwitcherUtils.addTimeDuration(addValue, Instant.now()).getEpochSecond()); this.authResponse = response; } } diff --git a/src/main/java/com/switcherapi/client/service/validators/DateValidator.java b/src/main/java/com/switcherapi/client/service/validators/DateValidator.java index f31161f..ad6a208 100644 --- a/src/main/java/com/switcherapi/client/service/validators/DateValidator.java +++ b/src/main/java/com/switcherapi/client/service/validators/DateValidator.java @@ -7,14 +7,15 @@ import com.switcherapi.client.model.EntryOperation; import com.switcherapi.client.model.StrategyValidator; import com.switcherapi.client.model.criteria.StrategyConfig; -import org.apache.commons.lang3.time.DateUtils; -import java.text.ParseException; -import java.util.Date; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeParseException; public class DateValidator extends DateTimeValidator { public static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss"; + private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern(DATE_FORMAT); @Override public StrategyValidator getType() { @@ -27,34 +28,34 @@ public boolean process(final StrategyConfig strategyConfig, final Entry switcher try { return selectDateOperationCase(strategyConfig, switcherInput); - } catch (ParseException e) { + } catch (DateTimeParseException e) { throw new SwitcherInvalidTimeFormat(strategyConfig.getStrategy(), e); } } - private boolean selectDateOperationCase(final StrategyConfig strategyConfig, final Entry switcherInput) throws ParseException { - Date stgDate; - Date stgDate2; - Date inputDate; + private boolean selectDateOperationCase(final StrategyConfig strategyConfig, final Entry switcherInput) { + LocalDateTime stgDate; + LocalDateTime stgDate2; + LocalDateTime inputDate; switch (strategyConfig.getEntryOperation()) { case LOWER: - stgDate = DateUtils.parseDate(getFullDate(strategyConfig.getValues()[0]), DATE_FORMAT); - inputDate = DateUtils.parseDate(getFullDate(switcherInput.getInput()), DATE_FORMAT); + stgDate = LocalDateTime.parse(getFullDate(strategyConfig.getValues()[0]), FORMATTER); + inputDate = LocalDateTime.parse(getFullDate(switcherInput.getInput()), FORMATTER); - return inputDate.before(stgDate); + return inputDate.isBefore(stgDate); case GREATER: - stgDate = DateUtils.parseDate(getFullDate(strategyConfig.getValues()[0]), DATE_FORMAT); - inputDate = DateUtils.parseDate(getFullDate(switcherInput.getInput()), DATE_FORMAT); + stgDate = LocalDateTime.parse(getFullDate(strategyConfig.getValues()[0]), FORMATTER); + inputDate = LocalDateTime.parse(getFullDate(switcherInput.getInput()), FORMATTER); - return inputDate.after(stgDate); + return inputDate.isAfter(stgDate); case BETWEEN: if (strategyConfig.getValues().length == 2) { - stgDate = DateUtils.parseDate(getFullDate(strategyConfig.getValues()[0]), DATE_FORMAT); - stgDate2 = DateUtils.parseDate(getFullDate(strategyConfig.getValues()[1]), DATE_FORMAT); - inputDate = DateUtils.parseDate(getFullDate(switcherInput.getInput()), DATE_FORMAT); + stgDate = LocalDateTime.parse(getFullDate(strategyConfig.getValues()[0]), FORMATTER); + stgDate2 = LocalDateTime.parse(getFullDate(strategyConfig.getValues()[1]), FORMATTER); + inputDate = LocalDateTime.parse(getFullDate(switcherInput.getInput()), FORMATTER); - return inputDate.after(stgDate) && inputDate.before(stgDate2); + return inputDate.isAfter(stgDate) && inputDate.isBefore(stgDate2); } throw new SwitcherInvalidOperationInputException(EntryOperation.BETWEEN.name()); diff --git a/src/main/java/com/switcherapi/client/service/validators/TimeValidator.java b/src/main/java/com/switcherapi/client/service/validators/TimeValidator.java index 10da884..8d9e9bf 100644 --- a/src/main/java/com/switcherapi/client/service/validators/TimeValidator.java +++ b/src/main/java/com/switcherapi/client/service/validators/TimeValidator.java @@ -7,15 +7,17 @@ import com.switcherapi.client.model.EntryOperation; import com.switcherapi.client.model.StrategyValidator; import com.switcherapi.client.model.criteria.StrategyConfig; -import org.apache.commons.lang3.time.DateUtils; -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.Date; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeParseException; public class TimeValidator extends DateTimeValidator { public static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss"; + private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern(DATE_FORMAT); @Override public StrategyValidator getType() { @@ -27,40 +29,37 @@ public boolean process(StrategyConfig strategyConfig, Entry switcherInput) throw SwitcherInvalidTimeFormat, SwitcherInvalidOperationInputException { try { - final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); - final String today = format.format(new Date()); + final String today = LocalDate.now(ZoneId.systemDefault()).toString(); return selectTimeOperationCase(strategyConfig, switcherInput, today); - } catch (ParseException e) { + } catch (DateTimeParseException e) { throw new SwitcherInvalidTimeFormat(strategyConfig.getStrategy(), e); } } - private boolean selectTimeOperationCase(final StrategyConfig strategyConfig, final Entry switcherInput, final String today) - throws ParseException { - Date stgDate; - Date stgDate2; - Date inputDate; + private boolean selectTimeOperationCase(final StrategyConfig strategyConfig, final Entry switcherInput, final String today) { + LocalDateTime stgDate; + LocalDateTime stgDate2; + LocalDateTime inputDate; switch (strategyConfig.getEntryOperation()) { case LOWER: - stgDate = DateUtils.parseDate(getFullTime(today, strategyConfig.getValues()[0]), DATE_FORMAT); - inputDate = DateUtils.parseDate(getFullTime(today, switcherInput.getInput()), DATE_FORMAT); + stgDate = LocalDateTime.parse(getFullTime(today, strategyConfig.getValues()[0]), FORMATTER); + inputDate = LocalDateTime.parse(getFullTime(today, switcherInput.getInput()), FORMATTER); - return inputDate.before(stgDate); + return inputDate.isBefore(stgDate); case GREATER: - stgDate = DateUtils.parseDate(getFullTime(today, strategyConfig.getValues()[0]), DATE_FORMAT); - inputDate = DateUtils.parseDate(getFullTime(today, switcherInput.getInput()), DATE_FORMAT); + stgDate = LocalDateTime.parse(getFullTime(today, strategyConfig.getValues()[0]), FORMATTER); + inputDate = LocalDateTime.parse(getFullTime(today, switcherInput.getInput()), FORMATTER); - return inputDate.after(stgDate); + return inputDate.isAfter(stgDate); case BETWEEN: if (strategyConfig.getValues().length == 2) { - stgDate = DateUtils.parseDate(getFullTime(today, strategyConfig.getValues()[0]), DATE_FORMAT); - stgDate2 = DateUtils.parseDate(getFullTime(today, strategyConfig.getValues()[1]), DATE_FORMAT); - inputDate = DateUtils.parseDate(getFullTime(today, switcherInput.getInput()), - DATE_FORMAT); + stgDate = LocalDateTime.parse(getFullTime(today, strategyConfig.getValues()[0]), FORMATTER); + stgDate2 = LocalDateTime.parse(getFullTime(today, strategyConfig.getValues()[1]), FORMATTER); + inputDate = LocalDateTime.parse(getFullTime(today, switcherInput.getInput()), FORMATTER); - return inputDate.after(stgDate) && inputDate.before(stgDate2); + return inputDate.isAfter(stgDate) && inputDate.isBefore(stgDate2); } throw new SwitcherInvalidOperationInputException(EntryOperation.BETWEEN.name()); diff --git a/src/main/java/com/switcherapi/client/utils/SwitcherUtils.java b/src/main/java/com/switcherapi/client/utils/SwitcherUtils.java index 89b6366..7994ea0 100644 --- a/src/main/java/com/switcherapi/client/utils/SwitcherUtils.java +++ b/src/main/java/com/switcherapi/client/utils/SwitcherUtils.java @@ -7,9 +7,10 @@ import com.google.gson.JsonObject; import com.google.gson.JsonParser; import org.apache.commons.lang3.StringUtils; -import org.apache.commons.lang3.time.DateUtils; import org.slf4j.Logger; +import java.time.Instant; +import java.time.temporal.ChronoUnit; import java.util.*; import java.util.Map.Entry; import java.util.regex.Matcher; @@ -28,17 +29,17 @@ public class SwitcherUtils { private SwitcherUtils() {} - public static Date addTimeDuration(final String addValue, final Date date) + public static Instant addTimeDuration(final String addValue, final Instant instant) throws SwitcherInvalidDateTimeArgumentException { switch (addValue.charAt(addValue.length() - 1)) { case 's': - return DateUtils.addSeconds(date, Integer.parseInt(addValue.replace(DURATION_UNIT[0], StringUtils.EMPTY))); + return instant.plus(Integer.parseInt(addValue.replace(DURATION_UNIT[0], StringUtils.EMPTY)), ChronoUnit.SECONDS); case 'm': - return DateUtils.addMinutes(date, Integer.parseInt(addValue.replace(DURATION_UNIT[1], StringUtils.EMPTY))); + return instant.plus(Integer.parseInt(addValue.replace(DURATION_UNIT[1], StringUtils.EMPTY)), ChronoUnit.MINUTES); case 'h': - return DateUtils.addHours(date, Integer.parseInt(addValue.replace(DURATION_UNIT[2], StringUtils.EMPTY))); + return instant.plus(Integer.parseInt(addValue.replace(DURATION_UNIT[2], StringUtils.EMPTY)), ChronoUnit.HOURS); case 'd': - return DateUtils.addDays(date, Integer.parseInt(addValue.replace(DURATION_UNIT[3], StringUtils.EMPTY))); + return instant.plus(Integer.parseInt(addValue.replace(DURATION_UNIT[3], StringUtils.EMPTY)), ChronoUnit.DAYS); default: throw new SwitcherInvalidDateTimeArgumentException(addValue); } diff --git a/src/test/java/com/switcherapi/client/utils/SwitcherUtilsTest.java b/src/test/java/com/switcherapi/client/utils/SwitcherUtilsTest.java index 5a5540b..3a1bd1c 100644 --- a/src/test/java/com/switcherapi/client/utils/SwitcherUtilsTest.java +++ b/src/test/java/com/switcherapi/client/utils/SwitcherUtilsTest.java @@ -5,14 +5,13 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import java.nio.file.Paths; -import java.text.ParseException; -import java.util.Date; +import java.time.Instant; +import java.time.LocalDateTime; +import java.time.ZoneOffset; +import java.time.format.DateTimeFormatter; import java.util.Properties; import java.util.stream.Stream; - import org.apache.commons.lang3.StringUtils; -import org.apache.commons.lang3.time.DateFormatUtils; -import org.apache.commons.lang3.time.DateUtils; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.EnabledOnJre; @@ -60,10 +59,14 @@ void shouldReturnError_snapshotHasErrors() { SwitcherContext::initializeClient); } + private static final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").withZone(ZoneOffset.UTC); + private static final String BASE_DATE = "2019-12-10 10:00:00"; + @Test - void shouldReturnInvalidFormat() throws ParseException { - Date date1 = DateUtils.parseDate("2019-12-10 10:00:00", "yyyy-MM-dd HH:mm:ss"); - assertThrows(Exception.class, () -> SwitcherUtils.addTimeDuration("1w", date1)); + void shouldReturnInvalidFormat() { + Instant instant = LocalDateTime.parse(BASE_DATE, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) + .toInstant(ZoneOffset.UTC); + assertThrows(Exception.class, () -> SwitcherUtils.addTimeDuration("1w", instant)); } /** @@ -83,10 +86,11 @@ static Stream timeArguments() { @ParameterizedTest() @MethodSource("timeArguments") - void shouldAddTime(String time, String expectedValue) throws ParseException { - Date date1 = DateUtils.parseDate("2019-12-10 10:00:00", "yyyy-MM-dd HH:mm:ss"); - date1 = SwitcherUtils.addTimeDuration(time, date1); - String dateString = DateFormatUtils.format(date1, "yyyy-MM-dd HH:mm:ss"); + void shouldAddTime(String time, String expectedValue) { + Instant instant = LocalDateTime.parse(BASE_DATE, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) + .toInstant(ZoneOffset.UTC); + Instant result = SwitcherUtils.addTimeDuration(time, instant); + String dateString = DATE_FORMAT.format(result); assertEquals(expectedValue, dateString); } diff --git a/src/test/java/com/switcherapi/fixture/MockWebServerHelper.java b/src/test/java/com/switcherapi/fixture/MockWebServerHelper.java index 78ac9ca..72019a0 100644 --- a/src/test/java/com/switcherapi/fixture/MockWebServerHelper.java +++ b/src/test/java/com/switcherapi/fixture/MockWebServerHelper.java @@ -12,7 +12,7 @@ import mockwebserver3.QueueDispatcher; import java.io.IOException; -import java.util.Date; +import java.time.Instant; import java.util.Set; import java.util.concurrent.TimeUnit; @@ -50,7 +50,7 @@ protected MockResponse generateTimeOut(int timeoutMs) { protected MockResponse generateMockAuth(int secondsAhead) { MockResponse.Builder builder = new MockResponse.Builder(); builder.body(String.format("{ \"token\": \"%s\", \"exp\": \"%s\" }", - "mocked_token", SwitcherUtils.addTimeDuration(secondsAhead + "s", new Date()).getTime()/1000)); + "mocked_token", SwitcherUtils.addTimeDuration(secondsAhead + "s", Instant.now()).getEpochSecond())); builder.addHeader("Content-Type", "application/json"); return builder.build(); }