Skip to content

Commit

Permalink
Merge pull request #567 from colbya/CreateOrUpdateDeleteOrg
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreBtz authored Mar 20, 2023
2 parents 6d89fa2 + 6f5bcf8 commit 41281aa
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/main/java/org/zendesk/client/v2/Zendesk.java
Original file line number Diff line number Diff line change
Expand Up @@ -1382,6 +1382,11 @@ public ListenableFuture<JobStatus> createOrganizationsAsync(List<Organization> o
Collections.singletonMap("organizations", organizations))), handleJobStatus());
}

public Organization createOrUpdateOrganization(Organization organization) {
return complete(submit(req("POST", cnst("/organizations/create_or_update.json"), JSON, json(
Collections.singletonMap("organization", organization))), handle(Organization.class, "organization")));
}

public Organization updateOrganization(Organization organization) {
checkHasId(organization);
return complete(submit(req("PUT", tmpl("/organizations/{id}.json").set("id", organization.getId()), JSON, json(
Expand Down Expand Up @@ -1410,6 +1415,11 @@ public void deleteOrganization(long id) {
complete(submit(req("DELETE", tmpl("/organizations/{id}.json").set("id", id)), handleStatus()));
}

public JobStatus deleteOrganizations(long... ids) {
return complete(submit(req("DELETE", tmpl("/organizations/destroy_many.json{?ids}").set("ids", ids)),
handleJobStatus()));
}

public Iterable<Organization> lookupOrganizationsByExternalId(String externalId) {
if (externalId == null || externalId.length() < 2) {
throw new IllegalArgumentException("Name must be at least 2 characters long");
Expand Down
60 changes: 60 additions & 0 deletions src/test/java/org/zendesk/client/v2/RealSmokeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1439,6 +1439,66 @@ public void updateOrganizations() throws Exception {
}
}

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

final List<Organization> organizations = createTestOrganizationsInZendesk();
final long[] ids = organizations.stream().mapToLong(Organization::getId).toArray();
final JobStatus status = waitJobCompletion(instance.deleteOrganizations(ids));

assertThat("Job is completed", status.getStatus(), is(JobStatus.JobStatusEnum.completed));
assertThat("The good number of organizations were processed", status.getTotal(), is(organizations.size()));
assertThat("We have a result for each organization", status.getResults(), hasSize(organizations.size()));
assertThat("Job reports that the same organization requested to be deleted were deleted",
status.getResults()
.stream()
.map(JobResult::getId)
.collect(Collectors.toList()),
containsInAnyOrder(Arrays.stream(ids).boxed().toArray()));
}

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

String name = "testCreateOrUpdateOrganization";
String externalId = "testCreateOrUpdateOrganization";

// Clean up to avoid conflicts
for (Organization o : instance.lookupOrganizationsByExternalId(externalId)) {
instance.deleteOrganization(o.getId());
}

String noteAtCreation = "This is the initial organization note.";
Organization org = new Organization();
org.setExternalId(externalId);
org.setName(name);
org.setNotes(noteAtCreation);

Organization createResult = instance.createOrUpdateOrganization(org);
assertNotNull(createResult);
assertNotNull(createResult.getId());
assertEquals(name, createResult.getName());
assertEquals(externalId, createResult.getExternalId());
assertEquals(noteAtCreation, createResult.getNotes());

String noteAtUpdate = "This is the updated organization note.";
Organization updateOrg = new Organization();
updateOrg.setId(createResult.getId());
updateOrg.setExternalId(externalId);
updateOrg.setNotes(noteAtUpdate);

Organization updateResult = instance.createOrUpdateOrganization(updateOrg);
assertNotNull(updateResult);
assertEquals(createResult.getId(), updateResult.getId());
assertEquals(name, updateResult.getName());
assertEquals(externalId, updateResult.getExternalId());
assertEquals(noteAtUpdate, updateResult.getNotes());

instance.deleteOrganization(updateResult);
}

@Test
public void createOrganizationMemberships() throws Exception {
createClientWithTokenOrPassword();
Expand Down

0 comments on commit 41281aa

Please sign in to comment.