7
7
package org .elasticsearch .xpack .constantkeyword .mapper ;
8
8
9
9
import org .elasticsearch .common .Strings ;
10
- import org .elasticsearch .common .bytes .BytesReference ;
11
10
import org .elasticsearch .common .compress .CompressedXContent ;
12
11
import org .elasticsearch .common .xcontent .XContentBuilder ;
13
- import org .elasticsearch .common .xcontent .XContentFactory ;
14
- import org .elasticsearch .common .xcontent .XContentType ;
15
12
import org .elasticsearch .index .mapper .DocumentMapper ;
16
13
import org .elasticsearch .index .mapper .FieldMapper ;
17
- import org .elasticsearch .index .mapper .FieldMapperTestCase2 ;
18
14
import org .elasticsearch .index .mapper .MapperParsingException ;
19
15
import org .elasticsearch .index .mapper .MapperService ;
20
16
import org .elasticsearch .index .mapper .MapperService .MergeReason ;
17
+ import org .elasticsearch .index .mapper .MapperTestCase ;
21
18
import org .elasticsearch .index .mapper .ParsedDocument ;
22
- import org .elasticsearch .index .mapper .SourceToParse ;
23
19
import org .elasticsearch .index .mapper .ValueFetcher ;
24
20
import org .elasticsearch .plugins .Plugin ;
25
21
import org .elasticsearch .search .lookup .SourceLookup ;
26
22
import org .elasticsearch .xpack .constantkeyword .ConstantKeywordMapperPlugin ;
27
- import org .junit .Before ;
28
23
29
24
import java .io .IOException ;
30
25
import java .util .Collection ;
31
26
import java .util .Collections ;
32
27
import java .util .List ;
33
- import java .util .Set ;
34
28
35
- public class ConstantKeywordFieldMapperTests extends FieldMapperTestCase2 < ConstantKeywordFieldMapper . Builder > {
29
+ public class ConstantKeywordFieldMapperTests extends MapperTestCase {
36
30
37
31
@ Override
38
32
protected Collection <Plugin > getPlugins () {
39
33
return List .of (new ConstantKeywordMapperPlugin ());
40
34
}
41
35
42
- @ Override
43
- protected Set <String > unsupportedProperties () {
44
- return Set .of ("analyzer" , "similarity" , "store" , "doc_values" , "index" );
45
- }
46
-
47
- @ Override
48
- protected ConstantKeywordFieldMapper .Builder newBuilder () {
49
- return new ConstantKeywordFieldMapper .Builder ("constant" );
50
- }
51
-
52
- @ Before
53
- public void addModifiers () {
54
- addModifier ("value" , false , (a , b ) -> {
55
- a .setValue ("foo" );
56
- b .setValue ("bar" );
57
- });
58
- addModifier ("unset" , false , (a , b ) -> {
59
- a .setValue ("foo" );
60
- ;
61
- });
62
- addModifier ("value-from-null" , true , (a , b ) -> { b .setValue ("bar" ); });
63
- }
64
-
65
36
public void testDefaults () throws Exception {
66
37
XContentBuilder mapping = fieldMapping (b -> b .field ("type" , "constant_keyword" ).field ("value" , "foo" ));
67
38
DocumentMapper mapper = createDocumentMapper (mapping );
68
39
assertEquals (Strings .toString (mapping ), mapper .mappingSource ().toString ());
69
40
70
- BytesReference source = BytesReference .bytes (XContentFactory .jsonBuilder ().startObject ().endObject ());
71
- ParsedDocument doc = mapper .parse (new SourceToParse ("test" , "1" , source , XContentType .JSON ));
41
+ ParsedDocument doc = mapper .parse (source (b -> {}));
72
42
assertNull (doc .rootDoc ().getField ("field" ));
73
43
74
- source = BytesReference .bytes (XContentFactory .jsonBuilder ().startObject ().field ("field" , "foo" ).endObject ());
75
- doc = mapper .parse (new SourceToParse ("test" , "1" , source , XContentType .JSON ));
44
+ doc = mapper .parse (source (b -> b .field ("field" , "foo" )));
76
45
assertNull (doc .rootDoc ().getField ("field" ));
77
46
78
- BytesReference illegalSource = BytesReference .bytes (XContentFactory .jsonBuilder ().startObject ().field ("field" , "bar" ).endObject ());
79
47
MapperParsingException e = expectThrows (
80
48
MapperParsingException .class ,
81
- () -> mapper .parse (new SourceToParse ( "test " , "1" , illegalSource , XContentType . JSON ))
49
+ () -> mapper .parse (source ( b -> b . field ( "field " , "bar" ) ))
82
50
);
83
51
assertEquals (
84
52
"[constant_keyword] field [field] only accepts values that are equal to the value defined in the mappings [foo], "
@@ -90,8 +58,7 @@ public void testDefaults() throws Exception {
90
58
public void testDynamicValue () throws Exception {
91
59
MapperService mapperService = createMapperService (fieldMapping (b -> b .field ("type" , "constant_keyword" )));
92
60
93
- BytesReference source = BytesReference .bytes (XContentFactory .jsonBuilder ().startObject ().field ("field" , "foo" ).endObject ());
94
- ParsedDocument doc = mapperService .documentMapper ().parse (new SourceToParse ("test" , "1" , source , XContentType .JSON ));
61
+ ParsedDocument doc = mapperService .documentMapper ().parse (source (b -> b .field ("field" , "foo" )));
95
62
assertNull (doc .rootDoc ().getField ("field" ));
96
63
assertNotNull (doc .dynamicMappingsUpdate ());
97
64
@@ -100,11 +67,55 @@ public void testDynamicValue() throws Exception {
100
67
String expectedMapping = Strings .toString (fieldMapping (b -> b .field ("type" , "constant_keyword" ).field ("value" , "foo" )));
101
68
assertEquals (expectedMapping , updatedMapper .mappingSource ().toString ());
102
69
103
- doc = updatedMapper .parse (new SourceToParse ( "test " , "1" , source , XContentType . JSON ));
70
+ doc = updatedMapper .parse (source ( b -> b . field ( "field " , "foo" ) ));
104
71
assertNull (doc .rootDoc ().getField ("field" ));
105
72
assertNull (doc .dynamicMappingsUpdate ());
106
73
}
107
74
75
+ public void testBadValues () {
76
+ {
77
+ MapperParsingException e = expectThrows (MapperParsingException .class , () -> createMapperService (fieldMapping (b -> {
78
+ b .field ("type" , "constant_keyword" );
79
+ b .nullField ("value" );
80
+ })));
81
+ assertEquals (e .getMessage (),
82
+ "Failed to parse mapping: [value] on mapper [field] of type [constant_keyword] must not have a [null] value" );
83
+ }
84
+ {
85
+ MapperParsingException e = expectThrows (MapperParsingException .class , () -> createMapperService (fieldMapping (b -> {
86
+ b .field ("type" , "constant_keyword" );
87
+ b .startObject ("value" ).field ("foo" , "bar" ).endObject ();
88
+ })));
89
+ assertEquals (e .getMessage (),
90
+ "Failed to parse mapping: Property [value] on field [field] must be a number or a string, but got [{foo=bar}]" );
91
+ }
92
+ }
93
+
94
+ public void testNumericValue () throws IOException {
95
+ MapperService mapperService = createMapperService (fieldMapping (b -> {
96
+ b .field ("type" , "constant_keyword" );
97
+ b .field ("value" , 74 );
98
+ }));
99
+ ConstantKeywordFieldMapper .ConstantKeywordFieldType ft
100
+ = (ConstantKeywordFieldMapper .ConstantKeywordFieldType ) mapperService .fieldType ("field" );
101
+ assertEquals ("74" , ft .value ());
102
+ }
103
+
104
+ public void testUpdate () throws IOException {
105
+ MapperService mapperService = createMapperService (fieldMapping (b -> {
106
+ b .field ("type" , "constant_keyword" );
107
+ b .field ("value" , "foo" );
108
+ }));
109
+
110
+ IllegalArgumentException e = expectThrows (IllegalArgumentException .class , () -> merge (mapperService , fieldMapping (b -> {
111
+ b .field ("type" , "constant_keyword" );
112
+ b .field ("value" , "bar" );
113
+ })));
114
+ assertEquals (e .getMessage (),
115
+ "Mapper for [field] conflicts with existing mapper:\n " +
116
+ "\t Cannot update parameter [value] from [foo] to [bar]" );
117
+ }
118
+
108
119
@ Override
109
120
protected void minimalMapping (XContentBuilder b ) throws IOException {
110
121
b .field ("type" , "constant_keyword" );
0 commit comments