K8sInfraNamespaceWsAttributeValidator implements WorkspaceAttributeValidator { @Override public void validate(Map<String, String> attributes) throws ValidationException { String namespace = attributes.get(WORKSPACE_INFRASTRUCTURE_NAMESPACE_ATTRIBUTE); if (!isNullOrEmpty(namespace)) { NamespaceNameValidator.validate(namespace); namespaceFactoryProvider.get().checkIfNamespaceIsAllowed(namespace); } } @Inject  K8sInfraNamespaceWsAttributeValidator(
      Provider<KubernetesNamespaceFactory> namespaceFactoryProvider); @Override void validate(Map<String, String> attributes); @Override void validateUpdate(Map<String, String> existing, Map<String, String> update);  }
@Test( expectedExceptions = ValidationException.class, expectedExceptionsMessageRegExp = "The specified namespace [a]{64} is invalid: must be no more than 63 characters") public void testWhenNamespaceNameContainsMoreThan63Characters() throws ValidationException { StringBuilder sb = new StringBuilder(); for (int i = 0; i < 64; i++) { sb.append('a'); } validator.validate( ImmutableMap.of(WORKSPACE_INFRASTRUCTURE_NAMESPACE_ATTRIBUTE, sb.toString())); }
@Test( expectedExceptions = ValidationException.class, expectedExceptionsMessageRegExp = "The specified namespace .* is invalid: a DNS\\-1123 label must consist " + "of lower case alphanumeric characters or '\\-', and must start and end with an " + "alphanumeric character \\(e\\.g\\. .*", dataProvider = "invalidNamespaces") public void testWhenNamespaceNameHasNoValidFormat(String name) throws ValidationException { validator.validate(ImmutableMap.of(WORKSPACE_INFRASTRUCTURE_NAMESPACE_ATTRIBUTE, name)); }
@Test( expectedExceptions = ValidationException.class, expectedExceptionsMessageRegExp = "The specified namespace name is not valid") public void testWhenNamespaceFactoryThrowsErrorOnCheckingIfNamespaceIsAllowed() throws ValidationException { doThrow(new ValidationException("The specified namespace name is not valid")) .when(namespaceFactory) .checkIfNamespaceIsAllowed(anyString()); validator.validate(ImmutableMap.of(WORKSPACE_INFRASTRUCTURE_NAMESPACE_ATTRIBUTE, "any")); }
@Test public void shouldDoNothingWhenNamespaceAttributeIsMissing() throws ValidationException { validator.validate(emptyMap()); verifyZeroInteractions(namespaceFactory); }