Skip to content

Commit e014340

Browse files
fix: don't put empty strings for nulls into meta when creating TaskInformation
Closes #168
1 parent b5d6543 commit e014340

File tree

5 files changed

+110
-111
lines changed

5 files changed

+110
-111
lines changed

engine-adapter/c7-embedded-core/src/main/kotlin/dev/bpmcrafters/processengineapi/adapter/c7/embedded/task/delivery/TaskInformationExtensions.kt

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,15 @@ import dev.bpmcrafters.processengineapi.CommonRestrictions
44
import dev.bpmcrafters.processengineapi.task.TaskInformation
55
import org.camunda.bpm.engine.delegate.DelegateTask
66
import org.camunda.bpm.engine.externaltask.LockedExternalTask
7-
import org.camunda.bpm.engine.impl.persistence.entity.ExternalTaskEntity
8-
import org.camunda.bpm.engine.impl.persistence.entity.TaskEntity
97
import org.camunda.bpm.engine.task.IdentityLink
108
import org.camunda.bpm.engine.task.Task
11-
import java.time.Instant
12-
import java.time.OffsetDateTime
13-
import java.time.ZoneOffset
149
import java.util.*
1510

1611
fun Task.toTaskInformation(candidates: Set<IdentityLink>, processDefinitionKey: String? = null) =
1712
TaskInformation(
1813
taskId = this.id,
19-
meta = mapOf(
14+
meta = metaOf(
15+
CommonRestrictions.PROCESS_DEFINITION_KEY to processDefinitionKey,
2016
CommonRestrictions.PROCESS_DEFINITION_ID to this.processDefinitionId,
2117
CommonRestrictions.ACTIVITY_ID to this.taskDefinitionKey,
2218
CommonRestrictions.TENANT_ID to this.tenantId,
@@ -31,19 +27,13 @@ fun Task.toTaskInformation(candidates: Set<IdentityLink>, processDefinitionKey:
3127
"candidateUsers" to candidates.toUsersString(),
3228
"candidateGroups" to candidates.toGroupsString(),
3329
"lastUpdatedDate" to this.lastUpdated.toDateString()
34-
).let {
35-
if (processDefinitionKey != null) {
36-
it + (CommonRestrictions.PROCESS_DEFINITION_KEY to processDefinitionKey)
37-
} else {
38-
it
39-
}
40-
}
30+
)
4131
)
4232

4333
fun DelegateTask.toTaskInformation() =
4434
TaskInformation(
4535
taskId = this.id,
46-
meta = mapOf(
36+
meta = metaOf(
4737
CommonRestrictions.PROCESS_DEFINITION_ID to this.processDefinitionId,
4838
CommonRestrictions.ACTIVITY_ID to this.taskDefinitionKey,
4939
CommonRestrictions.TENANT_ID to this.tenantId,
@@ -63,33 +53,42 @@ fun DelegateTask.toTaskInformation() =
6353
fun LockedExternalTask.toTaskInformation(): TaskInformation =
6454
TaskInformation(
6555
taskId = this.id,
66-
meta = mapOf(
56+
meta = metaOf(
6757
CommonRestrictions.ACTIVITY_ID to this.activityId,
6858
CommonRestrictions.PROCESS_DEFINITION_ID to this.processDefinitionId,
6959
CommonRestrictions.PROCESS_DEFINITION_KEY to this.processDefinitionKey,
7060
CommonRestrictions.PROCESS_INSTANCE_ID to this.processInstanceId,
7161
CommonRestrictions.TENANT_ID to this.tenantId,
7262
"topicName" to this.topicName,
7363
"creationDate" to this.createTime.toDateString(),
74-
TaskInformation.RETRIES to (this.retries?.toString() ?: ""),
64+
TaskInformation.RETRIES to this.retries?.toString(),
7565
)
7666
)
7767

7868
/**
7969
* Converts engine internal representation into a string.
8070
*/
81-
fun Date?.toDateString() = this?.toInstant()?.toIso8601() ?: ""
82-
83-
/**
84-
* Converts to offset date time in ISO8601 in UTC.
85-
*/
86-
fun Instant.toIso8601() = OffsetDateTime.ofInstant(this, ZoneOffset.UTC).toString()
71+
fun Date?.toDateString() = this?.toInstant()?.toString()
8772

8873
/**
8974
* Extracts candidates groups as a comma-separated string.
9075
*/
9176
fun Set<IdentityLink>.toGroupsString() = this.mapNotNull { it.groupId }.sorted().joinToString(",")
77+
9278
/**
9379
* Extracts candidates users as a comma-separated string.
9480
*/
9581
fun Set<IdentityLink>.toUsersString() = this.mapNotNull { it.userId }.sorted().joinToString(",")
82+
83+
/**
84+
* Creates a map of the provided pairs.
85+
*
86+
* If the 2nd component of a pair is `null`, the pair is dropped and not added to the resulting map.
87+
*/
88+
fun metaOf(vararg pairs: Pair<String, String?>): Map<String, String> =
89+
sequenceOf(*pairs)
90+
.filter { it.second != null }
91+
.associate {
92+
@Suppress("UNCHECKED_CAST")
93+
it as Pair<String, String>
94+
}

engine-adapter/c7-embedded-core/src/test/kotlin/dev/bpmcrafters/processengineapi/adapter/c7/embedded/task/delivery/TaskInformationExtensionsKtTest.kt

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,20 @@ import org.camunda.bpm.engine.impl.persistence.entity.IdentityLinkEntity
66
import org.camunda.bpm.engine.task.IdentityLink
77
import org.camunda.community.mockito.delegate.DelegateTaskFake
88
import org.camunda.community.mockito.task.TaskFake
9-
import org.junit.jupiter.api.Test
9+
import org.junit.jupiter.params.ParameterizedTest
10+
import org.junit.jupiter.params.provider.NullSource
11+
import org.junit.jupiter.params.provider.ValueSource
1012
import java.time.Instant
1113
import java.util.*
1214

1315
class TaskInformationExtensionsKtTest {
1416

15-
@Test
16-
fun `should map Task`() {
17+
@ParameterizedTest
18+
@NullSource
19+
@ValueSource(strings = ["Sat, 12 Aug 1995 13:30:00 GMT+0430"])
20+
fun `should map Task`(maybeNullDate: Date?) {
1721
val now = Date.from(Instant.now())
22+
1823
val task = TaskFake.builder()
1924
.id("taskId")
2025
.processDefinitionId("processDefinitionId")
@@ -25,10 +30,10 @@ class TaskInformationExtensionsKtTest {
2530
.description("description")
2631
.assignee("assignee")
2732
.createTime(now)
28-
.followUpDate(now)
29-
.dueDate(now)
33+
.followUpDate(maybeNullDate)
34+
.dueDate(maybeNullDate)
3035
.formKey("formKey")
31-
.lastUpdated(now)
36+
.lastUpdated(maybeNullDate)
3237
.build()
3338

3439
val identityLinks =
@@ -45,16 +50,24 @@ class TaskInformationExtensionsKtTest {
4550
assertThat(taskInformation.meta["taskDescription"]).isEqualTo("description")
4651
assertThat(taskInformation.meta["assignee"]).isEqualTo("assignee")
4752
assertThat(taskInformation.meta["creationDate"]).isEqualTo(now.toDateString())
48-
assertThat(taskInformation.meta["followUpDate"]).isEqualTo(now.toDateString())
49-
assertThat(taskInformation.meta["dueDate"]).isEqualTo(now.toDateString())
5053
assertThat(taskInformation.meta["formKey"]).isEqualTo("formKey")
5154
assertThat(taskInformation.meta["candidateUsers"]).isEqualTo("user-1,user-2")
5255
assertThat(taskInformation.meta["candidateGroups"]).isEqualTo("group")
53-
assertThat(taskInformation.meta["lastUpdatedDate"]).isEqualTo(now.toDateString())
56+
if (maybeNullDate == null) {
57+
assertThat(taskInformation.meta).doesNotContainKey("followUpDate")
58+
assertThat(taskInformation.meta).doesNotContainKey("dueDate")
59+
assertThat(taskInformation.meta).doesNotContainKey("lastUpdatedDate")
60+
} else {
61+
assertThat(taskInformation.meta["followUpDate"]).isEqualTo(maybeNullDate.toDateString())
62+
assertThat(taskInformation.meta["dueDate"]).isEqualTo(maybeNullDate.toDateString())
63+
assertThat(taskInformation.meta["lastUpdatedDate"]).isEqualTo(maybeNullDate.toDateString())
64+
}
5465
}
5566

56-
@Test
57-
fun `should map DelegateTask`() {
67+
@ParameterizedTest
68+
@NullSource
69+
@ValueSource(strings = ["Sat, 12 Aug 1995 13:30:00 GMT+0430"])
70+
fun `should map DelegateTask`(maybeNullDate: Date?) {
5871
val now = Date.from(Instant.now())
5972

6073
var delegateTask = DelegateTaskFake("taskId")
@@ -66,9 +79,9 @@ class TaskInformationExtensionsKtTest {
6679
delegateTask = delegateTask.withDescription("description")
6780
delegateTask = delegateTask.withAssignee("assignee")
6881
delegateTask = delegateTask.withCreateTime(now)
69-
delegateTask = delegateTask.withFollowUpDate(now)
70-
delegateTask = delegateTask.withLastUpdated(now)
71-
delegateTask.dueDate = now
82+
delegateTask = delegateTask.withFollowUpDate(maybeNullDate)
83+
delegateTask = delegateTask.withLastUpdated(maybeNullDate)
84+
delegateTask.dueDate = maybeNullDate
7285
delegateTask.addGroupIdentityLink("group-1", "CANDIDATE")
7386
delegateTask.addGroupIdentityLink("group-2", "CANDIDATE")
7487
delegateTask.addUserIdentityLink("user-1", "CANDIDATE")
@@ -84,13 +97,18 @@ class TaskInformationExtensionsKtTest {
8497
assertThat(taskInformation.meta["taskDescription"]).isEqualTo("description")
8598
assertThat(taskInformation.meta["assignee"]).isEqualTo("assignee")
8699
assertThat(taskInformation.meta["creationDate"]).isEqualTo(now.toDateString())
87-
assertThat(taskInformation.meta["followUpDate"]).isEqualTo(now.toDateString())
88-
assertThat(taskInformation.meta["dueDate"]).isEqualTo(now.toDateString())
89100
assertThat(taskInformation.meta["formKey"]).isNull()
90101
assertThat(taskInformation.meta["candidateUsers"]).isEqualTo("user-1,user-2")
91102
assertThat(taskInformation.meta["candidateGroups"]).isEqualTo("group-1,group-2")
92-
assertThat(taskInformation.meta["lastUpdatedDate"]).isEqualTo(now.toDateString())
93-
103+
if (maybeNullDate == null) {
104+
assertThat(taskInformation.meta).doesNotContainKey("followUpDate")
105+
assertThat(taskInformation.meta).doesNotContainKey("dueDate")
106+
assertThat(taskInformation.meta).doesNotContainKey("lastUpdatedDate")
107+
} else {
108+
assertThat(taskInformation.meta["followUpDate"]).isEqualTo(maybeNullDate.toDateString())
109+
assertThat(taskInformation.meta["dueDate"]).isEqualTo(maybeNullDate.toDateString())
110+
assertThat(taskInformation.meta["lastUpdatedDate"]).isEqualTo(maybeNullDate.toDateString())
111+
}
94112
}
95113

96114
private fun identityLink(userId: String? = null, groupId: String? = null): IdentityLink {
@@ -100,5 +118,4 @@ class TaskInformationExtensionsKtTest {
100118
return identityLink
101119
}
102120

103-
104121
}

engine-adapter/c7-remote-core/src/main/kotlin/dev/bpmcrafters/processengineapi/adapter/c7/remote/task/delivery/TaskInformationExtensions.kt

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,22 @@ import java.util.*
1515
fun LockedExternalTaskDto.toTaskInformation(pdMetaDataResolver: ProcessDefinitionMetaDataResolver): TaskInformation =
1616
TaskInformation(
1717
taskId = this.id!!,
18-
meta = mapOf(
18+
meta = metaOf(
1919
CommonRestrictions.ACTIVITY_ID to this.activityId,
2020
CommonRestrictions.PROCESS_DEFINITION_ID to this.processDefinitionId,
2121
CommonRestrictions.PROCESS_DEFINITION_KEY to this.processDefinitionKey,
2222
CommonRestrictions.PROCESS_INSTANCE_ID to this.processInstanceId,
2323
CommonRestrictions.TENANT_ID to this.tenantId,
2424
"topicName" to this.topicName,
2525
"creationDate" to this.createTime.toDateString(),
26-
TaskInformation.RETRIES to (this.retries?.toString() ?: ""),
26+
TaskInformation.RETRIES to this.retries?.toString(),
2727
).enrichWithProcessDefinitionMetadata(this.processDefinitionId!!, pdMetaDataResolver)
2828
)
2929

3030
fun TaskWithAttachmentAndCommentDto.toTaskInformation(candidates: Set<IdentityLinkDto>, pdMetaDataResolver: ProcessDefinitionMetaDataResolver) =
3131
TaskInformation(
3232
taskId = this.id!!,
33-
meta = mapOf(
33+
meta = metaOf(
3434
CommonRestrictions.ACTIVITY_ID to this.taskDefinitionKey,
3535
CommonRestrictions.TENANT_ID to this.tenantId,
3636
CommonRestrictions.PROCESS_DEFINITION_ID to this.processDefinitionId,
@@ -68,17 +68,12 @@ fun Map<String, String>.enrichWithProcessDefinitionMetadata(processDefinitionId:
6868
/**
6969
* Converts engine internal representation into a string.
7070
*/
71-
fun Date?.toDateString() = this?.toInstant()?.toIso8601() ?: ""
71+
fun Date?.toDateString() = this?.toInstant()?.toString()
7272

7373
/**
7474
* Converts offset date time to string representation in ISO8601 in UTC.
7575
*/
76-
fun OffsetDateTime?.toDateString() = this?.atZoneSameInstant(ZoneOffset.UTC)?.toString() ?: ""
77-
78-
/**
79-
* Converts to offset date time in ISO8601 in UTC.
80-
*/
81-
fun Instant.toIso8601() = OffsetDateTime.ofInstant(this, ZoneOffset.UTC).toString()
76+
fun OffsetDateTime?.toDateString() = this?.atZoneSameInstant(ZoneOffset.UTC)?.toString()
8277

8378
/**
8479
* Extracts candidates groups as a comma-separated string.
@@ -90,6 +85,18 @@ fun Set<IdentityLinkDto>.toGroupsString() = this.mapNotNull { it.groupId }.sorte
9085
*/
9186
fun Set<IdentityLinkDto>.toUsersString() = this.mapNotNull { it.userId }.sorted().joinToString(",")
9287

88+
/**
89+
* Creates a map of the provided pairs.
90+
*
91+
* If the 2nd component of a pair is `null`, the pair is dropped and not added to the resulting map.
92+
*/
93+
fun metaOf(vararg pairs: Pair<String, String?>): Map<String, String> =
94+
sequenceOf(*pairs)
95+
.filter { it.second != null }
96+
.associate {
97+
@Suppress("UNCHECKED_CAST")
98+
it as Pair<String, String>
99+
}
93100

94101
fun <T : Any> Map<String, T>.filterBySubscription(subscription: TaskSubscriptionHandle): Map<String, T> =
95102
if (subscription.payloadDescription != null) {
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package dev.bpmcrafters.processengineapi.adapter.c7.remote.task.delivery.subscribe
22

33
import dev.bpmcrafters.processengineapi.CommonRestrictions
4+
import dev.bpmcrafters.processengineapi.adapter.c7.remote.task.delivery.metaOf
45
import dev.bpmcrafters.processengineapi.adapter.c7.remote.task.delivery.toDateString
56
import dev.bpmcrafters.processengineapi.task.TaskInformation
67
import org.camunda.bpm.client.task.ExternalTask
78

89
fun ExternalTask.toTaskInformation(): TaskInformation = TaskInformation(
910
taskId = this.id,
10-
meta = mapOf(
11+
meta = metaOf(
1112
CommonRestrictions.ACTIVITY_ID to this.activityId,
1213
CommonRestrictions.PROCESS_DEFINITION_KEY to this.processDefinitionKey,
1314
CommonRestrictions.PROCESS_INSTANCE_ID to this.processInstanceId,
@@ -16,6 +17,6 @@ fun ExternalTask.toTaskInformation(): TaskInformation = TaskInformation(
1617
CommonRestrictions.TENANT_ID to this.tenantId,
1718
"topicName" to this.topicName,
1819
"creationDate" to this.createTime.toDateString(),
19-
TaskInformation.RETRIES to (this.retries?.toString() ?: ""),
20+
TaskInformation.RETRIES to this.retries?.toString(),
2021
)
2122
)

0 commit comments

Comments
 (0)