Skip to content

Commit

Permalink
[AJ-1279] Add alternate constructors for AttributeSchema (#465)
Browse files Browse the repository at this point in the history
* Add alternate constructors for AttributeSchema

* Fix order of expected/actual values

* Remove public modifiers

* Add another test
  • Loading branch information
nawatts authored Jan 23, 2024
1 parent 19a01fc commit bef46a3
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -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());
}

Expand All @@ -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());
}

Expand All @@ -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());
}

Expand Down Expand Up @@ -2188,24 +2186,25 @@ void describeType() throws Exception {

List<AttributeSchema> 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);

Expand Down Expand Up @@ -2273,22 +2272,22 @@ void describeAllTypes() throws Exception {

List<AttributeSchema> 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<RecordTypeSchema> expectedSchemas =
Arrays.asList(
Expand Down
Original file line number Diff line number Diff line change
@@ -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"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ class RecordTypeSchemaTest {
void setUp() {
List<AttributeSchema> 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);
}
Expand All @@ -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
Expand Down

0 comments on commit bef46a3

Please sign in to comment.