SdkServiceIdValidator extends AbstractValidator { public static void validateServiceId(String serviceId) { if (isPreviouslyReleasedInvalidServiceId(serviceId)) { return; } List<String> messages = new ArrayList<>(); if (!validForPattern(serviceId)) { messages.add(String.format("Does not match the required pattern `%s`", SERVICE_ID_PATTERN.pattern())); } if (containsCompanyName(serviceId)) { messages.add(String.format( "Must not contain any of the following company names: [%s]", ValidationUtils.tickedList(COMPANY_NAMES))); } endsWithForbiddenWord(serviceId).ifPresent(suffix -> { messages.add(String.format("Must not case-insensitively end with `%s`", suffix)); }); if (serviceId.length() < 2 || serviceId.length() > 50) { messages.add("Must be between 2 and 50 characters long."); } if (!messages.isEmpty()) { throw new IllegalArgumentException(String.format( "Invalid SDK service ID value, `%s`: %s", serviceId, String.join(";", messages))); } }  @Override List<ValidationEvent> validate(Model model); static boolean isPreviouslyReleasedInvalidServiceId(String serviceId); static void validateServiceId(String serviceId);  }
@Test public void doesNotAllowCompanyNames() { IllegalArgumentException thrown = Assertions.assertThrows(IllegalArgumentException.class, () -> SdkServiceIdValidator.validateServiceId("AWS Foo")); assertThat(thrown.getMessage(), containsString("company names")); }
@Test public void doesNotAllowBadSuffix() { IllegalArgumentException thrown = Assertions.assertThrows(IllegalArgumentException.class, () -> SdkServiceIdValidator.validateServiceId("Foo Service")); assertThat(thrown.getMessage(), containsString("case-insensitively end with")); }
@Test public void mustMatchRegex() { Assertions.assertThrows(IllegalArgumentException.class, () -> SdkServiceIdValidator.validateServiceId("!Nope!")); }
@Test public void noTrailingWhitespace() { Assertions.assertThrows(IllegalArgumentException.class, () -> SdkServiceIdValidator.validateServiceId("Foo ")); }
@Test public void doesNotAllowShortIds() { IllegalArgumentException thrown = Assertions.assertThrows(IllegalArgumentException.class, () -> SdkServiceIdValidator.validateServiceId("F")); assertThat(thrown.getMessage(), containsString("2 and 50")); }
@Test public void doesNotAllowLongIds() { IllegalArgumentException thrown = Assertions.assertThrows(IllegalArgumentException.class, () -> SdkServiceIdValidator.validateServiceId("Foobarbazqux Foobarbazqux Foobarbazqux Foobarbazqux")); assertThat(thrown.getMessage(), containsString("2 and 50")); }