<commit_before>package org.purl.wf4ever.robundle.manifest;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.purl.wf4ever.robundle.Bundle;
import org.purl.wf4ever.robundle.Bundles;

import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.Resource;

public class TestManifest {
    private Bundle bundle;

    @Test
    public void populateFromBundle() throws Exception {
        Path r = bundle.getRoot();
        URI base = r.toUri();

        Manifest manifest = new Manifest(bundle);
        manifest.populateFromBundle();

        List<String> uris = new ArrayList<>();
        for (PathMetadata s : manifest.getAggregates()) {
            uris.add(s.getFile().toString());
            Path path = s.getFile();
            assertNotNull(path.getParent());
            assertEquals(Manifest.withSlash(path.getParent()), s.getFolder());
            if (s.getFile().equals(URI.create("/f/nested/empty/"))) {
                continue;
                // Folder's don't need proxy and createdOn
            }            
            assertEquals("urn", s.getProxy().getScheme());
            UUID.fromString(s.getProxy().getSchemeSpecificPart().replace("uuid:", ""));
            assertEquals(s.getCreatedOn(), Files.getLastModifiedTime(path));
        }
        System.out.println(uris);
        assertFalse(uris.contains("/mimetype"));
        assertFalse(uris.contains("/META-INF"));
        assertTrue(uris.remove("/hello.txt"));
        assertTrue(uris.remove("/f/file1.txt"));
        assertTrue(uris.remove("/f/file2.txt"));
        assertTrue(uris.remove("/f/file3.txt"));
        assertTrue(uris.remove("/f/nested/file1.txt"));
        assertTrue(uris.remove("/f/nested/empty/"));
        assertTrue(uris.isEmpty());
    }

    private Path uri2path(URI base, URI uri) {
        URI fileUri = base.resolve(uri);
        return Paths.get(fileUri);
    }


    @Test
    public void writeAsJsonLD() throws Exception {
        Manifest manifest = new Manifest(bundle);
        manifest.populateFromBundle();
        PathMetadata helloMeta = null;        
        for (PathMetadata meta : manifest.getAggregates()) {            
            if (meta.getFile().endsWith("hello.txt")) {
                helloMeta = meta;
            }
        }
        assertNotNull("No metadata for </hello.txt>", helloMeta);
        
        
        
        Path jsonld = manifest.writeAsJsonLD();
        assertEquals(bundle.getFileSystem().getPath(".ro",  "manifest.json"), jsonld);
        assertTrue(Files.exists(jsonld));
        String manifestStr = new String(Files.readAllBytes(jsonld), "UTF8");
        System.out.println(manifestStr);
        
        // Rough and ready that somethings are there
        // TODO: Read back and check as JSON structure
        // TODO: Check as JSON-LD graph 
        assertTrue(manifestStr.contains("@context"));
        assertTrue(manifestStr.contains("http://purl.org/wf4ever/ro-bundle/context.json"));
        assertTrue(manifestStr.contains("/f/file2.txt"));
        assertTrue(manifestStr.contains("/hello.txt"));
        assertTrue(manifestStr.contains(helloMeta.getProxy().toASCIIString()));
        
        // Parse back as JSON-LD
        try (InputStream jsonIn = Files.newInputStream(jsonld)) {        
            URI baseURI = bundle.getRoot().toUri(); 
                    //RDFToManifest.makeBaseURI();
            Model model = RDFToManifest.jsonLdAsJenaModel(jsonIn, baseURI);
            model.write(System.out, "TURTLE", baseURI.toString());
            model.write(System.out, "RDF/XML", baseURI.toString());
           
            String queryStr = "PREFIX ore: <http://www.openarchives.org/ore/terms/>" +<commit_msg>parse relative to absolute URI of .ro/manifest.json<commit_after>package org.purl.wf4ever.robundle.manifest;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.purl.wf4ever.robundle.Bundle;
import org.purl.wf4ever.robundle.Bundles;

import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.Resource;

public class TestManifest {
    private Bundle bundle;

    @Test
    public void populateFromBundle() throws Exception {
        Path r = bundle.getRoot();
        URI base = r.toUri();

        Manifest manifest = new Manifest(bundle);
        manifest.populateFromBundle();

        List<String> uris = new ArrayList<>();
        for (PathMetadata s : manifest.getAggregates()) {
            uris.add(s.getFile().toString());
            Path path = s.getFile();
            assertNotNull(path.getParent());
            assertEquals(Manifest.withSlash(path.getParent()), s.getFolder());
            if (s.getFile().equals(URI.create("/f/nested/empty/"))) {
                continue;
                // Folder's don't need proxy and createdOn
            }            
            assertEquals("urn", s.getProxy().getScheme());
            UUID.fromString(s.getProxy().getSchemeSpecificPart().replace("uuid:", ""));
            assertEquals(s.getCreatedOn(), Files.getLastModifiedTime(path));
        }
        System.out.println(uris);
        assertFalse(uris.contains("/mimetype"));
        assertFalse(uris.contains("/META-INF"));
        assertTrue(uris.remove("/hello.txt"));
        assertTrue(uris.remove("/f/file1.txt"));
        assertTrue(uris.remove("/f/file2.txt"));
        assertTrue(uris.remove("/f/file3.txt"));
        assertTrue(uris.remove("/f/nested/file1.txt"));
        assertTrue(uris.remove("/f/nested/empty/"));
        assertTrue(uris.isEmpty());
    }

    private Path uri2path(URI base, URI uri) {
        URI fileUri = base.resolve(uri);
        return Paths.get(fileUri);
    }


    @Test
    public void writeAsJsonLD() throws Exception {
        Manifest manifest = new Manifest(bundle);
        manifest.populateFromBundle();
        PathMetadata helloMeta = null;        
        for (PathMetadata meta : manifest.getAggregates()) {            
            if (meta.getFile().endsWith("hello.txt")) {
                helloMeta = meta;
            }
        }
        assertNotNull("No metadata for </hello.txt>", helloMeta);
        
        
        
        Path jsonld = manifest.writeAsJsonLD();
        assertEquals(bundle.getFileSystem().getPath(".ro",  "manifest.json"), jsonld);
        assertTrue(Files.exists(jsonld));
        String manifestStr = new String(Files.readAllBytes(jsonld), "UTF8");
        System.out.println(manifestStr);
        
        // Rough and ready that somethings are there
        // TODO: Read back and check as JSON structure
        // TODO: Check as JSON-LD graph 
        assertTrue(manifestStr.contains("@context"));
        assertTrue(manifestStr.contains("http://purl.org/wf4ever/ro-bundle/context.json"));
        assertTrue(manifestStr.contains("/f/file2.txt"));
        assertTrue(manifestStr.contains("/hello.txt"));
        assertTrue(manifestStr.contains(helloMeta.getProxy().toASCIIString()));
        
        // Parse back as JSON-LD
        try (InputStream jsonIn = Files.newInputStream(jsonld)) {        
            URI baseURI = jsonld.toUri();
            Model model = RDFToManifest.jsonLdAsJenaModel(jsonIn, baseURI);
            model.write(System.out, "TURTLE", baseURI.toString());
            model.write(System.out, "RDF/XML", baseURI.toString());
           
            String queryStr = "PREFIX ore: <http://www.openarchives.org/ore/terms/>" +
{'fraction_non_alphanumeric': 0.13611738148984198, 'fraction_numerical': 0.0024830699774266367, 'mean_word_length': 2.673714759535655, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1116000', 'n_tokens_mistral': 2621, 'n_tokens_neox': 2588, 'n_words': 457}