From bef46a3ed3c05b53184ff9864040f9226cbd8f15 Mon Sep 17 00:00:00 2001 From: Nick Watts <1156625+nawatts@users.noreply.github.com> Date: Tue, 23 Jan 2024 10:37:21 -0500 Subject: [PATCH] [AJ-1279] Add alternate constructors for AttributeSchema (#465) * Add alternate constructors for AttributeSchema * Fix order of expected/actual values * Remove public modifiers * Add another test --- .../service/model/AttributeSchema.java | 19 ++++- .../RecordControllerMockMvcTest.java | 79 +++++++++---------- .../service/model/AttributeSchemaTest.java | 24 ++++++ .../service/model/RecordTypeSchemaTest.java | 7 +- 4 files changed, 84 insertions(+), 45 deletions(-) create mode 100644 service/src/test/java/org/databiosphere/workspacedataservice/service/model/AttributeSchemaTest.java diff --git a/service/src/main/java/org/databiosphere/workspacedataservice/service/model/AttributeSchema.java b/service/src/main/java/org/databiosphere/workspacedataservice/service/model/AttributeSchema.java index 6a61bfe9e..1aa81c5f4 100644 --- a/service/src/main/java/org/databiosphere/workspacedataservice/service/model/AttributeSchema.java +++ b/service/src/main/java/org/databiosphere/workspacedataservice/service/model/AttributeSchema.java @@ -4,4 +4,21 @@ import org.databiosphere.workspacedataservice.shared.model.RecordType; @JsonInclude(JsonInclude.Include.NON_NULL) -public record AttributeSchema(String name, String datatype, RecordType relatesTo) {} +public record AttributeSchema(String name, String datatype, RecordType relatesTo) { + + public AttributeSchema(String name, DataTypeMapping datatype, RecordType relatesTo) { + this(name, datatype.name(), relatesTo); + } + + public AttributeSchema(String name, String datatype) { + this(name, datatype, null); + } + + public AttributeSchema(String name, DataTypeMapping datatype) { + this(name, datatype, null); + } + + public AttributeSchema(String name) { + this(name, (String) null, null); + } +} diff --git a/service/src/test/java/org/databiosphere/workspacedataservice/controller/RecordControllerMockMvcTest.java b/service/src/test/java/org/databiosphere/workspacedataservice/controller/RecordControllerMockMvcTest.java index e6ff917b4..1f657316e 100644 --- a/service/src/test/java/org/databiosphere/workspacedataservice/controller/RecordControllerMockMvcTest.java +++ b/service/src/test/java/org/databiosphere/workspacedataservice/controller/RecordControllerMockMvcTest.java @@ -2012,8 +2012,7 @@ void renameAttribute() throws Exception { recordType, attributeToRename) .contentType(MediaType.APPLICATION_JSON) - .content( - mapper.writeValueAsString(new AttributeSchema(newAttributeName, null, null)))) + .content(mapper.writeValueAsString(new AttributeSchema(newAttributeName)))) .andExpect(status().isOk()); MvcResult mvcResult = @@ -2050,7 +2049,7 @@ void renameNonExistentAttribute() throws Exception { recordType, "doesNotExist") .contentType(MediaType.APPLICATION_JSON) - .content(mapper.writeValueAsString(new AttributeSchema("newAttr", null, null)))) + .content(mapper.writeValueAsString(new AttributeSchema("newAttr")))) .andExpect(status().isNotFound()); } @@ -2071,7 +2070,7 @@ void renamePrimaryKeyAttribute() throws Exception { recordType, attributeToRename) .contentType(MediaType.APPLICATION_JSON) - .content(mapper.writeValueAsString(new AttributeSchema("newAttr", null, null)))) + .content(mapper.writeValueAsString(new AttributeSchema("newAttr")))) .andExpect(status().isBadRequest()); } @@ -2093,8 +2092,7 @@ void renameAttributeConflict() throws Exception { recordType, attributeToRename) .contentType(MediaType.APPLICATION_JSON) - .content( - mapper.writeValueAsString(new AttributeSchema(newAttributeName, null, null)))) + .content(mapper.writeValueAsString(new AttributeSchema(newAttributeName)))) .andExpect(status().isConflict()); } @@ -2188,24 +2186,25 @@ void describeType() throws Exception { List expectedAttributes = Arrays.asList( - new AttributeSchema("array-of-date", "ARRAY_OF_DATE", null), - new AttributeSchema("array-of-datetime", "ARRAY_OF_DATE_TIME", null), - new AttributeSchema("array-of-relation", "ARRAY_OF_RELATION", referencedType), - new AttributeSchema("array-of-string", "ARRAY_OF_STRING", null), - new AttributeSchema("attr-boolean", "BOOLEAN", null), - new AttributeSchema("attr-dt", "DATE_TIME", null), - new AttributeSchema("attr-json", "JSON", null), - new AttributeSchema("attr-ref", "RELATION", referencedType), - new AttributeSchema("attr1", "STRING", null), - new AttributeSchema("attr2", "NUMBER", null), - new AttributeSchema("attr3", "DATE", null), - new AttributeSchema("attr4", "STRING", null), - new AttributeSchema("attr5", "NUMBER", null), - new AttributeSchema("sys_name", "STRING", null), - new AttributeSchema("z-array-of-boolean", "ARRAY_OF_BOOLEAN", null), - new AttributeSchema("z-array-of-number-double", "ARRAY_OF_NUMBER", null), - new AttributeSchema("z-array-of-number-long", "ARRAY_OF_NUMBER", null), - new AttributeSchema("z-array-of-string", "ARRAY_OF_STRING", null)); + new AttributeSchema("array-of-date", DataTypeMapping.ARRAY_OF_DATE), + new AttributeSchema("array-of-datetime", DataTypeMapping.ARRAY_OF_DATE_TIME), + new AttributeSchema( + "array-of-relation", DataTypeMapping.ARRAY_OF_RELATION, referencedType), + new AttributeSchema("array-of-string", DataTypeMapping.ARRAY_OF_STRING), + new AttributeSchema("attr-boolean", DataTypeMapping.BOOLEAN), + new AttributeSchema("attr-dt", DataTypeMapping.DATE_TIME), + new AttributeSchema("attr-json", DataTypeMapping.JSON), + new AttributeSchema("attr-ref", DataTypeMapping.RELATION, referencedType), + new AttributeSchema("attr1", DataTypeMapping.STRING), + new AttributeSchema("attr2", DataTypeMapping.NUMBER), + new AttributeSchema("attr3", DataTypeMapping.DATE), + new AttributeSchema("attr4", DataTypeMapping.STRING), + new AttributeSchema("attr5", DataTypeMapping.NUMBER), + new AttributeSchema("sys_name", DataTypeMapping.STRING), + new AttributeSchema("z-array-of-boolean", DataTypeMapping.ARRAY_OF_BOOLEAN), + new AttributeSchema("z-array-of-number-double", DataTypeMapping.ARRAY_OF_NUMBER), + new AttributeSchema("z-array-of-number-long", DataTypeMapping.ARRAY_OF_NUMBER), + new AttributeSchema("z-array-of-string", DataTypeMapping.ARRAY_OF_STRING)); RecordTypeSchema expected = new RecordTypeSchema(type, expectedAttributes, 1, RECORD_ID); @@ -2273,22 +2272,22 @@ void describeAllTypes() throws Exception { List expectedAttributes = Arrays.asList( - new AttributeSchema("array-of-date", "ARRAY_OF_DATE", null), - new AttributeSchema("array-of-datetime", "ARRAY_OF_DATE_TIME", null), - new AttributeSchema("array-of-string", "ARRAY_OF_STRING", null), - new AttributeSchema("attr-boolean", "BOOLEAN", null), - new AttributeSchema("attr-dt", "DATE_TIME", null), - new AttributeSchema("attr-json", "JSON", null), - new AttributeSchema("attr1", "STRING", null), - new AttributeSchema("attr2", "NUMBER", null), - new AttributeSchema("attr3", "DATE", null), - new AttributeSchema("attr4", "STRING", null), - new AttributeSchema("attr5", "NUMBER", null), - new AttributeSchema("sys_name", "STRING", null), - new AttributeSchema("z-array-of-boolean", "ARRAY_OF_BOOLEAN", null), - new AttributeSchema("z-array-of-number-double", "ARRAY_OF_NUMBER", null), - new AttributeSchema("z-array-of-number-long", "ARRAY_OF_NUMBER", null), - new AttributeSchema("z-array-of-string", "ARRAY_OF_STRING", null)); + new AttributeSchema("array-of-date", DataTypeMapping.ARRAY_OF_DATE), + new AttributeSchema("array-of-datetime", DataTypeMapping.ARRAY_OF_DATE_TIME), + new AttributeSchema("array-of-string", DataTypeMapping.ARRAY_OF_STRING), + new AttributeSchema("attr-boolean", DataTypeMapping.BOOLEAN), + new AttributeSchema("attr-dt", DataTypeMapping.DATE_TIME), + new AttributeSchema("attr-json", DataTypeMapping.JSON), + new AttributeSchema("attr1", DataTypeMapping.STRING), + new AttributeSchema("attr2", DataTypeMapping.NUMBER), + new AttributeSchema("attr3", DataTypeMapping.DATE), + new AttributeSchema("attr4", DataTypeMapping.STRING), + new AttributeSchema("attr5", DataTypeMapping.NUMBER), + new AttributeSchema("sys_name", DataTypeMapping.STRING), + new AttributeSchema("z-array-of-boolean", DataTypeMapping.ARRAY_OF_BOOLEAN), + new AttributeSchema("z-array-of-number-double", DataTypeMapping.ARRAY_OF_NUMBER), + new AttributeSchema("z-array-of-number-long", DataTypeMapping.ARRAY_OF_NUMBER), + new AttributeSchema("z-array-of-string", DataTypeMapping.ARRAY_OF_STRING)); List expectedSchemas = Arrays.asList( diff --git a/service/src/test/java/org/databiosphere/workspacedataservice/service/model/AttributeSchemaTest.java b/service/src/test/java/org/databiosphere/workspacedataservice/service/model/AttributeSchemaTest.java new file mode 100644 index 000000000..e8ceb7976 --- /dev/null +++ b/service/src/test/java/org/databiosphere/workspacedataservice/service/model/AttributeSchemaTest.java @@ -0,0 +1,24 @@ +package org.databiosphere.workspacedataservice.service.model; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.databiosphere.workspacedataservice.shared.model.RecordType; +import org.junit.jupiter.api.Test; + +class AttributeSchemaTest { + @Test + void testAlternateConstructors() { + assertEquals( + new AttributeSchema("attr", "STRING", RecordType.valueOf("otherAttr")), + new AttributeSchema("attr", DataTypeMapping.STRING, RecordType.valueOf("otherAttr"))); + + assertEquals( + new AttributeSchema("attr", "STRING", null), new AttributeSchema("attr", "STRING")); + + assertEquals( + new AttributeSchema("attr", "STRING", null), + new AttributeSchema("attr", DataTypeMapping.STRING)); + + assertEquals(new AttributeSchema("attr", (String) null, null), new AttributeSchema("attr")); + } +} diff --git a/service/src/test/java/org/databiosphere/workspacedataservice/service/model/RecordTypeSchemaTest.java b/service/src/test/java/org/databiosphere/workspacedataservice/service/model/RecordTypeSchemaTest.java index f6fa5e1af..70b650d1e 100644 --- a/service/src/test/java/org/databiosphere/workspacedataservice/service/model/RecordTypeSchemaTest.java +++ b/service/src/test/java/org/databiosphere/workspacedataservice/service/model/RecordTypeSchemaTest.java @@ -23,8 +23,8 @@ class RecordTypeSchemaTest { void setUp() { List attributes = Arrays.asList( - new AttributeSchema(/* name */ "id", /* datatype */ "STRING", /* relatesTo */ null), - new AttributeSchema(/* name */ "attr1", /* datatype */ "STRING", /* relatesTo */ null)); + new AttributeSchema("id", DataTypeMapping.STRING), + new AttributeSchema("attr1", DataTypeMapping.STRING)); schema = new RecordTypeSchema(RECORD_TYPE, attributes, 0, PRIMARY_KEY); } @@ -44,8 +44,7 @@ void testContainsAttribute() { @Test void testGetAttributeSchema() { assertEquals( - schema.getAttributeSchema("attr1"), - new AttributeSchema(/* name */ "attr1", /* datatype */ "STRING", /* relatesTo */ null)); + new AttributeSchema("attr1", DataTypeMapping.STRING), schema.getAttributeSchema("attr1")); } @Test