use of org.estatio.module.budget.dom.keyitem.KeyItem in project estatio by estatio.
the class DistributionService_Test method generateKeyValuesRoundingBy3DecimalsPositiveDeltaPercent.
@Test
public void generateKeyValuesRoundingBy3DecimalsPositiveDeltaPercent() {
// 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);
// 0.99
KeyItem item2 = new KeyItem();
item2.setSourceValue(BigDecimal.valueOf(0.99));
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.0005 = +/- 0.007
// in this example here we get to 0.002
// when
List<Distributable> output = distributionService.distribute(input, new BigDecimal(100), 3);
BigDecimal sumRoundedValues = BigDecimal.ZERO;
for (Distributable object : output) {
sumRoundedValues = sumRoundedValues.add(object.getValue(), MathContext.DECIMAL64);
}
// then
assertThat(output).hasSize(14);
assertThat(output.get(0).getValue()).isEqualTo(BigDecimal.valueOf(99.752).setScale(3, BigDecimal.ROUND_HALF_UP));
assertThat(output.get(1).getValue()).isEqualTo(BigDecimal.valueOf(0.010).setScale(3, BigDecimal.ROUND_HALF_UP));
for (int i = 2; i < 12; i = i + 1) {
assertThat(output.get(i).getValue()).isEqualTo(BigDecimal.valueOf(0.020).setScale(3, BigDecimal.ROUND_HALF_UP));
}
for (int i = 12; i < 14; i = i + 1) {
assertThat(output.get(i).getValue()).isEqualTo(BigDecimal.valueOf(0.019).setScale(3, BigDecimal.ROUND_HALF_UP));
}
// Corrected Rounding Error for 3 decimals
assertThat(sumRoundedValues.setScale(3, BigDecimal.ROUND_HALF_UP)).isEqualTo(BigDecimal.valueOf(100.000).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 generateKeyValuesRoundingBy3DecimalsNegativeDeltaPromille.
@Test
public void generateKeyValuesRoundingBy3DecimalsNegativeDeltaPromille() {
// 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);
// 0.99
KeyItem item2 = new KeyItem();
item2.setSourceValue(BigDecimal.valueOf(0.99));
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.0005 = +/- 0.007
// in this example here we get to -0.006
// when
List<Distributable> output = distributionService.distribute(input, new BigDecimal(1000), 3);
BigDecimal sumRoundedValues = BigDecimal.ZERO;
for (Distributable object : output) {
sumRoundedValues = sumRoundedValues.add(object.getValue());
}
// then
assertThat(output).hasSize(14);
assertThat(output.get(0).getValue()).isEqualTo(BigDecimal.valueOf(997.519).setScale(3, BigDecimal.ROUND_HALF_UP));
assertThat(output.get(1).getValue()).isEqualTo(BigDecimal.valueOf(0.099).setScale(3, BigDecimal.ROUND_HALF_UP));
for (int i = 2; i < 8; i = i + 1) {
assertThat(output.get(i).getValue()).isEqualTo(BigDecimal.valueOf(0.198).setScale(3, BigDecimal.ROUND_HALF_UP));
}
for (int i = 8; i < 14; i = i + 1) {
assertThat(output.get(i).getValue()).isEqualTo(BigDecimal.valueOf(0.199).setScale(3, BigDecimal.ROUND_HALF_UP));
}
assertThat(sumRoundedValues.setScale(3, BigDecimal.ROUND_HALF_UP)).isEqualTo(BigDecimal.valueOf(1000.000).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 noDeltaAssignedToItemsWithZeroSourceValue.
@Test
public void noDeltaAssignedToItemsWithZeroSourceValue() {
// 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);
// 0.99
KeyItem item2 = new KeyItem();
item2.setSourceValue(BigDecimal.valueOf(0.99));
input.add(item2);
// 1.99
for (int i = 2; i < 11; i = i + 1) {
KeyItem item = new KeyItem();
item.setSourceValue(BigDecimal.valueOf(1.99));
input.add(item);
}
// 0
for (int i = 11; i < 14; i = i + 1) {
KeyItem item = new KeyItem();
item.setSourceValue(BigDecimal.ZERO);
input.add(item);
}
// theoretical max rounding error: 14*0.0005 = +/- 0.007
// in this example here we get to +0.004
// when
List<Distributable> output = distributionService.distribute(input, new BigDecimal(1000), 3);
BigDecimal sumRoundedValues = BigDecimal.ZERO;
for (Distributable object : output) {
sumRoundedValues = sumRoundedValues.add(object.getValue());
}
// then
assertThat(output).hasSize(14);
assertThat(output.get(0).getValue()).isEqualTo(BigDecimal.valueOf(998.114).setScale(3, BigDecimal.ROUND_HALF_UP));
assertThat(output.get(1).getValue()).isEqualTo(BigDecimal.valueOf(0.099).setScale(3, BigDecimal.ROUND_HALF_UP));
for (int i = 2; i < 7; i = i + 1) {
assertThat(output.get(i).getValue()).isEqualTo(BigDecimal.valueOf(0.199).setScale(3, BigDecimal.ROUND_HALF_UP));
}
for (int i = 7; i < 11; i = i + 1) {
assertThat(output.get(i).getValue()).isEqualTo(BigDecimal.valueOf(0.198).setScale(3, BigDecimal.ROUND_HALF_UP));
}
for (int i = 11; i < 14; i = i + 1) {
assertThat(output.get(i).getValue()).isEqualTo(BigDecimal.valueOf(0.000).setScale(3, BigDecimal.ROUND_HALF_UP));
}
assertThat(sumRoundedValues.setScale(3, BigDecimal.ROUND_HALF_UP)).isEqualTo(BigDecimal.valueOf(1000.000).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 testValidateChangeValue.
@Test
public void testValidateChangeValue() {
// given
KeyTable table = new KeyTable();
table.setPrecision(3);
KeyItem item = new KeyItem();
item.setValue(new BigDecimal(2));
item.setKeyTable(table);
assertTrue(item.getValue().equals(new BigDecimal(2)));
// when, then
assertEquals(item.validateChangeValue(BigDecimal.valueOf(-0.001)), "Value cannot be less than zero");
}
use of org.estatio.module.budget.dom.keyitem.KeyItem in project estatio by estatio.
the class KeyItem_Test method testChangeValue.
@Test
public void testChangeValue() {
// given
KeyTable table = new KeyTable();
table.setPrecision(3);
KeyItem item = new KeyItem();
item.setValue(new BigDecimal(2));
item.setKeyTable(table);
assertTrue(item.getValue().equals(new BigDecimal(2)));
// when
item.changeValue(BigDecimal.valueOf(2.3335));
// then
assertEquals(item.getValue(), BigDecimal.valueOf(2.334).setScale(3, BigDecimal.ROUND_HALF_UP));
}
Aggregations