Skip to content

Commit

Permalink
Merge pull request #308 from PierreBtz/pbeitz/dev
Browse files Browse the repository at this point in the history
#307 Add a sorting order for the comment retrieval
  • Loading branch information
aheritier authored Nov 22, 2019
2 parents f07a80d + eba57f1 commit 9fda174
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 2 deletions.
15 changes: 15 additions & 0 deletions src/main/java/org/zendesk/client/v2/SortOrder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.zendesk.client.v2;

public enum SortOrder {
ASCENDING("asc"), DESCENDING("desc");

private String queryParameter;

SortOrder(String queryParameter) {
this.queryParameter = queryParameter;
}

public String getQueryParameter() {
return queryParameter;
}
}
11 changes: 9 additions & 2 deletions src/main/java/org/zendesk/client/v2/Zendesk.java
Original file line number Diff line number Diff line change
Expand Up @@ -1048,8 +1048,15 @@ public Iterable<Comment> getRequestComments(long id) {
}

public Iterable<Comment> getTicketComments(long id) {
return new PagedIterable<>(tmpl("/tickets/{id}/comments.json").set("id", id),
handleList(Comment.class, "comments"));
return getTicketComments(id, SortOrder.ASCENDING);
}

public Iterable<Comment> getTicketComments(long id, SortOrder order) {
return new PagedIterable<>(
tmpl("/tickets/{id}/comments.json?sort_order={order}")
.set("id", id)
.set("order", order.getQueryParameter()),
handleList(Comment.class, "comments"));
}

public Comment getRequestComment(org.zendesk.client.v2.model.Request request, Comment comment) {
Expand Down
51 changes: 51 additions & 0 deletions src/test/java/org/zendesk/client/v2/RealSmokeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.lessThanOrEqualTo;
import static org.hamcrest.core.StringContains.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
Expand Down Expand Up @@ -1106,4 +1107,54 @@ public void createTicketForm() throws Exception {
assertEquals(givenName, createdForm.getRawName());
assertEquals(givenName, createdForm.getRawDisplayName());
}

@Test
public void getTicketCommentsShouldBeAscending() throws Exception {
createClientWithTokenOrPassword();

Ticket t = new Ticket(
new Ticket.Requester(config.getProperty("requester.name"), config.getProperty("requester.email")),
"This is an automated test ticket", new Comment("1"));
Ticket ticket = null;
try {
ticket = instance.createTicket(t);
instance.createComment(ticket.getId(), new Comment("2"));
Iterable<Comment> ticketCommentsIt = instance.getTicketComments(ticket.getId());
List<Comment> comments = new ArrayList<>();
ticketCommentsIt.forEach(comments::add);

assertThat(comments.size(), is(2));
assertThat(comments.get(0).getBody(), containsString("1"));
assertThat(comments.get(1).getBody(), containsString("2"));
} finally {
if (ticket != null) {
instance.deleteTicket(ticket.getId());
}
}
}

@Test
public void getTicketCommentsDescending() throws Exception {
createClientWithTokenOrPassword();

Ticket t = new Ticket(
new Ticket.Requester(config.getProperty("requester.name"), config.getProperty("requester.email")),
"This is an automated test ticket", new Comment("1"));
Ticket ticket = null;
try {
ticket = instance.createTicket(t);
instance.createComment(ticket.getId(), new Comment("2"));
Iterable<Comment> ticketCommentsIt = instance.getTicketComments(ticket.getId(), SortOrder.DESCENDING);
List<Comment> comments = new ArrayList<>();
ticketCommentsIt.forEach(comments::add);

assertThat(comments.size(), is(2));
assertThat(comments.get(0).getBody(), containsString("2"));
assertThat(comments.get(1).getBody(), containsString("1"));
} finally {
if (ticket != null) {
instance.deleteTicket(ticket.getId());
}
}
}
}

0 comments on commit 9fda174

Please sign in to comment.