Skip to content

Commit

Permalink
Add test for Canonicalize command
Browse files Browse the repository at this point in the history
  • Loading branch information
remiceres committed Jul 23, 2024
1 parent 4726d8a commit 45e0fd7
Show file tree
Hide file tree
Showing 10 changed files with 395 additions and 5 deletions.
4 changes: 2 additions & 2 deletions corese-command/src/main/java/fr/inria/corese/command/App.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package fr.inria.corese.command;

import fr.inria.corese.command.programs.canonicalize;
import fr.inria.corese.command.programs.Canonicalize;
import fr.inria.corese.command.programs.Convert;
import fr.inria.corese.command.programs.RemoteSparql;
import fr.inria.corese.command.programs.Shacl;
Expand All @@ -9,7 +9,7 @@
import picocli.CommandLine.Command;

@Command(name = "Corese-command", version = App.version, mixinStandardHelpOptions = true, subcommands = {
Convert.class, Sparql.class, Shacl.class, RemoteSparql.class, canonicalize.class
Convert.class, Sparql.class, Shacl.class, RemoteSparql.class, Canonicalize.class
})

public final class App implements Runnable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import picocli.CommandLine.Spec;

@Command(name = "canonicalize", version = App.version, description = "Canonicalize an RDF file to a specific format.", mixinStandardHelpOptions = true)
public class canonicalize implements Callable<Integer> {
public class Canonicalize implements Callable<Integer> {

private final String DEFAULT_OUTPUT_FILE_NAME = "output";
private final int ERROR_EXIT_CODE_SUCCESS = 0;
Expand All @@ -43,7 +43,7 @@ public class canonicalize implements Callable<Integer> {
"--output-data" }, description = "Output file path. If not provided, the result will be written to standard output.", arity = "0..1", fallbackValue = DEFAULT_OUTPUT_FILE_NAME)
private Path output;

@Option(names = { "-r", "-a", "-ca", "-of",
@Option(names = { "-a", "-ca", "-r", "-of",
"--canonical-algo" }, required = true, description = "Canonicalization algorithm to which the input file should be converted. Possible values:\u001b[34m ${COMPLETION-CANDIDATES}\u001b[0m.", defaultValue = "RDFC10SHA256")
private EnumCanonicAlgo canonicalAlgo;

Expand All @@ -67,7 +67,7 @@ public class canonicalize implements Callable<Integer> {

private boolean isDefaultOutputName = false;

public canonicalize() {
public Canonicalize() {
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
package fr.inria.corese.command.programs;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.nio.file.Paths;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import picocli.CommandLine;

public class CanonicalizeTest {

private Canonicalize canonicalize = new Canonicalize();
private CommandLine cmd = new CommandLine(canonicalize);

private StringWriter out = new StringWriter();
private StringWriter err = new StringWriter();

private String inputPath = ConvertTest.class
.getResource("/fr/inria/corese/command/programs/canonicalize/input/")
.getPath();
private String referencesPath = CanonicalizeTest.class
.getResource("/fr/inria/corese/command/programs/canonicalize/references/")
.getPath();
private String resultPath = CanonicalizeTest.class
.getResource("/fr/inria/corese/command/programs/canonicalize/results/")
.getPath();

@BeforeEach
public void setUp() {
PrintWriter out = new PrintWriter(this.out);
PrintWriter err = new PrintWriter(this.err);
cmd.setOut(out);
cmd.setErr(err);
}

private String getStringContent(String path) {
try {
return new String(java.nio.file.Files.readAllBytes(Paths.get(path)));
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}

@Test
public void test1InputFile() {
String input = inputPath + "beatles.ttl";
String expected = referencesPath + "beatles.nq";
String output = resultPath + "beatles.nq";

String[] args = { "-i", input, "-a", "rdfc-1.0-sha256", "-o", output };
int exitCode = cmd.execute(args);

assertEquals(0, exitCode);
assertEquals("", err.toString());
assertEquals("", out.toString());
assertEquals(getStringContent(expected), getStringContent(output));
}

@Test
public void test1Url() {
String input = "https://files.inria.fr/corese/data/unit-test/beatles.ttl";
String expected = referencesPath + "beatles.nq";
String output = resultPath + "beatles.nq";

String[] args = { "-i", input, "-a", "rdfc-1.0-sha256", "-o", output };
int exitCode = cmd.execute(args);

assertEquals(0, exitCode);
assertEquals("", err.toString());
assertEquals("", out.toString());
assertEquals(getStringContent(expected), getStringContent(output));
}

@Test
public void test1Directory() {
String input = inputPath;
String expected = referencesPath + "beatles.nq";
String output = resultPath + "beatles.nq";

String[] args = { "-i", input, "-a", "rdfc-1.0-sha256", "-o", output };
int exitCode = cmd.execute(args);

assertEquals(0, exitCode);
assertEquals("", err.toString());
assertEquals("", out.toString());
assertEquals(getStringContent(expected), getStringContent(output));
}

@Test
public void test1DirectoryRecursive() {
String input = inputPath;
String expected = referencesPath + "recursive.nq";
String output = resultPath + "recursive.nq";

String[] args = { "-i", input, "-a", "rdfc-1.0-sha256", "-o", output, "-R" };
int exitCode = cmd.execute(args);

assertEquals(0, exitCode);
assertEquals("", err.toString());
assertEquals("", out.toString());
assertEquals(getStringContent(expected), getStringContent(output));
}

@Test
public void testMultipleSources() {
String input1 = inputPath + "beatles.ttl";
String input2 = Paths.get(inputPath, "recursive-level1", "person.ttl").toString();
String expected = referencesPath + "multiple.nq";
String output = resultPath + "multiple.nq";

String[] args = { "-i", input1, input2, "-a", "rdfc-1.0-sha256", "-o", output };
int exitCode = cmd.execute(args);

assertEquals(0, exitCode);
assertEquals("", err.toString());
assertEquals("", out.toString());
assertEquals(getStringContent(expected), getStringContent(output));
}

@Test
public void testInputFormat() {
String input = inputPath + "beatles.ttl";
String expected = referencesPath + "beatles.nq";
String output = resultPath + "beatles.nq";

String[] args = { "-i", input, "-f", "text/turtle", "-a", "rdfc-1.0-sha256", "-o", output };
int exitCode = cmd.execute(args);

assertEquals(0, exitCode);
assertEquals("", err.toString());
assertEquals("", out.toString());
assertEquals(getStringContent(expected), getStringContent(output));
}

@Test
public void testInputBadFormat() {
String input = inputPath + "beatles.ttl";
String output = resultPath + "beatles.nq";

String[] args = { "-i", input, "-f", "rdfxml", "-a", "rdfc-1.0-sha256", "-o", output };
int exitCode = cmd.execute(args);

assertEquals(1, exitCode);
assertEquals("", out.toString());
assertTrue(err.toString().contains("Failed to parse RDF file."));
}

@Test
public void testSha384() {
String input = inputPath + "beatles.ttl";
String expected = referencesPath + "beatles-sha384.nq";
String output = resultPath + "beatles-sha384.nq";

String[] args = { "-i", input, "-a", "rdfc-1.0-sha384", "-o", output };
int exitCode = cmd.execute(args);

assertEquals(0, exitCode);
assertEquals("", err.toString());
assertEquals("", out.toString());
assertEquals(getStringContent(expected), getStringContent(output));
}

@Test
public void testDefaultAlgorithm() {
String input = inputPath + "beatles.ttl";
String expected = referencesPath + "beatles.nq";
String output = resultPath + "beatles.nq";

String[] args = { "-i", input, "-o", output };
int exitCode = cmd.execute(args);

assertEquals(0, exitCode);
assertEquals("", err.toString());
assertEquals("", out.toString());
assertEquals(getStringContent(expected), getStringContent(output));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix ns1: <http://example.com/> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .


<http://example.com/Love_Me_Do> ns1:length 125 ;
ns1:name "Love Me Do" ;
ns1:writer ns1:John_Lennon ;
ns1:writer ns1:Paul_McCartney ;
rdf:type ns1:Song .

ns1:The_Beatles ns1:member ns1:John_Lennon ;
ns1:member ns1:Paul_McCartney ;
ns1:member ns1:Ringo_Starr ;
ns1:member ns1:George_Harrison ;
ns1:name "The Beatles" ;
rdf:type ns1:Band .

ns1:Please_Please_Me ns1:artist ns1:The_Beatles ;
ns1:date "1963-03-22"^^xsd:date ;
ns1:name "Please Please Me" ;
ns1:track ns1:Love_Me_Do ;
rdf:type ns1:Album .

ns1:George_Harrison rdf:type ns1:SoloArtist .

ns1:Ringo_Starr rdf:type ns1:SoloArtist .

ns1:John_Lennon rdf:type ns1:SoloArtist .

ns1:Paul_McCartney rdf:type ns1:SoloArtist .

ns1:McCartney ns1:artist ns1:Paul_McCartney ;
ns1:date "1970-04-17"^^xsd:date ;
ns1:name "McCartney" ;
rdf:type ns1:Album .

ns1:Imagine ns1:artist ns1:John_Lennon ;
ns1:date "1971-10-11"^^xsd:date ;
ns1:name "Imagine" ;
rdf:type ns1:Album .

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
@prefix ex: <http://example.org/ns#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

ex:Alice ex:ssn "987-65-4323" ;
ex:worksFor ex:Haribo, ex:KitKat ;
rdf:type ex:Person .

ex:Bob ex:ssn "124-35-6789" ;
ex:worksFor ex:Twitch ;
rdf:type ex:Person .

ex:Calvin ex:ssn "648-67-6545" ;
ex:worksFor ex:UntypedCompany ;
rdf:type ex:Person .

ex:Haribo rdf:type ex:Company .

ex:KitKat rdf:type ex:Company .

ex:Twitch rdf:type ex:Company .

ex:UntypedCompany rdf:type ex:Company .

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<http://example.com/George_Harrison> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.com/SoloArtist> .
<http://example.com/Imagine> <http://example.com/artist> <http://example.com/John_Lennon> .
<http://example.com/Imagine> <http://example.com/date> "1971-10-11"^^<http://www.w3.org/2001/XMLSchema#date> .
<http://example.com/Imagine> <http://example.com/name> "Imagine" .
<http://example.com/Imagine> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.com/Album> .
<http://example.com/John_Lennon> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.com/SoloArtist> .
<http://example.com/Love_Me_Do> <http://example.com/length> "125"^^<http://www.w3.org/2001/XMLSchema#integer> .
<http://example.com/Love_Me_Do> <http://example.com/name> "Love Me Do" .
<http://example.com/Love_Me_Do> <http://example.com/writer> <http://example.com/John_Lennon> .
<http://example.com/Love_Me_Do> <http://example.com/writer> <http://example.com/Paul_McCartney> .
<http://example.com/Love_Me_Do> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.com/Song> .
<http://example.com/McCartney> <http://example.com/artist> <http://example.com/Paul_McCartney> .
<http://example.com/McCartney> <http://example.com/date> "1970-04-17"^^<http://www.w3.org/2001/XMLSchema#date> .
<http://example.com/McCartney> <http://example.com/name> "McCartney" .
<http://example.com/McCartney> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.com/Album> .
<http://example.com/Paul_McCartney> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.com/SoloArtist> .
<http://example.com/Please_Please_Me> <http://example.com/artist> <http://example.com/The_Beatles> .
<http://example.com/Please_Please_Me> <http://example.com/date> "1963-03-22"^^<http://www.w3.org/2001/XMLSchema#date> .
<http://example.com/Please_Please_Me> <http://example.com/name> "Please Please Me" .
<http://example.com/Please_Please_Me> <http://example.com/track> <http://example.com/Love_Me_Do> .
<http://example.com/Please_Please_Me> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.com/Album> .
<http://example.com/Ringo_Starr> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.com/SoloArtist> .
<http://example.com/The_Beatles> <http://example.com/member> <http://example.com/George_Harrison> .
<http://example.com/The_Beatles> <http://example.com/member> <http://example.com/John_Lennon> .
<http://example.com/The_Beatles> <http://example.com/member> <http://example.com/Paul_McCartney> .
<http://example.com/The_Beatles> <http://example.com/member> <http://example.com/Ringo_Starr> .
<http://example.com/The_Beatles> <http://example.com/name> "The Beatles" .
<http://example.com/The_Beatles> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.com/Band> .
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<http://example.com/George_Harrison> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.com/SoloArtist> .
<http://example.com/Imagine> <http://example.com/artist> <http://example.com/John_Lennon> .
<http://example.com/Imagine> <http://example.com/date> "1971-10-11"^^<http://www.w3.org/2001/XMLSchema#date> .
<http://example.com/Imagine> <http://example.com/name> "Imagine" .
<http://example.com/Imagine> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.com/Album> .
<http://example.com/John_Lennon> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.com/SoloArtist> .
<http://example.com/Love_Me_Do> <http://example.com/length> "125"^^<http://www.w3.org/2001/XMLSchema#integer> .
<http://example.com/Love_Me_Do> <http://example.com/name> "Love Me Do" .
<http://example.com/Love_Me_Do> <http://example.com/writer> <http://example.com/John_Lennon> .
<http://example.com/Love_Me_Do> <http://example.com/writer> <http://example.com/Paul_McCartney> .
<http://example.com/Love_Me_Do> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.com/Song> .
<http://example.com/McCartney> <http://example.com/artist> <http://example.com/Paul_McCartney> .
<http://example.com/McCartney> <http://example.com/date> "1970-04-17"^^<http://www.w3.org/2001/XMLSchema#date> .
<http://example.com/McCartney> <http://example.com/name> "McCartney" .
<http://example.com/McCartney> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.com/Album> .
<http://example.com/Paul_McCartney> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.com/SoloArtist> .
<http://example.com/Please_Please_Me> <http://example.com/artist> <http://example.com/The_Beatles> .
<http://example.com/Please_Please_Me> <http://example.com/date> "1963-03-22"^^<http://www.w3.org/2001/XMLSchema#date> .
<http://example.com/Please_Please_Me> <http://example.com/name> "Please Please Me" .
<http://example.com/Please_Please_Me> <http://example.com/track> <http://example.com/Love_Me_Do> .
<http://example.com/Please_Please_Me> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.com/Album> .
<http://example.com/Ringo_Starr> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.com/SoloArtist> .
<http://example.com/The_Beatles> <http://example.com/member> <http://example.com/George_Harrison> .
<http://example.com/The_Beatles> <http://example.com/member> <http://example.com/John_Lennon> .
<http://example.com/The_Beatles> <http://example.com/member> <http://example.com/Paul_McCartney> .
<http://example.com/The_Beatles> <http://example.com/member> <http://example.com/Ringo_Starr> .
<http://example.com/The_Beatles> <http://example.com/name> "The Beatles" .
<http://example.com/The_Beatles> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.com/Band> .
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<http://example.com/George_Harrison> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.com/SoloArtist> .
<http://example.com/Imagine> <http://example.com/artist> <http://example.com/John_Lennon> .
<http://example.com/Imagine> <http://example.com/date> "1971-10-11"^^<http://www.w3.org/2001/XMLSchema#date> .
<http://example.com/Imagine> <http://example.com/name> "Imagine" .
<http://example.com/Imagine> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.com/Album> .
<http://example.com/John_Lennon> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.com/SoloArtist> .
<http://example.com/Love_Me_Do> <http://example.com/length> "125"^^<http://www.w3.org/2001/XMLSchema#integer> .
<http://example.com/Love_Me_Do> <http://example.com/name> "Love Me Do" .
<http://example.com/Love_Me_Do> <http://example.com/writer> <http://example.com/John_Lennon> .
<http://example.com/Love_Me_Do> <http://example.com/writer> <http://example.com/Paul_McCartney> .
<http://example.com/Love_Me_Do> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.com/Song> .
<http://example.com/McCartney> <http://example.com/artist> <http://example.com/Paul_McCartney> .
<http://example.com/McCartney> <http://example.com/date> "1970-04-17"^^<http://www.w3.org/2001/XMLSchema#date> .
<http://example.com/McCartney> <http://example.com/name> "McCartney" .
<http://example.com/McCartney> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.com/Album> .
<http://example.com/Paul_McCartney> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.com/SoloArtist> .
<http://example.com/Please_Please_Me> <http://example.com/artist> <http://example.com/The_Beatles> .
<http://example.com/Please_Please_Me> <http://example.com/date> "1963-03-22"^^<http://www.w3.org/2001/XMLSchema#date> .
<http://example.com/Please_Please_Me> <http://example.com/name> "Please Please Me" .
<http://example.com/Please_Please_Me> <http://example.com/track> <http://example.com/Love_Me_Do> .
<http://example.com/Please_Please_Me> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.com/Album> .
<http://example.com/Ringo_Starr> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.com/SoloArtist> .
<http://example.com/The_Beatles> <http://example.com/member> <http://example.com/George_Harrison> .
<http://example.com/The_Beatles> <http://example.com/member> <http://example.com/John_Lennon> .
<http://example.com/The_Beatles> <http://example.com/member> <http://example.com/Paul_McCartney> .
<http://example.com/The_Beatles> <http://example.com/member> <http://example.com/Ringo_Starr> .
<http://example.com/The_Beatles> <http://example.com/name> "The Beatles" .
<http://example.com/The_Beatles> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.com/Band> .
<http://example.org/ns#Alice> <http://example.org/ns#ssn> "987-65-4323" .
<http://example.org/ns#Alice> <http://example.org/ns#worksFor> <http://example.org/ns#Haribo> .
<http://example.org/ns#Alice> <http://example.org/ns#worksFor> <http://example.org/ns#KitKat> .
<http://example.org/ns#Alice> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.org/ns#Person> .
<http://example.org/ns#Bob> <http://example.org/ns#ssn> "124-35-6789" .
<http://example.org/ns#Bob> <http://example.org/ns#worksFor> <http://example.org/ns#Twitch> .
<http://example.org/ns#Bob> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.org/ns#Person> .
<http://example.org/ns#Calvin> <http://example.org/ns#ssn> "648-67-6545" .
<http://example.org/ns#Calvin> <http://example.org/ns#worksFor> <http://example.org/ns#UntypedCompany> .
<http://example.org/ns#Calvin> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.org/ns#Person> .
<http://example.org/ns#Haribo> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.org/ns#Company> .
<http://example.org/ns#KitKat> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.org/ns#Company> .
<http://example.org/ns#Twitch> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.org/ns#Company> .
<http://example.org/ns#UntypedCompany> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.org/ns#Company> .
Loading

0 comments on commit 45e0fd7

Please sign in to comment.