com.talis.storage.SubmittedItemTest.java Source code

Java tutorial

Introduction

Here is the source code for com.talis.storage.SubmittedItemTest.java

Source

/*
 * Copyright 2010 Talis Information Ltd
 * 
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 * 
 *        http://www.apache.org/licenses/LICENSE-2.0
 * 
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.talis.storage;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.UUID;

import javax.ws.rs.core.MediaType;

import org.apache.commons.io.IOUtils;
import org.apache.commons.io.input.NullInputStream;
import org.junit.Test;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Iterables;
import com.google.common.collect.Multimap;

public class SubmittedItemTest {

    @Test
    public void getEntityReturnsInputStreamFromConstructorUnread() throws IOException {
        byte[] data = UUID.randomUUID().toString().getBytes();
        InputStream entity = new ByteArrayInputStream(data);
        SubmittedItem submittedItem = new SubmittedItem(MediaType.TEXT_PLAIN_TYPE, entity);
        assertSame(entity, submittedItem.getEntity());
        assertTrue(Arrays.equals(data, IOUtils.toByteArray(submittedItem.getEntity())));
    }

    @Test
    public void getMediaTypeReturnsInputStreamFromConstructor() {
        SubmittedItem submittedItem = new SubmittedItem(MediaType.TEXT_XML_TYPE, new NullInputStream(1024));
        assertEquals(MediaType.TEXT_XML_TYPE, submittedItem.getMediaType());
    }

    @Test
    public void metadataIsEmptyAfterIfNotSuppliedInConstructor() {
        SubmittedItem submittedItem = new SubmittedItem(MediaType.TEXT_XML_TYPE, new NullInputStream(1024));
        assertTrue(submittedItem.getMetadata().isEmpty());
    }

    @Test(expected = UnsupportedOperationException.class)
    public void getMetadataReturnsImmutableCopy() {
        SubmittedItem submittedItem = new SubmittedItem(MediaType.TEXT_XML_TYPE, new NullInputStream(1024));
        submittedItem.getMetadata().put("KEY", "VALUE");
    }

    @Test
    public void getMetadataReturnsThatSuppliedInConstructor() {
        Multimap<String, String> meta = ArrayListMultimap.create();
        meta.put("key-0", "value-0");
        meta.put("key-0", "value-1");
        meta.put("key-1", "value-3");

        SubmittedItem submittedItem = new SubmittedItem(MediaType.TEXT_XML_TYPE, new NullInputStream(1024), meta);
        Multimap<String, String> fromItem = submittedItem.getMetadata();
        assertTrue(Iterables.elementsEqual(meta.keySet(), fromItem.keySet()));
        for (String key : meta.keySet()) {
            assertTrue(Iterables.elementsEqual(meta.get(key), fromItem.get(key)));
        }
    }

    @Test
    public void settingMetadataInConstructorCreatesImmutableCopy() {
        Multimap<String, String> meta = ArrayListMultimap.create();
        meta.put("key-0", "value-0");
        meta.put("key-0", "value-1");
        meta.put("key-1", "value-3");

        SubmittedItem submittedItem = new SubmittedItem(MediaType.TEXT_XML_TYPE, new NullInputStream(1024), meta);
        meta.put("key-2", "value-4");

        Multimap<String, String> fromItem = submittedItem.getMetadata();
        assertFalse(submittedItem.getMetadata().containsKey("key-2"));
    }
}