@Override public Content adapt(DocumentWriteOperation operation) { String uri = operation.getUri(); ContentCreateOptions options = adaptMetadata(operation.getMetadata()); AbstractWriteHandle handle = operation.getContent(); if (handle instanceof StringHandle) { return ContentFactory.newContent(uri, ((StringHandle) handle).get(), options); } else if (handle instanceof FileHandle) { return ContentFactory.newContent(uri, ((FileHandle) handle).get(), options); } else if (handle instanceof BytesHandle) { return ContentFactory.newContent(uri, ((BytesHandle) handle).get(), options); } else if (handle instanceof InputStreamHandle) { try { return ContentFactory.newContent(uri, ((InputStreamHandle) handle).get(), options); } catch (IOException e) { throw new RuntimeException("Unable to read content input stream: " + e.getMessage(), e); } } else if (handle instanceof DOMHandle) { return ContentFactory.newContent(uri, ((DOMHandle) handle).get(), options); } else throw new IllegalArgumentException("No support yet for content class: " + handle.getClass().getName()); }
System.out.println(file1.getName() + ":" + fsize1 + " " + readHandle1.get().getName() + ":" + readHandle1.get().length()); System.out.println(file2.getName() + ":" + fsize2 + " " + readHandle2.get().getName() + ":" + readHandle2.get().length()); assertEquals("Size of the File 1" + docId[0], fsize1, readHandle1.get().length()); assertEquals("Size of the File 1" + docId[1], fsize2, readHandle2.get().length());
@Test public void testWrite_rdfjson_FileHandle() throws Exception { ObjectMapper mapper = new ObjectMapper(); new ObjectMapper(); File file = new File(datasource + "relative6.json"); FileHandle filehandle = new FileHandle(); filehandle.set(file); gmWriter.write("htp://test.sem.graph/rdfjson", filehandle.withMimetype("application/rdf+json")); FileHandle handle = new FileHandle(); handle.setMimetype(RDFMimeTypes.RDFJSON); handle = gmWriter.read("htp://test.sem.graph/rdfjson", handle); File readFile = handle.get(); JsonNode readContent = mapper.readTree(readFile); // Verify read content with inserted content assertTrue("Did not insert document or inserted empty doc", readContent.toString() .contains("http://purl.org/dc/elements/1.1/title")); }
@Test public void testWriteRead_defaultGraph() throws FileNotFoundException { File file = new File(datasource + "semantics.trig"); FileHandle filehandle = new FileHandle(); filehandle.set(file); gmWriter.write(gmWriter.DEFAULT_GRAPH, filehandle.withMimetype(RDFMimeTypes.TRIG)); FileHandle handle = gmWriter.read(gmWriter.DEFAULT_GRAPH, new FileHandle()); GraphPermissions permissions = gmWriter.getPermissions(gmWriter.DEFAULT_GRAPH); System.out.println(permissions); assertEquals(Capability.UPDATE, permissions.get("rest-writer").iterator().next()); assertEquals(Capability.READ, permissions.get("rest-reader").iterator().next()); gmWriter.deletePermissions(gmWriter.DEFAULT_GRAPH); permissions = gmWriter.getPermissions(gmWriter.DEFAULT_GRAPH); System.out.println(permissions); assertEquals(Capability.UPDATE, permissions.get("rest-writer").iterator().next()); File readFile = handle.get(); String expectedContent = convertFileToString(readFile); System.out.println(gmWriter.listGraphUris().next().toString()); assertTrue("" + gmWriter.listGraphUris().next().toString(), gmWriter.listGraphUris().next().toString().equals("http://marklogic.com/semantics#default-graph")); assertTrue("Did not insert document or inserted empty doc", expectedContent.contains("http://www.example.org/exampleDocument#Monica")); }
File fileRead = readHandle.get();
@Test public void testMerge_trig_FileHandle() throws Exception { File file = new File(datasource + "trig.trig"); FileHandle filehandle = new FileHandle(); filehandle.set(file); File file2 = new File(datasource + "semantics.trig"); FileHandle filehandle2 = new FileHandle(); filehandle2.set(file2); gmWriter.write("htp://test.sem.graph/trigMerge", filehandle.withMimetype(RDFMimeTypes.TRIG)); gmWriter.merge("htp://test.sem.graph/trigMerge", filehandle2.withMimetype(RDFMimeTypes.TRIG)); FileHandle handle = gmWriter.read("htp://test.sem.graph/trigMerge", new FileHandle()); File readFile = handle.get(); String expectedContent = convertFileToString(readFile); assertTrue( "Did not insert document or inserted empty doc", expectedContent.contains("http://www.example.org/exampleDocument#Monica") && expectedContent.contains("http://purl.org/dc/elements/1.1/publisher")); }
@Test public void testMerge_jsonxml_FileHandle() throws Exception { File file = new File(datasource + "bug25348.json"); FileHandle filehandle = new FileHandle(); filehandle.set(file); File file2 = new File(datasource + "relative5.xml"); FileHandle filehandle2 = new FileHandle(); filehandle2.set(file2); gmWriter.write("htp://test.sem.graph/trigM", filehandle.withMimetype(RDFMimeTypes.RDFJSON)); gmWriter.merge("htp://test.sem.graph/trigM", filehandle2.withMimetype(RDFMimeTypes.TRIPLEXML)); FileHandle handle = gmWriter.read("htp://test.sem.graph/trigM", new FileHandle().withMimetype(RDFMimeTypes.RDFJSON)); File readFile = handle.get(); String expectedContent = convertFileToString(readFile); assertTrue("Did not insert document or inserted empty doc", expectedContent.contains("http://example.com/ns/person#firstName") && expectedContent.contains("Anna's Homepage")); }
@Test public void testWrite_trig_FileHandle() throws Exception { File file = new File(datasource + "semantics.trig"); FileHandle filehandle = new FileHandle(); filehandle.set(file); gmWriter.write("htp://test.sem.graph/trig", filehandle.withMimetype("application/trig")); gmWriter.write("htp://test.sem.graph/trig", filehandle.withMimetype("application/trig")); FileHandle handle = gmWriter.read("htp://test.sem.graph/trig", new FileHandle()); File readFile = handle.get(); String expectedContent = convertFileToString(readFile); assertTrue("Did not insert document or inserted empty doc", expectedContent.contains("http://www.example.org/exampleDocument#Monica")); }
@Test public void testMergeSameDoc_jsonxml_FileHandle() throws Exception { gmWriter.setDefaultMimetype(RDFMimeTypes.RDFJSON); File file = new File(datasource + "bug25348.json"); FileHandle filehandle = new FileHandle(); filehandle.set(file); gmWriter.write("htp://test.sem.graph/trigMSameDoc", filehandle); gmWriter.merge("htp://test.sem.graph/trigMSameDoc", filehandle); FileHandle handle = gmWriter.read("htp://test.sem.graph/trigMSameDoc", new FileHandle()); File readFile = handle.get(); String expectedContent = convertFileToString(readFile); assertTrue( "Did not insert document or inserted empty doc", expectedContent .equals("{\"http://example.com/ns/directory#m\":{\"http://example.com/ns/person#firstName\":[{\"value\":\"Michelle\", \"" + "type\":\"literal\", \"datatype\":\"http://www.w3.org/2001/XMLSchema#string\"}]}}")); }
@Test public void testWriteMergeDelete_Trx() throws Exception { Transaction trx = writerClient.openTransaction(); GraphManager gmWriter = writerClient.newGraphManager(); File file = new File(datasource + "triplexml1.xml"); gmWriter.write("htp://test.sem.graph/mergetrx", new FileHandle(file).withMimetype(RDFMimeTypes.TRIPLEXML), trx); file = new File(datasource + "bug25348.json"); gmWriter.merge("htp://test.sem.graph/mergetrx", new FileHandle(file).withMimetype(RDFMimeTypes.RDFJSON), trx); FileHandle handle = gmWriter.read("htp://test.sem.graph/mergetrx", new FileHandle(), trx); File readFile = handle.get(); String expectedContent = convertFileToString(readFile); try { assertTrue("Did not Merge document or inserted empty doc", expectedContent.contains("Michelle") && expectedContent.contains("Anna's Homepage")); gmWriter.delete("htp://test.sem.graph/mergetrx", trx); trx.commit(); trx = null; StringHandle readContent = gmWriter.read("htp://test.sem.graph/mergetrx", new StringHandle(), trx); assertTrue("Unexpected Content from read, expecting null", readContent == null); } catch (Exception e) { assertTrue("Unexpected Exception Thrown", e.toString().contains("ResourceNotFoundException")); } finally { if (trx != null) trx.commit(); trx = null; } }
File fileReadUpdate = updateHandle.get();
@Test public void testMergeReplace_quads() throws FileNotFoundException, InterruptedException { String uri = "http://test.sem.quads/json-quads"; String ntriple6 = "<http://example.org/s6> <http://example.com/mergeQuadP> <http://example.org/o2> <http://test.sem.quads/json-quads>."; File file = new File(datasource + "bug25348.json"); FileHandle filehandle = new FileHandle(); filehandle.set(file); gmWriter.write(uri, filehandle.withMimetype(RDFMimeTypes.RDFJSON)); gmWriter.mergeGraphs(new StringHandle(ntriple6).withMimetype(RDFMimeTypes.NQUADS)); FileHandle handle = gmWriter.read(uri, new FileHandle()); File readFile = handle.get(); String expectedContent = convertFileToString(readFile); assertTrue("Did not merge Quad", expectedContent.contains("<http://example.com/mergeQuadP")); file = new File(datasource + "relative2.nq"); gmWriter.replaceGraphs(new FileHandle(file).withMimetype(RDFMimeTypes.NQUADS)); uri = "http://originalGraph"; StringHandle readQuads = gmWriter.read(uri, new StringHandle()); assertTrue("Did not Replace Quads", readQuads.toString().contains("#electricVehicle2")); gmWriter.deleteGraphs(); try { StringHandle readContent = gmWriter.read(uri, new StringHandle()); assertTrue("Unexpected content read, expecting Resource not found exception", readContent.get() == null); } catch (Exception e) { assertTrue("Unexpected Exception Thrown", e.toString().contains("ResourceNotFoundException")); } }
sourceNode.equals(readNode)); File file = docMgr.read(docId, new FileHandle()).get(); readNode = mapper.readTree(file); assertTrue("JSON document mismatch with file",
@Test public void testReadWrite() throws IOException { String docId = "/test/testWrite1.txt"; String text = "A simple text document"; TextDocumentManager docMgr = Common.client.newTextDocumentManager(); docMgr.write(docId, new StringHandle().with(text)); assertEquals("Text document write difference",text,docMgr.read(docId, new StringHandle()).get()); BytesHandle bytesHandle = new BytesHandle(); docMgr.read(docId, bytesHandle); assertEquals("Text document mismatch reading bytes", bytesHandle.get().length,text.length()); InputStreamHandle inputStreamHandle = new InputStreamHandle(); docMgr.read(docId, inputStreamHandle); byte[] b = Common.streamToBytes(inputStreamHandle.get()); assertEquals("Text document mismatch reading input stream",new String(b),text); Reader reader = docMgr.read(docId, new ReaderHandle()).get(); String s = Common.readerToString(reader); assertEquals("Text document mismatch with reader",s,text); File file = docMgr.read(docId, new FileHandle()).get(); assertEquals("Text document mismatch with file",text.length(),file.length()); }
FileHandle fh = new FileHandle(); fh = er.get(fh); assertEquals("files size", 2, fh.get().length());
File fileRead = readHandle.get(); String readContent = convertFileToString(fileRead);
File fileRead = readHandle.get(); String readContent = convertFileToString(fileRead);
} else if (er.getType().equals(Type.BINARY)) { FileHandle readHandle1 = new FileHandle(); assertEquals("size of the binary ", er.get(readHandle1).get().length(), sizeOfBinary);
File fileRead = readHandle.get(); String readContent = convertFileToString(fileRead);