21
21
22
22
import org .elasticsearch .action .admin .indices .alias .Alias ;
23
23
import org .elasticsearch .common .Strings ;
24
- import org .elasticsearch .common .bytes .BytesReference ;
25
24
import org .elasticsearch .common .io .stream .BytesStreamOutput ;
26
25
import org .elasticsearch .common .io .stream .StreamInput ;
27
- import org .elasticsearch .common .settings .Settings ;
28
26
import org .elasticsearch .common .xcontent .LoggingDeprecationHandler ;
29
27
import org .elasticsearch .common .xcontent .NamedXContentRegistry ;
30
28
import org .elasticsearch .common .xcontent .XContentParser ;
31
29
import org .elasticsearch .common .xcontent .XContentType ;
32
30
import org .elasticsearch .common .xcontent .json .JsonXContent ;
33
31
import org .elasticsearch .index .RandomCreateIndexGenerator ;
34
- import org .elasticsearch .test .ESTestCase ;
35
- import org .elasticsearch .test .hamcrest .ElasticsearchAssertions ;
32
+ import org .elasticsearch .test .AbstractXContentTestCase ;
36
33
37
34
import java .io .IOException ;
38
35
import java .util .Map ;
39
36
import java .util .Set ;
40
37
41
- import static org .elasticsearch .cluster .metadata .IndexMetaData .SETTING_NUMBER_OF_SHARDS ;
42
- import static org .elasticsearch .common .xcontent .ToXContent .EMPTY_PARAMS ;
38
+ public class CreateIndexRequestTests extends AbstractXContentTestCase <CreateIndexRequest > {
43
39
44
- public class CreateIndexRequestTests extends ESTestCase {
45
-
46
- public void testSerialization () throws IOException {
47
- CreateIndexRequest request = new CreateIndexRequest ("foo" );
48
- String mapping = Strings .toString (JsonXContent .contentBuilder ().startObject ().startObject ("type" ).endObject ().endObject ());
49
- request .mapping ("my_type" , mapping , XContentType .JSON );
50
-
51
- try (BytesStreamOutput output = new BytesStreamOutput ()) {
52
- request .writeTo (output );
53
-
54
- try (StreamInput in = output .bytes ().streamInput ()) {
55
- CreateIndexRequest serialized = new CreateIndexRequest ();
56
- serialized .readFrom (in );
57
- assertEquals (request .index (), serialized .index ());
58
- assertEquals (mapping , serialized .mappings ().get ("my_type" ));
59
- }
40
+ @ Override
41
+ protected CreateIndexRequest createTestInstance () {
42
+ try {
43
+ return RandomCreateIndexGenerator .randomCreateIndexRequest ();
44
+ } catch (IOException e ) {
45
+ throw new RuntimeException (e );
60
46
}
61
47
}
62
48
63
- public void testToXContent () throws IOException {
64
- CreateIndexRequest request = new CreateIndexRequest ("foo" );
65
-
66
- String mapping = Strings .toString (JsonXContent .contentBuilder ().startObject ().startObject ("type" ).endObject ().endObject ());
67
- request .mapping ("my_type" , mapping , XContentType .JSON );
68
-
69
- Alias alias = new Alias ("test_alias" );
70
- alias .routing ("1" );
71
- alias .filter ("{\" term\" :{\" year\" :2016}}" );
72
- alias .writeIndex (true );
73
- request .alias (alias );
74
-
75
- Settings .Builder settings = Settings .builder ();
76
- settings .put (SETTING_NUMBER_OF_SHARDS , 10 );
77
- request .settings (settings );
78
-
79
- String actualRequestBody = Strings .toString (request );
80
-
81
- String expectedRequestBody = "{\" settings\" :{\" index\" :{\" number_of_shards\" :\" 10\" }}," +
82
- "\" mappings\" :{\" my_type\" :{\" type\" :{}}}," +
83
- "\" aliases\" :{\" test_alias\" :{\" filter\" :{\" term\" :{\" year\" :2016}},\" routing\" :\" 1\" ,\" is_write_index\" :true}}}" ;
84
-
85
- assertEquals (expectedRequestBody , actualRequestBody );
49
+ @ Override
50
+ protected CreateIndexRequest doParseInstance (XContentParser parser ) throws IOException {
51
+ CreateIndexRequest request = new CreateIndexRequest ();
52
+ request .source (parser .map (), LoggingDeprecationHandler .INSTANCE );
53
+ return request ;
86
54
}
87
55
88
- public void testToAndFromXContent () throws IOException {
89
-
90
- final CreateIndexRequest createIndexRequest = RandomCreateIndexGenerator .randomCreateIndexRequest ();
91
-
92
- boolean humanReadable = randomBoolean ();
93
- final XContentType xContentType = randomFrom (XContentType .values ());
94
- BytesReference originalBytes = toShuffledXContent (createIndexRequest , xContentType , EMPTY_PARAMS , humanReadable );
95
-
96
- CreateIndexRequest parsedCreateIndexRequest = new CreateIndexRequest ();
97
- parsedCreateIndexRequest .source (originalBytes , xContentType );
98
-
99
- assertMappingsEqual (createIndexRequest .mappings (), parsedCreateIndexRequest .mappings ());
100
- assertAliasesEqual (createIndexRequest .aliases (), parsedCreateIndexRequest .aliases ());
101
- assertEquals (createIndexRequest .settings (), parsedCreateIndexRequest .settings ());
56
+ @ Override
57
+ protected void assertEqualInstances (CreateIndexRequest expectedInstance , CreateIndexRequest newInstance ) {
58
+ assertEquals (expectedInstance .settings (), newInstance .settings ());
59
+ assertAliasesEqual (expectedInstance .aliases (), newInstance .aliases ());
60
+ assertMappingsEqual (expectedInstance .mappings (), newInstance .mappings ());
61
+ }
102
62
103
- BytesReference finalBytes = toShuffledXContent (parsedCreateIndexRequest , xContentType , EMPTY_PARAMS , humanReadable );
104
- ElasticsearchAssertions .assertToXContentEquivalent (originalBytes , finalBytes , xContentType );
63
+ @ Override
64
+ protected boolean supportsUnknownFields () {
65
+ return false ;
105
66
}
106
67
107
- public static void assertMappingsEqual (Map <String , String > expected , Map <String , String > actual ) throws IOException {
68
+ public static void assertMappingsEqual (Map <String , String > expected , Map <String , String > actual ) {
108
69
assertEquals (expected .keySet (), actual .keySet ());
109
70
110
71
for (Map .Entry <String , String > expectedEntry : expected .entrySet ()) {
111
72
String expectedValue = expectedEntry .getValue ();
112
73
String actualValue = actual .get (expectedEntry .getKey ());
113
- XContentParser expectedJson = JsonXContent .jsonXContent .createParser (NamedXContentRegistry .EMPTY ,
114
- LoggingDeprecationHandler .INSTANCE , expectedValue );
115
- XContentParser actualJson = JsonXContent .jsonXContent .createParser (NamedXContentRegistry .EMPTY ,
116
- LoggingDeprecationHandler .INSTANCE , actualValue );
117
- assertEquals (expectedJson .map (), actualJson .map ());
74
+ try (XContentParser expectedJson = JsonXContent .jsonXContent .createParser (NamedXContentRegistry .EMPTY ,
75
+ LoggingDeprecationHandler .INSTANCE , expectedValue );
76
+ XContentParser actualJson = JsonXContent .jsonXContent .createParser (NamedXContentRegistry .EMPTY ,
77
+ LoggingDeprecationHandler .INSTANCE , actualValue )) {
78
+ assertEquals (expectedJson .map (), actualJson .map ());
79
+ } catch (IOException e ) {
80
+ throw new RuntimeException (e );
81
+ }
118
82
}
119
83
}
120
84
121
- public static void assertAliasesEqual (Set <Alias > expected , Set <Alias > actual ) throws IOException {
85
+ public static void assertAliasesEqual (Set <Alias > expected , Set <Alias > actual ) {
122
86
assertEquals (expected , actual );
123
87
124
88
for (Alias expectedAlias : expected ) {
@@ -132,4 +96,22 @@ public static void assertAliasesEqual(Set<Alias> expected, Set<Alias> actual) th
132
96
}
133
97
}
134
98
}
99
+
100
+ public void testSerialization () throws IOException {
101
+ CreateIndexRequest request = new CreateIndexRequest ("foo" );
102
+ String mapping = Strings .toString (JsonXContent .contentBuilder ().startObject ()
103
+ .startObject ("type" ).endObject ().endObject ());
104
+ request .mapping ("my_type" , mapping , XContentType .JSON );
105
+
106
+ try (BytesStreamOutput output = new BytesStreamOutput ()) {
107
+ request .writeTo (output );
108
+
109
+ try (StreamInput in = output .bytes ().streamInput ()) {
110
+ CreateIndexRequest serialized = new CreateIndexRequest ();
111
+ serialized .readFrom (in );
112
+ assertEquals (request .index (), serialized .index ());
113
+ assertEquals (mapping , serialized .mappings ().get ("my_type" ));
114
+ }
115
+ }
116
+ }
135
117
}
0 commit comments