Skip to content

Commit

Permalink
#42 support of maximum/minimum constraints on number/float/double par…
Browse files Browse the repository at this point in the history
…ameters
  • Loading branch information
galovics committed Oct 13, 2020
1 parent 3f25767 commit 5793d66
Show file tree
Hide file tree
Showing 7 changed files with 219 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ public NumberRequestParameter(RequestParameterInType inType, String name,
}

/**
* Determines if the object is integer typed or not.
* @return whether the object is integer typed
* Determines if the object is number typed or not.
* @return whether the object is number typed
*/
public boolean isIntegerTyped() {
return RequestParameterType.getIntegerTypes().contains(getRequestParameterType());
public boolean isNumberTyped() {
return RequestParameterType.getNumberTypes().contains(getRequestParameterType());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public RequestParameter create(Parameter from) {
String format = swSchema.getFormat();
RequestParameterType requestParameterType = RequestParameterType.from(type, format);
io.redskap.swagger.brake.core.model.Schema transformedSchema = schemaTransformer.transform(swSchema);
if (RequestParameterType.getIntegerTypes().contains(requestParameterType)) {
if (RequestParameterType.getNumberTypes().contains(requestParameterType)) {
BigDecimal maximum = swSchema.getMaximum();
Boolean exclusiveMaximum = swSchema.getExclusiveMaximum();
BigDecimal minimum = swSchema.getMinimum();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
@AllArgsConstructor
public enum RequestParameterType {
GENERIC(null, null),
NUMBER("number", null),
FLOAT("number", "float"),
DOUBLE("number", "double"),
INTEGER("integer", null),
INT_32("integer", "int32"),
INT_64("integer", "int64");
Expand All @@ -33,10 +36,10 @@ public static RequestParameterType from(String type, String format) {
}

/**
* Returns all integer types.
* @return all integer types
* Returns all number types.
* @return all number types
*/
public static Collection<RequestParameterType> getIntegerTypes() {
return ImmutableList.of(INTEGER, INT_32, INT_64);
public static Collection<RequestParameterType> getNumberTypes() {
return ImmutableList.of(NUMBER, FLOAT, DOUBLE, INTEGER, INT_32, INT_64);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class NumberMaximumConstraint implements RequestParameterConstraint<NumberReques
public Optional<RequestParameterConstraintChange> validateConstraints(NumberRequestParameter oldRequestParameter, NumberRequestParameter newRequestParameter) {
RequestParameterConstraintChange result = null;
if (oldRequestParameter != null && newRequestParameter != null) {
if (oldRequestParameter.isIntegerTyped() && newRequestParameter.isIntegerTyped()) {
if (oldRequestParameter.isNumberTyped() && newRequestParameter.isNumberTyped()) {
BigDecimal oldMaximum = oldRequestParameter.getMaximum();
BigDecimal newMaximum = newRequestParameter.getMaximum();
if (oldMaximum == null && newMaximum != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class NumberMinimumConstraint implements RequestParameterConstraint<NumberReques
public Optional<RequestParameterConstraintChange> validateConstraints(NumberRequestParameter oldRequestParameter, NumberRequestParameter newRequestParameter) {
RequestParameterConstraintChange result = null;
if (oldRequestParameter != null && newRequestParameter != null) {
if (oldRequestParameter.isIntegerTyped() && newRequestParameter.isIntegerTyped()) {
if (oldRequestParameter.isNumberTyped() && newRequestParameter.isNumberTyped()) {
BigDecimal oldMinimum = oldRequestParameter.getMinimum();
BigDecimal newMinimum = newRequestParameter.getMinimum();
if (oldMinimum == null && newMinimum != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,108 @@ public void testValidateConstraintsShouldReportConstraintChangeWhenMaximumValueS
assertThat(result).get().isEqualTo(expected);
}

@Test
public void testValidateConstraintsShouldReportConstraintChangeWhenMaximumValueSetGetsLimitedForNumber() {
// given
NumberRequestParameter oldRequestParameter = new NumberRequestParameter(
RequestParameterInType.PATH,
"testAttribute",
true,
null,
RequestParameterType.NUMBER,
BigDecimal.TEN,
BigDecimal.ZERO,
false,
true
);
NumberRequestParameter newRequestParameter = new NumberRequestParameter(
RequestParameterInType.PATH,
"testAttribute",
true,
null,
RequestParameterType.NUMBER,
BigDecimal.ONE,
BigDecimal.ZERO,
false,
true
);
RequestParameterConstraintChange expected = new RequestParameterConstraintChange(
"maximum", BigDecimal.TEN, BigDecimal.ONE
);
// when
Optional<RequestParameterConstraintChange> result = underTest.validateConstraints(oldRequestParameter, newRequestParameter);
// then
assertThat(result).get().isEqualTo(expected);
}

@Test
public void testValidateConstraintsShouldReportConstraintChangeWhenMaximumValueSetGetsLimitedForFloat() {
// given
NumberRequestParameter oldRequestParameter = new NumberRequestParameter(
RequestParameterInType.PATH,
"testAttribute",
true,
null,
RequestParameterType.FLOAT,
BigDecimal.TEN.add(new BigDecimal("0.5")),
BigDecimal.ZERO,
false,
true
);
NumberRequestParameter newRequestParameter = new NumberRequestParameter(
RequestParameterInType.PATH,
"testAttribute",
true,
null,
RequestParameterType.FLOAT,
BigDecimal.ONE.add(new BigDecimal("0.5")),
BigDecimal.ZERO,
false,
true
);
RequestParameterConstraintChange expected = new RequestParameterConstraintChange(
"maximum", BigDecimal.TEN.add(new BigDecimal("0.5")), BigDecimal.ONE.add(new BigDecimal("0.5"))
);
// when
Optional<RequestParameterConstraintChange> result = underTest.validateConstraints(oldRequestParameter, newRequestParameter);
// then
assertThat(result).get().isEqualTo(expected);
}

@Test
public void testValidateConstraintsShouldReportConstraintChangeWhenMaximumValueSetGetsLimitedForDouble() {
// given
NumberRequestParameter oldRequestParameter = new NumberRequestParameter(
RequestParameterInType.PATH,
"testAttribute",
true,
null,
RequestParameterType.DOUBLE,
BigDecimal.TEN.add(new BigDecimal("0.5")),
BigDecimal.ZERO,
false,
true
);
NumberRequestParameter newRequestParameter = new NumberRequestParameter(
RequestParameterInType.PATH,
"testAttribute",
true,
null,
RequestParameterType.DOUBLE,
BigDecimal.ONE.add(new BigDecimal("0.5")),
BigDecimal.ZERO,
false,
true
);
RequestParameterConstraintChange expected = new RequestParameterConstraintChange(
"maximum", BigDecimal.TEN.add(new BigDecimal("0.5")), BigDecimal.ONE.add(new BigDecimal("0.5"))
);
// when
Optional<RequestParameterConstraintChange> result = underTest.validateConstraints(oldRequestParameter, newRequestParameter);
// then
assertThat(result).get().isEqualTo(expected);
}

@Test
public void testValidateConstraintsShouldReportConstraintChangeWhenMaximumValueGetsSet() {
// given
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ public void testValidateConstraintsShouldReportConstraintChangeWhenMinimumValueS
// then
assertThat(result).isNotPresent();
}

@Test
public void testValidateConstraintsShouldReportConstraintChangeWhenMinimumValueSetIsLimitedWithExclusiveMaximumSettingEdgeCase2() {
// given
Expand Down Expand Up @@ -347,6 +347,108 @@ public void testValidateConstraintsShouldReportConstraintChangeWhenMinimumValueS
assertThat(result).get().isEqualTo(expected);
}

@Test
public void testValidateConstraintsShouldReportConstraintChangeWhenMinimumValueSetGetsLimitedForNumber() {
// given
NumberRequestParameter oldRequestParameter = new NumberRequestParameter(
RequestParameterInType.PATH,
"testAttribute",
true,
null,
RequestParameterType.NUMBER,
BigDecimal.TEN,
BigDecimal.ONE,
false,
true
);
NumberRequestParameter newRequestParameter = new NumberRequestParameter(
RequestParameterInType.PATH,
"testAttribute",
true,
null,
RequestParameterType.NUMBER,
BigDecimal.ONE,
BigDecimal.TEN,
false,
true
);
RequestParameterConstraintChange expected = new RequestParameterConstraintChange(
"minimum", BigDecimal.ONE, BigDecimal.TEN
);
// when
Optional<RequestParameterConstraintChange> result = underTest.validateConstraints(oldRequestParameter, newRequestParameter);
// then
assertThat(result).get().isEqualTo(expected);
}

@Test
public void testValidateConstraintsShouldReportConstraintChangeWhenMinimumValueSetGetsLimitedForFloat() {
// given
NumberRequestParameter oldRequestParameter = new NumberRequestParameter(
RequestParameterInType.PATH,
"testAttribute",
true,
null,
RequestParameterType.FLOAT,
BigDecimal.TEN,
BigDecimal.ONE.subtract(new BigDecimal("0.5")),
false,
true
);
NumberRequestParameter newRequestParameter = new NumberRequestParameter(
RequestParameterInType.PATH,
"testAttribute",
true,
null,
RequestParameterType.FLOAT,
BigDecimal.ONE,
BigDecimal.TEN.subtract(new BigDecimal("0.5")),
false,
true
);
RequestParameterConstraintChange expected = new RequestParameterConstraintChange(
"minimum", BigDecimal.ONE.subtract(new BigDecimal("0.5")), BigDecimal.TEN.subtract(new BigDecimal("0.5"))
);
// when
Optional<RequestParameterConstraintChange> result = underTest.validateConstraints(oldRequestParameter, newRequestParameter);
// then
assertThat(result).get().isEqualTo(expected);
}

@Test
public void testValidateConstraintsShouldReportConstraintChangeWhenMinimumValueSetGetsLimitedForDouble() {
// given
NumberRequestParameter oldRequestParameter = new NumberRequestParameter(
RequestParameterInType.PATH,
"testAttribute",
true,
null,
RequestParameterType.DOUBLE,
BigDecimal.TEN,
BigDecimal.ONE.subtract(new BigDecimal("0.5")),
false,
true
);
NumberRequestParameter newRequestParameter = new NumberRequestParameter(
RequestParameterInType.PATH,
"testAttribute",
true,
null,
RequestParameterType.DOUBLE,
BigDecimal.ONE,
BigDecimal.TEN.subtract(new BigDecimal("0.5")),
false,
true
);
RequestParameterConstraintChange expected = new RequestParameterConstraintChange(
"minimum", BigDecimal.ONE.subtract(new BigDecimal("0.5")), BigDecimal.TEN.subtract(new BigDecimal("0.5"))
);
// when
Optional<RequestParameterConstraintChange> result = underTest.validateConstraints(oldRequestParameter, newRequestParameter);
// then
assertThat(result).get().isEqualTo(expected);
}

@Test
public void testValidateConstraintsShouldReportConstraintChangeWhenMinimumValueGetsSet() {
// given
Expand Down

0 comments on commit 5793d66

Please sign in to comment.