use of org.estatio.module.budget.dom.keyitem.KeyItem in project estatio by estatio.
the class DistributionService_Test method generateKeyValuesRoundingBy2DecimalsNegativeDelta.
@Test
public void generateKeyValuesRoundingBy2DecimalsNegativeDelta() {
// given
DistributionService distributionService = new DistributionService();
List<Distributable> input = new ArrayList<>();
// 10000.99
KeyItem item1 = new KeyItem();
item1.setSourceValue(BigDecimal.valueOf(10000.99));
input.add(item1);
// 200.09
KeyItem item2 = new KeyItem();
item2.setSourceValue(BigDecimal.valueOf(200.09));
input.add(item2);
// 1.99
for (int i = 2; i < 14; i = i + 1) {
KeyItem item = new KeyItem();
item.setSourceValue(BigDecimal.valueOf(1.99));
input.add(item);
}
// theoretical max rounding error: 14*0.005 = +/-0.07
// in this example here we get to -0.05
// when
List<Distributable> output = distributionService.distribute(input, new BigDecimal(1000), 2);
BigDecimal sumRoundedValues = BigDecimal.ZERO;
for (Distributable object : output) {
sumRoundedValues = sumRoundedValues.add(object.getValue(), MathContext.DECIMAL32);
}
// then
assertThat(output).hasSize(14);
assertThat(output.get(0).getValue()).isEqualTo(BigDecimal.valueOf(978.10).setScale(2, BigDecimal.ROUND_HALF_UP));
assertThat(output.get(1).getValue()).isEqualTo(BigDecimal.valueOf(19.57).setScale(2, BigDecimal.ROUND_HALF_UP));
for (int i = 2; i < 9; i = i + 1) {
assertThat(output.get(i).getValue()).isEqualTo(BigDecimal.valueOf(0.19).setScale(2, BigDecimal.ROUND_HALF_UP));
}
for (int i = 9; i < 14; i = i + 1) {
assertThat(output.get(i).getValue()).isEqualTo(BigDecimal.valueOf(0.20).setScale(2, BigDecimal.ROUND_HALF_UP));
}
assertThat(sumRoundedValues.setScale(2, BigDecimal.ROUND_HALF_UP)).isEqualTo(BigDecimal.valueOf(1000.00).setScale(2, BigDecimal.ROUND_HALF_UP));
}
use of org.estatio.module.budget.dom.keyitem.KeyItem in project estatio by estatio.
the class DistributionService_Test method oneNonZeroSourceValue.
@Test
public void oneNonZeroSourceValue() {
// given
DistributionService distributionService = new DistributionService();
List<Distributable> input = new ArrayList<>();
for (int i = 1; i <= 5; i = i + 1) {
KeyItem item = new KeyItem();
item.setSourceValue(BigDecimal.ZERO);
input.add(item);
}
KeyItem item = new KeyItem();
item.setSourceValue(BigDecimal.ONE);
input.add(item);
// when
List<Distributable> output = distributionService.distribute(input, BigDecimal.ONE, 3);
BigDecimal sumRoundedValues = BigDecimal.ZERO;
for (Distributable object : output) {
sumRoundedValues = sumRoundedValues.add(object.getValue(), MathContext.DECIMAL64);
}
// then
assertThat(output).hasSize(6);
assertThat(sumRoundedValues.setScale(3, BigDecimal.ROUND_HALF_UP)).isEqualTo(BigDecimal.ONE.setScale(3, BigDecimal.ROUND_HALF_UP));
}
use of org.estatio.module.budget.dom.keyitem.KeyItem in project estatio by estatio.
the class DistributionService_Test method justZeroSourceValues.
@Test
public void justZeroSourceValues() {
// given
DistributionService distributionService = new DistributionService();
List<Distributable> input = new ArrayList<>();
for (int i = 1; i <= 5; i = i + 1) {
KeyItem item = new KeyItem();
item.setSourceValue(BigDecimal.ZERO);
input.add(item);
}
// when
List<Distributable> output = distributionService.distribute(input, BigDecimal.ONE, 3);
BigDecimal sumRoundedValues = BigDecimal.ZERO;
for (Distributable object : output) {
sumRoundedValues = sumRoundedValues.add(object.getValue(), MathContext.DECIMAL64);
}
// then
assertThat(output).hasSize(5);
assertThat(sumRoundedValues.setScale(3, BigDecimal.ROUND_HALF_UP)).isEqualTo(BigDecimal.ZERO.setScale(3, BigDecimal.ROUND_HALF_UP));
}
use of org.estatio.module.budget.dom.keyitem.KeyItem in project estatio by estatio.
the class KeyItem_Test method testChangeAuditedValue.
@Test
public void testChangeAuditedValue() {
// given
KeyTable table = new KeyTable();
table.setPrecision(3);
KeyItem item = new KeyItem();
item.setAuditedValue(new BigDecimal(2));
item.setKeyTable(table);
assertTrue(item.getAuditedValue().equals(new BigDecimal(2)));
// when
item.changeAuditedValue(BigDecimal.valueOf(2.3335));
// then
assertEquals(item.getAuditedValue(), BigDecimal.valueOf(2.334).setScale(3, BigDecimal.ROUND_HALF_UP));
}
use of org.estatio.module.budget.dom.keyitem.KeyItem in project estatio by estatio.
the class KeyItem_Test method testChangeSourceValue.
@Test
public void testChangeSourceValue() {
// given
KeyTable table = new KeyTable();
table.setPrecision(3);
KeyItem item = new KeyItem();
item.setSourceValue(new BigDecimal(2));
item.setKeyTable(table);
assertTrue(item.getSourceValue().equals(new BigDecimal(2)));
// when
item.changeSourceValue(BigDecimal.valueOf(2.335));
// then
assertEquals(item.getSourceValue(), BigDecimal.valueOf(2.34).setScale(2, BigDecimal.ROUND_HALF_UP));
}
Aggregations