Skip to content

Commit 86b2260

Browse files
committed
tests for selecting persistent ids in the GenericImportService (from PR #11010 by @stevenferey)
1 parent d6fc240 commit 86b2260

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

src/main/java/edu/harvard/iq/dataverse/api/imports/ImportGenericServiceBean.java

+4
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,10 @@ private FieldDTO makeDTO(DatasetFieldType dataverseFieldType, FieldDTO value, St
367367
return value;
368368
}
369369

370+
public String selectIdentifier(DatasetVersionDTO datasetVersionDTO, String suppliedIdentifier) {
371+
return selectIdentifier(datasetVersionDTO, suppliedIdentifier, false);
372+
}
373+
370374
private String selectIdentifier(DatasetVersionDTO datasetVersionDTO, String suppliedIdentifier, boolean preferSuppliedIdentifier) {
371375
List<String> otherIds = new ArrayList<>();
372376

src/test/java/edu/harvard/iq/dataverse/api/imports/ImportGenericServiceBeanTest.java

+52-1
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,70 @@
11
package edu.harvard.iq.dataverse.api.imports;
22

33
import edu.harvard.iq.dataverse.api.dto.DatasetDTO;
4+
import edu.harvard.iq.dataverse.api.dto.DatasetVersionDTO;
5+
6+
import org.apache.commons.io.FileUtils;
7+
import com.google.gson.Gson;
8+
import java.io.File;
9+
import java.io.IOException;
10+
411
import org.junit.jupiter.api.Test;
512
import org.junit.jupiter.api.extension.ExtendWith;
613
import org.mockito.InjectMocks;
714
import org.mockito.junit.jupiter.MockitoExtension;
815
import static org.junit.jupiter.api.Assertions.assertEquals;
916
import static org.junit.jupiter.api.Assertions.assertNull;
1017

18+
import java.nio.charset.StandardCharsets;
19+
1120
@ExtendWith(MockitoExtension.class)
1221
public class ImportGenericServiceBeanTest {
1322

1423
@InjectMocks
1524
private ImportGenericServiceBean importGenericService;
1625

1726
@Test
18-
public void testReassignIdentifierAsGlobalId() {
27+
void testIdentifierHarvestableWithOtherID() throws IOException {
28+
// "otherIdValue" containing the value : doi:10.7910/DVN/TJCLKP
29+
File file = new File("src/test/resources/json/importGenericWithOtherId.json");
30+
String text = FileUtils.readFileToString(file, StandardCharsets.UTF_8);
31+
DatasetVersionDTO dto = new Gson().fromJson(text, DatasetVersionDTO.class);
32+
33+
assertEquals("doi:10.7910/DVN/TJCLKP", importGenericService.selectIdentifier(dto, "https://doi.org/10.7910/DVN/TJCLKP"));
34+
// junk or null
35+
assertEquals("doi:10.7910/DVN/TJCLKP", importGenericService.selectIdentifier(dto, "junk"));
36+
assertEquals("doi:10.7910/DVN/TJCLKP", importGenericService.selectIdentifier(dto, null));
37+
assertEquals("doi:10.7910/DVN/TJCLKP", importGenericService.selectIdentifier(dto, "http://www.example.com"));
38+
assertEquals("doi:10.7910/DVN/TJCLKP", importGenericService.selectIdentifier(dto, "https://dataverse.org"));
39+
}
40+
41+
@Test
42+
void testIdentifierHarvestableWithoutOtherID() throws IOException {
43+
// Does not contain data of type "otherIdValue"
44+
File file = new File("src/test/resources/json/importGenericWithoutOtherId.json");
45+
String text = FileUtils.readFileToString(file, StandardCharsets.UTF_8);
46+
DatasetVersionDTO dto = new Gson().fromJson(text, DatasetVersionDTO.class);
47+
48+
// non-URL
49+
assertEquals("doi:10.7910/DVN/TJCLKP", importGenericService.selectIdentifier(dto, "doi:10.7910/DVN/TJCLKP"));
50+
assertEquals("hdl:10.7910/DVN/TJCLKP", importGenericService.selectIdentifier(dto, "hdl:10.7910/DVN/TJCLKP"));
51+
// HTTPS
52+
assertEquals("https://doi.org/10.7910/DVN/TJCLKP", importGenericService.selectIdentifier(dto, "https://doi.org/10.7910/DVN/TJCLKP"));
53+
assertEquals("https://dx.doi.org/10.7910/DVN/TJCLKP", importGenericService.selectIdentifier(dto, "https://dx.doi.org/10.7910/DVN/TJCLKP"));
54+
assertEquals("https://hdl.handle.net/10.7910/DVN/TJCLKP", importGenericService.selectIdentifier(dto, "https://hdl.handle.net/10.7910/DVN/TJCLKP"));
55+
// HTTP (no S)
56+
assertEquals("http://doi.org/10.7910/DVN/TJCLKP", importGenericService.selectIdentifier(dto, "http://doi.org/10.7910/DVN/TJCLKP"));
57+
assertEquals("http://dx.doi.org/10.7910/DVN/TJCLKP", importGenericService.selectIdentifier(dto, "http://dx.doi.org/10.7910/DVN/TJCLKP"));
58+
assertEquals("http://hdl.handle.net/10.7910/DVN/TJCLKP", importGenericService.selectIdentifier(dto, "http://hdl.handle.net/10.7910/DVN/TJCLKP"));
59+
// junk or null
60+
assertNull(importGenericService.selectIdentifier(dto, "junk"));
61+
assertNull(importGenericService.selectIdentifier(dto, null));
62+
assertNull(importGenericService.selectIdentifier(dto, "http://www.example.com"));
63+
assertNull(importGenericService.selectIdentifier(dto, "https://dataverse.org"));
64+
}
65+
66+
@Test
67+
void testReassignIdentifierAsGlobalId() {
1968
// non-URL
2069
assertEquals("doi:10.7910/DVN/TJCLKP", importGenericService.reassignIdentifierAsGlobalId("doi:10.7910/DVN/TJCLKP", new DatasetDTO()));
2170
assertEquals("hdl:10.7910/DVN/TJCLKP", importGenericService.reassignIdentifierAsGlobalId("hdl:10.7910/DVN/TJCLKP", new DatasetDTO()));
@@ -29,6 +78,8 @@ public void testReassignIdentifierAsGlobalId() {
2978
assertEquals("hdl:10.7910/DVN/TJCLKP", importGenericService.reassignIdentifierAsGlobalId("http://hdl.handle.net/10.7910/DVN/TJCLKP", new DatasetDTO()));
3079
// junk
3180
assertNull(importGenericService.reassignIdentifierAsGlobalId("junk", new DatasetDTO()));
81+
assertNull(importGenericService.reassignIdentifierAsGlobalId("http://www.example.com", new DatasetDTO()));
82+
assertNull(importGenericService.reassignIdentifierAsGlobalId("https://dataverse.org", new DatasetDTO()));
3283
}
3384

3485
}

0 commit comments

Comments
 (0)