Operators { private static void writeFieldCheckCondition( final String format, final QueryStatement query, final FieldPath contextFieldPath, final Object queryParameter ) throws QueryBuildException { if (contextFieldPath == null) { throw new QueryBuildException("Field check conditions not allowed outside of field context"); } try { query.getBodyWriter().write(String.format(format, contextFieldPath.toSQL())); query.pushParameterSetter((statement, index) -> { statement.setObject(index++, queryParameter); return index; }); } catch (IOException e) { throw new QueryBuildException("Query search conditions write failed because of exception", e); } } private  Operators(); static void addAll(final PostgresQueryWriterResolver resolver);  }
@Test public void writeFieldCheckConditionTest() throws Exception { final int OPERATORS_NUMBER = 10; final String queryParam = "testQueryParam"; List<String> operatorsNames = new ArrayList<>(OPERATORS_NUMBER); List<FieldPath> fieldsPaths = new ArrayList<>(OPERATORS_NUMBER); List<String> result = new ArrayList<>(OPERATORS_NUMBER); operatorsNames.addAll( Arrays.asList( "$eq", "$ne", "$lt", "$gt", "$lte", "$gte", "$date-from", "$date-to", "$hasTag", "$fulltext")); for (String name : operatorsNames) fieldsPaths.add(PostgresFieldPath.fromString(name.replace("$", ""))); result.addAll( Arrays.asList( "((document#>'{eq}')=to_json(?)::jsonb)", "((document#>'{ne}')!=to_json(?)::jsonb)", "((document#>'{lt}')<to_json(?)::jsonb)", "((document#>'{gt}')>to_json(?)::jsonb)", "((document#>'{lte}')<=to_json(?)::jsonb)", "((document#>'{gte}')>=to_json(?)::jsonb)", "(parse_timestamp_immutable(document#>'{date-from}')>=(?)::timestamp)", "(parse_timestamp_immutable(document#>'{date-to}')<=(?)::timestamp)", "((document#>'{hasTag}')??(?))", "fulltext@@(to_tsquery('russian',?))")); for (int i = 0; i < OPERATORS_NUMBER; ++i) { StringWriter body = new StringWriter(); QueryStatement queryStatement = mock(QueryStatement.class); when(queryStatement.getBodyWriter()).thenReturn(body); resolver .resolve(operatorsNames.get(i)) .write(queryStatement, resolver, fieldsPaths.get(i), queryParam); assertEquals(result.get(i), body.toString().trim()); verify(queryStatement).pushParameterSetter(any()); } }