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 GET timezones endpoint, add ianaTimeZone field to User #594

Merged
merged 2 commits into from
Jul 7, 2023
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
6 changes: 6 additions & 0 deletions src/main/java/org/zendesk/client/v2/Zendesk.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import org.zendesk.client.v2.model.TicketImport;
import org.zendesk.client.v2.model.TicketPage;
import org.zendesk.client.v2.model.TicketResult;
import org.zendesk.client.v2.model.TimeZone;
import org.zendesk.client.v2.model.Topic;
import org.zendesk.client.v2.model.Trigger;
import org.zendesk.client.v2.model.TwitterMonitor;
Expand Down Expand Up @@ -2588,6 +2589,11 @@ private Uri buildContentTagsSearchUrl(int pageSize, String namePrefixFilter, Str
return cnst(uriBuilder.toString());
}

public List<TimeZone> getTimeZones(){
return complete(submit(req("GET", cnst("/time_zones.json")), handleList(TimeZone.class,
"time_zones")));
}

//////////////////////////////////////////////////////////////////////
// Helper methods
//////////////////////////////////////////////////////////////////////
Expand Down
76 changes: 76 additions & 0 deletions src/main/java/org/zendesk/client/v2/model/TimeZone.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package org.zendesk.client.v2.model;

import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.Objects;

