@Test @DisplayName("remember and restore current tenant") void rememberCurrentTenant() { TenantId previousTenant = generate(); CurrentTenant.set(previousTenant); TenantId newTenant = generate(); TenantAwareOperation op = createOperation(newTenant); assertEquals(previousTenant, CurrentTenant.get() .get()); op.execute(); assertEquals(newTenant, getTenantFromRun(op)); assertEquals(previousTenant, CurrentTenant.get() .get()); }
public void execute() { Optional<TenantId> remembered = CurrentTenant.get(); try { CurrentTenant.set(tenantId()); run(); } finally { if (remembered.isPresent()) { CurrentTenant.set(remembered.get()); } else { CurrentTenant.clear(); } } }
TenantAwareOperation extends TenantAware implements Runnable { public void execute() { Optional<TenantId> remembered = CurrentTenant.get(); try { CurrentTenant.set(tenantId()); run(); } finally { if (remembered.isPresent()) { CurrentTenant.set(remembered.get()); } else { CurrentTenant.clear(); } } } }
TenantAwareOperation extends TenantAware implements Runnable { public void execute() { Optional<TenantId> remembered = CurrentTenant.get(); try { CurrentTenant.set(tenantId()); run(); } finally { if (remembered.isPresent()) { CurrentTenant.set(remembered.get()); } else { CurrentTenant.clear(); } } } protected  TenantAwareOperation(); protected  TenantAwareOperation(TenantId tenantId); }
TenantAwareOperation extends TenantAware implements Runnable { public void execute() { Optional<TenantId> remembered = CurrentTenant.get(); try { CurrentTenant.set(tenantId()); run(); } finally { if (remembered.isPresent()) { CurrentTenant.set(remembered.get()); } else { CurrentTenant.clear(); } } } protected  TenantAwareOperation(); protected  TenantAwareOperation(TenantId tenantId); void execute(); }
TenantAwareOperation extends TenantAware implements Runnable { public void execute() { Optional<TenantId> remembered = CurrentTenant.get(); try { CurrentTenant.set(tenantId()); run(); } finally { if (remembered.isPresent()) { CurrentTenant.set(remembered.get()); } else { CurrentTenant.clear(); } } } protected  TenantAwareOperation(); protected  TenantAwareOperation(TenantId tenantId); void execute();  }