QueryServlet extends TransformationServlet { protected String getQueryText(WorkbenchRequest req) throws BadRequestException, RDF4JException { String result; if (req.isParameterPresent(QUERY)) { String query = req.getParameter(QUERY); if (req.isParameterPresent(REF)) { String ref = req.getParameter(REF); if ("text".equals(ref)) { result = query; } else if ("hash".equals(ref)) { result = queryCache.get(query); if (null == result) { result = ""; } } else if ("id".equals(ref)) { result = storage.getQueryText((HTTPRepository) repository, getUserNameFromParameter(req, "owner"), query); } else { result = query; } } else { result = query; } } else { result = ""; } return result; }  @Override String[] getCookieNames(); @Override void init(final ServletConfig config); @Override void destroy(); @Override final void service(final HttpServletRequest req, final HttpServletResponse resp);  }
@Test public void testGetQueryTextRefText() throws BadRequestException, RDF4JException { WorkbenchRequest request = mock(WorkbenchRequest.class); when(request.isParameterPresent(QueryServlet.QUERY)).thenReturn(true); when(request.getParameter(QueryServlet.QUERY)).thenReturn(SHORT_QUERY); when(request.isParameterPresent(QueryServlet.REF)).thenReturn(true); when(request.getParameter(QueryServlet.REF)).thenReturn("text"); assertThat(servlet.getQueryText(request)).isEqualTo(SHORT_QUERY); }
@Test public void testGetQueryNoQuery() throws BadRequestException, RDF4JException { WorkbenchRequest request = mock(WorkbenchRequest.class); when(request.isParameterPresent(QueryServlet.QUERY)).thenReturn(false); assertThat(servlet.getQueryText(request)).isEmpty(); ; }
@Test public void testGetQueryTextUnrecognizedRef() throws BadRequestException, RDF4JException { WorkbenchRequest request = mock(WorkbenchRequest.class); when(request.isParameterPresent(QueryServlet.QUERY)).thenReturn(true); when(request.getParameter(QueryServlet.QUERY)).thenReturn(SHORT_QUERY); when(request.isParameterPresent(QueryServlet.REF)).thenReturn(true); when(request.getParameter(QueryServlet.REF)).thenReturn("junk"); assertThat(servlet.getQueryText(request)).isEqualTo(SHORT_QUERY); }
@Test public void testGetQueryTextNoRef() throws BadRequestException, RDF4JException { WorkbenchRequest request = mock(WorkbenchRequest.class); when(request.isParameterPresent(QueryServlet.QUERY)).thenReturn(true); when(request.getParameter(QueryServlet.QUERY)).thenReturn(SHORT_QUERY); when(request.isParameterPresent(QueryServlet.REF)).thenReturn(false); assertThat(servlet.getQueryText(request)).isEqualTo(SHORT_QUERY); }