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 org.apache.http.NoHttpResponseException;
021import org.apache.http.client.methods.CloseableHttpResponse;
022import org.apache.http.client.methods.HttpGet;
023import org.apache.jena.rdf.model.RDFNode;
024import org.apache.jena.rdf.model.Resource;
025import org.fcrepo.kernel.api.RdfLexicon;
026import org.junit.Before;
027import org.junit.Test;
028import org.junit.runner.RunWith;
029import org.slf4j.Logger;
030import org.springframework.test.annotation.DirtiesContext;
031import org.springframework.test.annotation.DirtiesContext.ClassMode;
032import org.springframework.test.context.TestExecutionListeners;
033import org.springframework.test.context.TestExecutionListeners.MergeMode;
034import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
035import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
036
037import javax.ws.rs.core.Link;
038import java.net.ConnectException;
039
040import static org.apache.http.HttpStatus.SC_OK;
041import static org.fcrepo.kernel.api.RdfLexicon.MEMENTO_TYPE;
042import static org.junit.Assert.assertEquals;
043import static org.slf4j.LoggerFactory.getLogger;
044
045/**
046 * @author dbernstein
047 */
048@RunWith(SpringJUnit4ClassRunner.class)
049@DirtiesContext(classMode = ClassMode.BEFORE_CLASS)
050@TestExecutionListeners(listeners = {
051        DependencyInjectionTestExecutionListener.class,
052        TestIsolationExecutionListener.class,
053        DirtyContextBeforeAndAfterClassTestExecutionListener.class
054}, mergeMode = MergeMode.MERGE_WITH_DEFAULTS)
055public class RepositoryInitializerIT extends AbstractResourceIT {
056
057    private static final Logger LOGGER = getLogger(RepositoryInitializerIT.class);
058
059    static {
060        System.setProperty("fcrepo.autoversioning.enabled", "false");
061    }
062
063    @Before
064    public void init() throws Exception {
065        // Because of the dirtied context, need to wait for fedora to restart before testing
066        int triesRemaining = 50;
067        while (true) {
068            final HttpGet get = new HttpGet(serverAddress);
069            try (final CloseableHttpResponse response = execute(get)) {
070                assertEquals(SC_OK, getStatus(response));
071                break;
072            } catch (final NoHttpResponseException | ConnectException e) {
073                if (triesRemaining-- > 0) {
074                    LOGGER.debug("Waiting for fedora to become available");
075                    Thread.sleep(50);
076                } else {
077                    throw new Exception("Fedora instance did not become available in allowed time");
078                }
079            }
080        }
081        // Now that fedora has started, clear the property so it won't impact other tests
082        System.clearProperty("fcrepo.autoversioning.enabled");
083    }
084
085    @Test
086    public void testRootResourceIsVersioned() throws Exception {
087        final var model = getModel("/fcr:versions");
088        final var statements = model.listStatements((Resource) null, RdfLexicon.CONTAINS, (RDFNode) null).toList();
089        assertEquals("Should be one version contained by time map", 1, statements.size());
090        final var mementURI = statements.get(0).getObject().asResource().getURI();
091
092        assertEquals("The contained link should be a memento", 1,
093                getLinkHeaders(new HttpGet(mementURI)).stream()
094                        .map(x -> Link.valueOf(x))
095                        .filter(x -> x.getRel().equals("type"))
096                        .filter(x -> x.getUri().toString().equals(MEMENTO_TYPE)).count());
097    }
098}