|
40 | 40 | import static org.elasticsearch.xpack.inference.services.ServiceUtils.extractOptionalTimeValue; |
41 | 41 | import static org.elasticsearch.xpack.inference.services.ServiceUtils.extractRequiredMap; |
42 | 42 | import static org.elasticsearch.xpack.inference.services.ServiceUtils.extractRequiredPositiveInteger; |
| 43 | +import static org.elasticsearch.xpack.inference.services.ServiceUtils.extractRequiredPositiveIntegerGreaterThanOrEqualToMin; |
43 | 44 | import static org.elasticsearch.xpack.inference.services.ServiceUtils.extractRequiredPositiveIntegerLessThanOrEqualToMax; |
44 | 45 | import static org.elasticsearch.xpack.inference.services.ServiceUtils.extractRequiredSecureString; |
45 | 46 | import static org.elasticsearch.xpack.inference.services.ServiceUtils.extractRequiredString; |
@@ -656,6 +657,69 @@ public void testExtractRequiredPositiveIntegerLessThanOrEqualToMax_AddsErrorWhen |
656 | 657 | assertThat(validation.validationErrors().get(1), is("[scope] does not contain the required setting [not_key]")); |
657 | 658 | } |
658 | 659 |
|
| 660 | + public void testExtractRequiredPositiveIntegerGreaterThanOrEqualToMin_ReturnsValueWhenValueIsEqualToMin() { |
| 661 | + testExtractRequiredPositiveIntegerGreaterThanOrEqualToMin_Successful(5, 5); |
| 662 | + } |
| 663 | + |
| 664 | + public void testExtractRequiredPositiveIntegerGreaterThanOrEqualToMin_ReturnsValueWhenValueIsGreaterThanToMin() { |
| 665 | + testExtractRequiredPositiveIntegerGreaterThanOrEqualToMin_Successful(5, 6); |
| 666 | + } |
| 667 | + |
| 668 | + private void testExtractRequiredPositiveIntegerGreaterThanOrEqualToMin_Successful(int minValue, int actualValue) { |
| 669 | + var validation = new ValidationException(); |
| 670 | + validation.addValidationError("previous error"); |
| 671 | + Map<String, Object> map = modifiableMap(Map.of("key", actualValue)); |
| 672 | + var parsedInt = extractRequiredPositiveIntegerGreaterThanOrEqualToMin(map, "key", minValue, "scope", validation); |
| 673 | + |
| 674 | + assertThat(validation.validationErrors(), hasSize(1)); |
| 675 | + assertNotNull(parsedInt); |
| 676 | + assertThat(parsedInt, is(actualValue)); |
| 677 | + assertTrue(map.isEmpty()); |
| 678 | + } |
| 679 | + |
| 680 | + public void testExtractRequiredPositiveIntegerGreaterThanOrEqualToMin_AddsErrorWhenValueIsLessThanMin() { |
| 681 | + testExtractRequiredPositiveIntegerGreaterThanOrEqualToMin_AddsError( |
| 682 | + "key", |
| 683 | + 5, |
| 684 | + 4, |
| 685 | + "[scope] Invalid value [4.0]. [key] must be a greater than or equal to [5.0]" |
| 686 | + ); |
| 687 | + } |
| 688 | + |
| 689 | + public void testExtractRequiredPositiveIntegerGreaterThanOrEqualToMin_AddsErrorWhenKeyIsMissing() { |
| 690 | + testExtractRequiredPositiveIntegerGreaterThanOrEqualToMin_AddsError( |
| 691 | + "not_key", |
| 692 | + 5, |
| 693 | + -1, |
| 694 | + "[scope] does not contain the required setting [not_key]" |
| 695 | + ); |
| 696 | + } |
| 697 | + |
| 698 | + public void testExtractRequiredPositiveIntegerGreaterThanOrEqualToMin_AddsErrorOnNegativeValue() { |
| 699 | + testExtractRequiredPositiveIntegerGreaterThanOrEqualToMin_AddsError( |
| 700 | + "key", |
| 701 | + 5, |
| 702 | + -1, |
| 703 | + "[scope] Invalid value [-1]. [key] must be a positive integer" |
| 704 | + ); |
| 705 | + } |
| 706 | + |
| 707 | + private void testExtractRequiredPositiveIntegerGreaterThanOrEqualToMin_AddsError( |
| 708 | + String key, |
| 709 | + int minValue, |
| 710 | + int actualValue, |
| 711 | + String error |
| 712 | + ) { |
| 713 | + var validation = new ValidationException(); |
| 714 | + validation.addValidationError("previous error"); |
| 715 | + Map<String, Object> map = modifiableMap(Map.of("key", actualValue)); |
| 716 | + var parsedInt = extractRequiredPositiveIntegerGreaterThanOrEqualToMin(map, key, minValue, "scope", validation); |
| 717 | + |
| 718 | + assertThat(validation.validationErrors(), hasSize(2)); |
| 719 | + assertNull(parsedInt); |
| 720 | + assertThat(validation.validationErrors().get(1), containsString(error)); |
| 721 | + } |
| 722 | + |
659 | 723 | public void testExtractRequiredPositiveIntegerBetween_ReturnsValueWhenValueIsBetweenMinAndMax() { |
660 | 724 | var minValue = randomNonNegativeInt(); |
661 | 725 | var maxValue = randomIntBetween(minValue + 2, minValue + 10); |
|
0 commit comments