2323import org .elasticsearch .common .xcontent .XContentParseException ;
2424import org .elasticsearch .common .xcontent .XContentParser ;
2525import org .elasticsearch .common .xcontent .json .JsonXContent ;
26+ import org .elasticsearch .search .aggregations .bucket .GeoHashGridTests ;
2627import org .elasticsearch .test .ESTestCase ;
2728
2829import static org .hamcrest .Matchers .containsString ;
3233
3334public class GeoHashGridParserTests extends ESTestCase {
3435 public void testParseValidFromInts () throws Exception {
35- int precision = randomIntBetween (1 , 12 );
36+ GeoHashType type = GeoHashGridTests .randomType ();
37+ int precision = GeoHashGridTests .randomPrecision (type );
3638 XContentParser stParser = createParser (JsonXContent .jsonXContent ,
37- "{\" field\" :\" my_loc\" , \" precision\" :" + precision + ", \" size\" : 500, \" shard_size\" : 550}" );
39+ "{\" field\" :\" my_loc\" , \" type\" :\" " + type + "\" , \" precision\" :" + precision +
40+ ", \" size\" : 500, \" shard_size\" : 550}" );
3841 XContentParser .Token token = stParser .nextToken ();
3942 assertSame (XContentParser .Token .START_OBJECT , token );
4043 // can create a factory
4144 assertNotNull (GeoGridAggregationBuilder .parse ("geohash_grid" , stParser ));
4245 }
4346
4447 public void testParseValidFromStrings () throws Exception {
45- int precision = randomIntBetween (1 , 12 );
48+ GeoHashType type = GeoHashGridTests .randomType ();
49+ int precision = GeoHashGridTests .randomPrecision (type );
4650 XContentParser stParser = createParser (JsonXContent .jsonXContent ,
47- "{\" field\" :\" my_loc\" , \" precision\" :\" " + precision + "\" , \" size\" : \" 500\" , \" shard_size\" : \" 550\" }" );
51+ "{\" field\" :\" my_loc\" , \" type\" :\" " + type + "\" , \" precision\" :\" " + precision +
52+ "\" , \" size\" : \" 500\" , \" shard_size\" : \" 550\" }" );
4853 XContentParser .Token token = stParser .nextToken ();
4954 assertSame (XContentParser .Token .START_OBJECT , token );
5055 // can create a factory
@@ -94,7 +99,9 @@ public void testParseDistanceUnitPrecisionTooSmall() throws Exception {
9499 }
95100
96101 public void testParseErrorOnBooleanPrecision () throws Exception {
97- XContentParser stParser = createParser (JsonXContent .jsonXContent , "{\" field\" :\" my_loc\" , \" precision\" :false}" );
102+ GeoHashType type = GeoHashGridTests .randomType ();
103+ XContentParser stParser = createParser (JsonXContent .jsonXContent ,
104+ "{\" field\" :\" my_loc\" , \" type\" :\" " + type + "\" , \" precision\" :false}" );
98105 XContentParser .Token token = stParser .nextToken ();
99106 assertSame (XContentParser .Token .START_OBJECT , token );
100107 XContentParseException e = expectThrows (XContentParseException .class ,
@@ -104,15 +111,28 @@ public void testParseErrorOnBooleanPrecision() throws Exception {
104111 }
105112
106113 public void testParseErrorOnPrecisionOutOfRange () throws Exception {
107- XContentParser stParser = createParser (JsonXContent .jsonXContent , "{\" field\" :\" my_loc\" , \" precision\" :\" 13\" }" );
114+ final GeoHashType type = GeoHashGridTests .randomType ();
115+ final int precision = GeoHashGridTests .maxPrecision (type ) + 1 ;
116+ XContentParser stParser = createParser (JsonXContent .jsonXContent ,
117+ "{\" field\" :\" my_loc\" , \" type\" :\" " + type + "\" , \" precision\" :\" " + precision +"\" }" );
108118 XContentParser .Token token = stParser .nextToken ();
109119 assertSame (XContentParser .Token .START_OBJECT , token );
110120 try {
111121 GeoGridAggregationBuilder .parse ("geohash_grid" , stParser );
112122 fail ();
113123 } catch (XContentParseException ex ) {
114124 assertThat (ex .getCause (), instanceOf (IllegalArgumentException .class ));
115- assertEquals ("Invalid geohash aggregation precision of 13. Must be between 1 and 12." , ex .getCause ().getMessage ());
125+ if (type == GeoHashType .GEOHASH ) {
126+ String expectedMsg ;
127+ switch (type ) {
128+ case GEOHASH :
129+ expectedMsg = "Invalid geohash aggregation precision of 13. Must be between 1 and 12." ;
130+ break ;
131+ default :
132+ throw new IllegalArgumentException ("GeoHashType." + type .name () + " was not added to the test" );
133+ }
134+ assertEquals (expectedMsg , ex .getCause ().getMessage ());
135+ }
116136 }
117137 }
118138}
0 commit comments