diff --git a/src/main/java/com/application/munera/views/expenses/ExpensesView.java b/src/main/java/com/application/munera/views/expenses/ExpensesView.java index 60f6648..593f219 100644 --- a/src/main/java/com/application/munera/views/expenses/ExpensesView.java +++ b/src/main/java/com/application/munera/views/expenses/ExpensesView.java @@ -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); diff --git a/src/main/java/com/application/munera/views/people/PeopleView.java b/src/main/java/com/application/munera/views/people/PeopleView.java index a02e513..c1ba31f 100644 --- a/src/main/java/com/application/munera/views/people/PeopleView.java +++ b/src/main/java/com/application/munera/views/people/PeopleView.java @@ -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)); - 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)); - setExpensePaidButton.addThemeVariants(ButtonVariant.LUMO_SMALL); - if (Boolean.TRUE.equals(((Expense) persona).getIsPaid())) setExpensePaidButton.setEnabled(false); - return setExpensePaidButton; - } else return new Span(); + 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; + } + 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).getIsPaid())) setExpensePaidButton.setEnabled(false); + return setExpensePaidButton; + } + 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); diff --git a/src/test/java/com/application/munera/services/ExpenseServiceTest.java b/src/test/java/com/application/munera/services/ExpenseServiceTest.java index 0b2e126..3008c96 100644 --- a/src/test/java/com/application/munera/services/ExpenseServiceTest.java +++ b/src/test/java/com/application/munera/services/ExpenseServiceTest.java @@ -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())) diff --git a/src/test/java/com/application/munera/services/PersonServiceTest.java b/src/test/java/com/application/munera/services/PersonServiceTest.java index a5715b0..230f489 100644 --- a/src/test/java/com/application/munera/services/PersonServiceTest.java +++ b/src/test/java/com/application/munera/services/PersonServiceTest.java @@ -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); - } - } \ No newline at end of file