Skip to content

Commit 15eb0ec

Browse files
committed
Fixed issue #4
1 parent da01220 commit 15eb0ec

File tree

21 files changed

+970
-878
lines changed

21 files changed

+970
-878
lines changed

src/main/java/edu/kit/datamanager/ro_crate/RoCrate.java

Lines changed: 332 additions & 325 deletions
Large diffs are not rendered by default.

src/main/java/edu/kit/datamanager/ro_crate/entities/AbstractEntity.java

Lines changed: 385 additions & 356 deletions
Large diffs are not rendered by default.

src/main/java/edu/kit/datamanager/ro_crate/externalproviders/dataentities/ImportFromDataCite.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,14 @@ public class ImportFromDataCite {
4848
https://api.datacite.org/application/vnd.datacite.datacite+json/10.1594/pangaea.149669
4949
* @param name the name the crate should have.
5050
* @param description the description of the crate.
51+
* @param datePublished the published date of the crate.
52+
* @param licenseId the license identifier of the crate.
5153
* @return the created crate.
5254
*/
5355
public static Crate createCrateFromDataCiteResource(
54-
String url, String name, String description) {
56+
String url, String name, String description, String datePublished, String licenseId) {
5557

56-
Crate crate = new RoCrate.RoCrateBuilder(name, description)
58+
Crate crate = new RoCrate.RoCrateBuilder(name, description, datePublished, licenseId)
5759
.build();
5860
addDataCiteToCrate(url, crate);
5961
return crate;
@@ -78,11 +80,13 @@ public static void addDataCiteToCrate(String url, Crate crate) {
7880
* @param json the Json object of the DataCite resource.
7981
* @param name the name of the crate that will be created.
8082
* @param description the description of the crate that will be created.
83+
* @param datePublished the published date of the crate.
84+
* @param licenseId the license identifier of the crate.
8185
* @return the created crate.
8286
*/
8387
public static Crate createCrateFromDataCiteJson(
84-
JsonNode json, String name, String description) {
85-
Crate crate = new RoCrate.RoCrateBuilder(name, description)
88+
JsonNode json, String name, String description, String datePublished, String licenseId) {
89+
Crate crate = new RoCrate.RoCrateBuilder(name, description, datePublished, licenseId)
8690
.build();
8791
addDataCiteToCrateFromJson(json, crate);
8892
return crate;

src/main/java/edu/kit/datamanager/ro_crate/externalproviders/dataentities/ImportFromZenodo.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,12 @@ public class ImportFromZenodo {
3232
* @param url the url of the zenodo resource.
3333
* @param name the name of the crate created.
3434
* @param description the description of the crate created.
35+
* @param datePublished the published date of the crate.
36+
* @param licenseId the license identifier of the crate.
3537
* @return the created crate.
3638
*/
37-
public static Crate createCrateWithItem(String url, String name, String description) {
38-
RoCrate crate = new RoCrate.RoCrateBuilder("name", "description").build();
39+
public static Crate createCrateWithItem(String url, String name, String description, String datePublished, String licenseId) {
40+
RoCrate crate = new RoCrate.RoCrateBuilder("name", "description", "datePublished", "licenseId").build();
3941
addToCrateFromZotero(url, crate);
4042
return crate;
4143
}

src/test/java/edu/kit/datamanager/ro_crate/HelpFunctions.java

Lines changed: 91 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -19,98 +19,102 @@
1919
import java.util.Map;
2020
import java.util.function.Function;
2121
import java.util.stream.Collectors;
22+
import static org.junit.jupiter.api.Assertions.assertEquals;
2223

2324
public class HelpFunctions {
2425

25-
public static void compareEntityWithFile(AbstractEntity entity, String string) throws IOException {
26-
InputStream inputStream =
27-
HelpFunctions.class.getResourceAsStream(string);
28-
JsonNode expectedJson = MyObjectMapper.getMapper().readTree(inputStream);
29-
JsonNode node = MyObjectMapper.getMapper().convertValue(entity, JsonNode.class);
30-
compare(expectedJson, node, true);
31-
}
32-
33-
public static void compare(JsonNode node1, JsonNode node2, Boolean equals) {
34-
var comparator = new JsonComparator() {
35-
public boolean compareValues(Object expected, Object actual) {
36-
return expected.equals(actual);
37-
}
38-
39-
public boolean compareFields(String expected, String actual) {
40-
return expected.equals(actual);
41-
}
42-
};
43-
if (equals) {
44-
JSONCompare.assertMatches(node1, node2, comparator);
45-
} else {
46-
JSONCompare.assertNotMatches(node1, node2, comparator);
26+
public static void compareEntityWithFile(AbstractEntity entity, String string) throws IOException {
27+
InputStream inputStream
28+
= HelpFunctions.class.getResourceAsStream(string);
29+
JsonNode expectedJson = MyObjectMapper.getMapper().readTree(inputStream);
30+
JsonNode node = MyObjectMapper.getMapper().convertValue(entity, JsonNode.class);
31+
//compare the size of the expected and actual node. Both nodes should have the same number of properties.
32+
assertEquals(expectedJson.size(), node.size());
33+
compare(expectedJson, node, true);
4734
}
48-
}
49-
50-
public static void compareTwoMetadataJsonNotEqual(Crate crate1, Crate crate2) throws JsonProcessingException {
51-
ObjectMapper objectMapper = MyObjectMapper.getMapper();
52-
JsonNode node1 = objectMapper.readTree(crate1.getJsonMetadata());
53-
JsonNode node2 = objectMapper.readTree(crate2.getJsonMetadata());
54-
compare(node1, node2, false);
55-
}
56-
57-
public static void compareTwoMetadataJsonNotEqual(Crate crate1, String jsonFileString) throws IOException {
58-
InputStream inputStream = HelpFunctions.class.getResourceAsStream(
59-
jsonFileString);
60-
ObjectMapper objectMapper = MyObjectMapper.getMapper();
61-
JsonNode node1 = objectMapper.readTree(crate1.getJsonMetadata());
62-
JsonNode node2 = JsonUtilFunctions.unwrapSingleArray(objectMapper.readTree(inputStream));
63-
compare(node1, node2, false);
64-
}
65-
66-
public static void compareTwoCrateJson(Crate crate1, Crate crate2) throws JsonProcessingException {
67-
ObjectMapper objectMapper = MyObjectMapper.getMapper();
68-
JsonNode node1 = objectMapper.readTree(crate1.getJsonMetadata());
69-
JsonNode node2 = objectMapper.readTree(crate2.getJsonMetadata());
70-
compare(node1, node2, true);
71-
}
72-
73-
public static void compareCrateJsonToFileInResources(File file1, File file2) throws IOException {
74-
ObjectMapper objectMapper = MyObjectMapper.getMapper();
75-
JsonNode node1 = JsonUtilFunctions.unwrapSingleArray(objectMapper.readTree(file1));
76-
JsonNode node2 = JsonUtilFunctions.unwrapSingleArray(objectMapper.readTree(file2));
77-
compare(node1, node2, true);
78-
}
79-
80-
public static void compareCrateJsonToFileInResources(Crate crate1, String jsonFileString) throws IOException {
81-
InputStream inputStream = HelpFunctions.class.getResourceAsStream(
82-
jsonFileString);
83-
ObjectMapper objectMapper = MyObjectMapper.getMapper();
84-
JsonNode node1 = objectMapper.readTree(crate1.getJsonMetadata());
85-
JsonNode node2 = JsonUtilFunctions.unwrapSingleArray(objectMapper.readTree(inputStream));
86-
compare(node1, node2, true);
87-
}
88-
89-
public static boolean compareTwoDir(File dir1, File dir2) throws IOException {
90-
// compare the content of the two directories
91-
List<File> a = (List<java.io.File>) FileUtils.listFiles(dir1, null, true);
92-
Map<String, File> result_map = a.stream()
93-
.collect(Collectors.toMap(java.io.File::getName, Function.identity()));
94-
95-
List<java.io.File> b = (List<java.io.File>) FileUtils.listFiles(dir2, null, true);
96-
Map<String, java.io.File> input_map = b.stream()
97-
.collect(Collectors.toMap(java.io.File::getName, Function.identity()));
98-
99-
if (result_map.size() != input_map.size()) {
100-
return false;
35+
36+
public static void compare(JsonNode node1, JsonNode node2, Boolean equals) {
37+
var comparator = new JsonComparator() {
38+
public boolean compareValues(Object expected, Object actual) {
39+
40+
return expected.equals(actual);
41+
}
42+
43+
public boolean compareFields(String expected, String actual) {
44+
return expected.equals(actual);
45+
}
46+
};
47+
if (equals) {
48+
JSONCompare.assertMatches(node1, node2, comparator);
49+
} else {
50+
JSONCompare.assertNotMatches(node1, node2, comparator);
51+
}
52+
}
53+
54+
public static void compareTwoMetadataJsonNotEqual(Crate crate1, Crate crate2) throws JsonProcessingException {
55+
ObjectMapper objectMapper = MyObjectMapper.getMapper();
56+
JsonNode node1 = objectMapper.readTree(crate1.getJsonMetadata());
57+
JsonNode node2 = objectMapper.readTree(crate2.getJsonMetadata());
58+
compare(node1, node2, false);
59+
}
60+
61+
public static void compareTwoMetadataJsonNotEqual(Crate crate1, String jsonFileString) throws IOException {
62+
InputStream inputStream = HelpFunctions.class.getResourceAsStream(
63+
jsonFileString);
64+
ObjectMapper objectMapper = MyObjectMapper.getMapper();
65+
JsonNode node1 = objectMapper.readTree(crate1.getJsonMetadata());
66+
JsonNode node2 = JsonUtilFunctions.unwrapSingleArray(objectMapper.readTree(inputStream));
67+
compare(node1, node2, false);
68+
}
69+
70+
public static void compareTwoCrateJson(Crate crate1, Crate crate2) throws JsonProcessingException {
71+
ObjectMapper objectMapper = MyObjectMapper.getMapper();
72+
JsonNode node1 = objectMapper.readTree(crate1.getJsonMetadata());
73+
JsonNode node2 = objectMapper.readTree(crate2.getJsonMetadata());
74+
compare(node1, node2, true);
10175
}
102-
for (String s : input_map.keySet()) {
103-
// we do that because the ro-crate-metadata.json can be differently formatted,
104-
// or the order of the entities may be different
105-
// the same holds for the html file
106-
if (s.equals("ro-crate-preview.html") || s.equals("ro-crate-metadata.json")) {
107-
if (!result_map.containsKey(s)) {
108-
return false;
76+
77+
public static void compareCrateJsonToFileInResources(File file1, File file2) throws IOException {
78+
ObjectMapper objectMapper = MyObjectMapper.getMapper();
79+
JsonNode node1 = JsonUtilFunctions.unwrapSingleArray(objectMapper.readTree(file1));
80+
JsonNode node2 = JsonUtilFunctions.unwrapSingleArray(objectMapper.readTree(file2));
81+
compare(node1, node2, true);
82+
}
83+
84+
public static void compareCrateJsonToFileInResources(Crate crate1, String jsonFileString) throws IOException {
85+
InputStream inputStream = HelpFunctions.class.getResourceAsStream(
86+
jsonFileString);
87+
ObjectMapper objectMapper = MyObjectMapper.getMapper();
88+
JsonNode node1 = objectMapper.readTree(crate1.getJsonMetadata());
89+
JsonNode node2 = JsonUtilFunctions.unwrapSingleArray(objectMapper.readTree(inputStream));
90+
compare(node1, node2, true);
91+
}
92+
93+
public static boolean compareTwoDir(File dir1, File dir2) throws IOException {
94+
// compare the content of the two directories
95+
List<File> a = (List<java.io.File>) FileUtils.listFiles(dir1, null, true);
96+
Map<String, File> result_map = a.stream()
97+
.collect(Collectors.toMap(java.io.File::getName, Function.identity()));
98+
99+
List<java.io.File> b = (List<java.io.File>) FileUtils.listFiles(dir2, null, true);
100+
Map<String, java.io.File> input_map = b.stream()
101+
.collect(Collectors.toMap(java.io.File::getName, Function.identity()));
102+
103+
if (result_map.size() != input_map.size()) {
104+
return false;
105+
}
106+
for (String s : input_map.keySet()) {
107+
// we do that because the ro-crate-metadata.json can be differently formatted,
108+
// or the order of the entities may be different
109+
// the same holds for the html file
110+
if (s.equals("ro-crate-preview.html") || s.equals("ro-crate-metadata.json")) {
111+
if (!result_map.containsKey(s)) {
112+
return false;
113+
}
114+
} else if (!FileUtils.contentEqualsIgnoreEOL(input_map.get(s), result_map.get(s), null)) {
115+
return false;
116+
}
109117
}
110-
} else if (!FileUtils.contentEqualsIgnoreEOL(input_map.get(s), result_map.get(s), null)) {
111-
return false;
112-
}
118+
return true;
113119
}
114-
return true;
115-
}
116120
}

src/test/java/edu/kit/datamanager/ro_crate/crate/BuilderTest.java

Lines changed: 48 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,37 +9,54 @@
99
import edu.kit.datamanager.ro_crate.HelpFunctions;
1010
import edu.kit.datamanager.ro_crate.RoCrate;
1111
import edu.kit.datamanager.ro_crate.entities.contextual.ContextualEntity;
12+
import java.io.IOException;
1213

1314
public class BuilderTest {
14-
@Test
15-
void testReadBuilder() throws JsonProcessingException {
16-
ContextualEntity license = new ContextualEntity.ContextualEntityBuilder()
17-
.addType("CreativeWork")
18-
.setId("https://creativecommons.org/licenses/by-nc-sa/3.0/au/")
19-
.addProperty("description",
20-
"This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Australia License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/au/ or send a letter to Creative Commons, PO Box 1866, Mountain View, CA 94042, USA.")
21-
.addProperty("identifier", "https://creativecommons.org/licenses/by-nc-sa/3.0/au/")
22-
.addProperty("name", "Attribution-NonCommercial-ShareAlike 3.0 Australia (CC BY-NC-SA 3.0 AU)")
23-
.build();
24-
25-
RoCrate crate = new RoCrate.RoCrateBuilder().addContextualEntity(license).build();
26-
RoCrate crate1 = new RoCrate.RoCrateBuilder(crate).build();
27-
28-
assertEquals(crate.getAllDataEntities(), crate1.getAllDataEntities());
29-
assertEquals(crate.getAllContextualEntities(), crate1.getAllContextualEntities());
30-
HelpFunctions.compareTwoCrateJson(crate1, crate);
31-
32-
}
33-
34-
@Test
35-
void testEmptyCrates() throws JsonProcessingException {
36-
RoCrate built = new RoCrate.RoCrateBuilder().build();
37-
RoCrate constructed = new RoCrate();
38-
39-
assertEquals(built.getAllDataEntities(), constructed.getAllDataEntities());
40-
assertEquals(built.getAllContextualEntities(), constructed.getAllContextualEntities());
41-
assertEquals(built.getJsonDescriptor().getTypes(), constructed.getJsonDescriptor().getTypes());
42-
assertEquals(built.getJsonDescriptor().getProperties(), constructed.getJsonDescriptor().getProperties());
43-
HelpFunctions.compareTwoCrateJson(built, constructed);
44-
}
15+
@Test
16+
void testReadBuilder() throws JsonProcessingException {
17+
ContextualEntity license = new ContextualEntity.ContextualEntityBuilder()
18+
.addType("CreativeWork")
19+
.setId("https://creativecommons.org/licenses/by-nc-sa/3.0/au/")
20+
.addProperty("description",
21+
"This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Australia License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/au/ or send a letter to Creative Commons, PO Box 1866, Mountain View, CA 94042, USA.")
22+
.addProperty("identifier", "https://creativecommons.org/licenses/by-nc-sa/3.0/au/")
23+
.addProperty("name", "Attribution-NonCommercial-ShareAlike 3.0 Australia (CC BY-NC-SA 3.0 AU)")
24+
.build();
25+
26+
RoCrate crate = new RoCrate.RoCrateBuilder().addContextualEntity(license).build();
27+
RoCrate crate1 = new RoCrate.RoCrateBuilder(crate).build();
28+
assertEquals(crate.getAllDataEntities(), crate1.getAllDataEntities());
29+
assertEquals(crate.getAllContextualEntities(), crate1.getAllContextualEntities());
30+
HelpFunctions.compareTwoCrateJson(crate1, crate);
31+
32+
}
33+
34+
@Test
35+
void testAddCrateWithOnlyRootDataEntity() throws IOException {
36+
ContextualEntity license = new ContextualEntity.ContextualEntityBuilder()
37+
.addType("CreativeWork")
38+
.setId("https://creativecommons.org/licenses/by-nc-sa/3.0/au/")
39+
.addProperty("description",
40+
"This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Australia License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/au/ or send a letter to Creative Commons, PO Box 1866, Mountain View, CA 94042, USA.")
41+
.addProperty("identifier", "https://creativecommons.org/licenses/by-nc-sa/3.0/au/")
42+
.addProperty("name", "Attribution-NonCommercial-ShareAlike 3.0 Australia (CC BY-NC-SA 3.0 AU)")
43+
.build();
44+
RoCrate crate = new RoCrate.RoCrateBuilder("Data files", "Palliative care planning...", "2024-02-09T08:21:41Z", license.getId()).addContextualEntity(license).build();
45+
46+
HelpFunctions.compareEntityWithFile(crate.getRootDataEntity(), "/json/entities/data/rootMinimalExample.json");
47+
System.out.println("Sab "+crate.getRootDataEntity());
48+
HelpFunctions.compareEntityWithFile(crate.getContextualEntityById("https://creativecommons.org/licenses/by-nc-sa/3.0/au/"), "/json/entities/data/license.json");
49+
}
50+
51+
@Test
52+
void testEmptyCrates() throws JsonProcessingException {
53+
RoCrate built = new RoCrate.RoCrateBuilder().build();
54+
RoCrate constructed = new RoCrate();
55+
56+
assertEquals(built.getAllDataEntities(), constructed.getAllDataEntities());
57+
assertEquals(built.getAllContextualEntities(), constructed.getAllContextualEntities());
58+
assertEquals(built.getJsonDescriptor().getTypes(), constructed.getJsonDescriptor().getTypes());
59+
assertEquals(built.getJsonDescriptor().getProperties(), constructed.getJsonDescriptor().getProperties());
60+
HelpFunctions.compareTwoCrateJson(built, constructed);
61+
}
4562
}

src/test/java/edu/kit/datamanager/ro_crate/crate/OtherFilesTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ void testOtherFiles(@TempDir Path tempDir) throws IOException, URISyntaxExceptio
3333
FileUtils.touch(file2.toFile());
3434
FileUtils.writeStringToFile(file1.toFile(), "content of file 1", Charset.defaultCharset());
3535
FileUtils.writeStringToFile(file2.toFile(), "content of file 2", Charset.defaultCharset());
36-
RoCrate roCrate = new RoCrate.RoCrateBuilder("minimal", "minimal RO_crate")
36+
RoCrate roCrate = new RoCrate.RoCrateBuilder("minimal", "minimal RO_crate", "2024", "https://creativecommons.org/licenses/by-nc-sa/3.0/au/")
3737
.addUntrackedFile(file1.toFile())
3838
.addUntrackedFile(file2.toFile())
3939
.build();

src/test/java/edu/kit/datamanager/ro_crate/crate/ReadAndWriteTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ void testReadingAndWriting(@TempDir Path path) throws IOException {
3030
Path fileInDir = htmlDir.resolve("file.html");
3131
FileUtils.writeStringToFile(fileInDir.toFile(), "fileN2", Charset.defaultCharset());
3232

33-
RoCrate crate = new RoCrate.RoCrateBuilder("name", "description")
33+
RoCrate crate = new RoCrate.RoCrateBuilder("name", "description", "2024", "https://creativecommons.org/licenses/by-nc-sa/3.0/au/")
3434
.setPreview(new CustomPreview(htmlFile.toFile(), htmlDir.toFile()))
3535
.build();
3636

0 commit comments

Comments
 (0)