WorkspaceValidator { public void validateConfig(WorkspaceConfig config) throws ValidationException, ServerException { checkNotNull(config.getName(), "Workspace name required"); check( WS_NAME.matcher(config.getName()).matches(), "Incorrect workspace name, it must be between 3 and 100 characters and may contain digits, " + "latin letters, underscores, dots, dashes and must start and end only with digits, " + "latin letters or underscores"); validateEnvironments(config); for (Command command : config.getCommands()) { check( !isNullOrEmpty(command.getName()), "Workspace %s contains command with null or empty name", config.getName()); check( !isNullOrEmpty(command.getCommandLine()) || !isNullOrEmpty( command.getAttributes().get(Command.COMMAND_ACTION_REFERENCE_CONTENT_ATTRIBUTE)), "Command line or content required for command '%s' in workspace '%s'.", command.getName(), config.getName()); } validatePlugins(config); } @Inject  WorkspaceValidator(Set<WorkspaceAttributeValidator> attributeValidators); void validateConfig(WorkspaceConfig config); void validateAttributes(Map<String, String> attributes); void validateUpdateAttributes(Map<String, String> existing, Map<String, String> update);  }
@Test public void shouldValidateCorrectWorkspace() throws Exception { final WorkspaceConfigDto config = createConfig(); wsValidator.validateConfig(config); }
@Test( expectedExceptions = ValidationException.class, expectedExceptionsMessageRegExp = "Workspace name required") public void shouldFailValidationIfNameIsNull() throws Exception { final WorkspaceConfigDto config = createConfig(); config.withName(null); wsValidator.validateConfig(config); }
@Test( dataProvider = "invalidNameProvider", expectedExceptions = ValidationException.class, expectedExceptionsMessageRegExp = "Incorrect workspace name, it must be between 3 and 100 " + "characters and may contain digits, latin letters, underscores, dots, dashes and must " + "start and end only with digits, latin letters or underscores") public void shouldFailValidationIfNameIsInvalid(String name) throws Exception { final WorkspaceConfigDto config = createConfig(); config.withName(name); wsValidator.validateConfig(config); }
@Test(dataProvider = "validNameProvider") public void shouldValidateCorrectWorkspaceName(String name) throws Exception { final WorkspaceConfigDto config = createConfig(); config.withName(name); wsValidator.validateConfig(config); }
@Test( expectedExceptions = ValidationException.class, expectedExceptionsMessageRegExp = "Workspace default environment name required") public void shouldFailValidationOfChe6WSIfDefaultEnvNameIsNull() throws Exception { final WorkspaceConfigDto config = createConfig(); config.setDefaultEnv(null); wsValidator.validateConfig(config); }
@Test( expectedExceptions = ValidationException.class, expectedExceptionsMessageRegExp = "Workspace default environment name required") public void shouldFailValidationOfChe6WSIfDefaultEnvNameIsEmpty() throws Exception { final WorkspaceConfigDto config = createConfig(); config.setDefaultEnv(""); wsValidator.validateConfig(config); }
@Test public void shouldNotFailValidationOfChe7WSIfDefaultEnvNameIsNullAndNoEnvIsPresent() throws Exception { final WorkspaceConfigDto config = createConfig(); config.setDefaultEnv(null); config.getEnvironments().clear(); config.getAttributes().put(Constants.WORKSPACE_TOOLING_EDITOR_ATTRIBUTE, "something"); wsValidator.validateConfig(config); }
@Test( expectedExceptions = ValidationException.class, expectedExceptionsMessageRegExp = "Workspace default environment configuration required") public void shouldFailValidationIfEnvWithDefaultEnvNameIsNull() throws Exception { final WorkspaceConfigDto config = createConfig(); config.setEnvironments(null); wsValidator.validateConfig(config); }
@Test( expectedExceptions = ValidationException.class, expectedExceptionsMessageRegExp = "Workspace ws-name contains command with null or empty name") public void shouldFailValidationIfCommandNameIsNull() throws Exception { final WorkspaceConfigDto config = createConfig(); config.getCommands().get(0).withName(null); wsValidator.validateConfig(config); }
@Test( expectedExceptions = ValidationException.class, expectedExceptionsMessageRegExp = "Value '.*' of attribute '" + MEMORY_LIMIT_ATTRIBUTE + "' in machine '.*' is illegal", dataProvider = "illegalMemoryAttributeValueProvider") public void shouldFailValidationIfMemoryLimitMachineAttributeHasIllegalValue( String attributeValue) throws Exception { final WorkspaceConfigDto config = createConfig(); EnvironmentDto env = config.getEnvironments().values().iterator().next(); MachineConfigDto machine = env.getMachines().values().iterator().next(); machine.getAttributes().put(MEMORY_LIMIT_ATTRIBUTE, attributeValue); wsValidator.validateConfig(config); }
@Test( expectedExceptions = ValidationException.class, expectedExceptionsMessageRegExp = "Value '.*' of attribute '" + MEMORY_REQUEST_ATTRIBUTE + "' in machine '.*' is illegal", dataProvider = "illegalMemoryAttributeValueProvider") public void shouldFailValidationIfMemoryRequestMachineAttributeHasIllegalValue( String attributeValue) throws Exception { final WorkspaceConfigDto config = createConfig(); EnvironmentDto env = config.getEnvironments().values().iterator().next(); MachineConfigDto machine = env.getMachines().values().iterator().next(); machine.getAttributes().put(MEMORY_REQUEST_ATTRIBUTE, attributeValue); wsValidator.validateConfig(config); }
@Test( expectedExceptions = ValidationException.class, expectedExceptionsMessageRegExp = "Workspace ws-name contains command with null or empty name") public void shouldFailValidationIfCommandNameIsEmpty() throws Exception { final WorkspaceConfigDto config = createConfig(); config.getCommands().get(0).withName(null); wsValidator.validateConfig(config); }
@Test( expectedExceptions = ValidationException.class, expectedExceptionsMessageRegExp = "Command line or content required for command '.*' in workspace '.*'\\.") public void shouldFailValidationIfCommandLineIsNull() throws Exception { final WorkspaceConfigDto config = createConfig(); config.getCommands().get(0).withCommandLine(null); wsValidator.validateConfig(config); }
@Test( expectedExceptions = ValidationException.class, expectedExceptionsMessageRegExp = "Command line or content required for command '.*' in workspace '.*'\\.") public void shouldFailValidationIfCommandLineIsEmpty() throws Exception { final WorkspaceConfigDto config = createConfig(); config.getCommands().get(0).withCommandLine(""); wsValidator.validateConfig(config); }
@Test( expectedExceptions = ValidationException.class, expectedExceptionsMessageRegExp = "Volume name '.*' in machine '.*' is invalid", dataProvider = "illegalVolumeNameProvider") public void shouldFailValidationIfVolumeNameDoesNotMatchCriteria(String volumeName) throws Exception { final WorkspaceConfigDto config = createConfig(); EnvironmentDto env = config.getEnvironments().values().iterator().next(); MachineConfigDto machine = env.getMachines().values().iterator().next(); machine.getVolumes().put(volumeName, newDto(VolumeDto.class).withPath("/path")); wsValidator.validateConfig(config); }
@Test( expectedExceptions = ValidationException.class, expectedExceptionsMessageRegExp = "Path of volume '.*' in machine '.*' is invalid. It should not be empty") public void shouldFailValidationIfVolumeValueIsEmpty() throws Exception { final WorkspaceConfigDto config = createConfig(); EnvironmentDto env = config.getEnvironments().values().iterator().next(); MachineConfigDto machine = env.getMachines().values().iterator().next(); machine.getVolumes().put("volume1", null); wsValidator.validateConfig(config); }
@Test( expectedExceptions = ValidationException.class, expectedExceptionsMessageRegExp = "Path of volume '.*' in machine '.*' is invalid. It should not be empty") public void shouldFailValidationIfVolumePathIsEmpty() throws Exception { final WorkspaceConfigDto config = createConfig(); EnvironmentDto env = config.getEnvironments().values().iterator().next(); MachineConfigDto machine = env.getMachines().values().iterator().next(); machine.getVolumes().put("volume1", newDto(VolumeDto.class).withPath("")); wsValidator.validateConfig(config); }
@Test( expectedExceptions = ValidationException.class, expectedExceptionsMessageRegExp = "Path of volume '.*' in machine '.*' is invalid. It should not be empty") public void shouldFailValidationIfVolumePathIsNull() throws Exception { final WorkspaceConfigDto config = createConfig(); EnvironmentDto env = config.getEnvironments().values().iterator().next(); MachineConfigDto machine = env.getMachines().values().iterator().next(); machine.getVolumes().put("volume1", newDto(VolumeDto.class).withPath(null)); wsValidator.validateConfig(config); }
@Test( expectedExceptions = ValidationException.class, expectedExceptionsMessageRegExp = "Path '.*' of volume '.*' in machine '.*' is invalid. It should be absolute") public void shouldFailValidationIfVolumePathIsNotAbsolute() throws Exception { final WorkspaceConfigDto config = createConfig(); EnvironmentDto env = config.getEnvironments().values().iterator().next(); MachineConfigDto machine = env.getMachines().values().iterator().next(); machine.getVolumes().put("volume1", newDto(VolumeDto.class).withPath("not/absolute/path")); wsValidator.validateConfig(config); }
@Test( expectedExceptions = ValidationException.class, expectedExceptionsMessageRegExp = ".*([pP]lugin.*[iI]nstaller|[iI]nstaller.*[pP]lugin).*") public void shouldFailValidationIfBothPluginsAndInstallersPresent() throws Exception { final WorkspaceConfigDto config = createConfig(); config.getAttributes().put(Constants.WORKSPACE_TOOLING_PLUGINS_ATTRIBUTE, "plugin1"); wsValidator.validateConfig(config); }
@Test( expectedExceptions = ValidationException.class, expectedExceptionsMessageRegExp = ".*([pP]lugin.*[iI]nstaller|[iI]nstaller.*[pP]lugin).*") public void shouldFailValidationIfBothEditorAndInstallersPresent() throws Exception { final WorkspaceConfigDto config = createConfig(); config.getAttributes().put(Constants.WORKSPACE_TOOLING_EDITOR_ATTRIBUTE, "editor1"); wsValidator.validateConfig(config); }