Skip to content

Commit 0a9c3ae

Browse files
authored
Remove UpdateSettingsTestHelper class (#32557)
* Remove UpdateSettingsTestHelper class By making the `settings()` method public on `UpdateSettingsRequest` (I think it should have been in the first place) we can get rid of this class entirely. Mock response objects are now constructed by parsing JSON without making the constructor public. Relates to #29823
1 parent 7ea7dd8 commit 0a9c3ae

File tree

5 files changed

+36
-63
lines changed

5 files changed

+36
-63
lines changed

server/src/main/java/org/elasticsearch/action/admin/indices/settings/put/UpdateSettingsRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public String[] indices() {
8888
return indices;
8989
}
9090

91-
Settings settings() {
91+
public Settings settings() {
9292
return settings;
9393
}
9494

server/src/main/java/org/elasticsearch/action/admin/indices/settings/put/UpdateSettingsResponse.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public class UpdateSettingsResponse extends AcknowledgedResponse {
3030
UpdateSettingsResponse() {
3131
}
3232

33-
UpdateSettingsResponse(boolean acknowledged) {
33+
public UpdateSettingsResponse(boolean acknowledged) {
3434
super(acknowledged);
3535
}
3636

x-pack/plugin/core/src/test/java/org/elasticsearch/action/admin/indices/settings/put/UpdateSettingsTestHelper.java

Lines changed: 0 additions & 48 deletions
This file was deleted.

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/SetSingleNodeAllocateStepTests.java

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import org.elasticsearch.action.ActionListener;
1111
import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest;
1212
import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsResponse;
13-
import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsTestHelper;
1413
import org.elasticsearch.client.AdminClient;
1514
import org.elasticsearch.client.Client;
1615
import org.elasticsearch.client.IndicesAdminClient;
@@ -39,8 +38,13 @@
3938
import org.mockito.invocation.InvocationOnMock;
4039
import org.mockito.stubbing.Answer;
4140

41+
import java.io.IOException;
4242
import java.util.HashSet;
4343
import java.util.Set;
44+
import java.util.stream.Collectors;
45+
46+
import static org.hamcrest.Matchers.anyOf;
47+
import static org.hamcrest.Matchers.equalTo;
4448

4549
public class SetSingleNodeAllocateStepTests extends AbstractStepTestCase<SetSingleNodeAllocateStep> {
4650

@@ -80,7 +84,18 @@ protected SetSingleNodeAllocateStep copyInstance(SetSingleNodeAllocateStep insta
8084
return new SetSingleNodeAllocateStep(instance.getKey(), instance.getNextStepKey(), client);
8185
}
8286

83-
public void testPerformActionNoAttrs() {
87+
public static void assertSettingsRequestContainsValueFrom(UpdateSettingsRequest request, String settingsKey,
88+
Set<String> acceptableValues, boolean assertOnlyKeyInSettings,
89+
String... expectedIndices) {
90+
assertNotNull(request);
91+
assertArrayEquals(expectedIndices, request.indices());
92+
assertThat(request.settings().get(settingsKey), anyOf(acceptableValues.stream().map(e -> equalTo(e)).collect(Collectors.toList())));
93+
if (assertOnlyKeyInSettings) {
94+
assertEquals(1, request.settings().size());
95+
}
96+
}
97+
98+
public void testPerformActionNoAttrs() throws IOException {
8499
IndexMetaData indexMetaData = IndexMetaData.builder(randomAlphaOfLength(10)).settings(settings(Version.CURRENT))
85100
.numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build();
86101
Index index = indexMetaData.getIndex();
@@ -101,7 +116,7 @@ public void testPerformActionNoAttrs() {
101116
assertNodeSelected(indexMetaData, index, validNodeNames, nodes);
102117
}
103118

104-
public void testPerformActionAttrsAllNodesValid() {
119+
public void testPerformActionAttrsAllNodesValid() throws IOException {
105120
int numAttrs = randomIntBetween(1, 10);
106121
String[][] validAttrs = new String[numAttrs][2];
107122
for (int i = 0; i < numAttrs; i++) {
@@ -132,7 +147,7 @@ public void testPerformActionAttrsAllNodesValid() {
132147
assertNodeSelected(indexMetaData, index, validNodeNames, nodes);
133148
}
134149

135-
public void testPerformActionAttrsSomeNodesValid() {
150+
public void testPerformActionAttrsSomeNodesValid() throws IOException {
136151
String[] validAttr = new String[] { "box_type", "valid" };
137152
String[] invalidAttr = new String[] { "box_type", "not_valid" };
138153
Settings.Builder indexSettings = settings(Version.CURRENT);
@@ -237,7 +252,7 @@ public Void answer(InvocationOnMock invocation) throws Throwable {
237252
UpdateSettingsRequest request = (UpdateSettingsRequest) invocation.getArguments()[0];
238253
@SuppressWarnings("unchecked")
239254
ActionListener<UpdateSettingsResponse> listener = (ActionListener<UpdateSettingsResponse>) invocation.getArguments()[1];
240-
UpdateSettingsTestHelper.assertSettingsRequestContainsValueFrom(request,
255+
assertSettingsRequestContainsValueFrom(request,
241256
IndexMetaData.INDEX_ROUTING_REQUIRE_GROUP_SETTING.getKey() + "_name", validNodeNames, true,
242257
indexMetaData.getIndex().getName());
243258
listener.onFailure(exception);
@@ -325,7 +340,8 @@ public void onFailure(Exception e) {
325340
Mockito.verifyZeroInteractions(client);
326341
}
327342

328-
private void assertNodeSelected(IndexMetaData indexMetaData, Index index, Set<String> validNodeNames, DiscoveryNodes.Builder nodes) {
343+
private void assertNodeSelected(IndexMetaData indexMetaData, Index index,
344+
Set<String> validNodeNames, DiscoveryNodes.Builder nodes) throws IOException {
329345
ImmutableOpenMap.Builder<String, IndexMetaData> indices = ImmutableOpenMap.<String, IndexMetaData> builder().fPut(index.getName(),
330346
indexMetaData);
331347
IndexRoutingTable.Builder indexRoutingTable = IndexRoutingTable.builder(index)
@@ -340,17 +356,18 @@ private void assertNodeSelected(IndexMetaData indexMetaData, Index index, Set<St
340356

341357
Mockito.when(client.admin()).thenReturn(adminClient);
342358
Mockito.when(adminClient.indices()).thenReturn(indicesClient);
359+
343360
Mockito.doAnswer(new Answer<Void>() {
344361

345362
@Override
346363
public Void answer(InvocationOnMock invocation) throws Throwable {
347364
UpdateSettingsRequest request = (UpdateSettingsRequest) invocation.getArguments()[0];
348365
@SuppressWarnings("unchecked")
349366
ActionListener<UpdateSettingsResponse> listener = (ActionListener<UpdateSettingsResponse>) invocation.getArguments()[1];
350-
UpdateSettingsTestHelper.assertSettingsRequestContainsValueFrom(request,
367+
assertSettingsRequestContainsValueFrom(request,
351368
IndexMetaData.INDEX_ROUTING_REQUIRE_GROUP_SETTING.getKey() + "_name", validNodeNames, true,
352369
indexMetaData.getIndex().getName());
353-
listener.onResponse(UpdateSettingsTestHelper.createMockResponse(true));
370+
listener.onResponse(new UpdateSettingsResponse(true));
354371
return null;
355372
}
356373

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/indexlifecycle/UpdateSettingsStepTests.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import org.elasticsearch.action.ActionListener;
1212
import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest;
1313
import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsResponse;
14-
import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsTestHelper;
1514
import org.elasticsearch.client.AdminClient;
1615
import org.elasticsearch.client.Client;
1716
import org.elasticsearch.client.IndicesAdminClient;
@@ -24,6 +23,8 @@
2423
import org.mockito.invocation.InvocationOnMock;
2524
import org.mockito.stubbing.Answer;
2625

26+
import static org.hamcrest.Matchers.equalTo;
27+
2728
public class UpdateSettingsStepTests extends AbstractStepTestCase<UpdateSettingsStep> {
2829

2930
private Client client;
@@ -70,7 +71,7 @@ public UpdateSettingsStep copyInstance(UpdateSettingsStep instance) {
7071
return new UpdateSettingsStep(instance.getKey(), instance.getNextStepKey(), instance.getClient(), instance.getSettings());
7172
}
7273

73-
public void testPerformAction() {
74+
public void testPerformAction() throws Exception {
7475
IndexMetaData indexMetaData = IndexMetaData.builder(randomAlphaOfLength(10)).settings(settings(Version.CURRENT))
7576
.numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)).build();
7677

@@ -81,15 +82,17 @@ public void testPerformAction() {
8182

8283
Mockito.when(client.admin()).thenReturn(adminClient);
8384
Mockito.when(adminClient.indices()).thenReturn(indicesClient);
85+
8486
Mockito.doAnswer(new Answer<Void>() {
8587

8688
@Override
8789
public Void answer(InvocationOnMock invocation) throws Throwable {
8890
UpdateSettingsRequest request = (UpdateSettingsRequest) invocation.getArguments()[0];
8991
@SuppressWarnings("unchecked")
9092
ActionListener<UpdateSettingsResponse> listener = (ActionListener<UpdateSettingsResponse>) invocation.getArguments()[1];
91-
UpdateSettingsTestHelper.assertSettingsRequest(request, step.getSettings(), indexMetaData.getIndex().getName());
92-
listener.onResponse(UpdateSettingsTestHelper.createMockResponse(true));
93+
assertThat(request.settings(), equalTo(step.getSettings()));
94+
assertThat(request.indices(), equalTo(new String[] {indexMetaData.getIndex().getName()}));
95+
listener.onResponse(new UpdateSettingsResponse(true));
9396
return null;
9497
}
9598

@@ -135,7 +138,8 @@ public Void answer(InvocationOnMock invocation) throws Throwable {
135138
UpdateSettingsRequest request = (UpdateSettingsRequest) invocation.getArguments()[0];
136139
@SuppressWarnings("unchecked")
137140
ActionListener<UpdateSettingsResponse> listener = (ActionListener<UpdateSettingsResponse>) invocation.getArguments()[1];
138-
UpdateSettingsTestHelper.assertSettingsRequest(request, step.getSettings(), indexMetaData.getIndex().getName());
141+
assertThat(request.settings(), equalTo(step.getSettings()));
142+
assertThat(request.indices(), equalTo(new String[] {indexMetaData.getIndex().getName()}));
139143
listener.onFailure(exception);
140144
return null;
141145
}

0 commit comments

Comments
 (0)