Search in sources :

Example 1 with GraphCollection

use of de.uni_koblenz.aggrimm.icp.crypto.sign.graph.GraphCollection in project webofneeds by researchstudio-sat.

the class WonAssemblerTest method testAssembleOneGraphSignature.

@Test
public void testAssembleOneGraphSignature() throws Exception {
    // The Signingframework reader cannot reproduce the correct graph
    // structure, it has problems with blank nodes [] parts.
    // GraphCollection gc = TriGPlusReader.readFile(inFile);
    // create dataset that contains need core data graph
    Dataset testDataset = TestSigningUtils.prepareTestDatasetFromNamedGraphs(RESOURCE_FILE, new String[] { NEED_CORE_DATA_URI });
    // convert to graph collection
    GraphCollection gc = ModelConverter.modelToGraphCollection(NEED_CORE_DATA_URI, testDataset);
    // create mock signature
    SignatureData mockSigData = createMockSignature();
    gc.setSignature(mockSigData);
    // test assemble()
    WonAssembler.assemble(gc, testDataset, NEED_CORE_DATA_SIG_URI);
    // use for debugging output
    // TestSigningUtils.writeToTempFile(testDataset);
    // extract names of the named graphs
    List<String> namesList = RdfUtils.getModelNames(testDataset);
    // do some checks to make sure there is 1 signed names graph
    Assert.assertEquals("should be one named graph with data and one named graph with signature", 2, namesList.size());
    Assert.assertTrue("should be some triples in signature graph", testDataset.getNamedModel(NEED_CORE_DATA_SIG_URI).listStatements().hasNext());
    Assert.assertTrue("should be no triples in default graph", !testDataset.getDefaultModel().listStatements().hasNext());
    int triplesCounter = TestSigningUtils.countTriples(testDataset.getNamedModel(NEED_CORE_DATA_SIG_URI).listStatements());
    Set<String> subjs = TestSigningUtils.getSubjects(testDataset.getNamedModel(NEED_CORE_DATA_SIG_URI));
    Set<String> objs = TestSigningUtils.getUriResourceObjects(testDataset.getNamedModel(NEED_CORE_DATA_SIG_URI));
    Assert.assertEquals("signature graph should contain 11 triples", 11, triplesCounter);
    Assert.assertTrue("signed graph name should be an object in signature triples", objs.contains(NEED_CORE_DATA_URI));
    Assert.assertTrue("signature graph name should be a subject in signature triples", subjs.contains(NEED_CORE_DATA_SIG_URI));
}
Also used : GraphCollection(de.uni_koblenz.aggrimm.icp.crypto.sign.graph.GraphCollection) SignatureData(de.uni_koblenz.aggrimm.icp.crypto.sign.graph.SignatureData) Dataset(org.apache.jena.query.Dataset) Test(org.junit.Test)

Example 2 with GraphCollection

use of de.uni_koblenz.aggrimm.icp.crypto.sign.graph.GraphCollection in project webofneeds by researchstudio-sat.

the class SigningFrameworkTest method readWriteGcTest.

// read write from signingframework by signingframework produces orig input?
// yes
@Test
public void readWriteGcTest() throws Exception {
    String inFile = SigningFrameworkTest.class.getResource(SIGNING_FW_TEST_1_FILE_SIGNED).getPath();
    // File outFile = testFolder.newFile();
    File outFile = File.createTempFile("won", ".trig");
    System.out.println(outFile);
    GraphCollection gc = TriGPlusReader.readFile(inFile);
    TriGPlusWriter.writeFile(gc, outFile.getAbsolutePath());
// outFile.delete();
// TODO chng to testFolder and assert equal graphs?
}
Also used : GraphCollection(de.uni_koblenz.aggrimm.icp.crypto.sign.graph.GraphCollection) Test(org.junit.Test)

Example 3 with GraphCollection

use of de.uni_koblenz.aggrimm.icp.crypto.sign.graph.GraphCollection in project webofneeds by researchstudio-sat.

the class ModelConverterTest method modelToGraphCollectionTest.

@Test
@Ignore
public /**
 * Reads from TRIG with Jena API into Dataset 1, transforms one
 * named graph from that Dataset into Signingframework's API
 * GraphCollection and writes it with Signingframework's API,
 * reads the result with Jena API into Dataset 2, and checks
 * if the specified named graph model from Dataset 1 is
 * isomorphic with the same named graph model from Dataset 2.
 */
