rdf.createQuad(Trellis.PreferServerManaged, identifier, DC.isPartOf, root))); when(mockResource.getBinaryMetadata()) .thenReturn(of(BinaryMetadata.builder(binaryId).mimeType(mimeType).build())); when(mockResource.getMemberOfRelation()).thenReturn(empty()); when(mockResource.getMemberRelation()).thenReturn(empty());
/** * Test an LDP:NonRDFSource. * @throws Exception if the create operation fails */ @Test @DisplayName("Test LDP-NR") default void testLdpNr() throws Exception { final Instant time = now(); final RDF rdf = getInstance(); final IRI identifier = rdf.createIRI(TRELLIS_DATA_PREFIX + getResourceService().generateIdentifier()); final Dataset dataset = buildDataset(identifier, "Create LDP-NR Test", SUBJECT2); final IRI binaryLocation = rdf.createIRI("binary:location/" + getResourceService().generateIdentifier()); final BinaryMetadata binary = BinaryMetadata.builder(binaryLocation).mimeType("text/plain").build(); assertEquals(MISSING_RESOURCE, getResourceService().get(identifier).toCompletableFuture().join(), "Check for no pre-existing LDP-NR"); assertDoesNotThrow(() -> getResourceService().create(Metadata.builder(identifier) .interactionModel(LDP.NonRDFSource).container(ROOT_CONTAINER).binary(binary).build(), dataset) .toCompletableFuture().join(), "Check the creation of an LDP-NR"); final Resource res = getResourceService().get(identifier).toCompletableFuture().join(); assertAll("Check the LDP-NR resource", checkResource(res, identifier, LDP.NonRDFSource, time, dataset)); assertEquals(3L, res.stream(Trellis.PreferUserManaged).count(), "Check the user-managed count of the LDP-NR"); res.getBinaryMetadata().ifPresent(b -> { assertEquals(binaryLocation, b.getIdentifier(), "Check the binary identifier"); assertEquals(of("text/plain"), b.getMimeType(), "Check the binary mimeType"); }); }
/** * Test an LDP:NonRDFSource. * @throws Exception if the create operation fails */ @Test @DisplayName("Test LDP-NR") default void testLdpNr() throws Exception { final Instant time = now(); final RDF rdf = getInstance(); final IRI identifier = rdf.createIRI(TRELLIS_DATA_PREFIX + getResourceService().generateIdentifier()); final Dataset dataset = buildDataset(identifier, "Create LDP-NR Test", SUBJECT2); final IRI binaryLocation = rdf.createIRI("binary:location/" + getResourceService().generateIdentifier()); final BinaryMetadata binary = BinaryMetadata.builder(binaryLocation).mimeType("text/plain").build(); assertEquals(MISSING_RESOURCE, getResourceService().get(identifier).toCompletableFuture().join(), "Check for no pre-existing LDP-NR"); assertDoesNotThrow(() -> getResourceService().create(Metadata.builder(identifier) .interactionModel(LDP.NonRDFSource).container(ROOT_CONTAINER).binary(binary).build(), dataset) .toCompletableFuture().join(), "Check the creation of an LDP-NR"); final Resource res = getResourceService().get(identifier).toCompletableFuture().join(); assertAll("Check the LDP-NR resource", checkResource(res, identifier, LDP.NonRDFSource, time, dataset)); assertEquals(3L, res.stream(Trellis.PreferUserManaged).count(), "Check the user-managed count of the LDP-NR"); res.getBinaryMetadata().ifPresent(b -> { assertEquals(binaryLocation, b.getIdentifier(), "Check the binary identifier"); assertEquals(of("text/plain"), b.getMimeType(), "Check the binary mimeType"); }); }
@Test public void testSetContent() throws Exception { final ByteArrayInputStream inputStream = new ByteArrayInputStream("FooBar".getBytes(UTF_8)); when(mockBinaryService.setContent(any(BinaryMetadata.class), any(InputStream.class))) .thenAnswer(inv -> { readLines((InputStream) inv.getArguments()[1], UTF_8); return completedFuture(null); }); assertDoesNotThrow(mockBinaryService .setContent(BinaryMetadata.builder(identifier).build(), inputStream, MessageDigest.getInstance("MD5")) .thenApply(MessageDigest::digest).thenApply(getEncoder()::encodeToString) .thenAccept(digest -> assertEquals("8yom4qOoqjOM13tuEmPFNQ==", digest)) .toCompletableFuture()::join); } }
@Test public void testMetadataBinary() { final BinaryMetadata binary = BinaryMetadata.builder(rdf.createIRI("http://example.com/binary")).build(); final Metadata metadata = Metadata.builder(identifier) .interactionModel(LDP.NonRDFSource) .container(root).binary(binary).build(); assertEquals(identifier, metadata.getIdentifier()); assertEquals(LDP.NonRDFSource, metadata.getInteractionModel()); assertEquals(of(root), metadata.getContainer()); assertEquals(of(binary), metadata.getBinary()); assertFalse(metadata.getMembershipResource().isPresent()); assertFalse(metadata.getMemberOfRelation().isPresent()); assertFalse(metadata.getInsertedContentRelation().isPresent()); assertFalse(metadata.getMemberRelation().isPresent()); }
@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 testSetFileContentError() throws Exception { final InputStream throwingMockInputStream = mock(InputStream.class, inv -> { throw new IOException("Expected error"); }); final BinaryService service = new FileBinaryService(); final IRI fileIRI = rdf.createIRI("file:///" + randomFilename()); assertAll(() -> service.setContent(BinaryMetadata.builder(fileIRI).build(), throwingMockInputStream) .handle((val, err) -> { assertNotNull(err, "There should have been an error with the input stream!"); return null; }).toCompletableFuture().join()); }
@Test public void testBinaryMetadataWithOptionalArgs() { final BinaryMetadata binary = BinaryMetadata.builder(identifier).build(); assertEquals(identifier, binary.getIdentifier(), "Identifier did not match"); assertFalse(binary.getMimeType().isPresent(), "MimeType was not absent"); assertTrue(binary.getHints().isEmpty(), "Hints are not empty!"); } }
@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!"); }
/** * Get a mutable builder for a {@link BinaryMetadata}. * @param identifier the identifier * @return a builder for a {@link BinaryMetadata} */ public static Builder builder(final IRI identifier) { return new Builder(identifier); }
/** * Get a mutable builder for a {@link BinaryMetadata}. * @param identifier the identifier * @return a builder for a {@link BinaryMetadata} */ public static Builder builder(final IRI identifier) { return new Builder(identifier); }
@Test public void testBinaryMetadata() { final BinaryMetadata binary = BinaryMetadata.builder(identifier).mimeType(mimeType).hints(hints).build(); assertEquals(identifier, binary.getIdentifier(), "Identifier did not match"); assertEquals(of(mimeType), binary.getMimeType(), "MimeType did not match"); assertEquals(hints, binary.getHints(), "hints did not match"); }
@Override public Optional<BinaryMetadata> getBinaryMetadata() { final String binaryLocation = metadata.getUserMetaDataOf(BINARY_LOCATION); final String binaryType = metadata.getUserMetaDataOf(BINARY_TYPE); return ofNullable(binaryLocation).map(rdf::createIRI) .map(loc -> BinaryMetadata.builder(loc).mimeType(binaryType).build()); }