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

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

import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.FileTime;
import java.util.Calendar;
import java.util.Locale;
import java.util.TimeZone;

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

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

public class TestManifestJSON {
	@Test
	public void createBundle() throws Exception {
		// Create bundle as in Example 3 of the specification
		// http://wf4ever.github.io/ro/bundle/2013-05-21/
		try (Bundle bundle = Bundles.createBundle()) {
			Calendar createdOnCal = Calendar.getInstance(TimeZone.getTimeZone("Z"), Locale.ENGLISH);
			// "2013-03-05T17:29:03Z"
			// Remember months are 0-based in java.util.Calendar!
			createdOnCal.set(2013, 3-1, 5, 17, 29, 03);			
			createdOnCal.set(Calendar.MILLISECOND, 0);
			FileTime createdOn = FileTime.fromMillis(createdOnCal.getTimeInMillis());			
			Manifest manifest = bundle.getManifest();
			manifest.setCreatedOn(createdOn);
			Agent createdBy = new Agent("<NAME>");
			createdBy.setUri(URI.create("http://example.com/foaf#alice"));
			createdBy.setOrcid(URI.create("http://orcid.org/0000-0002-1825-0097"));
			
			manifest.setCreatedBy(createdBy);
			
			Path evolutionPath = bundle.getPath(".ro/evolution.ttl");
			Files.createDirectories(evolutionPath.getParent());
			Bundles.setStringValue(evolutionPath, "<manifest.json> < http://purl.org/pav/retrievedFrom> " +
					"<http://wf4ever.github.io/ro/bundle/2013-05-21/example/.ro/manifest.json> .");
			manifest.getHistory().add(evolutionPath);
			
			
			Path jpeg = bundle.getPath("folder/soup.jpeg");
			Files.createDirectory(jpeg.getParent());
			Files.createFile(jpeg);
			// register in manifest first
			bundle.getManifest().getAggregation(jpeg);

			URI blog = URI.create("http://example.com/blog/");
			bundle.getManifest().getAggregation(blog);

			Path readme = bundle.getPath("README.txt");
			Files.createFile(readme);
			PathMetadata readmeMeta = bundle.getManifest().getAggregation(readme);
			readmeMeta.setMediatype("text/plain");
			Agent readmeCreatedby = new Agent("<NAME>");
			readmeCreatedby.setUri(URI.create("http://example.com/foaf#bob"));
			readmeMeta.setCreatedBy(readmeCreatedby);
			
			// 2013-02-12T19:37:32.939Z
			createdOnCal.set(2013, 2-1, 12, 19, 37, 32);
			createdOnCal.set(Calendar.MILLISECOND, 939);
			createdOn = FileTime.fromMillis(createdOnCal.getTimeInMillis());
			Files.setLastModifiedTime(readme, createdOn);
			readmeMeta.setCreatedOn(createdOn);

			PathMetadata comments = bundle.getManifest().getAggregation(URI.create("http://example.com/comments.txt"));
			comments.getOrCreateBundledAs().setProxy(URI.create("urn:uuid:a0cf8616-bee4-4a71-b21e-c60e6499a644"));
			comments.getOrCreateBundledAs().setFolder(bundle.getPath("/folder/"));
			comments.getOrCreateBundledAs().setFilename("external.txt");
			
            PathAnnotation jpegAnn = new PathAnnotation();
            jpegAnn.setAbout(jpeg);
            Path soupProps = Bundles.getAnnotations(bundle).resolve("soup-properties.ttl");
            Bundles.setStringValue(soupProps, "</folder/soup.jpeg> <http://xmlns.com/foaf/0.1/depicts> " +
            		"<http://example.com/menu/tomato-soup> .");
            jpegAnn.setContent(soupProps);
//            jpegAnn.setContent(URI.create("annotations/soup-properties.ttl"));
            jpegAnn.setAnnotation(URI.create("urn:uuid:d67466b4-3aeb-4855-8203-90febe71abdf"));
            manifest.getAnnotations().add(jpegAnn);
			
			
            PathAnnotation proxyAnn = new PathAnnotation();
            proxyAnn.setAbout(comments.getBundledAs().getProxy());
			proxyAnn.setContent(URI.create("http://example.com/blog/they-aggregated-our-file"));
            manifest.getAnnotations().add(proxyAnn);
            
            Path metaAnn = Bundles.getAnnotations(bundle).resolve("a-meta-annotation-in-this-ro.txt");
            Bundles.setStringValue(metaAnn, "This bundle contains an annotation about /folder/soup.jpeg");
            
            PathAnnotation metaAnnotation = new PathAnnotation();
            metaAnnotation.setAbout(bundle.getRoot());
            metaAnnotation.getAboutList().add(URI.create("urn:uuid:d67466b4-3aeb-4855-8203-90febe71abdf"));
            
            metaAnnotation.setContent(metaAnn);
			manifest.getAnnotations().add(metaAnnotation);
            
			Path jsonPath = bundle.getManifest().writeAsJsonLD();
			ObjectMapper objectMapper = new ObjectMapper();
			String jsonStr = Bundles.getStringValue(jsonPath);
			System.out.println(jsonStr);
			JsonNode json = objectMapper.readTree(jsonStr);
			
			checkManifestJson(json);
				
			
		}
	}
	
