Fall back to UNCATEGORIZED if the parsed category doesn't match any available categories.

This allows for deletion of categories, as the resources will just move into UNCATEGORIZED until they are next updated.
This commit is contained in:
CalDescent 2022-02-13 18:10:56 +00:00
parent a3d31bbaf1
commit ad9c466712
3 changed files with 29 additions and 1 deletions

View File

@ -42,7 +42,7 @@ public class ArbitraryDataTransactionMetadata extends ArbitraryDataMetadata {
this.tags = metadata.getString("tags");
}
if (metadata.has("category")) {
this.category = Category.valueOf(metadata.getString("category"));
this.category = Category.uncategorizedValueOf(metadata.getString("category"));
}
List<byte[]> chunksList = new ArrayList<>();

View File

@ -64,4 +64,18 @@ public enum Category {
return this.name;
}
/**
* Same as valueOf() but with fallback to UNCATEGORIZED if there's no match
* @param name
* @return a Category (using UNCATEGORIZED if no match found)
*/
public static Category uncategorizedValueOf(String name) {
try {
return Category.valueOf(name);
}
catch (IllegalArgumentException e) {
return Category.UNCATEGORIZED;
}
}
}

View File

@ -164,4 +164,18 @@ public class ArbitraryTransactionMetadataTests extends Common {
}
}
@Test
public void testExistingCategories() {
// Matching categories should be correctly located
assertEquals(Category.QORTAL, Category.uncategorizedValueOf("QORTAL"));
assertEquals(Category.TECHNOLOGY, Category.uncategorizedValueOf("TECHNOLOGY"));
}
@Test
public void testMissingCategory() {
// Missing or invalid categories should fall back to UNCATEGORIZED
assertEquals(Category.UNCATEGORIZED, Category.uncategorizedValueOf("INVALID_CATEGORY"));
assertEquals(Category.UNCATEGORIZED, Category.uncategorizedValueOf("Qortal")); // Case-sensitive match required
}
}