Skip to content

Commit 05bf406

Browse files
committed
Extract tests to individual methods
1 parent 47ea964 commit 05bf406

File tree

1 file changed

+80
-74
lines changed

1 file changed

+80
-74
lines changed

server/src/test/java/org/elasticsearch/common/IndexNameGeneratorTests.java

Lines changed: 80 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -74,88 +74,94 @@ public void testGenerateValidIndexSuffix() {
7474
}
7575
}
7676

77-
public void testValidateGeneratedIndexName() {
78-
{
79-
String generatedIndexName = generateValidIndexName(randomAlphaOfLengthBetween(5, 10), randomAlphaOfLengthBetween(5, 150));
80-
assertThat(validateGeneratedIndexName(generatedIndexName, createState(generatedIndexName, false, false, false)), nullValue());
81-
}
77+
public void testValidateGeneratedIndexName_valid() {
78+
String generatedIndexName = generateValidIndexName(randomAlphaOfLengthBetween(5, 10), randomAlphaOfLengthBetween(5, 150));
79+
assertThat(
80+
validateGeneratedIndexName(generatedIndexName, createProjectState(generatedIndexName, false, false, false)),
81+
nullValue()
82+
);
83+
}
8284

83-
{
84-
// index name is validated (invalid chars etc)
85-
String generatedIndexName = generateValidIndexName("_prefix-", randomAlphaOfLengthBetween(5, 150));
86-
assertThat(
87-
validateGeneratedIndexName(generatedIndexName, createState(generatedIndexName, false, false, false)).validationErrors(),
88-
containsInAnyOrder("Invalid index name [" + generatedIndexName + "], must not start with '_', '-', or '+'")
89-
);
90-
}
85+
public void testValidateGeneratedIndexName_invalidChars() {
86+
// index name is validated (invalid chars etc)
87+
String generatedIndexName = generateValidIndexName("_prefix-", randomAlphaOfLengthBetween(5, 150));
88+
assertThat(
89+
validateGeneratedIndexName(generatedIndexName, createProjectState(generatedIndexName, false, false, false)).validationErrors(),
90+
containsInAnyOrder("Invalid index name [" + generatedIndexName + "], must not start with '_', '-', or '+'")
91+
);
92+
}
9193

92-
{
93-
// index name is validated (invalid chars etc)
94-
String generatedIndexName = generateValidIndexName("shrink-", "shrink-indexName-random###");
95-
assertThat(
96-
validateGeneratedIndexName(generatedIndexName, createState(generatedIndexName, false, false, false)).validationErrors(),
97-
containsInAnyOrder("Invalid index name [" + generatedIndexName + "], must not contain '#'")
98-
);
99-
}
94+
public void testValidateGeneratedIndexName_invalidPound() {
95+
// index name is validated (invalid chars etc)
96+
String generatedIndexName = generateValidIndexName("shrink-", "shrink-indexName-random###");
97+
assertThat(
98+
validateGeneratedIndexName(generatedIndexName, createProjectState(generatedIndexName, false, false, false)).validationErrors(),
99+
containsInAnyOrder("Invalid index name [" + generatedIndexName + "], must not contain '#'")
100+
);
101+
}
100102

101-
{
102-
// generated index already exists as a standalone index
103-
String generatedIndexName = generateValidIndexName(randomAlphaOfLengthBetween(5, 10), randomAlphaOfLengthBetween(5, 150));
104-
ActionRequestValidationException validationException = validateGeneratedIndexName(
105-
generatedIndexName,
106-
createState(generatedIndexName, true, false, false)
107-
);
108-
assertThat(validationException, notNullValue());
109-
assertThat(
110-
validationException.validationErrors(),
111-
containsInAnyOrder("the index name we generated [" + generatedIndexName + "] already exists")
112-
);
113-
}
103+
public void testValidateGeneratedIndexName_alreadyExistsAsIndex() {
104+
// generated index already exists as a standalone index
105+
String generatedIndexName = generateValidIndexName(randomAlphaOfLengthBetween(5, 10), randomAlphaOfLengthBetween(5, 150));
106+
ActionRequestValidationException validationException = validateGeneratedIndexName(
107+
generatedIndexName,
108+
createProjectState(generatedIndexName, true, false, false)
109+
);
110+
assertThat(validationException, notNullValue());
111+
assertThat(
112+
validationException.validationErrors(),
113+
containsInAnyOrder("the index name we generated [" + generatedIndexName + "] already exists")
114+
);
115+
}
114116

115-
{
116-
// generated index name already exists as an index (cluster state routing table is also populated)
117-
String generatedIndexName = generateValidIndexName(randomAlphaOfLengthBetween(5, 10), randomAlphaOfLengthBetween(5, 150));
118-
ActionRequestValidationException validationException = validateGeneratedIndexName(
119-
generatedIndexName,
120-
createState(generatedIndexName, true, true, false)
121-
);
122-
assertThat(validationException, notNullValue());
123-
assertThat(
124-
validationException.validationErrors(),
125-
containsInAnyOrder("the index name we generated [" + generatedIndexName + "] already exists")
126-
);
127-
}
117+
public void testValidateGeneratedIndexName_alreadyExistsAlsoRoutingTable() {
118+
// generated index name already exists as an index (cluster state routing table is also populated)
119+
String generatedIndexName = generateValidIndexName(randomAlphaOfLengthBetween(5, 10), randomAlphaOfLengthBetween(5, 150));
120+
ActionRequestValidationException validationException = validateGeneratedIndexName(
121+
generatedIndexName,
122+
createProjectState(generatedIndexName, true, true, false)
123+
);
124+
assertThat(validationException, notNullValue());
125+
assertThat(
126+
validationException.validationErrors(),
127+
containsInAnyOrder("the index name we generated [" + generatedIndexName + "] already exists")
128+
);
129+
}
128130

129-
{
130-
// generated index name already exists as an index but only in routing table
131-
String generatedIndexName = generateValidIndexName(randomAlphaOfLengthBetween(5, 10), randomAlphaOfLengthBetween(5, 150));
132-
ActionRequestValidationException validationException = validateGeneratedIndexName(
133-
generatedIndexName,
134-
createState(generatedIndexName, false, true, false)
135-
);
136-
assertThat(validationException, notNullValue());
137-
assertThat(
138-
validationException.validationErrors(),
139-
containsInAnyOrder("the index name we generated [" + generatedIndexName + "] already exists")
140-
);
141-
}
131+
public void testValidateGeneratedIndexName_alreadyExistsOnlyRoutingTable() {
132+
// generated index name already exists as an index but only in routing table
133+
String generatedIndexName = generateValidIndexName(randomAlphaOfLengthBetween(5, 10), randomAlphaOfLengthBetween(5, 150));
134+
ActionRequestValidationException validationException = validateGeneratedIndexName(
135+
generatedIndexName,
136+
createProjectState(generatedIndexName, false, true, false)
137+
);
138+
assertThat(validationException, notNullValue());
139+
assertThat(
140+
validationException.validationErrors(),
141+
containsInAnyOrder("the index name we generated [" + generatedIndexName + "] already exists")
142+
);
143+
}
142144

143-
{
144-
// generated index name already exists as an alias to another index
145-
String generatedIndexName = generateValidIndexName(randomAlphaOfLengthBetween(5, 10), randomAlphaOfLengthBetween(5, 150));
146-
ActionRequestValidationException validationException = validateGeneratedIndexName(
147-
generatedIndexName,
148-
createState(generatedIndexName, true, false, true)
149-
);
150-
assertThat(validationException, notNullValue());
151-
assertThat(
152-
validationException.validationErrors(),
153-
containsInAnyOrder("the index name we generated [" + generatedIndexName + "] already exists as alias")
154-
);
155-
}
145+
public void testValidateGeneratedIndexName_alreadyExistsAsAlias() {
146+
// generated index name already exists as an alias to another index
147+
String generatedIndexName = generateValidIndexName(randomAlphaOfLengthBetween(5, 10), randomAlphaOfLengthBetween(5, 150));
148+
ActionRequestValidationException validationException = validateGeneratedIndexName(
149+
generatedIndexName,
150+
createProjectState(generatedIndexName, true, false, true)
151+
);
152+
assertThat(validationException, notNullValue());
153+
assertThat(
154+
validationException.validationErrors(),
155+
containsInAnyOrder("the index name we generated [" + generatedIndexName + "] already exists as alias")
156+
);
156157
}
157158

158-
private ProjectState createState(String generatedName, boolean addIndexToMetadata, boolean addIndexToRoutingTable, boolean addAlias) {
159+
private ProjectState createProjectState(
160+
String generatedName,
161+
boolean addIndexToMetadata,
162+
boolean addIndexToRoutingTable,
163+
boolean addAlias
164+
) {
159165
final var indexName = addAlias ? randomAlphaOfLengthBetween(10, 30) : generatedName;
160166
final var indexMetadataBuilder = IndexMetadata.builder(indexName)
161167
.settings(settings(IndexVersion.current()))

0 commit comments

Comments
 (0)