FilterTermsTreeBuilder { public static FilterTermsTree convertExpresionItemsTree(final ExpressionOperator rootItem) { return convertExpresionItemsTree(rootItem, Collections.emptySet()); } private  FilterTermsTreeBuilder(); static FilterTermsTree convertExpresionItemsTree(final ExpressionOperator rootItem); static FilterTermsTree convertExpresionItemsTree(final ExpressionOperator rootItem,
                                                            final Set<String> blackListedFieldNames);  }
@Test void testConvertExpressionItemsTree() { final String term1value1 = "term1value1"; final String term1value2 = "term1value2"; final String term1value3 = "term1value3"; final ExpressionOperator.Builder op1 = new ExpressionOperator.Builder(Op.AND) .addTerm("term1field", Condition.IN, term1value1 + "," + term1value2 + "," + term1value3) .addOperator(new ExpressionOperator.Builder(Op.OR) .addTerm("term2field", Condition.EQUALS, "term2value") .addTerm("term3field", Condition.EQUALS, "term3value") .addOperator(new ExpressionOperator.Builder(Op.NOT) .addTerm("term4field", Condition.EQUALS, "term4value") .build()) .build()); final FilterTermsTree filterTermsTree = FilterTermsTreeBuilder.convertExpresionItemsTree(op1.build()); final OperatorNode newOp1 = (OperatorNode) filterTermsTree.getRootNode(); assertThat(newOp1.getFilterOperationMode().toString()).isEqualTo(Op.AND.toString()); final OperatorNode newTerm1OpNode = (OperatorNode) newOp1.getChildren().get(0); assertThat(newTerm1OpNode.getFilterOperationMode().toString()).isEqualTo(Op.OR.toString()); assertThat(newTerm1OpNode.getChildren().size()).isEqualTo(3); final TermNode newTerm1SubTerm1 = (TermNode) newTerm1OpNode.getChildren().get(0); final TermNode newTerm1SubTerm2 = (TermNode) newTerm1OpNode.getChildren().get(1); final TermNode newTerm1SubTerm3 = (TermNode) newTerm1OpNode.getChildren().get(2); assertThat(newTerm1SubTerm1.getTag()).isEqualTo("term1field"); assertThat(newTerm1SubTerm1.getValue()).isEqualTo(term1value1); assertThat(newTerm1SubTerm2.getTag()).isEqualTo("term1field"); assertThat(newTerm1SubTerm2.getValue()).isEqualTo(term1value2); assertThat(newTerm1SubTerm3.getTag()).isEqualTo("term1field"); assertThat(newTerm1SubTerm3.getValue()).isEqualTo(term1value3); final OperatorNode newOp2 = (OperatorNode) newOp1.getChildren().get(1); assertThat(newOp2.getFilterOperationMode().toString()).isEqualTo(Op.OR.toString()); final TermNode newTerm2 = (TermNode) newOp2.getChildren().get(0); final TermNode newTerm3 = (TermNode) newOp2.getChildren().get(1); final OperatorNode newOp3 = (OperatorNode) newOp2.getChildren().get(2); assertThat(newTerm2.getTag()).isEqualTo("term2field"); assertThat(newTerm2.getValue()).isEqualTo("term2value"); assertThat(newTerm3.getTag()).isEqualTo("term3field"); assertThat(newTerm3.getValue()).isEqualTo("term3value"); assertThat(newOp3.getFilterOperationMode().toString()).isEqualTo(Op.NOT.toString()); final TermNode newTerm4 = (TermNode) newOp3.getChildren().get(0); assertThat(newTerm4.getTag()).isEqualTo("term4field"); assertThat(newTerm4.getValue()).isEqualTo("term4value"); }
@Test void testEmptyExpressionTree() { final ExpressionOperator.Builder op1 = new ExpressionOperator.Builder(Op.AND); FilterTermsTreeBuilder.convertExpresionItemsTree(op1.build()); }
@Test void testInvalidCondition() { assertThatThrownBy(() -> { final ExpressionOperator.Builder and = new ExpressionOperator.Builder(Op.AND); and.addTerm(StatisticStoreDoc.FIELD_NAME_DATE_TIME, Condition.EQUALS, "123456789"); and.addTerm("term2field", Condition.BETWEEN, "1,2"); FilterTermsTreeBuilder.convertExpresionItemsTree(and.build(), fieldBlackList); }).isInstanceOf(RuntimeException.class); }
@Test void testNonEqualsConditionForDatetimeField() { final ExpressionOperator.Builder and = new ExpressionOperator.Builder(Op.AND); and.addTerm(StatisticStoreDoc.FIELD_NAME_DATE_TIME, Condition.BETWEEN, "1,2"); and.addTerm("term2field", Condition.EQUALS, "123456789"); final FilterTermsTree filterTermsTree = FilterTermsTreeBuilder.convertExpresionItemsTree(and.build(), fieldBlackList); assertThat(filterTermsTree != null).isTrue(); }
@Test void testInConditionOneValue() { final ExpressionOperator.Builder and = new ExpressionOperator.Builder(Op.AND); and.addTerm(StatisticStoreDoc.FIELD_NAME_DATE_TIME, Condition.BETWEEN, "1,2"); and.addTerm("term1field", Condition.IN, "123456789"); final FilterTermsTree filterTermsTree = FilterTermsTreeBuilder.convertExpresionItemsTree(and.build(), fieldBlackList); final TermNode term2Node = (TermNode) filterTermsTree.getRootNode(); assertThat(term2Node.getTag()).isEqualTo("term1field"); assertThat(term2Node.getValue()).isEqualTo("123456789"); }
@Test void testInConditionNoValue() { final ExpressionOperator.Builder and = new ExpressionOperator.Builder(Op.AND); and.addTerm(StatisticStoreDoc.FIELD_NAME_DATE_TIME, Condition.BETWEEN, "1,2"); and.addTerm("term1field", Condition.IN, ""); final FilterTermsTree filterTermsTree = FilterTermsTreeBuilder.convertExpresionItemsTree(and.build(), fieldBlackList); final TermNode term2Node = (TermNode) filterTermsTree.getRootNode(); assertThat(term2Node).isNull(); }