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 org.apache.jena.rdf.model.ModelFactory.createDefaultModel; 021import static org.apache.http.entity.ContentType.parse; 022import static java.net.URI.create; 023import static javax.ws.rs.core.UriBuilder.fromUri; 024import static org.junit.Assert.assertNotNull; 025import static org.apache.jena.riot.RDFLanguages.contentTypeToLang; 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 java.security.Principal; 034 035import javax.jcr.Session; 036import javax.jcr.Workspace; 037import javax.servlet.ServletContext; 038import javax.ws.rs.core.SecurityContext; 039import javax.ws.rs.core.UriBuilder; 040import javax.ws.rs.core.UriInfo; 041 042import org.apache.http.HttpEntity; 043import org.apache.jena.riot.Lang; 044 045import org.fcrepo.http.commons.AbstractResource; 046import org.fcrepo.kernel.api.services.functions.ConfigurableHierarchicalSupplier; 047 048import org.mockito.invocation.InvocationOnMock; 049import org.mockito.stubbing.Answer; 050 051import org.apache.jena.rdf.model.Model; 052 053/** 054 * <p>Abstract TestHelpers class.</p> 055 * 056 * @author awoods 057 */ 058public abstract class TestHelpers { 059 060 public static ServletContext getServletContextImpl() { 061 final ServletContext sc = mock(ServletContext.class); 062 when(sc.getContextPath()).thenReturn("/fcrepo"); 063 return sc; 064 } 065 066 public static UriInfo getUriInfoImpl() { 067 // UriInfo ui = mock(UriInfo.class,withSettings().verboseLogging()); 068 final UriInfo ui = mock(UriInfo.class); 069 070 final Answer<UriBuilder> answer = new Answer<UriBuilder>() { 071 072 @Override 073 public UriBuilder answer(final InvocationOnMock invocation) { 074 return fromUri("http://localhost/fcrepo"); 075 } 076 }; 077 078 when(ui.getRequestUri()).thenReturn( 079 URI.create("http://localhost/fcrepo")); 080 when(ui.getBaseUri()).thenReturn(create("http://localhost/fcrepo")); 081 when(ui.getBaseUriBuilder()).thenAnswer(answer); 082 when(ui.getAbsolutePathBuilder()).thenAnswer(answer); 083 084 return ui; 085 } 086 087 public static Session mockSession(final AbstractResource testObj) { 088 089 final SecurityContext mockSecurityContext = mock(SecurityContext.class); 090 final Principal mockPrincipal = mock(Principal.class); 091 092 final String mockUser = "testuser"; 093 094 final Session mockSession = mock(Session.class); 095 when(mockSession.getUserID()).thenReturn(mockUser); 096 when(mockSecurityContext.getUserPrincipal()).thenReturn(mockPrincipal); 097 when(mockPrincipal.getName()).thenReturn(mockUser); 098 099 final Workspace mockWorkspace = mock(Workspace.class); 100 when(mockSession.getWorkspace()).thenReturn(mockWorkspace); 101 when(mockWorkspace.getName()).thenReturn("default"); 102 103 setField(testObj, "uriInfo", getUriInfoImpl()); 104 setField(testObj, "pidMinter", new ConfigurableHierarchicalSupplier()); 105 return mockSession; 106 107 } 108 109 private static String getRdfSerialization(final HttpEntity entity) { 110 final Lang lang = contentTypeToLang(parse(entity.getContentType().getValue()).getMimeType()); 111 assertNotNull("Entity is not an RDF serialization", lang); 112 return lang.getName(); 113 } 114 115 public static CloseableDataset parseTriples(final HttpEntity entity) throws IOException { 116 return parseTriples(entity.getContent(), getRdfSerialization(entity)); 117 } 118 119 public static CloseableDataset parseTriples(final InputStream content) { 120 return parseTriples(content, "N3"); 121 } 122 123 public static CloseableDataset parseTriples(final InputStream content, final String contentType) { 124 final Model model = createDefaultModel(); 125 model.read(content, "", contentType); 126 return new CloseableDataset(model); 127 } 128 129 /** 130 * Set a field via reflection 131 * 132 * @param parent the owner object of the field 133 * @param name the name of the field 134 * @param obj the value to set 135 */ 136 public static void setField(final Object parent, final String name, final Object obj) { 137 /* check the parent class too if the field could not be found */ 138 try { 139 final Field f = findField(parent.getClass(), name); 140 f.setAccessible(true); 141 f.set(parent, obj); 142 } catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException e) { 143 throw new RuntimeException(e); 144 } 145 } 146 147 private static Field findField(final Class<?> clazz, final String name) 148 throws NoSuchFieldException { 149 for (final Field f : clazz.getDeclaredFields()) { 150 if (f.getName().equals(name)) { 151 return f; 152 } 153 } 154 if (clazz.getSuperclass() == null) { 155 throw new NoSuchFieldException("Field " + name 156 + " could not be found"); 157 } 158 return findField(clazz.getSuperclass(), name); 159 } 160}