From dd017eac7846b7accac3e2cf11f116fad4cf85ac Mon Sep 17 00:00:00 2001 From: Jim Ferenczi Date: Tue, 17 Sep 2024 18:39:50 +0100 Subject: [PATCH] fix test --- .../index/mapper/PatchSourceMapperTests.java | 22 +++++++++++++------ .../index/mapper/SourceFieldMapperTests.java | 15 +++++++------ 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/server/src/test/java/org/elasticsearch/index/mapper/PatchSourceMapperTests.java b/server/src/test/java/org/elasticsearch/index/mapper/PatchSourceMapperTests.java index 2ee6b492f6e2b..eca1f5ad6557f 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/PatchSourceMapperTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/PatchSourceMapperTests.java @@ -1,9 +1,10 @@ /* * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License - * 2.0 and the Server Side Public License, v 1; you may not use this file except - * in compliance with, at your election, the Elastic License 2.0 or the Server - * Side Public License, v 1. + * or more contributor license agreements. Licensed under the "Elastic License + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side + * Public License v 1"; you may not use this file except in compliance with, at + * your election, the "Elastic License 2.0", the "GNU Affero General Public + * License v3.0 only", or the "Server Side Public License, v 1". */ package org.elasticsearch.index.mapper; @@ -65,6 +66,11 @@ public void testPatchSourceFlatWithCopyTo() throws IOException { b.startObject("another_field"); b.field("type", "keyword"); b.endObject(); + + // another_field + b.startObject("extra_field"); + b.field("type", "keyword"); + b.endObject(); })); assertSourcePatch(mapperService, Map.of("field", "key1")); } @@ -94,7 +100,7 @@ public void testPatchSourceFlatMulti() throws IOException { ) ); assertThat(exc.status(), equalTo(RestStatus.BAD_REQUEST)); - assertThat(exc.getDetailedMessage(), containsString("doesn't support patching multiple values")); + assertThat(exc.getDetailedMessage(), containsString("[field] does not support patching multiple values")); } public void testPatchSourceObject() throws IOException { @@ -394,7 +400,9 @@ protected BinaryPatchSourceFieldMapper(String simpleName, BinaryFieldMapper dele @Override protected void parseCreateField(DocumentParserContext context) throws IOException { - context.addSourceFieldPatch(this, context.parser().getTokenLocation()); + if (context.isWithinCopyTo() == false) { + context.addSourceFieldPatch(this, context.parser().getTokenLocation()); + } XContentBuilder b = XContentBuilder.builder(context.parser().contentType().xContent()); b.copyCurrentStructure(context.parser()); context.doc().add(new BinaryDocValuesField(fullPath(), BytesReference.bytes(b).toBytesRef())); @@ -426,7 +434,7 @@ protected void writeValue(XContentBuilder b, BytesRef value) throws IOException @Override protected SourceLoader.PatchFieldLoader patchFieldLoader() { - return new SourceLoader.SyntheticPatchFieldLoader(syntheticFieldLoader()); + return new SourceLoader.SyntheticPatchFieldLoader(syntheticSourceSupport().loader()); } } } diff --git a/server/src/test/java/org/elasticsearch/index/mapper/SourceFieldMapperTests.java b/server/src/test/java/org/elasticsearch/index/mapper/SourceFieldMapperTests.java index 916de79a66be2..6db779e13bd6e 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/SourceFieldMapperTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/SourceFieldMapperTests.java @@ -567,7 +567,7 @@ public void testWrongLocationPatchSourceInPostParse() throws Exception { XContentType.JSON ) ) { - DocumentParserContext context = new TestDocumentParserContext(MappingLookup.EMPTY, sourceToParse) { + DocumentParserContext context = new TestDocumentParserContext(mapperService.mappingLookup(), sourceToParse) { @Override public XContentParser parser() { return parser; @@ -576,7 +576,7 @@ public XContentParser parser() { var xContentLocation = new XContentLocation(0, 3); context.addSourceFieldPatch(fieldMapper, xContentLocation); var exc = expectThrows(IllegalArgumentException.class, () -> mapper.postParse(context)); - assertThat(exc.getMessage(), containsString("Cannot find patch")); + assertThat(exc.getMessage(), containsString("Registered patch not found")); } } @@ -594,7 +594,8 @@ public void testRemainingPatchSourceInPostParse() throws Exception { XContentBuilder builder = JsonXContent.contentBuilder(); builder.value(Map.of("another_field", 45)); var sourceToParse = new SourceToParse("0", BytesReference.bytes(builder), builder.contentType()); - FieldMapper fieldMapper = (FieldMapper) mapperService.mappingLookup().getMapper("field"); + var fieldMapper = (FieldMapper) mapperService.mappingLookup().getMapper("field"); + var anotherFieldMapper = (FieldMapper) mapperService.mappingLookup().getMapper("another_field"); try ( var parser = XContentHelper.createParserNotCompressed( XContentParserConfiguration.EMPTY, @@ -602,7 +603,7 @@ public void testRemainingPatchSourceInPostParse() throws Exception { XContentType.JSON ) ) { - DocumentParserContext context = new TestDocumentParserContext(MappingLookup.EMPTY, sourceToParse) { + DocumentParserContext context = new TestDocumentParserContext(mapperService.mappingLookup(), sourceToParse) { @Override public XContentParser parser() { return parser; @@ -612,13 +613,13 @@ public XContentParser parser() { context.addSourceFieldPatch(fieldMapper, xContentLocation1); { var exc = expectThrows(IllegalArgumentException.class, () -> context.addSourceFieldPatch(fieldMapper, xContentLocation1)); - assertThat(exc.getMessage(), containsString(xContentLocation1.toString())); + assertThat(exc.getMessage(), containsString("Field [field] does not support patching the same location ")); } var xContentLocation2 = new XContentLocation(2, 6); - context.addSourceFieldPatch(fieldMapper, xContentLocation2); + context.addSourceFieldPatch(anotherFieldMapper, xContentLocation2); var exc = expectThrows(IllegalArgumentException.class, () -> mapper.postParse(context)); - assertThat(exc.getMessage(), allOf(containsString(xContentLocation2.toString()), containsString(xContentLocation1.toString()))); + assertThat(exc.getMessage(), containsString("Registered patch not found")); } } }