Skip to content
Open
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 @@ -154,7 +154,7 @@ public void rescheduleJob(final ScheduledJobDetail scheduledJobDetail) {

@Override
public void pauseScheduler() {
final SchedulerDetail schedulerDetail = this.schedularWritePlatformService.retriveSchedulerDetail();
final SchedulerDetail schedulerDetail = this.schedularWritePlatformService.retrieveSchedulerDetail();
if (!schedulerDetail.isSuspended()) {
schedulerDetail.setSuspended(true);
this.schedularWritePlatformService.updateSchedulerDetail(schedulerDetail);
Expand All @@ -163,7 +163,7 @@ public void pauseScheduler() {

@Override
public void startScheduler() {
final SchedulerDetail schedulerDetail = this.schedularWritePlatformService.retriveSchedulerDetail();
final SchedulerDetail schedulerDetail = this.schedularWritePlatformService.retrieveSchedulerDetail();
if (schedulerDetail.isSuspended()) {
schedulerDetail.setSuspended(false);
this.schedularWritePlatformService.updateSchedulerDetail(schedulerDetail);
Expand Down Expand Up @@ -233,7 +233,7 @@ public void executeJobWithParameters(final Long jobId, String jobParametersJson)

@Override
public boolean isSchedulerRunning() {
return !this.schedularWritePlatformService.retriveSchedulerDetail().isSuspended();
return !this.schedularWritePlatformService.retrieveSchedulerDetail().isSuspended();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void onApplicationEvent(ContextRefreshedEvent event) {
jobDetails.setTriggerMisfired(false);
schedularWritePlatformService.saveOrUpdate(jobDetails);
}
final SchedulerDetail schedulerDetail = schedularWritePlatformService.retriveSchedulerDetail();
final SchedulerDetail schedulerDetail = schedularWritePlatformService.retrieveSchedulerDetail();
if (schedulerDetail.isResetSchedulerOnBootup()) {
schedulerDetail.setSuspended(false);
schedularWritePlatformService.updateSchedulerDetail(schedulerDetail);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public interface SchedularWritePlatformService {

CommandProcessingResult updateJobDetail(Long jobId, JsonCommand command);

SchedulerDetail retriveSchedulerDetail();
SchedulerDetail retrieveSchedulerDetail();

void updateSchedulerDetail(SchedulerDetail schedulerDetail);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.apache.fineract.infrastructure.jobs.domain.SchedulerDetailRepository;
import org.apache.fineract.infrastructure.jobs.exception.JobNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand Down Expand Up @@ -103,13 +104,9 @@ public void updateSchedulerDetail(final SchedulerDetail schedulerDetail) {
}

@Override
public SchedulerDetail retriveSchedulerDetail() {
SchedulerDetail schedulerDetail = null;
final List<SchedulerDetail> schedulerDetailList = this.schedulerDetailRepository.findAll();
if (schedulerDetailList != null) {
schedulerDetail = schedulerDetailList.get(0);
}
return schedulerDetail;
public SchedulerDetail retrieveSchedulerDetail() {
return this.schedulerDetailRepository.findAll(PageRequest.of(0, 1))
.stream().findFirst().orElse(null);
}

@Transactional
Expand Down Expand Up @@ -142,7 +139,7 @@ public boolean processJobDetailForExecution(final String jobKey, final String tr
&& scheduledJobDetail.getNextRunTime().after(new Date()))) {
isStopExecution = true;
}
final SchedulerDetail schedulerDetail = retriveSchedulerDetail();
final SchedulerDetail schedulerDetail = retrieveSchedulerDetail();
if (triggerType.equals(SchedulerServiceConstants.TRIGGER_TYPE_CRON) && schedulerDetail.isSuspended()) {
scheduledJobDetail.setTriggerMisfired(true);
isStopExecution = true;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.infrastructure.jobs.service;

import org.apache.fineract.infrastructure.jobs.domain.SchedulerDetail;
import org.apache.fineract.infrastructure.jobs.domain.SchedulerDetailRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import java.util.Collections;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

/**
* Unit tests for {@link SchedularWritePlatformServiceJpaRepositoryImpl}
*/
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
class SchedularWritePlatformServiceJpaRepositoryImplTest {

@InjectMocks
private SchedularWritePlatformServiceJpaRepositoryImpl underTest;

@Mock
private SchedulerDetailRepository schedulerDetailRepository;

private SchedulerDetail schedulerDetail;

@BeforeEach
void setUp() {
// Create a sample SchedulerDetail for testing
schedulerDetail = new SchedulerDetail();
schedulerDetail.setExecuteInstructionForMisfiredJobs(true);
schedulerDetail.setSuspended(false);
schedulerDetail.setResetSchedulerOnBootup(false);
}

@Test
void testRetrieveSchedulerDetail_WhenRecordExists_ShouldReturnSchedulerDetail() {
// Arrange
Page<SchedulerDetail> page = new PageImpl<>(Collections.singletonList(schedulerDetail));
when(schedulerDetailRepository.findAll(any(PageRequest.class))).thenReturn(page);

// Act
SchedulerDetail result = underTest.retrieveSchedulerDetail();

// Assert
assertNotNull(result, "SchedulerDetail should not be null");
assertEquals(schedulerDetail.isExecuteInstructionForMisfiredJobs(), result.isExecuteInstructionForMisfiredJobs(),
"Execute instruction for misfired jobs should match");
assertEquals(schedulerDetail.isSuspended(), result.isSuspended(), "Suspended status should match");
assertEquals(schedulerDetail.isResetSchedulerOnBootup(), result.isResetSchedulerOnBootup(),
"Reset scheduler on bootup should match");

// Verify that findAll was called with PageRequest.of(0, 1)
verify(schedulerDetailRepository, times(1)).findAll(PageRequest.of(0, 1));
}

@Test
void testRetrieveSchedulerDetail_WhenNoRecordExists_ShouldReturnNull() {
// Arrange
Page<SchedulerDetail> emptyPage = new PageImpl<>(Collections.emptyList());
when(schedulerDetailRepository.findAll(any(PageRequest.class))).thenReturn(emptyPage);

// Act
SchedulerDetail result = underTest.retrieveSchedulerDetail();

// Assert
assertNull(result, "SchedulerDetail should be null when no records exist");

// Verify that findAll was called with PageRequest.of(0, 1)
verify(schedulerDetailRepository, times(1)).findAll(PageRequest.of(0, 1));
}

@Test
void testRetrieveSchedulerDetail_ShouldFetchOnlyOneRecord() {
// Arrange
Page<SchedulerDetail> page = new PageImpl<>(Collections.singletonList(schedulerDetail));
when(schedulerDetailRepository.findAll(any(PageRequest.class))).thenReturn(page);

// Act
underTest.retrieveSchedulerDetail();

// Assert - Verify that PageRequest was created with page=0 and size=1
verify(schedulerDetailRepository, times(1)).findAll(PageRequest.of(0, 1));
}

@Test
void testRetrieveSchedulerDetail_WithMultipleRecords_ShouldReturnFirstRecord() {
// Arrange
SchedulerDetail firstDetail = new SchedulerDetail();
firstDetail.setExecuteInstructionForMisfiredJobs(true);
firstDetail.setSuspended(false);

// Even though we're testing with PageRequest.of(0, 1), let's verify it returns the first one
Page<SchedulerDetail> page = new PageImpl<>(Collections.singletonList(firstDetail));
when(schedulerDetailRepository.findAll(any(PageRequest.class))).thenReturn(page);

// Act
SchedulerDetail result = underTest.retrieveSchedulerDetail();

// Assert
assertNotNull(result, "SchedulerDetail should not be null");
assertEquals(firstDetail.isExecuteInstructionForMisfiredJobs(), result.isExecuteInstructionForMisfiredJobs(),
"Should return the first record");
verify(schedulerDetailRepository, times(1)).findAll(PageRequest.of(0, 1));
}
}