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.fcrepo.kernel.api.FedoraTypes.FCR_TOMBSTONE;
021import static org.slf4j.LoggerFactory.getLogger;
022
023import static javax.ws.rs.core.HttpHeaders.LINK;
024import static javax.ws.rs.core.Response.Status.CREATED;
025import static javax.ws.rs.core.Response.Status.GONE;
026import static javax.ws.rs.core.Response.Status.NOT_FOUND;
027import static javax.ws.rs.core.Response.Status.NO_CONTENT;
028import static javax.ws.rs.core.Response.Status.OK;
029
030import static org.junit.Assert.assertEquals;
031
032import org.apache.http.client.methods.CloseableHttpResponse;
033import org.apache.http.client.methods.HttpDelete;
034import org.apache.http.client.methods.HttpGet;
035import org.apache.http.client.methods.HttpPost;
036import org.apache.http.client.methods.HttpPut;
037import org.junit.Ignore;
038import org.junit.Test;
039import org.slf4j.Logger;
040import org.springframework.test.context.TestExecutionListeners;
041
042/**
043 * Test of OCFL specific issues from the HTTP layer.
044 *
045 * @author whikloj
046 */
047@TestExecutionListeners(
048        listeners = { TestIsolationExecutionListener.class },
049        mergeMode = TestExecutionListeners.MergeMode.MERGE_WITH_DEFAULTS)
050public class OcflPersistenceIT extends AbstractResourceIT {
051
052    private static final Logger LOGGER = getLogger(OcflPersistenceIT.class);
053
054    @Test
055    public void testDeleteAgChild() throws Exception {
056        final String id = getRandomUniqueId();
057        final HttpPost postAg = postObjMethod();
058        postAg.setHeader("Link", "<http://fedora.info/definitions/v4/repository#ArchivalGroup>;rel=\"type\"");
059        postAg.setHeader("Slug", id);
060        assertEquals(CREATED.getStatusCode(), getStatus(postAg));
061
062        assertEquals(OK.getStatusCode(), getStatus(getObjMethod(id)));
063
064        final String childId = getRandomUniqueId();
065        final HttpPost postChild = postObjMethod(id);
066        postAg.setHeader("Slug", childId);
067        final String childLocation;
068        try (final CloseableHttpResponse response = execute(postChild)) {
069            assertEquals(CREATED.getStatusCode(), getStatus(response));
070            childLocation = getLocation(response);
071        }
072
073        final HttpGet getChild = new HttpGet(childLocation);
074        assertEquals(OK.getStatusCode(), getStatus(getChild));
075
076        final HttpDelete deleteChild = new HttpDelete(childLocation);
077        assertEquals(NO_CONTENT.getStatusCode(), getStatus(deleteChild));
078
079        final HttpGet getChildAgain = new HttpGet(childLocation);
080        assertEquals(GONE.getStatusCode(), getStatus(getChildAgain));
081    }
082
083    @Test
084    public void testCreateDeletePurgeCreateWholeObjectRdf() throws Exception {
085        final HttpPost httpPost = postObjMethod();
086        final String id;
087        try (final CloseableHttpResponse response = execute(httpPost)) {
088            assertEquals(CREATED.getStatusCode(), getStatus(response));
089            id = getLocation(response);
090        }
091        assertEquals(OK.getStatusCode(), getStatus(new HttpGet(id)));
092
093        final HttpDelete deleteObj = new HttpDelete(id);
094        assertEquals(NO_CONTENT.getStatusCode(), getStatus(deleteObj));
095
096        final HttpDelete deleteTomb = new HttpDelete(id + "/" + FCR_TOMBSTONE);
097        assertEquals(NO_CONTENT.getStatusCode(), getStatus(deleteTomb));
098
099        final HttpGet getObj = new HttpGet(id);
100        assertEquals(NOT_FOUND.getStatusCode(), getStatus(getObj));
101
102        final HttpPut putObj = new HttpPut(id);
103        assertEquals(CREATED.getStatusCode(), getStatus(putObj));
104    }
105
106    @Test
107    public void testCreateDeletePurgeCreateWholeObjectBinary() throws Exception {
108        final HttpPost httpPost = postObjMethod();
109        httpPost.setHeader(LINK, NON_RDF_SOURCE_LINK_HEADER);
110        final String id;
111        try (final CloseableHttpResponse response = execute(httpPost)) {
112            assertEquals(CREATED.getStatusCode(), getStatus(response));
113            id = getLocation(response);
114        }
115        assertEquals(OK.getStatusCode(), getStatus(new HttpGet(id)));
116
117        final HttpDelete deleteObj = new HttpDelete(id);
118        assertEquals(NO_CONTENT.getStatusCode(), getStatus(deleteObj));
119
120        final HttpDelete deleteTomb = new HttpDelete(id + "/" + FCR_TOMBSTONE);
121        assertEquals(NO_CONTENT.getStatusCode(), getStatus(deleteTomb));
122
123        final HttpGet getObj = new HttpGet(id);
124        assertEquals(NOT_FOUND.getStatusCode(), getStatus(getObj));
125
126        final HttpPut putObj = new HttpPut(id);
127        assertEquals(CREATED.getStatusCode(), getStatus(putObj));
128    }
129
130    @Test
131    public void testCreateDeletePurgeCreateSubpath() throws Exception {
132        final HttpPost httpPost = postObjMethod();
133        httpPost.setHeader("Link", "<http://fedora.info/definitions/v4/repository#ArchivalGroup>;rel=\"type\"");
134        final String parent;
135        try (final CloseableHttpResponse response = execute(httpPost)) {
136            assertEquals(CREATED.getStatusCode(), getStatus(response));
137            parent = getLocation(response);
138        }
139        assertEquals(OK.getStatusCode(), getStatus(new HttpGet(parent)));
140
141        final HttpPost postChild = new HttpPost(parent);
142        final String id;
143        try (final CloseableHttpResponse response = execute(postChild)) {
144            assertEquals(CREATED.getStatusCode(), getStatus(response));
145            id = getLocation(response);
146        }
147        assertEquals(OK.getStatusCode(), getStatus(new HttpGet(id)));
148
149        final HttpDelete deleteObj = new HttpDelete(id);
150        assertEquals(NO_CONTENT.getStatusCode(), getStatus(deleteObj));
151
152        final HttpDelete deleteTomb = new HttpDelete(id + "/" + FCR_TOMBSTONE);
153        assertEquals(NO_CONTENT.getStatusCode(), getStatus(deleteTomb));
154
155        final HttpGet getObj = new HttpGet(id);
156        assertEquals(NOT_FOUND.getStatusCode(), getStatus(getObj));
157
158        final HttpPut putObj = new HttpPut(id);
159        assertEquals(CREATED.getStatusCode(), getStatus(putObj));
160    }
161
162
163    /*
164     * Tests upsert mapping in fedoraToOcflIndex
165     */
166    @Ignore("Needs upsert in containment index - https://jira.lyrasis.org/browse/FCREPO-3369")
167    @Test
168    public void testUpsertMappingOcflIndex() throws Exception {
169        // Create a container.
170        final HttpPost post = postObjMethod();
171        final String id;
172        try (final CloseableHttpResponse response = execute(post)) {
173            assertEquals(CREATED.getStatusCode(), getStatus(response));
174            id = getLocation(response);
175        }
176        assertEquals(OK.getStatusCode(), getStatus(new HttpGet(id)));
177
178        final String txLocation = createTransaction();
179        // Inside a transaction delete the container.
180        final HttpDelete delete = new HttpDelete(id);
181        addTxTo(delete, txLocation);
182        assertEquals(NO_CONTENT.getStatusCode(), getStatus(delete));
183        // Ensure the container is removed in the transaction.
184        final HttpGet getInside = new HttpGet(id);
185        addTxTo(getInside, txLocation);
186        assertEquals(GONE.getStatusCode(), getStatus(getInside));
187        // Inside a transaction delete the tombstone.
188        final HttpDelete deleteTomb = new HttpDelete(id + "/" + FCR_TOMBSTONE);
189        addTxTo(deleteTomb, txLocation);
190        assertEquals(NO_CONTENT.getStatusCode(), getStatus(deleteTomb));
191        // Ensure the container is totally removed in the transaction.
192        final HttpGet getInside2 = new HttpGet(id);
193        addTxTo(getInside2, txLocation);
194        assertEquals(NOT_FOUND.getStatusCode(), getStatus(getInside2));
195        // Inside the transaction put back the container.
196        final HttpPut putBack = new HttpPut(id);
197        addTxTo(putBack, txLocation);
198        assertEquals(CREATED.getStatusCode(), getStatus(putBack));
199        // Commit the transaction.
200        final HttpPut commitTx = new HttpPut(txLocation);
201        assertEquals(NO_CONTENT.getStatusCode(), getStatus(commitTx));
202        // Verify you can still get the container.
203        assertEquals(OK.getStatusCode(), getStatus(new HttpGet(id)));
204
205    }
206}