@NonNull private Map<String, List<FacetValue>> parseFacets(@Nullable JSONObject facets) { Map<String, List<FacetValue>> facetMap = new HashMap<>(); if (facets == null) { return facetMap; } final Iterator<String> attributesIterator = facets.keys(); while (attributesIterator.hasNext()) { final String attribute = attributesIterator.next(); final JSONObject attributeFacets = facets.optJSONObject(attribute); final Iterator<String> valuesIterator = attributeFacets.keys(); List<FacetValue> facetList = new ArrayList<>(); while (valuesIterator.hasNext()) { final String value = valuesIterator.next(); final int count = attributeFacets.optInt(value); facetList.add(new FacetValue(value, count)); } facetMap.put(attribute, facetList); } return facetMap; }
@Test public void shouldSortWithMoreValuesThanLimit() { //Given two facet value, and a RefinementList with limit=1 final RefinementList.FacetAdapter facetAdapter = (RefinementList.FacetAdapter) mRefinementList.getAdapter(); facetAdapter.addAll(Arrays.asList(new FacetValue("foo", 1), new FacetValue("bar", 2))); mRefinementList.setLimit(1); // Expect successful sort and one facet stored facetAdapter.sort(mRefinementList.getSortComparator()); assertThat(facetAdapter.getCount(), is(1)); }
@Test public void shouldSortWithLessValuesThanLimit() { //Given one facet value, and a RefinementList with limit=10 final RefinementList.FacetAdapter facetAdapter = (RefinementList.FacetAdapter) mRefinementList.getAdapter(); facetAdapter.addAll(Collections.singletonList(new FacetValue("foo", 1))); // Expect successful sort and one facet stored facetAdapter.sort(mRefinementList.getSortComparator()); assertThat(facetAdapter.getCount(), is(1)); }