diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/test/TestSecurityClient.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/test/TestSecurityClient.java index abca7d63c9a98..970021fcbebe6 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/test/TestSecurityClient.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/test/TestSecurityClient.java @@ -48,6 +48,7 @@ import java.util.Optional; import java.util.stream.Collectors; +import static org.elasticsearch.common.xcontent.XContentParserUtils.ensureExpectedToken; import static org.elasticsearch.test.rest.ESRestTestCase.entityAsMap; public class TestSecurityClient { @@ -389,7 +390,7 @@ public TokenInvalidation invalidateTokens(String requestBody) throws IOException return new TokenInvalidation( ((Number) responseBody.get("invalidated_tokens")).intValue(), ((Number) responseBody.get("previously_invalidated_tokens")).intValue(), - errors == null ? List.of() : errors.stream().map(this::toException).toList() + errors == null ? List.of() : errors.stream().map(TestSecurityClient::toException).toList() ); } @@ -463,11 +464,10 @@ private XContentParser getParser(Response response) throws IOException { return XContentType.JSON.xContent().createParser(XContentParserConfiguration.EMPTY, responseBody); } - private ElasticsearchException toException(Map map) { - try { - return ElasticsearchException.fromXContent( - XContentType.JSON.xContent().createParser(XContentParserConfiguration.EMPTY, toJson(map)) - ); + private static ElasticsearchException toException(Map map) { + try (final XContentParser parser = XContentType.JSON.xContent().createParser(XContentParserConfiguration.EMPTY, toJson(map))) { + ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser); + return ElasticsearchException.fromXContent(parser); } catch (IOException e) { throw new RuntimeIoException(e); }