### Question:
TagsExportServlet extends SlingSafeMethodsServlet { @Override public void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException, ServletException { ResourceResolver rr = request.getResourceResolver(); Parameters params = new Parameters(request); if (params.containsPath()) { Writer writer = response.getWriter(); response.setContentType("text/csv"); response.setCharacterEncoding("UTF-8"); if (params.isLocalized()) { writer.write(tagsExportService.exportLocalizedTagsForPath(params.getPath(), rr, params.getDefaultLocalization())); } else { writer.write(tagsExportService.exportNonLocalizedTagsForPath(params.getPath(), rr)); } } else { log.warn("Cannot generate tag CSV file, missing 'path' parameter in request."); } }  @Override void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response);  }### Answer:
@Test public void testCorrectParameters_defaultLocalizationAppliedOnlyToTagWithoutLocalization() throws Exception { Map<String, Object> params = ImmutableMap.of( PARAMETER_PATH, "/etc/tags/root/zeroLocalizedTitle", PARAMETER_LOCALIZED,"true", PARAMETER_DEFAULT_LOCALIZATION,"pl"); context.request().setParameterMap(params); servlet.doGet(context.request(), context.response()); assertEquals(HttpStatus.SC_OK, context.response().getStatus()); String output = context.response().getOutputAsString(); assertTrue(output.contains("en[root] {{root}}")); assertTrue(output.contains("pl[Zero Localized Title] {{zeroLocalizedTitle}}")); }
@Test public void testWrongPath_emptyOutput() throws Exception { Map<String, Object> params = ImmutableMap.of( PARAMETER_PATH, "/wrong/Path", PARAMETER_LOCALIZED,"true", PARAMETER_DEFAULT_LOCALIZATION,"pl"); context.request().setParameterMap(params); servlet.doGet(context.request(), context.response()); assertEquals(HttpStatus.SC_OK, context.response().getStatus()); String output = context.response().getOutputAsString(); assertEquals(EXPECTED_OUTPUT_WRONG_PATH_MESSAGE, output); }
@Test public void testMissingPath_emptyOutput() throws Exception { Map<String, Object> params = ImmutableMap.of( PARAMETER_LOCALIZED,"true", PARAMETER_DEFAULT_LOCALIZATION,"pl"); context.request().setParameterMap(params); servlet.doGet(context.request(), context.response()); assertEquals(HttpStatus.SC_OK, context.response().getStatus()); String output = context.response().getOutputAsString(); assertTrue(output.isEmpty()); }
@Test public void testCorrectParameters_nonLocalizedOutput() throws Exception { Map<String, Object> params = ImmutableMap.of( PARAMETER_PATH, BRAND_TAGS_ROOT, PARAMETER_LOCALIZED,"false", PARAMETER_DEFAULT_LOCALIZATION,"pl"); context.request().setParameterMap(params); servlet.doGet(context.request(), context.response()); assertEquals(HttpStatus.SC_OK, context.response().getStatus()); String output = context.response().getOutputAsString(); assertTrue(output.contains("Products {{products}}")); assertFalse(output.contains("pl[")); assertFalse(output.contains("en[")); }
@Test public void missingLocalizationParams_defaultParamsApplied_nonLocalizedOutput() throws Exception { Map<String, Object> params = ImmutableMap.of( PARAMETER_PATH, BRAND_TAGS_ROOT); context.request().setParameterMap(params); servlet.doGet(context.request(), context.response()); assertEquals(HttpStatus.SC_OK, context.response().getStatus()); String output = context.response().getOutputAsString(); assertTrue(output.contains("Products {{products}}")); assertFalse(output.contains("pl[")); assertFalse(output.contains("en[")); }