void modelToGraphCollectionTest() throws Exception {
    for (String resourceFile : RESOURCE_FILES) {
        // prepare the input Dataset containg the Model to be converted
        InputStream is = this.getClass().getResourceAsStream(resourceFile);
        File outFile = testFolder.newFile();
        // use this when debugging:
        // File outFile = File.createTempFile("won", ".trig");
        // System.out.println(outFile);
        Dataset dataset = DatasetFactory.createGeneral();
        RDFDataMgr.read(dataset, is, RDFFormat.TRIG.getLang());
        is.close();
        // test the convertion from the Model to the NamedGraph
        String modelName = dataset.listNames().next();
        Model model = dataset.getNamedModel(modelName);
        // the method to be tested
        GraphCollection gc = ModelConverter.modelToGraphCollection(modelName, dataset);
        TriGPlusWriter.writeFile(gc, outFile.getAbsolutePath(), false);
        // check that the resulting graph collection is a representation
        // of the converted model. For this, read the resulting graph collection
        // as a Model with Jena API
        InputStream is2 = new FileInputStream(outFile);
        Dataset dataset2 = DatasetFactory.createGeneral();
        RDFDataMgr.read(dataset2, is2, RDFFormat.TRIG.getLang());
        is2.close();
        Model model2 = dataset2.getNamedModel(modelName);
        File outFile2 = testFolder.newFile();
        // use this when debugging:
        // File outFile2 = File.createTempFile("won", ".trig");
        // System.out.println(outFile2);
        OutputStream os = new FileOutputStream(outFile2);
        RDFDataMgr.write(os, dataset2, RDFFormat.TRIG.getLang());
        os.close();
        // check that the model obtained from resulting graph collection is
        // a representation of the original converted model.
        Assert.assertTrue(model.listStatements().hasNext() && model2.listStatements().hasNext());
        Assert.assertTrue(model.isIsomorphicWith(model2));
    }
}
Also used : GraphCollection(de.uni_koblenz.aggrimm.icp.crypto.sign.graph.GraphCollection) Dataset(org.apache.jena.query.Dataset) Model(org.apache.jena.rdf.model.Model) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 4 with GraphCollection

use of de.uni_koblenz.aggrimm.icp.crypto.sign.graph.GraphCollection in project webofneeds by researchstudio-sat.

the class ModelConverterTest method namedGraphToModelTest.

@Test
@Ignore
public /**
 * Reads from TRIG with Jena API into Dataset 1, transforms one
 * named Model from that Dataset into Signingframework's API
 * GraphCollection with one NamedGraph, transforms (converts)
 * that NamedGraph into Jena's Model, and checks
 * if the resulting Model is the same as original Model.
 */
void namedGraphToModelTest() throws Exception {
    for (String resourceFile : RESOURCE_FILES) {
        // prepare GraphCollection with NamedGraph to be converted:
        InputStream is = this.getClass().getResourceAsStream(resourceFile);
        Dataset dataset = DatasetFactory.createGeneral();
        RDFDataMgr.read(dataset, is, RDFFormat.TRIG.getLang());
        is.close();
        String modelName = dataset.listNames().next();
        Model model1 = dataset.getNamedModel(modelName);
        // this method is not tested here and used just for input
        // generation and to make it easier Namedgraph<->Model comparison
        // (but it's tested in other method, see modelToGraphCollectionTest())
        GraphCollection gc = ModelConverter.modelToGraphCollection(modelName, dataset);
        LinkedList<NamedGraph> graphs = gc.getGraphs();
        String graphName = null;
        for (NamedGraph g : graphs) {
            if (!g.getName().isEmpty() && g.getName().contains(modelName)) {
                graphName = g.getName();
                break;
            }
        }
        // use this when debugging:
        // File outFile0 = File.createTempFile("won", ".trig");
        // System.out.println(outFile0);
        // OutputStream os0 = new FileOutputStream(outFile0);
        // TriGPlusWriter.writeFile(gc, outFile0.getAbsolutePath(), false);
        // os0.close();
        // test convert from NamedGraph of GraphCollection into Model
        Model model2 = ModelConverter.namedGraphToModel(graphName, gc);
        Dataset dataset2 = DatasetFactory.createGeneral();
        dataset2.addNamedModel(modelName, model2);
        // TODO maybe chng the API so that the prefix map is taken care of in the converter:
        // if it makes sense from the the usage of this in Assembler point of view
        dataset2.getDefaultModel().setNsPrefixes(dataset2.getNamedModel(modelName).getNsPrefixMap());
        File outFile = testFolder.newFile();
        // use this when debugging:
        // File outFile = File.createTempFile("won", ".trig");
        // System.out.println(outFile);
        OutputStream os = new FileOutputStream(outFile);
        RDFDataMgr.write(os, dataset2, RDFFormat.TRIG.getLang());
        os.close();
        // make sure that the original Model that was used to generate test input
        // GraphCollection with NamedGraph is isomorphic with the Model after
        // conversion is applied:
        Assert.assertTrue(model1.listStatements().hasNext() && model2.listStatements().hasNext());
        Assert.assertTrue(model1.isIsomorphicWith(model2));
    }
}
Also used : GraphCollection(de.uni_koblenz.aggrimm.icp.crypto.sign.graph.GraphCollection) NamedGraph(de.uni_koblenz.aggrimm.icp.crypto.sign.graph.NamedGraph) Dataset(org.apache.jena.query.Dataset) Model(org.apache.jena.rdf.model.Model) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 5 with GraphCollection

