Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#307 Add a sorting order for the comment retrieval #308

Merged
merged 1 commit into from
Nov 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -48,6 +48,7 @@
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.CoreMatchers.nullValue;
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 @@ -1084,4 +1085,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());
}
}
}
}