|
20 | 20 | import java.util.Map; |
21 | 21 | import java.util.Set; |
22 | 22 |
|
| 23 | +import org.flowable.common.engine.impl.DefaultTenantProvider; |
23 | 24 | import org.flowable.common.engine.impl.interceptor.EngineConfigurationConstants; |
24 | 25 | import org.flowable.engine.ProcessEngineConfiguration; |
25 | 26 | import org.flowable.engine.repository.DeploymentBuilder; |
@@ -278,6 +279,101 @@ public void testStartProcessInstanceWithSameEventKeyDeployedInDifferentTenants() |
278 | 279 | .containsExactly("task tenant A", "task tenant B", "task tenant B"); |
279 | 280 | } |
280 | 281 |
|
| 282 | + @Test |
| 283 | + public void testChannelOfATenantOnlyStartsProcessInstancesInItsTenant() { |
| 284 | + deployProcessModel("startProcessInstanceSameKeyTenantA.bpmn20.xml", TENANT_A); |
| 285 | + deployProcessModel("startProcessInstanceSameKeyTenantB.bpmn20.xml", TENANT_B); |
| 286 | + |
| 287 | + // The channel of tenant A detects the tenant from the event, which can name another tenant, the default tenant or no tenant |
| 288 | + getEventRegistryEngineConfiguration().getExpressionManager().getBeans() |
| 289 | + .put("tenantAJsonPointerChannelAdapter", new TestInboundChannelAdapter(processEngineConfiguration.getObjectMapper())); |
| 290 | + getEventRepositoryService().createInboundChannelModelBuilder() |
| 291 | + .key("tenantAJsonPointerChannel") |
| 292 | + .resourceName("tenantAJsonPointerChannel.channel") |
| 293 | + .deploymentTenantId(TENANT_A) |
| 294 | + .channelAdapter("${tenantAJsonPointerChannelAdapter}") |
| 295 | + .jsonDeserializer() |
| 296 | + .fixedEventKey("sameKey") |
| 297 | + .detectEventTenantUsingJsonPointerExpression("/tenantId") |
| 298 | + .jsonFieldsMapDirectlyToPayload() |
| 299 | + .deploy(); |
| 300 | + TestInboundChannelAdapter channelAdapter = (TestInboundChannelAdapter) ((InboundChannelModel) getEventRepositoryService() |
| 301 | + .getChannelModelByKey("tenantAJsonPointerChannel", TENANT_A)).getInboundEventChannelAdapter(); |
| 302 | + |
| 303 | + channelAdapter.triggerEventForTenantId("customerA", TENANT_B); |
| 304 | + assertThat(runtimeService.createProcessInstanceQuery().processInstanceTenantId(TENANT_A).count()).isEqualTo(1); |
| 305 | + assertThat(runtimeService.createProcessInstanceQuery().processInstanceTenantId(TENANT_B).count()).isZero(); |
| 306 | + |
| 307 | + channelAdapter.triggerEventForTenantId("customerA", ProcessEngineConfiguration.NO_TENANT_ID); |
| 308 | + channelAdapter.triggerEventForTenantId("customerA", null); |
| 309 | + assertThat(runtimeService.createProcessInstanceQuery().processInstanceTenantId(TENANT_A).count()).isEqualTo(3); |
| 310 | + assertThat(runtimeService.createProcessInstanceQuery().processInstanceTenantId(TENANT_B).count()).isZero(); |
| 311 | + assertThat(runtimeService.createProcessInstanceQuery().count()).isEqualTo(3); |
| 312 | + } |
| 313 | + |
| 314 | + @Test |
| 315 | + public void testChannelOfATenantWithoutTenantDetectionOnlyStartsProcessInstancesInItsTenant() { |
| 316 | + deployProcessModel("startProcessInstanceSameKeyTenantA.bpmn20.xml", TENANT_A); |
| 317 | + deployProcessModel("startProcessInstanceSameKeyTenantB.bpmn20.xml", TENANT_B); |
| 318 | + |
| 319 | + // Without the tenant of the channel the event would have no tenant, which correlates it with the event subscriptions of all tenants |
| 320 | + getEventRegistryEngineConfiguration().getExpressionManager().getBeans() |
| 321 | + .put("tenantANoTenantDetectionChannelAdapter", new TestInboundChannelAdapter(processEngineConfiguration.getObjectMapper())); |
| 322 | + getEventRepositoryService().createInboundChannelModelBuilder() |
| 323 | + .key("tenantANoTenantDetectionChannel") |
| 324 | + .resourceName("tenantANoTenantDetectionChannel.channel") |
| 325 | + .deploymentTenantId(TENANT_A) |
| 326 | + .channelAdapter("${tenantANoTenantDetectionChannelAdapter}") |
| 327 | + .jsonDeserializer() |
| 328 | + .fixedEventKey("sameKey") |
| 329 | + .jsonFieldsMapDirectlyToPayload() |
| 330 | + .deploy(); |
| 331 | + TestInboundChannelAdapter channelAdapter = (TestInboundChannelAdapter) ((InboundChannelModel) getEventRepositoryService() |
| 332 | + .getChannelModelByKey("tenantANoTenantDetectionChannel", TENANT_A)).getInboundEventChannelAdapter(); |
| 333 | + |
| 334 | + channelAdapter.triggerEventForTenantId("customerA", TENANT_B); |
| 335 | + channelAdapter.triggerEventWithoutTenantId("customerA"); |
| 336 | + assertThat(runtimeService.createProcessInstanceQuery().processInstanceTenantId(TENANT_A).count()).isEqualTo(2); |
| 337 | + assertThat(runtimeService.createProcessInstanceQuery().processInstanceTenantId(TENANT_B).count()).isZero(); |
| 338 | + } |
| 339 | + |
| 340 | + @Test |
| 341 | + public void testChannelOfTheDefaultTenantStartsProcessInstancesInTheDetectedTenant() { |
| 342 | + deployProcessModel("startProcessInstanceSameKeyTenantA.bpmn20.xml", TENANT_A); |
| 343 | + deployProcessModel("startProcessInstanceSameKeyTenantB.bpmn20.xml", TENANT_B); |
| 344 | + |
| 345 | + // A channel in the default tenant is shared with the other tenants, so it gets events for the tenant it detects |
| 346 | + DefaultTenantProvider originalDefaultTenantProvider = getEventRegistryEngineConfiguration().getDefaultTenantProvider(); |
| 347 | + getEventRegistryEngineConfiguration().setDefaultTenantValue("defaultTenant"); |
| 348 | + try { |
| 349 | + getEventRegistryEngineConfiguration().getExpressionManager().getBeans() |
| 350 | + .put("defaultTenantChannelAdapter", new TestInboundChannelAdapter(processEngineConfiguration.getObjectMapper())); |
| 351 | + getEventRepositoryService().createInboundChannelModelBuilder() |
| 352 | + .key("defaultTenantChannel") |
| 353 | + .resourceName("defaultTenantChannel.channel") |
| 354 | + .deploymentTenantId("defaultTenant") |
| 355 | + .channelAdapter("${defaultTenantChannelAdapter}") |
| 356 | + .jsonDeserializer() |
| 357 | + .fixedEventKey("sameKey") |
| 358 | + .detectEventTenantUsingJsonPointerExpression("/tenantId") |
| 359 | + .jsonFieldsMapDirectlyToPayload() |
| 360 | + .deploy(); |
| 361 | + TestInboundChannelAdapter channelAdapter = (TestInboundChannelAdapter) ((InboundChannelModel) getEventRepositoryService() |
| 362 | + .getChannelModelByKey("defaultTenantChannel", "defaultTenant")).getInboundEventChannelAdapter(); |
| 363 | + |
| 364 | + channelAdapter.triggerEventForTenantId("customerA", TENANT_B); |
| 365 | + assertThat(runtimeService.createProcessInstanceQuery().processInstanceTenantId(TENANT_A).count()).isZero(); |
| 366 | + assertThat(runtimeService.createProcessInstanceQuery().processInstanceTenantId(TENANT_B).count()).isEqualTo(1); |
| 367 | + |
| 368 | + channelAdapter.triggerEventForTenantId("customerA", TENANT_A); |
| 369 | + assertThat(runtimeService.createProcessInstanceQuery().processInstanceTenantId(TENANT_A).count()).isEqualTo(1); |
| 370 | + assertThat(runtimeService.createProcessInstanceQuery().processInstanceTenantId(TENANT_B).count()).isEqualTo(1); |
| 371 | + |
| 372 | + } finally { |
| 373 | + getEventRegistryEngineConfiguration().setDefaultTenantProvider(originalDefaultTenantProvider); |
| 374 | + } |
| 375 | + } |
| 376 | + |
281 | 377 | @Test |
282 | 378 | public void testUniqueStartProcessInstanceWithSameEventKeyDeployedInDifferentTenants() { |
283 | 379 | deployProcessModel("startUniqueProcessInstanceSameKeyTenantA.bpmn20.xml", TENANT_A); |
|
0 commit comments