com.talis.storage.StoredItemTest.java Source code

Java tutorial

Introduction

Here is the source code for com.talis.storage.StoredItemTest.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.assertSame;

import java.io.InputStream;
import java.util.Date;
import java.util.UUID;

import javax.ws.rs.core.MediaType;

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

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

public class StoredItemTest {

    private String etag;

    @Before
    public void setup() {
        etag = UUID.randomUUID().toString();
    }

    @Test
    public void getEntityFromEnclosedItem() {
        InputStream entity = new NullInputStream(100);
        SubmittedItem enclosed = new SubmittedItem(MediaType.TEXT_PLAIN_TYPE, entity);

        StoredItem stored = new StoredItem(enclosed, now(), etag);
        InputStream storedEntity = stored.getEntity();
        assertSame(entity, storedEntity);
    }

    @Test
    public void getMediaTypeFromEnclosedItem() {
        StoredItem stored;
        stored = new StoredItem(new SubmittedItem(MediaType.TEXT_PLAIN_TYPE, new NullInputStream(100)), now(),
                etag);
        assertEquals(MediaType.TEXT_PLAIN_TYPE, stored.getMediaType());

        stored = new StoredItem(new SubmittedItem(MediaType.APPLICATION_JSON_TYPE, new NullInputStream(100)), now(),
                etag);
        assertEquals(MediaType.APPLICATION_JSON_TYPE, stored.getMediaType());

        MediaType customMediaType = new MediaType("application", "vnd.test.foo");
        stored = new StoredItem(new SubmittedItem(customMediaType, new NullInputStream(100)), now(), etag);
        assertEquals(customMediaType, stored.getMediaType());
    }

    @Test
    public void getMetadataFromEnclosedItem() {
        String key = UUID.randomUUID().toString();
        Multimap<String, String> meta = ArrayListMultimap.create();
        meta.put(key, UUID.randomUUID().toString());
        StoredItem stored = new StoredItem(
                new SubmittedItem(MediaType.TEXT_PLAIN_TYPE, new NullInputStream(100), meta), now(), etag);

        assertEquals(meta.get(key), stored.getMetadata().get(key));
    }

    private Date now() {
        return new Date(System.currentTimeMillis());
    }
}