Revert "Removing not-implemented InterestRange from interface, but keeping it in DB."

This reverts commit 73193ab
diff --git a/api/src/main/java/io/mifos/portfolio/api/v1/domain/InterestRange.java b/api/src/main/java/io/mifos/portfolio/api/v1/domain/InterestRange.java
new file mode 100644
index 0000000..d119bd7
--- /dev/null
+++ b/api/src/main/java/io/mifos/portfolio/api/v1/domain/InterestRange.java
@@ -0,0 +1,86 @@
+/*
+ * Copyright 2017 The Mifos Initiative.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package io.mifos.portfolio.api.v1.domain;
+
+import org.hibernate.validator.constraints.ScriptAssert;
+
+import javax.validation.constraints.DecimalMax;
+import javax.validation.constraints.DecimalMin;
+import java.math.BigDecimal;
+
+/**
+ * @author Myrle Krantz
+ */
+@SuppressWarnings({"WeakerAccess", "unused"})
+@ScriptAssert(lang = "javascript", script = "_this.maximum != null && _this.minimum != null && _this.maximum.compareTo(_this.minimum) >= 0 && _this.minimum.scale() <= 2 && _this.maximum.scale() <= 2")
+public class InterestRange {
+  @DecimalMin(value = "0.00")
+  @DecimalMax(value = "999.99")
+  private BigDecimal minimum;
+  @DecimalMin(value = "0.00")
+  @DecimalMax(value = "999.99")
+  private BigDecimal maximum;
+
+  public InterestRange() {
+  }
+
+  public InterestRange(BigDecimal minimum, BigDecimal maximum) {
+    this.minimum = minimum;
+    this.maximum = maximum;
+  }
+
+  public BigDecimal getMinimum() {
+    return minimum;
+  }
+
+  public void setMinimum(BigDecimal minimum) {
+    this.minimum = minimum;
+  }
+
+  public BigDecimal getMaximum() {
+    return maximum;
+  }
+
+  public void setMaximum(BigDecimal maximum) {
+    this.maximum = maximum;
+  }
+
+  @Override
+  public boolean equals(Object o) {
+    if (this == o) return true;
+    if (o == null || getClass() != o.getClass()) return false;
+
+    InterestRange that = (InterestRange) o;
+
+    return minimum != null ? minimum.equals(that.minimum) : that.minimum == null && (maximum != null ? maximum.equals(that.maximum) : that.maximum == null);
+
+  }
+
+  @Override
+  public int hashCode() {
+    int result = minimum != null ? minimum.hashCode() : 0;
+    result = 31 * result + (maximum != null ? maximum.hashCode() : 0);
+    return result;
+  }
+
+  @Override
+  public String toString() {
+    return "InterestRange{" +
+            "minimum=" + minimum +
+            ", maximum=" + maximum +
+            '}';
+  }
+}
diff --git a/api/src/main/java/io/mifos/portfolio/api/v1/domain/Product.java b/api/src/main/java/io/mifos/portfolio/api/v1/domain/Product.java
index 6f34f9b..0e7274d 100644
--- a/api/src/main/java/io/mifos/portfolio/api/v1/domain/Product.java
+++ b/api/src/main/java/io/mifos/portfolio/api/v1/domain/Product.java
@@ -43,6 +43,9 @@
   @Valid
   private BalanceRange balanceRange;
   @NotNull
+  @Valid
+  private InterestRange interestRange;
+  @NotNull
   private InterestBasis interestBasis;
   @NotNull
   @ValidIdentifier(maxLength = 512)
@@ -101,6 +104,14 @@
     this.balanceRange = balanceRange;
   }
 
+  public InterestRange getInterestRange() {
+    return interestRange;
+  }
+
+  public void setInterestRange(InterestRange interestRange) {
+    this.interestRange = interestRange;
+  }
+
   public InterestBasis getInterestBasis() {
     return interestBasis;
   }
