From ba7a616f9ba092f7500f9872730d87a944af0d28 Mon Sep 17 00:00:00 2001 From: Selvarasu Sathiah <140747625+ssathiah@users.noreply.github.com> Date: Mon, 22 Apr 2024 09:05:10 -0400 Subject: [PATCH 1/5] Removed the option option as timestamp to be set from the java code. --- .../deadletter/repository/model/ElrDeadLetterModel.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/data-ingestion-service/src/main/java/gov/cdc/dataingestion/deadletter/repository/model/ElrDeadLetterModel.java b/data-ingestion-service/src/main/java/gov/cdc/dataingestion/deadletter/repository/model/ElrDeadLetterModel.java index b705ae1d5..840eeb3ad 100644 --- a/data-ingestion-service/src/main/java/gov/cdc/dataingestion/deadletter/repository/model/ElrDeadLetterModel.java +++ b/data-ingestion-service/src/main/java/gov/cdc/dataingestion/deadletter/repository/model/ElrDeadLetterModel.java @@ -34,9 +34,7 @@ public class ElrDeadLetterModel { @Column(name="message") private String message; - @Basic(optional = false) - @Column(name = "created_on",insertable = false, updatable = false) - @Temporal(TemporalType.TIMESTAMP) + @Column(name = "created_on") private Timestamp createdOn; @Column(name = "updated_on") From d465e1104a212753ddc76e0e219f04ca0ee227a6 Mon Sep 17 00:00:00 2001 From: Selvarasu Sathiah <140747625+ssathiah@users.noreply.github.com> Date: Mon, 22 Apr 2024 09:06:20 -0400 Subject: [PATCH 2/5] Update application.yaml Set the timezone for the JPA --- .../src/main/resources/application.yaml | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/data-ingestion-service/src/main/resources/application.yaml b/data-ingestion-service/src/main/resources/application.yaml index 05d677413..a497dda7c 100644 --- a/data-ingestion-service/src/main/resources/application.yaml +++ b/data-ingestion-service/src/main/resources/application.yaml @@ -56,6 +56,14 @@ spring: show-sql: true hibernate: ddl-auto: none + properties: + hibernate: + default_schema: dbo + format_sql: true + jdbc: + time_zone: UTC + timezone: + default_storage: NORMALIZE kafka: bootstrap-servers: ${BOOTSTRAP_SERVERS} group-id: data-ingestion-group @@ -111,6 +119,14 @@ spring: show-sql: true hibernate: ddl-auto: none + properties: + hibernate: + default_schema: dbo + format_sql: true + jdbc: + time_zone: UTC + timezone: + default_storage: NORMALIZE kafka: bootstrap-servers: ${BOOTSTRAP_SERVERS} group-id: data-ingestion-group @@ -155,4 +171,7 @@ sftp: enabled: ${DI_SFTP_ENABLED:disabled} host: ${DI_SFTP_HOST:dummyhost} username: ${DI_SFTP_USER:dummyuser} - password: ${DI_SFTP_PWD:dummypwd} \ No newline at end of file + password: ${DI_SFTP_PWD:dummypwd} + +app: + timezone: ${DI_TIMEZONE:UTC} \ No newline at end of file From bbe0f265ab0419e9c79919801f8be4d42c539c20 Mon Sep 17 00:00:00 2001 From: Selvarasu Sathiah <140747625+ssathiah@users.noreply.github.com> Date: Mon, 22 Apr 2024 09:07:02 -0400 Subject: [PATCH 3/5] Externalizing the time zone. --- .../share/helper/TimeStampHelper.java | 25 +++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/data-ingestion-service/src/main/java/gov/cdc/dataingestion/share/helper/TimeStampHelper.java b/data-ingestion-service/src/main/java/gov/cdc/dataingestion/share/helper/TimeStampHelper.java index c1404f3ff..ef73c3afa 100644 --- a/data-ingestion-service/src/main/java/gov/cdc/dataingestion/share/helper/TimeStampHelper.java +++ b/data-ingestion-service/src/main/java/gov/cdc/dataingestion/share/helper/TimeStampHelper.java @@ -1,24 +1,45 @@ package gov.cdc.dataingestion.share.helper; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; + import java.sql.Timestamp; import java.time.Instant; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.ZonedDateTime; +@Service public class TimeStampHelper { - private TimeStampHelper() { + private static String TIME_ZONE; + + private TimeStampHelper() { } + + /** + * Create timestamp with the given timezone. Default UTC + * Timezones: UTC America/New_York America/Chicago America/Denver America/Phoenix America/Los_Angeles + * @return Timestamp + */ public static Timestamp getCurrentTimeStamp() { // Another Option: Timestamp.from(ZonedDateTime.now().toInstant()) //NOSONAR //return Timestamp.from(Instant.now());//old implementation. //NOSONAR + + System.out.println("input timezone: " + TIME_ZONE); + if (TIME_ZONE == null || TIME_ZONE.isBlank()) { + TIME_ZONE="UTC"; + } LocalDateTime ldt = LocalDateTime.now(); ZonedDateTime zdt = ZonedDateTime.of(ldt, ZoneId.systemDefault()); - ZonedDateTime gmt = zdt.withZoneSameInstant(ZoneId.of("UTC")); + ZonedDateTime gmt = zdt.withZoneSameInstant(ZoneId.of(TIME_ZONE)); return Timestamp.valueOf(gmt.toLocalDateTime()); } public static Instant getInstantNow() { return Instant.now(); } + @Value("${app.timezone}") + public void setDatabase(String timeZone){ + TIME_ZONE=timeZone; + } } From 517ce250d3e1cb46f41a563ee6e06c9ef7ed5041 Mon Sep 17 00:00:00 2001 From: Selvarasu Sathiah <140747625+ssathiah@users.noreply.github.com> Date: Mon, 22 Apr 2024 10:29:21 -0400 Subject: [PATCH 4/5] Added unit test cases --- .../share/helper/TimeStampHelper.java | 16 ++++--- .../share/helper/TimeStampHelperTest.java | 45 +++++++++++++++++++ 2 files changed, 54 insertions(+), 7 deletions(-) create mode 100644 data-ingestion-service/src/test/java/gov/cdc/dataingestion/share/helper/TimeStampHelperTest.java diff --git a/data-ingestion-service/src/main/java/gov/cdc/dataingestion/share/helper/TimeStampHelper.java b/data-ingestion-service/src/main/java/gov/cdc/dataingestion/share/helper/TimeStampHelper.java index ef73c3afa..4c2d78ae6 100644 --- a/data-ingestion-service/src/main/java/gov/cdc/dataingestion/share/helper/TimeStampHelper.java +++ b/data-ingestion-service/src/main/java/gov/cdc/dataingestion/share/helper/TimeStampHelper.java @@ -12,7 +12,7 @@ @Service public class TimeStampHelper { - private static String TIME_ZONE; + private static String timeZone; private TimeStampHelper() { } @@ -26,20 +26,22 @@ public static Timestamp getCurrentTimeStamp() { // Another Option: Timestamp.from(ZonedDateTime.now().toInstant()) //NOSONAR //return Timestamp.from(Instant.now());//old implementation. //NOSONAR - System.out.println("input timezone: " + TIME_ZONE); - if (TIME_ZONE == null || TIME_ZONE.isBlank()) { - TIME_ZONE="UTC"; + if (timeZone == null || timeZone.isBlank()) { + timeZone="UTC"; } LocalDateTime ldt = LocalDateTime.now(); ZonedDateTime zdt = ZonedDateTime.of(ldt, ZoneId.systemDefault()); - ZonedDateTime gmt = zdt.withZoneSameInstant(ZoneId.of(TIME_ZONE)); + ZonedDateTime gmt = zdt.withZoneSameInstant(ZoneId.of(timeZone)); return Timestamp.valueOf(gmt.toLocalDateTime()); } public static Instant getInstantNow() { return Instant.now(); } @Value("${app.timezone}") - public void setDatabase(String timeZone){ - TIME_ZONE=timeZone; + public void setEnvTimeZone(String envTimeZone){ + timeZone=envTimeZone; + } + public String getEnvTimeZone(){ + return timeZone; } } diff --git a/data-ingestion-service/src/test/java/gov/cdc/dataingestion/share/helper/TimeStampHelperTest.java b/data-ingestion-service/src/test/java/gov/cdc/dataingestion/share/helper/TimeStampHelperTest.java new file mode 100644 index 000000000..0e16b4da4 --- /dev/null +++ b/data-ingestion-service/src/test/java/gov/cdc/dataingestion/share/helper/TimeStampHelperTest.java @@ -0,0 +1,45 @@ +package gov.cdc.dataingestion.share.helper; + +import org.junit.jupiter.api.Test; + +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +class TimeStampHelperTest { + + @Test + void testGetCurrentTimeStampWithEnvTimeZone() throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException { + //TimeStampHelper + Constructor pcc = TimeStampHelper.class.getDeclaredConstructor(); + pcc.setAccessible(true); + TimeStampHelper privateConstructorInstance = pcc.newInstance(); + privateConstructorInstance.setEnvTimeZone("UTC"); + assertEquals("UTC", privateConstructorInstance.getEnvTimeZone()); + assertNotNull(TimeStampHelper.getCurrentTimeStamp()); + } + + @Test + void testGetCurrentTimeStampWithEmptyEnvTimeZone() throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException { + //TimeStampHelper + Constructor pcc = TimeStampHelper.class.getDeclaredConstructor(); + pcc.setAccessible(true); + TimeStampHelper privateConstructorInstance = pcc.newInstance(); + privateConstructorInstance.setEnvTimeZone(""); + assertEquals("", privateConstructorInstance.getEnvTimeZone()); + assertNotNull(TimeStampHelper.getCurrentTimeStamp()); + } + + @Test + void testGetCurrentTimeStampWithNullEnvTimeZone() throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException { + //TimeStampHelper + Constructor pcc = TimeStampHelper.class.getDeclaredConstructor(); + pcc.setAccessible(true); + TimeStampHelper privateConstructorInstance = pcc.newInstance(); + privateConstructorInstance.setEnvTimeZone(null); + assertEquals(null, privateConstructorInstance.getEnvTimeZone()); + assertNotNull(TimeStampHelper.getCurrentTimeStamp()); + } +} \ No newline at end of file From 4527b4f5d77ea43a52d9e61febfb7b9890963a2d Mon Sep 17 00:00:00 2001 From: Selvarasu Sathiah <140747625+ssathiah@users.noreply.github.com> Date: Mon, 22 Apr 2024 10:56:48 -0400 Subject: [PATCH 5/5] Updated test cases --- .../share/helper/TimeStampHelper.java | 3 +- .../share/helper/TimeStampHelperTest.java | 37 +++++-------------- 2 files changed, 11 insertions(+), 29 deletions(-) diff --git a/data-ingestion-service/src/main/java/gov/cdc/dataingestion/share/helper/TimeStampHelper.java b/data-ingestion-service/src/main/java/gov/cdc/dataingestion/share/helper/TimeStampHelper.java index 4c2d78ae6..ac71bcc7a 100644 --- a/data-ingestion-service/src/main/java/gov/cdc/dataingestion/share/helper/TimeStampHelper.java +++ b/data-ingestion-service/src/main/java/gov/cdc/dataingestion/share/helper/TimeStampHelper.java @@ -37,6 +37,7 @@ public static Timestamp getCurrentTimeStamp() { public static Instant getInstantNow() { return Instant.now(); } + @SuppressWarnings("java:S2696") @Value("${app.timezone}") public void setEnvTimeZone(String envTimeZone){ timeZone=envTimeZone; @@ -44,4 +45,4 @@ public void setEnvTimeZone(String envTimeZone){ public String getEnvTimeZone(){ return timeZone; } -} +} \ No newline at end of file diff --git a/data-ingestion-service/src/test/java/gov/cdc/dataingestion/share/helper/TimeStampHelperTest.java b/data-ingestion-service/src/test/java/gov/cdc/dataingestion/share/helper/TimeStampHelperTest.java index 0e16b4da4..e9e8149b4 100644 --- a/data-ingestion-service/src/test/java/gov/cdc/dataingestion/share/helper/TimeStampHelperTest.java +++ b/data-ingestion-service/src/test/java/gov/cdc/dataingestion/share/helper/TimeStampHelperTest.java @@ -1,6 +1,8 @@ package gov.cdc.dataingestion.share.helper; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.NullSource; +import org.junit.jupiter.params.provider.ValueSource; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; @@ -10,36 +12,15 @@ class TimeStampHelperTest { - @Test - void testGetCurrentTimeStampWithEnvTimeZone() throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException { - //TimeStampHelper + @ParameterizedTest + @ValueSource(strings = {"UTC", ""}) + @NullSource + void testGetCurrentTimeStampWithEnvTimeZone(String timeStamp) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException { Constructor pcc = TimeStampHelper.class.getDeclaredConstructor(); pcc.setAccessible(true); TimeStampHelper privateConstructorInstance = pcc.newInstance(); - privateConstructorInstance.setEnvTimeZone("UTC"); - assertEquals("UTC", privateConstructorInstance.getEnvTimeZone()); - assertNotNull(TimeStampHelper.getCurrentTimeStamp()); - } - - @Test - void testGetCurrentTimeStampWithEmptyEnvTimeZone() throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException { - //TimeStampHelper - Constructor pcc = TimeStampHelper.class.getDeclaredConstructor(); - pcc.setAccessible(true); - TimeStampHelper privateConstructorInstance = pcc.newInstance(); - privateConstructorInstance.setEnvTimeZone(""); - assertEquals("", privateConstructorInstance.getEnvTimeZone()); - assertNotNull(TimeStampHelper.getCurrentTimeStamp()); - } - - @Test - void testGetCurrentTimeStampWithNullEnvTimeZone() throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException { - //TimeStampHelper - Constructor pcc = TimeStampHelper.class.getDeclaredConstructor(); - pcc.setAccessible(true); - TimeStampHelper privateConstructorInstance = pcc.newInstance(); - privateConstructorInstance.setEnvTimeZone(null); - assertEquals(null, privateConstructorInstance.getEnvTimeZone()); + privateConstructorInstance.setEnvTimeZone(timeStamp); + assertEquals(timeStamp, privateConstructorInstance.getEnvTimeZone()); assertNotNull(TimeStampHelper.getCurrentTimeStamp()); } } \ No newline at end of file