FiltersParser { public static Map<RequestObject, Object> parseFilters(Map<String, Object> rawFilters) { Map<RequestObject, Object> requestParameters = new HashMap<>(); rawFilters.forEach((key, entry) -> { if (isNotFieldNorLogicalOperator(key)) { throw new IllegalStateException(key + " isNotFieldNorLogicalOperator"); } if (isField(key)) { if (!(entry instanceof Map)) { throw new IllegalStateException("Bad value (" + entry + ") type for field: " + entry.getClass().toString()); } requestParameters.put(RequestField.of(key), processFieldValue((Map) entry)); } else { if (!(entry instanceof Map)) { throw new IllegalStateException("Bad value (" + entry + ") type for or operation: " + entry.getClass().toString()); } requestParameters.put(RequestLogicalOperator.of(key), processRootLogicalOperatorValue((Map) entry)); } }); LOG.info("Parsed filters: {}", requestParameters); return requestParameters; } private  FiltersParser(); static Map<RequestObject, Object> parseFilters(Map<String, Object> rawFilters);  }
@Test public void shouldProduceEmptyFilters() throws Exception { Map<String, Object> rawFilters = readFiltersFromResources("EmptyFilters.json"); Map<RequestObject, Object> filters = FiltersParser.parseFilters(rawFilters); Assertions.assertThat(filters) .hasSize(0); }
@Test(expected = IllegalStateException.class) public void shouldThrowExceptionForIncorrectRootElement() throws Exception { Map<String, Object> rawFilters = readFiltersFromResources("IncorrectFiltersStartingNeitherFromFieldOrAnOperator.json"); Map<RequestObject, Object> filters = FiltersParser.parseFilters(rawFilters); }
@Test public void testSingleFieldWithOperator() throws Exception { Map<String, Object> rawFilters = readFiltersFromResources("SingleFieldWithOperator.json"); Map<RequestObject, Object> filters = FiltersParser.parseFilters(rawFilters); Assertions.assertThat(filters) .hasSize(1) .containsOnlyKeys(RequestField.of("name")); assertThat((Map) filters.get(RequestField.of("name"))) .containsEntry(RequestOperator.of(OperatorType.EQUAL.getName()), "John"); }
@Test public void testMultipleFieldsWithOperator() throws Exception { Map<String, Object> rawFilters = readFiltersFromResources("MultipleFieldsWithOperator.json"); Map<RequestObject, Object> filters = FiltersParser.parseFilters(rawFilters); Assertions.assertThat(filters) .hasSize(2) .containsOnlyKeys(RequestField.of("name"), RequestField.of("size")); assertThat((Map) filters.get(RequestField.of("name"))) .containsEntry(RequestOperator.of(OperatorType.EQUAL.getName()), "John"); assertThat((Map) filters.get(RequestField.of("size"))) .containsEntry(RequestOperator.of(OperatorType.GREATER_THAN_EQUAL.getName()), "5"); }
@Test public void testLogicalOperator() throws Exception { Map<String, Object> rawFilters = readFiltersFromResources("LogicalOperator.json"); Map<RequestObject, Object> filters = FiltersParser.parseFilters(rawFilters); Assertions.assertThat(filters) .hasSize(1) .containsOnlyKeys(RequestLogicalOperator.of(LogicalOperatorType.OR)); assertThat((Map) filters.get(RequestLogicalOperator.of(LogicalOperatorType.OR))) .hasSize(2) .containsEntry(RequestField.of("name"), ImmutableMap.of(RequestOperator.of(OperatorType.EQUAL), "John")) .containsEntry(RequestField.of("surname"), ImmutableMap.of(RequestOperator.of(OperatorType.STARTS_WITH), "Doe")); }
@Test public void testMultipleNestedOperators() throws Exception { Map<String, Object> rawFilters = readFiltersFromResources("MultipleNestedOperators.json"); Map<RequestObject, Object> filters = FiltersParser.parseFilters(rawFilters); Assertions.assertThat(filters) .hasSize(3) .containsOnlyKeys(RequestField.of("name"), RequestField.of("lastName"), RequestLogicalOperator.of(LogicalOperatorType.OR)); assertThat((Map) filters.get(RequestField.of("name"))) .hasSize(2) .containsEntry(RequestOperator.of(OperatorType.STARTS_WITH), "John") .containsEntry(RequestOperator.of(OperatorType.IN), Lists.newArrayList("Johnny", "Abc")); assertThat((Map) filters.get(RequestField.of("lastName"))) .hasSize(1) .containsEntry(RequestLogicalOperator.of(LogicalOperatorType.OR), ImmutableMap.of( RequestOperator.of(OperatorType.STARTS_WITH), "John", RequestOperator.of(OperatorType.IN), Lists.newArrayList("Johnny", "Abc") )); assertThat((Map) filters.get(RequestLogicalOperator.of(LogicalOperatorType.OR))) .hasSize(2) .containsEntry(RequestField.of("name"), ImmutableMap.of(RequestOperator.of(OperatorType.EQUAL), "John")) .containsEntry(RequestField.of("last"), ImmutableMap.of(RequestOperator.of(OperatorType.EQUAL), "Doe")); }
@Test public void testRegexOperator() throws Exception { Map<String, Object> rawFilters = readFiltersFromResources("RegexOperator.json"); Map<RequestObject, Object> filters = FiltersParser.parseFilters(rawFilters); assertThat((Map) filters.get(RequestField.of("name"))) .hasSize(1) .containsEntry(RequestOperator.of(OperatorType.REGEX), "John"); }