Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • xkollar3/online-banking-service
1 result
Show changes
package cz.muni.fi.obs.service.payment;
import cz.muni.fi.obs.api.ScheduledPaymentCreateDto;
import cz.muni.fi.obs.data.dbo.AccountDbo;
import cz.muni.fi.obs.data.dbo.PaymentFrequency;
import cz.muni.fi.obs.data.dbo.ScheduledPayment;
import cz.muni.fi.obs.data.repository.AccountRepository;
import cz.muni.fi.obs.data.repository.ScheduledPaymentRepository;
import cz.muni.fi.obs.exceptions.ResourceNotFoundException;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import java.time.Instant;
import java.time.LocalDate;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
@SpringBootTest
@ActiveProfiles("test")
class ScheduledPaymentServiceTest {
@Autowired
private ScheduledPaymentService scheduledPaymentService;
@Autowired
private ScheduledPaymentRepository repository;
@Autowired
private AccountRepository accountRepository;
private boolean setupDone = false;
@BeforeEach
public void setUp() {
if (setupDone) {
return;
}
AccountDbo accountDbo = new AccountDbo();
accountDbo.setId("123");
accountDbo.setAccountNumber("1233");
accountDbo.setCurrencyCode("CZK");
accountDbo.setCustomerId("mikoflosso");
accountRepository.save(accountDbo);
AccountDbo accountDbo1 = new AccountDbo();
accountDbo1.setId("1234");
accountDbo1.setAccountNumber("12356");
accountDbo1.setCurrencyCode("CZK");
accountDbo1.setCustomerId("ego");
accountRepository.save(accountDbo1);
setupDone = true;
}
@AfterEach
public void cleanScheduledPayments() {
repository.deleteAll();
}
@Test
public void createScheduledPaymentMonthly_bothAccountsExist_createsPaymentMonthly() {
LocalDate date = LocalDate.now();
ScheduledPaymentCreateDto scheduledPaymentCreateDto = new ScheduledPaymentCreateDto(date,
null, PaymentFrequency.MONTHLY, "123", "1234");
scheduledPaymentService.createPayment(scheduledPaymentCreateDto);
List<ScheduledPayment> allPayments = repository.findAll();
assertThat(allPayments.size()).isEqualTo(1);
ScheduledPayment first = allPayments.getFirst();
assertThat(first.getDayOfMonth()).isEqualTo(date.getDayOfMonth());
assertThat(first.getDayOfWeek()).isNull();
assertThat(first.getDayOfYear()).isNull();
}
@Test
public void createScheduledPaymentYearly_bothAccountsExist_createsPaymentYearly() {
LocalDate date = LocalDate.now();
ScheduledPaymentCreateDto scheduledPaymentCreateDto = new ScheduledPaymentCreateDto(date,
null, PaymentFrequency.YEARLY, "123", "1234");
scheduledPaymentService.createPayment(scheduledPaymentCreateDto);
List<ScheduledPayment> allPayments = repository.findAll();
assertThat(allPayments.size()).isEqualTo(1);
ScheduledPayment first = allPayments.getFirst();
assertThat(first.getDayOfMonth()).isNull();
assertThat(first.getDayOfWeek()).isNull();
assertThat(first.getDayOfYear()).isEqualTo(date.getDayOfYear());
}
@Test
public void createScheduledPaymentWeekly_bothAccountsExists_createsWeeklyPayment() {
LocalDate date = LocalDate.now();
ScheduledPaymentCreateDto scheduledPaymentCreateDto = new ScheduledPaymentCreateDto(date,
null, PaymentFrequency.WEEKLY, "123", "1234");
scheduledPaymentService.createPayment(scheduledPaymentCreateDto);
List<ScheduledPayment> allPayments = repository.findAll();
assertThat(allPayments.size()).isEqualTo(1);
ScheduledPayment first = allPayments.getFirst();
assertThat(first.getDayOfMonth()).isNull();
assertThat(first.getDayOfWeek()).isEqualTo(date.getDayOfWeek().ordinal());
assertThat(first.getDayOfYear()).isNull();
}
@Test
public void createScheduledPayment_toAccountDoesNotExist_throwsException() {
ScheduledPaymentCreateDto scheduledPaymentCreateDto = new ScheduledPaymentCreateDto(LocalDate.now(),
null, PaymentFrequency.WEEKLY, "this id does not exist in db", "1234");
assertThrows(ResourceNotFoundException.class, () -> scheduledPaymentService.createPayment(scheduledPaymentCreateDto));
}
@Test
public void disableScheduledPayment_paymentExists_disablesPayment() {
LocalDate date = LocalDate.now();
ScheduledPaymentCreateDto scheduledPaymentCreateDto = new ScheduledPaymentCreateDto(date,
null, PaymentFrequency.WEEKLY, "123", "1234");
scheduledPaymentService.createPayment(scheduledPaymentCreateDto);
ScheduledPayment payment = repository.findAll().getFirst();
scheduledPaymentService.disablePayment(payment.getId(), Instant.now());
ScheduledPayment retrieved = repository.findById(payment.getId())
.orElseThrow(() -> new IllegalStateException("Payment was created but does not exist anymore"));
assertThat(retrieved.getValidUntil()).isBefore(Instant.now());
}
@Test
public void disableScheduledPayment_paymentDoesNotExist_disablesPayment() {
assertThrows(ResourceNotFoundException.class,
() -> scheduledPaymentService.disablePayment("123", Instant.now()));
}
}
\ No newline at end of file
......@@ -7,14 +7,15 @@ VALUES ('1', 'customer-1', 'CZK', 'account-1'),
('6', 'customer-6', 'EUR', 'account-6');
INSERT INTO transactions(id, conversion_rate, withdraws_from, deposits_to, withdrawn_amount, deposited_amount, note, variable_symbol)
VALUES ('1', 0.04, '1', '2', 1000, 40, 'note-1', '00'),
('2', 25.0, '3', '1', 100, 2500, 'note-2', '11'),
('3', 25.0, '2', '4', 50, 1250, 'note-3', '22'),
('4', 1.0, '5', '3', 100, 100, 'note-4', '33'),
('5', 0.04, '4', '2', 200, 8, 'note-5', '44'),
('6', 1.0, '3', '5', 1200, 1200, 'note-6', '55'),
('7', 1.0, '1', '4', 700, 700, 'note-7', '66'),
('8', 1.0, '2', '3', 150, 150, 'note-8', '77'),
('9', 25, '5', '1', 400, 10000, 'note-9', '88');
INSERT INTO transactions(id, conversion_rate, withdraws_from, deposits_to, withdrawn_amount, deposited_amount, note,
variable_symbol, transaction_state)
VALUES ('1', 0.04, '1', '2', 1000, 40, 'note-1', '00', 'SUCCESSFUL'),
('2', 25.0, '3', '1', 100, 2500, 'note-2', '11', 'SUCCESSFUL'),
('3', 25.0, '2', '4', 50, 1250, 'note-3', '22', 'SUCCESSFUL'),
('4', 1.0, '5', '3', 100, 100, 'note-4', '33', 'SUCCESSFUL'),
('5', 0.04, '4', '2', 200, 8, 'note-5', '44', 'SUCCESSFUL'),
('6', 1.0, '3', '5', 1200, 1200, 'note-6', '55', 'SUCCESSFUL'),
('7', 1.0, '1', '4', 700, 700, 'note-7', '66', 'SUCCESSFUL'),
('8', 1.0, '2', '3', 150, 150, 'note-8', '77', 'SUCCESSFUL'),
('9', 25, '5', '1', 400, 10000, 'note-9', '88', 'SUCCESSFUL');