RequestValidator { public static void validateExpression(Expression expression) { Assert.notNull(expression, "subscription expression can't null"); Assert.isTrue(Operator.AND.equals(expression.getOperator()), "Only AND operator is supported now"); Assert.notEmpty(expression.getOperands(), "Operands can't be empty"); expression.getOperands().forEach(operand -> validateOperand(operand)); }  static void validateUser(User user); static void validateExpression(Expression expression); static void validateBoolean(Boolean condition); static void validateMappingDetector(ConsumerDetectorMapping consumerDetectorMapping); static void validateDetectorDocument(DetectorDocument document); static void validateDetector(Detector detector);  }
@Test public void testValidateExpression_successful() { Expression expression = new Expression(); expression.setOperator(Operator.AND); List<Operand> operandsList = new ArrayList<>(); Operand operand1 = new Operand(); Operand operand2 = new Operand(); Operand operand3 = new Operand(); Field field1 = new Field(); field1.setKey("name"); field1.setValue("sample-app"); Field field2 = new Field(); field2.setKey("env"); field2.setValue("prod"); Field field3 = new Field(); field3.setKey("type"); field3.setValue("gauge"); operand1.setField(field1); operand2.setField(field2); operand3.setField(field3); operandsList.add(operand1); operandsList.add(operand2); expression.setOperands(operandsList); RequestValidator.validateExpression(expression); }
@Test(expected = RuntimeException.class) public void testValidateOperand_keyempty() { Expression expression = new Expression(); expression.setOperator(Operator.AND); List<Operand> operandsList = new ArrayList<>(); Operand testOperand = new Operand(); testOperand.setField(new Field("", "sample-app")); operandsList.add(testOperand); expression.setOperands(operandsList); RequestValidator.validateExpression(expression); }
@Test(expected = RuntimeException.class) public void testValidateOperand_valueempty() { Expression expression = new Expression(); expression.setOperator(Operator.AND); List<Operand> operandsList = new ArrayList<>(); Operand testoperand = new Operand(); testoperand.setField(new Field("name", "")); operandsList.add(testoperand); expression.setOperands(operandsList); RequestValidator.validateExpression(expression); }
@Test(expected = RuntimeException.class) public void testValidateOperand_keystartswithDetectorMappingEntityAAPREFIX() { Expression expression = new Expression(); expression.setOperator(Operator.AND); List<Operand> operandsList = new ArrayList<>(); Operand testOperand = new Operand(); testOperand.setField(new Field("aa_name", "sample-app")); operandsList.add(testOperand); expression.setOperands(operandsList); RequestValidator.validateExpression(expression); }