Skip to content

Commit

Permalink
Test fix
Browse files Browse the repository at this point in the history
  • Loading branch information
bbimber authored and lbergelson committed Aug 1, 2023
1 parent 662c827 commit 500cffd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
20 changes: 16 additions & 4 deletions src/main/java/htsjdk/beta/io/bundle/BundleJSON.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,19 +108,19 @@ public static <T extends IOPath> Bundle toBundle(
}

// validate the schema name
final String schemaName = jsonDocument.optString(JSON_PROPERTY_SCHEMA_NAME, null);
final String schemaName = getRequiredPropertyAsString(jsonDocument, JSON_PROPERTY_SCHEMA_NAME);
if (!schemaName.equals(JSON_SCHEMA_NAME)) {
throw new IllegalArgumentException(
String.format("Expected bundle schema name %s but found %s", JSON_SCHEMA_NAME, schemaName));
}

// validate the schema version
final String schemaVersion = jsonDocument.optString(JSON_PROPERTY_SCHEMA_VERSION, null);
final String schemaVersion = getRequiredPropertyAsString(jsonDocument, JSON_PROPERTY_SCHEMA_VERSION);
if (!schemaVersion.equals(JSON_SCHEMA_VERSION)) {
throw new IllegalArgumentException(String.format("Expected bundle schema version %s but found %s",
JSON_SCHEMA_VERSION, schemaVersion));
}
primaryContentType = jsonDocument.optString(JSON_PROPERTY_PRIMARY, null);
primaryContentType = getRequiredPropertyAsString(jsonDocument, JSON_PROPERTY_PRIMARY);

jsonDocument.keySet().forEach((String contentType) -> {
if (! (jsonDocument.get(contentType) instanceof JSONObject jsonDoc)) {
Expand All @@ -130,7 +130,7 @@ public static <T extends IOPath> Bundle toBundle(
if (!TOP_LEVEL_PROPERTIES.contains(contentType)) {
final String format = jsonDoc.optString(JSON_PROPERTY_FORMAT, null);
final IOPathResource ioPathResource = new IOPathResource(
ioPathConstructor.apply(jsonDoc.optString(JSON_PROPERTY_PATH, null)),
ioPathConstructor.apply(getRequiredPropertyAsString(jsonDoc, JSON_PROPERTY_PATH)),
contentType,
format == null ?
null :
Expand All @@ -147,4 +147,16 @@ public static <T extends IOPath> Bundle toBundle(

return new Bundle(primaryContentType, resources);
}

private static String getRequiredPropertyAsString(JSONObject jsonDocument, String propertyName) {
final String propertyValue = jsonDocument.optString(propertyName, null);
if (propertyValue == null) {
throw new IllegalArgumentException(
String.format("JSON bundle is missing the required property %s (%s)",
propertyName,
jsonDocument));
}

return propertyValue;
}
}
5 changes: 3 additions & 2 deletions src/test/java/htsjdk/beta/io/bundle/BundleJSONTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import htsjdk.HtsjdkTest;
import htsjdk.io.HtsPath;
import htsjdk.io.IOPath;
import org.json.JSONObject;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
Expand Down Expand Up @@ -157,7 +158,7 @@ public Object[][] getRoundTripJSON() {
public void testRoundTripJSON(final String jsonString, final String primaryKey, final List<BundleResource> resources) {
final Bundle bundleFromResources = new Bundle(primaryKey, resources);
final String actualJSONString = BundleJSON.toJSON(bundleFromResources);
Assert.assertEquals(actualJSONString, jsonString);
Assert.assertEquals(actualJSONString, new JSONObject(jsonString).toString(1));

// now recreate the bundle from JSON
final Bundle bundleFromJSON = BundleJSON.toBundle(jsonString);
Expand Down Expand Up @@ -227,7 +228,7 @@ public Object[][] getInvalidBundleJSON() {
// syntax error (missing quote in before schemaName
{ "{\"schemaVersion\":\"0.1.0\",schemaName\":\"htsbundle\",\"ALIGNED_READS\":{\"path\":\"myreads" +
".bam\",\"format\":\"BAM\"},\"primary\":\"ALIGNED_READS\"}",
"Invalid JSON near position: 25" },
"Expected a ':' after a key at 36" },
// no enclosing {} -> UnsupportedOperationException (no text message)
{"\"schemaName\":\"htsbundle\", \"schemaVersion\":\"0.1.0\"",
"", },
Expand Down

0 comments on commit 500cffd

Please sign in to comment.