1919
2020package org .elasticsearch .client .indices ;
2121
22+ import com .carrotsearch .hppc .cursors .ObjectObjectCursor ;
2223import org .apache .lucene .util .CollectionUtil ;
24+ import org .elasticsearch .client .AbstractResponseTestCase ;
2325import org .elasticsearch .client .GetAliasesResponseTests ;
2426import org .elasticsearch .cluster .metadata .AliasMetadata ;
2527import org .elasticsearch .cluster .metadata .MappingMetadata ;
2628import org .elasticsearch .common .collect .ImmutableOpenMap ;
2729import org .elasticsearch .common .settings .IndexScopedSettings ;
2830import org .elasticsearch .common .settings .Settings ;
29- import org .elasticsearch .common .xcontent .ToXContent ;
30- import org .elasticsearch .common .xcontent .ToXContent .Params ;
31- import org .elasticsearch .common .xcontent .XContentBuilder ;
31+ import org .elasticsearch .common .xcontent .XContentParser ;
32+ import org .elasticsearch .common .xcontent .XContentType ;
3233import org .elasticsearch .index .RandomCreateIndexGenerator ;
3334import org .elasticsearch .index .mapper .MapperService ;
34- import org .elasticsearch .rest .BaseRestHandler ;
35- import org .elasticsearch .test .ESTestCase ;
3635
3736import java .io .IOException ;
3837import java .util .ArrayList ;
4443import java .util .Map ;
4544import java .util .Objects ;
4645
47- import static org .elasticsearch .test .AbstractXContentTestCase .xContentTester ;
48-
49- public class GetIndexResponseTests extends ESTestCase {
50-
51- // Because the client-side class does not have a toXContent method, we test xContent serialization by creating
52- // a random client object, converting it to a server object then serializing it to xContent, and finally
53- // parsing it back as a client object. We check equality between the original client object, and the parsed one.
54- public void testFromXContent () throws IOException {
55- xContentTester (
56- this ::createParser ,
57- GetIndexResponseTests ::createTestInstance ,
58- GetIndexResponseTests ::toXContent ,
59- GetIndexResponse ::fromXContent )
60- .supportsUnknownFields (false )
61- .assertToXContentEquivalence (false )
62- .assertEqualsConsumer (GetIndexResponseTests ::assertEqualInstances )
63- .test ();
64- }
46+ import static org .hamcrest .Matchers .equalTo ;
6547
66- private static void assertEqualInstances (GetIndexResponse expected , GetIndexResponse actual ) {
67- assertArrayEquals (expected .getIndices (), actual .getIndices ());
68- assertEquals (expected .getMappings (), actual .getMappings ());
69- assertEquals (expected .getSettings (), actual .getSettings ());
70- assertEquals (expected .getDefaultSettings (), actual .getDefaultSettings ());
71- assertEquals (expected .getAliases (), actual .getAliases ());
72- }
48+ public class GetIndexResponseTests extends AbstractResponseTestCase <org .elasticsearch .action .admin .indices .get .GetIndexResponse ,
49+ GetIndexResponse > {
7350
74- private static GetIndexResponse createTestInstance () {
51+ @ Override
52+ protected org .elasticsearch .action .admin .indices .get .GetIndexResponse createServerTestInstance (XContentType xContentType ) {
7553 String [] indices = generateRandomStringArray (5 , 5 , false , false );
76- Map <String , MappingMetadata > mappings = new HashMap <> ();
77- Map <String , List <AliasMetadata >> aliases = new HashMap <> ();
78- Map <String , Settings > settings = new HashMap <> ();
79- Map <String , Settings > defaultSettings = new HashMap <> ();
80- Map <String , String > dataStreams = new HashMap <> ();
54+ ImmutableOpenMap . Builder <String , ImmutableOpenMap < String , MappingMetadata >> mappings = ImmutableOpenMap . builder ();
55+ ImmutableOpenMap . Builder <String , List <AliasMetadata >> aliases = ImmutableOpenMap . builder ();
56+ ImmutableOpenMap . Builder <String , Settings > settings = ImmutableOpenMap . builder ();
57+ ImmutableOpenMap . Builder <String , Settings > defaultSettings = ImmutableOpenMap . builder ();
58+ ImmutableOpenMap . Builder <String , String > dataStreams = ImmutableOpenMap . builder ();
8159 IndexScopedSettings indexScopedSettings = IndexScopedSettings .DEFAULT_SCOPED_SETTINGS ;
8260 boolean includeDefaults = randomBoolean ();
8361 for (String index : indices ) {
84- mappings .put (index , createMappingsForIndex ());
62+ ImmutableOpenMap .Builder <String , MappingMetadata > indexMapping = ImmutableOpenMap .builder ();
63+ indexMapping .put (MapperService .SINGLE_MAPPING_NAME , createMappingsForIndex ());
64+ mappings .put (index , indexMapping .build ());
8565
8666 List <AliasMetadata > aliasMetadataList = new ArrayList <>();
8767 int aliasesNum = randomIntBetween (0 , 3 );
@@ -103,7 +83,29 @@ private static GetIndexResponse createTestInstance() {
10383 dataStreams .put (index , randomAlphaOfLength (5 ).toLowerCase (Locale .ROOT ));
10484 }
10585 }
106- return new GetIndexResponse (indices , mappings , aliases , settings , defaultSettings , dataStreams );
86+ return new org .elasticsearch .action .admin .indices .get .GetIndexResponse (indices ,
87+ mappings .build (), aliases .build (), settings .build (), defaultSettings .build (), dataStreams .build ());
88+ }
89+
90+ @ Override
91+ protected GetIndexResponse doParseToClientInstance (XContentParser parser ) throws IOException {
92+ return GetIndexResponse .fromXContent (parser );
93+ }
94+
95+ @ Override
96+ protected void assertInstances (org .elasticsearch .action .admin .indices .get .GetIndexResponse serverTestInstance ,
97+ GetIndexResponse clientInstance ) {
98+ assertArrayEquals (serverTestInstance .getIndices (), clientInstance .getIndices ());
99+ assertThat (serverTestInstance .getMappings ().size (), equalTo (clientInstance .getMappings ().size ()));
100+ for (ObjectObjectCursor <String , ImmutableOpenMap <String , MappingMetadata >> cursor : serverTestInstance .getMappings ()) {
101+ MappingMetadata serverMapping = cursor .value .get (MapperService .SINGLE_MAPPING_NAME );
102+ MappingMetadata clientMapping = clientInstance .getMappings ().get (cursor .key );
103+ assertThat (serverMapping , equalTo (clientMapping ));
104+ }
105+ assertMapEquals (serverTestInstance .getSettings (), clientInstance .getSettings ());
106+ assertMapEquals (serverTestInstance .defaultSettings (), clientInstance .getDefaultSettings ());
107+ assertMapEquals (serverTestInstance .getAliases (), clientInstance .getAliases ());
108+ assertMapEquals (serverTestInstance .getDataStreams (), clientInstance .getDataStreams ());
107109 }
108110
109111 private static MappingMetadata createMappingsForIndex () {
@@ -166,38 +168,4 @@ private static Map<String, Object> randomFieldMapping() {
166168 return mappings ;
167169 }
168170
169- private static void toXContent (GetIndexResponse response , XContentBuilder builder ) throws IOException {
170- // first we need to repackage from GetIndexResponse to org.elasticsearch.action.admin.indices.get.GetIndexResponse
171- ImmutableOpenMap .Builder <String , ImmutableOpenMap <String , MappingMetadata >> allMappings = ImmutableOpenMap .builder ();
172- ImmutableOpenMap .Builder <String , List <AliasMetadata >> aliases = ImmutableOpenMap .builder ();
173- ImmutableOpenMap .Builder <String , Settings > settings = ImmutableOpenMap .builder ();
174- ImmutableOpenMap .Builder <String , Settings > defaultSettings = ImmutableOpenMap .builder ();
175-
176- Map <String , MappingMetadata > indexMappings = response .getMappings ();
177- for (String index : response .getIndices ()) {
178- MappingMetadata mmd = indexMappings .get (index );
179- ImmutableOpenMap .Builder <String , MappingMetadata > typedMappings = ImmutableOpenMap .builder ();
180- if (mmd != null ) {
181- typedMappings .put (MapperService .SINGLE_MAPPING_NAME , mmd );
182- }
183- allMappings .put (index , typedMappings .build ());
184- aliases .put (index , response .getAliases ().get (index ));
185- settings .put (index , response .getSettings ().get (index ));
186- defaultSettings .put (index , response .getDefaultSettings ().get (index ));
187- }
188-
189- org .elasticsearch .action .admin .indices .get .GetIndexResponse serverResponse
190- = new org .elasticsearch .action .admin .indices .get .GetIndexResponse (
191- response .getIndices (),
192- allMappings .build (),
193- aliases .build (),
194- settings .build (),
195- defaultSettings .build (),
196- ImmutableOpenMap .<String , String >builder ().build ()
197- );
198-
199- // then we can call its toXContent method, forcing no output of types
200- Params params = new ToXContent .MapParams (Collections .singletonMap (BaseRestHandler .INCLUDE_TYPE_NAME_PARAMETER , "false" ));
201- serverResponse .toXContent (builder , params );
202- }
203171}
0 commit comments