From 1b1c1ce3121c6c0297a585e3c89e997d8b32eed9 Mon Sep 17 00:00:00 2001 From: effe Date: Thu, 12 Sep 2024 19:54:25 -0400 Subject: [PATCH 1/4] refactor: tests --- .../com/application/munera/services/ExpenseServiceTest.java | 2 +- .../com/application/munera/services/PersonServiceTest.java | 5 ----- 2 files changed, 1 insertion(+), 6 deletions(-) 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..c176d59 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); @@ -180,5 +176,4 @@ class PersonServiceTest { // Assert assertEquals(new BigDecimal("-50.00"), totalDebt); } - } \ No newline at end of file From dcaae0b481a13d8949821925a7a615a0e26a49d4 Mon Sep 17 00:00:00 2001 From: effe Date: Thu, 12 Sep 2024 20:01:48 -0400 Subject: [PATCH 2/4] fix: fixed cost field validation --- .../com/application/munera/views/expenses/ExpensesView.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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); From 88276b8c55af399ccd13ea7e4ac1d48990ee6993 Mon Sep 17 00:00:00 2001 From: effe Date: Thu, 12 Sep 2024 20:29:24 -0400 Subject: [PATCH 3/4] test: removed old tests --- .../munera/services/PersonServiceTest.java | 36 ------------------- 1 file changed, 36 deletions(-) diff --git a/src/test/java/com/application/munera/services/PersonServiceTest.java b/src/test/java/com/application/munera/services/PersonServiceTest.java index c176d59..230f489 100644 --- a/src/test/java/com/application/munera/services/PersonServiceTest.java +++ b/src/test/java/com/application/munera/services/PersonServiceTest.java @@ -121,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 @@ -155,25 +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 From 0bb2d06525d5a2f710d60be49f4a4e07ee8ca262 Mon Sep 17 00:00:00 2001 From: effe Date: Thu, 12 Sep 2024 20:44:47 -0400 Subject: [PATCH 4/4] refactor: PeopleView --- .../munera/views/people/PeopleView.java | 49 ++++++++++--------- 1 file changed, 26 insertions(+), 23 deletions(-) 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);