Skip to content

Commit

Permalink
Merge pull request #32 from knocklabs/franco-kno-5026-java-sdk-add-te…
Browse files Browse the repository at this point in the history
…nant-inline-identification-support

feat(KNO-5026): Add tenant inline identification
  • Loading branch information
francoborr authored Jan 24, 2024
2 parents 915c300 + dd63b8d commit 1a59dc0
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 5 deletions.
13 changes: 11 additions & 2 deletions src/main/java/app/knock/api/model/CreateSchedulesRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Builder;
import lombok.Getter;
import lombok.Singular;
import lombok.Value;
import lombok.extern.jackson.Jacksonized;
Expand All @@ -28,7 +27,7 @@ public class CreateSchedulesRequest {
List<Object> recipients;
List<ScheduleRepeat> repeats;
Object actor;
String tenant;
Object tenant;
ZonedDateTime scheduledAt;

@Singular("data")
Expand Down Expand Up @@ -62,6 +61,16 @@ public CreateSchedulesRequestBuilder addActor(Map<String, Object> actor) {
return this;
}

public CreateSchedulesRequestBuilder addTenant(String tenant) {
this.tenant = tenant;
return this;
}

public CreateSchedulesRequestBuilder addTenant(Map<String, Object> tenant) {
this.tenant = tenant;
return this;
}

public CreateSchedulesRequestBuilder addRecipient(String... userIds) {
if (this.recipients == null) { this.recipients = new ArrayList<>(); }
Collections.addAll(this.recipients, userIds);
Expand Down
18 changes: 16 additions & 2 deletions src/main/java/app/knock/api/model/UpdateSchedulesRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Builder;
import lombok.Getter;
import lombok.Singular;
import lombok.Value;
import lombok.extern.jackson.Jacksonized;
Expand All @@ -27,7 +26,7 @@ public class UpdateSchedulesRequest {
List<String> scheduleIds;
List<ScheduleRepeat> repeats;
Object actor;
String tenant;
Object tenant;
ZonedDateTime scheduledAt;

@Singular("data")
Expand Down Expand Up @@ -56,6 +55,21 @@ public UpdateSchedulesRequestBuilder addActor(String actor) {
return this;
}

public UpdateSchedulesRequestBuilder addActor(Map<String, Object> actor) {
this.actor = actor;
return this;
}

public UpdateSchedulesRequestBuilder addTenant(String tenant) {
this.tenant = tenant;
return this;
}

public UpdateSchedulesRequestBuilder addTenant(Map<String, Object> tenant) {
this.tenant = tenant;
return this;
}

public UpdateSchedulesRequestBuilder addRepeat(ScheduleRepeat repeat) {
if (this.repeats == null) { this.repeats = new ArrayList<>(); }
Collections.addAll(this.repeats, repeat);
Expand Down
17 changes: 16 additions & 1 deletion src/main/java/app/knock/api/model/WorkflowTriggerRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class WorkflowTriggerRequest {
Object actor;
List<Object> recipients;
String cancellationKey;
String tenant;
Object tenant;

@Singular("data")
@JsonAnySetter
Expand Down Expand Up @@ -56,6 +56,21 @@ public WorkflowTriggerRequestBuilder addActor(String actor) {
return this;
}

public WorkflowTriggerRequestBuilder addActor(Map<String, Object> actor) {
this.actor = actor;
return this;
}

public WorkflowTriggerRequestBuilder addTenant(String tenant) {
this.tenant = tenant;
return this;
}

public WorkflowTriggerRequestBuilder addTenant(Map<String, Object> tenant) {
this.tenant = tenant;
return this;
}

public WorkflowTriggerRequestBuilder addRecipient(String... userIds) {
if (this.recipients == null) { this.recipients = new ArrayList<>(); }
Collections.addAll(this.recipients, userIds);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.junit.jupiter.api.Test;
import org.skyscreamer.jsonassert.JSONAssert;

import java.util.HashMap;
import java.util.Map;

public class WorkflowTriggerSerializeTests {
Expand Down Expand Up @@ -101,5 +102,78 @@ void mappedPropertiesShouldUnwrapActorObject() throws JSONException {
"}" +
"}}", json, false);
}

@Test
void mappedPropertiesShouldUnwrapActorAndTenantWithProperties() throws JSONException {
Map<String, Object> actorProperties = new HashMap<>();
actorProperties.put("id", "actor-1");
actorProperties.put("collection", "actor-collection");
actorProperties.put("name", "custom actor");
actorProperties.put("foo", "var");

Map<String, Object> tenantProperties = new HashMap<>();
tenantProperties.put("id", "tenant-1");
tenantProperties.put("name", "custom tenant");
tenantProperties.put("lala", "1234");

WorkflowTriggerRequest workflowTrigger = WorkflowTriggerRequest.builder()
.key("new-feature")
.actor(actorProperties)
.tenant(tenantProperties)
.cancellationKey("test")
.addRecipient("recipientId1", "recipientId2")
.addRecipient(Map.of("id", "user_3"))
.addRecipient(WorkflowTriggerRequest.ObjectRecipientIdentifier.builder()
.id("identifier_id")
.collection("collection")
.build())
.data("thing_1", "thing one value")
.data("thing_2", "thing two value")
.data("nested_thing", Map.of("nested_thing_1", 123.44, "nested_thing_2", 123))
.build();

String json = Json.writeString(workflowTrigger);

JSONAssert.assertEquals(
"{actor:{" +
"id: \"actor-1\"," +
"collection: \"actor-collection\"," +
"name: \"custom actor\"," +
"foo: \"var\"" +
"}}",
json,
false);
JSONAssert.assertEquals(
"{tenant:{" +
"id: \"tenant-1\"," +
"name: \"custom tenant\"," +
"lala: \"1234\"" +
"}}",
json,
false);

JSONAssert.assertEquals("{cancellation_key:\"test\"}", json, false);
JSONAssert.assertEquals("{" +
"recipients: [" +
"\"recipientId1\", " +
"\"recipientId2\", " +
" {" +
" id: \"identifier_id\", " +
" collection: \"collection\"" +
" }," +
" { id: \"user_3\"}" +
"]" +
"}", json, false);

JSONAssert.assertEquals("{" +
"data: {" +
"thing_1: \"thing one value\"," +
"thing_2: \"thing two value\"," +
"nested_thing: {" +
"nested_thing_1: 123.44," +
"nested_thing_2: 123" +
"}" +
"}}", json, false);
}

}

0 comments on commit 1a59dc0

Please sign in to comment.