Skip to content

Commit c0bb260

Browse files
committed
Forward port changes from backport of #125562
The backport to `8.x` needed some changes to pass through CI; this commit forward-ports the relevant bits of those changes back into `main` to keep the branches aligned.
1 parent 20eb590 commit c0bb260

File tree

3 files changed

+24
-24
lines changed

3 files changed

+24
-24
lines changed

test/framework/src/main/java/org/elasticsearch/rest/RestResponseUtils.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,19 @@
1212
import org.elasticsearch.common.bytes.BytesReference;
1313
import org.elasticsearch.common.io.stream.BytesStreamOutput;
1414
import org.elasticsearch.core.CheckedConsumer;
15-
import org.elasticsearch.test.ESTestCase;
15+
import org.elasticsearch.xcontent.ToXContent;
16+
import org.elasticsearch.xcontent.XContentBuilder;
1617

1718
import java.io.IOException;
1819
import java.io.StringWriter;
1920
import java.io.Writer;
2021
import java.util.Iterator;
2122

23+
import static org.elasticsearch.test.ESTestCase.asInstanceOf;
24+
import static org.elasticsearch.test.ESTestCase.fail;
2225
import static org.elasticsearch.transport.BytesRefRecycler.NON_RECYCLING_INSTANCE;
26+
import static org.mockito.ArgumentMatchers.any;
27+
import static org.mockito.Mockito.when;
2328

2429
public class RestResponseUtils {
2530
private RestResponseUtils() {}
@@ -48,7 +53,7 @@ public static BytesReference getBodyContent(RestResponse restResponse) {
4853
out.flush();
4954
return out.bytes();
5055
} catch (Exception e) {
51-
return ESTestCase.fail(e);
56+
return fail(e);
5257
}
5358
}
5459

@@ -60,7 +65,18 @@ public static String getTextBodyContent(Iterator<CheckedConsumer<Writer, IOExcep
6065
writer.flush();
6166
return writer.toString();
6267
} catch (Exception e) {
63-
return ESTestCase.fail(e);
68+
return fail(e);
6469
}
6570
}
71+
72+
public static <T extends ToXContent> T setUpXContentMock(T mock) {
73+
try {
74+
when(mock.toXContent(any(), any())).thenAnswer(
75+
invocation -> asInstanceOf(XContentBuilder.class, invocation.getArgument(0)).startObject().endObject()
76+
);
77+
} catch (IOException e) {
78+
fail(e);
79+
}
80+
return mock;
81+
}
6682
}

x-pack/plugin/logstash/src/main/java/org/elasticsearch/xpack/logstash/rest/RestPutPipelineAction.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import org.elasticsearch.rest.ServerlessScope;
1717
import org.elasticsearch.rest.action.RestActionListener;
1818
import org.elasticsearch.xcontent.XContentParser;
19-
import org.elasticsearch.xcontent.XContentType;
2019
import org.elasticsearch.xpack.logstash.Pipeline;
2120
import org.elasticsearch.xpack.logstash.action.PutPipelineAction;
2221
import org.elasticsearch.xpack.logstash.action.PutPipelineRequest;
@@ -55,9 +54,9 @@ protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient cli
5554
new PutPipelineRequest(id, content, request.getXContentType()),
5655
new RestActionListener<>(restChannel) {
5756
@Override
58-
protected void processResponse(PutPipelineResponse putPipelineResponse) throws Exception {
57+
protected void processResponse(PutPipelineResponse putPipelineResponse) {
5958
channel.sendResponse(
60-
new RestResponse(putPipelineResponse.status(), XContentType.JSON.mediaType(), BytesArray.EMPTY)
59+
new RestResponse(putPipelineResponse.status(), RestResponse.TEXT_CONTENT_TYPE, BytesArray.EMPTY)
6160
);
6261
}
6362
}

x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/rest/inference/RestUpdateTrainedModelDeploymentActionTests.java

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,16 @@
1212
import org.elasticsearch.rest.RestRequest;
1313
import org.elasticsearch.test.rest.FakeRestRequest;
1414
import org.elasticsearch.test.rest.RestActionTestCase;
15-
import org.elasticsearch.xcontent.XContentBuilder;
1615
import org.elasticsearch.xcontent.XContentType;
1716
import org.elasticsearch.xpack.core.ml.action.CreateTrainedModelAssignmentAction;
1817
import org.elasticsearch.xpack.core.ml.action.UpdateTrainedModelDeploymentAction;
1918

20-
import java.io.IOException;
2119
import java.util.HashMap;
2220

21+
import static org.elasticsearch.rest.RestResponseUtils.setUpXContentMock;
2322
import static org.hamcrest.Matchers.equalTo;
2423
import static org.hamcrest.Matchers.instanceOf;
25-
import static org.mockito.ArgumentMatchers.any;
2624
import static org.mockito.Mockito.mock;
27-
import static org.mockito.Mockito.when;
2825

2926
public class RestUpdateTrainedModelDeploymentActionTests extends RestActionTestCase {
3027
public void testNumberOfAllocationInParam() {
@@ -37,7 +34,7 @@ public void testNumberOfAllocationInParam() {
3734
assertEquals(request.getNumberOfAllocations().intValue(), 5);
3835

3936
executeCalled.set(true);
40-
return newMockResponse();
37+
return setUpXContentMock(mock(CreateTrainedModelAssignmentAction.Response.class));
4138
}));
4239
var params = new HashMap<String, String>();
4340
params.put("number_of_allocations", "5");
@@ -60,7 +57,7 @@ public void testNumberOfAllocationInBody() {
6057
assertEquals(request.getNumberOfAllocations().intValue(), 6);
6158

6259
executeCalled.set(true);
63-
return newMockResponse();
60+
return setUpXContentMock(mock(CreateTrainedModelAssignmentAction.Response.class));
6461
}));
6562

6663
final String content = """
@@ -73,16 +70,4 @@ public void testNumberOfAllocationInBody() {
7370
dispatchRequest(inferenceRequest);
7471
assertThat(executeCalled.get(), equalTo(true));
7572
}
76-
77-
private static CreateTrainedModelAssignmentAction.Response newMockResponse() {
78-
final var response = mock(CreateTrainedModelAssignmentAction.Response.class);
79-
try {
80-
when(response.toXContent(any(), any())).thenAnswer(
81-
invocation -> asInstanceOf(XContentBuilder.class, invocation.getArgument(0)).startObject().endObject()
82-
);
83-
} catch (IOException e) {
84-
fail(e);
85-
}
86-
return response;
87-
}
8873
}

0 commit comments

Comments
 (0)