QueryTranslator { private Criterion<?> translate(CriterionDTO dto) throws QueryTranslatorException { return Optional.ofNullable(getCriterionParser(dto)) .map(criterionGeneratingFunction -> criterionGeneratingFunction.apply(dto.getValue())) .orElseThrow(() -> new QueryTranslatorException("pair of fieldName: '" + dto.getFieldName() + "' and operator: '" + dto.getOperator() + "' is not supported")); } @Inject @VisibleForTesting  QueryTranslator(MailboxId.Factory mailboxIdFactory); static FieldName getField(String fieldNameString); Query translate(QueryElement queryElement); QueryDTO toDTO(Query query);  }
@Test void translateShouldThrowWhenPassingNotAndOperator() { assertThatThrownBy(() -> queryTranslator.translate(new QueryDTO("or", ImmutableList.of()))) .isInstanceOf(IllegalArgumentException.class) .hasMessage("combinator 'or' is not yet handled"); }
@Test void translateShouldNotThrowWhenPassingNullOperator() { String nullOperator = null; assertThatCode(() -> queryTranslator.translate(new QueryDTO(nullOperator, ImmutableList.of()))) .doesNotThrowAnyException(); }
@Test void translateShouldThrowWhenPassingNestedQuery() { assertThatThrownBy(() -> queryTranslator.translate(QueryDTO.and( QueryDTO.and(new CriterionDTO(FieldName.SUBJECT.getValue(), Operator.CONTAINS.getValue(), "james")) ))) .isInstanceOf(IllegalArgumentException.class) .hasMessage("nested query structure is not yet handled"); }
@Test void translateShouldNotThrowWhenPassingFlattenQuery() { assertThatCode(() -> queryTranslator.translate(QueryDTO.and( new CriterionDTO(FieldName.SUBJECT.getValue(), Operator.CONTAINS.getValue(), "james"), new CriterionDTO(FieldName.SENDER.getValue(), Operator.EQUALS.getValue(), "user@james.org"), new CriterionDTO(FieldName.HAS_ATTACHMENT.getValue(), Operator.EQUALS.getValue(), "true") ))) .doesNotThrowAnyException(); }