use of de.uni_koblenz.aggrimm.icp.crypto.sign.graph.GraphCollection in project webofneeds by researchstudio-sat.

the class ModelConverter method modelToGraphCollection.

private static GraphCollection modelToGraphCollection(String name, Model model, Map<String, String> pm) {
    // Convert each subj pred obj in Jena Statement into String and add to
    // SigningFramework's NamedGraph.
    // The simpler approach with just using Jena's writer and Signingframework's
    // reader to transform data between data structures won't work since
    // Signingframework has problems with recognizing the [] structure
    GraphCollection graphc = new GraphCollection();
    NamedGraph namedGraph = new NamedGraph(enclose(name, "<", ">"), 0, null);
    StmtIterator iterator = model.listStatements();
    while (iterator.hasNext()) {
        Statement stmt = iterator.nextStatement();
        String subjString = rdfNodeAsString(stmt.getSubject());
        String objString = rdfNodeAsString(stmt.getObject());
        String predString = enclose(stmt.getPredicate().asResource().getURI(), "<", ">");
        Triple gcTriple = new Triple(subjString, predString, objString);
        namedGraph.addTriple(gcTriple);
    }
    graphc.addGraph(namedGraph);
    for (String prefix : pm.keySet()) {
        graphc.addPrefix(new Prefix(prefix + ":", "<" + pm.get(prefix) + ">"));
    }
    return graphc;
}
Also used : GraphCollection(de.uni_koblenz.aggrimm.icp.crypto.sign.graph.GraphCollection) Triple(de.uni_koblenz.aggrimm.icp.crypto.sign.graph.Triple) StmtIterator(org.apache.jena.rdf.model.StmtIterator) NamedGraph(de.uni_koblenz.aggrimm.icp.crypto.sign.graph.NamedGraph) Statement(org.apache.jena.rdf.model.Statement) Prefix(de.uni_koblenz.aggrimm.icp.crypto.sign.graph.Prefix)

Aggregations

GraphCollection (de.uni_koblenz.aggrimm.icp.crypto.sign.graph.GraphCollection)7 Test (org.junit.Test)4 Dataset (org.apache.jena.query.Dataset)3 NamedGraph (de.uni_koblenz.aggrimm.icp.crypto.sign.graph.NamedGraph)2 SignatureData (de.uni_koblenz.aggrimm.icp.crypto.sign.graph.SignatureData)2 StringWriter (java.io.StringWriter)2 Model (org.apache.jena.rdf.model.Model)2 Ignore (org.junit.Ignore)2 WonSignatureData (won.protocol.message.WonSignatureData)2 SignatureAlgorithmInterface (de.uni_koblenz.aggrimm.icp.crypto.sign.algorithm.SignatureAlgorithmInterface)1 SignatureAlgorithmFisteus2010 (de.uni_koblenz.aggrimm.icp.crypto.sign.algorithm.algorithm.SignatureAlgorithmFisteus2010)1 Prefix (de.uni_koblenz.aggrimm.icp.crypto.sign.graph.Prefix)1 Triple (de.uni_koblenz.aggrimm.icp.crypto.sign.graph.Triple)1 BigInteger (java.math.BigInteger)1 MessageDigest (java.security.MessageDigest)1 PublicKey (java.security.PublicKey)1 Signature (java.security.Signature)1 ArrayList (java.util.ArrayList)1 Statement (org.apache.jena.rdf.model.Statement)1 StmtIterator (org.apache.jena.rdf.model.StmtIterator)1