Skip to content

Commit a2ed724

Browse files
authored
Merge pull request #284 from dastultz/master
Add search by labels.
2 parents 5c3ca19 + 3337f10 commit a2ed724

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

src/main/java/org/zendesk/client/v2/Zendesk.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,16 @@ public Iterable<Article> getArticleFromSearch(String searchTerm, Long sectionId)
406406
.set("query", searchTerm).set("section", sectionId), handleList(Article.class, "results"));
407407
}
408408

409+
public Iterable<Article> getArticlesFromAnyLabels(List<String> labels) {
410+
return new PagedIterable<>(tmpl("/help_center/articles/search.json{?label_names}").set("label_names", labels),
411+
handleList(Article.class, "results"));
412+
}
413+
414+
public Iterable<Article> getArticlesFromAllLabels(List<String> labels) {
415+
return new PagedIterable<>(tmpl("/help_center/en-us/articles.json{?label_names}").set("label_names", labels),
416+
handleList(Article.class, "articles"));
417+
}
418+
409419
public List<ArticleAttachments> getAttachmentsFromArticle(Long articleID) {
410420
return complete(submit(req("GET", tmpl("/help_center/articles/{id}/attachments.json").set("id", articleID)),
411421
handleArticleAttachmentsList("article_attachments")));

src/test/java/org/zendesk/client/v2/RealSmokeTest.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.zendesk.client.v2;
22

3+
import org.hamcrest.core.IsCollectionContaining;
34
import org.junit.After;
45
import org.junit.BeforeClass;
56
import org.junit.Ignore;
@@ -42,8 +43,10 @@
4243
import java.util.Collections;
4344
import java.util.Date;
4445
import java.util.HashMap;
46+
import java.util.HashSet;
4547
import java.util.List;
4648
import java.util.Properties;
49+
import java.util.Set;
4750
import java.util.UUID;
4851
import java.util.stream.StreamSupport;
4952

@@ -848,6 +851,47 @@ public void getArticles() throws Exception {
848851
}
849852
}
850853

854+
@Test
855+
public void getArticlesFromAnyLabels() throws Exception {
856+
createClientWithTokenOrPassword();
857+
/*
858+
Given 3 articles
859+
Article 1 with title "SomeLabelOne" and label "SomeLabelA"
860+
Article 2 with title "SomeLabelTwo" and labels "SomeLabelB" and "SomeLabelC"
861+
Article 3 with title "SomeLabelThree" and label "SomeLabelD"
862+
When a search by labels "SomeLabelA", "SomeLabelB"
863+
Then we get Article 1 and Article 2 but not Article 3
864+
because Article 1 and 2 have at least one of the labels, Article 3 has none
865+
*/
866+
Iterable<Article> result = instance.getArticlesFromAnyLabels(Arrays.asList("SomeLabelA", "SomeLabelB"));
867+
Set<String> actualTitles = extractTitles(result);
868+
assertThat(actualTitles.size(), is(2));
869+
assertThat(actualTitles, IsCollectionContaining.hasItems("SomeLabelOne", "SomeLabelTwo"));
870+
}
871+
872+
@Test
873+
public void getArticlesFromAllLabels() throws Exception {
874+
createClientWithTokenOrPassword();
875+
/*
876+
Given 2 articles
877+
Article 1 with title "AllLabelOne" and label "AllLabelA"
878+
Article 2 with title "AllLabelTwo" and labels "AllLabelA" and "AllLabelB"
879+
When a search by labels "AllLabelA", "AllLabelB"
880+
Then we get Article 2 but not Article 1
881+
because Article 2 has both labels and Article 1 has only one
882+
*/
883+
Iterable<Article> result = instance.getArticlesFromAllLabels(Arrays.asList("AllLabelA", "AllLabelB"));
884+
Set<String> actualTitles = extractTitles(result);
885+
assertThat(actualTitles.size(), is(1));
886+
assertThat(actualTitles, IsCollectionContaining.hasItems("AllLabelTwo"));
887+
}
888+
889+
private Set<String> extractTitles(Iterable<Article> iter) {
890+
Set<String> result = new HashSet<>();
891+
iter.forEach(article -> result.add(article.getTitle()));
892+
return result;
893+
}
894+
851895
@Test
852896
public void getArticleSubscriptions() throws Exception {
853897
createClientWithTokenOrPassword();

0 commit comments

Comments
 (0)