Permalink
Browse files

catalog: Add default initialization for non required `Double` fields …

…in the catalog. Fixes #688

Note that code in JAXRS can now safely assume such fields will be not null and the NPE reported in the bug
disappears (as shown by the new profile test)
  • Loading branch information...
1 parent 735aac5 commit 275b89ef6b52228b061770872180b8d5439affac @sbrossie sbrossie committed Jan 11, 2017
@@ -33,6 +33,7 @@
public static final Integer DEFAULT_NON_REQUIRED_INTEGER_FIELD_VALUE = -1;
+ public static final Double DEFAULT_NON_REQUIRED_DOUBLE_FIELD_VALUE = new Double(-1);
private static final Map<Class, LinkedList<Field>> perCatalogClassNonRequiredFields = new HashMap<Class, LinkedList<Field>>();
@@ -60,6 +61,8 @@ public static void initializeNonRequiredNullFieldsWithDefaultValue(final Object
}
} else if (Integer.class.equals(f.getType())) {
initializeFieldWithValue(obj, f, DEFAULT_NON_REQUIRED_INTEGER_FIELD_VALUE);
+ } else if (Double.class.equals(f.getType())) {
+ initializeFieldWithValue(obj, f, DEFAULT_NON_REQUIRED_DOUBLE_FIELD_VALUE);
}
}
}
@@ -97,6 +100,8 @@ public static void initializeNonRequiredNullFieldsWithDefaultValue(final Object
}
} else if (Integer.class.equals(f.getType())) {
result.add(f);
+ } else if (Double.class.equals(f.getType())) {
+ result.add(f);
}
}
}
@@ -17,6 +17,8 @@
package org.killbill.billing.catalog;
+import java.net.URI;
+
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
@@ -78,7 +80,7 @@ public InternationalPrice getPrice() {
@Override
public Double getMinTopUpCredit() throws CatalogApiException {
- if (minTopUpCredit != null && type != BlockType.TOP_UP) {
+ if (minTopUpCredit != CatalogSafetyInitializer.DEFAULT_NON_REQUIRED_DOUBLE_FIELD_VALUE && type != BlockType.TOP_UP) {
throw new CatalogApiException(ErrorCode.CAT_NOT_TOP_UP_BLOCK, phase.getName());
}
return minTopUpCredit;
@@ -91,13 +93,20 @@ public ValidationErrors validate(final StandaloneCatalog catalog, final Validati
throw new IllegalStateException("type should have been automatically been initialized with VANILLA ");
}
- if (type == BlockType.TOP_UP && minTopUpCredit == null) {
+ if (type == BlockType.TOP_UP && minTopUpCredit == CatalogSafetyInitializer.DEFAULT_NON_REQUIRED_DOUBLE_FIELD_VALUE) {
errors.add(new ValidationError(String.format("TOP_UP block needs to define minTopUpCredit for phase %s",
phase.getName()), catalog.getCatalogURI(), DefaultUsage.class, ""));
}
return errors;
}
+ @Override
+ public void initialize(final StandaloneCatalog catalog, final URI sourceURI) {
+ super.initialize(catalog, sourceURI);
+ CatalogSafetyInitializer.initializeNonRequiredNullFieldsWithDefaultValue(this);
+ }
+
+
public DefaultBlock setType(final BlockType type) {
this.type = type;
return this;
@@ -69,12 +69,13 @@ public Double getMin() {
@Override
public ValidationErrors validate(StandaloneCatalog root, ValidationErrors errors) {
- if (max == null && min == null) {
+ if (max == CatalogSafetyInitializer.DEFAULT_NON_REQUIRED_DOUBLE_FIELD_VALUE && min == CatalogSafetyInitializer.DEFAULT_NON_REQUIRED_DOUBLE_FIELD_VALUE) {
errors.add(new ValidationError("max and min cannot both be ommitted", root.getCatalogURI(), Limit.class, ""));
- } else if (max != null && min != null && max.doubleValue() < min.doubleValue()) {
+ } else if (max != CatalogSafetyInitializer.DEFAULT_NON_REQUIRED_DOUBLE_FIELD_VALUE &&
+ min != CatalogSafetyInitializer.DEFAULT_NON_REQUIRED_DOUBLE_FIELD_VALUE &&
+ max.doubleValue() < min.doubleValue()) {
errors.add(new ValidationError("max must be greater than min", root.getCatalogURI(), Limit.class, ""));
}
-
return errors;
}
@@ -87,12 +88,12 @@ public void initialize(final StandaloneCatalog catalog, final URI sourceURI) {
@Override
public boolean compliesWith(double value) {
- if (max != null) {
+ if (max != CatalogSafetyInitializer.DEFAULT_NON_REQUIRED_DOUBLE_FIELD_VALUE) {
if (value > max.doubleValue()) {
return false;
}
}
- if (min != null) {
+ if (min != CatalogSafetyInitializer.DEFAULT_NON_REQUIRED_DOUBLE_FIELD_VALUE) {
if (value < min.doubleValue()) {
return false;
}
@@ -126,10 +127,10 @@ public boolean equals(final Object o) {
final DefaultLimit that = (DefaultLimit) o;
- if (max != null ? !max.equals(that.max) : that.max != null) {
+ if (!max.equals(that.max)) {
return false;
}
- if (min != null ? !min.equals(that.min) : that.min != null) {
+ if (!min.equals(that.min)) {
return false;
}
if (unit != null ? !unit.equals(that.unit) : that.unit != null) {
@@ -142,8 +143,8 @@ public boolean equals(final Object o) {
@Override
public int hashCode() {
int result = unit != null ? unit.hashCode() : 0;
- result = 31 * result + (max != null ? max.hashCode() : 0);
- result = 31 * result + (min != null ? min.hashCode() : 0);
+ result = 31 * result + max.hashCode();
+ result = 31 * result + min.hashCode();
return result;
}
}
@@ -134,6 +134,12 @@ public ValidationErrors validate(final StandaloneCatalog catalog, final Validati
public void initialize(final StandaloneCatalog catalog, final URI sourceURI) {
super.initialize(catalog, sourceURI);
CatalogSafetyInitializer.initializeNonRequiredNullFieldsWithDefaultValue(this);
+ for (DefaultLimit cur : limits) {
+ cur.initialize(catalog, sourceURI);
+ }
+ for (DefaultBlock cur : blocks) {
+ cur.initialize(catalog, sourceURI);
+ }
}
@@ -61,6 +61,15 @@ public void testMultiTenantCatalog() throws Exception {
}
@Test(groups = "slow")
+ public void testUploadAndFetchUsageCatlog() throws Exception {
+ final String versionPath1 = Resources.getResource("UsageExperimental.xml").getPath();
+ killBillClient.uploadXMLCatalog(versionPath1, requestOptions);
+ String catalog = killBillClient.getXMLCatalog(requestOptions);
+ Assert.assertNotNull(catalog);
+ }
+
+
+ @Test(groups = "slow")
public void testUploadWithErrors() throws Exception {
final String versionPath1 = Resources.getResource("SpyCarBasic.xml").getPath();
killBillClient.uploadXMLCatalog(versionPath1, requestOptions);
Oops, something went wrong.

1 comment on commit 275b89e

@pierre
Member
pierre commented on 275b89e Jan 16, 2017

👍

Please sign in to comment.