@Test public void testDownloadSingleScriptRevision() throws Exception { /* setup mock */ String filter_id = "name:type"; String action = "DOWNLOAD"; when(request.getPathInfo()).thenReturn("?action=" + action + "&filter_id=" + filter_id + "&revision=2"); when(request.getMethod()).thenReturn("GET"); when(request.getParameter("action")).thenReturn(action); when(request.getParameter("filter_id")).thenReturn(filter_id); when(request.getParameter("revision")).thenReturn("2"); /* setup mock DAO */ ZuulFilterDAO dao = mock(ZuulFilterDAOCassandra.class); FilterInfo script = mock(FilterInfo.class); when(dao.getFilterInfoForFilter(filter_id, 2)).thenReturn(script); String code = "code"; when(script.getFilterCode()).thenReturn(code); FilterScriptManagerServlet servlet = getEndpointScriptManagerImplementation(dao); servlet.service(request, response); /* verify the default status is used */ verify(response, never()).setStatus(anyInt()); // verify mime-type verify(response).setContentType("text/plain"); // verify the script is written to the response verify(responseWriter).write("code"); }
FilterScriptManagerServlet servlet = getEndpointScriptManagerImplementation(dao); servlet.service(request, response); List<Map<String, Object>> scriptsJson = new ArrayList<Map<String, Object>>(); for (FilterInfo script : scriptsForEndpoint) { Map<String, Object> scriptJson = createExpectedJsonMap(script); scriptsJson.add(scriptJson);
FilterScriptManagerServlet servlet = getEndpointScriptManagerImplementation(dao); servlet.service(request, response);
@Test public void testDownloadSingleScriptPlusErrorHandlerLatest() throws Exception { /* setup mock */ String filter_id = "name:type"; String action = "DOWNLOAD"; when(request.getPathInfo()).thenReturn("?action=" + action + "&filter_id=" + filter_id); when(request.getMethod()).thenReturn("GET"); when(request.getParameter("action")).thenReturn(action); when(request.getParameter("filter_id")).thenReturn(filter_id); /* setup mock DAO */ ZuulFilterDAO dao = mock(ZuulFilterDAOCassandra.class); FilterInfo script = mock(FilterInfo.class); when(dao.getLatestFilterInfoForFilter(filter_id)).thenReturn(script); when(dao.getFilterInfoForFilter(filter_id, 2)).thenReturn(script); when(script.getFilterCode()).thenReturn("code"); FilterScriptManagerServlet servlet = getEndpointScriptManagerImplementation(dao); servlet.service(request, response); /* verify the default status is used */ verify(response, never()).setStatus(anyInt()); // the writer should not be touched since we need binary, not text verify(responseWriter).write(anyString()); }
@Test public void testDownloadSingleScriptLatest() throws Exception { /* setup mock */ String filter_id = "name:type"; String action = "DOWNLOAD"; when(request.getPathInfo()).thenReturn("?action=" + action + "&filter_id=" + filter_id); when(request.getMethod()).thenReturn("GET"); when(request.getParameter("action")).thenReturn(action); when(request.getParameter("filter_id")).thenReturn(filter_id); /* setup mock DAO */ ZuulFilterDAO dao = mock(ZuulFilterDAOCassandra.class); FilterInfo script = mock(FilterInfo.class); when(dao.getLatestFilterInfoForFilter(filter_id)).thenReturn(script); when(dao.getFilterInfoForFilter(filter_id, 2)).thenReturn(script); when(script.getFilterCode()).thenReturn("code"); FilterScriptManagerServlet servlet = getEndpointScriptManagerImplementation(dao); servlet.service(request, response); /* verify the default status is used */ verify(response, never()).setStatus(anyInt()); // verify mime-type verify(response).setContentType("text/plain"); // verify the script is written to the response verify(responseWriter).write("code"); }
/** * Not found response for a "list scripts" request. * * @throws Exception */ @Test public void testListScriptNotFound() throws Exception { /* setup mock request to return URI */ when(request.getPathInfo()).thenReturn("?action=LIST&filter_id=name2:type"); when(request.getParameter("action")).thenReturn("LIST"); when(request.getParameter("filter_id")).thenReturn("name:type"); when(request.getMethod()).thenReturn("GET"); /* setup mock DAO */ ZuulFilterDAO dao = mock(ZuulFilterDAOCassandra.class); List<FilterInfo> emptyResponse = Collections.emptyList(); when(dao.getZuulFiltersForFilterId(anyString())).thenReturn(emptyResponse); /* construct servlet */ FilterScriptManagerServlet servlet = getEndpointScriptManagerImplementation(dao); servlet.service(request, response); // verify that we get a 404 when this endpoint isn't found verify(response).setStatus(404); }
@Test public void testActivateRevision() throws Exception { /* setup mock */ String endpoint = "name:type"; String action = "ACTIVATE"; when(request.getPathInfo()).thenReturn("?action=" + action + "&filter_id=" + endpoint + "&revision=2"); when(request.getMethod()).thenReturn("POST"); when(request.getParameter("action")).thenReturn(action); when(request.getParameter("filter_id")).thenReturn(endpoint); when(request.getParameter("revision")).thenReturn("2"); /* setup mock DAO */ ZuulFilterDAOCassandra dao = mock(ZuulFilterDAOCassandra.class); /* setup the mock script that will be uploaded */ FilterInfo script = mockEndpointScript(); // userAuthenticationRequired should default to true when(dao.setFilterActive(endpoint, 2)).thenReturn(script); // execute the servlet FilterScriptManagerServlet servlet = getEndpointScriptManagerImplementation(dao); servlet.service(request, response); /* verify the default status is used */ verify(response, never()).setStatus(anyInt()); verify(dao).setFilterActive(endpoint, 2); }
@Test public void testActivateEndpointNotFound() throws Exception { /* setup mock */ String action = "ACTIVATE"; when(request.getPathInfo()).thenReturn("?action=" + action); when(request.getMethod()).thenReturn("POST"); when(request.getParameter("action")).thenReturn(action); ZuulFilterDAO dao = mock(ZuulFilterDAOCassandra.class); FilterScriptManagerServlet servlet = getEndpointScriptManagerImplementation(dao); servlet.service(request, response); // a 404 because incorrect arguments were given (so the resource 'does not exist') verify(response).setStatus(404); // test that the usage docs were output verify(response).getWriter(); verify(responseWriter).write("ERROR: No endpoint defined.\n\n"); verify(responseWriter).write(getUsageDoc()); }
@Test public void testDownloadEndpointNotFound() throws Exception { /* setup mock */ String action = "DOWNLOAD"; when(request.getPathInfo()).thenReturn("?action=" + action); when(request.getMethod()).thenReturn("GET"); when(request.getParameter("action")).thenReturn(action); ZuulFilterDAO dao = mock(ZuulFilterDAOCassandra.class); FilterScriptManagerServlet servlet = getEndpointScriptManagerImplementation(dao); servlet.service(request, response); // a 404 because incorrect arguments were given (so the resource 'does not exist') verify(response).setStatus(404); // test that the usage docs were output verify(response).getWriter(); verify(responseWriter).write("ERROR: No endpoint defined.\n\n"); verify(responseWriter).write(getUsageDoc()); }
@Test public void testActivateRevisionNotFound() throws Exception { /* setup mock */ String endpoint = "/ps3/{userID}/home"; String action = "ACTIVATE"; when(request.getPathInfo()).thenReturn("?action=" + action + "&filter_id=" + endpoint); when(request.getMethod()).thenReturn("POST"); when(request.getParameter("action")).thenReturn(action); when(request.getParameter("filter_id")).thenReturn(endpoint); ZuulFilterDAO dao = mock(ZuulFilterDAOCassandra.class); FilterScriptManagerServlet servlet = getEndpointScriptManagerImplementation(dao); servlet.service(request, response); // a 404 because incorrect arguments were given (so the resource 'does not exist') verify(response).setStatus(404); // test that the usage docs were output verify(response).getWriter(); verify(responseWriter).write("ERROR: No revision defined.\n\n"); verify(responseWriter).write(getUsageDoc()); }