Skip to content

Commit

Permalink
Fail on invalid formats (#2897)
Browse files Browse the repository at this point in the history
* Fail on invalid quantity format

* a more robust implementation

* cleanup
  • Loading branch information
andreaTP committed Mar 12, 2021
1 parent c3a1fb4 commit 72e6189
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class Quantity implements Serializable {

private static final String AT_LEAST_ONE_DIGIT_REGEX = ".*\\d+.*";
private String amount;
private String format;
private String format = "";
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

/**
Expand Down Expand Up @@ -76,7 +76,9 @@ public Quantity(String amount) {
*/
public Quantity(String amount, String format) {
this.amount = amount;
this.format = format;
if (format != null) {
this.format = format;
}
}

public String getAmount() {
Expand Down Expand Up @@ -170,6 +172,10 @@ public static BigDecimal getAmountInBytes(Quantity quantity) throws ArithmeticEx
case "E":
multiple = decimalFactor.pow(18, MathContext.DECIMAL64);
break;
case "":
break;
default:
throw new IllegalArgumentException("Invalid quantity format passed to parse");
}

return digit.multiply(multiple);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,5 +160,7 @@ public void testParseFailure() {
assertThrows(IllegalArgumentException.class, () -> new Quantity(""));
assertThrows(IllegalArgumentException.class, () -> new Quantity(null));
assertThrows(IllegalArgumentException.class, () -> Quantity.getAmountInBytes(new Quantity()));
assertThrows(IllegalArgumentException.class, () -> Quantity.getAmountInBytes(new Quantity("4MiB")));
assertThrows(IllegalArgumentException.class, () -> Quantity.getAmountInBytes(new Quantity("4megabyte")));
}
}

0 comments on commit 72e6189

Please sign in to comment.