Skip to content

Commit

Permalink
update cron job base on changes in api
Browse files Browse the repository at this point in the history
  • Loading branch information
ndduc01 committed Dec 11, 2024
1 parent 4c304c8 commit 28bc867
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ public DataCompareApiService(RestTemplate restTemplate, TokenService tokenServic
this.tokenService = tokenService;
}
@Override
public void compareData(boolean runNow) {
public void compareData(boolean runNow, boolean autoApply) {
try {
String token = tokenService.getToken();
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", "Bearer " + token);
headers.add("runNowMode", String.valueOf(runNow));
headers.add("autoApply", String.valueOf(autoApply));

String url = compareEndpoint + "?runNow=" + runNow;
String url = compareEndpoint;
HttpEntity<String> entity = new HttpEntity<>(headers);

restTemplate.exchange(url, HttpMethod.GET, entity, String.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@
public class DataCompareCronService implements IDataCompareCronService {
private static final Logger logger = LoggerFactory.getLogger(DataCompareCronService.class);

@Value("${scheduler.run_now:false}")
@Value("${scheduler.run_now}")
private boolean defaultRunNow;

@Value("${scheduler.auto_apply}")
private boolean autoApply;

private final IDataCompareApiService apiService;

public DataCompareCronService(IDataCompareApiService apiService) {
Expand All @@ -23,12 +26,12 @@ public DataCompareCronService(IDataCompareApiService apiService) {

@Scheduled(cron = "${scheduler.cron}", zone = "${scheduler.zone}")
public void scheduleDataCompare() {
scheduleDataCompare(defaultRunNow);
scheduleDataCompare(defaultRunNow, autoApply);
}

@Override
public void scheduleDataCompare(boolean runNow) {
public void scheduleDataCompare(boolean runNow, boolean autoApply) {
logger.info("Starting scheduled data comparison with runNow: {}", runNow);
apiService.compareData(runNow);
apiService.compareData(runNow, autoApply);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

public interface IDataCompareApiService {

void compareData(boolean runNow);
void compareData(boolean runNow, boolean autoApply);
}


Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ public interface IDataCompareCronService {
* Triggers data comparison with optional immediate execution
* @param runNow If true, executes comparison immediately
*/
void scheduleDataCompare(boolean runNow);
void scheduleDataCompare(boolean runNow, boolean autoApply);
}
2 changes: 2 additions & 0 deletions DataCompareCron/src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ data_compare:
scheduler:
cron: ${SCHEDULER_CRON:0 */1 * * * *}
zone: ${SCHEDULER_TIMEZONE:UTC}
run_now: ${RUN_NOW:false}
auto_apply: ${AUTO_APPLY:false}

logging:
level:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,23 @@ void testCompareData_Success() {

HttpHeaders expectedHeaders = new HttpHeaders();
expectedHeaders.add("Authorization", "Bearer " + token);
expectedHeaders.add("runNowMode", "false");
expectedHeaders.add("autoApply", "false");

HttpEntity<String> expectedEntity = new HttpEntity<>(expectedHeaders);

when(restTemplate.exchange(
eq("http://example.com/compare?runNow=false"),
eq("http://example.com/compare"),
eq(HttpMethod.GET),
eq(expectedEntity),
eq(String.class)
)).thenReturn(ResponseEntity.ok("Success"));

apiService.compareData(false);
apiService.compareData(false, false);

verify(tokenService, times(1)).getToken();
verify(restTemplate, times(1)).exchange(
eq("http://example.com/compare?runNow=false"),
eq("http://example.com/compare"),
eq(HttpMethod.GET),
eq(expectedEntity),
eq(String.class)
Expand All @@ -61,7 +64,7 @@ void testCompareData_WhenTokenServiceFails() {
when(tokenService.getToken()).thenThrow(new RuntimeException("Token Error"));

Exception exception = assertThrows(RuntimeException.class, () ->
apiService.compareData(false)
apiService.compareData(false, false)
);
assertEquals("Failed to complete data comparison", exception.getMessage());
verify(tokenService, times(1)).getToken();
Expand All @@ -80,17 +83,20 @@ void testCompareData_WhenRestTemplateFails() {

HttpHeaders expectedHeaders = new HttpHeaders();
expectedHeaders.add("Authorization", "Bearer " + token);
expectedHeaders.add("runNowMode", "false");
expectedHeaders.add("autoApply", "false");

HttpEntity<String> expectedEntity = new HttpEntity<>(expectedHeaders);

when(restTemplate.exchange(
eq("http://example.com/compare?runNow=false"),
eq("http://example.com/compare"),
eq(HttpMethod.GET),
eq(expectedEntity),
eq(String.class)
)).thenThrow(new RuntimeException("API Error"));

Exception exception = assertThrows(RuntimeException.class, () ->
apiService.compareData(false)
apiService.compareData(false, false)
);
assertEquals("Failed to complete data comparison", exception.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ void setUp() {
@Test
void testScheduleDataCompare() {
cronService.scheduleDataCompare();
verify(apiService, times(1)).compareData(false);
verify(apiService, times(1)).compareData(false, false);
}

@Test
void testScheduleDataCompareWithRunNow() {
cronService.scheduleDataCompare(true);
verify(apiService, times(1)).compareData(true);
cronService.scheduleDataCompare(true, false);
verify(apiService, times(1)).compareData(true, false);
}
}

0 comments on commit 28bc867

Please sign in to comment.