Skip to content

Commit

Permalink
Added ability to delete a Dataset
Browse files Browse the repository at this point in the history
  • Loading branch information
knighto82 committed Nov 26, 2024
1 parent 8c27012 commit e506d8e
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import static com.github.tomakehurst.wiremock.client.WireMock.equalToJson;
import static io.github.jpmorganchase.fusion.test.TestUtils.listOf;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;

Expand Down Expand Up @@ -166,7 +167,7 @@ public void testUpdateDataset() {
dataset.update();

// Then Verify the response
//TODO :: Contract for response of dataset.create() needs to be decided
//TODO :: Contract for response of dataset.update() needs to be decided
}

@Test
Expand Down Expand Up @@ -197,7 +198,48 @@ public void testUpdateDatasetRetrievedFromListDatasets() {
amendedDataset.update();

// Then Verify the response
//TODO :: Contract for response of dataset.create() needs to be decided
//TODO :: Contract for response of dataset.update() needs to be decided
}

@Test
public void testDeleteDataset() {
// Given
wireMockRule.stubFor(WireMock.delete(WireMock.urlEqualTo("/catalogs/common/datasets/SD0001"))
.willReturn(WireMock.aResponse()
.withHeader("Content-Type", "application/json")
.withStatus(200)
.withBodyFile("dataset/dataset-delete-response.json")));

Dataset dataset = sdk.builders().dataset()
.identifier("SD0001")
.build();

// When
dataset.delete();

// Then Verify the response
//TODO :: Contract for response of dataset.delete() needs to be decided
}

@Test
public void testDeleteDatasetWithCatalogOverride() {
// Given
wireMockRule.stubFor(WireMock.delete(WireMock.urlEqualTo("/catalogs/foobar/datasets/SD0001"))
.willReturn(WireMock.aResponse()
.withHeader("Content-Type", "application/json")
.withStatus(200)
.withBodyFile("dataset/dataset-delete-response.json")));

Dataset dataset = sdk.builders().dataset()
.identifier("SD0001")
.catalogIdentifier("foobar")
.build();

// When
dataset.delete();

// Then Verify the response
//TODO :: Contract for response of dataset.delete() needs to be decided
}

@Test
Expand All @@ -213,10 +255,9 @@ public void testListDatasets() {
Map<String, Dataset> datasets = sdk.listDatasets();

// Then Verify the response
MatcherAssert.assertThat(datasets.containsKey("SD0001"), is(equalTo(true)));
MatcherAssert.assertThat(datasets.containsKey("SD0002"), is(equalTo(true)));
MatcherAssert.assertThat(datasets.containsKey("SD0003"), is(equalTo(true)));

assertThat(datasets.containsKey("SD0001"), is(equalTo(true)));
assertThat(datasets.containsKey("SD0002"), is(equalTo(true)));
assertThat(datasets.containsKey("SD0003"), is(equalTo(true)));
}

@Test
Expand All @@ -232,9 +273,9 @@ public void testListDatasetsUsingIdContains() {
Map<String, Dataset> datasets = sdk.listDatasets("common", "SD0001", true);

// Then Verify the response
MatcherAssert.assertThat(datasets.containsKey("SD0001"), is(equalTo(true)));
MatcherAssert.assertThat(datasets.containsKey("SD0002"), is(equalTo(false)));
MatcherAssert.assertThat(datasets.containsKey("SD0003"), is(equalTo(false)));
assertThat(datasets.containsKey("SD0001"), is(equalTo(true)));
assertThat(datasets.containsKey("SD0002"), is(equalTo(false)));
assertThat(datasets.containsKey("SD0003"), is(equalTo(false)));

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,12 @@ public interface APIManager extends APIDownloadOperations, APIUploadOperations {
* @throws APICallException if the response status indicates an error or the request fails
*/
String callAPIToPut(String apiPath, CatalogResource catalogResource);

/**
* Sends a DELETE request to the specified API endpoint with the provided catalog resource.
*
* @param apiPath the API endpoint path to which the PUT request will be sent
* @throws APICallException if the response status indicates an error or the request fails
*/
String callAPIToDelete(String apiPath);
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,22 @@ public String callAPIToPut(String apiPath, CatalogResource catalogResource) {
return response.getBody();
}

/**
* Sends a DELETE request to the specified API endpoint with the provided catalog resource.
*
* @param apiPath the API endpoint path to which the PUT request will be sent
* @throws APICallException if the response status indicates an error or the request fails
*/
@Override
public String callAPIToDelete(String apiPath) {
Map<String, String> requestHeaders = new HashMap<>();
requestHeaders.put("Authorization", "Bearer " + tokenProvider.getSessionBearerToken());

HttpResponse<String> response = httpClient.delete(apiPath, requestHeaders, null);
checkResponseStatus(response);
return response.getBody();
}

@Override
public void callAPIFileDownload(
String apiPath, String fileName, String catalog, String dataset, Map<String, String> headers)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ public String update() {
return this.apiManager.callAPIToPut(getApiPath(), this);
}

public String delete() {
return this.apiManager.callAPIToDelete(getApiPath());
}

/**
* Returns the API path used to perform operations on this catalog resource.
* This path is utilized for creating, reading, updating, and deleting the resource.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,19 @@ void successfulPutCall() {
thenTheResponseBodyShouldMatchExpected();
}

@Test
void successfulDeleteCall() {
givenFusionApiManager();
givenApiPath("http://localhost:8080/test");
givenSessionBearerToken("my-token");
givenResponseBody("sample response");
givenCatalogResource("dataset_one");
givenRequestHeader("Authorization", "Bearer my-token");
givenCallToClientToDeleteIsSuccessful();
WhenFusionApiManagerIsCalledToDelete();
thenTheResponseBodyShouldMatchExpected();
}

private void givenSerializedCatalogResource(String identifier) {
serializedCatalogResource = String.format("{\"identifier\":\"%s\"}", identifier);
}
Expand Down Expand Up @@ -146,6 +159,10 @@ private void WhenFusionApiManagerIsCalledToPut() {
actualResponse = fusionAPIManager.callAPIToPut(apiPath, catalogResource);
}

private void WhenFusionApiManagerIsCalledToDelete() {
actualResponse = fusionAPIManager.callAPIToDelete(apiPath);
}

private void givenResponseBody(String responseBody) {
this.responseBody = responseBody;
}
Expand Down Expand Up @@ -174,6 +191,14 @@ private void givenCallToClientToPutIsSuccessful() {
when(client.put(apiPath, serializedCatalogResource, requestHeaders)).thenReturn(expectedHttpResponse);
}

private void givenCallToClientToDeleteIsSuccessful() {
HttpResponse<String> expectedHttpResponse = HttpResponse.<String>builder()
.statusCode(200)
.body(responseBody)
.build();
when(client.delete(apiPath, requestHeaders, null)).thenReturn(expectedHttpResponse);
}

private void thenExceptionMessageShouldMatchExpected(String message) {
assertThat(thrown.getMessage(), is(equalTo(message)));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"{\"message\": \"Success\"}"

0 comments on commit e506d8e

Please sign in to comment.