diff --git a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/dynapi/RESTEndpoint.java b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/dynapi/RESTEndpoint.java index 283373fe52..a9791a053a 100644 --- a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/dynapi/RESTEndpoint.java +++ b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/dynapi/RESTEndpoint.java @@ -1,6 +1,6 @@ package edu.cornell.mannlib.vitro.webapp.dynapi; -import static edu.cornell.mannlib.vitro.webapp.dynapi.request.RequestPath.REST_BASE_PATH; +import static edu.cornell.mannlib.vitro.webapp.dynapi.request.RequestPath.REST_SERVLET_PATH; import static java.lang.String.format; import java.io.IOException; @@ -22,7 +22,7 @@ import edu.cornell.mannlib.vitro.webapp.dynapi.components.ResourceKey; import edu.cornell.mannlib.vitro.webapp.dynapi.request.RequestPath; -@WebServlet(name = "RESTEndpoint", urlPatterns = { REST_BASE_PATH + "/*" }) +@WebServlet(name = "RESTEndpoint", urlPatterns = { REST_SERVLET_PATH + "/*" }) public class RESTEndpoint extends VitroHttpServlet { private static final Log log = LogFactory.getLog(RESTEndpoint.class); diff --git a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/dynapi/RPCEndpoint.java b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/dynapi/RPCEndpoint.java index f3ca8b7f31..da64746e48 100644 --- a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/dynapi/RPCEndpoint.java +++ b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/dynapi/RPCEndpoint.java @@ -1,6 +1,6 @@ package edu.cornell.mannlib.vitro.webapp.dynapi; -import static edu.cornell.mannlib.vitro.webapp.dynapi.request.RequestPath.RPC_BASE_PATH; +import static edu.cornell.mannlib.vitro.webapp.dynapi.request.RequestPath.RPC_SERVLET_PATH; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServletRequest; @@ -14,7 +14,7 @@ import edu.cornell.mannlib.vitro.webapp.dynapi.components.OperationResult; import edu.cornell.mannlib.vitro.webapp.dynapi.request.RequestPath; -@WebServlet(name = "RPCEndpoint", urlPatterns = { RPC_BASE_PATH + "/*" }) +@WebServlet(name = "RPCEndpoint", urlPatterns = { RPC_SERVLET_PATH + "/*" }) public class RPCEndpoint extends VitroHttpServlet { private static final Log log = LogFactory.getLog(RESTEndpoint.class); diff --git a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/dynapi/request/RequestPath.java b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/dynapi/request/RequestPath.java index a8b4ef7af8..47ce879b12 100644 --- a/api/src/main/java/edu/cornell/mannlib/vitro/webapp/dynapi/request/RequestPath.java +++ b/api/src/main/java/edu/cornell/mannlib/vitro/webapp/dynapi/request/RequestPath.java @@ -11,9 +11,9 @@ public class RequestPath { public static final String RESOURCE_ID_PARAM = "RESOURCE_ID"; - public static final String API_BASE_PATH = "/api"; - public static final String RPC_BASE_PATH = API_BASE_PATH + "/rpc"; - public static final String REST_BASE_PATH = API_BASE_PATH + "/rest"; + public static final String API_SERVLET_PATH = "/api"; + public static final String RPC_SERVLET_PATH = API_SERVLET_PATH + "/rpc"; + public static final String REST_SERVLET_PATH = API_SERVLET_PATH + "/rest"; private final RequestType type; @@ -28,8 +28,8 @@ public class RequestPath { private final String actionName; private RequestPath(HttpServletRequest request) { - String contextPath = request != null && request.getContextPath() != null - ? request.getContextPath() + String servletPath = request != null && request.getServletPath() != null + ? request.getServletPath() : EMPTY; String pathInfo = request != null && request.getPathInfo() != null ? request.getPathInfo() @@ -37,13 +37,13 @@ private RequestPath(HttpServletRequest request) { pathParts = pathInfo.split("/"); - if (contextPath.toLowerCase().contains(RPC_BASE_PATH)) { + if (servletPath.toLowerCase().contains(RPC_SERVLET_PATH)) { type = RequestType.RPC; actionName = pathParts.length > 1 ? pathParts[1] : null; resourceVersion = null; resourceName = null; resourceId = null; - } else if (contextPath.toLowerCase().contains(REST_BASE_PATH)) { + } else if (servletPath.toLowerCase().contains(REST_SERVLET_PATH)) { type = RequestType.REST; resourceVersion = pathParts.length > 1 ? pathParts[1] : null; resourceName = pathParts.length > 2 ? pathParts[2] : null; diff --git a/api/src/test/java/edu/cornell/mannlib/vitro/webapp/dynapi/RESTEndpointTest.java b/api/src/test/java/edu/cornell/mannlib/vitro/webapp/dynapi/RESTEndpointTest.java index 9ac562131b..5ac729ec9e 100644 --- a/api/src/test/java/edu/cornell/mannlib/vitro/webapp/dynapi/RESTEndpointTest.java +++ b/api/src/test/java/edu/cornell/mannlib/vitro/webapp/dynapi/RESTEndpointTest.java @@ -1,6 +1,6 @@ package edu.cornell.mannlib.vitro.webapp.dynapi; -import static edu.cornell.mannlib.vitro.webapp.dynapi.request.RequestPath.REST_BASE_PATH; +import static edu.cornell.mannlib.vitro.webapp.dynapi.request.RequestPath.REST_SERVLET_PATH; import static org.apache.commons.lang3.StringUtils.EMPTY; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.doNothing; @@ -40,7 +40,6 @@ public class RESTEndpointTest { private final static String PATH_INFO = "/1/test"; - private final static String CONTEXT_PATH = REST_BASE_PATH + PATH_INFO; private Map params = new HashMap<>(); @@ -173,7 +172,7 @@ private void run(String method) { private void prepareMocks(String method, String actionName) { when(request.getMethod()).thenReturn(method); - when(request.getContextPath()).thenReturn(CONTEXT_PATH); + when(request.getServletPath()).thenReturn(REST_SERVLET_PATH); when(request.getPathInfo()).thenReturn(PATH_INFO); when(action.run(any(OperationData.class))) @@ -206,7 +205,7 @@ private void prepareMocksRPCNotImplemented(String method, String actionName) { private void prepareMocksCustomAction(String method, String actionName) { when(request.getMethod()).thenReturn(method); - when(request.getContextPath()).thenReturn(CONTEXT_PATH + "/" + actionName); + when(request.getServletPath()).thenReturn(REST_SERVLET_PATH); when(request.getPathInfo()).thenReturn(PATH_INFO + "/" + actionName); when(action.run(any(OperationData.class))) diff --git a/api/src/test/java/edu/cornell/mannlib/vitro/webapp/dynapi/RPCEndpointITest.java b/api/src/test/java/edu/cornell/mannlib/vitro/webapp/dynapi/RPCEndpointITest.java index 2f7ce3913b..b24baed0c2 100644 --- a/api/src/test/java/edu/cornell/mannlib/vitro/webapp/dynapi/RPCEndpointITest.java +++ b/api/src/test/java/edu/cornell/mannlib/vitro/webapp/dynapi/RPCEndpointITest.java @@ -1,5 +1,6 @@ package edu.cornell.mannlib.vitro.webapp.dynapi; +import static edu.cornell.mannlib.vitro.webapp.dynapi.request.RequestPath.RPC_SERVLET_PATH; import static javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR; import static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND; import static javax.servlet.http.HttpServletResponse.SC_NOT_IMPLEMENTED; @@ -36,8 +37,7 @@ @RunWith(Parameterized.class) public class RPCEndpointITest extends ServletContextITest { - private final static String URI_CONTEXT = "/api/rpc/"; - private final static String URI_BASE = "http://localhost" + URI_CONTEXT; + private final static String URI_BASE = "http://localhost:8080" + RPC_SERVLET_PATH; private RPCEndpoint rpcEndpoint; @@ -92,13 +92,13 @@ public void beforeEach() throws Exception { loadDefaultModel(); when(request.getServletContext()).thenReturn(servletContext); - when(request.getContextPath()).thenReturn(URI_CONTEXT); + when(request.getServletPath()).thenReturn(RPC_SERVLET_PATH); actionPool.init(request.getServletContext()); actionPool.reload(); if (testAction != null) { - StringBuffer buffer = new StringBuffer(URI_BASE + testAction); + StringBuffer buffer = new StringBuffer(URI_BASE + "/" + testAction); when(request.getRequestURL()).thenReturn(buffer); when(request.getPathInfo()).thenReturn("/" + testAction); } diff --git a/api/src/test/java/edu/cornell/mannlib/vitro/webapp/dynapi/RPCEndpointTest.java b/api/src/test/java/edu/cornell/mannlib/vitro/webapp/dynapi/RPCEndpointTest.java index 90346485fd..b1ab7983de 100644 --- a/api/src/test/java/edu/cornell/mannlib/vitro/webapp/dynapi/RPCEndpointTest.java +++ b/api/src/test/java/edu/cornell/mannlib/vitro/webapp/dynapi/RPCEndpointTest.java @@ -1,6 +1,6 @@ package edu.cornell.mannlib.vitro.webapp.dynapi; -import static edu.cornell.mannlib.vitro.webapp.dynapi.request.RequestPath.RPC_BASE_PATH; +import static edu.cornell.mannlib.vitro.webapp.dynapi.request.RequestPath.RPC_SERVLET_PATH; import static org.apache.commons.lang3.StringUtils.EMPTY; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mockStatic; @@ -30,7 +30,6 @@ public class RPCEndpointTest { private final static String PATH_INFO = "/test"; - private final static String CONTEXT_PATH = RPC_BASE_PATH + PATH_INFO; private Map params; @@ -80,7 +79,7 @@ public void doGetTest() { public void doPostTest() { OperationResult result = new OperationResult(HttpServletResponse.SC_OK); - when(request.getContextPath()).thenReturn(CONTEXT_PATH); + when(request.getServletPath()).thenReturn(RPC_SERVLET_PATH); when(request.getPathInfo()).thenReturn(PATH_INFO); when(action.run(any(OperationData.class))).thenReturn(result); diff --git a/api/src/test/java/edu/cornell/mannlib/vitro/webapp/dynapi/ServletContextTest.java b/api/src/test/java/edu/cornell/mannlib/vitro/webapp/dynapi/ServletContextTest.java index e54287849d..c1847baf2f 100644 --- a/api/src/test/java/edu/cornell/mannlib/vitro/webapp/dynapi/ServletContextTest.java +++ b/api/src/test/java/edu/cornell/mannlib/vitro/webapp/dynapi/ServletContextTest.java @@ -47,14 +47,6 @@ public void setup() { loader = new ConfigurationBeanLoader(ontModel, servletContext); } - protected void loadModelsFromN3(String fileFormat, String... paths) throws IOException { - for(String path : paths){ - loadModel( - new RDFFile(fileFormat, path) - ); - } - } - protected void loadTestModel() throws IOException { // all actions reuse testSparqlQuery1 from testing action loadModel( @@ -83,6 +75,12 @@ protected void loadModel(RDFFile... rdfFiles) throws IOException { } } + protected void loadModels(String fileFormat, String... paths) throws IOException { + for (String path : paths) { + loadModel(new RDFFile(fileFormat, path)); + } + } + protected String readFile(String path) throws IOException { Path p = new File(path).toPath(); diff --git a/api/src/test/java/edu/cornell/mannlib/vitro/webapp/dynapi/SolrQueryTest.java b/api/src/test/java/edu/cornell/mannlib/vitro/webapp/dynapi/SolrQueryTest.java index dfed1a7b14..521cf59713 100644 --- a/api/src/test/java/edu/cornell/mannlib/vitro/webapp/dynapi/SolrQueryTest.java +++ b/api/src/test/java/edu/cornell/mannlib/vitro/webapp/dynapi/SolrQueryTest.java @@ -7,7 +7,6 @@ import edu.cornell.mannlib.vitro.webapp.application.ApplicationImpl; import edu.cornell.mannlib.vitro.webapp.application.ApplicationUtils; -import edu.cornell.mannlib.vitro.webapp.dynapi.components.Action; import edu.cornell.mannlib.vitro.webapp.dynapi.components.Parameter; import edu.cornell.mannlib.vitro.webapp.dynapi.components.SolrQuery; import edu.cornell.mannlib.vitro.webapp.modules.searchEngine.SearchEngine; @@ -28,11 +27,11 @@ public class SolrQueryTest extends ServletContextTest{ private final static String TEST_DATA_PATH="src/test/resources/rdf/abox/filegraph/dynamic-api-individuals-solr-test.n3"; private final static String TEST_SOLR_QUERY_URI="https://vivoweb.org/ontology/vitro-dynamic-api/solrQuery/genericSolrTextQuery"; + private static MockedStatic applicationUtils; + @Spy private SolrQuery solrQuery; - private static MockedStatic applicationUtils; - @Mock ApplicationImpl application; @@ -69,7 +68,7 @@ public void setupQuery(){ @Test public void testLoadingAndPropertiesSetup() throws IOException, ConfigurationBeanLoaderException { loadDefaultModel(); - loadModelsFromN3(TEST_DATA_PATH.split("\\.")[1],TEST_DATA_PATH); + loadModels(TEST_DATA_PATH.split("\\.")[1], TEST_DATA_PATH); SolrQuery query = loader.loadInstance(TEST_SOLR_QUERY_URI, SolrQuery.class); assertNotNull(query); diff --git a/api/src/test/java/edu/cornell/mannlib/vitro/webapp/dynapi/request/RequestPathTest.java b/api/src/test/java/edu/cornell/mannlib/vitro/webapp/dynapi/request/RequestPathTest.java index db9f48383f..29cc0d6f26 100644 --- a/api/src/test/java/edu/cornell/mannlib/vitro/webapp/dynapi/request/RequestPathTest.java +++ b/api/src/test/java/edu/cornell/mannlib/vitro/webapp/dynapi/request/RequestPathTest.java @@ -1,5 +1,8 @@ package edu.cornell.mannlib.vitro.webapp.dynapi.request; +import static edu.cornell.mannlib.vitro.webapp.dynapi.request.RequestPath.REST_SERVLET_PATH; +import static edu.cornell.mannlib.vitro.webapp.dynapi.request.RequestPath.RPC_SERVLET_PATH; + import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; @@ -27,7 +30,7 @@ public class RequestPathTest { @Test public void testRpc() { - when(request.getContextPath()).thenReturn("/api/rpc/create"); + when(request.getServletPath()).thenReturn(RPC_SERVLET_PATH); when(request.getPathInfo()).thenReturn("/create"); RequestPath requestPath = RequestPath.from(request); @@ -41,25 +44,25 @@ public void testRpc() { assertEquals(null, requestPath.getResourceId()); - when(request.getContextPath()).thenReturn("/api/rpc/"); + when(request.getServletPath()).thenReturn(RPC_SERVLET_PATH); when(request.getPathInfo()).thenReturn("/"); assertFalse(RequestPath.from(request).isValid()); - when(request.getContextPath()).thenReturn(null); + when(request.getServletPath()).thenReturn(null); when(request.getPathInfo()).thenReturn("/create"); assertFalse(RequestPath.from(request).isValid()); - when(request.getContextPath()).thenReturn("/api/rpc"); + when(request.getServletPath()).thenReturn(RPC_SERVLET_PATH); when(request.getPathInfo()).thenReturn(null); assertFalse(RequestPath.from(request).isValid()); - when(request.getContextPath()).thenReturn(null); + when(request.getServletPath()).thenReturn(null); when(request.getPathInfo()).thenReturn(null); assertFalse(RequestPath.from(request).isValid()); @@ -67,7 +70,7 @@ public void testRpc() { @Test public void testRestCollection() { - when(request.getContextPath()).thenReturn("/api/rest/1/persons"); + when(request.getServletPath()).thenReturn(REST_SERVLET_PATH); when(request.getPathInfo()).thenReturn("/1/persons"); RequestPath requestPath = RequestPath.from(request); @@ -81,25 +84,25 @@ public void testRestCollection() { assertEquals(null, requestPath.getActionName()); - when(request.getContextPath()).thenReturn("/api/rest/"); + when(request.getServletPath()).thenReturn(REST_SERVLET_PATH); when(request.getPathInfo()).thenReturn("/"); assertFalse(RequestPath.from(request).isValid()); - when(request.getContextPath()).thenReturn(null); + when(request.getServletPath()).thenReturn(null); when(request.getPathInfo()).thenReturn("/1/persons"); assertFalse(RequestPath.from(request).isValid()); - when(request.getContextPath()).thenReturn("/api/rest"); + when(request.getServletPath()).thenReturn(REST_SERVLET_PATH); when(request.getPathInfo()).thenReturn(null); assertFalse(RequestPath.from(request).isValid()); - when(request.getContextPath()).thenReturn(null); + when(request.getServletPath()).thenReturn(null); when(request.getPathInfo()).thenReturn(null); assertFalse(RequestPath.from(request).isValid()); @@ -107,7 +110,7 @@ public void testRestCollection() { @Test public void testRestCustomAction() { - when(request.getContextPath()).thenReturn("/api/rest/1/persons/dedupe"); + when(request.getServletPath()).thenReturn(REST_SERVLET_PATH); when(request.getPathInfo()).thenReturn("/1/persons/dedupe"); RequestPath requestPath = RequestPath.from(request); @@ -123,7 +126,7 @@ public void testRestCustomAction() { @Test public void testRestIndividual() { - when(request.getContextPath()).thenReturn("/api/rest/1/persons/resource:" + encodedResourceId); + when(request.getServletPath()).thenReturn(REST_SERVLET_PATH); when(request.getPathInfo()).thenReturn("/1/persons/resource:" + encodedResourceId); RequestPath requestPath = RequestPath.from(request); @@ -139,7 +142,7 @@ public void testRestIndividual() { @Test public void testRestIndividualCustomAction() { - when(request.getContextPath()).thenReturn("/api/rest/1/persons/resource:" + encodedResourceId + "/patch"); + when(request.getServletPath()).thenReturn(REST_SERVLET_PATH); when(request.getPathInfo()).thenReturn("/1/persons/resource:" + encodedResourceId + "/patch"); RequestPath requestPath = RequestPath.from(request); @@ -155,17 +158,17 @@ public void testRestIndividualCustomAction() { @Test public void testNotFound() { - when(request.getContextPath()).thenReturn("/api/rest/1/persons/resource:" + encodedResourceId + "/patch/foo"); + when(request.getServletPath()).thenReturn(REST_SERVLET_PATH); when(request.getPathInfo()).thenReturn("/1/persons/resource:" + encodedResourceId + "/patch/foo"); assertFalse(RequestPath.from(request).isValid()); - when(request.getContextPath()).thenReturn("/api/bar/1/persons"); + when(request.getServletPath()).thenReturn("/api/bar"); when(request.getPathInfo()).thenReturn("/1/persons"); assertFalse(RequestPath.from(request).isValid()); - when(request.getContextPath()).thenReturn("/some/random/path"); + when(request.getServletPath()).thenReturn("/some/random/path"); when(request.getPathInfo()).thenReturn("/3/2/1"); assertFalse(RequestPath.from(request).isValid());