Merge remote-tracking branch 'origin/main'

This commit is contained in:
filippo-ferrari 2024-09-12 21:16:24 +02:00
commit 23156d01ba
4 changed files with 29 additions and 66 deletions

View file

@ -34,6 +34,7 @@ import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.vaadin.klaudeta.PaginatedGrid;
import java.math.BigDecimal;
import java.util.Objects;
import java.util.Optional;
@ -129,8 +130,8 @@ public class ExpensesView extends Div implements BeforeEnterObserver {
binder.forField(cost)
.asRequired("Cost is required")
.withConverter(new StringToBigDecimalConverter("Invalid cost"))
.withValidator(costValue -> costValue.compareTo(BigDecimal.ONE) > 0, "Cost must be greater than 1")
.bind(Expense::getCost, Expense::setCost);
binder.forField(category)
.asRequired("Category is required")
.bind(Expense::getCategory, Expense::setCategory);

View file

@ -84,27 +84,33 @@ public class PeopleView extends Div implements BeforeEnterObserver {
grid.addThemeVariants(GridVariant.LUMO_NO_BORDER);
grid.addHierarchyColumn(this::getNodeName).setHeader("Name");
grid.addColumn(this::getNodeCost).setHeader("Balance").setSortable(true);
grid.addColumn(new ComponentRenderer<>(persona -> {
if (persona instanceof Person) return this.viewsService.createPersonBadge(personService.calculateNetBalance((Person) persona));
else return this.viewsService.createExpenseBadge(((Expense) persona));
grid.addColumn(new ComponentRenderer<>(personEntry -> {
if (personEntry instanceof Person person1) return this.viewsService.createPersonBadge(personService.calculateNetBalance(person1));
else return this.viewsService.createExpenseBadge(((Expense) personEntry));
})).setHeader("Balance Status");
grid.addColumn(new ComponentRenderer<>(persona -> {
if (persona instanceof Person) {
Button setDebtPaidButton = new Button("Set all debt as paid", event -> this.personFacade.setDebtPaid((Person) persona, grid));
switch (persona) {
case Person person1 -> {
Button setDebtPaidButton = new Button("Set all debt as paid", event -> this.personFacade.setDebtPaid(person1, grid));
setDebtPaidButton.addThemeVariants(ButtonVariant.LUMO_SMALL, ButtonVariant.LUMO_PRIMARY);
return setDebtPaidButton;
} else if (persona instanceof Expense) {
Button setExpensePaidButton = new Button("Set as paid", event -> this.expenseFacade.setExpensePaid((Expense) persona, grid));
}
case Expense expense -> {
Button setExpensePaidButton = new Button("Set as paid", event -> this.expenseFacade.setExpensePaid(expense, grid));
setExpensePaidButton.addThemeVariants(ButtonVariant.LUMO_SMALL);
if (Boolean.TRUE.equals(((Expense) persona).getIsPaid())) setExpensePaidButton.setEnabled(false);
if (Boolean.TRUE.equals((expense).getIsPaid())) setExpensePaidButton.setEnabled(false);
return setExpensePaidButton;
} else return new Span();
}
default -> {
return new Span();
}
}
}));
grid.addColumn(new ComponentRenderer<>(persona -> {
if (persona instanceof Person) {
Button setCreditPaidButton = new Button("Set all credit as paid", event -> this.personFacade.setCreditPaid((Person) persona, grid));
if (persona instanceof Person person1) {
Button setCreditPaidButton = new Button("Set all credit as paid", event -> this.personFacade.setCreditPaid(person1, grid));
setCreditPaidButton.addThemeVariants(ButtonVariant.LUMO_SMALL, ButtonVariant.LUMO_PRIMARY);
return setCreditPaidButton;
} else return new Span();
@ -175,14 +181,14 @@ public class PeopleView extends Div implements BeforeEnterObserver {
}
private String getNodeName(Object node) {
if (node instanceof Person) return ((Person) node).getFirstName() + " " + ((Person) node).getLastName();
else if (node instanceof Expense) return ((Expense) node).getName();
if (node instanceof Person person1) return (person1).getFirstName() + " " + (person1).getLastName();
else if (node instanceof Expense expense1) return (expense1).getName();
return "";
}
private String getNodeCost(Object node) {
if (node instanceof Person) return this.personService.calculateNetBalance((Person) node) + "";
else if (node instanceof Expense) return ((Expense) node).getCost().toString() + "";
if (node instanceof Person person1) return this.personService.calculateNetBalance(person1) + "";
else if (node instanceof Expense expense1) return (expense1).getCost().toString() + "";
return "";
}
@ -196,9 +202,7 @@ public class PeopleView extends Div implements BeforeEnterObserver {
Notification.show(
String.format("The requested person was not found, ID = %s", personId.get()), 3000,
Position.BOTTOM_START);
// when a row is selected but the data is no longer available,
// refresh grid
refreshGrid();
refreshGrid(); // when a row is selected but the data is no longer available refresh grid
event.forwardTo(PeopleView.class);
}
}
@ -207,7 +211,6 @@ public class PeopleView extends Div implements BeforeEnterObserver {
private void createEditorLayout(SplitLayout splitLayout) {
Div editorLayoutDiv = new Div();
editorLayoutDiv.setClassName("editor-layout");
Div editorDiv = new Div();
editorDiv.setClassName("editor");
editorLayoutDiv.add(editorDiv);

View file

@ -41,7 +41,7 @@ class ExpenseServiceTest {
}
@Test
public void testFetchExpensesForDashboard_NoExpenses() {
void testFetchExpensesForDashboard_NoExpenses() {
when(expenseRepository.findExpensesByPayerAndBeneficiaryAndYear(loggedInPerson.getId(), year.getValue()))
.thenReturn(new ArrayList<>());
when(expenseRepository.findExpensesByBeneficiaryAndYear(loggedInPerson.getId(), year.getValue()))

View file

@ -38,10 +38,6 @@ class PersonServiceTest {
person2.setLastName("second");
person2.setFirstName("person");
// Person person3 = new Person();
// person3.setLastName("third");
// person3.setFirstName("person");
Expense expense1 = mock(Expense.class);
when(expense1.getPayer()).thenReturn(person);
when(expense1.getBeneficiary()).thenReturn(person2);
@ -125,21 +121,6 @@ class PersonServiceTest {
assertEquals(BigDecimal.ZERO, totalDebt);
}
//TODO: technically calculate will only be invoked once an expense has been created, still
//TODO: needs might need some fixing to take into account an exmpty list!!!
@Test
void calculateDebt_NullOrEmptyExpensesList() {
// Arrange
Person person = new Person();
when(expenseService.findExpensesWherePayer(person)).thenReturn(null); // Null case
// Act
BigDecimal totalDebt = personService.calculateDebt(person);
// Assert
assertEquals(BigDecimal.ZERO, totalDebt);
}
@Test
void calculateDebt_ExpensesWithNullAttributes() {
// Arrange
@ -159,26 +140,4 @@ class PersonServiceTest {
// Assert
assertEquals(BigDecimal.ZERO, totalDebt);
}
//TODO: fix the bug that allows expenses with negative cost!!!
@Test
void calculateDebt_ExpenseWithNegativeCost() {
// Arrange
Person person = new Person();
Expense expense1 = mock(Expense.class);
when(expense1.getPayer()).thenReturn(person);
when(expense1.getBeneficiary()).thenReturn(new Person());
when(expense1.getCost()).thenReturn(new BigDecimal("-50.00")); // Negative cost
when(expense1.getIsPaid()).thenReturn(false);
when(expenseService.findExpensesWherePayer(person)).thenReturn(List.of(expense1));
// Act
BigDecimal totalDebt = personService.calculateDebt(person);
// Assert
assertEquals(new BigDecimal("-50.00"), totalDebt);
}
}