@Test public void testUsernameTooShort() { when(field.getName()).thenReturn("createNotification"); when(fieldAndArguments.getField()).thenReturn(field); when(fieldAndArguments.getArgumentValue("username")).thenReturn("aa"); when(environment.getFields()).thenReturn(Collections.singletonList(fieldAndArguments)); when(environment.mkError( eq("username must be between 3 and 64 characters"), any(FieldAndArguments.class))) .thenReturn(buildError("username must be between 3 and 64 characters")); final List<GraphQLError> actual = validation.validateFields(environment); verify(environment) .mkError(eq("username must be between 3 and 64 characters"), any(FieldAndArguments.class)); assertThat(actual.isEmpty()).isFalse(); assertThat(actual.get(0).getMessage()) .isEqualTo("username must be between 3 and 64 characters"); }
@Override public List<GraphQLError> validateFields(FieldValidationEnvironment environment) { final List<GraphQLError> errors = new ArrayList<>(); for (FieldAndArguments fieldAndArguments : environment.getFields()) { final Field field = fieldAndArguments.getField(); if (!VALID_FIELDS.contains(field.getName())) { continue; } LOGGER.debug("Field: {}", field.getName()); final String username = fieldAndArguments.getArgumentValue("username"); if (Strings.isNullOrEmpty(username)) { errors.add(environment.mkError("username cannot be empty", fieldAndArguments)); } else { final int length = username.codePointCount(0, username.length()); if (length < USERNAME_MIN_LENGTH || length > USERNAME_MAX_LENGTH) { errors.add( environment.mkError( String.format( "username must be between %d and %d characters", USERNAME_MIN_LENGTH, USERNAME_MAX_LENGTH), fieldAndArguments)); } else if (!username.matches("[A-Za-z0-9]+")) { errors.add( environment.mkError( "username must only contain alphanumeric characters", fieldAndArguments)); } } } return errors; }
UsernameFieldValidation implements FieldValidation { @Override public List<GraphQLError> validateFields(FieldValidationEnvironment environment) { final List<GraphQLError> errors = new ArrayList<>(); for (FieldAndArguments fieldAndArguments : environment.getFields()) { final Field field = fieldAndArguments.getField(); if (!VALID_FIELDS.contains(field.getName())) { continue; } LOGGER.debug("Field: {}", field.getName()); final String username = fieldAndArguments.getArgumentValue("username"); if (Strings.isNullOrEmpty(username)) { errors.add(environment.mkError("username cannot be empty", fieldAndArguments)); } else { final int length = username.codePointCount(0, username.length()); if (length < USERNAME_MIN_LENGTH || length > USERNAME_MAX_LENGTH) { errors.add( environment.mkError( String.format( "username must be between %d and %d characters", USERNAME_MIN_LENGTH, USERNAME_MAX_LENGTH), fieldAndArguments)); } else if (!username.matches("[A-Za-z0-9]+")) { errors.add( environment.mkError( "username must only contain alphanumeric characters", fieldAndArguments)); } } } return errors; } }
UsernameFieldValidation implements FieldValidation { @Override public List<GraphQLError> validateFields(FieldValidationEnvironment environment) { final List<GraphQLError> errors = new ArrayList<>(); for (FieldAndArguments fieldAndArguments : environment.getFields()) { final Field field = fieldAndArguments.getField(); if (!VALID_FIELDS.contains(field.getName())) { continue; } LOGGER.debug("Field: {}", field.getName()); final String username = fieldAndArguments.getArgumentValue("username"); if (Strings.isNullOrEmpty(username)) { errors.add(environment.mkError("username cannot be empty", fieldAndArguments)); } else { final int length = username.codePointCount(0, username.length()); if (length < USERNAME_MIN_LENGTH || length > USERNAME_MAX_LENGTH) { errors.add( environment.mkError( String.format( "username must be between %d and %d characters", USERNAME_MIN_LENGTH, USERNAME_MAX_LENGTH), fieldAndArguments)); } else if (!username.matches("[A-Za-z0-9]+")) { errors.add( environment.mkError( "username must only contain alphanumeric characters", fieldAndArguments)); } } } return errors; }  }
UsernameFieldValidation implements FieldValidation { @Override public List<GraphQLError> validateFields(FieldValidationEnvironment environment) { final List<GraphQLError> errors = new ArrayList<>(); for (FieldAndArguments fieldAndArguments : environment.getFields()) { final Field field = fieldAndArguments.getField(); if (!VALID_FIELDS.contains(field.getName())) { continue; } LOGGER.debug("Field: {}", field.getName()); final String username = fieldAndArguments.getArgumentValue("username"); if (Strings.isNullOrEmpty(username)) { errors.add(environment.mkError("username cannot be empty", fieldAndArguments)); } else { final int length = username.codePointCount(0, username.length()); if (length < USERNAME_MIN_LENGTH || length > USERNAME_MAX_LENGTH) { errors.add( environment.mkError( String.format( "username must be between %d and %d characters", USERNAME_MIN_LENGTH, USERNAME_MAX_LENGTH), fieldAndArguments)); } else if (!username.matches("[A-Za-z0-9]+")) { errors.add( environment.mkError( "username must only contain alphanumeric characters", fieldAndArguments)); } } } return errors; }  @Override List<GraphQLError> validateFields(FieldValidationEnvironment environment); }
UsernameFieldValidation implements FieldValidation { @Override public List<GraphQLError> validateFields(FieldValidationEnvironment environment) { final List<GraphQLError> errors = new ArrayList<>(); for (FieldAndArguments fieldAndArguments : environment.getFields()) { final Field field = fieldAndArguments.getField(); if (!VALID_FIELDS.contains(field.getName())) { continue; } LOGGER.debug("Field: {}", field.getName()); final String username = fieldAndArguments.getArgumentValue("username"); if (Strings.isNullOrEmpty(username)) { errors.add(environment.mkError("username cannot be empty", fieldAndArguments)); } else { final int length = username.codePointCount(0, username.length()); if (length < USERNAME_MIN_LENGTH || length > USERNAME_MAX_LENGTH) { errors.add( environment.mkError( String.format( "username must be between %d and %d characters", USERNAME_MIN_LENGTH, USERNAME_MAX_LENGTH), fieldAndArguments)); } else if (!username.matches("[A-Za-z0-9]+")) { errors.add( environment.mkError( "username must only contain alphanumeric characters", fieldAndArguments)); } } } return errors; }  @Override List<GraphQLError> validateFields(FieldValidationEnvironment environment);  }