JSONUtils { public static JValue filtersToJSON(ResourceListQuery query, ListProvidersService listProvidersService, Organization org) throws ListProviderException { List<Field> filtersJSON = new ArrayList<Field>(); List<Field> fields = null; List<ResourceListFilter<?>> filters = query.getAvailableFilters(); for (ResourceListFilter<?> f : filters) { fields = new ArrayList<Field>(); fields.add(f("type", v(f.getSourceType().toString().toLowerCase()))); fields.add(f("label", v(f.getLabel()))); Option<String> listProviderName = f.getValuesListName(); if (listProviderName.isSome()) { Map<String, String> values = null; boolean translatable = false; if (org != null && !listProvidersService.hasProvider(listProviderName.get(), org.getId()) && !listProvidersService.hasProvider(listProviderName.get())) { values = new HashMap<String, String>(); } else { values = listProvidersService.getList(listProviderName.get(), query, false); translatable = listProvidersService.isTranslatable(listProviderName.get()); } List<Field> valuesJSON = new ArrayList<Field>(); for (Entry<String, String> entry : values.entrySet()) { valuesJSON.add(f(entry.getKey(), v(entry.getValue(), Jsons.BLANK))); } fields.add(f("options", obj(valuesJSON))); fields.add(f("translatable", translatable)); } filtersJSON.add(f(f.getName(), obj(fields))); } return obj(filtersJSON); } private  JSONUtils(); static JObject mapToJSON(Map<String, String> map); static JValue setToJSON(Set<String> set); static JValue filtersToJSON(ResourceListQuery query, ListProvidersService listProvidersService,
          Organization org); static JValue filtersToJSONSeriesWriteAccess(ResourceListQuery query, ListProvidersService listProvidersService,
          Organization org, Map<String, String> series); static JSONObject fromMap(Map<String, String> map); static Map<String, String> toMap(JSONObject json);  }
@Test public void testFiltersToJSON() throws Exception { String expectedJSON = IOUtils.toString(getClass().getResource("/filters.json")); SecurityService securityService = EasyMock.createNiceMock(SecurityService.class); Organization organization = EasyMock.createNiceMock(Organization.class); EasyMock.expect(securityService.getOrganization()).andReturn(organization).anyTimes(); EasyMock.expect(organization.getId()).andReturn("mh_default_org").anyTimes(); EasyMock.replay(organization); EasyMock.replay(securityService); ListProvidersServiceImpl listProvidersService = new ListProvidersServiceImpl(); SimpleSerializer serializer = new SimpleSerializer(); listProvidersService.setSecurityService(securityService); final Map<String, String> license = new HashMap<String, String>(); license.put("contributor1", "My first contributor"); license.put("contributor2", "My second contributor"); license.put("contributor3", "My third contributor"); listProvidersService.addProvider(ContributorsListProvider.DEFAULT, new ResourceListProvider() { @Override public String[] getListNames() { return new String[] { ContributorsListProvider.DEFAULT }; } @Override public Map<String, String> getList(String listName, ResourceListQuery query) throws ListProviderException { return ListProviderUtil.filterMap(license, query); } @Override public boolean isTranslatable(String listName) { return false; } @Override public String getDefault() { return null; } }, organization.getId()); List<ResourceListFilter<?>> filters = new ArrayList<ResourceListFilter<?>>(); filters.add(SeriesListQuery.createContributorsFilter(Option.<String> none())); filters.add(new StringListFilter("")); ResourceListQueryImpl query = EasyMock.createNiceMock(ResourceListQueryImpl.class); EasyMock.expect(query.getAvailableFilters()).andReturn(filters).anyTimes(); EasyMock.expect(query.getFilters()).andReturn(new ArrayList<ResourceListFilter<?>>()).anyTimes(); EasyMock.expect(query.getLimit()).andReturn(Option.<Integer> none()).anyTimes(); EasyMock.expect(query.getOffset()).andReturn(Option.<Integer> none()).anyTimes(); EasyMock.replay(query); JValue result = JSONUtils.filtersToJSON(query, listProvidersService, organization); StreamingOutput stream = RestUtils.stream(serializer.fn.toJson(result)); ByteArrayOutputStream resultStream = new ByteArrayOutputStream(); try { stream.write(resultStream); assertThat(expectedJSON, SameJSONAs.sameJSONAs(resultStream.toString())); } finally { IOUtils.closeQuietly(resultStream); } }