### Question:
OptionsServlet extends SlingSafeMethodsServlet { @Override @SuppressWarnings({"squid:S3776", "squid:S1141"}) protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException { response.setContentType("application/json"); final JsonWriter writer = new JsonWriter(response.getWriter()); writer.beginArray(); String type = request.getRequestPathInfo().getSelectorString(); if (type != null) { try { Set<String> categories = new TreeSet<String>(); LibraryType libraryType = LibraryType.valueOf(type.toUpperCase()); Map<String, ClientLibrary> libraries = libraryManager.getLibraries(); for (ClientLibrary library : libraries.values()) { if (library.getTypes() != null && library.getTypes().contains(libraryType)) { String[] libraryCats = library.getCategories(); if (libraryCats != null) { for (String cat : libraryCats) { categories.add(cat); } } } } for (String cat : categories) { writer.beginObject(); writer.name("value"); writer.value(cat); writer.name("text"); writer.value(cat); writer.endObject(); } } catch (IllegalArgumentException e) { } } writer.endArray(); writer.close(); }    }### Answer:
@Test public void testWithNoType() throws Exception { MockSlingHttpServletRequest request = new MockSlingHttpServletRequest( "/apps/acs-commons/components/utilities/designer/clientlibsmanager/options", null, "json", null, null); MockSlingHttpServletResponse response = new MockSlingHttpServletResponse(); servlet.doGet(request, response); assertEquals("application/json", response.getContentType()); assertEquals("[]", response.getOutput().toString()); }
@Test public void testWithBadType() throws Exception { MockSlingHttpServletRequest request = new MockSlingHttpServletRequest( "/apps/acs-commons/components/utilities/designer/clientlibsmanager/options", "html", "json", null, null); MockSlingHttpServletResponse response = new MockSlingHttpServletResponse(); servlet.doGet(request, response); assertEquals("application/json", response.getContentType()); assertEquals("[]", response.getOutput().toString()); }
@Test public void testWithNormalType() throws Exception { Map<String, ClientLibrary> libraries = new HashMap<String, ClientLibrary>(); String jsOnlyCategory1 = RandomStringUtils.randomAlphanumeric(5); String jsOnlyCategory2 = RandomStringUtils.randomAlphanumeric(5); String bothCategory1 = RandomStringUtils.randomAlphanumeric(5); String bothCategory2 = RandomStringUtils.randomAlphanumeric(5); addLibrary(libraries, RandomStringUtils.random(10), new String[] { "js" }, new String[] { jsOnlyCategory1, jsOnlyCategory2 }); addLibrary(libraries, RandomStringUtils.random(10), new String[] { "js" }, new String[] { jsOnlyCategory2 }); addLibrary(libraries, RandomStringUtils.random(10), new String[] { "js", "css" }, new String[] { bothCategory1, bothCategory2 }); addLibrary(libraries, RandomStringUtils.random(10), new String[] { "js", "css" }, new String[] { bothCategory2 }); when(manager.getLibraries()).thenReturn(libraries); MockSlingHttpServletRequest request = new MockSlingHttpServletRequest( "/apps/acs-commons/components/utilities/designer/clientlibsmanager/options", "js", "json", null, null); MockSlingHttpServletResponse response = new MockSlingHttpServletResponse(); servlet.doGet(request, response); assertEquals("application/json", response.getContentType()); JSONArray array = new JSONArray(response.getOutput().toString()); assertEquals(4, array.length()); }