001/**
002 * Copyright 2015 DuraSpace, Inc.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.fcrepo.client;
017
018import static org.junit.Assert.assertEquals;
019import static java.net.URI.create;
020import static java.nio.charset.StandardCharsets.UTF_8;
021
022import java.net.URI;
023import java.io.ByteArrayInputStream;
024import java.io.InputStream;
025import java.io.IOException;
026
027import org.junit.Test;
028import org.junit.runner.RunWith;
029import org.apache.commons.io.IOUtils;
030import org.mockito.runners.MockitoJUnitRunner;
031
032/**
033 * @author ajs6f
034 */
035@RunWith(MockitoJUnitRunner.class)
036public class FcrepoResponseTest {
037
038    @Test
039    public void testResponse() throws IOException {
040        final URI uri = create("http://localhost/path/a/b");
041        final int status = 200;
042        final String contentType = "text/plain";
043        final URI location = create("http://localhost/path/a/b/c");
044        final String body = "Text response";
045        final InputStream bodyStream = new ByteArrayInputStream(body.getBytes(UTF_8));
046        final FcrepoResponse response = new FcrepoResponse(uri, status, contentType, location, bodyStream);
047
048        assertEquals(response.getUrl(), uri);
049        assertEquals(response.getStatusCode(), status);
050        assertEquals(response.getContentType(), contentType);
051        assertEquals(response.getLocation(), location);
052        assertEquals(IOUtils.toString(response.getBody(), UTF_8), body);
053
054        response.setUrl(create("http://example.org/path/a/b"));
055        assertEquals(response.getUrl(), create("http://example.org/path/a/b"));
056
057        response.setStatusCode(301);
058        assertEquals(response.getStatusCode(), 301);
059
060        response.setContentType("application/n-triples");
061        assertEquals(response.getContentType(), "application/n-triples");
062
063        response.setLocation(create("http://example.org/path/a/b/c"));
064        assertEquals(response.getLocation(), create("http://example.org/path/a/b/c"));
065
066        response.setBody(new ByteArrayInputStream(
067                    "<http://example.org/book/3> <dc:title> \"Title\" .".getBytes(UTF_8)));
068        assertEquals(IOUtils.toString(response.getBody(), UTF_8),
069                    "<http://example.org/book/3> <dc:title> \"Title\" .");
070    }
071
072}