001/*
002 * The contents of this file are subject to the license and copyright
003 * detailed in the LICENSE and NOTICE files at the root of the source
004 * tree.
005 */
006package org.fcrepo.kernel.impl.operations;
007
008import static java.nio.charset.StandardCharsets.UTF_8;
009import static java.util.Arrays.asList;
010import static org.apache.commons.io.IOUtils.toInputStream;
011import static org.fcrepo.kernel.api.models.ExternalContent.PROXY;
012import static org.junit.Assert.assertEquals;
013import static org.junit.Assert.assertNull;
014
015import java.io.InputStream;
016import java.net.URI;
017import java.util.Collection;
018
019import org.apache.commons.io.IOUtils;
020
021import org.fcrepo.kernel.api.Transaction;
022import org.fcrepo.kernel.api.identifiers.FedoraId;
023import org.fcrepo.kernel.api.operations.NonRdfSourceOperation;
024import org.fcrepo.kernel.api.operations.NonRdfSourceOperationBuilder;
025import org.junit.Test;
026import org.mockito.Mock;
027
028/**
029 * @author bbpennel
030 */
031public class UpdateNonRdfSourceOperationBuilderTest {
032
033    private final FedoraId RESOURCE_ID = FedoraId.create("info:fedora/test-subject");
034
035    private final String MIME_TYPE = "text/plain";
036
037    private final String FILENAME = "someFile.txt";
038
039    private final long FILESIZE = 123L;
040
041    private final Collection<URI> DIGESTS = asList(URI.create("urn:sha1:1234abcd"), URI.create("urn:md5:zyxw9876"));
042
043    @Mock
044    private Transaction tx;
045
046    @Test
047    public void buildExternalBinary() {
048        final URI uri = URI.create("http://example.org/test/location");
049
050        final NonRdfSourceOperationBuilder builder =
051                new UpdateNonRdfSourceOperationBuilder(tx, RESOURCE_ID, PROXY, uri);
052        builder.mimeType(MIME_TYPE)
053                .contentDigests(DIGESTS)
054                .contentSize(FILESIZE)
055                .filename(FILENAME);
056
057        final NonRdfSourceOperation op = builder.build();
058        assertEquals(uri, op.getContentUri());
059        assertEquals(PROXY, op.getExternalHandling());
060        assertEquals(MIME_TYPE, op.getMimeType());
061        assertEquals(FILENAME, op.getFilename());
062        assertEquals(FILESIZE, op.getContentSize());
063        assertEquals(DIGESTS, op.getContentDigests());
064        assertNull(op.getContentStream());
065    }
066
067    @Test
068    public void buildInternalBinary() throws Exception {
069        final String contentString = "This is some test data";
070        final InputStream stream = toInputStream(contentString, UTF_8);
071
072        final NonRdfSourceOperationBuilder builder =
073                new UpdateNonRdfSourceOperationBuilder(tx, RESOURCE_ID, stream);
074        builder.mimeType(MIME_TYPE)
075                .contentDigests(DIGESTS)
076                .contentSize(FILESIZE)
077                .filename(FILENAME);
078
079        final NonRdfSourceOperation op = builder.build();
080        assertEquals(contentString, IOUtils.toString(op.getContentStream(), UTF_8));
081        assertEquals(MIME_TYPE, op.getMimeType());
082        assertEquals(FILENAME, op.getFilename());
083        assertEquals(FILESIZE, op.getContentSize());
084        assertEquals(DIGESTS, op.getContentDigests());
085        assertNull(op.getExternalHandling());
086        assertNull(op.getContentUri());
087    }
088}