implemented unitTest to verify if compare works

This commit is contained in:
Sandro La Bruzzo 2022-02-23 10:42:23 +01:00
parent dc514a7281
commit 88a347d978
1 changed files with 123 additions and 33 deletions

View File

@ -1,39 +1,34 @@
package eu.dnetlib.dhp.schema.sx.scholix; package eu.dnetlib.dhp.schema.sx.scholix;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals;
public class ScholixCompareTest { public class ScholixCompareTest {
private ScholixIdentifier generateMockScholixId(final String baseId ,boolean toUpper, int idCount) {
final String id = String.format("%s.%d",baseId, idCount);
private ScholixIdentifier generateMockScholixId(boolean toUpper, int idCount) {
final String id = String.format("10.11646/zootaxa.5099.1.%d", idCount);
final String schema = "DOI"; final String schema = "DOI";
final String url =String.format("http://dx.dOI.org/10.11646/Zootaxa.5099.1.%d", idCount); final String url =String.format("http://dx.dOI.org/%s.%d",baseId, idCount);
final ScholixIdentifier result = new ScholixIdentifier(); final ScholixIdentifier result = new ScholixIdentifier();
result.setIdentifier(toUpper ? id.toUpperCase(): id.toLowerCase()); result.setIdentifier(toUpper ? id.toUpperCase(): id.toLowerCase());
result.setSchema(toUpper ? schema.toUpperCase():schema.toLowerCase()); result.setSchema(toUpper ? schema.toUpperCase():schema.toLowerCase());
result.setUrl(toUpper ? url.toUpperCase():url.toLowerCase()); result.setUrl(toUpper ? url.toUpperCase():url.toLowerCase());
return result; return result;
} }
private ScholixIdentifier generateMockScholixId(boolean toUpper, int idCount) {
return generateMockScholixId("10.11646/zootaxa.5099.1", toUpper, idCount);
}
private ScholixEntityId generateMockScholixEntityId(boolean toUpper, boolean invertOrder, int numberOfIds) { private ScholixEntityId generateMockScholixEntityId(String name, boolean toUpper, boolean invertOrder, int numberOfIds) {
final String datasourceName = "Datacite";
final List<ScholixIdentifier> ids = new ArrayList<>(); final List<ScholixIdentifier> ids = new ArrayList<>();
@ -48,20 +43,30 @@ public class ScholixCompareTest {
} }
} }
return new ScholixEntityId(toUpper? datasourceName.toUpperCase(): datasourceName.toLowerCase(), ids); return new ScholixEntityId(toUpper? name.toUpperCase(): name.toLowerCase(), ids);
} }
private ScholixCollectedFrom generateMockScholixCollectedFrom(boolean toUpper, boolean invertOrder, int numberOfIds) { private ScholixEntityId generateMockScholixEntityId(boolean toUpper, boolean invertOrder, int numberOfIds) {
return generateMockScholixEntityId("datacite", toUpper, invertOrder,numberOfIds);
}
private ScholixCollectedFrom generateMockScholixCollectedFrom(final String dsName , final boolean toUpper, final boolean invertOrder, final int numberOfIds) {
final ScholixCollectedFrom result = new ScholixCollectedFrom(); final ScholixCollectedFrom result = new ScholixCollectedFrom();
final String completionStatus = "complete"; final String completionStatus = "complete";
final String provisionMode = "collected"; final String provisionMode = "collected";
result.setProvider(generateMockScholixEntityId(toUpper, invertOrder, numberOfIds)); result.setProvider(generateMockScholixEntityId(dsName,toUpper, invertOrder, numberOfIds));
result.setCompletionStatus(toUpper ? completionStatus.toUpperCase(): completionStatus.toLowerCase()); result.setCompletionStatus(toUpper ? completionStatus.toUpperCase(): completionStatus.toLowerCase());
result.setProvisionMode(toUpper ? provisionMode.toUpperCase(): provisionMode.toLowerCase()); result.setProvisionMode(toUpper ? provisionMode.toUpperCase(): provisionMode.toLowerCase());
return result; return result;
} }
private ScholixCollectedFrom generateMockScholixCollectedFrom(boolean toUpper, boolean invertOrder, int numberOfIds) {
return generateMockScholixCollectedFrom("datasource", toUpper, invertOrder,numberOfIds);
}
private ScholixRelationship generateMockScholixRelationships(boolean toUpper) { private ScholixRelationship generateMockScholixRelationships(boolean toUpper) {
final String name = "IsRelatedTo"; final String name = "IsRelatedTo";
@ -78,26 +83,86 @@ public class ScholixCompareTest {
} }
private ScholixResource generateMockScholixResource(final String sourceBase, boolean toUpper, int idCount, boolean invertOrder, int numEntityIds) {
final ScholixResource resource = new ScholixResource();
final String baseSourceId = "%s_10.1000/ID_%d/";
final String creatorBase ="Creator %d";
final String publisherBase ="Publisher %d";
final String collectedFromBase ="Datasource %d";
final String title = String.format("Resource %s Title 1", sourceBase);
final List<ScholixIdentifier> ids = IntStream.range(0,numEntityIds)
.mapToObj(i -> String.format(baseSourceId,sourceBase, i))
.map(s ->generateMockScholixId(s, toUpper, idCount))
.collect(Collectors.toList());
resource.setIdentifier(ids);
resource.setDnetIdentifier("dnetId");
resource.setObjectType("dataset");
resource.setObjectSubType("dataset");
resource.setTitle(title);
final List<ScholixEntityId> creators = IntStream.range(0,numEntityIds)
.mapToObj(i -> String.format(creatorBase, i))
.map(s ->generateMockScholixEntityId(s, toUpper,invertOrder,idCount))
.collect(Collectors.toList());
resource.setCreator(creators);
resource.setPublicationDate("22-02-2022");
final List<ScholixEntityId> publishers = IntStream.range(0,numEntityIds)
.mapToObj(i -> String.format(publisherBase, i))
.map(s ->generateMockScholixEntityId(s, toUpper,invertOrder,idCount))
.collect(Collectors.toList());
resource.setPublisher(publishers);
final List<ScholixCollectedFrom> collectedFroms = IntStream.range(0,numEntityIds)
.mapToObj(i -> String.format(collectedFromBase, i))
.map(s -> generateMockScholixCollectedFrom(s,toUpper, invertOrder, idCount))
.collect(Collectors.toList());
resource.setCollectedFrom(collectedFroms);
return resource;
}
private Scholix generateMockScholix( boolean toUpper, int idCount, boolean invertOrder, int numEntityIds) {
final Scholix result = new Scholix();
result.setPublicationDate("2022-02-22");
final String id = "dnetID";
final String publisherBase ="Publisher %d";
result.setPublisher(IntStream.range(0,numEntityIds)
.mapToObj(i -> String.format(publisherBase, i))
.map(s ->generateMockScholixEntityId(s, toUpper,invertOrder,idCount))
.collect(Collectors.toList()));
result.setLinkprovider(IntStream.range(0,numEntityIds)
.mapToObj(i -> String.format(publisherBase, i))
.map(s ->generateMockScholixEntityId(s, toUpper,invertOrder,idCount))
.collect(Collectors.toList()));
result.setRelationship(generateMockScholixRelationships(toUpper));
result.setSource(generateMockScholixResource("source", toUpper, idCount,invertOrder, numEntityIds));
result.setTarget(generateMockScholixResource("target", toUpper, idCount,invertOrder, numEntityIds));
result.setIdentifier(toUpper?id.toUpperCase():id.toLowerCase());
return result;
}
@Test @Test
public void testScholixIdentifierComparison() { public void testScholixIdentifierComparison() {
final ScholixIdentifier left = generateMockScholixId(true, 1); final ScholixIdentifier left = generateMockScholixId(true, 1);
final ScholixIdentifier right = generateMockScholixId(false,1); final ScholixIdentifier right = generateMockScholixId(false,1);
assertEquals(0,left.compareTo(right)); assertEquals(0,left.compareTo(right));
assertEquals(left, right); assertEquals(left, right);
assertEquals(left.hashCode(), right.hashCode()); assertEquals(left.hashCode(), right.hashCode());
left.setIdentifier(null); left.setIdentifier(null);
assertEquals(-1, left.compareTo(right)); assertEquals(-1, left.compareTo(right));
} }
@ -116,8 +181,6 @@ public class ScholixCompareTest {
public void testScholixCollectedFromComparison() { public void testScholixCollectedFromComparison() {
final ScholixCollectedFrom cfLeft = generateMockScholixCollectedFrom(true, true, 20); final ScholixCollectedFrom cfLeft = generateMockScholixCollectedFrom(true, true, 20);
final ScholixCollectedFrom cfRight = generateMockScholixCollectedFrom(false, false, 20); final ScholixCollectedFrom cfRight = generateMockScholixCollectedFrom(false, false, 20);
assertEquals(cfLeft, cfRight); assertEquals(cfLeft, cfRight);
@ -127,6 +190,35 @@ public class ScholixCompareTest {
cfRight.setCompletionStatus(null); cfRight.setCompletionStatus(null);
assertNotEquals(cfLeft, cfRight); assertNotEquals(cfLeft, cfRight);
}
@Test
public void testCompareScholixResource() {
final ScholixResource left = generateMockScholixResource("source",true,5, true,5);
final ScholixResource right = generateMockScholixResource("source",false,5, false,5);
assertEquals(left,right);
assertEquals(left.hashCode(), right.hashCode());
final ScholixResource different = generateMockScholixResource("source",false,4, false,5);
assertNotEquals(different,right);
assertNotEquals(different,left);
assertNotEquals(left.hashCode(), different.hashCode());
assertNotEquals(right.hashCode(), different.hashCode());
}
@Test
public void testCompareScholix() {
final Scholix left = generateMockScholix(true,5, true,5);
final Scholix right = generateMockScholix(false,5, false,5);
assertEquals(left,right);
assertEquals(left.hashCode(), right.hashCode());
final Scholix different = generateMockScholix(true,4, false,5);
assertNotEquals(different,right);
assertNotEquals(different,left);
assertNotEquals(left.hashCode(), different.hashCode());
assertNotEquals(right.hashCode(), different.hashCode());
} }
@ -134,16 +226,14 @@ public class ScholixCompareTest {
@Test @Test
public void testCompareScholixRelation() { public void testCompareScholixRelation() {
final ScholixRelationship left = generateMockScholixRelationships(true); final ScholixRelationship left = generateMockScholixRelationships(true);
final ScholixRelationship right = generateMockScholixRelationships(false); final ScholixRelationship right = generateMockScholixRelationships(false);
assertEquals(left, right); assertEquals(left, right);
assertEquals(left.hashCode(), right.hashCode()); assertEquals(left.hashCode(), right.hashCode());
} }
} }