/**
* @author nkharabaruk
* @since 22/06/2023 22:00
*/
public class TimeZone {

private String translatedName;
private String name;
private String ianaName;
private int offset;
private String formattedOffset;

@JsonProperty("translated_name")
public String getTranslatedName() {
return translatedName;
}

public void setTranslatedName(String translatedName) {
this.translatedName = translatedName;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@JsonProperty("iana_name")
public String getIanaName() {
return ianaName;
}

public void setIanaName(String ianaName) {
this.ianaName = ianaName;
}

public int getOffset() {
return offset;
}

public void setOffset(int offset) {
this.offset = offset;
}

@JsonProperty("formatted_offset")
public String getFormattedOffset() {
return formattedOffset;
}

public void setFormattedOffset(String formattedOffset) {
this.formattedOffset = formattedOffset;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
TimeZone timeZone = (TimeZone) o;
return offset == timeZone.offset && Objects.equals(translatedName, timeZone.translatedName) &&
Objects.equals(name, timeZone.name) && Objects.equals(ianaName, timeZone.ianaName) &&
Objects.equals(formattedOffset, timeZone.formattedOffset);
}

@Override
public int hashCode() {
return Objects.hash(translatedName, name, ianaName, offset, formattedOffset);
}
}
24 changes: 17 additions & 7 deletions src/main/java/org/zendesk/client/v2/model/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class User extends Collaborator implements SearchResultEntity, Serializab
private Long localeId;
private String locale;
private String timeZone;
private String ianaTimeZone;
private Date lastLoginAt;
private String phone;
private Boolean restrictedAgent;
Expand Down Expand Up @@ -291,6 +292,15 @@ public void setTimeZone(String timeZone) {
this.timeZone = timeZone;
}

@JsonProperty("iana_time_zone")
public String getIanaTimeZone() {
return ianaTimeZone;
}

public void setIanaTimeZone(String ianaTimeZone) {
this.ianaTimeZone = ianaTimeZone;
}

@JsonProperty("updated_at")
public Date getUpdatedAt() {
return updatedAt;
Expand Down Expand Up @@ -408,12 +418,12 @@ public boolean equals(Object o) {
Objects.equals(active, user.active) && Objects.equals(verified, user.verified) &&
Objects.equals(shared, user.shared) && Objects.equals(localeId, user.localeId) &&
Objects.equals(locale, user.locale) && Objects.equals(timeZone, user.timeZone) &&
Objects.equals(lastLoginAt, user.lastLoginAt) && Objects.equals(phone, user.phone) &&
Objects.equals(restrictedAgent, user.restrictedAgent) && Objects.equals(signature, user.signature) &&
Objects.equals(details, user.details) && Objects.equals(notes, user.notes) &&
Objects.equals(organizationId, user.organizationId) && role == user.role &&
Objects.equals(customRoleId, user.customRoleId) && Objects.equals(moderator, user.moderator) &&
ticketRestriction == user.ticketRestriction &&
Objects.equals(ianaTimeZone, user.ianaTimeZone) && Objects.equals(lastLoginAt, user.lastLoginAt) &&
Objects.equals(phone, user.phone) && Objects.equals(restrictedAgent, user.restrictedAgent) &&
Objects.equals(signature, user.signature) && Objects.equals(details, user.details) &&
Objects.equals(notes, user.notes) && Objects.equals(organizationId, user.organizationId)
&& role == user.role && Objects.equals(customRoleId, user.customRoleId)
&& Objects.equals(moderator, user.moderator) && ticketRestriction == user.ticketRestriction &&
Objects.equals(onlyPrivateComments, user.onlyPrivateComments) &&
Objects.equals(tags, user.tags) && Objects.equals(suspended, user.suspended) &&
Objects.equals(photo, user.photo) && Objects.equals(identities, user.identities) &&
Expand All @@ -427,7 +437,7 @@ public boolean equals(Object o) {
@Override
public int hashCode() {
return Objects.hash(id, url, externalId, alias, createdAt, updatedAt, active, verified, shared, localeId,
locale, timeZone, lastLoginAt, phone, restrictedAgent, signature, details, notes, organizationId,
locale, timeZone, ianaTimeZone, lastLoginAt, phone, restrictedAgent, signature, details, notes, organizationId,
role, customRoleId, moderator, ticketRestriction, onlyPrivateComments, tags, suspended, photo,
identities, remotePhotoUrl, userFields, chatOnly, sharedPhoneNumber, defaultGroupId, roleType,
twoFactorAuthEnabled, reportCsv);
Expand Down
18 changes: 17 additions & 1 deletion src/test/java/org/zendesk/client/v2/RealSmokeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,22 @@ public void createUsers() throws Exception {
}
}

@Test
public void createUser() throws Exception {
// given
createClientWithTokenOrPassword();
final User userToCreate = newTestUser();
userToCreate.setTimeZone("Pacific Time (US & Canada)");

// when
User createdUser = instance.createUser(userToCreate);

// then
assertThat("A unique ID must be set", createdUser.getId(), notNullValue());
assertEquals("Time Zone must be set", userToCreate.getTimeZone(), createdUser.getTimeZone());
assertEquals("Iana Time Zone must be automatically set", "America/Los_Angeles", createdUser.getIanaTimeZone());
}

@Test
public void updateUsers() throws Exception {
createClientWithTokenOrPassword();
Expand Down Expand Up @@ -1266,7 +1282,7 @@ public void getUsersById() throws Exception {
assertThat(user.getName(), notNullValue());
}
}

@Test
public void getUsersIncrementally() throws Exception {
createClientWithTokenOrPassword();
Expand Down
93 changes: 93 additions & 0 deletions src/test/java/org/zendesk/client/v2/model/TimeZoneTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package org.zendesk.client.v2.model;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.tomakehurst.wiremock.junit.WireMockClassRule;
import org.apache.commons.text.RandomStringGenerator;
import org.junit.*;
import org.zendesk.client.v2.Zendesk;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
import static org.assertj.core.api.Assertions.assertThat;

public class TimeZoneTest {

private static final String MOCK_URL_FORMATTED_STRING = "http://localhost:%d";
public static final RandomStringGenerator RANDOM_STRING_GENERATOR =
new RandomStringGenerator.Builder().withinRange('a', 'z').build();
private static final String MOCK_API_TOKEN = RANDOM_STRING_GENERATOR.generate(15);
private static final String MOCK_USERNAME = RANDOM_STRING_GENERATOR.generate(10).toLowerCase() + "@cloudbees.com";

@ClassRule
public static WireMockClassRule zendeskApiClass = new WireMockClassRule(options()
.dynamicPort()
.dynamicHttpsPort()
.usingFilesUnderClasspath("wiremock")
);

@Rule
public WireMockClassRule zendeskApiMock = zendeskApiClass;

private Zendesk client;
private final ObjectMapper objectMapper = Zendesk.createMapper();

@Before
public void setUp() {
int ephemeralPort = zendeskApiMock.port();
String hostname = String.format(MOCK_URL_FORMATTED_STRING, ephemeralPort);
client = new Zendesk.Builder(hostname)
.setUsername(MOCK_USERNAME)
.setToken(MOCK_API_TOKEN)
.build();
}

@After
public void closeClient() {
if (client != null) {
client.close();
}
client = null;
}

@Test
public void getTimeZones() throws JsonProcessingException {

TimeZone timeZone1 = new TimeZone();
timeZone1.setTranslatedName("Pacific Time (US & Canada)");
timeZone1.setName("Pacific Time (US & Canada)");
timeZone1.setIanaName("America/Los_Angeles");
timeZone1.setOffset(-420);
timeZone1.setFormattedOffset("GMT-07:00");

TimeZone timeZone2 = new TimeZone();
timeZone1.setTranslatedName("Kyiv");
timeZone1.setName("Kyiv");
timeZone1.setIanaName("Europe/Kiev");
timeZone1.setOffset(180);
timeZone1.setFormattedOffset("GMT+03:00");

String expectedJsonResponse = objectMapper.writeValueAsString(
Collections.singletonMap("time_zones", Arrays.asList(timeZone1, timeZone2)));

zendeskApiMock.stubFor(
get(
urlPathEqualTo("/api/v2/time_zones.json"))
.willReturn(ok()
.withBody(expectedJsonResponse)
)
);

List<TimeZone> timeZones = client.getTimeZones();

zendeskApiMock.verify(getRequestedFor(
urlPathEqualTo("/api/v2/time_zones.json"))
);

assertThat(timeZones).containsExactly(timeZone1, timeZone2);
}
}