Skip to content

Commit

Permalink
Dev clean up 2 (#21)
Browse files Browse the repository at this point in the history
* clean up

* added more query

* added a check to return empty string if no match found

* aded log

* update
  • Loading branch information
ndduc01 authored Aug 29, 2024
1 parent 9f41def commit 38812a3
Show file tree
Hide file tree
Showing 13 changed files with 45 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public ResponseEntity<String> exchangingData(@RequestParam("cnStatusTime") Strin
@GetMapping(path = "/api/data-exchange-generic/{tableName}")
public ResponseEntity<String> exchangingData(@PathVariable String tableName, @RequestParam(required = false) String timestamp,
@RequestHeader(name = "limit", defaultValue = "0") String limit,
@RequestHeader(name = "initial_load", defaultValue = "false", required = false) String initialLoadApplied) throws DataExchangeException {
@RequestHeader(name = "initialLoad", defaultValue = "false", required = false) String initialLoadApplied) throws DataExchangeException {
int intLimit = Integer.parseInt(limit);

boolean initialLoad = initialLoadApplied.equalsIgnoreCase("true");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import gov.cdc.nnddataexchangeservice.configuration.TimestampAdapter;
import gov.cdc.nnddataexchangeservice.exception.DataExchangeException;
import gov.cdc.nnddataexchangeservice.repository.rdb.DataSyncConfigRepository;
import gov.cdc.nnddataexchangeservice.repository.rdb.model.DataSyncConfig;
import gov.cdc.nnddataexchangeservice.service.interfaces.IDataExchangeGenericService;
import gov.cdc.nnddataexchangeservice.shared.DataSimplification;
import org.springframework.beans.factory.annotation.Qualifier;
Expand All @@ -20,6 +21,7 @@

@Service
public class DataExchangeGenericService implements IDataExchangeGenericService {

private final DataSyncConfigRepository dataSyncConfigRepository;
private final JdbcTemplate jdbcTemplate;
private final JdbcTemplate srteJdbcTemplate;
Expand All @@ -42,12 +44,15 @@ public DataExchangeGenericService(DataSyncConfigRepository dataSyncConfigReposit
@SuppressWarnings("javasecurity:S3649")
public String getGenericDataExchange(String tableName, String timeStamp, Integer limit,boolean initialLoad) throws DataExchangeException {
// Retrieve configuration based on table name
var dataConfig = dataSyncConfigRepository.findById(tableName).orElseThrow(() -> new DataExchangeException("Selected Table Not Found"));

var dataConfig = dataSyncConfigRepository.findById(tableName).orElse(new DataSyncConfig());
if (timeStamp == null) {
timeStamp = "";
}
try {
if (dataConfig.getTableName() == null) {
return DataSimplification.dataCompressionAndEncode("");
}

// Execute the query and retrieve the dataset
String baseQuery = "";

Expand All @@ -68,6 +73,8 @@ public String getGenericDataExchange(String tableName, String timeStamp, Integer
nullQuery = nullQuery.replaceAll(";", ""); //NOSONAR
query = nullQuery + " UNION " + query + ";";
}

System.out.println("TEST QUERY: " + query);
}

if (baseQuery.contains(LIMIT_PARAM)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,23 @@ INSERT INTO [RDB].[dbo].[data_sync_config] (table_name, source_db, query, query_
'SELECT TOP(:limit) N.* FROM NOTIFICATION N WHERE (N.NOTIFICATION_LAST_CHANGE_TIME >= :timestamp) OR (N.NOTIFICATION_LAST_CHANGE_TIME IS NULL AND EXISTS (SELECT 1 FROM NOTIFICATION_EVENT NE JOIN INVESTIGATION I ON NE.INVESTIGATION_KEY = I.INVESTIGATION_KEY WHERE NE.NOTIFICATION_KEY = N.NOTIFICATION_KEY AND I.LAST_CHG_TIME >= :timestamp)) ORDER BY N.NOTIFICATION_LAST_CHANGE_TIME ASC;',
NULL),
('CONDITION_CODE', 'SRTE', 'SELECT * FROM CONDITION_CODE', NULL, NULL),
('CODE_TO_CODE', 'SRTE', 'SELECT * FROM CODE_TO_CODE', NULL, NULL);
('CODE_TO_CODE', 'SRTE', 'SELECT * FROM CODE_TO_CODE', NULL, NULL),
('BMIRD_MULTI_VALUE_FIELD_GROUP', 'RDB',
'SELECT BMIRD_MULTI_VALUE_FIELD_GROUP.* FROM BMIRD_MULTI_VALUE_FIELD_GROUP JOIN BMIRD_CASE ON BMIRD_MULTI_VALUE_FIELD_GROUP.BMIRD_MULTI_VAL_GRP_KEY = BMIRD_CASE.BMIRD_MULTI_VAL_GRP_KEY JOIN INVESTIGATION ON BMIRD_CASE.INVESTIGATION_KEY = INVESTIGATION.INVESTIGATION_KEY WHERE INVESTIGATION.LAST_CHG_TIME >= :timestamp;',
'SELECT TOP(:limit) BMIRD_MULTI_VALUE_FIELD_GROUP.* FROM BMIRD_MULTI_VALUE_FIELD_GROUP JOIN BMIRD_CASE ON BMIRD_MULTI_VALUE_FIELD_GROUP.BMIRD_MULTI_VAL_GRP_KEY = BMIRD_CASE.BMIRD_MULTI_VAL_GRP_KEY JOIN INVESTIGATION ON BMIRD_CASE.INVESTIGATION_KEY = INVESTIGATION.INVESTIGATION_KEY WHERE INVESTIGATION.LAST_CHG_TIME >= :timestamp ORDER BY INVESTIGATION.LAST_CHG_TIME ASC;',
NULL),
('D_VAR_PAM', 'RDB',
'SELECT D_VAR_PAM.* FROM D_VAR_PAM JOIN F_VAR_PAM ON F_VAR_PAM.D_VAR_PAM_KEY = D_VAR_PAM.D_VAR_PAM_KEY JOIN INVESTIGATION ON F_VAR_PAM.INVESTIGATION_KEY = INVESTIGATION.INVESTIGATION_KEY WHERE INVESTIGATION.ADD_TIME >= :timestamp OR INVESTIGATION.LAST_CHG_TIME >= :timestamp;',
'SELECT TOP(:limit) D_VAR_PAM.* FROM D_VAR_PAM JOIN F_VAR_PAM ON F_VAR_PAM.D_VAR_PAM_KEY = D_VAR_PAM.D_VAR_PAM_KEY JOIN INVESTIGATION ON F_VAR_PAM.INVESTIGATION_KEY = INVESTIGATION.INVESTIGATION_KEY WHERE INVESTIGATION.ADD_TIME >= :timestamp OR INVESTIGATION.LAST_CHG_TIME >= :timestamp ORDER BY INVESTIGATION.LAST_CHG_TIME ASC;',
'SELECT D_VAR_PAM.* FROM D_VAR_PAM JOIN F_VAR_PAM ON F_VAR_PAM.D_VAR_PAM_KEY = D_VAR_PAM.D_VAR_PAM_KEY JOIN INVESTIGATION ON F_VAR_PAM.INVESTIGATION_KEY = INVESTIGATION.INVESTIGATION_KEY WHERE INVESTIGATION.ADD_TIME IS NULL AND INVESTIGATION.LAST_CHG_TIME IS NULL;'),
('D_PCR_SOURCE_GROUP', 'RDB',
'SELECT D_PCR_SOURCE_GROUP.* FROM D_PCR_SOURCE_GROUP JOIN F_VAR_PAM ON F_VAR_PAM.D_PCR_SOURCE_GROUP_KEY = D_PCR_SOURCE_GROUP.D_PCR_SOURCE_GROUP_KEY JOIN INVESTIGATION ON F_VAR_PAM.INVESTIGATION_KEY = INVESTIGATION.INVESTIGATION_KEY WHERE INVESTIGATION.ADD_TIME >= :timestamp OR INVESTIGATION.LAST_CHG_TIME >= :timestamp;',
'SELECT TOP(:limit) D_PCR_SOURCE_GROUP.* FROM D_PCR_SOURCE_GROUP JOIN F_VAR_PAM ON F_VAR_PAM.D_PCR_SOURCE_GROUP_KEY = D_PCR_SOURCE_GROUP.D_PCR_SOURCE_GROUP_KEY JOIN INVESTIGATION ON F_VAR_PAM.INVESTIGATION_KEY = INVESTIGATION.INVESTIGATION_KEY WHERE INVESTIGATION.ADD_TIME >= :timestamp OR INVESTIGATION.LAST_CHG_TIME >= :timestamp ORDER BY INVESTIGATION.LAST_CHG_TIME ASC;',
NULL),
('D_RASH_LOC_GEN_GROUP', 'RDB',
'SELECT D_RASH_LOC_GEN_GROUP.* FROM D_RASH_LOC_GEN_GROUP JOIN F_VAR_PAM ON F_VAR_PAM.D_RASH_LOC_GEN_GROUP_KEY = D_RASH_LOC_GEN_GROUP.D_RASH_LOC_GEN_GROUP_KEY JOIN INVESTIGATION ON F_VAR_PAM.INVESTIGATION_KEY = INVESTIGATION.INVESTIGATION_KEY WHERE INVESTIGATION.ADD_TIME >= :timestamp OR INVESTIGATION.LAST_CHG_TIME >= :timestamp;',
'SELECT TOP(:limit) D_RASH_LOC_GEN_GROUP.* FROM D_RASH_LOC_GEN_GROUP JOIN F_VAR_PAM ON F_VAR_PAM.D_RASH_LOC_GEN_GROUP_KEY = D_RASH_LOC_GEN_GROUP.D_RASH_LOC_GEN_GROUP_KEY JOIN INVESTIGATION ON F_VAR_PAM.INVESTIGATION_KEY = INVESTIGATION.INVESTIGATION_KEY WHERE INVESTIGATION.ADD_TIME >= :timestamp OR INVESTIGATION.LAST_CHG_TIME >= :timestamp ORDER BY INVESTIGATION.LAST_CHG_TIME ASC;',
NULL);



Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,17 @@ void setUp() {


@Test
void getGenericDataExchange_WithMissingTable_ThrowsDataExchangeException() {
void getGenericDataExchange_WithMissingTable_NoRelevantData() throws DataExchangeException {
String tableName = "invalid_table";
String timeStamp = "2024-07-11";
int limit = 10;

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

assertThrows(DataExchangeException.class, () ->
dataExchangeGenericService.getGenericDataExchange(tableName, timeStamp, limit, false));
var res = dataExchangeGenericService.getGenericDataExchange(tableName, timeStamp, limit, false);

assertNotNull(res);

}

@SuppressWarnings("java:S5976")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ public class TokenService implements ITokenService {
@Value("${data_exchange.secret}")
private String clientSecret;

private String currentToken;

private final RestTemplate restTemplate;

public TokenService(RestTemplate restTemplate) {
Expand All @@ -29,16 +27,7 @@ public TokenService(RestTemplate restTemplate) {


public String getToken() {
// if (currentToken == null) {
// currentToken = fetchNewToken();
// }
currentToken = fetchNewToken();
return currentToken;
}

private boolean isTokenExpired() {
// Implement your logic to check if the token is expired
return false;
return fetchNewToken();
}

private String fetchNewToken() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,14 @@ public void truncatingData() {
public String getMaxTimestamp() throws DataPollException {
try {
var time = transportQOutRepository.findMaxTimeStampInvolvingWithNotification();
if (time.isPresent()) {
return time.get().toString();
}
else {
return "";
}
return time.orElse("");
} catch (Exception e) {
throw new DataPollException(e.getMessage());
}

}

@SuppressWarnings("java:S1141")
public void saveDataExchange(List<TransportQOutDto> transportQOutDtoList) throws DataPollException {
try {
List<TransportQOut> cnTransportQOutList = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.*;

public class CNTransportQOutServiceTest {
class CNTransportQOutServiceTest {

@Mock
private CNTransportQOutRepository cnTransportQOutRepository;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.Mockito.*;

public class DataHandlingServiceTest {
class DataHandlingServiceTest {

@Mock
private ICNTransportQOutService icnTransportQOutService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.*;

public class DataPullServiceTest {
class DataPullServiceTest {

@Mock
private IDataHandlingService dataHandlingService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.*;

public class ErrorHandlingServiceTest {
class ErrorHandlingServiceTest {

private ErrorHandlingService errorHandlingService;
@Mock
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.*;

public class NetsstTransportServiceTest {
class NetsstTransportServiceTest {

@Mock
private NETSSTransportQOutRepository netssTransportQOutRepository;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.*;

public class TokenServiceTest {
class TokenServiceTest {

@Mock
private RestTemplate restTemplate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.*;

public class TransportQOutServiceTest {
class TransportQOutServiceTest {

@Mock
private TransportQOutRepository transportQOutRepository;
Expand Down

0 comments on commit 38812a3

Please sign in to comment.