Permalink
Browse files

Initial cut of refinements config.

  • Loading branch information...
1 parent 347daf1 commit faf18d8757be7c48a61656fc41397ec97c04de38 Matthew Wall committed Jun 13, 2010
@@ -26,5 +26,6 @@
showFields,
section,
tag,
+ showRefinements,
apiKey
}
@@ -23,12 +23,14 @@
private final Set<TagType> tagTypesToQuery;
private final Set<FieldType> fieldsToQuery;
private final Set<StringUrlParameter> tagsToFilter;
+ private final Set<TagType> refinementsToReturn;
public ContentSearchQuery(SearchJsonParser jsonParser, HttpClient httpClient) {
super(httpClient, jsonParser);
tagTypesToQuery = new HashSet<TagType>();
fieldsToQuery = new HashSet<FieldType>();
tagsToFilter = new HashSet<StringUrlParameter>();
+ refinementsToReturn = new HashSet<TagType>();
}
public ContentSearchResult execute() {
@@ -64,6 +66,11 @@ public ContentSearchQuery showTagTypes(TagType... tags) {
return this;
}
+ public ContentSearchQuery showRefinementsOfType(TagType tagType) {
+ refinementsToReturn.add(tagType);
+ return this;
+ }
+
public ContentSearchQuery filterByTag(Tag tag) {
tagsToFilter.add(new StringUrlParameter(tag.getId()));
return this;
@@ -106,6 +113,16 @@ public ContentSearchQuery showField(FieldType... fieldTypes) {
return this;
}
+ @Override
+ protected void fillInApiParameters(Map<ApiParameter, Object> parameters) {
+ super.fillInApiParameters(parameters);
+
+ addHttpParameter(parameters, ApiParameter.showTags, tagTypesToQuery);
+ addHttpParameter(parameters, ApiParameter.showFields, fieldsToQuery);
+ addHttpParameter(parameters, ApiParameter.tag, tagsToFilter);
+ addHttpParameter(parameters, ApiParameter.showRefinements, refinementsToReturn);
+ }
+
Set<? extends ApiUrlParameter> getFieldsToFind() {
return Collections.unmodifiableSet(fieldsToQuery);
}
@@ -118,17 +135,12 @@ public ContentSearchQuery showField(FieldType... fieldTypes) {
return Collections.unmodifiableSet(tagsToFilter);
}
- boolean isRetrievingAllFields() {
- return fieldsToQuery.contains(FieldType.showAllFields);
+ Set<TagType> getRefinementsToReturn() {
+ return Collections.unmodifiableSet(refinementsToReturn);
}
- @Override
- protected void fillInApiParameters(Map<ApiParameter, Object> parameters) {
- super.fillInApiParameters(parameters);
-
- addHttpParameter(parameters, ApiParameter.showTags, getTagTypesToFind());
- addHttpParameter(parameters, ApiParameter.showFields, getFieldsToFind());
- addHttpParameter(parameters, ApiParameter.tag, getTagIdsToFilter());
+ boolean isRetrievingAllFields() {
+ return fieldsToQuery.contains(FieldType.showAllFields);
}
private void addShowTag(TagType tag) {
@@ -23,6 +23,7 @@
public class ContentSearchResult extends PaginatedSearchResult<ContentSearchQuery> {
protected final List<Content> content;
+ protected List<Refinement> refinements;
protected OrderBy orderBy;
protected ContentSearchResult(ContentSearchQuery query) {
@@ -53,4 +54,12 @@ void setOrderBy(OrderBy orderBy) {
public Content getFirst() {
return content.get(0);
}
+
+ public boolean hasRefinements() {
+ return refinements == null;
+ }
+
+ public List<Refinement> getRefinements() {
+ return refinements == null ? Collections.<Refinement>emptyList() : Collections.unmodifiableList(refinements);
+ }
}
@@ -65,7 +65,9 @@ public static ContentSearchQuery contentSearchQuery() {
*/
public static ContentSearchQuery contentSearchQueryWithRefinements() {
- throw new RuntimeException("Not implemented yet. TODO");
+ return container
+ .getComponent(ContentSearchQuery.class)
+ .configure(ConfigurationProfile.allTagsAndFieldsAndRefinements);
}
public static TagSearchQuery tagSearchQuery() {
@@ -0,0 +1,8 @@
+package com.gu.openplatform.contentapi;
+
+public class Refinement extends ApiObject {
+
+ protected Refinement(String id) {
+ super(id);
+ }
+}
@@ -24,7 +24,12 @@
allTagsAndFields(new PageSizeConfiguration(10),
new ShowAllTagsConfiguration(),
- new RetrieveAllFieldsConfiguration());
+ new RetrieveAllFieldsConfiguration()),
+
+ allTagsAndFieldsAndRefinements(new PageSizeConfiguration(10),
+ new ShowAllTagsConfiguration(),
+ new RetrieveAllFieldsConfiguration(),
+ new RetrieveAllRefinementsConfiguration());
private final List<SearchConfiguration> profiles;
@@ -0,0 +1,12 @@
+package com.gu.openplatform.contentapi.configuration.search;
+
+import com.gu.openplatform.contentapi.ContentSearchQuery;
+import com.gu.openplatform.contentapi.TagType;
+
+public class RetrieveAllRefinementsConfiguration implements SearchConfiguration<ContentSearchQuery> {
+
+ @Override
+ public void configure(ContentSearchQuery contentSearchClient) {
+ contentSearchClient.showRefinementsOfType(TagType.all);
+ }
+}
@@ -46,6 +46,16 @@ public void testCanSearchForContent() throws Exception {
}
@Test
+ public void testCanRetrieveRefinements() throws Exception {
+ results = GuardianContentApi.contentSearchQueryWithRefinements().execute();
+
+ assertTrue(results.hasRefinements());
+
+ for (Refinement refinement : results.getRefinements())
+ RefinementValidator.validateRefinements(refinement);
+ }
+
+ @Test
public void testCanPaginateThroughResults() throws Exception {
results = GuardianContentApi.minimalContentSearchQuery().execute();
@@ -0,0 +1,19 @@
+package com.gu.openplatform.contentapi;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import static org.hamcrest.Matchers.notNullValue;
+import static org.junit.Assert.assertThat;
+
+public abstract class RefinementValidator {
+
+ private static final Logger logger = LoggerFactory.getLogger(RefinementValidator.class);
+
+ public static void validateRefinements(Refinement refinement) {
+ if (logger.isTraceEnabled())
+ logger.trace("Checking refinement " + refinement);
+
+ assertThat(refinement.getId(), notNullValue());
+ }
+}
@@ -25,7 +25,9 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
public class SearchClientConfigurationTest {
@@ -39,6 +41,16 @@ public void setUp() throws Exception {
}
@Test
+ public void testCanConfigureRefinements() throws Exception {
+ assertThat(contentSearchQuery.getRefinementsToReturn().size(), equalTo(0));
+
+ contentSearchQuery.showRefinementsOfType(TagType.keyword);
+
+ assertThat(contentSearchQuery.getRefinementsToReturn().size(), equalTo(1));
+ assertTrue(contentSearchQuery.getRefinementsToReturn().contains(TagType.keyword));
+ }
+
+ @Test
public void testCanCongfigureAllFields() throws Exception {
assertFalse(contentSearchQuery.isRetrievingAllFields());
contentSearchQuery.configure(new RetrieveAllFieldsConfiguration());

0 comments on commit faf18d8

Please sign in to comment.