From 476ad3b3b3d481aa9f63d88bde28d502ecd655b1 Mon Sep 17 00:00:00 2001 From: Filip Hrisafov Date: Mon, 9 Oct 2023 15:32:33 +0200 Subject: [PATCH] Add a check for whether the tenant id is set or not --- .../engine/api/tenant/TenantContext.java | 7 +++ .../impl/tenant/ThreadLocalTenantContext.java | 7 +++ .../tenant/ThreadLocalTenantContextTest.java | 57 +++++++++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 modules/flowable-engine-common/src/test/java/org/flowable/common/engine/impl/tenant/ThreadLocalTenantContextTest.java diff --git a/modules/flowable-engine-common-api/src/main/java/org/flowable/common/engine/api/tenant/TenantContext.java b/modules/flowable-engine-common-api/src/main/java/org/flowable/common/engine/api/tenant/TenantContext.java index 88a73b36316..5544e596ec1 100644 --- a/modules/flowable-engine-common-api/src/main/java/org/flowable/common/engine/api/tenant/TenantContext.java +++ b/modules/flowable-engine-common-api/src/main/java/org/flowable/common/engine/api/tenant/TenantContext.java @@ -38,4 +38,11 @@ public interface TenantContext { */ void clearTenantId(); + /** + * Flag indicating whether the tenant id is set. + * + * @return {@code true} if the tenant id can return a value for {@link #getTenantId()}, {@code false} otherwise + */ + boolean isTenantIdSet(); + } diff --git a/modules/flowable-engine-common/src/main/java/org/flowable/common/engine/impl/tenant/ThreadLocalTenantContext.java b/modules/flowable-engine-common/src/main/java/org/flowable/common/engine/impl/tenant/ThreadLocalTenantContext.java index 52490f43e46..347b8b893b2 100644 --- a/modules/flowable-engine-common/src/main/java/org/flowable/common/engine/impl/tenant/ThreadLocalTenantContext.java +++ b/modules/flowable-engine-common/src/main/java/org/flowable/common/engine/impl/tenant/ThreadLocalTenantContext.java @@ -23,6 +23,7 @@ public class ThreadLocalTenantContext implements TenantContext { protected final ThreadLocal tenantId = ThreadLocal.withInitial(() -> { throw new FlowableException("Tenant value has not been set"); }); + protected final ThreadLocal tenantIdSet = ThreadLocal.withInitial(() -> Boolean.FALSE); @Override public String getTenantId() { @@ -32,11 +33,17 @@ public String getTenantId() { @Override public void setTenantId(String tenantId) { this.tenantId.set(tenantId); + this.tenantIdSet.set(true); } @Override public void clearTenantId() { tenantId.remove(); + tenantIdSet.remove(); } + @Override + public boolean isTenantIdSet() { + return tenantIdSet.get(); + } } diff --git a/modules/flowable-engine-common/src/test/java/org/flowable/common/engine/impl/tenant/ThreadLocalTenantContextTest.java b/modules/flowable-engine-common/src/test/java/org/flowable/common/engine/impl/tenant/ThreadLocalTenantContextTest.java new file mode 100644 index 00000000000..1612393eedf --- /dev/null +++ b/modules/flowable-engine-common/src/test/java/org/flowable/common/engine/impl/tenant/ThreadLocalTenantContextTest.java @@ -0,0 +1,57 @@ +/* Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.flowable.common.engine.impl.tenant; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import org.flowable.common.engine.api.FlowableException; +import org.flowable.common.engine.api.tenant.TenantContext; +import org.junit.jupiter.api.Test; + +/** + * @author Filip Hrisafov + */ +class ThreadLocalTenantContextTest { + + protected TenantContext underTest = new ThreadLocalTenantContext(); + + @Test + void getTenantId() { + assertThatThrownBy(() -> underTest.getTenantId()) + .isExactlyInstanceOf(FlowableException.class) + .hasMessage("Tenant value has not been set"); + + underTest.setTenantId("acme"); + assertThat(underTest.getTenantId()).isEqualTo("acme"); + + underTest.setTenantId("muppets"); + assertThat(underTest.getTenantId()).isEqualTo("muppets"); + + underTest.clearTenantId(); + assertThatThrownBy(() -> underTest.getTenantId()) + .isExactlyInstanceOf(FlowableException.class) + .hasMessage("Tenant value has not been set"); + } + + @Test + void isTenantIdSet() { + assertThat(underTest.isTenantIdSet()).isFalse(); + + underTest.setTenantId("flowable"); + assertThat(underTest.isTenantIdSet()).isTrue(); + + underTest.clearTenantId(); + assertThat(underTest.isTenantIdSet()).isFalse(); + } +}