Testing branch #40
@@ -13,6 +13,7 @@ import java.time.LocalDate;
|
|||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
@Entity(name = "incomes")
|
@Entity(name = "incomes")
|
||||||
|
@Builder
|
||||||
public class Income implements IMoneyTransaction {
|
public class Income implements IMoneyTransaction {
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
|
|||||||
@@ -34,8 +34,8 @@ public class User {
|
|||||||
private String username;
|
private String username;
|
||||||
|
|
||||||
@Transient
|
@Transient
|
||||||
@NotNull(message = "Password must not be null")
|
// @NotNull(message = "Password must not be null")
|
||||||
@NotEmpty(message = "Password must not be empty")
|
// @NotEmpty(message = "Password must not be empty")
|
||||||
private String password;
|
private String password;
|
||||||
|
|
||||||
@OneToMany(mappedBy = "user", fetch = FetchType.LAZY)
|
@OneToMany(mappedBy = "user", fetch = FetchType.LAZY)
|
||||||
|
|||||||
@@ -0,0 +1,122 @@
|
|||||||
|
package com.faf223.expensetrackerfaf.repository;
|
||||||
|
|
||||||
|
import com.faf223.expensetrackerfaf.model.Income;
|
||||||
|
import com.faf223.expensetrackerfaf.model.IncomeCategory;
|
||||||
|
import com.faf223.expensetrackerfaf.model.User;
|
||||||
|
import org.assertj.core.api.Assertions;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
|
||||||
|
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
@DataJpaTest
|
||||||
|
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
|
||||||
|
public class IncomeRepositoryTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private IncomeRepository incomeRepository;
|
||||||
|
@Autowired
|
||||||
|
private IncomeCategoryRepository incomeCategoryRepository;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private UserRepository userRepository;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void IncomeRepository_SaveAll_ReturnIncome() {
|
||||||
|
|
||||||
|
User user = User.builder()
|
||||||
|
.firstName("Test")
|
||||||
|
.lastName("TestLast")
|
||||||
|
.username("UserTest")
|
||||||
|
.incomes(new ArrayList<>())
|
||||||
|
.expenses(new ArrayList<>())
|
||||||
|
.build();
|
||||||
|
|
||||||
|
userRepository.save(user);
|
||||||
|
IncomeCategory incomeCategory = incomeCategoryRepository.getReferenceById(1L);
|
||||||
|
|
||||||
|
Income income = Income.builder()
|
||||||
|
.user(user)
|
||||||
|
.amount(BigDecimal.valueOf(77))
|
||||||
|
.category(incomeCategory)
|
||||||
|
.date(LocalDate.now())
|
||||||
|
.build();
|
||||||
|
|
||||||
|
Income savedIncome = incomeRepository.save(income);
|
||||||
|
|
||||||
|
Assertions.assertThat(savedIncome).isNotNull();
|
||||||
|
Assertions.assertThat(savedIncome.getId()).isGreaterThan(0L);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void IncomeRepository_GateAll_ReturnsMoreThenOneIncome() {
|
||||||
|
|
||||||
|
User user = User.builder()
|
||||||
|
.firstName("Test")
|
||||||
|
.lastName("TestLast")
|
||||||
|
.username("UserTest")
|
||||||
|
.incomes(new ArrayList<>())
|
||||||
|
.expenses(new ArrayList<>())
|
||||||
|
.build();
|
||||||
|
|
||||||
|
userRepository.save(user);
|
||||||
|
IncomeCategory incomeCategory = incomeCategoryRepository.getReferenceById(1L);
|
||||||
|
|
||||||
|
List<Income> incomeList = incomeRepository.findAll();
|
||||||
|
int qtyBefore = incomeList.size();
|
||||||
|
|
||||||
|
Income income1 = Income.builder()
|
||||||
|
.user(user)
|
||||||
|
.amount(BigDecimal.valueOf(77))
|
||||||
|
.category(incomeCategory)
|
||||||
|
.date(LocalDate.now())
|
||||||
|
.build();
|
||||||
|
Income income2 = Income.builder()
|
||||||
|
.user(user)
|
||||||
|
.amount(BigDecimal.valueOf(177))
|
||||||
|
.category(incomeCategory)
|
||||||
|
.date(LocalDate.now())
|
||||||
|
.build();
|
||||||
|
|
||||||
|
incomeRepository.save(income1);
|
||||||
|
incomeRepository.save(income2);
|
||||||
|
|
||||||
|
incomeList = incomeRepository.findAll();
|
||||||
|
|
||||||
|
Assertions.assertThat(incomeList).isNotNull();
|
||||||
|
Assertions.assertThat(incomeList.size()).isEqualTo(qtyBefore + 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void IncomeRepository_FindById_ReturnIncome() {
|
||||||
|
|
||||||
|
User user = User.builder()
|
||||||
|
.firstName("Test")
|
||||||
|
.lastName("TestLast")
|
||||||
|
.username("UserTest")
|
||||||
|
.incomes(new ArrayList<>())
|
||||||
|
.expenses(new ArrayList<>())
|
||||||
|
.build();
|
||||||
|
|
||||||
|
userRepository.save(user);
|
||||||
|
IncomeCategory incomeCategory = incomeCategoryRepository.getReferenceById(1L);
|
||||||
|
|
||||||
|
Income income = Income.builder()
|
||||||
|
.user(user)
|
||||||
|
.amount(BigDecimal.valueOf(77))
|
||||||
|
.category(incomeCategory)
|
||||||
|
.date(LocalDate.now())
|
||||||
|
.build();
|
||||||
|
|
||||||
|
incomeRepository.save(income);
|
||||||
|
|
||||||
|
Optional<Income> incomeReturn = incomeRepository.findById(income.getId());
|
||||||
|
Assertions.assertThat(incomeReturn.isPresent()).isTrue();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user