Skip to content

Commit

Permalink
DCJ-515: Fix json parsing for dataset updates (#2358)
Browse files Browse the repository at this point in the history
  • Loading branch information
rushtong authored Jul 15, 2024
1 parent ea6a428 commit e03a530
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
package org.broadinstitute.consent.http.models;

import com.google.gson.Gson;
import java.util.List;
import org.broadinstitute.consent.http.util.gson.GsonUtil;

public record DatasetUpdate (
public record DatasetUpdate(
String name,
Integer dacId,
List<DatasetProperty> properties
){
) {

private static final Gson GSON = GsonUtil.gsonBuilderWithAdapters().create();

public DatasetUpdate(String json) {
this(GSON.fromJson(json, DatasetUpdate.class).getName(),
GSON.fromJson(json, DatasetUpdate.class).getDacId(),
GSON.fromJson(json, DatasetUpdate.class).getDatasetProperties());
}

public String getName() {
return this.name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,12 +233,10 @@ public Response updateByDatasetUpdate(
@FormDataParam("dataset") String json) {

try {

DatasetUpdate update = new Gson().fromJson(json, DatasetUpdate.class);

if (Objects.isNull(update)) {
if (json == null || json.isEmpty()) {
throw new BadRequestException("Dataset is required");
}
DatasetUpdate update = new DatasetUpdate(json);

Dataset datasetExists = datasetService.findDatasetById(datasetId);
if (Objects.isNull(datasetExists)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.cloud.storage.BlobId;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.ToNumberPolicy;
import java.sql.Timestamp;
import java.time.Instant;
import java.util.Date;
Expand All @@ -29,6 +30,7 @@ public static Gson buildGsonNullSerializer() {

public static GsonBuilder gsonBuilderWithAdapters() {
return new GsonBuilder()
.setObjectToNumberStrategy(ToNumberPolicy.LONG_OR_DOUBLE)
.registerTypeAdapter(
Instant.class,
new InstantTypeAdapter())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package org.broadinstitute.consent.http.models;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.util.ArrayList;
import java.util.List;
import org.broadinstitute.consent.http.models.dataset_registration_v1.FileTypeObject;
import org.broadinstitute.consent.http.models.dataset_registration_v1.FileTypeObject.FileType;
import org.junit.jupiter.api.Test;

public class DatasetUpdateTest {

@Test
void testDatasetUpdate() {
String json = """
{
"name": "Test Dataset Update",
"dacId": 1,
"properties": [
{
"propertyName": "string value",
"propertyValue": "TDR Location"
},
{
"propertyName": "long value",
"propertyValue": 12
},
{
"propertyName": "boolean value",
"propertyValue": true
},
{
"propertyName": "double value",
"propertyValue": 10.001
},
{
"propertyName": "File Types",
"propertyValue": [{"fileType":"ARRAYS","functionalEquivalence":"testing"}]
}
]
}""";
DatasetUpdate update = new DatasetUpdate(json);

List<DatasetProperty> props = update.getDatasetProperties();
assertEquals("TDR Location", getPropByName("string value", props).getPropertyValue());
assertEquals(12L, getPropByName("long value", props).getPropertyValue());
assertEquals(true, getPropByName("boolean value", props).getPropertyValue());
assertEquals(10.001D, getPropByName("double value", props).getPropertyValue());

// Parsing the object value of this prop is a little complicated due to JSON serialization
DatasetProperty fileTypeProp = getPropByName("File Types", props);
java.lang.reflect.Type listOfFileTypes = new TypeToken<ArrayList<FileTypeObject>>() {}.getType();
Gson gson = new Gson();
List<FileTypeObject> fileTypes = gson.fromJson(fileTypeProp.getPropertyValueAsString(), listOfFileTypes);
assertFalse(fileTypes.isEmpty());
FileTypeObject type = fileTypes.get(0);
assertEquals(FileType.ARRAYS, type.getFileType());
}

private DatasetProperty getPropByName(String name, List<DatasetProperty> props ) {
return props.stream().filter(p -> p.getPropertyName().equals(name)).findFirst().orElse(null);
}

@Test
void testDatasetUpdateNullValues() {
String json = """
{
"not_a_name": "Test Dataset Update",
"not_a_dac_id": 1,
"no_properties": [
{
"propertyName": "string value",
"propertyValue": "TDR Location"
}
]
}""";
DatasetUpdate update = new DatasetUpdate(json);
assertNull(update.getName());
assertNull(update.getDacId());
assertNull(update.getDatasetProperties());
}

}

0 comments on commit e03a530

Please sign in to comment.