22
22
import org .elasticsearch .ElasticsearchParseException ;
23
23
import org .elasticsearch .action .admin .indices .alias .Alias ;
24
24
import org .elasticsearch .common .Strings ;
25
+ import org .elasticsearch .common .bytes .BytesReference ;
25
26
import org .elasticsearch .common .io .stream .BytesStreamOutput ;
26
27
import org .elasticsearch .common .io .stream .StreamInput ;
28
+ import org .elasticsearch .common .settings .Settings ;
27
29
import org .elasticsearch .common .xcontent .LoggingDeprecationHandler ;
28
30
import org .elasticsearch .common .xcontent .NamedXContentRegistry ;
29
31
import org .elasticsearch .common .xcontent .XContentParser ;
30
32
import org .elasticsearch .common .xcontent .XContentType ;
31
33
import org .elasticsearch .common .xcontent .json .JsonXContent ;
32
34
import org .elasticsearch .index .RandomCreateIndexGenerator ;
33
- import org .elasticsearch .test .AbstractXContentTestCase ;
35
+ import org .elasticsearch .test .ESTestCase ;
36
+ import org .elasticsearch .test .hamcrest .ElasticsearchAssertions ;
34
37
35
38
import java .io .IOException ;
36
39
import java .util .Map ;
37
40
import java .util .Set ;
38
41
39
- public class CreateIndexRequestTests extends AbstractXContentTestCase <CreateIndexRequest > {
42
+ import static org .elasticsearch .cluster .metadata .IndexMetaData .SETTING_NUMBER_OF_SHARDS ;
43
+ import static org .elasticsearch .common .xcontent .ToXContent .EMPTY_PARAMS ;
40
44
41
- @ Override
42
- protected CreateIndexRequest createTestInstance () {
43
- try {
44
- return RandomCreateIndexGenerator .randomCreateIndexRequest ();
45
- } catch (IOException e ) {
46
- throw new RuntimeException (e );
47
- }
48
- }
49
-
50
- @ Override
51
- protected CreateIndexRequest doParseInstance (XContentParser parser ) throws IOException {
52
- CreateIndexRequest request = new CreateIndexRequest ();
53
- request .source (parser .map (), LoggingDeprecationHandler .INSTANCE );
54
- return request ;
55
- }
56
-
57
- @ Override
58
- protected void assertEqualInstances (CreateIndexRequest expectedInstance , CreateIndexRequest newInstance ) {
59
- assertEquals (expectedInstance .settings (), newInstance .settings ());
60
- assertAliasesEqual (expectedInstance .aliases (), newInstance .aliases ());
61
- assertMappingsEqual (expectedInstance .mappings (), newInstance .mappings ());
62
- }
63
-
64
- @ Override
65
- protected boolean supportsUnknownFields () {
66
- return false ;
67
- }
68
-
69
- public static void assertMappingsEqual (Map <String , String > expected , Map <String , String > actual ) {
70
- assertEquals (expected .keySet (), actual .keySet ());
71
-
72
- for (Map .Entry <String , String > expectedEntry : expected .entrySet ()) {
73
- String expectedValue = expectedEntry .getValue ();
74
- String actualValue = actual .get (expectedEntry .getKey ());
75
- try (XContentParser expectedJson = JsonXContent .jsonXContent .createParser (NamedXContentRegistry .EMPTY ,
76
- LoggingDeprecationHandler .INSTANCE , expectedValue );
77
- XContentParser actualJson = JsonXContent .jsonXContent .createParser (NamedXContentRegistry .EMPTY ,
78
- LoggingDeprecationHandler .INSTANCE , actualValue )) {
79
- assertEquals (expectedJson .map (), actualJson .map ());
80
- } catch (IOException e ) {
81
- throw new RuntimeException (e );
82
- }
83
- }
84
- }
85
-
86
- public static void assertAliasesEqual (Set <Alias > expected , Set <Alias > actual ) {
87
- assertEquals (expected , actual );
88
-
89
- for (Alias expectedAlias : expected ) {
90
- for (Alias actualAlias : actual ) {
91
- if (expectedAlias .equals (actualAlias )) {
92
- // As Alias#equals only looks at name, we check the equality of the other Alias parameters here.
93
- assertEquals (expectedAlias .filter (), actualAlias .filter ());
94
- assertEquals (expectedAlias .indexRouting (), actualAlias .indexRouting ());
95
- assertEquals (expectedAlias .searchRouting (), actualAlias .searchRouting ());
96
- }
97
- }
98
- }
99
- }
45
+ public class CreateIndexRequestTests extends ESTestCase {
100
46
101
47
public void testSerialization () throws IOException {
102
48
CreateIndexRequest request = new CreateIndexRequest ("foo" );
103
- String mapping = Strings .toString (JsonXContent .contentBuilder ().startObject ()
104
- .startObject ("type" ).endObject ().endObject ());
49
+ String mapping = Strings .toString (JsonXContent .contentBuilder ().startObject ().startObject ("type" ).endObject ().endObject ());
105
50
request .mapping ("my_type" , mapping , XContentType .JSON );
106
51
107
52
try (BytesStreamOutput output = new BytesStreamOutput ()) {
@@ -118,7 +63,7 @@ public void testSerialization() throws IOException {
118
63
119
64
public void testTopLevelKeys () {
120
65
String createIndex =
121
- "{\n "
66
+ "{\n "
122
67
+ " \" FOO_SHOULD_BE_ILLEGAL_HERE\" : {\n "
123
68
+ " \" BAR_IS_THE_SAME\" : 42\n "
124
69
+ " },\n "
@@ -135,7 +80,81 @@ public void testTopLevelKeys() {
135
80
136
81
CreateIndexRequest request = new CreateIndexRequest ();
137
82
ElasticsearchParseException e = expectThrows (ElasticsearchParseException .class ,
138
- () -> {request .source (createIndex , XContentType .JSON );});
83
+ () -> {request .source (createIndex , XContentType .JSON );});
139
84
assertEquals ("unknown key [FOO_SHOULD_BE_ILLEGAL_HERE] for create index" , e .getMessage ());
140
85
}
86
+
87
+ public void testToXContent () throws IOException {
88
+ CreateIndexRequest request = new CreateIndexRequest ("foo" );
89
+
90
+ String mapping = Strings .toString (JsonXContent .contentBuilder ().startObject ().startObject ("type" ).endObject ().endObject ());
91
+ request .mapping ("my_type" , mapping , XContentType .JSON );
92
+
93
+ Alias alias = new Alias ("test_alias" );
94
+ alias .routing ("1" );
95
+ alias .filter ("{\" term\" :{\" year\" :2016}}" );
96
+ alias .writeIndex (true );
97
+ request .alias (alias );
98
+
99
+ Settings .Builder settings = Settings .builder ();
100
+ settings .put (SETTING_NUMBER_OF_SHARDS , 10 );
101
+ request .settings (settings );
102
+
103
+ String actualRequestBody = Strings .toString (request );
104
+
105
+ String expectedRequestBody = "{\" settings\" :{\" index\" :{\" number_of_shards\" :\" 10\" }}," +
106
+ "\" mappings\" :{\" my_type\" :{\" type\" :{}}}," +
107
+ "\" aliases\" :{\" test_alias\" :{\" filter\" :{\" term\" :{\" year\" :2016}},\" routing\" :\" 1\" ,\" is_write_index\" :true}}}" ;
108
+
109
+ assertEquals (expectedRequestBody , actualRequestBody );
110
+ }
111
+
112
+ public void testToAndFromXContent () throws IOException {
113
+
114
+ final CreateIndexRequest createIndexRequest = RandomCreateIndexGenerator .randomCreateIndexRequest ();
115
+
116
+ boolean humanReadable = randomBoolean ();
117
+ final XContentType xContentType = randomFrom (XContentType .values ());
118
+ BytesReference originalBytes = toShuffledXContent (createIndexRequest , xContentType , EMPTY_PARAMS , humanReadable );
119
+
120
+ CreateIndexRequest parsedCreateIndexRequest = new CreateIndexRequest ();
121
+ parsedCreateIndexRequest .source (originalBytes , xContentType );
122
+
123
+ assertMappingsEqual (createIndexRequest .mappings (), parsedCreateIndexRequest .mappings ());
124
+ assertAliasesEqual (createIndexRequest .aliases (), parsedCreateIndexRequest .aliases ());
125
+ assertEquals (createIndexRequest .settings (), parsedCreateIndexRequest .settings ());
126
+
127
+ BytesReference finalBytes = toShuffledXContent (parsedCreateIndexRequest , xContentType , EMPTY_PARAMS , humanReadable );
128
+ ElasticsearchAssertions .assertToXContentEquivalent (originalBytes , finalBytes , xContentType );
129
+ }
130
+
131
+ public static void assertMappingsEqual (Map <String , String > expected , Map <String , String > actual ) throws IOException {
132
+ assertEquals (expected .keySet (), actual .keySet ());
133
+
134
+ for (Map .Entry <String , String > expectedEntry : expected .entrySet ()) {
135
+ String expectedValue = expectedEntry .getValue ();
136
+ String actualValue = actual .get (expectedEntry .getKey ());
137
+ try (XContentParser expectedJson = JsonXContent .jsonXContent .createParser (NamedXContentRegistry .EMPTY ,
138
+ LoggingDeprecationHandler .INSTANCE , expectedValue );
139
+ XContentParser actualJson = JsonXContent .jsonXContent .createParser (NamedXContentRegistry .EMPTY ,
140
+ LoggingDeprecationHandler .INSTANCE , actualValue )){
141
+ assertEquals (expectedJson .map (), actualJson .map ());
142
+ }
143
+ }
144
+ }
145
+
146
+ public static void assertAliasesEqual (Set <Alias > expected , Set <Alias > actual ) throws IOException {
147
+ assertEquals (expected , actual );
148
+
149
+ for (Alias expectedAlias : expected ) {
150
+ for (Alias actualAlias : actual ) {
151
+ if (expectedAlias .equals (actualAlias )) {
152
+ // As Alias#equals only looks at name, we check the equality of the other Alias parameters here.
153
+ assertEquals (expectedAlias .filter (), actualAlias .filter ());
154
+ assertEquals (expectedAlias .indexRouting (), actualAlias .indexRouting ());
155
+ assertEquals (expectedAlias .searchRouting (), actualAlias .searchRouting ());
156
+ }
157
+ }
158
+ }
159
+ }
141
160
}
0 commit comments