2424import static org .hamcrest .CoreMatchers .containsString ;
2525import static org .hamcrest .CoreMatchers .equalTo ;
2626import static org .hamcrest .CoreMatchers .not ;
27+ import static org .hamcrest .CoreMatchers .anyOf ;
2728import static org .hamcrest .MatcherAssert .assertThat ;
2829import org .opensearch .test .framework .TestSecurityConfig ;
2930import org .opensearch .test .framework .cluster .ClusterManager ;
3536import java .nio .charset .StandardCharsets ;
3637import java .util .List ;
3738import java .util .concurrent .CompletableFuture ;
38- import java .util .concurrent .ExecutionException ;
3939import java .util .concurrent .ForkJoinPool ;
4040import java .util .concurrent .TimeUnit ;
41- import java .util .concurrent .TimeoutException ;
4241import java .util .stream .Collectors ;
4342import java .util .stream .IntStream ;
4443import java .util .zip .GZIPOutputStream ;
4544import org .opensearch .test .framework .cluster .TestRestClient .HttpResponse ;
4645
47- import static org .junit .Assert .fail ;
4846import static org .opensearch .test .framework .TestSecurityConfig .AuthcDomain .AUTHC_HTTPBASIC_INTERNAL ;
4947import static org .opensearch .test .framework .TestSecurityConfig .Role .ALL_ACCESS ;
5048import static org .opensearch .test .framework .cluster .TestRestClientConfiguration .getBasicAuthHeader ;
@@ -89,12 +87,13 @@ public void testAuthenticatedGzippedRequests() throws Exception {
8987
9088 waitingOn .stream ().forEach (future -> {
9189 try {
92- final HttpResponse response = future .get ();
93- response .assertStatusCode (HttpStatus .SC_OK );
90+ final HttpResponse response = future .get ();
91+ response .assertStatusCode (HttpStatus .SC_OK );
9492 } catch (final Exception ex ) {
9593 throw new RuntimeException (ex );
9694 }
97- });;
95+ });
96+ ;
9897 }
9998 }
10099
@@ -107,30 +106,35 @@ public void testMixOfAuthenticatedAndUnauthenticatedGzippedRequests() throws Exc
107106 final String rawBody = "{ \" query\" : { \" match\" : { \" foo\" : \" bar\" }}}" ;
108107
109108 final byte [] compressedRequestBody = createCompressedRequestBody (rawBody );
110- try (TestRestClient client = cluster .getRestClient (new BasicHeader ("Content-Encoding" , "gzip" ))) {
109+ try (final TestRestClient client = cluster .getRestClient (new BasicHeader ("Content-Encoding" , "gzip" ))) {
111110
112111 final ForkJoinPool forkJoinPool = new ForkJoinPool (parallelism );
113112
114113 final Header basicAuthHeader = getBasicAuthHeader (ADMIN_USER .getName (), ADMIN_USER .getPassword ());
115114
116- final List <CompletableFuture <Void >> waitingOn = IntStream .rangeClosed (1 , totalNumberOfRequests )
115+ final List <CompletableFuture <HttpResponse >> waitingOn = IntStream .rangeClosed (1 , totalNumberOfRequests )
117116 .boxed ()
118- .map (i -> CompletableFuture .runAsync (() -> {
117+ .map (i -> CompletableFuture .supplyAsync (() -> {
119118 final HttpPost post = new HttpPost (client .getHttpServerUri () + requestPath );
120119 post .setEntity (new ByteArrayEntity (compressedRequestBody , ContentType .APPLICATION_JSON ));
121- final TestRestClient .HttpResponse response = i % 2 == 0
122- ? client .executeRequest (post )
123- : client .executeRequest (post , basicAuthHeader );
124- assertThat (response .getStatusCode (), equalTo (i % 2 == 0 ? HttpStatus .SC_UNAUTHORIZED : HttpStatus .SC_OK ));
125- assertThat (response .getBody (), not (containsString ("json_parse_exception" )));
120+ return i % 2 == 0 ? client .executeRequest (post ) : client .executeRequest (post , basicAuthHeader );
126121 }, forkJoinPool ))
127122 .collect (Collectors .toList ());
128123
129124 final CompletableFuture <Void > allOfThem = CompletableFuture .allOf (waitingOn .toArray (new CompletableFuture [0 ]));
130125
131126 allOfThem .get (30 , TimeUnit .SECONDS );
132127
133-
128+ waitingOn .stream ().forEach (future -> {
129+ try {
130+ final HttpResponse response = future .get ();
131+ assertThat (response .getBody (), not (containsString ("json_parse_exception" )));
132+ assertThat (response .getStatusCode (), anyOf (equalTo (HttpStatus .SC_UNAUTHORIZED ), equalTo (HttpStatus .SC_OK )));
133+ } catch (final Exception ex ) {
134+ throw new RuntimeException (ex );
135+ }
136+ });
137+ ;
134138 }
135139 }
136140
0 commit comments