From 500cffda9c511d88a736bcbb91d3f10259b967fb Mon Sep 17 00:00:00 2001 From: bbimber Date: Sun, 25 Jun 2023 22:18:47 -0700 Subject: [PATCH] Test fix --- .../htsjdk/beta/io/bundle/BundleJSON.java | 20 +++++++++++++++---- .../htsjdk/beta/io/bundle/BundleJSONTest.java | 5 +++-- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/main/java/htsjdk/beta/io/bundle/BundleJSON.java b/src/main/java/htsjdk/beta/io/bundle/BundleJSON.java index c6358ea862..324e2b8bed 100644 --- a/src/main/java/htsjdk/beta/io/bundle/BundleJSON.java +++ b/src/main/java/htsjdk/beta/io/bundle/BundleJSON.java @@ -108,19 +108,19 @@ public static 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)) { @@ -130,7 +130,7 @@ public static 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 : @@ -147,4 +147,16 @@ public static 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; + } } diff --git a/src/test/java/htsjdk/beta/io/bundle/BundleJSONTest.java b/src/test/java/htsjdk/beta/io/bundle/BundleJSONTest.java index 43a9377251..54eadf14eb 100644 --- a/src/test/java/htsjdk/beta/io/bundle/BundleJSONTest.java +++ b/src/test/java/htsjdk/beta/io/bundle/BundleJSONTest.java @@ -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; @@ -157,7 +158,7 @@ public Object[][] getRoundTripJSON() { public void testRoundTripJSON(final String jsonString, final String primaryKey, final List 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); @@ -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\"", "", },