@Test public void testGetContent() throws IOException { final ByteArrayInputStream inputStream = new ByteArrayInputStream("FooBar".getBytes(UTF_8)); when(mockBinaryService.get(eq(identifier))).thenAnswer(inv -> completedFuture(mockBinary)); when(mockBinary.getContent(anyInt(), anyInt())).thenReturn(inputStream); try (final InputStream content = mockBinaryService.get(identifier) .thenApply(b -> b.getContent(0, 6)).toCompletableFuture().join()) { assertEquals("FooBar", IOUtils.toString(content, UTF_8), "Binary content did not match"); } }
@Test public void testGetBinaryErrorSkip() throws IOException { when(mockBinaryService.get(eq(binaryInternalIdentifier))).thenAnswer(inv -> completedFuture(mockBinary)); when(mockBinary.getContent()).thenReturn(mockInputStream); when(mockInputStream.skip(anyLong())).thenThrow(new IOException()); final Response res = target(BINARY_PATH).request().header(RANGE, "bytes=300-400").get(); assertEquals(SC_INTERNAL_SERVER_ERROR, res.getStatus(), "Unexpected response code!"); }
private void setUpBinaryService() throws Exception { when(mockBinary.getContent(eq(3), eq(10))).thenReturn(new ByteArrayInputStream("e input".getBytes(UTF_8))); when(mockBinary.getContent()).thenReturn(new ByteArrayInputStream("Some input stream".getBytes(UTF_8))); when(mockBinaryService.generateIdentifier()).thenReturn("file:///" + randomUUID()); when(mockBinaryService.supportedAlgorithms()).thenReturn(new HashSet<>(asList("MD5", "SHA-1"))); when(mockDigest.digest()).thenReturn("computed-digest".getBytes(UTF_8)); when(mockBinaryService.calculateDigest(any(IRI.class), any(MessageDigest.class))) .thenReturn(completedFuture(mockDigest)); when(mockBinaryService.get(any(IRI.class))).thenAnswer(inv -> completedFuture(mockBinary)); when(mockBinaryService.purgeContent(any(IRI.class))).thenReturn(completedFuture(null)); when(mockBinaryService.setContent(any(BinaryMetadata.class), any(InputStream.class), any())) .thenAnswer(inv -> { readLines((InputStream) inv.getArguments()[1], UTF_8); return completedFuture(null); }); when(mockBinaryService.setContent(any(BinaryMetadata.class), any(InputStream.class))) .thenAnswer(inv -> { readLines((InputStream) inv.getArguments()[1], UTF_8); return completedFuture(null); }); doCallRealMethod().when(mockBinaryService) .setContent(any(BinaryMetadata.class), any(InputStream.class), any()); }
private void setUpBinaryService() throws Exception { when(mockBinaryService.supportedAlgorithms()).thenReturn(new HashSet<>(asList("MD5", "SHA-1", "SHA"))); when(mockDigest.digest()).thenReturn(getDecoder().decode("Q29tcHV0ZWREaWdlc3Q=")); when(mockBinaryService.calculateDigest(eq(binaryInternalIdentifier), any(MessageDigest.class))) .thenReturn(completedFuture(mockDigest)); when(mockBinaryService.get(eq(binaryInternalIdentifier))).thenAnswer(inv -> completedFuture(mockBinary)); when(mockBinary.getContent(eq(3), eq(10))).thenReturn(new ByteArrayInputStream("e input".getBytes(UTF_8))); when(mockBinary.getContent()).thenReturn(new ByteArrayInputStream("Some input stream".getBytes(UTF_8))); when(mockBinaryService.setContent(any(BinaryMetadata.class), any(InputStream.class), any())) .thenAnswer(inv -> { readLines((InputStream) inv.getArguments()[1], UTF_8); return completedFuture(null); }); when(mockBinaryService.setContent(any(BinaryMetadata.class), any(InputStream.class))) .thenAnswer(inv -> { readLines((InputStream) inv.getArguments()[1], UTF_8); return completedFuture(null); }); when(mockBinaryService.purgeContent(any(IRI.class))).thenReturn(completedFuture(null)); when(mockBinaryService.generateIdentifier()).thenReturn(RANDOM_VALUE); doCallRealMethod().when(mockBinaryService) .setContent(any(BinaryMetadata.class), any(InputStream.class), any()); }
private CompletionStage<InputStream> getBinaryStream(final IRI dsid, final TrellisRequest req) { if (isNull(req.getRange())) { return getServices().getBinaryService().get(dsid).thenApply(Binary::getContent); } return getServices().getBinaryService().get(dsid) .thenApply(b -> b.getContent(req.getRange().getFrom(), req.getRange().getTo())); }
@Test public void testGetFileContentError() throws IOException { final BinaryService service = new FileBinaryService(); final IRI fileIRI = rdf.createIRI("file:///" + randomFilename()); assertThrows(CompletionException.class, () -> service.get(fileIRI).thenApply(Binary::getContent) .toCompletableFuture().join(), "Fetching from invalid file should have thrown an exception!"); assertThrows(CompletionException.class, () -> service.get(fileIRI).thenApply(b -> b.getContent(0, 4)) .toCompletableFuture().join(), "Fetching binary segment from invalid file should have thrown an exception!"); }
@Test public void testFileContentSegment() { final BinaryService service = new FileBinaryService(); assertEquals(" tes", service.get(file).thenApply(b -> b.getContent(1, 5)).thenApply(this::uncheckedToString) .toCompletableFuture().join(), "Incorrect segment when fetching from a file!"); assertEquals("oc", service.get(file).thenApply(b -> b.getContent(8, 10)).thenApply(this::uncheckedToString) .toCompletableFuture().join(), "Incorrect segment when fetching from a file!"); }
@Test public void testFileContent() { final BinaryService service = new FileBinaryService(); assertEquals("A test document.\n", service.get(file).thenApply(Binary::getContent).thenApply(this::uncheckedToString) .toCompletableFuture().join(), "Incorrect content when fetching from a file!"); }
@Test public void testFileContentSegmentBeyond() { final BinaryService service = new FileBinaryService(); assertEquals("", service.get(file).thenApply(b -> b.getContent(1000, 1005)).thenApply(this::uncheckedToString) .toCompletableFuture().join(), "Incorrect out-of-range segment when fetching from a file!"); }
@Test public void testFilePurge() { final BinaryService service = new FileBinaryService(); final IRI fileIRI = rdf.createIRI("file:///" + randomFilename()); final InputStream inputStream = new ByteArrayInputStream("Some data".getBytes(UTF_8)); assertNull(service.setContent(BinaryMetadata.builder(fileIRI).build(), inputStream) .toCompletableFuture().join(), "setContent didn't complete cleanly!"); assertEquals("Some data", uncheckedToString(service.get(fileIRI).thenApply(Binary::getContent) .toCompletableFuture().join()), "incorrect value for getContent!"); assertNull(service.purgeContent(fileIRI).toCompletableFuture().join(), "purgeContent didn't complete cleanly!"); assertNull(service.purgeContent(fileIRI).toCompletableFuture().join(), "purgeContent (2) didn't complete cleanly!"); }
@Test public void testSetFileContent() { final String contents = "A new file"; final BinaryService service = new FileBinaryService(); final IRI fileIRI = rdf.createIRI("file:///" + randomFilename()); final InputStream inputStream = new ByteArrayInputStream(contents.getBytes(UTF_8)); assertNull(service.setContent(BinaryMetadata.builder(fileIRI).build(), inputStream) .toCompletableFuture().join(), "Setting content didn't complete cleanly!"); assertEquals(contents, service.get(fileIRI).thenApply(Binary::getContent).thenApply(this::uncheckedToString) .toCompletableFuture().join(), "Fetching new content returned incorrect value!"); }
@Test public void testBadIdentifier() { final BinaryService service = new FileBinaryService(); assertFalse(service.get(rdf.createIRI("http://example.com/")).thenApply(Binary::getContent) .handle(this::checkError).toCompletableFuture().join(), "Shouldn't be able to fetch content from a bad IRI!"); }
@Test public void testGetFileSkipContentError() throws Exception { final BinaryService service = new FileBinaryService(); final IRI fileIRI = rdf.createIRI("file:///" + randomFilename()); assertAll(() -> service.get(fileIRI).thenApply(binary -> binary.getContent(10, 20)).handle((val, err) -> { assertNotNull(err, "There should have been an error with the input stream!"); return null; }).toCompletableFuture().join()); }