001/*
002 * Licensed to DuraSpace under one or more contributor license agreements.
003 * See the NOTICE file distributed with this work for additional information
004 * regarding copyright ownership.
005 *
006 * DuraSpace licenses this file to you under the Apache License,
007 * Version 2.0 (the "License"); you may not use this file except in
008 * compliance with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.fcrepo.kernel.impl.operations;
019
020import static java.nio.charset.StandardCharsets.UTF_8;
021import static java.util.Arrays.asList;
022import static org.apache.commons.io.IOUtils.toInputStream;
023import static org.fcrepo.kernel.api.models.ExternalContent.PROXY;
024import static org.junit.Assert.assertEquals;
025import static org.junit.Assert.assertNull;
026
027import java.io.InputStream;
028import java.net.URI;
029import java.util.Collection;
030
031import org.apache.commons.io.IOUtils;
032
033import org.fcrepo.kernel.api.Transaction;
034import org.fcrepo.kernel.api.identifiers.FedoraId;
035import org.fcrepo.kernel.api.operations.NonRdfSourceOperation;
036import org.fcrepo.kernel.api.operations.NonRdfSourceOperationBuilder;
037import org.junit.Test;
038import org.mockito.Mock;
039
040/**
041 * @author bbpennel
042 */
043public class UpdateNonRdfSourceOperationBuilderTest {
044
045    private final FedoraId RESOURCE_ID = FedoraId.create("info:fedora/test-subject");
046
047    private final String MIME_TYPE = "text/plain";
048
049    private final String FILENAME = "someFile.txt";
050
051    private final long FILESIZE = 123L;
052
053    private final Collection<URI> DIGESTS = asList(URI.create("urn:sha1:1234abcd"), URI.create("urn:md5:zyxw9876"));
054
055    @Mock
056    private Transaction tx;
057
058    @Test
059    public void buildExternalBinary() {
060        final URI uri = URI.create("http://example.org/test/location");
061
062        final NonRdfSourceOperationBuilder builder =
063                new UpdateNonRdfSourceOperationBuilder(tx, RESOURCE_ID, PROXY, uri);
064        builder.mimeType(MIME_TYPE)
065                .contentDigests(DIGESTS)
066                .contentSize(FILESIZE)
067                .filename(FILENAME);
068
069        final NonRdfSourceOperation op = builder.build();
070        assertEquals(uri, op.getContentUri());
071        assertEquals(PROXY, op.getExternalHandling());
072        assertEquals(MIME_TYPE, op.getMimeType());
073        assertEquals(FILENAME, op.getFilename());
074        assertEquals(FILESIZE, op.getContentSize());
075        assertEquals(DIGESTS, op.getContentDigests());
076        assertNull(op.getContentStream());
077    }
078
079    @Test
080    public void buildInternalBinary() throws Exception {
081        final String contentString = "This is some test data";
082        final InputStream stream = toInputStream(contentString, UTF_8);
083
084        final NonRdfSourceOperationBuilder builder =
085                new UpdateNonRdfSourceOperationBuilder(tx, RESOURCE_ID, stream);
086        builder.mimeType(MIME_TYPE)
087                .contentDigests(DIGESTS)
088                .contentSize(FILESIZE)
089                .filename(FILENAME);
090
091        final NonRdfSourceOperation op = builder.build();
092        assertEquals(contentString, IOUtils.toString(op.getContentStream(), UTF_8));
093        assertEquals(MIME_TYPE, op.getMimeType());
094        assertEquals(FILENAME, op.getFilename());
095        assertEquals(FILESIZE, op.getContentSize());
096        assertEquals(DIGESTS, op.getContentDigests());
097        assertNull(op.getExternalHandling());
098        assertNull(op.getContentUri());
099    }
100}