[ML] Move job group validation after parsing (elastic/x-pack-elasticsearch#2207)

Validating job groups during parsing results into
the validation error being wrapped into a parse
exception. The UI then does not display the cause of the
error. Finally, it is conceptually not a parse error, so
it belongs outside the parsing phase.

Original commit: elastic/x-pack-elasticsearch@a03f002bdc
This commit is contained in:
Dimitris Athanasiou 2017-08-08 15:59:04 +01:00 committed by GitHub
parent cff3418e96
commit 2864078da2
2 changed files with 14 additions and 7 deletions

View File

@ -694,11 +694,6 @@ public class Job extends AbstractDiffable<Job> implements Writeable, ToXContentO
public void setGroups(List<String> groups) { public void setGroups(List<String> groups) {
this.groups = groups == null ? Collections.emptyList() : groups; this.groups = groups == null ? Collections.emptyList() : groups;
for (String group : this.groups) {
if (MlStrings.isValidId(group) == false) {
throw new IllegalArgumentException(Messages.getMessage(Messages.INVALID_GROUP, group));
}
}
} }
public Date getCreateTime() { public Date getCreateTime() {
@ -994,6 +989,8 @@ public class Job extends AbstractDiffable<Job> implements Writeable, ToXContentO
throw new IllegalArgumentException(Messages.getMessage(Messages.JOB_CONFIG_ID_TOO_LONG, MAX_JOB_ID_LENGTH)); throw new IllegalArgumentException(Messages.getMessage(Messages.JOB_CONFIG_ID_TOO_LONG, MAX_JOB_ID_LENGTH));
} }
validateGroups();
// Results index name not specified in user input means use the default, so is acceptable in this validation // Results index name not specified in user input means use the default, so is acceptable in this validation
if (!Strings.isNullOrEmpty(resultsIndexName) && !MlStrings.isValidId(resultsIndexName)) { if (!Strings.isNullOrEmpty(resultsIndexName) && !MlStrings.isValidId(resultsIndexName)) {
throw new IllegalArgumentException( throw new IllegalArgumentException(
@ -1003,6 +1000,14 @@ public class Job extends AbstractDiffable<Job> implements Writeable, ToXContentO
// Creation time is NOT required in user input, hence validated only on build // Creation time is NOT required in user input, hence validated only on build
} }
private void validateGroups() {
for (String group : this.groups) {
if (MlStrings.isValidId(group) == false) {
throw new IllegalArgumentException(Messages.getMessage(Messages.INVALID_GROUP, group));
}
}
}
/** /**
* Builds a job with the given {@code createTime} and the current version. * Builds a job with the given {@code createTime} and the current version.
* This should be used when a new job is created as opposed to {@link #build()}. * This should be used when a new job is created as opposed to {@link #build()}.

View File

@ -470,15 +470,17 @@ public class JobTests extends AbstractSerializingTestCase<Job> {
public void testEmptyGroup() { public void testEmptyGroup() {
Job.Builder builder = buildJobBuilder("foo"); Job.Builder builder = buildJobBuilder("foo");
builder.setGroups(Arrays.asList("foo-group", ""));
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, IllegalArgumentException e = expectThrows(IllegalArgumentException.class,
() -> builder.setGroups(Arrays.asList("foo-group", ""))); () -> builder.build());
assertThat(e.getMessage(), containsString("Invalid group id ''")); assertThat(e.getMessage(), containsString("Invalid group id ''"));
} }
public void testInvalidGroup() { public void testInvalidGroup() {
Job.Builder builder = buildJobBuilder("foo"); Job.Builder builder = buildJobBuilder("foo");
builder.setGroups(Arrays.asList("foo-group", "$$$"));
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, IllegalArgumentException e = expectThrows(IllegalArgumentException.class,
() -> builder.setGroups(Arrays.asList("foo-group", "$$$"))); () -> builder.build());
assertThat(e.getMessage(), containsString("Invalid group id '$$$'")); assertThat(e.getMessage(), containsString("Invalid group id '$$$'"));
} }