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