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