Skip to content
This repository has been archived by the owner on Jan 28, 2025. It is now read-only.

Commit

Permalink
Improve Corese-Command unit test
Browse files Browse the repository at this point in the history
- Use canonicalRDF
- Fix Sparql Tests
- Check if result is not null
  • Loading branch information
remiceres committed Jul 10, 2024
1 parent defc82b commit a79ece9
Show file tree
Hide file tree
Showing 13 changed files with 241 additions and 113 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
package fr.inria.corese.command.programs;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import org.junit.Before;
import org.junit.Test;

import fr.inria.corese.core.Graph;
import fr.inria.corese.core.load.Load;
import fr.inria.corese.core.print.CanonicalRdf10Format;
import picocli.CommandLine;

public class ShaclTest {
Expand All @@ -38,7 +41,7 @@ public class ShaclTest {
.getPath();

private static final String UUID_REGEX = "<urn:uuid:[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}>";
private static final String BLANK_NODE_REGEX = "_:(b|bb)\\d+";
private static final String NEUTRAL_UUID = "<urn:uuid:00000000-0000-0000-0000-000000000000>";

@Before
public void setUp() throws Exception {
Expand All @@ -48,30 +51,48 @@ public void setUp() throws Exception {
cmd.setErr(err);
}

public boolean compareFiles(String filePath1, String filePath2) throws IOException {
String content1 = new String(Files.readAllBytes(Paths.get(filePath1)));
String content2 = new String(Files.readAllBytes(Paths.get(filePath2)));
public boolean compareFiles(String filePath1, String filePath2, int format) throws IOException {
// Get content of files
String content1 = getStringContent(filePath1);
String content2 = getStringContent(filePath2);

String normalizedContent1 = sort(trimLines(removeUUIDsAndBlankNodes(content1)));
String normalizedContent2 = sort(trimLines(removeUUIDsAndBlankNodes(content2)));
// Remove UUIDs and Blank Nodes
String clearContent1 = maskUUIDs(content1);
String clearContent2 = maskUUIDs(content2);

return normalizedContent1.equals(normalizedContent2);
}
// Canonicalize RDF content
String canonicallFile1 = canonicalize(clearContent1, format);
String canonicallFile2 = canonicalize(clearContent2, format);

private String sort(String content) {
String[] lines = content.split("\n");
Arrays.sort(lines);
return Arrays.stream(lines).collect(Collectors.joining("\n"));
return canonicallFile1.equals(canonicallFile2);
}

private String removeUUIDsAndBlankNodes(String content) {
content = Pattern.compile(UUID_REGEX).matcher(content).replaceAll("");
content = Pattern.compile(BLANK_NODE_REGEX).matcher(content).replaceAll("");
private String maskUUIDs(String content) {
content = Pattern.compile(UUID_REGEX).matcher(content).replaceAll(NEUTRAL_UUID);
return content;
}

private String trimLines(String content) {
return Arrays.stream(content.split("\n")).map(String::trim).collect(Collectors.joining("\n"));
private String getStringContent(String path) throws IOException {
return new String(java.nio.file.Files.readAllBytes(Paths.get(path)));
}

private String canonicalize(String content, int format) {

// Content String to Input Stream
InputStream is = new ByteArrayInputStream(content.getBytes());

// Load RDF content into a Graph
Graph graph = Graph.create();
Load ld = Load.create(graph);

try {
ld.parse(is, format);
} catch (Exception e) {
e.printStackTrace();
}

// Return Canonical RDF content
return CanonicalRdf10Format.create(graph).toString();
}

@Test
Expand All @@ -91,7 +112,8 @@ public void test1RDF1SHACLBeatlesOk() throws IOException {
assertEquals(0, exitCode);
assertEquals("", this.out.toString());
assertEquals("", this.err.toString());
assertTrue(this.compareFiles(expected, result));
assertTrue(this.compareFiles(expected, result, Load.TURTLE_FORMAT));
assertNotEquals("", result);
}

@Test
Expand All @@ -111,7 +133,8 @@ public void test1RDF1SHACLBeatlesErr() throws IOException {
assertEquals(0, exitCode);
assertEquals("", this.out.toString());
assertEquals("", this.err.toString());
assertTrue(this.compareFiles(expected, result));
assertTrue(this.compareFiles(expected, result, Load.TURTLE_FORMAT));
assertNotEquals("", result);
}

@Test
Expand All @@ -136,7 +159,8 @@ public void test2RDF2SHACLBeatlesOk() throws IOException {
assertEquals(0, exitCode);
assertEquals("", this.out.toString());
assertEquals("", this.err.toString());
assertTrue(this.compareFiles(expected, result));
assertTrue(this.compareFiles(expected, result, Load.TURTLE_FORMAT));
assertNotEquals("", result);
}

@Test
Expand All @@ -161,7 +185,8 @@ public void test2RDF2SHACLBeatlesErr() throws IOException {
assertEquals(0, exitCode);
assertEquals("", this.out.toString());
assertEquals("", this.err.toString());
assertTrue(this.compareFiles(expected, result));
assertTrue(this.compareFiles(expected, result, Load.TURTLE_FORMAT));
assertNotEquals("", result);
}

@Test
Expand All @@ -181,7 +206,8 @@ public void test1RDFUrl1SHACLBeatlesOk() throws IOException {
assertEquals(0, exitCode);
assertEquals("", this.out.toString());
assertEquals("", this.err.toString());
assertTrue(this.compareFiles(expected, result));
assertTrue(this.compareFiles(expected, result, Load.TURTLE_FORMAT));
assertNotEquals("", result);
}

@Test
Expand All @@ -201,7 +227,8 @@ public void test1RDF1SHACLUrlBeatlesOk() throws IOException {
assertEquals(0, exitCode);
assertEquals("", this.out.toString());
assertEquals("", this.err.toString());
assertTrue(this.compareFiles(expected, result));
assertTrue(this.compareFiles(expected, result, Load.TURTLE_FORMAT));
assertNotEquals("", result);
}

@Test
Expand All @@ -221,7 +248,8 @@ public void testRDFSHACLDirectoryBeatlesErr() throws IOException {
assertEquals(0, exitCode);
assertEquals("", this.out.toString());
assertEquals("", this.err.toString());
assertTrue(this.compareFiles(expected, result));
assertTrue(this.compareFiles(expected, result, Load.TURTLE_FORMAT));
assertNotEquals("", result);
}

@Test
Expand All @@ -244,7 +272,8 @@ public void test1RDF1SHACLBeatlesOkrdf() throws IOException {
assertEquals(0, exitCode);
assertEquals("", this.out.toString());
assertEquals("", this.err.toString());
assertTrue(this.compareFiles(expected, result));
assertTrue(this.compareFiles(expected, result, Load.RDFXML_FORMAT));
assertNotEquals("", result);
}

@Test
Expand All @@ -267,7 +296,8 @@ public void test1RDF1SHACLBeatlesOkjsonld() throws IOException {
assertEquals(0, exitCode);
assertEquals("", this.out.toString());
assertEquals("", this.err.toString());
assertTrue(this.compareFiles(expected, result));
assertTrue(this.compareFiles(expected, result, Load.JSONLD_FORMAT));
assertNotEquals("", result);
}

@Test
Expand All @@ -288,7 +318,8 @@ public void testRDFSHACLDirectoryRecursiveBeatlesErr() throws IOException {
assertEquals(0, exitCode);
assertEquals("", this.out.toString());
assertEquals("", this.err.toString());
assertTrue(this.compareFiles(expected, result));
assertTrue(this.compareFiles(expected, result, Load.TURTLE_FORMAT));
assertNotEquals("", result);
}

}
Loading

0 comments on commit a79ece9

Please sign in to comment.