@@ -199,6 +210,7 @@
             Objects.equals(name, product.name) &&
             Objects.equals(termRange, product.termRange) &&
             Objects.equals(balanceRange, product.balanceRange) &&
+            Objects.equals(interestRange, product.interestRange) &&
             interestBasis == product.interestBasis &&
             Objects.equals(patternPackage, product.patternPackage) &&
             Objects.equals(description, product.description) &&
@@ -209,7 +221,7 @@
 
   @Override
   public int hashCode() {
-    return Objects.hash(identifier, name, termRange, balanceRange, interestBasis, patternPackage, description, currencyCode, minorCurrencyUnitDigits, accountAssignments, parameters);
+    return Objects.hash(identifier, name, termRange, balanceRange, interestRange, interestBasis, patternPackage, description, currencyCode, minorCurrencyUnitDigits, accountAssignments, parameters);
   }
 
   @Override
@@ -219,6 +231,7 @@
             ", name='" + name + '\'' +
             ", termRange=" + termRange +
             ", balanceRange=" + balanceRange +
+            ", interestRange=" + interestRange +
             ", interestBasis=" + interestBasis +
             ", patternPackage='" + patternPackage + '\'' +
             ", description='" + description + '\'' +
diff --git a/api/src/test/java/io/mifos/Fixture.java b/api/src/test/java/io/mifos/Fixture.java
index f45d457..c253395 100644
--- a/api/src/test/java/io/mifos/Fixture.java
+++ b/api/src/test/java/io/mifos/Fixture.java
@@ -55,6 +55,7 @@
     product.setDescription("Loan for seeds or agricultural equipment");
     product.setTermRange(new TermRange(ChronoUnit.MONTHS, 12));
     product.setBalanceRange(new BalanceRange(fixScale(BigDecimal.ZERO), fixScale(new BigDecimal(10000))));
+    product.setInterestRange(new InterestRange(BigDecimal.valueOf(3, 2), BigDecimal.valueOf(12, 2)));
     product.setInterestBasis(InterestBasis.CURRENT_BALANCE);
 
     product.setCurrencyCode("XXX");
