mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(appsync): Phase 3 — $util runtime library for VTL resolvers (#1223)
* test(appsync): add remaining SDK tests and fix listDomainNames field name
- Add 4 SDK tests: getIntrospectionSchema, listDomainNames, getFunction, listResolversByFunction
- Fix listDomainNames response field from 'domainNames' to 'domainNameConfigs' (AWS SDK v2 expects domainNameConfigs)
- Update integration test assertion to match corrected field name
* feat(appsync): add $util runtime library for VTL resolvers
Standalone utility library matching AWS AppSync $util namespace.
Validated against official AWS AppSync documentation.
Classes:
- VtlErrorSignal: exception for $util.error() and $util.unauthorized()
- AppSyncUtil: top-level $util with error/validate/type-check methods
- StrUtil: $util.str (toUpper, toLower, toReplace, normalize)
- TimeUtil: $util.time (12 methods, epochMilliSeconds-based per AWS)
- MathUtil: $util.math (roundNum, minVal, maxVal, randomDouble, randomWithinRange)
- DynamoDbUtil: $util.dynamodb (24 methods: value + JSON variants)
- TransformUtil: $util.transform (DynamoDB filter, Elasticsearch DSL, subscription filter)
- ListUtil: $util.list (copyAndRetainAll, copyAndRemoveAll, sortList)
- MapUtil: $util.map (copyAndRetainAllKeys, copyAndRemoveAllKeys)
* test(appsync): add unit tests for $util runtime library
234 test cases covering all $util methods with edge cases:
- AppSyncUtilTest: root methods, error, validate, type checks (85 tests)
- DynamoDbUtilTest: DynamoDB attribute value conversion (47 tests)
- StrUtilTest: string operations (21 tests)
- TimeUtilTest: time operations (21 tests)
- MathUtilTest: math operations (19 tests)
- ListUtilTest: list operations (17 tests)
- TransformUtilTest: filter/DSL conversion (12 tests)
- MapUtilTest: map operations (12 tests)
* fix(appsync): fix DynamoDbUtil *Json methods and refactor utils
- Fix 10 *Json methods to return JSON String instead of Map
- Add private helpers: toJson(Map), base64Encode(String) to DynamoDbUtil
- Extract parseInput(Object) helper in TransformUtil to remove duplication
- Chain qr() to quiet() in AppSyncUtil
- Chain validate() overloads in AppSyncUtil
- Update tests to verify *Json methods return correct JSON strings
* fix(appsync): address review feedback on $util methods
- toMapValues: return inner map without M wrapper (matches AWS: toMapValues = toMap.get("M"))
- toBinary/toBinarySet: wrap input as-is, don't re-encode base64
- str.normalize: use Unicode normalization (NFC/NFD/NFKC/NFKD) instead of whitespace collapse
- typeOf: return exact AWS values (Null/Number/String/Map/List/Boolean/Object)
- unauthorized: errorType 'Unauthorized' not 'UnauthorizedException'
- nowISO8601: always include 3 fractional digits per AWS format
- Mark transform.* methods as not-yet-implemented (complex expression building)
- Update all affected tests
* fix(appsync): change default errorType from UnknownError to Unknown
Research confirms AWS AppSync uses 'Unknown' as default errorType when
$util.error(message) is called without errorType argument.
* chore(appsync): remove dead base64Encode helper from DynamoDbUtil
This commit is contained in:
1 parent
61c3a07199
commit
e20f7d83d2
20 files changed
+2402
-2
No files matched your search
+1
-1
@@ -1653,7 +1653,7 @@ class AppSyncIntegrationTest {
|
||||
.get("/v1/domainnames")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("domainNames", hasSize(greaterThanOrEqualTo(1)));
|
||||
.body("domainNameConfigs", hasSize(greaterThanOrEqualTo(1)));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+502
@@ -0,0 +1,502 @@
|
||||
package io.github.hectorvent.floci.services.appsync.graphql.util;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class AppSyncUtilTest {
|
||||
|
||||
private final AppSyncUtil util = new AppSyncUtil(new com.fasterxml.jackson.databind.ObjectMapper());
|
||||
|
||||
@Test
|
||||
void escapeJavaScript_null() {
|
||||
assertThat(util.escapeJavaScript(null), is(""));
|
||||
}
|
||||
|
||||
@Test
|
||||
void escapeJavaScript_empty() {
|
||||
assertThat(util.escapeJavaScript(""), is(""));
|
||||
}
|
||||
|
||||
@Test
|
||||
void escapeJavaScript_plain() {
|
||||
assertThat(util.escapeJavaScript("hello"), is("hello"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void escapeJavaScript_quotes() {
|
||||
assertThat(util.escapeJavaScript("a\"b"), is("a\\\"b"));
|
||||
assertThat(util.escapeJavaScript("a'b"), is("a\\'b"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void escapeJavaScript_backslash() {
|
||||
assertThat(util.escapeJavaScript("a\\b"), is("a\\\\b"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void escapeJavaScript_controlChars() {
|
||||
assertThat(util.escapeJavaScript("a\tb"), is("a\\tb"));
|
||||
assertThat(util.escapeJavaScript("a\nb"), is("a\\nb"));
|
||||
assertThat(util.escapeJavaScript("a\rb"), is("a\\rb"));
|
||||
assertThat(util.escapeJavaScript("a\bb"), is("a\\bb"));
|
||||
assertThat(util.escapeJavaScript("a\fb"), is("a\\fb"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void escapeJavaScript_unicode() {
|
||||
assertThat(util.escapeJavaScript("caf\u00e9"), containsString("\\u"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void escapeJavaScript_slash() {
|
||||
assertThat(util.escapeJavaScript("a/b"), is("a\\/b"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void urlEncode_null() {
|
||||
assertThat(util.urlEncode(null), is(""));
|
||||
}
|
||||
|
||||
@Test
|
||||
void urlEncode_empty() {
|
||||
assertThat(util.urlEncode(""), is(""));
|
||||
}
|
||||
|
||||
@Test
|
||||
void urlEncode_spaces() {
|
||||
assertThat(util.urlEncode("hello world"), is("hello+world"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void urlEncode_specialChars() {
|
||||
assertThat(util.urlEncode("a&b=c"), is("a%26b%3Dc"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void urlDecode_null() {
|
||||
assertThat(util.urlDecode(null), is(""));
|
||||
}
|
||||
|
||||
@Test
|
||||
void urlDecode_empty() {
|
||||
assertThat(util.urlDecode(""), is(""));
|
||||
}
|
||||
|
||||
@Test
|
||||
void urlDecode_spaces() {
|
||||
assertThat(util.urlDecode("hello+world"), is("hello world"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void urlDecode_percentEncoded() {
|
||||
assertThat(util.urlDecode("a%26b"), is("a&b"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void urlEncode_roundtrip() {
|
||||
String original = "hello world! @#$%";
|
||||
assertThat(util.urlDecode(util.urlEncode(original)), is(original));
|
||||
}
|
||||
|
||||
@Test
|
||||
void base64Encode_null() {
|
||||
assertThat(util.base64Encode(null), is(""));
|
||||
}
|
||||
|
||||
@Test
|
||||
void base64Encode_hello() {
|
||||
assertThat(util.base64Encode("hello".getBytes(StandardCharsets.UTF_8)), is("aGVsbG8="));
|
||||
}
|
||||
|
||||
@Test
|
||||
void base64Decode_null() {
|
||||
assertThat(util.base64Decode(null), is(""));
|
||||
}
|
||||
|
||||
@Test
|
||||
void base64Decode_hello() {
|
||||
assertThat(util.base64Decode("aGVsbG8="), is("hello"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void base64_roundtrip() {
|
||||
String original = "test string with unicode: \u00e9";
|
||||
assertThat(util.base64Decode(util.base64Encode(original.getBytes(StandardCharsets.UTF_8))), is(original));
|
||||
}
|
||||
|
||||
@Test
|
||||
void parseJson_null() {
|
||||
Object result = util.parseJson(null);
|
||||
assertThat(result, instanceOf(Map.class));
|
||||
assertThat(((Map<?, ?>) result).isEmpty(), is(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
void parseJson_empty() {
|
||||
Object result = util.parseJson("");
|
||||
assertThat(result, instanceOf(Map.class));
|
||||
assertThat(((Map<?, ?>) result).isEmpty(), is(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
void parseJson_invalid() {
|
||||
Object result = util.parseJson("not json");
|
||||
assertThat(result, instanceOf(Map.class));
|
||||
assertThat(((Map<?, ?>) result).isEmpty(), is(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
void parseJson_object() {
|
||||
Object result = util.parseJson("{\"a\":1,\"b\":\"hello\"}");
|
||||
assertThat(result, instanceOf(Map.class));
|
||||
Map<?, ?> map = (Map<?, ?>) result;
|
||||
assertThat(map.get("a"), is(1));
|
||||
assertThat(map.get("b"), is("hello"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void parseJson_array() {
|
||||
Object result = util.parseJson("[1,2,3]");
|
||||
assertThat(result, instanceOf(List.class));
|
||||
List<?> list = (List<?>) result;
|
||||
assertThat(list, hasSize(3));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toJson_null() {
|
||||
assertThat(util.toJson(null), is("null"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toJson_string() {
|
||||
assertThat(util.toJson("hello"), is("\"hello\""));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toJson_map() {
|
||||
String json = util.toJson(Map.of("a", 1));
|
||||
assertThat(json, containsString("\"a\""));
|
||||
assertThat(json, containsString("1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toJson_list() {
|
||||
String json = util.toJson(List.of(1, 2, 3));
|
||||
assertThat(json, is("[1,2,3]"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void autoId_format() {
|
||||
String id = util.autoId();
|
||||
assertThat(id, matchesPattern("[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void autoId_uniqueness() {
|
||||
String id1 = util.autoId();
|
||||
String id2 = util.autoId();
|
||||
assertThat(id1, is(not(id2)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void quiet_any() {
|
||||
assertThat(util.quiet("hello"), is(""));
|
||||
assertThat(util.quiet(null), is(""));
|
||||
assertThat(util.quiet(""), is(""));
|
||||
}
|
||||
|
||||
@Test
|
||||
void qr_any() {
|
||||
assertThat(util.qr("hello"), is(""));
|
||||
assertThat(util.qr(null), is(""));
|
||||
assertThat(util.qr(""), is(""));
|
||||
}
|
||||
|
||||
@Test
|
||||
void matches_match() {
|
||||
assertThat(util.matches("[a-z]+", "hello"), is(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
void matches_noMatch() {
|
||||
assertThat(util.matches("[a-z]+", "HELLO"), is(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
void matches_nullPattern() {
|
||||
assertThat(util.matches(null, "hello"), is(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
void matches_nullValue() {
|
||||
assertThat(util.matches("[a-z]+", null), is(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
void defaultIfNull_null() {
|
||||
assertThat(util.defaultIfNull(null, "default"), is("default"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void defaultIfNull_nonNull() {
|
||||
assertThat(util.defaultIfNull("value", "default"), is("value"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void defaultIfNull_bothNull() {
|
||||
assertThat(util.defaultIfNull(null, null), is(nullValue()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void defaultIfNullOrEmpty_null() {
|
||||
assertThat(util.defaultIfNullOrEmpty(null, "default"), is("default"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void defaultIfNullOrEmpty_empty() {
|
||||
assertThat(util.defaultIfNullOrEmpty("", "default"), is("default"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void defaultIfNullOrEmpty_nonEmpty() {
|
||||
assertThat(util.defaultIfNullOrEmpty("value", "default"), is("value"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void defaultIfNullOrEmpty_whitespace() {
|
||||
assertThat(util.defaultIfNullOrEmpty(" ", "default"), is(" "));
|
||||
}
|
||||
|
||||
@Test
|
||||
void defaultIfNullOrBlank_null() {
|
||||
assertThat(util.defaultIfNullOrBlank(null, "default"), is("default"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void defaultIfNullOrBlank_empty() {
|
||||
assertThat(util.defaultIfNullOrBlank("", "default"), is("default"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void defaultIfNullOrBlank_whitespace() {
|
||||
assertThat(util.defaultIfNullOrBlank(" ", "default"), is("default"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void defaultIfNullOrBlank_nonBlank() {
|
||||
assertThat(util.defaultIfNullOrBlank("value", "default"), is("value"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void isNull_null() {
|
||||
assertThat(util.isNull(null), is(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
void isNull_nonNull() {
|
||||
assertThat(util.isNull("value"), is(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
void isNullOrEmpty_null() {
|
||||
assertThat(util.isNullOrEmpty(null), is(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
void isNullOrEmpty_empty() {
|
||||
assertThat(util.isNullOrEmpty(""), is(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
void isNullOrEmpty_nonEmpty() {
|
||||
assertThat(util.isNullOrEmpty("a"), is(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
void isNullOrBlank_null() {
|
||||
assertThat(util.isNullOrBlank(null), is(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
void isNullOrBlank_empty() {
|
||||
assertThat(util.isNullOrBlank(""), is(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
void isNullOrBlank_whitespace() {
|
||||
assertThat(util.isNullOrBlank(" "), is(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
void isNullOrBlank_nonBlank() {
|
||||
assertThat(util.isNullOrBlank("a"), is(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
void isString_string() {
|
||||
assertThat(util.isString("hello"), is(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
void isString_nonString() {
|
||||
assertThat(util.isString(123), is(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
void isNumber_number() {
|
||||
assertThat(util.isNumber(123), is(true));
|
||||
assertThat(util.isNumber(1.5), is(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
void isNumber_nonNumber() {
|
||||
assertThat(util.isNumber("hello"), is(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
void isBoolean_boolean() {
|
||||
assertThat(util.isBoolean(true), is(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
void isBoolean_nonBoolean() {
|
||||
assertThat(util.isBoolean("true"), is(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
void isList_list() {
|
||||
assertThat(util.isList(List.of()), is(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
void isList_nonList() {
|
||||
assertThat(util.isList("[]"), is(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
void isMap_map() {
|
||||
assertThat(util.isMap(Map.of()), is(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
void isMap_nonMap() {
|
||||
assertThat(util.isMap("{}"), is(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
void typeOf_null() {
|
||||
assertThat(util.typeOf(null), is("Null"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void typeOf_string() {
|
||||
assertThat(util.typeOf("hello"), is("String"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void typeOf_number() {
|
||||
assertThat(util.typeOf(42), is("Number"));
|
||||
assertThat(util.typeOf(3.14), is("Number"));
|
||||
assertThat(util.typeOf(123L), is("Number"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void typeOf_boolean() {
|
||||
assertThat(util.typeOf(true), is("Boolean"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void typeOf_list() {
|
||||
assertThat(util.typeOf(List.of()), is("List"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void typeOf_map() {
|
||||
assertThat(util.typeOf(Map.of()), is("Map"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void typeOf_unknown() {
|
||||
assertThat(util.typeOf(new Object()), is("Object"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void error_singleArg() {
|
||||
VtlErrorSignal ex = assertThrows(VtlErrorSignal.class, () -> util.error("msg"));
|
||||
assertThat(ex.getMessage(), is("msg"));
|
||||
assertThat(ex.getErrorType(), is("Unknown"));
|
||||
assertThat(ex.getData(), is(nullValue()));
|
||||
assertThat(ex.getErrorInfo(), is(nullValue()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void error_twoArgs() {
|
||||
VtlErrorSignal ex = assertThrows(VtlErrorSignal.class, () -> util.error("msg", "Custom"));
|
||||
assertThat(ex.getMessage(), is("msg"));
|
||||
assertThat(ex.getErrorType(), is("Custom"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void error_threeArgs() {
|
||||
Object data = Map.of("key", "value");
|
||||
VtlErrorSignal ex = assertThrows(VtlErrorSignal.class, () -> util.error("msg", "Type", data));
|
||||
assertThat(ex.getData(), is(data));
|
||||
assertThat(ex.getErrorInfo(), is(nullValue()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void error_fourArgs() {
|
||||
Object data = Map.of("key", "value");
|
||||
Object info = Map.of("info", "detail");
|
||||
VtlErrorSignal ex = assertThrows(VtlErrorSignal.class, () -> util.error("msg", "Type", data, info));
|
||||
assertThat(ex.getData(), is(data));
|
||||
assertThat(ex.getErrorInfo(), is(info));
|
||||
}
|
||||
|
||||
@Test
|
||||
void unauthorized() {
|
||||
VtlErrorSignal ex = assertThrows(VtlErrorSignal.class, util::unauthorized);
|
||||
assertThat(ex.getMessage(), is("Not Authorized"));
|
||||
assertThat(ex.getErrorType(), is("Unauthorized"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void validate_true() {
|
||||
assertDoesNotThrow(() -> util.validate(true, "msg"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void validate_false() {
|
||||
VtlErrorSignal ex = assertThrows(VtlErrorSignal.class, () -> util.validate(false, "msg"));
|
||||
assertThat(ex.getMessage(), is("msg"));
|
||||
assertThat(ex.getErrorType(), is("Unknown"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void validate_false_withType() {
|
||||
VtlErrorSignal ex = assertThrows(VtlErrorSignal.class, () -> util.validate(false, "msg", "Custom"));
|
||||
assertThat(ex.getErrorType(), is("Custom"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void validate_false_withData() {
|
||||
Object data = Map.of("k", "v");
|
||||
VtlErrorSignal ex = assertThrows(VtlErrorSignal.class, () -> util.validate(false, "msg", "Type", data));
|
||||
assertThat(ex.getData(), is(data));
|
||||
}
|
||||
|
||||
@Test
|
||||
void appendError_noException() {
|
||||
assertDoesNotThrow(() -> util.appendError("msg"));
|
||||
assertDoesNotThrow(() -> util.appendError("msg", "Type"));
|
||||
assertDoesNotThrow(() -> util.appendError("msg", "Type", Map.of()));
|
||||
assertDoesNotThrow(() -> util.appendError("msg", "Type", Map.of(), Map.of()));
|
||||
}
|
||||
}
|
||||
+339
@@ -0,0 +1,339 @@
|
||||
package io.github.hectorvent.floci.services.appsync.graphql.util;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class DynamoDbUtilTest {
|
||||
|
||||
private final DynamoDbUtil dynamoDb = new DynamoDbUtil(new ObjectMapper());
|
||||
|
||||
@Test
|
||||
void toDynamoDB_string() {
|
||||
Map<String, Object> result = dynamoDb.toDynamoDB("hello");
|
||||
assertThat(result, hasEntry("S", "hello"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toDynamoDB_number() {
|
||||
Map<String, Object> result = dynamoDb.toDynamoDB(42);
|
||||
assertThat(result, hasEntry("N", "42"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toDynamoDB_long() {
|
||||
Map<String, Object> result = dynamoDb.toDynamoDB(123456789L);
|
||||
assertThat(result, hasEntry("N", "123456789"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toDynamoDB_double() {
|
||||
Map<String, Object> result = dynamoDb.toDynamoDB(3.14);
|
||||
assertThat(result, hasEntry("N", "3.14"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toDynamoDB_boolean_true() {
|
||||
Map<String, Object> result = dynamoDb.toDynamoDB(true);
|
||||
assertThat(result, hasEntry("BOOL", true));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toDynamoDB_boolean_false() {
|
||||
Map<String, Object> result = dynamoDb.toDynamoDB(false);
|
||||
assertThat(result, hasEntry("BOOL", false));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toDynamoDB_null() {
|
||||
Map<String, Object> result = dynamoDb.toDynamoDB(null);
|
||||
assertThat(result, hasEntry("NULL", true));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toDynamoDB_list() {
|
||||
Map<String, Object> result = dynamoDb.toDynamoDB(List.of("a", "b"));
|
||||
assertThat(result, hasKey("L"));
|
||||
List<?> l = (List<?>) result.get("L");
|
||||
assertThat(l, hasSize(2));
|
||||
assertThat((Map<?, ?>) l.get(0), hasEntry("S", "a"));
|
||||
assertThat((Map<?, ?>) l.get(1), hasEntry("S", "b"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toDynamoDB_map() {
|
||||
Map<String, Object> result = dynamoDb.toDynamoDB(Map.of("key", "value"));
|
||||
assertThat(result, hasKey("M"));
|
||||
Map<?, ?> m = (Map<?, ?>) result.get("M");
|
||||
assertThat((Map<?, ?>) m.get("key"), hasEntry("S", "value"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toDynamoDB_nestedMap() {
|
||||
Map<String, Object> inner = Map.of("b", "c");
|
||||
Map<String, Object> outer = Map.of("a", inner);
|
||||
Map<String, Object> result = dynamoDb.toDynamoDB(outer);
|
||||
assertThat(result, hasKey("M"));
|
||||
Map<?, ?> m = (Map<?, ?>) result.get("M");
|
||||
assertThat(m, hasKey("a"));
|
||||
Map<?, ?> innerResult = (Map<?, ?>) ((Map<?, ?>) m.get("a")).get("M");
|
||||
Map<?, ?> bResult = (Map<?, ?>) innerResult.get("b");
|
||||
assertThat((String) bResult.get("S"), is("c"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toDynamoDB_listOfMaps() {
|
||||
List<Map<String, Object>> list = List.of(Map.of("k", "v"));
|
||||
Map<String, Object> result = dynamoDb.toDynamoDB(list);
|
||||
assertThat(result, hasKey("L"));
|
||||
List<?> l = (List<?>) result.get("L");
|
||||
assertThat(l, hasSize(1));
|
||||
assertThat((Map<?, ?>) ((Map<?, ?>) l.get(0)).get("M"), hasKey("k"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toDynamoDB_unknownType() {
|
||||
Map<String, Object> result = dynamoDb.toDynamoDB(new Object());
|
||||
assertThat((String) result.get("S"), startsWith("java.lang.Object"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toString_basic() {
|
||||
Map<String, Object> result = dynamoDb.toString("hello");
|
||||
assertThat(result, hasEntry("S", "hello"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toString_empty() {
|
||||
Map<String, Object> result = dynamoDb.toString("");
|
||||
assertThat(result, hasEntry("S", ""));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toString_null() {
|
||||
Map<String, Object> result = dynamoDb.toString(null);
|
||||
assertThat(result, hasKey("S"));
|
||||
assertThat(result.get("S"), is(nullValue()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toNumber_int() {
|
||||
Map<String, Object> result = dynamoDb.toNumber(42);
|
||||
assertThat(result, hasEntry("N", "42"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toNumber_long() {
|
||||
Map<String, Object> result = dynamoDb.toNumber(123456789L);
|
||||
assertThat(result, hasEntry("N", "123456789"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toNumber_double() {
|
||||
Map<String, Object> result = dynamoDb.toNumber(3.14);
|
||||
assertThat(result, hasEntry("N", "3.14"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toBoolean_true() {
|
||||
Map<String, Object> result = dynamoDb.toBoolean(true);
|
||||
assertThat(result, hasEntry("BOOL", true));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toBoolean_false() {
|
||||
Map<String, Object> result = dynamoDb.toBoolean(false);
|
||||
assertThat(result, hasEntry("BOOL", false));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toNull() {
|
||||
Map<String, Object> result = dynamoDb.toNull();
|
||||
assertThat(result, hasEntry("NULL", true));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toList_strings() {
|
||||
Map<String, Object> result = dynamoDb.toList(List.of("a", "b"));
|
||||
assertThat(result, hasKey("L"));
|
||||
List<?> l = (List<?>) result.get("L");
|
||||
assertThat(l, hasSize(2));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toList_numbers() {
|
||||
Map<String, Object> result = dynamoDb.toList(List.of(1, 2, 3));
|
||||
assertThat(result, hasKey("L"));
|
||||
List<?> l = (List<?>) result.get("L");
|
||||
assertThat(l, hasSize(3));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toList_empty() {
|
||||
Map<String, Object> result = dynamoDb.toList(List.of());
|
||||
assertThat(result, hasKey("L"));
|
||||
List<?> l = (List<?>) result.get("L");
|
||||
assertThat(l, empty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void toMap_flat() {
|
||||
Map<String, Object> result = dynamoDb.toMap(Map.of("k", "v"));
|
||||
assertThat(result, hasKey("M"));
|
||||
Map<?, ?> m = (Map<?, ?>) result.get("M");
|
||||
assertThat((String) ((Map<?, ?>) m.get("k")).get("S"), is("v"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toMap_empty() {
|
||||
Map<String, Object> result = dynamoDb.toMap(Map.of());
|
||||
assertThat(result, hasKey("M"));
|
||||
Map<?, ?> m = (Map<?, ?>) result.get("M");
|
||||
assertThat(m, anEmptyMap());
|
||||
}
|
||||
|
||||
@Test
|
||||
void toStringSet() {
|
||||
Map<String, Object> result = dynamoDb.toStringSet(List.of("a", "b"));
|
||||
assertThat(result, hasEntry("SS", List.of("a", "b")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toStringSet_empty() {
|
||||
Map<String, Object> result = dynamoDb.toStringSet(List.of());
|
||||
assertThat(result, hasEntry("SS", List.of()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toNumberSet() {
|
||||
Map<String, Object> result = dynamoDb.toNumberSet(List.of(1, 2, 3));
|
||||
assertThat(result, hasKey("NS"));
|
||||
List<?> ns = (List<?>) result.get("NS");
|
||||
assertThat(ns, hasSize(3));
|
||||
assertThat(ns, contains("1", "2", "3"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toNumberSet_empty() {
|
||||
Map<String, Object> result = dynamoDb.toNumberSet(List.of());
|
||||
assertThat(result, hasEntry("NS", List.of()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toBinary() {
|
||||
Map<String, Object> result = dynamoDb.toBinary("hello");
|
||||
assertThat(result, hasKey("B"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toBinary_empty() {
|
||||
Map<String, Object> result = dynamoDb.toBinary("");
|
||||
assertThat(result, hasKey("B"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toBinarySet() {
|
||||
Map<String, Object> result = dynamoDb.toBinarySet(List.of("a", "b"));
|
||||
assertThat(result, hasKey("BS"));
|
||||
List<?> bs = (List<?>) result.get("BS");
|
||||
assertThat(bs, hasSize(2));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toBinarySet_empty() {
|
||||
Map<String, Object> result = dynamoDb.toBinarySet(List.of());
|
||||
assertThat(result, hasEntry("BS", List.of()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toDynamoDBJson_valid() {
|
||||
String json = dynamoDb.toDynamoDBJson("hello");
|
||||
assertThat(json, containsString("\"S\""));
|
||||
assertThat(json, containsString("hello"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toDynamoDBJson_map() {
|
||||
String json = dynamoDb.toDynamoDBJson(Map.of("key", "value"));
|
||||
assertThat(json, containsString("\"M\""));
|
||||
assertThat(json, containsString("key"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toStringJson() {
|
||||
String result = dynamoDb.toStringJson("hello");
|
||||
assertThat(result, containsString("\"S\""));
|
||||
assertThat(result, containsString("hello"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toNumberJson() {
|
||||
String result = dynamoDb.toNumberJson(42);
|
||||
assertThat(result, containsString("\"N\""));
|
||||
assertThat(result, containsString("42"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toBooleanJson() {
|
||||
String result = dynamoDb.toBooleanJson(true);
|
||||
assertThat(result, containsString("\"BOOL\""));
|
||||
assertThat(result, containsString("true"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toNullJson() {
|
||||
String result = dynamoDb.toNullJson();
|
||||
assertThat(result, containsString("\"NULL\""));
|
||||
assertThat(result, containsString("true"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toListJson() {
|
||||
String result = dynamoDb.toListJson(List.of("a"));
|
||||
assertThat(result, containsString("\"L\""));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toMapJson() {
|
||||
String result = dynamoDb.toMapJson(Map.of("k", "v"));
|
||||
assertThat(result, containsString("\"M\""));
|
||||
assertThat(result, containsString("k"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toMapValues() {
|
||||
Map<String, Object> result = dynamoDb.toMapValues(Map.of("k", "v"));
|
||||
assertThat(result, hasKey("k"));
|
||||
assertThat((Map<?, ?>) result.get("k"), hasEntry("S", "v"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toMapValuesJson() {
|
||||
String result = dynamoDb.toMapValuesJson(Map.of("k", "v"));
|
||||
assertThat(result, containsString("k"));
|
||||
assertThat(result, containsString("\"S\""));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toStringSetJson() {
|
||||
String result = dynamoDb.toStringSetJson(List.of("a"));
|
||||
assertThat(result, containsString("\"SS\""));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toNumberSetJson() {
|
||||
String result = dynamoDb.toNumberSetJson(List.of(1));
|
||||
assertThat(result, containsString("\"NS\""));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toBinarySetJson() {
|
||||
String result = dynamoDb.toBinarySetJson(List.of("a"));
|
||||
assertThat(result, containsString("\"BS\""));
|
||||
}
|
||||
}
|
||||
+127
@@ -0,0 +1,127 @@
|
||||
package io.github.hectorvent.floci.services.appsync.graphql.util;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class ListUtilTest {
|
||||
|
||||
private final ListUtil listUtil = new ListUtil();
|
||||
|
||||
@Test
|
||||
void copyAndRetainAll_basic() {
|
||||
List<Object> result = listUtil.copyAndRetainAll(List.of(1, 2, 3), List.of(2, 3, 4));
|
||||
assertThat(result, containsInAnyOrder(2, 3));
|
||||
}
|
||||
|
||||
@Test
|
||||
void copyAndRetainAll_noOverlap() {
|
||||
List<Object> result = listUtil.copyAndRetainAll(List.of(1, 2, 3), List.of(4, 5));
|
||||
assertThat(result, empty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void copyAndRetainAll_allRetained() {
|
||||
List<Object> result = listUtil.copyAndRetainAll(List.of(1, 2, 3), List.of(1, 2, 3));
|
||||
assertThat(result, containsInAnyOrder(1, 2, 3));
|
||||
}
|
||||
|
||||
@Test
|
||||
void copyAndRetainAll_emptyKeep() {
|
||||
List<Object> result = listUtil.copyAndRetainAll(List.of(1, 2, 3), List.of());
|
||||
assertThat(result, empty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void copyAndRetainAll_nullList() {
|
||||
List<Object> result = listUtil.copyAndRetainAll(null, List.of(1));
|
||||
assertThat(result, empty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void copyAndRetainAll_nullKeep() {
|
||||
List<Object> result = listUtil.copyAndRetainAll(List.of(1, 2, 3), null);
|
||||
assertThat(result, containsInAnyOrder(1, 2, 3));
|
||||
}
|
||||
|
||||
@Test
|
||||
void copyAndRemoveAll_basic() {
|
||||
List<Object> result = listUtil.copyAndRemoveAll(List.of(1, 2, 3), List.of(2));
|
||||
assertThat(result, containsInAnyOrder(1, 3));
|
||||
}
|
||||
|
||||
@Test
|
||||
void copyAndRemoveAll_noOverlap() {
|
||||
List<Object> result = listUtil.copyAndRemoveAll(List.of(1, 2, 3), List.of(4, 5));
|
||||
assertThat(result, containsInAnyOrder(1, 2, 3));
|
||||
}
|
||||
|
||||
@Test
|
||||
void copyAndRemoveAll_allRemoved() {
|
||||
List<Object> result = listUtil.copyAndRemoveAll(List.of(1, 2, 3), List.of(1, 2, 3));
|
||||
assertThat(result, empty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void copyAndRemoveAll_emptyRemove() {
|
||||
List<Object> result = listUtil.copyAndRemoveAll(List.of(1, 2, 3), List.of());
|
||||
assertThat(result, containsInAnyOrder(1, 2, 3));
|
||||
}
|
||||
|
||||
@Test
|
||||
void copyAndRemoveAll_nullList() {
|
||||
List<Object> result = listUtil.copyAndRemoveAll(null, List.of(1));
|
||||
assertThat(result, empty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void copyAndRemoveAll_nullRemove() {
|
||||
List<Object> result = listUtil.copyAndRemoveAll(List.of(1, 2, 3), null);
|
||||
assertThat(result, containsInAnyOrder(1, 2, 3));
|
||||
}
|
||||
|
||||
@Test
|
||||
void sortList_basic() {
|
||||
List<Object> result = listUtil.sortList(List.of(3, 1, 2), false, null);
|
||||
assertThat(result, contains(1, 2, 3));
|
||||
}
|
||||
|
||||
@Test
|
||||
void sortList_descending() {
|
||||
List<Object> result = listUtil.sortList(List.of(3, 1, 2), true, null);
|
||||
assertThat(result, contains(3, 2, 1));
|
||||
}
|
||||
|
||||
@Test
|
||||
void sortList_withFieldName() {
|
||||
var map1 = new java.util.HashMap<String, Object>();
|
||||
map1.put("name", "Charlie");
|
||||
var map2 = new java.util.HashMap<String, Object>();
|
||||
map2.put("name", "Alice");
|
||||
var map3 = new java.util.HashMap<String, Object>();
|
||||
map3.put("name", "Bob");
|
||||
List<Object> input = List.of(map1, map2, map3);
|
||||
List<Object> result = listUtil.sortList(input, false, "name");
|
||||
assertThat(((Map<?, ?>) result.get(0)).get("name"), is("Alice"));
|
||||
assertThat(((Map<?, ?>) result.get(1)).get("name"), is("Bob"));
|
||||
assertThat(((Map<?, ?>) result.get(2)).get("name"), is("Charlie"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void sortList_null() {
|
||||
List<Object> result = listUtil.sortList(null, false, null);
|
||||
assertThat(result, empty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void sortList_empty() {
|
||||
List<Object> result = listUtil.sortList(List.of(), false, null);
|
||||
assertThat(result, empty());
|
||||
}
|
||||
}
|
||||
+110
@@ -0,0 +1,110 @@
|
||||
package io.github.hectorvent.floci.services.appsync.graphql.util;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class MapUtilTest {
|
||||
|
||||
private final MapUtil mapUtil = new MapUtil();
|
||||
|
||||
@Test
|
||||
void copyAndRetainAllKeys_basic() {
|
||||
Map<String, Object> map = new LinkedHashMap<>();
|
||||
map.put("a", 1);
|
||||
map.put("b", 2);
|
||||
map.put("c", 3);
|
||||
Map<String, Object> result = mapUtil.copyAndRetainAllKeys(map, List.of("a", "c"));
|
||||
assertThat(result, hasEntry("a", 1));
|
||||
assertThat(result, hasEntry("c", 3));
|
||||
assertThat(result, not(hasKey("b")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void copyAndRetainAllKeys_noOverlap() {
|
||||
Map<String, Object> map = Map.of("a", 1, "b", 2);
|
||||
Map<String, Object> result = mapUtil.copyAndRetainAllKeys(map, List.of("x", "y"));
|
||||
assertThat(result, anEmptyMap());
|
||||
}
|
||||
|
||||
@Test
|
||||
void copyAndRetainAllKeys_allRetained() {
|
||||
Map<String, Object> map = Map.of("a", 1, "b", 2);
|
||||
Map<String, Object> result = mapUtil.copyAndRetainAllKeys(map, List.of("a", "b"));
|
||||
assertThat(result, hasEntry("a", 1));
|
||||
assertThat(result, hasEntry("b", 2));
|
||||
}
|
||||
|
||||
@Test
|
||||
void copyAndRetainAllKeys_emptyKeys() {
|
||||
Map<String, Object> map = Map.of("a", 1);
|
||||
Map<String, Object> result = mapUtil.copyAndRetainAllKeys(map, List.of());
|
||||
assertThat(result, anEmptyMap());
|
||||
}
|
||||
|
||||
@Test
|
||||
void copyAndRetainAllKeys_nullMap() {
|
||||
Map<String, Object> result = mapUtil.copyAndRetainAllKeys(null, List.of("a"));
|
||||
assertThat(result, anEmptyMap());
|
||||
}
|
||||
|
||||
@Test
|
||||
void copyAndRetainAllKeys_nullKeys() {
|
||||
Map<String, Object> map = Map.of("a", 1);
|
||||
Map<String, Object> result = mapUtil.copyAndRetainAllKeys(map, null);
|
||||
assertThat(result, hasEntry("a", 1));
|
||||
}
|
||||
|
||||
@Test
|
||||
void copyAndRemoveAllKeys_basic() {
|
||||
Map<String, Object> map = new LinkedHashMap<>();
|
||||
map.put("a", 1);
|
||||
map.put("b", 2);
|
||||
map.put("c", 3);
|
||||
Map<String, Object> result = mapUtil.copyAndRemoveAllKeys(map, List.of("b"));
|
||||
assertThat(result, hasEntry("a", 1));
|
||||
assertThat(result, hasEntry("c", 3));
|
||||
assertThat(result, not(hasKey("b")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void copyAndRemoveAllKeys_noOverlap() {
|
||||
Map<String, Object> map = Map.of("a", 1, "b", 2);
|
||||
Map<String, Object> result = mapUtil.copyAndRemoveAllKeys(map, List.of("x", "y"));
|
||||
assertThat(result, hasEntry("a", 1));
|
||||
assertThat(result, hasEntry("b", 2));
|
||||
}
|
||||
|
||||
@Test
|
||||
void copyAndRemoveAllKeys_allRemoved() {
|
||||
Map<String, Object> map = Map.of("a", 1, "b", 2);
|
||||
Map<String, Object> result = mapUtil.copyAndRemoveAllKeys(map, List.of("a", "b"));
|
||||
assertThat(result, anEmptyMap());
|
||||
}
|
||||
|
||||
@Test
|
||||
void copyAndRemoveAllKeys_emptyKeys() {
|
||||
Map<String, Object> map = Map.of("a", 1);
|
||||
Map<String, Object> result = mapUtil.copyAndRemoveAllKeys(map, List.of());
|
||||
assertThat(result, hasEntry("a", 1));
|
||||
}
|
||||
|
||||
@Test
|
||||
void copyAndRemoveAllKeys_nullMap() {
|
||||
Map<String, Object> result = mapUtil.copyAndRemoveAllKeys(null, List.of("a"));
|
||||
assertThat(result, anEmptyMap());
|
||||
}
|
||||
|
||||
@Test
|
||||
void copyAndRemoveAllKeys_nullKeys() {
|
||||
Map<String, Object> map = Map.of("a", 1);
|
||||
Map<String, Object> result = mapUtil.copyAndRemoveAllKeys(map, null);
|
||||
assertThat(result, hasEntry("a", 1));
|
||||
}
|
||||
}
|
||||
+119
@@ -0,0 +1,119 @@
|
||||
package io.github.hectorvent.floci.services.appsync.graphql.util;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class MathUtilTest {
|
||||
|
||||
private final MathUtil math = new MathUtil();
|
||||
|
||||
@Test
|
||||
void roundNum_basic() {
|
||||
assertThat(math.roundNum(2.3), is(2));
|
||||
assertThat(math.roundNum(2.5), is(3));
|
||||
assertThat(math.roundNum(2.7), is(3));
|
||||
}
|
||||
|
||||
@Test
|
||||
void roundNum_negative() {
|
||||
assertThat(math.roundNum(-2.3), is(-2));
|
||||
assertThat(math.roundNum(-2.5), is(-3));
|
||||
}
|
||||
|
||||
@Test
|
||||
void roundNum_null() {
|
||||
assertThat(math.roundNum(null), is(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
void roundNum_integer() {
|
||||
assertThat(math.roundNum(3.0), is(3));
|
||||
}
|
||||
|
||||
@Test
|
||||
void minVal_basic() {
|
||||
assertThat(math.minVal(3.0, 1.0), is(1.0));
|
||||
assertThat(math.minVal(1.0, 3.0), is(1.0));
|
||||
}
|
||||
|
||||
@Test
|
||||
void minVal_equal() {
|
||||
assertThat(math.minVal(2.0, 2.0), is(2.0));
|
||||
}
|
||||
|
||||
@Test
|
||||
void minVal_negative() {
|
||||
assertThat(math.minVal(-1.0, -3.0), is(-3.0));
|
||||
}
|
||||
|
||||
@Test
|
||||
void minVal_nullFirst() {
|
||||
assertThat(math.minVal(null, 5.0), is(5.0));
|
||||
}
|
||||
|
||||
@Test
|
||||
void minVal_nullSecond() {
|
||||
assertThat(math.minVal(5.0, null), is(5.0));
|
||||
}
|
||||
|
||||
@Test
|
||||
void minVal_bothNull() {
|
||||
assertThat(math.minVal(null, null), is(0.0));
|
||||
}
|
||||
|
||||
@Test
|
||||
void maxVal_basic() {
|
||||
assertThat(math.maxVal(3.0, 1.0), is(3.0));
|
||||
assertThat(math.maxVal(1.0, 3.0), is(3.0));
|
||||
}
|
||||
|
||||
@Test
|
||||
void maxVal_equal() {
|
||||
assertThat(math.maxVal(2.0, 2.0), is(2.0));
|
||||
}
|
||||
|
||||
@Test
|
||||
void maxVal_negative() {
|
||||
assertThat(math.maxVal(-1.0, -3.0), is(-1.0));
|
||||
}
|
||||
|
||||
@Test
|
||||
void maxVal_nullFirst() {
|
||||
assertThat(math.maxVal(null, 5.0), is(5.0));
|
||||
}
|
||||
|
||||
@Test
|
||||
void maxVal_nullSecond() {
|
||||
assertThat(math.maxVal(5.0, null), is(5.0));
|
||||
}
|
||||
|
||||
@Test
|
||||
void maxVal_bothNull() {
|
||||
assertThat(math.maxVal(null, null), is(0.0));
|
||||
}
|
||||
|
||||
@Test
|
||||
void randomDouble_range() {
|
||||
double result = math.randomDouble();
|
||||
assertThat(result, greaterThanOrEqualTo(0.0));
|
||||
assertThat(result, lessThan(1.0));
|
||||
}
|
||||
|
||||
@Test
|
||||
void randomWithinRange_range() {
|
||||
for (int i = 0; i < 100; i++) {
|
||||
int result = math.randomWithinRange(5, 10);
|
||||
assertThat(result, greaterThanOrEqualTo(5));
|
||||
assertThat(result, lessThanOrEqualTo(10));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void randomWithinRange_null() {
|
||||
assertThat(math.randomWithinRange(null, 10), is(0));
|
||||
assertThat(math.randomWithinRange(5, null), is(0));
|
||||
}
|
||||
}
|
||||
+131
@@ -0,0 +1,131 @@
|
||||
package io.github.hectorvent.floci.services.appsync.graphql.util;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class StrUtilTest {
|
||||
|
||||
private final StrUtil str = new StrUtil();
|
||||
|
||||
@Test
|
||||
void toUpper_basic() {
|
||||
assertThat(str.toUpper("hello"), is("HELLO"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toUpper_mixed() {
|
||||
assertThat(str.toUpper("Hello World"), is("HELLO WORLD"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toUpper_null() {
|
||||
assertThat(str.toUpper(null), is(""));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toUpper_empty() {
|
||||
assertThat(str.toUpper(""), is(""));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toLower_basic() {
|
||||
assertThat(str.toLower("HELLO"), is("hello"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toLower_mixed() {
|
||||
assertThat(str.toLower("Hello World"), is("hello world"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toLower_null() {
|
||||
assertThat(str.toLower(null), is(""));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toLower_empty() {
|
||||
assertThat(str.toLower(""), is(""));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toReplace_basic() {
|
||||
assertThat(str.toReplace("hello world", "world", "there"), is("hello there"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toReplace_multiple() {
|
||||
assertThat(str.toReplace("aabbcc", "bb", "xx"), is("aaxxcc"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toReplace_noMatch() {
|
||||
assertThat(str.toReplace("hello", "xyz", "abc"), is("hello"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toReplace_nullString() {
|
||||
assertThat(str.toReplace(null, "a", "b"), is(""));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toReplace_nullTarget() {
|
||||
assertThat(str.toReplace("hello", null, "b"), is("hello"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toReplace_nullReplacement() {
|
||||
assertThat(str.toReplace("hello", "l", null), is("hello"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void normalize_nfc() {
|
||||
// e + combining acute accent → precomposed é
|
||||
String input = "e\u0301";
|
||||
assertThat(str.normalize(input, "nfc"), is("\u00e9"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void normalize_nfd() {
|
||||
// precomposed é → e + combining acute accent
|
||||
String input = "\u00e9";
|
||||
assertThat(str.normalize(input, "nfd"), is("e\u0301"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void normalize_nfkc() {
|
||||
// fullwidth Latin small letter e → normal e
|
||||
String input = "\uff45";
|
||||
assertThat(str.normalize(input, "nfkc"), is("e"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void normalize_nfkd() {
|
||||
// fullwidth Latin small letter e → normal e
|
||||
String input = "\uff45";
|
||||
assertThat(str.normalize(input, "nfkd"), is("e"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void normalize_caseInsensitive() {
|
||||
String input = "e\u0301";
|
||||
assertThat(str.normalize(input, "NFC"), is("\u00e9"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void normalize_null() {
|
||||
assertThat(str.normalize(null, "nfc"), is(""));
|
||||
}
|
||||
|
||||
@Test
|
||||
void normalize_nullForm() {
|
||||
assertThat(str.normalize("hello", null), is("hello"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void normalize_alreadyNormalized() {
|
||||
assertThat(str.normalize("hello", "nfc"), is("hello"));
|
||||
}
|
||||
}
|
||||
+150
@@ -0,0 +1,150 @@
|
||||
package io.github.hectorvent.floci.services.appsync.graphql.util;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneOffset;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class TimeUtilTest {
|
||||
|
||||
private final TimeUtil time = new TimeUtil();
|
||||
|
||||
@Test
|
||||
void nowISO8601_format() {
|
||||
String result = time.nowISO8601();
|
||||
assertThat(result, matchesPattern("\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\.\\d+Z"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void nowISO8601_currentDate() {
|
||||
String result = time.nowISO8601();
|
||||
String today = DateTimeFormatter.ofPattern("yyyy-MM-dd").format(ZonedDateTime.now(ZoneOffset.UTC));
|
||||
assertThat(result, startsWith(today));
|
||||
}
|
||||
|
||||
@Test
|
||||
void nowEpochSeconds_positive() {
|
||||
long result = time.nowEpochSeconds();
|
||||
assertThat(result, greaterThan(0L));
|
||||
}
|
||||
|
||||
@Test
|
||||
void nowEpochSeconds_withinRange() {
|
||||
long expected = System.currentTimeMillis() / 1000;
|
||||
long result = time.nowEpochSeconds();
|
||||
assertThat(Math.abs(result - expected), is(lessThanOrEqualTo(1L)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void nowEpochMilliSeconds_positive() {
|
||||
long result = time.nowEpochMilliSeconds();
|
||||
assertThat(result, greaterThan(0L));
|
||||
}
|
||||
|
||||
@Test
|
||||
void nowEpochMilliSeconds_withinRange() {
|
||||
long expected = System.currentTimeMillis();
|
||||
long result = time.nowEpochMilliSeconds();
|
||||
assertThat(Math.abs(result - expected), is(lessThanOrEqualTo(100L)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void nowFormatted_utc() {
|
||||
String result = time.nowFormatted("yyyy-MM-dd");
|
||||
String today = DateTimeFormatter.ofPattern("yyyy-MM-dd").format(ZonedDateTime.now(ZoneOffset.UTC));
|
||||
assertThat(result, is(today));
|
||||
}
|
||||
|
||||
@Test
|
||||
void nowFormatted_withOffset() {
|
||||
String result = time.nowFormatted("HH", "+05:30");
|
||||
assertThat(result, matchesPattern("\\d{2}"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void nowFormatted_invalidFormat() {
|
||||
assertThrows(Exception.class, () -> time.nowFormatted("INVALID"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void parseISO8601ToEpochMilliSeconds_valid() {
|
||||
long result = time.parseISO8601ToEpochMilliSeconds("2024-01-15T12:00:00Z");
|
||||
Instant instant = Instant.parse("2024-01-15T12:00:00Z");
|
||||
assertThat(result, is(instant.toEpochMilli()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void parseISO8601ToEpochMilliSeconds_epoch0() {
|
||||
long result = time.parseISO8601ToEpochMilliSeconds("1970-01-01T00:00:00Z");
|
||||
assertThat(result, is(0L));
|
||||
}
|
||||
|
||||
@Test
|
||||
void parseISO8601ToEpochMilliSeconds_invalid() {
|
||||
assertThrows(Exception.class, () -> time.parseISO8601ToEpochMilliSeconds("not-a-date"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void parseFormattedToEpochMilliSeconds_valid() {
|
||||
long result = time.parseFormattedToEpochMilliSeconds("2024-01-15 12:00:00", "yyyy-MM-dd HH:mm:ss");
|
||||
Instant expected = Instant.parse("2024-01-15T12:00:00Z");
|
||||
assertThat(result, is(expected.toEpochMilli()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void parseFormattedToEpochMilliSeconds_withTimezone() {
|
||||
long result = time.parseFormattedToEpochMilliSeconds("2024-01-15 12:00:00", "yyyy-MM-dd HH:mm:ss", "+00:00");
|
||||
Instant expected = Instant.parse("2024-01-15T12:00:00Z");
|
||||
assertThat(result, is(expected.toEpochMilli()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void parseFormattedToEpochMilliSeconds_invalid() {
|
||||
assertThrows(Exception.class, () -> time.parseFormattedToEpochMilliSeconds("invalid", "yyyy-MM-dd"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void epochMilliSecondsToISO8601_valid() {
|
||||
Instant instant = Instant.parse("2024-01-15T12:00:00Z");
|
||||
String result = time.epochMilliSecondsToISO8601(instant.toEpochMilli());
|
||||
assertThat(result, is("2024-01-15T12:00:00.000Z"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void epochMilliSecondsToISO8601_epoch0() {
|
||||
String result = time.epochMilliSecondsToISO8601(0L);
|
||||
assertThat(result, is("1970-01-01T00:00:00.000Z"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void epochMilliSecondsToSeconds_valid() {
|
||||
long result = time.epochMilliSecondsToSeconds(1705310400000L);
|
||||
assertThat(result, is(1705310400L));
|
||||
}
|
||||
|
||||
@Test
|
||||
void epochMilliSecondsToSeconds_epoch0() {
|
||||
long result = time.epochMilliSecondsToSeconds(0L);
|
||||
assertThat(result, is(0L));
|
||||
}
|
||||
|
||||
@Test
|
||||
void epochMilliSecondsToFormatted_valid() {
|
||||
Instant instant = Instant.parse("2024-01-15T12:00:00Z");
|
||||
String result = time.epochMilliSecondsToFormatted(instant.toEpochMilli(), "yyyy-MM-dd");
|
||||
assertThat(result, is("2024-01-15"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void epochMilliSecondsToFormatted_withOffset() {
|
||||
Instant instant = Instant.parse("2024-01-15T12:00:00Z");
|
||||
String result = time.epochMilliSecondsToFormatted(instant.toEpochMilli(), "yyyy-MM-dd", "+00:00");
|
||||
assertThat(result, is("2024-01-15"));
|
||||
}
|
||||
}
|
||||
+103
@@ -0,0 +1,103 @@
|
||||
package io.github.hectorvent.floci.services.appsync.graphql.util;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class TransformUtilTest {
|
||||
|
||||
private final TransformUtil transform = new TransformUtil(new ObjectMapper());
|
||||
|
||||
@Test
|
||||
void toDynamoDBFilterExpression_null() {
|
||||
Map<String, Object> result = transform.toDynamoDBFilterExpression(null);
|
||||
assertThat(result, anEmptyMap());
|
||||
}
|
||||
|
||||
@Test
|
||||
void toDynamoDBFilterExpression_simple() {
|
||||
Map<String, Object> input = new HashMap<>();
|
||||
input.put("expression", "#pk = :pk");
|
||||
input.put("expressionNames", Map.of("#pk", "id"));
|
||||
input.put("expressionValues", Map.of(":pk", Map.of("S", "123")));
|
||||
|
||||
Map<String, Object> result = transform.toDynamoDBFilterExpression(input);
|
||||
assertThat(result, hasEntry("FilterExpression", "#pk = :pk"));
|
||||
assertThat(result, hasKey("ExpressionAttributeNames"));
|
||||
assertThat(result, hasKey("ExpressionAttributeValues"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toDynamoDBFilterExpression_jsonString() {
|
||||
String json = "{\"expression\":\"#pk = :pk\"}";
|
||||
Map<String, Object> result = transform.toDynamoDBFilterExpression(json);
|
||||
assertThat(result, hasEntry("FilterExpression", "#pk = :pk"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toDynamoDBFilterExpression_emptyMap() {
|
||||
Map<String, Object> result = transform.toDynamoDBFilterExpression(Map.of());
|
||||
assertThat(result, anEmptyMap());
|
||||
}
|
||||
|
||||
@Test
|
||||
void toElasticsearchQueryDSL_null() {
|
||||
Map<String, Object> result = transform.toElasticsearchQueryDSL(null);
|
||||
assertThat(result, anEmptyMap());
|
||||
}
|
||||
|
||||
@Test
|
||||
void toElasticsearchQueryDSL_map() {
|
||||
Map<String, Object> input = Map.of("query", Map.of("match_all", Map.of()));
|
||||
Map<String, Object> result = transform.toElasticsearchQueryDSL(input);
|
||||
assertThat(result, is(input));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toElasticsearchQueryDSL_jsonString() {
|
||||
String json = "{\"query\":{\"match_all\":{}}}";
|
||||
Map<String, Object> result = transform.toElasticsearchQueryDSL(json);
|
||||
assertThat(result, hasKey("query"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toSubscriptionFilter_null() {
|
||||
Map<String, Object> result = transform.toSubscriptionFilter(null);
|
||||
assertThat(result, anEmptyMap());
|
||||
}
|
||||
|
||||
@Test
|
||||
void toSubscriptionFilter_map() {
|
||||
Map<String, Object> input = Map.of("filter", Map.of("field", "value"));
|
||||
Map<String, Object> result = transform.toSubscriptionFilter(input);
|
||||
assertThat(result, is(input));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toSubscriptionFilter_jsonString() {
|
||||
String json = "{\"filter\":{\"field\":\"value\"}}";
|
||||
Map<String, Object> result = transform.toSubscriptionFilter(json);
|
||||
assertThat(result, hasKey("filter"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toSubscriptionFilter_withFilterKeys() {
|
||||
Map<String, Object> input = Map.of("filter", Map.of("field", "value"));
|
||||
Map<String, Object> result = transform.toSubscriptionFilter(input, java.util.List.of("key1"));
|
||||
assertThat(result, hasKey("filterKeys"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toSubscriptionFilter_withHeaderOverrides() {
|
||||
Map<String, Object> input = Map.of("filter", Map.of("field", "value"));
|
||||
Map<String, Object> result = transform.toSubscriptionFilter(
|
||||
input, java.util.List.of("key1"), Map.of("header", "value"));
|
||||
assertThat(result, hasKey("headerOverrides"));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user