	public void checkManifestJson(JsonNode json) {
		JsonNode context = json.get("@context");
		assertNotNull("Could not find @context", context);
		assertTrue("@context SHOULD be an array", context.isArray());
		assertTrue("@context SHOULD include a context", context.size() > 0);
		JsonNode lastContext = context.get(context.size() -1);
		assertEquals("@context SHOULD include https://w3id.org/bundle/context as last item", 
				"https://w3id.org/bundle/context",
				lastContext.asText());
		
		assertEquals("/", json.get("id").asText());<commit_msg>formatting<commit_after>package org.purl.wf4ever.robundle.manifest;

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

import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.FileTime;
import java.util.Calendar;
import java.util.Locale;
import java.util.TimeZone;

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

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

public class TestManifestJSON {
	@Test
	public void createBundle() throws Exception {
		// Create bundle as in Example 3 of the specification
		// http://wf4ever.github.io/ro/bundle/2013-05-21/
		try (Bundle bundle = Bundles.createBundle()) {
			Calendar createdOnCal = Calendar.getInstance(TimeZone.getTimeZone("Z"), Locale.ENGLISH);
			// "2013-03-05T17:29:03Z"
			// Remember months are 0-based in java.util.Calendar!
			createdOnCal.set(2013, 3-1, 5, 17, 29, 03);			
			createdOnCal.set(Calendar.MILLISECOND, 0);
			FileTime createdOn = FileTime.fromMillis(createdOnCal.getTimeInMillis());			
			Manifest manifest = bundle.getManifest();
			manifest.setCreatedOn(createdOn);
			Agent createdBy = new Agent("<NAME>");
			createdBy.setUri(URI.create("http://example.com/foaf#alice"));
			createdBy.setOrcid(URI.create("http://orcid.org/0000-0002-1825-0097"));
			
			manifest.setCreatedBy(createdBy);
			
			Path evolutionPath = bundle.getPath(".ro/evolution.ttl");
			Files.createDirectories(evolutionPath.getParent());
			Bundles.setStringValue(evolutionPath, "<manifest.json> < http://purl.org/pav/retrievedFrom> " +
					"<http://wf4ever.github.io/ro/bundle/2013-05-21/example/.ro/manifest.json> .");
			manifest.getHistory().add(evolutionPath);
			
			
			Path jpeg = bundle.getPath("folder/soup.jpeg");
			Files.createDirectory(jpeg.getParent());
			Files.createFile(jpeg);
			// register in manifest first
			bundle.getManifest().getAggregation(jpeg);

			URI blog = URI.create("http://example.com/blog/");
			bundle.getManifest().getAggregation(blog);

			Path readme = bundle.getPath("README.txt");
			Files.createFile(readme);
			PathMetadata readmeMeta = bundle.getManifest().getAggregation(readme);
			readmeMeta.setMediatype("text/plain");
			Agent readmeCreatedby = new Agent("<NAME>");
			readmeCreatedby.setUri(URI.create("http://example.com/foaf#bob"));
			readmeMeta.setCreatedBy(readmeCreatedby);
			
			// 2013-02-12T19:37:32.939Z
			createdOnCal.set(2013, 2-1, 12, 19, 37, 32);
			createdOnCal.set(Calendar.MILLISECOND, 939);
			createdOn = FileTime.fromMillis(createdOnCal.getTimeInMillis());
			Files.setLastModifiedTime(readme, createdOn);
			readmeMeta.setCreatedOn(createdOn);

			PathMetadata comments = bundle.getManifest().getAggregation(URI.create("http://example.com/comments.txt"));
			comments.getOrCreateBundledAs().setProxy(URI.create("urn:uuid:a0cf8616-bee4-4a71-b21e-c60e6499a644"));
			comments.getOrCreateBundledAs().setFolder(bundle.getPath("/folder/"));
			comments.getOrCreateBundledAs().setFilename("external.txt");
			
            PathAnnotation jpegAnn = new PathAnnotation();
            jpegAnn.setAbout(jpeg);
            Path soupProps = Bundles.getAnnotations(bundle).resolve("soup-properties.ttl");
            Bundles.setStringValue(soupProps, "</folder/soup.jpeg> <http://xmlns.com/foaf/0.1/depicts> " +
            		"<http://example.com/menu/tomato-soup> .");
            jpegAnn.setContent(soupProps);
//            jpegAnn.setContent(URI.create("annotations/soup-properties.ttl"));
            jpegAnn.setAnnotation(URI.create("urn:uuid:d67466b4-3aeb-4855-8203-90febe71abdf"));
            manifest.getAnnotations().add(jpegAnn);
			
			
            PathAnnotation proxyAnn = new PathAnnotation();
            proxyAnn.setAbout(comments.getBundledAs().getProxy());
			proxyAnn.setContent(URI.create("http://example.com/blog/they-aggregated-our-file"));
            manifest.getAnnotations().add(proxyAnn);
            
            Path metaAnn = Bundles.getAnnotations(bundle).resolve("a-meta-annotation-in-this-ro.txt");
            Bundles.setStringValue(metaAnn, "This bundle contains an annotation about /folder/soup.jpeg");
            
            PathAnnotation metaAnnotation = new PathAnnotation();
            metaAnnotation.setAbout(bundle.getRoot());
            metaAnnotation.getAboutList().add(URI.create("urn:uuid:d67466b4-3aeb-4855-8203-90febe71abdf"));
            
            metaAnnotation.setContent(metaAnn);
			manifest.getAnnotations().add(metaAnnotation);
            
			Path jsonPath = bundle.getManifest().writeAsJsonLD();
			ObjectMapper objectMapper = new ObjectMapper();
			String jsonStr = Bundles.getStringValue(jsonPath);
			System.out.println(jsonStr);
			JsonNode json = objectMapper.readTree(jsonStr);			
			checkManifestJson(json);
		}
	}
	
	public void checkManifestJson(JsonNode json) {
		JsonNode context = json.get("@context");
		assertNotNull("Could not find @context", context);
		assertTrue("@context SHOULD be an array", context.isArray());
		assertTrue("@context SHOULD include a context", context.size() > 0);
		JsonNode lastContext = context.get(context.size() -1);
		assertEquals("@context SHOULD include https://w3id.org/bundle/context as last item", 
				"https://w3id.org/bundle/context",
				lastContext.asText());
		
		assertEquals("/", json.get("id").asText());
{'fraction_non_alphanumeric': 0.14253454972450547, 'fraction_numerical': 0.02980760545569506, 'mean_word_length': 3.989634970707526, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 21, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, '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': '5218680', 'n_tokens_mistral': 4040, 'n_tokens_neox': 3475, 'n_words': 495}