Skip to content

Commit

Permalink
Merge pull request #9010 from bencomp/overload-datasetfield
Browse files Browse the repository at this point in the history
Overload DatasetField method to remove type check
  • Loading branch information
landreev authored Jul 19, 2024
2 parents 4615f4f + b143a89 commit bfbfa4d
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 13 deletions.
32 changes: 19 additions & 13 deletions src/main/java/edu/harvard/iq/dataverse/DatasetField.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,18 @@ public int compare(DatasetField o1, DatasetField o2) {
o2.getDatasetFieldType().getDisplayOrder() );
}};

public static DatasetField createNewEmptyDatasetField(DatasetFieldType dsfType, Object dsv) {
public static DatasetField createNewEmptyDatasetField(DatasetFieldType dsfType, DatasetVersion dsv) {

DatasetField dsfv = createNewEmptyDatasetField(dsfType);
//TODO - a better way to handle this?
if (dsv.getClass().getName().equals("edu.harvard.iq.dataverse.DatasetVersion")){
dsfv.setDatasetVersion((DatasetVersion)dsv);
} else {
dsfv.setTemplate((Template)dsv);
}
dsfv.setDatasetVersion(dsv);

return dsfv;
}

public static DatasetField createNewEmptyDatasetField(DatasetFieldType dsfType, Template dsv) {

DatasetField dsfv = createNewEmptyDatasetField(dsfType);
dsfv.setTemplate(dsv);

return dsfv;
}
Expand Down Expand Up @@ -545,25 +548,28 @@ public String toString() {
return "edu.harvard.iq.dataverse.DatasetField[ id=" + id + " ]";
}

public DatasetField copy(Object version) {
public DatasetField copy(DatasetVersion version) {
return copy(version, null);
}
public DatasetField copy(Template template) {
return copy(template, null);
}

// originally this was an overloaded method, but we renamed it to get around an issue with Bean Validation
// (that looked t overloaded methods, when it meant to look at overriden methods
public DatasetField copyChild(DatasetFieldCompoundValue parent) {
return copy(null, parent);
}

private DatasetField copy(Object version, DatasetFieldCompoundValue parent) {
private DatasetField copy(Object versionOrTemplate, DatasetFieldCompoundValue parent) {
DatasetField dsf = new DatasetField();
dsf.setDatasetFieldType(datasetFieldType);

if (version != null) {
if (version.getClass().getName().equals("edu.harvard.iq.dataverse.DatasetVersion")) {
dsf.setDatasetVersion((DatasetVersion) version);
if (versionOrTemplate != null) {
if (versionOrTemplate instanceof DatasetVersion) {
dsf.setDatasetVersion((DatasetVersion) versionOrTemplate);
} else {
dsf.setTemplate((Template) version);
dsf.setTemplate((Template) versionOrTemplate);
}
}

Expand Down
64 changes: 64 additions & 0 deletions src/test/java/edu/harvard/iq/dataverse/DatasetFieldTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package edu.harvard.iq.dataverse;

import edu.harvard.iq.dataverse.DatasetFieldType.FieldType;
import edu.harvard.iq.dataverse.mocks.MocksFactory;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertNull;


class DatasetFieldTest {
@Test
void testCreateNewEmptyDatasetField_withEmptyTemplate() {
Template template = new Template();

DatasetField field = DatasetField.createNewEmptyDatasetField(new DatasetFieldType("subject", FieldType.TEXT, false), template);
assertTrue(field.getTemplate() == template);
assertTrue(template.getDatasetFields().isEmpty());
}

@Test
void testNotEqualDatasetFields() {
DatasetFieldType type1 = new DatasetFieldType("subject", FieldType.TEXT, false);
Template template1 = new Template();
DatasetField field1 = DatasetField.createNewEmptyDatasetField(type1, template1);
field1.setId(MocksFactory.nextId());
DatasetFieldType type2 = new DatasetFieldType("subject", FieldType.TEXT, false);
Template template2 = new Template();
DatasetField field2 = DatasetField.createNewEmptyDatasetField(type2, template2);
field2.setId(MocksFactory.nextId());

assertNotEquals(field1, field2);
assertNotEquals(field1, template2);
}

@Test
void testEqualDatasetFields() {
DatasetField field1 = DatasetField.createNewEmptyDatasetField(new DatasetFieldType("subject", FieldType.TEXT, false), new Template());
field1.setId(100L);
DatasetField field2 = DatasetField.createNewEmptyDatasetField(new DatasetFieldType("subject", FieldType.TEXT, false), new Template());

// Fields are not equal before both have IDs set
assertNotEquals(field1, field2);

field2.setId(100L);

assertEquals(field1, field2);
}

@Test
void testCopyDatasetFields() {
DatasetField field1 = DatasetField.createNewEmptyDatasetField(new DatasetFieldType("subject", FieldType.TEXT, false), new Template());
field1.setId(100L);
DatasetField field2 = field1.copy(field1.getTemplate());

assertNull(field2.getId());
// A copy of a field should not be equal
assertNotEquals(field1, field2);

assertEquals(field2.getDatasetFieldType(), field1.getDatasetFieldType());
}
}

0 comments on commit bfbfa4d

Please sign in to comment.