/
MonetaryAmountPathTest.java
107 lines (84 loc) · 4.07 KB
/
MonetaryAmountPathTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package com.pallasathenagroup.querydsl;
import com.querydsl.core.QueryResults;
import com.querydsl.core.group.GroupBy;
import com.querydsl.jpa.impl.JPAQuery;
import org.hamcrest.CoreMatchers;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import javax.money.CurrencyUnit;
import javax.money.Monetary;
import javax.money.MonetaryAmount;
import java.math.BigDecimal;
import java.util.Map;
import static com.pallasathenagroup.querydsl.QMonetaryAmountEntity.monetaryAmountEntity;
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
public class MonetaryAmountPathTest extends BaseCoreFunctionalTestCase {
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] { MonetaryAmountEntity.class };
}
@Override
protected boolean isCleanupTestDataRequired() {
return true;
}
@Before
public void setUp() {
doInJPA(this::sessionFactory, entityManager -> {
CurrencyUnit eur = Monetary.getCurrency("EUR");
MonetaryAmount monetaryAmount = Monetary.getDefaultAmountFactory().setCurrency(eur).setNumber(200).create();
MonetaryAmountEntity moneyEntity = new MonetaryAmountEntity();
moneyEntity.setId(1L);
moneyEntity.setMonetaryAmount(monetaryAmount);
moneyEntity.setSummableValue(new BigDecimal("5.00"));
entityManager.persist(moneyEntity);
});
}
@Test
public void testArrayPaths() {
doInJPA(this::sessionFactory, entityManager -> {
Map<CurrencyUnit, BigDecimal> result = new JPAQuery<PeriodEntity>(entityManager, ExtendedHQLTemplates.DEFAULT)
.from(monetaryAmountEntity)
.groupBy(monetaryAmountEntity.monetaryAmount.currencyUnit)
.transform(GroupBy.groupBy(monetaryAmountEntity.monetaryAmount.currencyUnit).as(monetaryAmountEntity.monetaryAmount.amount.sum()));
Assert.assertThat(result, CoreMatchers.is(CoreMatchers.notNullValue()));
});
}
@Test
public void testMoneyProjection() {
doInJPA(this::sessionFactory, entityManager -> {
QueryResults<MonetaryAmount> result = new JPAQuery<PeriodEntity>(entityManager, ExtendedHQLTemplates.DEFAULT)
.from(monetaryAmountEntity)
.groupBy(monetaryAmountEntity.monetaryAmount.currencyUnit)
.select(MoneyProjections.money(monetaryAmountEntity.monetaryAmount.amount.sum(), monetaryAmountEntity.monetaryAmount.currencyUnit))
.fetchResults();
Assert.assertThat(result, CoreMatchers.is(CoreMatchers.notNullValue()));
});
}
@Test
public void testMoneyComponentExpressions() {
doInJPA(this::sessionFactory, entityManager -> {
MonetaryAmount result = new JPAQuery<PeriodEntity>(entityManager, ExtendedHQLTemplates.DEFAULT)
.from(monetaryAmountEntity)
.where(monetaryAmountEntity.monetaryAmount.amount.gt(new BigDecimal("100")))
.where(monetaryAmountEntity.monetaryAmount.currencyUnit.eq(Monetary.getCurrency("EUR")))
.select(monetaryAmountEntity.monetaryAmount)
.fetchOne();
Assert.assertThat(result, CoreMatchers.is(CoreMatchers.notNullValue()));
});
}
@Test
public void testMoneyParameter() {
doInJPA(this::sessionFactory, entityManager -> {
CurrencyUnit eur = Monetary.getCurrency("EUR");
MonetaryAmount monetaryAmount = Monetary.getDefaultAmountFactory().setCurrency(eur).setNumber(200).create();
MonetaryAmount result = new JPAQuery<PeriodEntity>(entityManager, ExtendedHQLTemplates.DEFAULT)
.from(monetaryAmountEntity)
.where(monetaryAmountEntity.monetaryAmount.eq(monetaryAmount))
.select(monetaryAmountEntity.monetaryAmount)
.fetchOne();
Assert.assertEquals(monetaryAmount, result);
});
}
}