forked from Qortal/qortal
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:
parent
a3d31bbaf1
commit
ad9c466712
@ -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<>();
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user