|
10 | 10 | import org.elasticsearch.common.io.stream.Writeable; |
11 | 11 | import org.elasticsearch.test.AbstractWireSerializingTestCase; |
12 | 12 | import org.elasticsearch.xpack.core.ml.action.PutTrainedModelAliasAction.Request; |
13 | | -import org.junit.Before; |
| 13 | + |
| 14 | +import java.util.HashSet; |
| 15 | +import java.util.List; |
| 16 | +import java.util.Locale; |
| 17 | +import java.util.Set; |
14 | 18 |
|
15 | 19 | import static org.hamcrest.Matchers.containsString; |
| 20 | +import static org.hamcrest.Matchers.hasSize; |
16 | 21 | import static org.hamcrest.Matchers.not; |
17 | 22 | import static org.hamcrest.Matchers.nullValue; |
18 | 23 |
|
19 | 24 | public class PutTrainedModelAliasActionRequestTests extends AbstractWireSerializingTestCase<Request> { |
20 | 25 |
|
21 | | - private String modelAlias; |
22 | | - |
23 | | - @Before |
24 | | - public void setupModelAlias() { |
25 | | - modelAlias = randomAlphaOfLength(10); |
26 | | - } |
| 26 | + private static final Set<String> INVALID_CHARACTERS = Set.of( |
| 27 | + "!", |
| 28 | + "@", |
| 29 | + "#", |
| 30 | + "$", |
| 31 | + "%", |
| 32 | + "^", |
| 33 | + "&", |
| 34 | + "*", |
| 35 | + "(", |
| 36 | + ")", |
| 37 | + "+", |
| 38 | + "=", |
| 39 | + "[", |
| 40 | + "]", |
| 41 | + "{", |
| 42 | + "}", |
| 43 | + "|", |
| 44 | + ";", |
| 45 | + ":", |
| 46 | + "'", |
| 47 | + "\"", |
| 48 | + ",", |
| 49 | + "<", |
| 50 | + ">", |
| 51 | + "/", |
| 52 | + "?" |
| 53 | + ); |
27 | 54 |
|
28 | 55 | @Override |
29 | 56 | protected Request createTestInstance() { |
30 | | - return new Request(modelAlias, randomAlphaOfLength(10), randomBoolean()); |
| 57 | + return new Request(randomAlphaOfLength(10), randomAlphaOfLength(10), randomBoolean()); |
31 | 58 | } |
32 | 59 |
|
33 | 60 | @Override |
34 | 61 | protected Request mutateInstance(Request instance) { |
35 | | - return null;// TODO implement https://github.com/elastic/elasticsearch/issues/25929 |
| 62 | + String modelAlias = instance.getModelAlias(); |
| 63 | + String modelId = instance.getModelId(); |
| 64 | + boolean reassign = instance.isReassign(); |
| 65 | + int value = randomInt(2); |
| 66 | + return switch (value) { |
| 67 | + case 0 -> new Request(randomValueOtherThan(modelAlias, () -> randomAlphaOfLength(10)), modelId, reassign); |
| 68 | + case 1 -> new Request(modelAlias, randomValueOtherThan(modelId, () -> randomAlphaOfLength(10)), reassign); |
| 69 | + case 2 -> new Request(modelAlias, modelId, reassign == false); |
| 70 | + default -> throw new IllegalStateException("Unexpected value " + value); |
| 71 | + }; |
36 | 72 | } |
37 | 73 |
|
38 | 74 | @Override |
39 | 75 | protected Writeable.Reader<Request> instanceReader() { |
40 | 76 | return Request::new; |
41 | 77 | } |
42 | 78 |
|
43 | | - public void testCtor() { |
| 79 | + public void testConstructor() { |
44 | 80 | expectThrows(Exception.class, () -> new Request(null, randomAlphaOfLength(10), randomBoolean())); |
45 | 81 | expectThrows(Exception.class, () -> new Request(randomAlphaOfLength(10), null, randomBoolean())); |
46 | 82 | } |
47 | 83 |
|
48 | 84 | public void testValidate() { |
49 | | - |
50 | | - { // model_alias equal to model Id |
51 | | - ActionRequestValidationException ex = new Request("foo", "foo", randomBoolean()).validate(); |
52 | | - assertThat(ex, not(nullValue())); |
53 | | - assertThat(ex.getMessage(), containsString("model_alias [foo] cannot equal model_id [foo]")); |
54 | | - } |
55 | | - { // model_alias cannot end in numbers |
56 | | - modelAlias = randomAlphaOfLength(10) + randomIntBetween(0, Integer.MAX_VALUE); |
| 85 | + List<String> validAliases = List.of("a", "1", "2b", "c-3d", "e_4f", "g.5h"); |
| 86 | + for (String modelAlias : validAliases) { |
57 | 87 | ActionRequestValidationException ex = new Request(modelAlias, "foo", randomBoolean()).validate(); |
58 | | - assertThat(ex, not(nullValue())); |
59 | | - assertThat( |
60 | | - ex.getMessage(), |
61 | | - containsString( |
62 | | - "can contain lowercase alphanumeric (a-z and 0-9), hyphens or underscores; " |
63 | | - + "must start with alphanumeric and cannot end with numbers" |
64 | | - ) |
65 | | - ); |
| 88 | + assertThat("For alias [" + modelAlias + "]", ex, nullValue()); |
66 | 89 | } |
67 | 90 | } |
68 | 91 |
|
| 92 | + public void testValidate_modelAliasEqualToModelId() { |
| 93 | + ActionRequestValidationException ex = new Request("foo", "foo", randomBoolean()).validate(); |
| 94 | + assertThat(ex, not(nullValue())); |
| 95 | + assertThat(ex.getMessage(), containsString("model_alias [foo] cannot equal model_id [foo]")); |
| 96 | + } |
| 97 | + |
| 98 | + public void testValidate_modelAliasEqualToModelIdWithInvalidCharacter() { |
| 99 | + String modelAlias = "foo" + randomFrom(INVALID_CHARACTERS); |
| 100 | + ActionRequestValidationException ex = new Request(modelAlias, modelAlias, randomBoolean()).validate(); |
| 101 | + assertThat("For alias [" + modelAlias + "]", ex, not(nullValue())); |
| 102 | + assertThat(ex.validationErrors(), hasSize(2)); |
| 103 | + assertThat(ex.getMessage(), containsString("model_alias [" + modelAlias + "] cannot equal model_id [" + modelAlias + "]")); |
| 104 | + assertThat( |
| 105 | + ex.getMessage(), |
| 106 | + containsString( |
| 107 | + "can contain lowercase alphanumeric (a-z and 0-9), hyphens or underscores; " |
| 108 | + + "must start with alphanumeric and cannot end with numbers" |
| 109 | + ) |
| 110 | + ); |
| 111 | + } |
| 112 | + |
| 113 | + public void testValidate_modelAliasContainsUppercase() { |
| 114 | + List<String> invalidAliases = List.of("Start", "midDle", "enD"); |
| 115 | + for (String invalidAlias : invalidAliases) { |
| 116 | + assertInvalidAlias(invalidAlias); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + public void testValidate_modelAliasEndsWithNumber() { |
| 121 | + String modelAlias = (randomAlphaOfLength(10) + randomIntBetween(0, Integer.MAX_VALUE)).toLowerCase(Locale.ROOT); |
| 122 | + assertInvalidAlias(modelAlias); |
| 123 | + } |
| 124 | + |
| 125 | + public void testValidate_modelAliasStartsWithInvalidCharacter() { |
| 126 | + Set<String> invalidFirstCharacters = new HashSet<>(INVALID_CHARACTERS); |
| 127 | + // '-', '_' and '.' are not valid as the first character |
| 128 | + invalidFirstCharacters.addAll(Set.of("-", "_", ".")); |
| 129 | + String modelAlias = (randomFrom(invalidFirstCharacters) + randomAlphaOfLength(10)).toLowerCase(Locale.ROOT); |
| 130 | + assertInvalidAlias(modelAlias); |
| 131 | + } |
| 132 | + |
| 133 | + public void testValidate_modelAliasContainsInvalidCharacter() { |
| 134 | + String modelAlias = (randomAlphaOfLength(5) + randomFrom(INVALID_CHARACTERS) + randomAlphaOfLength(5)).toLowerCase(Locale.ROOT); |
| 135 | + assertInvalidAlias(modelAlias); |
| 136 | + } |
| 137 | + |
| 138 | + public void testValidate_modelAliasEndsWithInvalidCharacter() { |
| 139 | + Set<String> invalidLastCharacters = new HashSet<>(INVALID_CHARACTERS); |
| 140 | + // '-', '_' and '.' are not valid as the last character |
| 141 | + invalidLastCharacters.addAll(Set.of("-", "_", ".")); |
| 142 | + String modelAlias = (randomAlphaOfLength(10) + randomFrom(invalidLastCharacters)).toLowerCase(Locale.ROOT); |
| 143 | + assertInvalidAlias(modelAlias); |
| 144 | + } |
| 145 | + |
| 146 | + private static void assertInvalidAlias(String modelAlias) { |
| 147 | + ActionRequestValidationException ex = new Request(modelAlias, "foo", randomBoolean()).validate(); |
| 148 | + assertThat("For alias [" + modelAlias + "]", ex, not(nullValue())); |
| 149 | + assertThat( |
| 150 | + ex.getMessage(), |
| 151 | + containsString( |
| 152 | + "can contain lowercase alphanumeric (a-z and 0-9), hyphens or underscores; " |
| 153 | + + "must start with alphanumeric and cannot end with numbers" |
| 154 | + ) |
| 155 | + ); |
| 156 | + } |
69 | 157 | } |
0 commit comments