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.integration;
017
018import static java.lang.Integer.MAX_VALUE;
019import static org.apache.http.HttpStatus.SC_CONFLICT;
020import static org.apache.http.HttpStatus.SC_CREATED;
021import static org.apache.http.HttpStatus.SC_NO_CONTENT;
022import static org.apache.http.HttpStatus.SC_OK;
023import static org.fcrepo.kernel.api.RdfLexicon.CONSTRAINED_BY;
024import static org.junit.Assert.assertEquals;
025
026import java.io.IOException;
027import java.net.URI;
028
029import org.apache.http.HttpResponse;
030import org.apache.http.client.HttpClient;
031import org.apache.http.client.methods.HttpGet;
032import org.apache.http.client.methods.HttpPost;
033import org.apache.http.client.methods.HttpPut;
034import org.apache.http.client.methods.HttpUriRequest;
035import org.apache.http.entity.StringEntity;
036import org.apache.http.impl.client.HttpClientBuilder;
037import org.apache.http.util.EntityUtils;
038import org.junit.Before;
039import org.junit.Test;
040import org.slf4j.Logger;
041import org.slf4j.LoggerFactory;
042
043import javax.ws.rs.core.Link;
044
045/**
046 * <p>SanityCheckIT class.</p>
047 *
048 * @author fasseg
049 */
050public class SanityCheckIT {
051
052    /**
053     * The server port of the application, set as system property by
054     * maven-failsafe-plugin.
055     */
056    private static final String SERVER_PORT = System.getProperty("fcrepo.dynamic.test.port");
057
058    /**
059    * The context path of the application (including the leading "/"), set as
060    * system property by maven-failsafe-plugin.
061    */
062    private static final String CONTEXT_PATH = System
063            .getProperty("fcrepo.test.context.path");
064
065    protected Logger logger;
066
067    @Before
068    public void setLogger() {
069        logger = LoggerFactory.getLogger(this.getClass());
070    }
071
072    protected static final String HOSTNAME = "localhost";
073
074    protected static final String serverAddress = "http://" + HOSTNAME + ":" +
075            SERVER_PORT + CONTEXT_PATH;
076
077    protected static HttpClient client;
078
079    static {
080        client =
081                HttpClientBuilder.create().setMaxConnPerRoute(MAX_VALUE)
082                        .setMaxConnTotal(MAX_VALUE).build();
083    }
084
085    @Test
086    public void doASanityCheck() throws IOException {
087        executeAndVerify(new HttpGet(serverAddress + "rest/"), SC_OK);
088    }
089
090    private HttpResponse executeAndVerify(final HttpUriRequest method, final int statusCode) throws IOException {
091        logger.debug("Executing: " + method.getMethod() + " to " + method.getURI());
092        final HttpResponse response = client.execute(method);
093
094        assertEquals(statusCode, response.getStatusLine().getStatusCode());
095        return response;
096    }
097
098    @Test
099    public void testConstraintLink() throws Exception {
100        // Create a resource
101        final HttpPost post = new HttpPost(serverAddress + "rest/");
102        final HttpResponse postResponse = executeAndVerify(post, SC_CREATED);
103
104        final String location = postResponse.getFirstHeader("Location").getValue();
105        logger.debug("new resource location: {}", location);
106
107        // GET the new resource
108        final HttpGet get = new HttpGet(location);
109        final HttpResponse getResponse = executeAndVerify(get, SC_OK);
110
111        final String body = EntityUtils.toString(getResponse.getEntity());
112        logger.debug("new resource body: {}", body);
113
114        // PUT the exact body back to the new resource... successfully
115        final HttpPut put = new HttpPut(location);
116        put.setEntity(new StringEntity(body));
117        put.setHeader("Content-Type", "text/turtle");
118        executeAndVerify(put, SC_NO_CONTENT);
119
120        // Update a server managed property in the resource body... not allowed!
121        final String body2 = body.replaceFirst("fedora:created \"2\\d\\d\\d", "fedora:created \"1999");
122
123        // PUT the erroneous body back to the new resource... unsuccessfully
124        final HttpPut put2 = new HttpPut(location);
125        put2.setEntity(new StringEntity(body2));
126        put2.setHeader("Content-Type", "text/turtle");
127        final HttpResponse put2Response = executeAndVerify(put2, SC_CONFLICT);
128
129        // Verify the returned LINK header
130        final String linkHeader = put2Response.getFirstHeader("Link").getValue();
131        final Link link = Link.valueOf(linkHeader);
132        logger.debug("constraint linkHeader: {}", linkHeader);
133
134        // Verify the LINK rel
135        final String linkRel = link.getRel();
136        assertEquals(CONSTRAINED_BY.getURI(), linkRel);
137
138        // Verify the LINK URI by fetching it
139        final URI linkURI = link.getUri();
140        logger.debug("constraint linkURI: {}", linkURI);
141
142        final HttpGet getLink = new HttpGet(linkURI);
143        executeAndVerify(getLink, SC_OK);
144    }
145
146}