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.http.commons.test.util;
019
020import static java.net.URI.create;
021import static javax.ws.rs.core.UriBuilder.fromUri;
022import static org.apache.http.entity.ContentType.parse;
023import static org.apache.jena.rdf.model.ModelFactory.createDefaultModel;
024import static org.apache.jena.riot.RDFLanguages.contentTypeToLang;
025import static org.junit.Assert.assertNotNull;
026import static org.mockito.Mockito.mock;
027import static org.mockito.Mockito.when;
028
029import java.io.IOException;
030import java.io.InputStream;
031import java.lang.reflect.Field;
032import java.net.URI;
033import javax.servlet.ServletContext;
034import javax.ws.rs.core.UriBuilder;
035import javax.ws.rs.core.UriInfo;
036
037import org.apache.http.HttpEntity;
038import org.apache.jena.rdf.model.Model;
039import org.apache.jena.riot.Lang;
040import org.mockito.invocation.InvocationOnMock;
041import org.mockito.stubbing.Answer;
042
043/**
044 * <p>Abstract TestHelpers class.</p>
045 *
046 * @author awoods
047 */
048public abstract class TestHelpers {
049
050    public static ServletContext getServletContextImpl() {
051        final ServletContext sc = mock(ServletContext.class);
052        when(sc.getContextPath()).thenReturn("/fcrepo");
053        return sc;
054    }
055
056    public static UriInfo getUriInfoImpl() {
057        // UriInfo ui = mock(UriInfo.class,withSettings().verboseLogging());
058        final UriInfo ui = mock(UriInfo.class);
059
060        final Answer<UriBuilder> answer = new Answer<UriBuilder>() {
061
062            @Override
063            public UriBuilder answer(final InvocationOnMock invocation) {
064                return fromUri("http://localhost/fcrepo");
065            }
066        };
067
068        when(ui.getRequestUri()).thenReturn(
069                URI.create("http://localhost/fcrepo"));
070        when(ui.getBaseUri()).thenReturn(create("http://localhost/fcrepo"));
071        when(ui.getBaseUriBuilder()).thenAnswer(answer);
072        when(ui.getAbsolutePathBuilder()).thenAnswer(answer);
073
074        return ui;
075    }
076
077
078
079    private static String getRdfSerialization(final HttpEntity entity) {
080        final Lang lang = contentTypeToLang(parse(entity.getContentType().getValue()).getMimeType());
081        assertNotNull("Entity is not an RDF serialization", lang);
082        return lang.getName();
083    }
084
085    public static CloseableDataset parseTriples(final HttpEntity entity) throws IOException {
086        return parseTriples(entity.getContent(), getRdfSerialization(entity));
087    }
088
089    public static CloseableDataset parseTriples(final InputStream content) {
090        return parseTriples(content, "N3");
091    }
092
093    public static CloseableDataset parseTriples(final InputStream content, final String contentType) {
094        final Model model = createDefaultModel();
095        model.read(content, "", contentType);
096        return new CloseableDataset(model);
097    }
098
099    /**
100     * Set a field via reflection
101     *
102     * @param parent the owner object of the field
103     * @param name the name of the field
104     * @param obj the value to set
105     */
106    public static void setField(final Object parent, final String name, final Object obj) {
107        /* check the parent class too if the field could not be found */
108        try {
109            final Field f = findField(parent.getClass(), name);
110            f.setAccessible(true);
111            f.set(parent, obj);
112        } catch (final IllegalArgumentException | IllegalAccessException | NoSuchFieldException e) {
113            throw new RuntimeException(e);
114        }
115    }
116
117    private static Field findField(final Class<?> clazz, final String name)
118        throws NoSuchFieldException {
119        for (final Field f : clazz.getDeclaredFields()) {
120            if (f.getName().equals(name)) {
121                return f;
122            }
123        }
124        if (clazz.getSuperclass() == null) {
125            throw new NoSuchFieldException("Field " + name
126                    + " could not be found");
127        }
128        return findField(clazz.getSuperclass(), name);
129    }
130}