Skip to content

Commit

Permalink
Revert "Fixed issue #4"
Browse files Browse the repository at this point in the history
This reverts commit 15eb0ec.
  • Loading branch information
sabrineChe committed Feb 14, 2024
1 parent 93f826d commit e54c7f3
Show file tree
Hide file tree
Showing 21 changed files with 878 additions and 970 deletions.
657 changes: 325 additions & 332 deletions src/main/java/edu/kit/datamanager/ro_crate/RoCrate.java

Large diffs are not rendered by default.

741 changes: 356 additions & 385 deletions src/main/java/edu/kit/datamanager/ro_crate/entities/AbstractEntity.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,12 @@ public class ImportFromDataCite {
https://api.datacite.org/application/vnd.datacite.datacite+json/10.1594/pangaea.149669
* @param name the name the crate should have.
* @param description the description of the crate.
* @param datePublished the published date of the crate.
* @param licenseId the license identifier of the crate.
* @return the created crate.
*/
public static Crate createCrateFromDataCiteResource(
String url, String name, String description, String datePublished, String licenseId) {
String url, String name, String description) {

Crate crate = new RoCrate.RoCrateBuilder(name, description, datePublished, licenseId)
Crate crate = new RoCrate.RoCrateBuilder(name, description)
.build();
addDataCiteToCrate(url, crate);
return crate;
Expand All @@ -80,13 +78,11 @@ public static void addDataCiteToCrate(String url, Crate crate) {
* @param json the Json object of the DataCite resource.
* @param name the name of the crate that will be created.
* @param description the description of the crate that will be created.
* @param datePublished the published date of the crate.
* @param licenseId the license identifier of the crate.
* @return the created crate.
*/
public static Crate createCrateFromDataCiteJson(
JsonNode json, String name, String description, String datePublished, String licenseId) {
Crate crate = new RoCrate.RoCrateBuilder(name, description, datePublished, licenseId)
JsonNode json, String name, String description) {
Crate crate = new RoCrate.RoCrateBuilder(name, description)
.build();
addDataCiteToCrateFromJson(json, crate);
return crate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,10 @@ public class ImportFromZenodo {
* @param url the url of the zenodo resource.
* @param name the name of the crate created.
* @param description the description of the crate created.
* @param datePublished the published date of the crate.
* @param licenseId the license identifier of the crate.
* @return the created crate.
*/
public static Crate createCrateWithItem(String url, String name, String description, String datePublished, String licenseId) {
RoCrate crate = new RoCrate.RoCrateBuilder("name", "description", "datePublished", "licenseId").build();
public static Crate createCrateWithItem(String url, String name, String description) {
RoCrate crate = new RoCrate.RoCrateBuilder("name", "description").build();
addToCrateFromZotero(url, crate);
return crate;
}
Expand Down
178 changes: 87 additions & 91 deletions src/test/java/edu/kit/datamanager/ro_crate/HelpFunctions.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,102 +19,98 @@
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class HelpFunctions {

public static void compareEntityWithFile(AbstractEntity entity, String string) throws IOException {
InputStream inputStream
= HelpFunctions.class.getResourceAsStream(string);
JsonNode expectedJson = MyObjectMapper.getMapper().readTree(inputStream);
JsonNode node = MyObjectMapper.getMapper().convertValue(entity, JsonNode.class);
//compare the size of the expected and actual node. Both nodes should have the same number of properties.
assertEquals(expectedJson.size(), node.size());
compare(expectedJson, node, true);
public static void compareEntityWithFile(AbstractEntity entity, String string) throws IOException {
InputStream inputStream =
HelpFunctions.class.getResourceAsStream(string);
JsonNode expectedJson = MyObjectMapper.getMapper().readTree(inputStream);
JsonNode node = MyObjectMapper.getMapper().convertValue(entity, JsonNode.class);
compare(expectedJson, node, true);
}

public static void compare(JsonNode node1, JsonNode node2, Boolean equals) {
var comparator = new JsonComparator() {
public boolean compareValues(Object expected, Object actual) {
return expected.equals(actual);
}

public boolean compareFields(String expected, String actual) {
return expected.equals(actual);
}
};
if (equals) {
JSONCompare.assertMatches(node1, node2, comparator);
} else {
JSONCompare.assertNotMatches(node1, node2, comparator);
}

public static void compare(JsonNode node1, JsonNode node2, Boolean equals) {
var comparator = new JsonComparator() {
public boolean compareValues(Object expected, Object actual) {

return expected.equals(actual);
}

public boolean compareFields(String expected, String actual) {
return expected.equals(actual);
}
};
if (equals) {
JSONCompare.assertMatches(node1, node2, comparator);
} else {
JSONCompare.assertNotMatches(node1, node2, comparator);
}
}

public static void compareTwoMetadataJsonNotEqual(Crate crate1, Crate crate2) throws JsonProcessingException {
ObjectMapper objectMapper = MyObjectMapper.getMapper();
JsonNode node1 = objectMapper.readTree(crate1.getJsonMetadata());
JsonNode node2 = objectMapper.readTree(crate2.getJsonMetadata());
compare(node1, node2, false);
}

public static void compareTwoMetadataJsonNotEqual(Crate crate1, String jsonFileString) throws IOException {
InputStream inputStream = HelpFunctions.class.getResourceAsStream(
jsonFileString);
ObjectMapper objectMapper = MyObjectMapper.getMapper();
JsonNode node1 = objectMapper.readTree(crate1.getJsonMetadata());
JsonNode node2 = JsonUtilFunctions.unwrapSingleArray(objectMapper.readTree(inputStream));
compare(node1, node2, false);
}

public static void compareTwoCrateJson(Crate crate1, Crate crate2) throws JsonProcessingException {
ObjectMapper objectMapper = MyObjectMapper.getMapper();
JsonNode node1 = objectMapper.readTree(crate1.getJsonMetadata());
JsonNode node2 = objectMapper.readTree(crate2.getJsonMetadata());
compare(node1, node2, true);
}

public static void compareTwoMetadataJsonNotEqual(Crate crate1, Crate crate2) throws JsonProcessingException {
ObjectMapper objectMapper = MyObjectMapper.getMapper();
JsonNode node1 = objectMapper.readTree(crate1.getJsonMetadata());
JsonNode node2 = objectMapper.readTree(crate2.getJsonMetadata());
compare(node1, node2, false);
}

public static void compareTwoMetadataJsonNotEqual(Crate crate1, String jsonFileString) throws IOException {
InputStream inputStream = HelpFunctions.class.getResourceAsStream(
jsonFileString);
ObjectMapper objectMapper = MyObjectMapper.getMapper();
JsonNode node1 = objectMapper.readTree(crate1.getJsonMetadata());
JsonNode node2 = JsonUtilFunctions.unwrapSingleArray(objectMapper.readTree(inputStream));
compare(node1, node2, false);
}

public static void compareTwoCrateJson(Crate crate1, Crate crate2) throws JsonProcessingException {
ObjectMapper objectMapper = MyObjectMapper.getMapper();
JsonNode node1 = objectMapper.readTree(crate1.getJsonMetadata());
JsonNode node2 = objectMapper.readTree(crate2.getJsonMetadata());
compare(node1, node2, true);
}

public static void compareCrateJsonToFileInResources(File file1, File file2) throws IOException {
ObjectMapper objectMapper = MyObjectMapper.getMapper();
JsonNode node1 = JsonUtilFunctions.unwrapSingleArray(objectMapper.readTree(file1));
JsonNode node2 = JsonUtilFunctions.unwrapSingleArray(objectMapper.readTree(file2));
compare(node1, node2, true);
}

public static void compareCrateJsonToFileInResources(Crate crate1, String jsonFileString) throws IOException {
InputStream inputStream = HelpFunctions.class.getResourceAsStream(
jsonFileString);
ObjectMapper objectMapper = MyObjectMapper.getMapper();
JsonNode node1 = objectMapper.readTree(crate1.getJsonMetadata());
JsonNode node2 = JsonUtilFunctions.unwrapSingleArray(objectMapper.readTree(inputStream));
compare(node1, node2, true);
}

public static boolean compareTwoDir(File dir1, File dir2) throws IOException {
// compare the content of the two directories
List<File> a = (List<java.io.File>) FileUtils.listFiles(dir1, null, true);
Map<String, File> result_map = a.stream()
.collect(Collectors.toMap(java.io.File::getName, Function.identity()));

List<java.io.File> b = (List<java.io.File>) FileUtils.listFiles(dir2, null, true);
Map<String, java.io.File> input_map = b.stream()
.collect(Collectors.toMap(java.io.File::getName, Function.identity()));

if (result_map.size() != input_map.size()) {
return false;
}

public static void compareCrateJsonToFileInResources(File file1, File file2) throws IOException {
ObjectMapper objectMapper = MyObjectMapper.getMapper();
JsonNode node1 = JsonUtilFunctions.unwrapSingleArray(objectMapper.readTree(file1));
JsonNode node2 = JsonUtilFunctions.unwrapSingleArray(objectMapper.readTree(file2));
compare(node1, node2, true);
}

public static void compareCrateJsonToFileInResources(Crate crate1, String jsonFileString) throws IOException {
InputStream inputStream = HelpFunctions.class.getResourceAsStream(
jsonFileString);
ObjectMapper objectMapper = MyObjectMapper.getMapper();
JsonNode node1 = objectMapper.readTree(crate1.getJsonMetadata());
JsonNode node2 = JsonUtilFunctions.unwrapSingleArray(objectMapper.readTree(inputStream));
compare(node1, node2, true);
}

public static boolean compareTwoDir(File dir1, File dir2) throws IOException {
// compare the content of the two directories
List<File> a = (List<java.io.File>) FileUtils.listFiles(dir1, null, true);
Map<String, File> result_map = a.stream()
.collect(Collectors.toMap(java.io.File::getName, Function.identity()));

List<java.io.File> b = (List<java.io.File>) FileUtils.listFiles(dir2, null, true);
Map<String, java.io.File> input_map = b.stream()
.collect(Collectors.toMap(java.io.File::getName, Function.identity()));

if (result_map.size() != input_map.size()) {
return false;
}
for (String s : input_map.keySet()) {
// we do that because the ro-crate-metadata.json can be differently formatted,
// or the order of the entities may be different
// the same holds for the html file
if (s.equals("ro-crate-preview.html") || s.equals("ro-crate-metadata.json")) {
if (!result_map.containsKey(s)) {
return false;
}
} else if (!FileUtils.contentEqualsIgnoreEOL(input_map.get(s), result_map.get(s), null)) {
return false;
}
for (String s : input_map.keySet()) {
// we do that because the ro-crate-metadata.json can be differently formatted,
// or the order of the entities may be different
// the same holds for the html file
if (s.equals("ro-crate-preview.html") || s.equals("ro-crate-metadata.json")) {
if (!result_map.containsKey(s)) {
return false;
}
return true;
} else if (!FileUtils.contentEqualsIgnoreEOL(input_map.get(s), result_map.get(s), null)) {
return false;
}
}
return true;
}
}
79 changes: 31 additions & 48 deletions src/test/java/edu/kit/datamanager/ro_crate/crate/BuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,54 +9,37 @@
import edu.kit.datamanager.ro_crate.HelpFunctions;
import edu.kit.datamanager.ro_crate.RoCrate;
import edu.kit.datamanager.ro_crate.entities.contextual.ContextualEntity;
import java.io.IOException;

public class BuilderTest {
@Test
void testReadBuilder() throws JsonProcessingException {
ContextualEntity license = new ContextualEntity.ContextualEntityBuilder()
.addType("CreativeWork")
.setId("https://creativecommons.org/licenses/by-nc-sa/3.0/au/")
.addProperty("description",
"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.")
.addProperty("identifier", "https://creativecommons.org/licenses/by-nc-sa/3.0/au/")
.addProperty("name", "Attribution-NonCommercial-ShareAlike 3.0 Australia (CC BY-NC-SA 3.0 AU)")
.build();

RoCrate crate = new RoCrate.RoCrateBuilder().addContextualEntity(license).build();
RoCrate crate1 = new RoCrate.RoCrateBuilder(crate).build();
assertEquals(crate.getAllDataEntities(), crate1.getAllDataEntities());
assertEquals(crate.getAllContextualEntities(), crate1.getAllContextualEntities());
HelpFunctions.compareTwoCrateJson(crate1, crate);

}

@Test
void testAddCrateWithOnlyRootDataEntity() throws IOException {
ContextualEntity license = new ContextualEntity.ContextualEntityBuilder()
.addType("CreativeWork")
.setId("https://creativecommons.org/licenses/by-nc-sa/3.0/au/")
.addProperty("description",
"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.")
.addProperty("identifier", "https://creativecommons.org/licenses/by-nc-sa/3.0/au/")
.addProperty("name", "Attribution-NonCommercial-ShareAlike 3.0 Australia (CC BY-NC-SA 3.0 AU)")
.build();
RoCrate crate = new RoCrate.RoCrateBuilder("Data files", "Palliative care planning...", "2024-02-09T08:21:41Z", license.getId()).addContextualEntity(license).build();

HelpFunctions.compareEntityWithFile(crate.getRootDataEntity(), "/json/entities/data/rootMinimalExample.json");
System.out.println("Sab "+crate.getRootDataEntity());
HelpFunctions.compareEntityWithFile(crate.getContextualEntityById("https://creativecommons.org/licenses/by-nc-sa/3.0/au/"), "/json/entities/data/license.json");
}

@Test
void testEmptyCrates() throws JsonProcessingException {
RoCrate built = new RoCrate.RoCrateBuilder().build();
RoCrate constructed = new RoCrate();

assertEquals(built.getAllDataEntities(), constructed.getAllDataEntities());
assertEquals(built.getAllContextualEntities(), constructed.getAllContextualEntities());
assertEquals(built.getJsonDescriptor().getTypes(), constructed.getJsonDescriptor().getTypes());
assertEquals(built.getJsonDescriptor().getProperties(), constructed.getJsonDescriptor().getProperties());
HelpFunctions.compareTwoCrateJson(built, constructed);
}
@Test
void testReadBuilder() throws JsonProcessingException {
ContextualEntity license = new ContextualEntity.ContextualEntityBuilder()
.addType("CreativeWork")
.setId("https://creativecommons.org/licenses/by-nc-sa/3.0/au/")
.addProperty("description",
"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.")
.addProperty("identifier", "https://creativecommons.org/licenses/by-nc-sa/3.0/au/")
.addProperty("name", "Attribution-NonCommercial-ShareAlike 3.0 Australia (CC BY-NC-SA 3.0 AU)")
.build();

RoCrate crate = new RoCrate.RoCrateBuilder().addContextualEntity(license).build();
RoCrate crate1 = new RoCrate.RoCrateBuilder(crate).build();

assertEquals(crate.getAllDataEntities(), crate1.getAllDataEntities());
assertEquals(crate.getAllContextualEntities(), crate1.getAllContextualEntities());
HelpFunctions.compareTwoCrateJson(crate1, crate);

}

@Test
void testEmptyCrates() throws JsonProcessingException {
RoCrate built = new RoCrate.RoCrateBuilder().build();
RoCrate constructed = new RoCrate();

assertEquals(built.getAllDataEntities(), constructed.getAllDataEntities());
assertEquals(built.getAllContextualEntities(), constructed.getAllContextualEntities());
assertEquals(built.getJsonDescriptor().getTypes(), constructed.getJsonDescriptor().getTypes());
assertEquals(built.getJsonDescriptor().getProperties(), constructed.getJsonDescriptor().getProperties());
HelpFunctions.compareTwoCrateJson(built, constructed);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void testOtherFiles(@TempDir Path tempDir) throws IOException, URISyntaxExceptio
FileUtils.touch(file2.toFile());
FileUtils.writeStringToFile(file1.toFile(), "content of file 1", Charset.defaultCharset());
FileUtils.writeStringToFile(file2.toFile(), "content of file 2", Charset.defaultCharset());
RoCrate roCrate = new RoCrate.RoCrateBuilder("minimal", "minimal RO_crate", "2024", "https://creativecommons.org/licenses/by-nc-sa/3.0/au/")
RoCrate roCrate = new RoCrate.RoCrateBuilder("minimal", "minimal RO_crate")
.addUntrackedFile(file1.toFile())
.addUntrackedFile(file2.toFile())
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ void testReadingAndWriting(@TempDir Path path) throws IOException {
Path fileInDir = htmlDir.resolve("file.html");
FileUtils.writeStringToFile(fileInDir.toFile(), "fileN2", Charset.defaultCharset());

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

Expand Down
Loading

0 comments on commit e54c7f3

Please sign in to comment.