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

Add Ticket properties to support safe update #630

Merged
merged 3 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
33 changes: 33 additions & 0 deletions src/main/java/org/zendesk/client/v2/model/Ticket.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.zendesk.client.v2.model;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.Date;
import java.util.List;
Expand Down Expand Up @@ -32,6 +34,7 @@ public class Ticket extends Request implements SearchResultEntity {
private Long ticketFormId;
private Long brandId;
private Boolean isPublic;
private Boolean safeUpdate;

public Ticket() {}

Expand Down Expand Up @@ -218,6 +221,34 @@ public void setIsPublic(Boolean isPublic) {
this.isPublic = isPublic;
}

/**
* The safe_update & update_stamp parameters are used by the Zendesk API to protect against update
PierreBtz marked this conversation as resolved.
Show resolved Hide resolved
* collisions. If the safe_update parameter is set to true and Zendesk detects that a ticket has
* been updated since the time specified in update_stamp then it will return an HTTP CONFLICT
* status to let the caller know that the ticket was not successfully updated.
*
* <p>These properties are annotated with JsonInclude(Include.NON_DEFAULT) so that they will only
* be serialized to JSON if safeUpdate is set to TRUE.
*
* <p>For more information see Zendesk documentation at:
* https://developer.zendesk.com/documentation/ticketing/managing-tickets/creating-and-updating-tickets/#protecting-against-ticket-update-collisions
*/
@JsonInclude(Include.NON_DEFAULT)
@JsonProperty("safe_update")
public Boolean getSafeUpdate() {
return safeUpdate;
}

public void setSafeUpdate(Boolean safeUpdate) {
this.safeUpdate = safeUpdate;
}

@JsonInclude(Include.NON_DEFAULT)
@JsonProperty("updated_stamp")
private Date getUpdatedStamp() {
return Boolean.TRUE.equals(safeUpdate) ? updatedAt : null;
}

@Override
public String toString() {
return "Ticket"
Expand Down Expand Up @@ -285,6 +316,8 @@ public String toString() {
+ brandId
+ ", isPublic="
+ isPublic
+ ", safeUpdate="
+ safeUpdate
+ ", createdAt="
+ createdAt
+ ", updatedAt="
Expand Down
54 changes: 54 additions & 0 deletions src/test/java/org/zendesk/client/v2/model/TicketTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package org.zendesk.client.v2.model;

import static org.assertj.core.api.Assertions.assertThat;

import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;
import org.junit.Test;
import org.zendesk.client.v2.Zendesk;

public class TicketTest {

private static final Random RANDOM = new Random();
private static final String TICKET_COMMENT1 = "Please ignore this ticket";
private static final Date NOW = Calendar.getInstance().getTime();

@Test
public void serializeWithNullSafeUpdate() throws Exception {
ObjectMapper mapper = Zendesk.createMapper();
Ticket ticket = createSampleTicket();
assertThat(mapper.writeValueAsString(ticket))
.doesNotContain("\"safe_update\"")
.doesNotContain("\"updated_stamp\"");
}

@Test
public void serializeWithFalseSafeUpdate() throws Exception {
ObjectMapper mapper = Zendesk.createMapper();
Ticket ticket = createSampleTicket();
ticket.setSafeUpdate(false);
assertThat(mapper.writeValueAsString(ticket))
.doesNotContain("\"safe_update\"")
.doesNotContain("\"updated_stamp\"");
}

@Test
public void serializeWithSafeUpdate() throws Exception {
ObjectMapper mapper = Zendesk.createMapper();
Ticket ticket = createSampleTicket();
ticket.setSafeUpdate(true);
assertThat(mapper.writeValueAsString(ticket))
.contains("\"safe_update\"")
.contains("\"updated_stamp\"");
}

private Ticket createSampleTicket() {
Ticket ticket = new Ticket();
ticket.setId(Math.abs(RANDOM.nextLong()));
ticket.setComment(new Comment(TICKET_COMMENT1));
ticket.setUpdatedAt(NOW);
return ticket;
}
}
Loading