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.integration.http.api;
019
020import static org.apache.commons.lang3.StringUtils.contains;
021import static org.junit.Assert.assertEquals;
022import static org.junit.Assert.assertTrue;
023import static javax.ws.rs.core.HttpHeaders.ACCEPT;
024
025import java.io.IOException;
026
027import org.apache.http.client.methods.CloseableHttpResponse;
028import org.apache.http.client.methods.HttpGet;
029import org.apache.http.util.EntityUtils;
030import org.junit.Test;
031import org.springframework.test.context.TestExecutionListeners;
032
033/**
034 * <p>FedoraHtmlIT class.</p>
035 *
036 * @author awoods
037 */
038@TestExecutionListeners(
039        listeners = { TestIsolationExecutionListener.class },
040        mergeMode = TestExecutionListeners.MergeMode.MERGE_WITH_DEFAULTS)
041public class FedoraHtmlIT extends AbstractResourceIT {
042
043    @Test
044    public void testGetRoot() {
045
046        final HttpGet method = new HttpGet(serverAddress);
047        method.addHeader(ACCEPT, "text/html");
048        assertEquals(200, getStatus(method));
049    }
050
051    @Test
052    public void testGetNode() {
053
054        final String pid = getRandomUniqueId();
055        createObject(pid);
056
057        final HttpGet method = new HttpGet(serverAddress + pid);
058        method.addHeader(ACCEPT, "text/html");
059        assertEquals(200, getStatus(method));
060    }
061
062    @Test
063    public void testGetDatastreamNode() throws IOException {
064
065        final String pid = getRandomUniqueId();
066        createObject(pid);
067
068        createDatastream(pid, "ds1", "foo");
069
070        final HttpGet method =
071            new HttpGet(serverAddress + pid + "/ds1");
072
073        method.addHeader(ACCEPT, "text/plain");
074        assertEquals(200, getStatus(method));
075    }
076
077    @Test
078    public void testGetContainerTemplate() throws IOException {
079        final String pid = getRandomUniqueId();
080        createObject(pid);
081
082        final HttpGet method = new HttpGet(serverAddress + pid);
083        method.addHeader(ACCEPT, "text/html");
084        try (final CloseableHttpResponse response = execute(method)) {
085            final String html = EntityUtils.toString(response.getEntity());
086            assertTrue(contains(html, "class=\"fcrepo_resource\""));
087        }
088    }
089
090    @Test
091    public void testGetBinaryTemplate() throws IOException {
092        final String pid = getRandomUniqueId();
093        createObject(pid);
094        createDatastream(pid, "file", "binary content");
095
096        final HttpGet method = new HttpGet(serverAddress + pid + "/file/fcr:metadata");
097        method.addHeader(ACCEPT, "text/html");
098        try (final CloseableHttpResponse response = execute(method)) {
099            final String html = EntityUtils.toString(response.getEntity());
100            assertTrue(contains(html, "class=\"fcrepo_binary\""));
101        }
102    }
103
104    @Test
105    public void testGetRootTemplate() throws IOException {
106
107        final HttpGet method = new HttpGet(serverAddress);
108        method.addHeader(ACCEPT, "text/html");
109        try (final CloseableHttpResponse response = execute(method)) {
110            final String html = EntityUtils.toString(response.getEntity());
111            assertTrue(html, contains(html, "class=\"fcrepo_root\""));
112        }
113    }
114
115}