Search in sources :

Example 21 with KeyItem

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));
}
Also used : ArrayList(java.util.ArrayList) KeyItem(org.estatio.module.budget.dom.keyitem.KeyItem) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 22 with KeyItem

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));
}
Also used : ArrayList(java.util.ArrayList) KeyItem(org.estatio.module.budget.dom.keyitem.KeyItem) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 23 with KeyItem

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));
}
Also used : ArrayList(java.util.ArrayList) KeyItem(org.estatio.module.budget.dom.keyitem.KeyItem) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 24 with KeyItem

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");
}
Also used : KeyTable(org.estatio.module.budget.dom.keytable.KeyTable) KeyItem(org.estatio.module.budget.dom.keyitem.KeyItem) BigDecimal(java.math.BigDecimal) AbstractBeanPropertiesTest(org.incode.module.unittestsupport.dom.bean.AbstractBeanPropertiesTest) Test(org.junit.Test)

Example 25 with KeyItem

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));
}
Also used : KeyTable(org.estatio.module.budget.dom.keytable.KeyTable) KeyItem(org.estatio.module.budget.dom.keyitem.KeyItem) BigDecimal(java.math.BigDecimal) AbstractBeanPropertiesTest(org.incode.module.unittestsupport.dom.bean.AbstractBeanPropertiesTest) Test(org.junit.Test)

Aggregations

KeyItem (org.estatio.module.budget.dom.keyitem.KeyItem)27 BigDecimal (java.math.BigDecimal)23 Test (org.junit.Test)22 ArrayList (java.util.ArrayList)17 KeyTable (org.estatio.module.budget.dom.keytable.KeyTable)7 AbstractBeanPropertiesTest (org.incode.module.unittestsupport.dom.bean.AbstractBeanPropertiesTest)6 TreeSet (java.util.TreeSet)3 Action (org.apache.isis.applib.annotation.Action)3 List (java.util.List)1 ActionLayout (org.apache.isis.applib.annotation.ActionLayout)1 MemberOrder (org.apache.isis.applib.annotation.MemberOrder)1 Unit (org.estatio.module.asset.dom.Unit)1 Distributable (org.estatio.module.budget.dom.Distributable)1 DistributionService (org.estatio.module.budget.dom.DistributionService)1 KeyItemImportExportLineItem (org.estatio.module.budgetassignment.imports.KeyItemImportExportLineItem)1 WorksheetSpec (org.isisaddons.module.excel.dom.WorksheetSpec)1