Skip to content

Commit 78242e8

Browse files
committed
Fully functional test case
Signed-off-by: Peter Nied <petern@amazon.com>
1 parent eb26f53 commit 78242e8

1 file changed

Lines changed: 19 additions & 15 deletions

File tree

src/integrationTest/java/org/opensearch/security/rest/CompressionTests.java

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import static org.hamcrest.CoreMatchers.containsString;
2525
import static org.hamcrest.CoreMatchers.equalTo;
2626
import static org.hamcrest.CoreMatchers.not;
27+
import static org.hamcrest.CoreMatchers.anyOf;
2728
import static org.hamcrest.MatcherAssert.assertThat;
2829
import org.opensearch.test.framework.TestSecurityConfig;
2930
import org.opensearch.test.framework.cluster.ClusterManager;
@@ -35,16 +36,13 @@
3536
import java.nio.charset.StandardCharsets;
3637
import java.util.List;
3738
import java.util.concurrent.CompletableFuture;
38-
import java.util.concurrent.ExecutionException;
3939
import java.util.concurrent.ForkJoinPool;
4040
import java.util.concurrent.TimeUnit;
41-
import java.util.concurrent.TimeoutException;
4241
import java.util.stream.Collectors;
4342
import java.util.stream.IntStream;
4443
import java.util.zip.GZIPOutputStream;
4544
import org.opensearch.test.framework.cluster.TestRestClient.HttpResponse;
4645

47-
import static org.junit.Assert.fail;
4846
import static org.opensearch.test.framework.TestSecurityConfig.AuthcDomain.AUTHC_HTTPBASIC_INTERNAL;
4947
import static org.opensearch.test.framework.TestSecurityConfig.Role.ALL_ACCESS;
5048
import 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

Comments
 (0)