Skip to content

Commit

Permalink
Create re-usable methods
Browse files Browse the repository at this point in the history
Signed-off-by: Craig Perkins <[email protected]>
  • Loading branch information
cwperks committed Jan 10, 2025
1 parent f20daa6 commit 4c17c5c
Showing 1 changed file with 47 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void testPluginsAreInstalled() throws IOException {
);
}

private static Map<String, String> createSampleResource(String name, Optional<Tuple<String, String>> credentials) throws IOException {
private Map<String, String> createSampleResource(String name, Optional<Tuple<String, String>> credentials) throws IOException {
RequestOptions.Builder options = RequestOptions.DEFAULT.toBuilder();
options.setWarningsHandler((warnings) -> false);
credentials.ifPresent(
Expand All @@ -79,7 +79,31 @@ private static Map<String, String> createSampleResource(String name, Optional<Tu
return createResourceResponse;
}

private static Map<String, String> updateSharing(String resourceId, String payload, Optional<Tuple<String, String>> credentials)
private Map<String, Object> listSampleResource(Optional<Tuple<String, String>> credentials) throws IOException {
RequestOptions.Builder options = RequestOptions.DEFAULT.toBuilder();
options.setWarningsHandler((warnings) -> false);
credentials.ifPresent(
stringStringTuple -> options.addHeader(
"Authorization",
"Basic "
+ Base64.getEncoder()
.encodeToString((stringStringTuple.v1() + ":" + stringStringTuple.v2()).getBytes(StandardCharsets.UTF_8))
)
);

Request listRequest = new Request("GET", "/_plugins/resource_sharing_example/resource");
listRequest.setOptions(options);
Response listResponse = client().performRequest(listRequest);
Map<String, Object> listResourceResponse = JsonXContent.jsonXContent.createParser(
NamedXContentRegistry.EMPTY,
LoggingDeprecationHandler.INSTANCE,
listResponse.getEntity().getContent()
).map();
System.out.println("listResourceResponse: " + listResourceResponse);
return listResourceResponse;
}

private Map<String, String> updateSharing(String resourceId, String payload, Optional<Tuple<String, String>> credentials)
throws IOException {
RequestOptions.Builder options = RequestOptions.DEFAULT.toBuilder();
options.setWarningsHandler((warnings) -> false);
Expand All @@ -104,6 +128,21 @@ private static Map<String, String> updateSharing(String resourceId, String paylo
return updateSharingResponse;
}

private static Map<String, Object> listSampleResourcesWithAdminClient() throws IOException {
RequestOptions.Builder options = RequestOptions.DEFAULT.toBuilder();
options.setWarningsHandler((warnings) -> false);
Request listRequest = new Request("GET", "/_plugins/resource_sharing_example/resource");
listRequest.setOptions(options);
Response listResponse = client().performRequest(listRequest);
Map<String, Object> listResourceResponse = JsonXContent.jsonXContent.createParser(
NamedXContentRegistry.EMPTY,
LoggingDeprecationHandler.INSTANCE,
listResponse.getEntity().getContent()
).map();
System.out.println("listResourceResponse: " + listResourceResponse);
return listResourceResponse;
}

public void testCreateSampleResource() throws IOException, InterruptedException {
RequestOptions.Builder options = RequestOptions.DEFAULT.toBuilder();
options.setWarningsHandler((warnings) -> false);
Expand All @@ -121,25 +160,11 @@ public void testCreateSampleResource() throws IOException, InterruptedException
// Sleep to give ResourceSharingListener time to create the .resource-sharing index
Thread.sleep(1000);

Request listRequest = new Request("GET", "/_plugins/resource_sharing_example/resource");
listRequest.setOptions(options);
Response listResponse = client().performRequest(listRequest);
Map<String, Object> listResourceResponse = JsonXContent.jsonXContent.createParser(
NamedXContentRegistry.EMPTY,
LoggingDeprecationHandler.INSTANCE,
listResponse.getEntity().getContent()
).map();
Map<String, Object> listResourceResponse = listSampleResource(Optional.empty());
System.out.println("listResourceResponse: " + listResourceResponse);

Request resourceSharingRequest = new Request("POST", "/.sample_extension_resources/_search");
resourceSharingRequest.setOptions(options);
Response resourceSharingResponse = adminClient().performRequest(resourceSharingRequest);
Map<String, Object> resourceSharingResponseMap = JsonXContent.jsonXContent.createParser(
NamedXContentRegistry.EMPTY,
LoggingDeprecationHandler.INSTANCE,
resourceSharingResponse.getEntity().getContent()
).map();
System.out.println("sampleResources: " + resourceSharingResponseMap);
Map<String, Object> allSampleResources = listSampleResourcesWithAdminClient();
System.out.println("allSampleResources: " + allSampleResources);

Map<String, String> updateSharingResponse = updateSharing(
resourceId,
Expand All @@ -150,20 +175,10 @@ public void testCreateSampleResource() throws IOException, InterruptedException

Thread.sleep(1000);

resourceSharingResponse = adminClient().performRequest(resourceSharingRequest);
resourceSharingResponseMap = JsonXContent.jsonXContent.createParser(
NamedXContentRegistry.EMPTY,
LoggingDeprecationHandler.INSTANCE,
resourceSharingResponse.getEntity().getContent()
).map();
System.out.println("sampleResources after update: " + resourceSharingResponseMap);
allSampleResources = listSampleResourcesWithAdminClient();
System.out.println("allSampleResources after update: " + allSampleResources);

listResponse = client().performRequest(listRequest);
listResourceResponse = JsonXContent.jsonXContent.createParser(
NamedXContentRegistry.EMPTY,
LoggingDeprecationHandler.INSTANCE,
listResponse.getEntity().getContent()
).map();
listResourceResponse = listSampleResource(Optional.empty());
System.out.println("listResourceResponse after update: " + listResourceResponse);
}
}

0 comments on commit 4c17c5c

Please sign in to comment.