PipelineManagerServlet extends AbstractApiServlet { private void create(HttpServletRequest req, HttpServletResponse resp) throws IOException { String name = req.getParameter(PARAM_NAME); String yaml = req.getParameter(PARAM_YAML); String multi = req.getParameter(PARAM_MULTIPLICITY); int multiplicity = Strings.isNullOrEmpty(multi) ? 1 : Integer.parseInt(multi); if (!parametersPresent(name, yaml) || manager.has(name)) { respondWithBadArguments(resp); return; } List<BaleenPipeline> t; try { t = manager.create(name, new YamlPipelineConfiguration(yaml), multiplicity); } catch (BaleenException e) { LOGGER.error("Unable to create", e); respondWithError( resp, HttpStatus.BAD_REQUEST_400, "Creation of pipeline(s) from yaml failed"); return; } if (multiplicity == 1) { respondWithJson(resp, t.get(0)); } else { respondWithJson(resp, t); } } PipelineManagerServlet(BaleenPipelineManager manager);  PipelineManagerServlet(Logger logger, Class<?> clazz, BaleenPipelineManager manager); @Override WebPermission[] getPermissions(); static final String PARAM_NAME; static final String PARAM_YAML; static final String PARAM_MULTIPLICITY; }
@Test public void testPostForCreate() throws Exception { String yaml = "yaml"; when(pipelineManager.create(eq("new"), anyPiplieConfiguration(), eq(1))) .thenReturn(ImmutableList.of(realPipeline)); ServletCaller caller = new ServletCaller(); caller.setRequestUri(ROOT); caller.addParameter(NAME, "new"); caller.addParameter(YAML, yaml); caller.doPost(new PipelineManagerServlet(pipelineManager)); assertEquals(200, caller.getResponseStatus().intValue()); verify(pipelineManager).create(eq("new"), anyPiplieConfiguration(), eq(1)); }
@Test public void testPostForCreateMultiple() throws Exception { String yaml = "yaml: test"; when(pipelineManager.create(eq("new"), anyPiplieConfiguration(), eq(2))) .thenReturn(ImmutableList.of(realPipeline, realPipeline)); ServletCaller caller = new ServletCaller(); caller.setRequestUri(ROOT); caller.addParameter(NAME, "new"); caller.addParameter(YAML, yaml); caller.addParameter(MULTIPLICITY, "2"); caller.doPost(new PipelineManagerServlet(pipelineManager)); assertEquals(200, caller.getResponseStatus().intValue()); verify(pipelineManager).create(eq("new"), anyPiplieConfiguration(), eq(2)); }
@Test public void testPostForCreateWithSameName() throws Exception { String yaml = "yaml"; ServletCaller caller = new ServletCaller(); caller.setRequestUri(ROOT); caller.addParameter(NAME, REAL); caller.addParameter(YAML, yaml); caller.doPost(new PipelineManagerServlet(pipelineManager)); assertEquals(400, caller.getSentError().intValue()); verify(pipelineManager, never()).create(REAL, pipelineConfiguration); }
@Test public void testPostForCreateWithInvalid() throws Exception { String yaml = "yaml"; ServletCaller missingNameCaller = new ServletCaller(); missingNameCaller.setRequestUri(ROOT); missingNameCaller.addParameter(YAML, yaml); missingNameCaller.doPost(new PipelineManagerServlet(pipelineManager)); assertEquals(400, missingNameCaller.getSentError().intValue()); verify(pipelineManager, never()).create(anyString(), anyPiplieConfiguration()); ServletCaller missingYamlCaller = new ServletCaller(); missingYamlCaller.setRequestUri(ROOT); missingYamlCaller.addParameter(NAME, NAME); missingYamlCaller.doPost(new PipelineManagerServlet(pipelineManager)); assertEquals(400, missingYamlCaller.getSentError().intValue()); verify(pipelineManager, never()).create(anyString(), anyPiplieConfiguration()); ServletCaller emptyYamlCaller = new ServletCaller(); emptyYamlCaller.setRequestUri(ROOT); emptyYamlCaller.addParameter(NAME, NAME); emptyYamlCaller.addParameter(YAML, ""); emptyYamlCaller.doPost(new PipelineManagerServlet(pipelineManager)); assertEquals(400, missingYamlCaller.getSentError().intValue()); verify(pipelineManager, never()).create(anyString(), anyPiplieConfiguration()); ServletCaller emptyNameCaller = new ServletCaller(); emptyNameCaller.setRequestUri(ROOT); emptyNameCaller.addParameter(NAME, NAME); emptyNameCaller.addParameter(YAML, ""); emptyNameCaller.doPost(new PipelineManagerServlet(pipelineManager)); assertEquals(400, emptyNameCaller.getSentError().intValue()); verify(pipelineManager, never()).create(anyString(), anyPiplieConfiguration()); }
@Test public void testPostCreationFailure() throws Exception { doThrow(BaleenException.class) .when(pipelineManager) .create(anyString(), anyPiplieConfiguration(), anyInt()); String yaml = "yaml"; ServletCaller caller = new ServletCaller(); caller.setRequestUri(ROOT); caller.addParameter(NAME, yaml); caller.addParameter(YAML, yaml); caller.doPost(new PipelineManagerServlet(pipelineManager)); assertEquals(400, caller.getSentError().intValue()); }