diff --git a/api/src/test/java/io/mifos/portfolio/api/v1/domain/InterestRangeTest.java b/api/src/test/java/io/mifos/portfolio/api/v1/domain/InterestRangeTest.java
new file mode 100644
index 0000000..0f4a851
--- /dev/null
+++ b/api/src/test/java/io/mifos/portfolio/api/v1/domain/InterestRangeTest.java
@@ -0,0 +1,79 @@
+/*
+ * Copyright 2017 The Mifos Initiative.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package io.mifos.portfolio.api.v1.domain;
+
+import io.mifos.core.test.domain.ValidationTest;
+import io.mifos.core.test.domain.ValidationTestCase;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+import java.math.BigDecimal;
+import java.util.ArrayList;
+import java.util.Collection;
+
+/**
+ * @author Myrle Krantz
+ */
+@RunWith(Parameterized.class)
+public class InterestRangeTest extends ValidationTest<InterestRange> {
+  public InterestRangeTest(ValidationTestCase<InterestRange> testCase) {
+    super(testCase);
+  }
+
+  @Override
+  protected InterestRange createValidTestSubject() {
+    return new InterestRange(BigDecimal.valueOf(0.02d).setScale(2, BigDecimal.ROUND_UNNECESSARY),
+            BigDecimal.valueOf(0.03d).setScale(2, BigDecimal.ROUND_UNNECESSARY));
+  }
+
+  @Parameterized.Parameters
+  public static Collection testCases() {
+    final Collection<ValidationTestCase> ret = new ArrayList<>();
+    ret.add(new ValidationTestCase<InterestRange>("basicCase")
+            .adjustment(x -> {})
+            .valid(true));
+    ret.add(new ValidationTestCase<InterestRange>("5 and 10")
+            .adjustment(x -> {
+              x.setMinimum(BigDecimal.valueOf(5L).setScale(2, BigDecimal.ROUND_UNNECESSARY));
+              x.setMaximum(BigDecimal.valueOf(10L).setScale(2, BigDecimal.ROUND_UNNECESSARY));
+            })
+            .valid(true));
+    ret.add(new ValidationTestCase<InterestRange>("5 and 5")
+            .adjustment(x -> {
+              x.setMinimum(BigDecimal.valueOf(5L).setScale(2, BigDecimal.ROUND_UNNECESSARY));
+              x.setMaximum(BigDecimal.valueOf(5L).setScale(2, BigDecimal.ROUND_UNNECESSARY));
+            })
+            .valid(true));
+    ret.add(new ValidationTestCase<InterestRange>("maxNull")
+            .adjustment((x) -> x.setMaximum(null))
+            .valid(false));
+    ret.add(new ValidationTestCase<InterestRange>("maximim smaller than minimum")
+            .adjustment(x -> {
+              x.setMinimum(BigDecimal.valueOf(10L).setScale(2, BigDecimal.ROUND_UNNECESSARY));
+              x.setMaximum(BigDecimal.valueOf(5L).setScale(2, BigDecimal.ROUND_UNNECESSARY));
+            })
+            .valid(false));
+    ret.add(new ValidationTestCase<InterestRange>("too large scale")
+            .adjustment(x ->
+                    x.setMinimum(x.getMinimum().setScale(3, BigDecimal.ROUND_UNNECESSARY)))
+            .valid(false));
+    ret.add(new ValidationTestCase<InterestRange>("smaller scale")
+            .adjustment(x ->
+                    x.setMinimum(x.getMinimum().setScale(1, BigDecimal.ROUND_HALF_EVEN)))
+            .valid(true));
+    return ret;
+  }
+}
\ No newline at end of file
diff --git a/api/src/test/java/io/mifos/portfolio/api/v1/domain/ProductTest.java b/api/src/test/java/io/mifos/portfolio/api/v1/domain/ProductTest.java
index 9ab2448..3ab8947 100644
--- a/api/src/test/java/io/mifos/portfolio/api/v1/domain/ProductTest.java
+++ b/api/src/test/java/io/mifos/portfolio/api/v1/domain/ProductTest.java
@@ -80,6 +80,22 @@
     ret.add(new ValidationTestCase<Product>("switchedBalanceRangeMinMax")
             .adjustment(product -> product.setBalanceRange(new BalanceRange(Fixture.fixScale(BigDecimal.TEN), Fixture.fixScale(BigDecimal.ZERO))))
             .valid(false));
+    ret.add(new ValidationTestCase<Product>("nullInterestRange")
+            .adjustment(product -> product.setInterestRange(null))
+            .valid(false));
+    //noinspection BigDecimalMethodWithoutRoundingCalled
+    ret.add(new ValidationTestCase<Product>("switchedInterestRangeMinMax")
+            .adjustment(product -> product.setInterestRange(new InterestRange(BigDecimal.valueOf(200, 2), BigDecimal.valueOf(0.9).setScale(2))))
+            .valid(false));
+    ret.add(new ValidationTestCase<Product>("tooBigMaximumInterestRange")
+            .adjustment(product -> product.setInterestRange(new InterestRange(new BigDecimal("999.99"), new BigDecimal("1000.00"))))
+            .valid(false));
+    ret.add(new ValidationTestCase<Product>("negativeMinimumInterestRange")
+            .adjustment(product -> product.setInterestRange(new InterestRange(BigDecimal.valueOf(-1, 2), BigDecimal.valueOf(1, 2))))
+            .valid(false));
+    ret.add(new ValidationTestCase<Product>("tooManyDigitsAfterTheDecimalInterestRange")
+            .adjustment(product -> product.setInterestRange(new InterestRange(BigDecimal.valueOf(1, 2), new BigDecimal("1.001"))))
+            .valid(false));
     ret.add(new ValidationTestCase<Product>("nullInterestBasis")
             .adjustment(product -> product.setInterestBasis(null))
             .valid(false));
