### Question:
CreateCloudConfigServlet extends SlingAllMethodsServlet { @Override protected void doPost(@Nonnull SlingHttpServletRequest request, @Nonnull SlingHttpServletResponse response) throws IOException { ResourceResolver resolver = request.getResourceResolver(); PageManager pageManager = resolver.adaptTo(PageManager.class); HtmlResponse resp = new HtmlResponse(); if (pageManager == null) { resp.setError(new IOException("Unable to get page manager")); } else { String configPath = getParam(request, "configPath"); Resource cloudConfigFolder = ResourceUtil.getOrCreateResource(resolver, configPath + "/settings/cloudconfigs", Collections.singletonMap(JcrConstants.JCR_PRIMARYTYPE, JcrResourceConstants.NT_SLING_FOLDER), JcrResourceConstants.NT_SLING_FOLDER, false); log.debug("Creating Cloud Config in: {}", cloudConfigFolder); resp.setParentLocation(cloudConfigFolder.getPath()); Page page; try { page = pageManager.create(cloudConfigFolder.getPath(), getParam(request, "name"), getParam(request, "template"), getParam(request, "title")); resp.setPath(page.getPath()); resp.setLocation(page.getPath()); resp.setStatus(200, "Created Cloud Configuration"); log.debug("Created configuration: {}", page.getPath()); resolver.commit(); } catch (WCMException e) { resp.setError(e); } } response.setContentType("text/plain"); resp.send(response, true); }    }### Answer:
@Test public void testDoPost() throws IOException { Map<String, Object> parameterMap = new HashMap<>(); parameterMap.put("configPath", "/conf/test2"); parameterMap.put("title", "Test"); parameterMap.put("name", "test"); parameterMap.put("template", "/apps/core/wcm/templates/marketocloudconfig"); context.request().setParameterMap(parameterMap); CreateCloudConfigServlet cccSrvlt = new CreateCloudConfigServlet(); cccSrvlt.doPost(context.request(), context.response()); assertNotNull(context.response().getOutputAsString()); assertTrue(context.response().getOutputAsString().contains("Created Cloud Configuration")); assertNotNull(context.resourceResolver().getResource("/conf/test2/settings/cloudconfigs/test")); ValueMap properties = Optional .ofNullable(context.resourceResolver().getResource("/conf/test2/settings/cloudconfigs/test/jcr:content")) .map(r -> r.getValueMap()).orElse(null); assertEquals("Test", properties.get("jcr:title", String.class)); assertEquals("/apps/core/wcm/templates/marketocloudconfig", properties.get("cq:template", String.class)); }
@Test public void testInvalid() throws IOException { Map<String, Object> parameterMap = new HashMap<>(); parameterMap.put("configPath", "/conf/test2"); parameterMap.put("name", "test2"); parameterMap.put("template", "/apps/core/wcm/templates/marketocloudconfig"); context.request().setParameterMap(parameterMap); try { CreateCloudConfigServlet cccSrvlt = new CreateCloudConfigServlet(); cccSrvlt.doPost(context.request(), context.response()); fail(); } catch (IOException e) { assertNull(context.resourceResolver().getResource("/conf/test2/settings/cloudconfigs/test2")); } }