Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Use Duration class instead of custom util to calculate seconds from date to now #1249

Merged
merged 1 commit into from
Apr 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import io.jans.orm.annotation.ObjectClass;

import java.io.Serializable;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
Expand Down Expand Up @@ -86,7 +87,8 @@ public void setTtl(Integer ttl) {
}

public void resetTtlFromExpirationDate() {
final Integer calculatedTtl = Util.getNumberOfSecondFromNow(getExpirationDate());
final Long duration = Duration.between(new Date().toInstant(), getExpirationDate().toInstant()).getSeconds();
final Integer calculatedTtl = duration.intValue();
if (calculatedTtl != null) {
setTtl(calculatedTtl);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import jakarta.validation.constraints.NotNull;
import java.io.Serializable;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
Expand Down Expand Up @@ -91,7 +92,8 @@ public void setTtl(Integer ttl) {
}

public void resetTtlFromExpirationDate() {
final Integer calculatedTtl = Util.getNumberOfSecondFromNow(getExpirationDate());
final Long duration = Duration.between(new Date().toInstant(), getExpirationDate().toInstant()).getSeconds();
final Integer calculatedTtl = duration.intValue();
if (calculatedTtl != null) {
setTtl(calculatedTtl);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
Expand Down Expand Up @@ -330,20 +331,6 @@ public static String byteArrayToHexString(byte[] b) {
return result.toString();
}

public static Integer getNumberOfSecondFromNow(Date date) {
if (date == null) {
return 0;
}

long now = new Date().getTime();
final long time = date.getTime();
if (time > now) {
return (int) (time - now) / 1000;
}

return null;
}

public static Date createExpirationDate(Integer lifetimeInSeconds) {
if (lifetimeInSeconds == null || lifetimeInSeconds == 0)
throw new IllegalArgumentException("lifetime can't be null or zero");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -416,33 +416,6 @@ public void byteArrayToHexString_byteArray_hexString() {
assertEquals(result, "48656c6c6f20776f726c64");
}

@Test
public void getNumberOfSecondFromNow_null_zero() {
showTitle("getNumberOfSecondFromNow_null_zero");
Integer seconds = Util.getNumberOfSecondFromNow(null);
assertNotNull(seconds);
assertEquals(seconds, Integer.valueOf(0));
}

@Test
public void getNumberOfSecondFromNow_dateInPast_null() {
showTitle("getNumberOfSecondFromNow_dateInPast_null");
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(cal.getTimeInMillis() - 10000L);
Integer seconds = Util.getNumberOfSecondFromNow(cal.getTime());
assertNull(seconds);
}

@Test
public void getNumberOfSecondFromNow_dateInFuture_intNumberOfSeconds() {
showTitle("getNumberOfSecondFromNow_dateInFuture_intNumberOfSeconds");
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(cal.getTimeInMillis() + 10999L);
Integer seconds = Util.getNumberOfSecondFromNow(cal.getTime());
assertNotNull(seconds);
assertTrue(seconds >= 10);
}

@Test
public void createExpirationDate_nullLifeTime_IllegalArgumentException() {
showTitle("createExpirationDate_nullLifeTime_IllegalArgumentException");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import io.jans.orm.model.base.Deletable;

import java.io.Serializable;
import java.time.Duration;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -105,7 +106,8 @@ private void initTtl() {
}

public void resetTtlFromExpirationDate() {
final Integer seconds = Util.getNumberOfSecondFromNow(getExpirationDate());
final Long duration = Duration.between(new Date().toInstant(), getExpirationDate().toInstant()).getSeconds();
final Integer seconds = duration.intValue();
if (seconds != null) {
this.ttl = seconds;
}
Expand Down