diff --git a/component-test/src/main/java/io/mifos/portfolio/Fixture.java b/component-test/src/main/java/io/mifos/portfolio/Fixture.java
index 0f02397..8c1a110 100644
--- a/component-test/src/main/java/io/mifos/portfolio/Fixture.java
+++ b/component-test/src/main/java/io/mifos/portfolio/Fixture.java
@@ -53,6 +53,7 @@
     product.setDescription("Loan for seeds or agricultural equipment");
     product.setTermRange(new TermRange(ChronoUnit.MONTHS, 12));
     product.setBalanceRange(new BalanceRange(fixScale(BigDecimal.ZERO), fixScale(new BigDecimal(10000))));
+    product.setInterestRange(new InterestRange(BigDecimal.valueOf(3, 2), BigDecimal.valueOf(12, 2)));
     product.setInterestBasis(InterestBasis.CURRENT_BALANCE);
 
     product.setCurrencyCode("XXX");
diff --git a/component-test/src/main/java/io/mifos/portfolio/TestProducts.java b/component-test/src/main/java/io/mifos/portfolio/TestProducts.java
index f370f90..876d07a 100644
--- a/component-test/src/main/java/io/mifos/portfolio/TestProducts.java
+++ b/component-test/src/main/java/io/mifos/portfolio/TestProducts.java
@@ -332,6 +332,7 @@
     product.setDescription(StringUtils.repeat("x", 4096));
     product.setTermRange(new TermRange(ChronoUnit.MONTHS, 12));
     product.setBalanceRange(new BalanceRange(BigDecimal.ZERO.setScale(4, BigDecimal.ROUND_UNNECESSARY), new BigDecimal(10000).setScale(4, BigDecimal.ROUND_UNNECESSARY)));
+    product.setInterestRange(new InterestRange(new BigDecimal("999.98"), new BigDecimal("999.99")));
     product.setInterestBasis(InterestBasis.CURRENT_BALANCE);
     product.setCurrencyCode("XTS");
     product.setMinorCurrencyUnitDigits(4);
diff --git a/service/src/main/java/io/mifos/portfolio/service/internal/mapper/ProductMapper.java b/service/src/main/java/io/mifos/portfolio/service/internal/mapper/ProductMapper.java
index 40d85d8..4e470b3 100644
--- a/service/src/main/java/io/mifos/portfolio/service/internal/mapper/ProductMapper.java
+++ b/service/src/main/java/io/mifos/portfolio/service/internal/mapper/ProductMapper.java
@@ -43,6 +43,8 @@
                     productEntity.getTermRangeMaximum()));
     product.setBalanceRange(
             new BalanceRange(productEntity.getBalanceRangeMinimum().setScale(productEntity.getMinorCurrencyUnitDigits(), BigDecimal.ROUND_HALF_EVEN), productEntity.getBalanceRangeMaximum().setScale(productEntity.getMinorCurrencyUnitDigits(), BigDecimal.ROUND_HALF_EVEN)));
+    product.setInterestRange(
+            new InterestRange(productEntity.getInterestRangeMinimum(), productEntity.getInterestRangeMaximum()));
     product.setInterestBasis(productEntity.getInterestBasis());
     product.setPatternPackage(productEntity.getPatternPackage());
     product.setDescription(productEntity.getDescription());
@@ -70,8 +72,8 @@
     ret.setTermRangeMaximum(product.getTermRange().getMaximum());
     ret.setBalanceRangeMinimum(product.getBalanceRange().getMinimum());
     ret.setBalanceRangeMaximum(product.getBalanceRange().getMaximum());
-    ret.setInterestRangeMinimum(BigDecimal.ZERO);
-    ret.setInterestRangeMaximum(BigDecimal.valueOf(999.99));
+    ret.setInterestRangeMinimum(product.getInterestRange().getMinimum());
+    ret.setInterestRangeMaximum(product.getInterestRange().getMaximum());
     ret.setInterestBasis(product.getInterestBasis());
     ret.setPatternPackage(product.getPatternPackage());
     ret.setDescription(product.getDescription());