K8sInfraNamespaceWsAttributeValidator implements WorkspaceAttributeValidator { @Override public void validateUpdate(Map<String, String> existing, Map<String, String> update) throws ValidationException { String existingNamespace = existing.get(WORKSPACE_INFRASTRUCTURE_NAMESPACE_ATTRIBUTE); String updateNamespace = update.get(WORKSPACE_INFRASTRUCTURE_NAMESPACE_ATTRIBUTE); if (isNullOrEmpty(updateNamespace)) { if (isNullOrEmpty(existingNamespace)) { return; } throw new ValidationException( format( "The namespace information must not be updated or " + "deleted. You must provide \"%s\" attribute with \"%s\" as a value", WORKSPACE_INFRASTRUCTURE_NAMESPACE_ATTRIBUTE, existingNamespace)); } if (isNullOrEmpty(existingNamespace)) { validate(update); return; } if (!updateNamespace.equals(existingNamespace)) { throw new ValidationException( format( "The namespace from the provided object \"%s\" does " + "not match the actual namespace \"%s\"", updateNamespace, existingNamespace)); } } @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 namespace from the provided object \"new\" does not match the actual namespace \"actual\"") public void shouldDoNotAllowToChangeNamespaceAttribute() throws ValidationException { validator.validateUpdate( ImmutableMap.of(WORKSPACE_INFRASTRUCTURE_NAMESPACE_ATTRIBUTE, "actual"), ImmutableMap.of(WORKSPACE_INFRASTRUCTURE_NAMESPACE_ATTRIBUTE, "new")); }
@Test( expectedExceptions = ValidationException.class, expectedExceptionsMessageRegExp = "The namespace information must not be updated or deleted. You must provide \"infrastructureNamespace\" attribute with \"che-workspaces\" as a value") public void shouldDoNotAllowToRemoveNamespaceAttribute() throws ValidationException { validator.validateUpdate( ImmutableMap.of(WORKSPACE_INFRASTRUCTURE_NAMESPACE_ATTRIBUTE, "che-workspaces"), emptyMap()); }
@Test public void shouldValidateFullyIfExistingIsEmpty() throws ValidationException { validator.validateUpdate( emptyMap(), ImmutableMap.of(WORKSPACE_INFRASTRUCTURE_NAMESPACE_ATTRIBUTE, "che-workspaces")); verify(namespaceFactory).checkIfNamespaceIsAllowed(eq("che-workspaces")); }
@Test public void shouldNotValidateFullyIfExistingIsNotEmpty() throws ValidationException { validator.validateUpdate( ImmutableMap.of(WORKSPACE_INFRASTRUCTURE_NAMESPACE_ATTRIBUTE, "che-workspaces"), ImmutableMap.of(WORKSPACE_INFRASTRUCTURE_NAMESPACE_ATTRIBUTE, "che-workspaces")); verify(namespaceFactory, never()).checkIfNamespaceIsAllowed(any()); }