15
15
import org .elasticsearch .client .Response ;
16
16
import org .elasticsearch .client .ResponseException ;
17
17
import org .elasticsearch .common .CheckedSupplier ;
18
+ import org .elasticsearch .common .Strings ;
18
19
import org .elasticsearch .common .collect .Tuple ;
19
20
import org .elasticsearch .common .io .Streams ;
20
21
import org .elasticsearch .common .xcontent .XContentHelper ;
21
22
import org .elasticsearch .common .xcontent .json .JsonXContent ;
22
23
import org .elasticsearch .test .NotEqualMessageBuilder ;
23
24
import org .elasticsearch .test .rest .ESRestTestCase ;
25
+ import org .elasticsearch .xpack .sql .proto .StringUtils ;
24
26
import org .elasticsearch .xpack .sql .qa .ErrorsTestCase ;
25
27
import org .hamcrest .Matcher ;
26
28
@@ -97,10 +99,10 @@ public void testNextPage() throws IOException {
97
99
for (int i = 0 ; i < 20 ; i += 2 ) {
98
100
Map <String , Object > response ;
99
101
if (i == 0 ) {
100
- response = runSql (mode , new StringEntity (sqlRequest , ContentType .APPLICATION_JSON ));
102
+ response = runSql (new StringEntity (sqlRequest , ContentType .APPLICATION_JSON ), "" );
101
103
} else {
102
- response = runSql (mode , new StringEntity ("{\" cursor\" :\" " + cursor + "\" }" ,
103
- ContentType .APPLICATION_JSON ));
104
+ response = runSql (new StringEntity ("{\" cursor\" :\" " + cursor + "\" " + mode ( mode ) + "}" ,
105
+ ContentType .APPLICATION_JSON ), StringUtils . EMPTY );
104
106
}
105
107
106
108
Map <String , Object > expected = new HashMap <>();
@@ -120,8 +122,8 @@ public void testNextPage() throws IOException {
120
122
}
121
123
Map <String , Object > expected = new HashMap <>();
122
124
expected .put ("rows" , emptyList ());
123
- assertResponse (expected , runSql (mode , new StringEntity ("{ \" cursor\" :\" " + cursor + "\" }" ,
124
- ContentType .APPLICATION_JSON )));
125
+ assertResponse (expected , runSql (new StringEntity ("{ \" cursor\" :\" " + cursor + "\" " + mode ( mode ) + "}" ,
126
+ ContentType .APPLICATION_JSON ), StringUtils . EMPTY ));
125
127
}
126
128
127
129
@ AwaitsFix (bugUrl = "https://github.com/elastic/x-pack-elasticsearch/issues/2074" )
@@ -136,8 +138,7 @@ public void testTimeZone() throws IOException {
136
138
expected .put ("size" , 2 );
137
139
138
140
// Default TimeZone is UTC
139
- assertResponse (expected , runSql (mode , new StringEntity ("{\" query\" :\" SELECT DAY_OF_YEAR(test), COUNT(*) FROM test\" }" ,
140
- ContentType .APPLICATION_JSON )));
141
+ assertResponse (expected , runSql (mode , "SELECT DAY_OF_YEAR(test), COUNT(*) FROM test" ));
141
142
}
142
143
143
144
public void testScoreWithFieldNamedScore () throws IOException {
@@ -302,35 +303,29 @@ private void expectBadRequest(CheckedSupplier<Map<String, Object>, Exception> co
302
303
}
303
304
304
305
private Map <String , Object > runSql (String mode , String sql ) throws IOException {
305
- return runSql (mode , sql , "" );
306
+ return runSql (mode , sql , StringUtils . EMPTY );
306
307
}
307
308
308
309
private Map <String , Object > runSql (String mode , String sql , String suffix ) throws IOException {
309
- return runSql (mode , new StringEntity ("{\" query\" :\" " + sql + "\" }" , ContentType .APPLICATION_JSON ), suffix );
310
+ return runSql (new StringEntity ("{\" query\" :\" " + sql + "\" " + mode ( mode ) + "}" , ContentType .APPLICATION_JSON ), suffix );
310
311
}
311
312
312
- private Map <String , Object > runSql (String mode , HttpEntity sql ) throws IOException {
313
- return runSql (mode , sql , "" );
314
- }
315
-
316
- private Map <String , Object > runSql (String mode , HttpEntity sql , String suffix ) throws IOException {
313
+ private Map <String , Object > runSql (HttpEntity sql , String suffix ) throws IOException {
317
314
Request request = new Request ("POST" , "/_xpack/sql" + suffix );
318
315
request .addParameter ("error_trace" , "true" ); // Helps with debugging in case something crazy happens on the server.
319
316
request .addParameter ("pretty" , "true" ); // Improves error reporting readability
320
317
if (randomBoolean ()) {
321
318
// We default to JSON but we force it randomly for extra coverage
322
319
request .addParameter ("format" , "json" );
323
320
}
324
- if (false == mode .isEmpty ()) {
325
- request .addParameter ("mode" , mode ); // JDBC or PLAIN mode
326
- }
327
321
if (randomBoolean ()) {
328
322
// JSON is the default but randomly set it sometime for extra coverage
329
323
RequestOptions .Builder options = request .getOptions ().toBuilder ();
330
324
options .addHeader ("Accept" , randomFrom ("*/*" , "application/json" ));
331
325
request .setOptions (options );
332
326
}
333
327
request .setEntity (sql );
328
+
334
329
Response response = client ().performRequest (request );
335
330
try (InputStream content = response .getEntity ().getContent ()) {
336
331
return XContentHelper .convertToMap (JsonXContent .jsonXContent , content , false );
@@ -357,9 +352,9 @@ public void testBasicQueryWithFilter() throws IOException {
357
352
Map <String , Object > expected = new HashMap <>();
358
353
expected .put ("columns" , singletonList (columnInfo (mode , "test" , "text" , JDBCType .VARCHAR , 0 )));
359
354
expected .put ("rows" , singletonList (singletonList ("foo" )));
360
- assertResponse (expected , runSql (mode , new StringEntity ("{\" query\" :\" SELECT * FROM test\" , " +
361
- "\" filter\" :{\" match\" : {\" test\" : \" foo\" }}}" ,
362
- ContentType .APPLICATION_JSON )));
355
+ assertResponse (expected , runSql (new StringEntity ("{\" query\" :\" SELECT * FROM test\" , " +
356
+ "\" filter\" :{\" match\" : {\" test\" : \" foo\" }}" + mode ( mode ) + " }" ,
357
+ ContentType .APPLICATION_JSON ), StringUtils . EMPTY ));
363
358
}
364
359
365
360
public void testBasicQueryWithParameters () throws IOException {
@@ -373,16 +368,16 @@ public void testBasicQueryWithParameters() throws IOException {
373
368
columnInfo (mode , "param" , "integer" , JDBCType .INTEGER , 11 )
374
369
));
375
370
expected .put ("rows" , singletonList (Arrays .asList ("foo" , 10 )));
376
- assertResponse (expected , runSql (mode , new StringEntity ("{\" query\" :\" SELECT test, ? param FROM test WHERE test = ?\" , " +
377
- "\" params\" :[{\" type\" : \" integer\" , \" value\" : 10}, {\" type\" : \" keyword\" , \" value\" : \" foo\" }]}" ,
378
- ContentType .APPLICATION_JSON )));
371
+ assertResponse (expected , runSql (new StringEntity ("{\" query\" :\" SELECT test, ? param FROM test WHERE test = ?\" , " +
372
+ "\" params\" :[{\" type\" : \" integer\" , \" value\" : 10}, {\" type\" : \" keyword\" , \" value\" : \" foo\" }]"
373
+ + mode ( mode ) + "}" , ContentType .APPLICATION_JSON ), StringUtils . EMPTY ));
379
374
}
380
375
381
376
public void testBasicTranslateQueryWithFilter () throws IOException {
382
377
index ("{\" test\" :\" foo\" }" ,
383
378
"{\" test\" :\" bar\" }" );
384
379
385
- Map <String , Object > response = runSql ("" ,
380
+ Map <String , Object > response = runSql (
386
381
new StringEntity ("{\" query\" :\" SELECT * FROM test\" , \" filter\" :{\" match\" : {\" test\" : \" foo\" }}}" ,
387
382
ContentType .APPLICATION_JSON ), "/translate/"
388
383
);
@@ -424,7 +419,7 @@ public void testTranslateQueryWithGroupByAndHaving() throws IOException {
424
419
index ("{\" salary\" :100}" ,
425
420
"{\" age\" :20}" );
426
421
427
- Map <String , Object > response = runSql ("" ,
422
+ Map <String , Object > response = runSql (
428
423
new StringEntity ("{\" query\" :\" SELECT avg(salary) FROM test GROUP BY abs(age) HAVING avg(salary) > 50 LIMIT 10\" }" ,
429
424
ContentType .APPLICATION_JSON ), "/translate/"
430
425
);
@@ -551,10 +546,10 @@ public void testNextPageText() throws IOException {
551
546
for (int i = 0 ; i < 20 ; i += 2 ) {
552
547
Tuple <String , String > response ;
553
548
if (i == 0 ) {
554
- response = runSqlAsText ("" , new StringEntity (request , ContentType .APPLICATION_JSON ), "text/plain" );
549
+ response = runSqlAsText (StringUtils . EMPTY , new StringEntity (request , ContentType .APPLICATION_JSON ), "text/plain" );
555
550
} else {
556
- response = runSqlAsText ("" , new StringEntity ("{\" cursor\" :\" " + cursor + "\" }" , ContentType . APPLICATION_JSON ) ,
557
- "text/plain" );
551
+ response = runSqlAsText (StringUtils . EMPTY , new StringEntity ("{\" cursor\" :\" " + cursor + "\" }" ,
552
+ ContentType . APPLICATION_JSON ), "text/plain" );
558
553
}
559
554
560
555
StringBuilder expected = new StringBuilder ();
@@ -570,9 +565,10 @@ public void testNextPageText() throws IOException {
570
565
}
571
566
Map <String , Object > expected = new HashMap <>();
572
567
expected .put ("rows" , emptyList ());
573
- assertResponse (expected , runSql ("" , new StringEntity ("{\" cursor\" :\" " + cursor + "\" }" , ContentType .APPLICATION_JSON )));
568
+ assertResponse (expected , runSql (new StringEntity ("{\" cursor\" :\" " + cursor + "\" }" , ContentType .APPLICATION_JSON ),
569
+ StringUtils .EMPTY ));
574
570
575
- Map <String , Object > response = runSql ("" , new StringEntity ("{\" cursor\" :\" " + cursor + "\" }" , ContentType .APPLICATION_JSON ),
571
+ Map <String , Object > response = runSql (new StringEntity ("{\" cursor\" :\" " + cursor + "\" }" , ContentType .APPLICATION_JSON ),
576
572
"/close" );
577
573
assertEquals (true , response .get ("succeeded" ));
578
574
@@ -638,7 +634,7 @@ public void testQueryInTSV() throws IOException {
638
634
}
639
635
640
636
private Tuple <String , String > runSqlAsText (String sql , String accept ) throws IOException {
641
- return runSqlAsText ("" , new StringEntity ("{\" query\" :\" " + sql + "\" }" , ContentType .APPLICATION_JSON ), accept );
637
+ return runSqlAsText (StringUtils . EMPTY , new StringEntity ("{\" query\" :\" " + sql + "\" }" , ContentType .APPLICATION_JSON ), accept );
642
638
}
643
639
644
640
/**
@@ -716,7 +712,11 @@ private static Map<String, Object> searchStats() throws IOException {
716
712
}
717
713
718
714
public static String randomMode () {
719
- return randomFrom ("" , "jdbc" , "plain" );
715
+ return randomFrom (StringUtils .EMPTY , "jdbc" , "plain" );
716
+ }
717
+
718
+ public static String mode (String mode ) {
719
+ return Strings .isEmpty (mode ) ? StringUtils .EMPTY : ",\" mode\" :\" " + mode + "\" " ;
720
720
}
721
721
722
722
private void index (String ... docs ) throws IOException {
0 commit comments