Skip to content

Commit

Permalink
Added the ability to update a Dataset
Browse files Browse the repository at this point in the history
  • Loading branch information
knighto82 committed Nov 21, 2024
1 parent 682387f commit 7732723
Show file tree
Hide file tree
Showing 22 changed files with 420 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,37 @@ public void testCreateDatasetOverrideDefaultCatalog() {
//TODO :: Contract for response of dataset.create() needs to be decided
}

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

wireMockRule.stubFor(WireMock.put(WireMock.urlEqualTo("/catalogs/common/datasets/SD0001"))
.withRequestBody(equalToJson(TestUtils.loadJsonForIt("dataset/dataset-SD0001-update-request.json")))
.willReturn(WireMock.aResponse()
.withHeader("Content-Type", "application/json")
.withStatus(200)
.withBodyFile("dataset/dataset-create-response.json")));

Map<String, Dataset> datasets = sdk.listDatasets("common", "SD0001", true);
Dataset originalDataset = datasets.get("SD0001");

// When
Dataset amendedDataset = originalDataset
.toBuilder()
.description("Updated Sample dataset description 1")
.build();

amendedDataset.update();

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

@Test
public void testListDatasets() {
// Given
Expand Down
24 changes: 22 additions & 2 deletions src/main/java/io/github/jpmorganchase/fusion/Fusion.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import io.github.jpmorganchase.fusion.api.APIManager;
import io.github.jpmorganchase.fusion.api.FusionAPIManager;
import io.github.jpmorganchase.fusion.api.context.APIContext;
import io.github.jpmorganchase.fusion.api.exception.APICallException;
import io.github.jpmorganchase.fusion.api.exception.ApiInputValidationException;
import io.github.jpmorganchase.fusion.api.exception.FileDownloadException;
Expand Down Expand Up @@ -52,12 +53,13 @@ public class Fusion {
private String defaultPath;
private String rootURL;

@Builder.Default
private APIResponseParser responseParser = new GsonAPIResponseParser();
private APIResponseParser responseParser;

@Builder.Default
private APIRequestSerializer requestSerializer = new GsonAPIRequestSerializer();

private APIContext apiContext;

/**
* Get the default download path - Please see default {@link FusionConfiguration}
*/
Expand Down Expand Up @@ -808,6 +810,10 @@ public static class FusionBuilder {
protected FusionConfiguration configuration =
FusionConfiguration.builder().build();

protected APIResponseParser responseParser;
protected APIRequestSerializer requestSerializer;
protected APIContext apiContext;

public FusionBuilder configuration(FusionConfiguration configuration) {
this.configuration = configuration;
return this;
Expand Down Expand Up @@ -856,6 +862,10 @@ private FusionBuilder defaultCatalog(String defaultCatalog) {
private FusionBuilder defaultPath(String defaultPath) {
return this;
}

private FusionBuilder apiContext(APIContext apiContext) {
return this;
}
}

private static class CustomFusionBuilder extends FusionBuilder {
Expand Down Expand Up @@ -894,6 +904,16 @@ public Fusion build() {
.build();
}

apiContext = APIContext.builder()
.apiManager(api)
.rootUrl(rootURL)
.defaultCatalog(defaultCatalog)
.build();

if (Objects.isNull(responseParser)) {
responseParser = new GsonAPIResponseParser(apiContext);
}

return super.build();
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/io/github/jpmorganchase/fusion/api/APIManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,14 @@ public interface APIManager extends APIDownloadOperations, APIUploadOperations {
* @throws APICallException if the response status indicates an error or the request fails
*/
String callAPIToPost(String apiPath, CatalogResource catalogResource);

/**
* Sends a PUT 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
* @param catalogResource the resource object to be serialized and sent as the request body
* @return the response body as a {@code String} if the request is successful
* @throws APICallException if the response status indicates an error or the request fails
*/
String callAPIToPut(String apiPath, CatalogResource catalogResource);
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,24 @@ public String callAPIToPost(String apiPath, CatalogResource catalogResource) thr
return response.getBody();
}

/**
* Sends a PUT 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
* @param catalogResource the resource object to be serialized and sent as the request body
* @return the response body as a {@code String} if the request is successful
* @throws APICallException if the response status indicates an error or the request fails
*/
@Override
public String callAPIToPut(String apiPath, CatalogResource catalogResource) {
Map<String, String> requestHeaders = new HashMap<>();
requestHeaders.put("Authorization", "Bearer " + tokenProvider.getSessionBearerToken());

HttpResponse<String> response = httpClient.put(apiPath, serializer.serialize(catalogResource), requestHeaders);
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
@@ -0,0 +1,18 @@
package io.github.jpmorganchase.fusion.api.context;

import io.github.jpmorganchase.fusion.api.APIManager;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import lombok.Value;

@Value
@Builder
@EqualsAndHashCode
@ToString
public class APIContext {

APIManager apiManager;
String rootUrl;
String defaultCatalog;
}
2 changes: 2 additions & 0 deletions src/main/java/io/github/jpmorganchase/fusion/http/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ public interface Client {

HttpResponse<String> put(String path, Map<String, String> headers, InputStream body);

HttpResponse<String> put(String path, String body, Map<String, String> headers);

HttpResponse<String> delete(String path, Map<String, String> headers, String body);
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public HttpResponse<String> post(String path, Map<String, String> headers, Strin
return executeMethod(METHOD_POST, path, headers, body);
}

@Override
public HttpResponse<String> put(String path, String body, Map<String, String> headers) {
return executeMethod(METHOD_PUT, path, headers, body);
}

@Override
public HttpResponse<String> put(String path, Map<String, String> headers, InputStream body) {
if (body == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ public abstract class CatalogResource {

private final String identifier;

@Setter
private Map<String, Object> varArgs;

@Expose(serialize = false, deserialize = false)
Expand All @@ -26,6 +25,7 @@ public abstract class CatalogResource {
private final String rootUrl;

@Expose(serialize = false, deserialize = false)
@Getter
private final String catalogIdentifier;

public CatalogResource(
Expand All @@ -45,6 +45,10 @@ public String create() {
return this.apiManager.callAPIToPost(getApiPath(), this);
}

public String update() {
return this.apiManager.callAPIToPut(getApiPath(), this);
}

/**
* 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
12 changes: 6 additions & 6 deletions src/main/java/io/github/jpmorganchase/fusion/model/Dataset.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ public class Dataset extends CatalogResource {
String title;
String frequency;

@Builder
@Builder(toBuilder = true)
public Dataset(
String identifier,
Map<String, Object> varArgs,
APIManager apiManager,
String rootUrl,
String catalogIdentifier,
@Builder.ObtainVia(method = "getIdentifier") String identifier,
@Builder.ObtainVia(method = "getVarArgs") Map<String, Object> varArgs,
@Builder.ObtainVia(method = "getApiManager") APIManager apiManager,
@Builder.ObtainVia(method = "getRootUrl") String rootUrl,
@Builder.ObtainVia(method = "getCatalogIdentifier") String catalogIdentifier,
String description,
String linkedEntity,
String title,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.github.jpmorganchase.fusion.api.response.UploadedPart;
import io.github.jpmorganchase.fusion.model.*;
import io.github.jpmorganchase.fusion.serializing.mutation.ResourceMutationFactory;
import java.util.Map;

public interface APIResponseParser {
Expand All @@ -20,7 +21,7 @@ public interface APIResponseParser {
<T extends CatalogResource> Map<String, T> parseResourcesFromResponse(String json, Class<T> resourceClass);

<T extends CatalogResource> Map<String, T> parseResourcesWithVarArgsFromResponse(
String json, Class<T> resourceClass);
String json, Class<T> resourceClass, ResourceMutationFactory<T> mutator);

Map<String, Map<String, Object>> parseResourcesUntyped(String json);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

import com.google.gson.*;
import com.google.gson.reflect.TypeToken;
import io.github.jpmorganchase.fusion.api.context.APIContext;
import io.github.jpmorganchase.fusion.api.response.UploadedPart;
import io.github.jpmorganchase.fusion.model.*;
import io.github.jpmorganchase.fusion.serializing.mutation.MutationContext;
import io.github.jpmorganchase.fusion.serializing.mutation.ResourceMutationFactory;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.Field;
import java.lang.reflect.Type;
Expand All @@ -22,16 +25,22 @@ public class GsonAPIResponseParser implements APIResponseParser {
LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

private final Gson gson;
private final APIContext apiContext;

public GsonAPIResponseParser() {
this(APIContext.builder().build());
}

public GsonAPIResponseParser(APIContext apiContext) {
GsonBuilder gsonBuilder = new GsonBuilder();
// TODO: need to add a serializer as well once we get to update operations
gsonBuilder.registerTypeAdapter(LocalDate.class, new LocalDateDeserializer());
gson = gsonBuilder.create();
this.gson = gsonBuilder.create();
this.apiContext = apiContext;
}

public GsonAPIResponseParser(Gson gson) {
public GsonAPIResponseParser(Gson gson, APIContext apiContext) {
this.gson = gson;
this.apiContext = apiContext;
}

@Override
Expand All @@ -41,7 +50,12 @@ public Map<String, Catalog> parseCatalogResponse(String json) {

@Override
public Map<String, Dataset> parseDatasetResponse(String json) {
return parseResourcesWithVarArgsFromResponse(json, Dataset.class);
return parseResourcesWithVarArgsFromResponse(json, Dataset.class, (resource, mc) -> resource.toBuilder()
.varArgs(mc.getVarArgs())
.apiManager(mc.getApiContext().getApiManager())
.rootUrl(mc.getApiContext().getRootUrl())
.catalogIdentifier(mc.getApiContext().getDefaultCatalog())
.build());
}

@Override
Expand Down Expand Up @@ -76,23 +90,22 @@ public UploadedPart parseUploadPartResponse(String json) {

@Override
public <T extends CatalogResource> Map<String, T> parseResourcesWithVarArgsFromResponse(
String json, Class<T> resourceClass) {
String json, Class<T> resourceClass, ResourceMutationFactory<T> mutator) {

Map<String, Map<String, Object>> untypedResources = parseResourcesUntyped(json);
JsonArray resources = getResources(json);

Set<String> excludes = varArgsExclusions(resourceClass);
List<T> resourceList = new ArrayList<>();
for (JsonElement element : resources) {
resourceList.add(parseResourceWithVarArgs(resourceClass, excludes, element, untypedResources));
resourceList.add(parseResourceWithVarArgs(resourceClass, excludes, element, untypedResources, mutator));
}

return collectMapOfUniqueResources(resourceList);
}

@Override
public <T extends CatalogResource> Map<String, T> parseResourcesFromResponse(String json, Class<T> resourceClass) {
// TODO: handle varArgs

JsonArray resources = getResources(json);

Expand Down Expand Up @@ -146,13 +159,17 @@ private <T extends CatalogResource> T parseResourceWithVarArgs(
Class<T> resourceClass,
Set<String> excludes,
JsonElement element,
Map<String, Map<String, Object>> untypedResources) {
Map<String, Map<String, Object>> untypedResources,
ResourceMutationFactory<T> mutator) {
T obj = gson.fromJson(element, resourceClass);

Map<String, Object> varArgs = getVarArgsToInclude(untypedResources.get(obj.getIdentifier()), excludes);

obj.setVarArgs(varArgs);
return obj;
return mutator.mutate(
obj,
MutationContext.builder()
.varArgs(varArgs)
.apiContext(apiContext)
.build());
}

private Map<String, Object> getVarArgsToInclude(Map<String, Object> untypedResource, Set<String> exclusionList) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.github.jpmorganchase.fusion.serializing.mutation;

import io.github.jpmorganchase.fusion.api.context.APIContext;
import java.util.Map;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import lombok.Value;

@Value
@Builder
@EqualsAndHashCode
@ToString
public class MutationContext {

APIContext apiContext;
Map<String, Object> varArgs;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.github.jpmorganchase.fusion.serializing.mutation;

import io.github.jpmorganchase.fusion.model.CatalogResource;

@FunctionalInterface
public interface ResourceMutationFactory<T extends CatalogResource> {
T mutate(T resource, MutationContext mc);
}
Loading

0 comments on commit 7732723

Please sign in to comment.