Skip to content

Commit

Permalink
Add a check for whether the tenant id is set or not
Browse files Browse the repository at this point in the history
  • Loading branch information
filiphr committed Oct 9, 2023
1 parent 2d5391f commit 476ad3b
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class ThreadLocalTenantContext implements TenantContext {
protected final ThreadLocal<String> tenantId = ThreadLocal.withInitial(() -> {
throw new FlowableException("Tenant value has not been set");
});
protected final ThreadLocal<Boolean> tenantIdSet = ThreadLocal.withInitial(() -> Boolean.FALSE);

@Override
public String getTenantId() {
Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}

0 comments on commit 476ad3b

Please sign in to comment.