RequestParamsCriteriaBuilder { public Criteria build(Map<RequestObject, Object> requestParams) { return build(Optional.empty(), requestParams, FieldMapper.of(Maps.newHashMap())); }  Criteria build(Map<RequestObject, Object> requestParams); Criteria build(Map<RequestObject, Object> requestParams, FieldMapper fieldMapper); Criteria build(Criteria initialCriteria, Map<RequestObject, Object> requestParams); Criteria build(Criteria initialCriteria, Map<RequestObject, Object> requestParams, FieldMapper fieldMapper);  }
@Test public void testFilterStartingWithFieldFollowedByLogicalOperatorShouldProduceProperCriteria() throws Exception { Map<RequestObject, Object> filters = ImmutableMap.of( RequestField.of("name"), ImmutableMap.of( RequestLogicalOperator.of(LogicalOperatorType.OR), ImmutableMap.of( RequestOperator.of(OperatorType.STARTS_WITH), "John", RequestOperator.of(OperatorType.IN), Lists.newArrayList("John", "Bob")) ) ); Criteria result = requestParamsCriteriaBuilder.build(filters); assertThat(result).isEqualTo(new Criteria().orOperator( Criteria.where("name").regex("^John", "i"), Criteria.where("name").in("John", "Bob"))); }
@Test public void testFilterStartingWithFieldFollowedByOperatorShouldProduceProperCriteria() throws Exception { Map<RequestObject, Object> filters = ImmutableMap.of( RequestField.of("name"), ImmutableMap.of( RequestOperator.of(OperatorType.STARTS_WITH), "John", RequestOperator.of(OperatorType.IN), Lists.newArrayList("John", "Bob") ) ); Criteria result = requestParamsCriteriaBuilder.build(filters); assertThat(result).isEqualTo(new Criteria().andOperator( Criteria.where("name").regex("^John", "i"), Criteria.where("name").in("John", "Bob"))); }
@Test public void testFilterFromFieldFollowedByLogicalOperatorToOperatorShouldProduceProperCriteria() throws Exception { Map<RequestObject, Object> filters = ImmutableMap.of( RequestField.of("name"), ImmutableMap.of( RequestLogicalOperator.of(LogicalOperatorType.OR), ImmutableMap.of( RequestOperator.of(OperatorType.STARTS_WITH), "John", RequestOperator.of(OperatorType.IN), Lists.newArrayList("Doe", "Smith")) )); Criteria result = requestParamsCriteriaBuilder.build(filters); assertThat(result).isEqualTo(new Criteria().orOperator( Criteria.where("name").regex("^John", "i"), Criteria.where("name").in("Doe", "Smith"))); }
@Test(expected = IllegalArgumentException.class) public void testFilterFromFieldFollowedByLogicalOperatorToLogicalOperatorShouldFail() throws Exception { Map<RequestObject, Object> filters = ImmutableMap.of( RequestField.of("name"), ImmutableMap.of( RequestLogicalOperator.of(LogicalOperatorType.OR), ImmutableMap.of(RequestLogicalOperator.of(LogicalOperatorType.OR), ImmutableMap.of( RequestOperator.of(OperatorType.STARTS_WITH), "John", RequestOperator.of(OperatorType.IN), Lists.newArrayList("Doe", "Smith")) ) )); requestParamsCriteriaBuilder.build(filters); }
@Test(expected = IllegalArgumentException.class) public void testFilterFromFieldFollowedByLogicalOperatorWithIncorrectValue() throws Exception { Map<RequestObject, Object> filters = ImmutableMap.of( RequestField.of("name"), ImmutableMap.of( RequestLogicalOperator.of(LogicalOperatorType.OR), "incorrectTypeValue" )); requestParamsCriteriaBuilder.build(filters); }
@Test(expected = IllegalArgumentException.class) public void testFilterFromFieldFollowedByLogicalOperatorToFieldShouldFail() throws Exception { Map<RequestObject, Object> filters = ImmutableMap.of( RequestField.of("name"), ImmutableMap.of( RequestLogicalOperator.of(LogicalOperatorType.OR), ImmutableMap.of(RequestField.of("lastName"), ImmutableMap.of( RequestOperator.of(OperatorType.STARTS_WITH), "John", RequestOperator.of(OperatorType.IN), Lists.newArrayList("Doe", "Smith")) ) )); requestParamsCriteriaBuilder.build(filters); }
@Test(expected = IllegalArgumentException.class) public void testFilterFromFieldFollowedByFieldShouldFail() throws Exception { Map<RequestObject, Object> filters = ImmutableMap.of( RequestField.of("foo"), ImmutableMap.of(RequestField.of("bar"), Maps.newHashMap())); requestParamsCriteriaBuilder.build(filters); }
@Test public void testFilterFromFieldFollowedByOperatorShouldProduceProperCriteria() throws Exception { Map<RequestObject, Object> filters = ImmutableMap.of( RequestLogicalOperator.of(LogicalOperatorType.OR), ImmutableMap.of( RequestField.of("name"), ImmutableMap.of(RequestOperator.of(OperatorType.STARTS_WITH), "John"), RequestField.of("lastName"), ImmutableMap.of(RequestOperator.of(OperatorType.IN), Lists.newArrayList("Doe", "Smith")) ) ); Criteria result = requestParamsCriteriaBuilder.build(filters); assertThat(result).isEqualTo(new Criteria().orOperator( Criteria.where("name").regex("^John", "i"), Criteria.where("lastName").in("Doe", "Smith"))); }
@Test public void testOfAllOperators() throws Exception { Map<RequestObject, Object> filters = ImmutableMap.of( RequestField.of("name"), ImmutableMap.builder() .put(RequestOperator.of(OperatorType.STARTS_WITH), "John") .put(RequestOperator.of(OperatorType.IN), Lists.newArrayList("John", "Bob")) .put(RequestOperator.of(OperatorType.LESS_THAN), 4) .put(RequestOperator.of(OperatorType.EQUAL), "John") .put(RequestOperator.of(OperatorType.GREATER_THAN), 1) .put(RequestOperator.of(OperatorType.GREATER_THAN_EQUAL), 2) .put(RequestOperator.of(OperatorType.LESS_THAN_EQUAL), 3) .put(RequestOperator.of(OperatorType.NOT_EQUAL), "Bob") .put(RequestOperator.of(OperatorType.NOT_IN), Lists.newArrayList("Brian", "Adam")) .build() ); Criteria result = requestParamsCriteriaBuilder.build(filters); assertThat(result).isEqualTo(new Criteria().andOperator( Criteria.where("name").regex("^John", "i"), Criteria.where("name").in("John", "Bob"), Criteria.where("name").lt(4), Criteria.where("name").is("John"), Criteria.where("name").gt(1), Criteria.where("name").gte(2), Criteria.where("name").lte(3), Criteria.where("name").ne("Bob"), Criteria.where("name").nin("Brian", "Adam"))); }
@Test(expected = IllegalArgumentException.class) public void testRootElementWithIncorrectValue() throws Exception { Map<RequestObject, Object> filters = ImmutableMap.of( RequestLogicalOperator.of(LogicalOperatorType.OR), "incorrectTypeValue"); requestParamsCriteriaBuilder.build(filters); }
@Test public void testEmptyFiltersShouldProduceEmptyQuery() throws Exception { Map<RequestObject, Object> filters = ImmutableMap.of(); Criteria criteria = requestParamsCriteriaBuilder.build(filters); assertThat(criteria).isEqualTo(new Criteria()); }
@Test public void testProduceCriteriaWithRemappedNames() throws Exception { Map<RequestObject, Object> filters = ImmutableMap.of( RequestLogicalOperator.of(LogicalOperatorType.OR), ImmutableMap.of( RequestField.of("name"), ImmutableMap.of(RequestOperator.of(OperatorType.STARTS_WITH), "John"), RequestField.of("secondName"), ImmutableMap.of(RequestOperator.of(OperatorType.STARTS_WITH), "Bob"), RequestField.of("lastName"), ImmutableMap.of(RequestOperator.of(OperatorType.IN), Lists.newArrayList("Doe", "Smith")) ) ); FieldMapper fieldMapper = FieldMapper.of(ImmutableMap.of( "name", "remappedName", "lastName", "remappedLastName" )); Criteria result = requestParamsCriteriaBuilder.build(filters, fieldMapper); assertThat(result).isEqualTo(new Criteria().orOperator( Criteria.where("remappedName").regex("^John", "i"), Criteria.where("secondName").regex("^Bob", "i"), Criteria.where("remappedLastName").in("Doe", "Smith"))); }
@Test public void testProduceCriteriaWithInitialCriteriaIncluded() throws Exception { Map<RequestObject, Object> filters = ImmutableMap.of( RequestLogicalOperator.of(LogicalOperatorType.OR), ImmutableMap.of( RequestField.of("name"), ImmutableMap.of(RequestOperator.of(OperatorType.STARTS_WITH), "John"), RequestField.of("secondName"), ImmutableMap.of(RequestOperator.of(OperatorType.STARTS_WITH), "Bob"), RequestField.of("lastName"), ImmutableMap.of(RequestOperator.of(OperatorType.IN), Lists.newArrayList("Doe", "Smith")) ) ); FieldMapper fieldMapper = FieldMapper.of(ImmutableMap.of()); Criteria result = requestParamsCriteriaBuilder .build(Criteria.where("initialField").is("initialValue"), filters, fieldMapper); assertThat(result).isEqualTo(new Criteria().andOperator( Criteria.where("initialField").is("initialValue"), new Criteria().orOperator( Criteria.where("name").regex("^John", "i"), Criteria.where("secondName").regex("^Bob", "i"), Criteria.where("lastName").in("Doe", "Smith")) ) ); }
@Test public void testProduceCriteriaWithMultipleRootLevelElements() throws Exception { Map<RequestObject, Object> filters = ImmutableMap.of( RequestField.of("name"), ImmutableMap.of(RequestOperator.of(OperatorType.STARTS_WITH), "John"), RequestField.of("secondName"), ImmutableMap.of(RequestOperator.of(OperatorType.STARTS_WITH), "Bob"), RequestField.of("lastName"), ImmutableMap.of(RequestOperator.of(OperatorType.IN), Lists.newArrayList("Doe", "Smith")) ); Criteria result = requestParamsCriteriaBuilder.build(filters); assertThat(result).isEqualTo(new Criteria().andOperator( Criteria.where("name").regex("^John", "i"), Criteria.where("secondName").regex("^Bob", "i"), Criteria.where("lastName").in("Doe", "Smith"))); }
@Test public void testProduceCriteriaWithDateElements() throws Exception { Map<RequestObject, Object> filters = ImmutableMap.of( RequestField.of("date"), ImmutableMap.of(RequestOperator.of(OperatorType.LESS_THAN), "2016-01-01T22:54:36.115Z") ); Criteria result = requestParamsCriteriaBuilder.build(filters); Criteria expectedCriteria = Criteria.where("date").lt(ZonedDateTime.of(2016, 1, 1, 22, 54, 36, 115000000, ZoneId.systemDefault())); assertThat(result).isEqualTo(expectedCriteria); }
@Test public void testFilterStartingWithLogicalOperatorShouldProduceProperCriteria() throws Exception { Map<RequestObject, Object> filters = ImmutableMap.of( RequestLogicalOperator.of(LogicalOperatorType.OR), ImmutableMap.of(RequestField.of("name"), ImmutableMap.of(RequestOperator.of(OperatorType.STARTS_WITH), "John") )); Criteria result = requestParamsCriteriaBuilder.build(filters); assertThat(result).isEqualTo(new Criteria().orOperator(Criteria.where("name").regex("^John", "i"))); }
@Test(expected = IllegalArgumentException.class) public void testFilterStartingWithLogicalOperatorFollowedByLogicalOperatorShouldFail() throws Exception { Map<RequestObject, Object> filters = ImmutableMap.of( RequestLogicalOperator.of(LogicalOperatorType.OR), ImmutableMap.of(RequestLogicalOperator.of(LogicalOperatorType.OR), Maps.newHashMap())); requestParamsCriteriaBuilder.build(filters); }
@Test(expected = IllegalArgumentException.class) public void testFilterStartingWithLogicalOperatorFollowedByOperatorShouldFail() throws Exception { Map<RequestObject, Object> filters = ImmutableMap.of( RequestLogicalOperator.of(LogicalOperatorType.OR), ImmutableMap.of(RequestOperator.of(OperatorType.STARTS_WITH), Maps.newHashMap())); requestParamsCriteriaBuilder.build(filters); }
@Test(expected = IllegalArgumentException.class) public void testFilterStartingWithLogicalOperatorFollowedByOperatorWithIncorrectValueShouldFail() throws Exception { Map<RequestObject, Object> filters = ImmutableMap.of( RequestLogicalOperator.of(LogicalOperatorType.OR), ImmutableMap.of(RequestField.of("name"), "incorrectTypeValue")); requestParamsCriteriaBuilder.build(filters); }
@Test public void testFilterStartingWithLogicalOperatorFollowedByFieldShouldProduceProperCriteria() throws Exception { Map<RequestObject, Object> filters = ImmutableMap.of(RequestLogicalOperator.of(LogicalOperatorType.OR), ImmutableMap.of( RequestField.of("count"), ImmutableMap.of(RequestOperator.of(OperatorType.LESS_THAN), 5) )); Criteria result = requestParamsCriteriaBuilder.build(filters); assertThat(result).isEqualTo(new Criteria().orOperator(Criteria.where("count").lt(5))); }
@Test(expected = IllegalArgumentException.class) public void testFilterStartingWithOperatorInRootShouldFail() throws Exception { Map<RequestObject, Object> filters = ImmutableMap.of(RequestOperator.of(OperatorType.STARTS_WITH), Maps.newHashMap()); requestParamsCriteriaBuilder.build(filters); }
@Test public void testFilterStartingWithFieldShouldProduceProperCriteria() throws Exception { Map<RequestObject, Object> filters = ImmutableMap.of(RequestField.of("name"), ImmutableMap.of(RequestOperator.of(OperatorType.STARTS_WITH), "John") ); Criteria result = requestParamsCriteriaBuilder.build(filters); assertThat(result).isEqualTo(Criteria.where("name").regex("^John", "i")); }
@Test(expected = IllegalArgumentException.class) public void testFilterStartingWithFieldFollowedByFieldShouldFail() throws Exception { Map<RequestObject, Object> filters = ImmutableMap.of( RequestField.of("name"), ImmutableMap.of(RequestField.of("surname"), Maps.newHashMap())); requestParamsCriteriaBuilder.build(filters); }