refactor(core): share JSON integer coercion (#7213)

This commit is contained in:
Rashen Anupama authored and GitHub committed 2026-11-01 15:10:32 -04:00
1 parent 0d6bbe9662
commit 32823e6341
7 files changed
+175 -30

No files matched your search

@@ -0,0 +1,72 @@
package io.github.hectorvent.floci.core.common;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.DoubleNode;
import org.junit.jupiter.api.Test;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
class JsonNumberCoercionTest {
private static final ObjectMapper MAPPER = new ObjectMapper();
private static JsonNode json(String value) {
try {
return MAPPER.readTree(value);
} catch (Exception e) {
throw new AssertionError(e);
}
}
@Test
void truncatesFractionsTowardZero() {
assertEquals(1, JsonNumberCoercion.truncateAndClampToInt(json("1.9")));
assertEquals(-1, JsonNumberCoercion.truncateAndClampToInt(json("-1.9")));
assertEquals(0, JsonNumberCoercion.truncateAndClampToInt(json("-0.9")));
}
@Test
void preservesInRangeValuesAndClampsIntegerBounds() {
assertEquals(Integer.MAX_VALUE,
JsonNumberCoercion.truncateAndClampToInt(json("2147483647")));
assertEquals(Integer.MIN_VALUE,
JsonNumberCoercion.truncateAndClampToInt(json("-2147483648")));
assertEquals(Integer.MAX_VALUE,
JsonNumberCoercion.truncateAndClampToInt(json("2147483648")));
assertEquals(Integer.MIN_VALUE,
JsonNumberCoercion.truncateAndClampToInt(json("-2147483649")));
}
@Test
void clampsHugeIntegerAndExponentValues() {
assertEquals(Integer.MAX_VALUE,
JsonNumberCoercion.truncateAndClampToInt(json("92233720368547758081234567890")));
assertEquals(Integer.MIN_VALUE,
JsonNumberCoercion.truncateAndClampToInt(json("-92233720368547758081234567890")));
assertEquals(Integer.MAX_VALUE, JsonNumberCoercion.truncateAndClampToInt(json("1e400")));
assertEquals(Integer.MIN_VALUE, JsonNumberCoercion.truncateAndClampToInt(json("-1e400")));
}
@Test
void handlesNonFiniteNumericNodesWithoutDecimalConversion() {
assertEquals(Integer.MAX_VALUE,
JsonNumberCoercion.truncateAndClampToInt(DoubleNode.valueOf(Double.POSITIVE_INFINITY)));
assertEquals(Integer.MIN_VALUE,
JsonNumberCoercion.truncateAndClampToInt(DoubleNode.valueOf(Double.NEGATIVE_INFINITY)));
assertEquals(0, JsonNumberCoercion.truncateAndClampToInt(DoubleNode.valueOf(Double.NaN)));
}
@Test
void rejectsNonNumericNodes() {
for (String input : List.of("\"1\"", "true", "null", "[]", "{}")) {
assertThrows(IllegalArgumentException.class,
() -> JsonNumberCoercion.truncateAndClampToInt(json(input)), input);
}
assertThrows(IllegalArgumentException.class,
() -> JsonNumberCoercion.truncateAndClampToInt(null));
}
}
@@ -0,0 +1,43 @@
package io.github.hectorvent.floci.services.lambdaweb;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.hectorvent.floci.core.common.AwsException;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
class LambdaWebJsonTest {
private static final ObjectMapper MAPPER = new ObjectMapper();
private static JsonNode json(String value) {
try {
return MAPPER.readTree(value);
} catch (Exception e) {
throw new AssertionError(e);
}
}
@Test
void integerTruncatesAndSaturatesNumericMembers() {
JsonNode request = json("{\"fraction\":-1.9,\"large\":9223372036854775808123,\"null\":null}");
assertEquals(-1, LambdaWebJson.integer(request, "fraction"));
assertEquals(Integer.MAX_VALUE, LambdaWebJson.integer(request, "large"));
assertNull(LambdaWebJson.integer(request, "null"));
assertNull(LambdaWebJson.integer(request, "missing"));
}
@Test
void integerKeepsSerializationErrorForNonNumbers() {
AwsException exception = assertThrows(AwsException.class,
() -> LambdaWebJson.integer(json("{\"count\":\"1\"}"), "count"));
assertEquals("SerializationException", exception.getErrorCode());
assertEquals(400, exception.getHttpStatus());
assertEquals("STRING_VALUE can not be converted to an Integer", exception.getMessage());
}
}