EndpointNameChecker { public String check(String endpointName) { if (endpointName == null || endpointName.isEmpty()) { return "Endpoint name cannot be blank"; } if (ECMA_SCRIPT_RESERVED_WORDS.contains(endpointName)) { return "Endpoint name cannot be equal to JavaScript reserved words"; } if (CONTAINS_WHITESPACE_PATTERN.matcher(endpointName).matches()) { return "Endpoint name cannot contain any whitespaces"; } return null; }  String check(String endpointName); static final Set<String> ECMA_SCRIPT_RESERVED_WORDS; }
@Test public void should_ReturnValidationError_When_NullAsEndpointNameGiven() { String validationResult = checker.check(null); assertNotNull(validationResult); assertTrue(validationResult.contains("blank")); }
@Test public void should_ReturnValidationError_When_EmptyAsEndpointNameGiven() { String validationResult = checker.check(""); assertNotNull(validationResult); assertTrue(validationResult.contains("blank")); }
@Test public void should_ReturnValidationError_When_ReservedWordAsEndpointNameGiven() { for (String reservedEsWord : EndpointNameChecker.ECMA_SCRIPT_RESERVED_WORDS) { String validationResult = checker.check(reservedEsWord); assertNotNull(String.format( "Word '%s' should cause validation errors, but it did not", reservedEsWord), validationResult); assertTrue(validationResult.contains("reserved")); } }
@Test public void should_ReturnValidationError_When_EndpointNameWithWhitespaceGiven() { for (String wordWithWhitespace : Arrays.asList("bad name", "bad\nname", "bad\tname", "bad\rname")) { String validationResult = checker.check(wordWithWhitespace); assertNotNull(String.format( "Word '%s' should cause validation errors, but it did not", wordWithWhitespace), validationResult); assertTrue(validationResult.contains("whitespace")); } }
@Test public void should_ReturnNoErrors_When_ValidEndpointNameGiven() { String validationResult = checker.check("ordinaryName"); assertNull(validationResult); }