Buggy code:
@ Service public class LoanScheduleAssembler { private final FromJsonHelper fromApiJsonHelper ; private final LoanProductRepository loanProductRepository ; private final ApplicationCurrencyRepositoryWrapper applicationCurrencyRepository ; private final LoanChargeAssembler loanChargeAssembler ; private final LoanScheduleGeneratorFactory loanScheduleFactory ; private final AprCalculator aprCalculator ; private final CalendarRepository calendarRepository ; private final HolidayRepository holidayRepository ; private final ConfigurationDomainService configurationDomainService ; private final ClientRepositoryWrapper clientRepository ; private final GroupRepositoryWrapper groupRepository ; private final WorkingDaysRepositoryWrapper workingDaysRepository ; private final FloatingRatesReadPlatformService floatingRatesReadPlatformService ; private final VariableLoanScheduleFromApiJsonValidator variableLoanScheduleFromApiJsonValidator ; private final CalendarInstanceRepository calendarInstanceRepository ; private final PlatformSecurityContext context ; private final LoanUtilService loanUtilService ; @ Autowired public LoanScheduleAssembler ( final FromJsonHelper fromApiJsonHelper , final LoanProductRepository loanProductRepository , final ApplicationCurrencyRepositoryWrapper applicationCurrencyRepository , final LoanScheduleGeneratorFactory loanScheduleFactory , final AprCalculator aprCalculator , final LoanChargeAssembler loanChargeAssembler , final CalendarRepository calendarRepository , final HolidayRepository holidayRepository , final ConfigurationDomainService configurationDomainService , final ClientRepositoryWrapper clientRepository , final GroupRepositoryWrapper groupRepository , final WorkingDaysRepositoryWrapper workingDaysRepository , final FloatingRatesReadPlatformService floatingRatesReadPlatformService , final VariableLoanScheduleFromApiJsonValidator variableLoanScheduleFromApiJsonValidator , final CalendarInstanceRepository calendarInstanceRepository , final PlatformSecurityContext context , final LoanUtilService loanUtilService ) { } public LoanApplicationTerms assembleLoanTerms ( final JsonElement element ) { } private LoanApplicationTerms assembleLoanApplicationTermsFrom ( final JsonElement element , final LoanProduct loanProduct ) { } private CalendarInstance createInterestRecalculationCalendarInstance ( final LocalDate calendarStartDate , final RecalculationFrequencyType recalculationFrequencyType , final Integer frequency ) { } private List < DisbursementData > fetchDisbursementData ( final JsonObject command ) { } private void validateRepaymentsStartDateWithMeetingDates ( final LocalDate repaymentsStartingFromDate , final Calendar calendar ) { } public void validateDisbursementDateWithMeetingDates ( final LocalDate expectedDisbursementDate , final Calendar calendar ) { } private void validateRepaymentFrequencyIsSameAsMeetingFrequency ( final Integer meetingFrequency , final Integer repaymentFrequency , final Integer meetingInterval , final Integer repaymentInterval ) { } public LoanProductRelatedDetail assembleLoanProductRelatedDetail ( final JsonElement element ) { } public LoanScheduleModel assembleLoanScheduleFrom ( final JsonElement element ) { } public LoanScheduleModel assembleLoanScheduleFrom ( final LoanApplicationTerms loanApplicationTerms , final boolean isHolidayEnabled , final List < Holiday > holidays , final WorkingDays workingDays , final JsonElement element , Set < LoanDisbursementDetails > disbursementDetails ) { } public LoanScheduleModel assembleForInterestRecalculation ( final LoanApplicationTerms loanApplicationTerms , final Long officeId , List < LoanTransaction > transactions , final Set < LoanCharge > loanCharges , final LoanRepaymentScheduleTransactionProcessor loanRepaymentScheduleTransactionProcessor , final List < LoanRepaymentScheduleInstallment > repaymentScheduleInstallments , final LocalDate rescheduleFrom ) { } public LoanRepaymentScheduleInstallment calculatePrepaymentAmount ( MonetaryCurrency currency , LocalDate onDate , LoanApplicationTerms loanApplicationTerms , final Set < LoanCharge > loanCharges , final Long officeId , List < LoanTransaction > loanTransactions , final LoanRepaymentScheduleTransactionProcessor loanRepaymentScheduleTransactionProcessor ) { } public void assempleVariableScheduleFrom ( final Loan loan , final String json ) { this . variableLoanScheduleFromApiJsonValidator . validateSchedule ( json , loan ) ; List < LoanTermVariations > variations = loan . getLoanTermVariations ( ) ; List < LoanTermVariations > newVariations = new ArrayList < > ( ) ; extractLoanTermVariations ( loan , json , newVariations ) ; <START_BUG> if ( ! ( newVariations . isEmpty ( ) ) ) { <END_BUG> List < LoanTermVariations > retainVariations = adjustExistingVariations ( variations , newVariations ) ; newVariations = retainVariations ; } variations . addAll ( newVariations ) ; List < LoanRepaymentScheduleInstallment > installments = loan . fetchRepaymentScheduleInstallments ( ) ; Set < LocalDate > dueDates = new TreeSet < > ( ) ; LocalDate graceApplicable = loan . getExpectedDisbursedOnLocalDate ( ) ; Integer graceOnPrincipal = loan . getLoanProductRelatedDetail ( ) . graceOnPrincipalPayment ( ) ; if ( graceOnPrincipal == null ) { graceOnPrincipal = 0 ; } for ( LoanRepaymentScheduleInstallment installment : installments ) { dueDates . add ( installment . getDueDate ( ) ) ; if ( graceOnPrincipal == ( installment . getInstallmentNumber ( ) ) ) { graceApplicable = installment . getDueDate ( ) ; } } Set < LocalDate > actualDueDates = new TreeSet ( dueDates ) ; final List < ApiParameterError > dataValidationErrors = new ArrayList < > ( ) ; final DataValidatorBuilder baseDataValidator = new DataValidatorBuilder ( dataValidationErrors ) . resource ( "loan" ) ; for ( LoanTermVariations termVariations : variations ) { switch ( termVariations . getTermType ( ) ) { case INSERT_INSTALLMENT : dueDates . add ( termVariations . fetchTermApplicaDate ( ) ) ; if ( ! ( graceApplicable . isBefore ( termVariations . fetchTermApplicaDate ( ) ) ) ) { baseDataValidator . reset ( ) . failWithCodeNoParameterAddedToErrorCode ( "variable.schedule.insert.not.allowed.before.grace.period" , "Loan<seq2seq4repair_space>schedule<seq2seq4repair_space>insert<seq2seq4repair_space>request<seq2seq4repair_space>invalid" ) ; } break ; case DELETE_INSTALLMENT : if ( dueDates . contains ( termVariations . fetchTermApplicaDate ( ) ) ) { dueDates . remove ( termVariations . fetchTermApplicaDate ( ) ) ; } else { baseDataValidator . reset ( ) . failWithCodeNoParameterAddedToErrorCode ( "variable.schedule.remove.date.invalid" , "Loan<seq2seq4repair_space>schedule<seq2seq4repair_space>remove<seq2seq4repair_space>request<seq2seq4repair_space>invalid" ) ; } break ; case DUE_DATE : dueDates . remove ( termVariations . fetchTermApplicaDate ( ) ) ; dueDates . add ( termVariations . fetchDateValue ( ) ) ; break ; case PRINCIPAL_AMOUNT : case EMI_AMOUNT : if ( ! ( graceApplicable . isBefore ( termVariations . fetchTermApplicaDate ( ) ) ) ) { baseDataValidator . reset ( ) . failWithCodeNoParameterAddedToErrorCode ( "variable.schedule.amount.update.not.allowed.before.grace.period" , "Loan<seq2seq4repair_space>schedule<seq2seq4repair_space>modify<seq2seq4repair_space>request<seq2seq4repair_space>invalid" ) ; } if ( ! ( dueDates . contains ( termVariations . fetchTermApplicaDate ( ) ) ) ) { baseDataValidator . reset ( ) . failWithCodeNoParameterAddedToErrorCode ( "variable.schedule.amount.update.from.date.invalid" , "Loan<seq2seq4repair_space>schedule<seq2seq4repair_space>modify<seq2seq4repair_space>request<seq2seq4repair_space>invalid" ) ; } break ; default : break ; } } LoanProductVariableInstallmentConfig installmentConfig = loan . loanProduct ( ) . loanProductVariableInstallmentConfig ( ) ; final CalendarInstance loanCalendarInstance = calendarInstanceRepository . findCalendarInstaneByEntityId ( loan . getId ( ) , LOANS . getValue ( ) ) ; Calendar loanCalendar = null ; if ( loanCalendarInstance != null ) { loanCalendar = loanCalendarInstance . getCalendar ( ) ; } final Integer minGap = installmentConfig . getMinimumGap ( ) ; final Integer maxGap = installmentConfig . getMaximumGap ( ) ; LocalDate previousDate = loan . getDisbursementDate ( ) ; for ( LocalDate duedate : dueDates ) { int gap = Days . daysBetween ( previousDate , duedate ) . getDays ( ) ; previousDate = duedate ;

Fix:
if ( ! ( variations . isEmpty ( ) ) ) {