Skip to content

Commit

Permalink
Cndit 1644 3 (#18)
Browse files Browse the repository at this point in the history
* transport max timestamp query

* update naming to data sync config
  • Loading branch information
ndduc01 authored Aug 22, 2024
1 parent 2ec1b56 commit 69a07dc
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package gov.cdc.nnddataexchangeservice.repository.rdb;

import gov.cdc.nnddataexchangeservice.repository.rdb.model.DataExchangeConfig;
import gov.cdc.nnddataexchangeservice.repository.rdb.model.DataSyncConfig;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface DataExchangeConfigRepository extends JpaRepository<DataExchangeConfig, String> {
public interface DataSyncConfigRepository extends JpaRepository<DataSyncConfig, String> {

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
import java.sql.Timestamp;

@Entity
@Table(name = "data_exchange_config")
@Table(name = "data_sync_config")
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class DataExchangeConfig {
public class DataSyncConfig {
@Id
@Column(name = "table_name")
private String tableName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
import com.google.gson.GsonBuilder;
import gov.cdc.nnddataexchangeservice.configuration.TimestampAdapter;
import gov.cdc.nnddataexchangeservice.exception.DataExchangeException;
import gov.cdc.nnddataexchangeservice.repository.rdb.DataExchangeConfigRepository;
import gov.cdc.nnddataexchangeservice.repository.rdb.DataSyncConfigRepository;
import gov.cdc.nnddataexchangeservice.service.interfaces.IDataExchangeGenericService;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.ColumnMapRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

Expand All @@ -23,13 +22,13 @@

@Service
public class DataExchangeGenericService implements IDataExchangeGenericService {
private final DataExchangeConfigRepository dataExchangeConfigRepository;
private final DataSyncConfigRepository dataSyncConfigRepository;
private final JdbcTemplate jdbcTemplate;
private final Gson gson;

public DataExchangeGenericService(DataExchangeConfigRepository dataExchangeConfigRepository,
public DataExchangeGenericService(DataSyncConfigRepository dataSyncConfigRepository,
@Qualifier("rdbJdbcTemplate") JdbcTemplate jdbcTemplate) {
this.dataExchangeConfigRepository = dataExchangeConfigRepository;
this.dataSyncConfigRepository = dataSyncConfigRepository;
this.jdbcTemplate = jdbcTemplate;

this.gson = new GsonBuilder()
Expand All @@ -42,7 +41,7 @@ public DataExchangeGenericService(DataExchangeConfigRepository dataExchangeConfi
@SuppressWarnings("javasecurity:S3649")
public String getGenericDataExchange(String tableName, String timeStamp, Integer limit) throws DataExchangeException {
// Retrieve configuration based on table name
var dataConfig = dataExchangeConfigRepository.findById(tableName).orElseThrow(() -> new DataExchangeException("Selected Table Not Found"));
var dataConfig = dataSyncConfigRepository.findById(tableName).orElseThrow(() -> new DataExchangeException("Selected Table Not Found"));

if (timeStamp == null) {
timeStamp = "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ GO
IF NOT EXISTS(
SELECT 'X'
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = 'data_exchange_config')
WHERE TABLE_NAME = 'data_sync_config')
BEGIN
CREATE TABLE data_exchange_config
CREATE TABLE data_sync_config
(
table_name NVARCHAR(255) NOT NULL PRIMARY KEY,
source_db NVARCHAR(255) NOT NULL,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
INSERT INTO [RDB].[dbo].[data_exchange_config] (table_name, source_db, query, query_with_limit) VALUES
INSERT INTO [RDB].[dbo].[data_sync_config] (table_name, source_db, query, query_with_limit) VALUES
('CONDITION', 'RDB', 'SELECT * FROM CONDITION;', NULL),
('Rdb_Date', 'RDB', 'SELECT * FROM Rdb_Date;', NULL),
('D_PATIENT', 'RDB', 'SELECT * FROM D_PATIENT WHERE D_PATIENT.PATIENT_LAST_CHANGE_TIME >= :timestamp;', 'SELECT TOP(:limit) * FROM D_PATIENT WHERE D_PATIENT.PATIENT_LAST_CHANGE_TIME >= :timestamp ORDER BY D_PATIENT.PATIENT_LAST_CHANGE_TIME ASC;'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

import com.google.gson.Gson;
import gov.cdc.nnddataexchangeservice.exception.DataExchangeException;
import gov.cdc.nnddataexchangeservice.repository.rdb.DataExchangeConfigRepository;
import gov.cdc.nnddataexchangeservice.repository.rdb.model.DataExchangeConfig;
import gov.cdc.nnddataexchangeservice.repository.rdb.DataSyncConfigRepository;
import gov.cdc.nnddataexchangeservice.repository.rdb.model.DataSyncConfig;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
Expand All @@ -25,7 +25,7 @@
class DataExchangeGenericServiceTest {

@Mock
private DataExchangeConfigRepository dataExchangeConfigRepository;
private DataSyncConfigRepository dataSyncConfigRepository;

@Mock
private JdbcTemplate jdbcTemplate;
Expand All @@ -48,7 +48,7 @@ void getGenericDataExchange_WithMissingTable_ThrowsDataExchangeException() {
String timeStamp = "2024-07-11";
int limit = 10;

when(dataExchangeConfigRepository.findById(tableName)).thenReturn(Optional.empty());
when(dataSyncConfigRepository.findById(tableName)).thenReturn(Optional.empty());

assertThrows(DataExchangeException.class, () ->
dataExchangeGenericService.getGenericDataExchange(tableName, timeStamp, limit));
Expand All @@ -59,7 +59,7 @@ void getGenericDataExchange_1() throws DataExchangeException {
String tableName = "table";
String timeStamp = null;

DataExchangeConfig config = new DataExchangeConfig();
DataSyncConfig config = new DataSyncConfig();
config.setQuery("SELECT * FROM HERE");
config.setTableName(tableName);
config.setSourceDb("RDB");
Expand All @@ -72,7 +72,7 @@ void getGenericDataExchange_1() throws DataExchangeException {
map.put("2024-08-19 15:19:49.4830000", "2024-08-19 15:19:49.4830000");
data.add(map);

when(dataExchangeConfigRepository.findById(tableName)).thenReturn(Optional.of(config));
when(dataSyncConfigRepository.findById(tableName)).thenReturn(Optional.of(config));
when(jdbcTemplate.queryForList(any())).thenReturn(data);
when(gson.toJson(data)).thenReturn("TEST");
var res = dataExchangeGenericService.getGenericDataExchange(tableName, timeStamp, limit);
Expand All @@ -83,7 +83,7 @@ void getGenericDataExchange_2() throws DataExchangeException {
String tableName = "table";
String timeStamp = "2024-01-01";

DataExchangeConfig config = new DataExchangeConfig();
DataSyncConfig config = new DataSyncConfig();
config.setQuery("SELECT * FROM HERE :timestamp");
config.setTableName(tableName);
config.setSourceDb("RDB");
Expand All @@ -96,7 +96,7 @@ void getGenericDataExchange_2() throws DataExchangeException {
map.put("2024-08-19 15:19:49.4830000", "2024-08-19 15:19:49.4830000");
data.add(map);

when(dataExchangeConfigRepository.findById(tableName)).thenReturn(Optional.of(config));
when(dataSyncConfigRepository.findById(tableName)).thenReturn(Optional.of(config));
when(jdbcTemplate.queryForList(any())).thenReturn(data);
when(gson.toJson(data)).thenReturn("TEST");
var res = dataExchangeGenericService.getGenericDataExchange(tableName, timeStamp, limit);
Expand All @@ -108,7 +108,7 @@ void getGenericDataExchange_3() throws DataExchangeException {
String tableName = "table";
String timeStamp = "2024-01-01";

DataExchangeConfig config = new DataExchangeConfig();
DataSyncConfig config = new DataSyncConfig();
config.setQuery("SELECT * FROM HERE :timestamp ");
config.setQueryWithLimit("SELECT * FROM HERE :timestamp :limit");
config.setTableName(tableName);
Expand All @@ -122,7 +122,7 @@ void getGenericDataExchange_3() throws DataExchangeException {
map.put("2024-08-19 15:19:49.4830000", "2024-08-19 15:19:49.4830000");
data.add(map);

when(dataExchangeConfigRepository.findById(tableName)).thenReturn(Optional.of(config));
when(dataSyncConfigRepository.findById(tableName)).thenReturn(Optional.of(config));
when(jdbcTemplate.queryForList(any())).thenReturn(data);
when(gson.toJson(data)).thenReturn("TEST");
var res = dataExchangeGenericService.getGenericDataExchange(tableName, timeStamp, limit);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public interface TransportQOutRepository extends JpaRepository<TransportQOut, Lo
@Query("SELECT MAX(a.messageCreationTime) FROM TransportQOut a ")
Optional<String> findMaxTimeStamp ();

@Query(value = "SELECT MAX(a.messageCreationTime) FROM TransportQ_out a WHERE a.messageId NOT IN (SELECT DISTINCT notification_local_id FROM CN_transportq_out)", nativeQuery = true)
Optional<String> findMaxTimeStampInvolvingWithNotification();

@Modifying
@Transactional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void truncatingData() {

public String getMaxTimestamp() throws DataPollException {
try {
var time = transportQOutRepository.findMaxTimeStamp();
var time = transportQOutRepository.findMaxTimeStampInvolvingWithNotification();
if (time.isPresent()) {
return time.get().toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,31 +40,31 @@ void setUp() {
@Test
void testGetMaxTimestamp_WithTimestamp() throws DataPollException {
Timestamp maxTimestamp = new Timestamp(System.currentTimeMillis());
when(transportQOutRepository.findMaxTimeStamp()).thenReturn(Optional.of(maxTimestamp.toString()));
when(transportQOutRepository.findMaxTimeStampInvolvingWithNotification()).thenReturn(Optional.of(maxTimestamp.toString()));

String result = transportQOutService.getMaxTimestamp();

assertEquals(maxTimestamp.toString(), result);
verify(transportQOutRepository, times(1)).findMaxTimeStamp();
verify(transportQOutRepository, times(1)).findMaxTimeStampInvolvingWithNotification();
}

@Test
void testGetMaxTimestamp_NoTimestamp() throws DataPollException {
when(transportQOutRepository.findMaxTimeStamp()).thenReturn(Optional.empty());
when(transportQOutRepository.findMaxTimeStampInvolvingWithNotification()).thenReturn(Optional.empty());

String result = transportQOutService.getMaxTimestamp();

assertEquals("", result);
verify(transportQOutRepository, times(1)).findMaxTimeStamp();
verify(transportQOutRepository, times(1)).findMaxTimeStampInvolvingWithNotification();
}

@Test
void testGetMaxTimestamp_Exception() {
when(transportQOutRepository.findMaxTimeStamp()).thenThrow(new RuntimeException("Exception"));
when(transportQOutRepository.findMaxTimeStampInvolvingWithNotification()).thenThrow(new RuntimeException("Exception"));

assertThrows(DataPollException.class, () -> transportQOutService.getMaxTimestamp());

verify(transportQOutRepository, times(1)).findMaxTimeStamp();
verify(transportQOutRepository, times(1)).findMaxTimeStampInvolvingWithNotification();
}

@Test
Expand Down

0 comments on commit 69a07dc

Please sign in to comment.