Rule implements JsonObjectConvertible { @NotNull public static JsonKeys keys() { return KEYS; } Rule(final RuleType type, final Pattern pattern); @NotNull static JsonKeys keys(); RuleType getType(); Pattern getPattern(); boolean isInclude(); boolean isExclude(); boolean isAllow(); boolean isDeny(); boolean matches(String value); @Override JsonObject toJson(); @Override String toString(); @Override boolean equals(final Object o); @Override int hashCode();  }
@Test public void testFromJsonArray() { final Rule allowsDigits = Rules.fromJson(key(Rule.keys().type(), "ALLOW") .key(Rule.keys().pattern(), "[0-9]*").get()); final Rule allowsLetters = Rules.fromJson(key(Rule.keys().type(), "ALLOW") .key(Rule.keys().pattern(), "[a-z]*").get()); final List<Rule> expectRules = Arrays.asList(allowsDigits, allowsLetters); assertEquals("exect same rules", expectRules, Rules.fromJsonArray(arr() .val(key(Rule.keys().type(), "allow").key(Rule.keys().pattern(), "[0-9]*")) .val(key(Rule.keys().type(), "allow").key(Rule.keys().pattern(), "[a-z]*")) .get())); }
@Test public void testLastMatch() { final Rule allowsDigits = Rules.fromJson(key(Rule.keys().type(), "ALLOW") .key(Rule.keys().pattern(), "[0-9]*").get()); final Rule deniesLetters = Rules.fromJson(key(Rule.keys().type(), "DENY") .key(Rule.keys().pattern(), "[a-z]*").get()); final List<Rule> expectRules = Arrays.asList(allowsDigits, deniesLetters); final Rule adHocDefault = new Rule(RuleType.ALLOW, Pattern.compile("\\.\\.")); final Rule matched0 = Rules.lastMatch(expectRules, "...", rules -> adHocDefault); assertSame("expect specific default", adHocDefault, matched0); final Rule matched1 = Rules.lastMatch(expectRules, "...", null); assertSame("expect globbal default", Rules.DEFAULT_INCLUDE, matched1); final Rule matchedDigits = Rules.lastMatch(expectRules, "123"); assertSame("expect digits rule", allowsDigits, matchedDigits); final Rule matchedAlpha = Rules.lastMatch(expectRules, "abc"); assertSame("expect alpha rule", deniesLetters, matchedAlpha); }