TargetInstanceValidator extends AbstractBaseValidator { public void validate(Object aCommand, Errors aErrors) { TargetInstanceCommand cmd = (TargetInstanceCommand) aCommand; if (log.isDebugEnabled()) { log.debug("Validating target instance command."); } if (cmd.getCmd().equals(TargetInstanceCommand.ACTION_EDIT) && cmd.get_tab_current_page().equals("GENERAL")) { ValidationUtils.rejectIfEmptyOrWhitespace(aErrors, TargetInstanceCommand.PARAM_OWNER, "required", getObjectArrayForLabel(TargetInstanceCommand.PARAM_OWNER), "Owner is a required field."); ValidationUtils.rejectIfEmptyOrWhitespace(aErrors, TargetInstanceCommand.PARAM_OID, "required", getObjectArrayForLabel(TargetInstanceCommand.PARAM_OID), "Target Instance Id is a required field."); ValidationUtils.rejectIfEmptyOrWhitespace(aErrors, TargetInstanceCommand.PARAM_TIME, "required", getObjectArrayForLabel(TargetInstanceCommand.PARAM_TIME), "Scheduled Time is a required field."); if (!aErrors.hasErrors()) { if (cmd != null && cmd.getBandwidthPercent() != null) { ValidatorUtil.validateMaxBandwidthPercentage(aErrors, cmd.getBandwidthPercent(), "max.bandwidth.exeeded"); } } } else if (cmd.getCmd().equals(TargetInstanceCommand.ACTION_ADD_NOTE) || cmd.getCmd().equals(TargetInstanceCommand.ACTION_MODIFY_NOTE)) { ValidationUtils.rejectIfEmptyOrWhitespace(aErrors, TargetInstanceCommand.PARAM_OID, "required", getObjectArrayForLabel(TargetInstanceCommand.PARAM_OID), "Target Instance Id is a required field."); ValidationUtils.rejectIfEmptyOrWhitespace(aErrors, TargetInstanceCommand.PARAM_NOTE, "required", getObjectArrayForLabel(TargetInstanceCommand.PARAM_NOTE), "Annotation is a required field."); ValidatorUtil.validateStringMaxLength(aErrors, cmd.getNote(), TargetAnnotationCommand.CNST_MAX_NOTE_LENGTH, "string.maxlength", getObjectArrayForLabelAndInt(TargetAnnotationCommand.PARAM_NOTE, TargetAnnotationCommand.CNST_MAX_NOTE_LENGTH), "Annotation is too long"); } else if (cmd.getCmd().equals(TargetInstanceCommand.ACTION_EDIT) && cmd.get_tab_current_page().equals("DISPLAY")) { ValidatorUtil.validateStringMaxLength(aErrors, cmd.getDisplayNote(), TargetInstance.MAX_DISPLAY_NOTE_LENGTH, "string.maxlength", getObjectArrayForLabelAndInt(TargetInstanceCommand.PARAM_DISPLAY_NOTE, TargetInstance.MAX_DISPLAY_NOTE_LENGTH), "Display note is too long"); ValidatorUtil.validateStringMaxLength(aErrors, cmd.getDisplayChangeReason(), TargetInstance.MAX_DISPLAY_CHANGE_REASON_LENGTH, "string.maxlength", getObjectArrayForLabelAndInt(TargetInstanceCommand.PARAM_DISPLAY_CHANGE_REASON, TargetInstance.MAX_DISPLAY_CHANGE_REASON_LENGTH), "Display Change Reason is too long"); } } TargetInstanceValidator(); boolean supports(Class aClass); void validate(Object aCommand, Errors aErrors);  }
@Test public void testValidateCase01() { try { TargetInstanceCommand cmd = new TargetInstanceCommand(); TargetInstanceValidator validator = new TargetInstanceValidator(); Errors errors = new BindException(cmd, "TargetInstanceCommand"); cmd.setCmd(TargetInstanceCommand.ACTION_EDIT); cmd.set_tab_current_page("DISPLAY"); validator.validate(cmd, errors); assertEquals("Case01: Not expecting errors with newly initialised objects", 0, errors.getErrorCount()); } catch (Exception e) { String message = e.getClass().toString() + " - " + e.getMessage(); log.debug(message); fail(message); } }
@Test public void testValidateCase02() { try { TargetInstanceCommand cmd = new TargetInstanceCommand(); TargetInstanceValidator validator = new TargetInstanceValidator(); Errors errors = new BindException(cmd, "TargetInstanceCommand"); cmd.setCmd(TargetInstanceCommand.ACTION_EDIT); cmd.set_tab_current_page("DISPLAY"); cmd.setDisplayNote(makeLongString(4000, 'X')); validator.validate(cmd, errors); assertEquals("Case02: Expecting 0 errors", 0, errors.getErrorCount()); } catch (Exception e) { String message = e.getClass().toString() + " - " + e.getMessage(); log.debug(message); fail(message); } }
@Test public void testValidateCase03() { try { TargetInstanceCommand cmd = new TargetInstanceCommand(); TargetInstanceValidator validator = new TargetInstanceValidator(); Errors errors = new BindException(cmd, "TargetInstanceCommand"); cmd.setCmd(TargetInstanceCommand.ACTION_EDIT); cmd.set_tab_current_page("DISPLAY"); cmd.setDisplayNote(makeLongString(4001, 'X')); validator.validate(cmd, errors); assertEquals("Case02: Expecting 1 errors", 1, errors.getErrorCount()); assertEquals("Case02: Expecting error[0] for field Display Note", true, errors.getAllErrors().toArray()[0].toString().contains("Display note is too long")); } catch (Exception e) { String message = e.getClass().toString() + " - " + e.getMessage(); log.debug(message); fail(message); } }