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 org.junit.Assert.assertEquals;
021
022import org.apache.commons.io.IOUtils;
023
024import org.fcrepo.kernel.api.Transaction;
025import org.fcrepo.kernel.api.identifiers.FedoraId;
026import org.fcrepo.kernel.api.models.ExternalContent;
027import org.fcrepo.kernel.api.operations.NonRdfSourceOperation;
028import org.fcrepo.kernel.api.operations.NonRdfSourceOperationBuilder;
029import org.junit.Before;
030import org.junit.Test;
031import org.junit.runner.RunWith;
032import org.mockito.Mock;
033import org.mockito.junit.MockitoJUnitRunner;
034
035import java.io.InputStream;
036import java.net.URI;
037import java.util.Collection;
038import java.util.HashSet;
039import java.util.stream.Collectors;
040import java.util.stream.Stream;
041
042/**
043 * @author bseeger
044 */
045@RunWith(MockitoJUnitRunner.Silent.class)
046public class CreateNonRdfSourceOperationBuilderTest {
047
048    private InputStream stream;
049
050    private NonRdfSourceOperationBuilder internalBuilder;
051
052    final FedoraId resourceId = FedoraId.create("info:fedora/test-subject");
053
054    @Mock
055    private Transaction tx;
056
057    @Before
058    public void setUp() throws Exception {
059        stream = IOUtils.toInputStream("This is some test data", "UTF-8");
060        internalBuilder = new CreateNonRdfSourceOperationBuilderImpl(tx, resourceId, stream);
061    }
062
063    @Test
064    public void testContent() throws Exception {
065        final NonRdfSourceOperation op = internalBuilder.build();
066        assertEquals(stream, op.getContentStream());
067    }
068
069    @Test
070    public void testExternal() {
071        final URI uri = URI.create("http://example.org/test/location");
072        final String handling = ExternalContent.PROXY;
073        final NonRdfSourceOperationBuilder builder =
074                new CreateNonRdfSourceOperationBuilderImpl(tx, resourceId, handling, uri);
075        final NonRdfSourceOperation op = builder.build();
076        assertEquals(uri, op.getContentUri());
077        assertEquals(handling, op.getExternalHandling());
078    }
079
080    @Test
081    public void testMimeType() {
082        final String mimeType = "text/plain";
083        final NonRdfSourceOperation op = internalBuilder.mimeType(mimeType).build();
084        assertEquals(mimeType, op.getMimeType());
085    }
086
087    @Test
088    public void testFilename() {
089        final String filename = "someFile.txt";
090        final NonRdfSourceOperation op = internalBuilder.filename(filename).build();
091        assertEquals(filename, op.getFilename());
092    }
093
094    @Test
095    public void testSize() {
096        final long filesize = 123l;
097        final NonRdfSourceOperation op = internalBuilder.contentSize(filesize).build();
098        assertEquals(filesize, op.getContentSize());
099    }
100
101    @Test
102    public void testDigests() {
103        final Collection<URI> digests = Stream.of("urn:sha1:1234abcd", "urn:md5:zyxw9876").map(URI::create)
104                .collect(Collectors.toCollection(HashSet::new));
105        final NonRdfSourceOperation op = internalBuilder.contentDigests(digests).build();
106        assertEquals(digests, op.getContentDigests());
107    }
108}