Skip to content

Commit 4ab2a6c

Browse files
committed
Add null-handling for transaction linking and update tests
Introduced null checks in `LinkTransactionHandler` to handle cases where labels or names are unset during transaction linking. Added a new test case `handleRelationEvent_unset` and updated SQL scripts to include relevant scenarios for better coverage. Ensures robust handling of nullable inputs in transaction-related workflows.
1 parent 25c1568 commit 4ab2a6c

File tree

3 files changed

+34
-11
lines changed

3 files changed

+34
-11
lines changed

jpa-repository/src/main/java/com/jongsoft/finance/jpa/transaction/LinkTransactionHandler.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ private EntityJpa fetchRelation(LinkTransactionCommand.LinkType type, String rel
5858
}
5959

6060
private CategoryJpa category(String label) {
61+
if (label == null) {
62+
return null;
63+
}
6164
return entityManager.from(CategoryJpa.class)
6265
.fieldEq("user.username", authenticationFacade.authenticated())
6366
.fieldEq("label", label)
@@ -66,6 +69,9 @@ private CategoryJpa category(String label) {
6669
}
6770

6871
private ExpenseJpa expense(String name) {
72+
if (name == null) {
73+
return null;
74+
}
6975
return entityManager.from(ExpenseJpa.class)
7076
.fieldEq("name", name)
7177
.fieldEq("user.username", authenticationFacade.authenticated())
@@ -74,6 +80,9 @@ private ExpenseJpa expense(String name) {
7480
}
7581

7682
private ContractJpa contract(String name) {
83+
if (name == null) {
84+
return null;
85+
}
7786
return entityManager.from(ContractJpa.class)
7887
.fieldEq("name", name)
7988
.fieldEq("user.username", authenticationFacade.authenticated())

jpa-repository/src/test/java/com/jongsoft/finance/jpa/transaction/TransactionEventListenerIT.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import org.junit.jupiter.api.Test;
1818
import org.mockito.Mockito;
1919

20-
import java.io.Serializable;
2120
import java.math.BigDecimal;
2221
import java.time.LocalDate;
2322
import java.util.Objects;
@@ -131,6 +130,16 @@ void handleRelationEvent_category() {
131130
Assertions.assertThat(check.getCategory().getLabel()).isEqualTo("Test");
132131
}
133132

133+
@Test
134+
void handleRelationEvent_unset() {
135+
eventPublisher.publishEvent(new LinkTransactionCommand(
136+
3L,
137+
LinkTransactionCommand.LinkType.CATEGORY,
138+
null));
139+
var check = entityManager.find(TransactionJournal.class, 3L);
140+
Assertions.assertThat(check.getCategory()).isNull();
141+
}
142+
134143
@Test
135144
void handleTagEvent() {
136145
eventPublisher.publishEvent(new TagTransactionCommand(

jpa-repository/src/test/resources/sql/transaction/transaction-provider.sql

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,31 @@ values (1, 1, 'Account One', 'Demo Account', 1, false, 1, 'NLJND200001928233', '
44
(3, 1, 'Split account 1', null, 1, false, 1, '', '', ''),
55
(4, 1, 'Split account 2', null, 1, false, 7, '', '', '');
66

7+
insert into category (id, label, user_id, archived)
8+
values (1, 'Grocery', 1, false),
9+
(2, 'Test', 1, false);
10+
11+
insert into tags (id, name, user_id, archived)
12+
values (1, 'Sample 1', 1, 0),
13+
(2, 'Food', 1, 0),
14+
(3, 'Drugs', 1, 0);
15+
716
insert into transaction_journal (ID, created, updated, T_DATE, DESCRIPTION, currency_id, TYPE, USER_ID)
817
values (1, '2019-01-01', '2019-01-01', '2019-01-01', 'Sample transaction', 1, 'CREDIT', 1),
918
(2, '2019-01-02', '2019-01-02', '2019-01-02', 'Split transaction sample', 1, 'CREDIT', 1);
1019

20+
insert into transaction_journal (ID, created, updated, T_DATE, DESCRIPTION, currency_id, TYPE, USER_ID, CATEGORY_ID)
21+
values (3, '2019-01-01', '2019-01-01', '2019-01-01', 'Categorized transaction', 1, 'CREDIT', 1, 1);
22+
1123
insert into transaction_part (ID, created, updated, deleted, JOURNAL_ID, ACCOUNT_ID, AMOUNT)
1224
values (1, '2019-01-01', '2019-01-01', null, 1, 1, 20.2),
1325
(2, '2019-01-01', '2019-01-01', null, 1, 2, -20.2),
1426
(3, '2019-01-02', '2019-01-01', null, 2, 3, 20.2),
1527
(4, '2019-01-02', '2019-01-05', '2019-01-05', 2, 4, -20.2),
1628
(5, '2019-01-05', '2019-01-05', null, 2, 4, -10.2),
17-
(6, '2019-01-05', '2019-01-05', null, 2, 4, -10.0);
18-
19-
insert into category (id, label, user_id, archived)
20-
values (1, 'Grocery', 1, false),
21-
(2, 'Test', 1, false);
22-
23-
insert into tags (id, name, user_id, archived)
24-
values (1, 'Sample 1', 1, 0),
25-
(2, 'Food', 1, 0),
26-
(3, 'Drugs', 1, 0);
29+
(6, '2019-01-05', '2019-01-05', null, 2, 4, -10.0),
30+
(7, '2019-01-05', '2019-01-05', null, 3, 4, -10.2),
31+
(8, '2019-01-05', '2019-01-05', null, 3, 4, -10.0);
2732

2833
insert into transaction_tag (tag_id, transaction_id)
2934
values (1, 1),

0 commit comments

Comments
 (0)