Add AccessContext.asTenant and use where possible (#2838)

Signed-off-by: Avgustin Marinov <Avgustin.Marinov@bosch.com>
This commit is contained in:
Avgustin Marinov
2025-11-27 16:27:56 +02:00
committed by GitHub
parent f6f62db0ad
commit 42384b7e31
17 changed files with 184 additions and 97 deletions

View File

@@ -17,7 +17,7 @@ import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import java.time.Duration;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.awaitility.Awaitility;
@@ -63,8 +63,7 @@ class ConcurrentDistributionSetInvalidationTest extends AbstractJpaIntegrationTe
final Rollout rollout = createRollout(distributionSet);
final String tenant = AccessContext.tenant();
// run in new Thread so that the invalidation can be executed in
// parallel
// run in new Thread so that the invalidation can be executed in parallel
new Thread(() -> asSystemAsTenant(tenant, rolloutHandler::handleAll)).start();
// wait until at least one RolloutGroup is created, as this means that the thread has started and has acquired the lock
@@ -74,7 +73,7 @@ class ConcurrentDistributionSetInvalidationTest extends AbstractJpaIntegrationTe
.until(() -> asSystemAsTenant(tenant, () -> rolloutGroupManagement.findByRollout(rollout.getId(), PAGE).getSize() > 0));
final DistributionSetInvalidation distributionSetInvalidation = new DistributionSetInvalidation(
Collections.singletonList(distributionSet.getId()), ActionCancellationType.SOFT);
List.of(distributionSet.getId()), ActionCancellationType.SOFT);
assertThatExceptionOfType(StopRolloutException.class)
.as("Invalidation of distributionSet should throw an exception")
.isThrownBy(() -> distributionSetInvalidationManagement.invalidateDistributionSet(distributionSetInvalidation));

View File

@@ -12,6 +12,7 @@ package org.eclipse.hawkbit.repository.jpa.management;
import static org.assertj.core.api.Assertions.assertThat;
import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.hawkbit.auth.SpRole;
@@ -37,11 +38,15 @@ class SystemManagementTest extends AbstractJpaIntegrationTest {
*/
@Test
void findTenantsReturnsAllTenantsNotOnlyWhichLoggedIn() {
assertThat(systemManagement.findTenants(PAGE).getContent()).hasSize(1);
assertThat(listTenants()).hasSize(1);
createTestTenantsForSystemStatistics(2, 0, 0, 0);
assertThat(listTenants()).hasSize(3);
}
assertThat(systemManagement.findTenants(PAGE).getContent()).hasSize(3);
private List<String> listTenants() {
final List<String> tenants = new ArrayList<>();
systemManagement.forEachTenantAsSystem(tenants::add);
return tenants;
}
private void createTestTenantsForSystemStatistics(final int tenants, final int artifactSize, final int targets, final int updates) {

View File

@@ -11,11 +11,14 @@ package org.eclipse.hawkbit.repository.jpa.tenancy;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import static org.eclipse.hawkbit.context.AccessContext.asSystem;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.Callable;
import org.eclipse.hawkbit.context.AccessContext;
import org.eclipse.hawkbit.repository.exception.EntityNotFoundException;
import org.eclipse.hawkbit.repository.jpa.AbstractJpaIntegrationTest;
import org.eclipse.hawkbit.repository.model.DistributionSet;
@@ -59,8 +62,7 @@ class MultiTenancyEntityTest extends AbstractJpaIntegrationTest {
assertThat(findTargetsForTenant.getContent().get(0).getTenant().toUpperCase()).isEqualTo(tenant.toUpperCase());
final Page<? extends Target> findTargetsForAnotherTenant = findTargetsForTenant(anotherTenant);
assertThat(findTargetsForAnotherTenant).hasSize(1);
assertThat(findTargetsForAnotherTenant.getContent().get(0).getTenant().toUpperCase())
.isEqualTo(anotherTenant.toUpperCase());
assertThat(findTargetsForAnotherTenant.getContent().get(0).getTenant().toUpperCase()).isEqualTo(anotherTenant.toUpperCase());
}
/**
@@ -96,11 +98,9 @@ class MultiTenancyEntityTest extends AbstractJpaIntegrationTest {
final String controllerAnotherTenant = "anotherController";
createTargetForTenant(controllerAnotherTenant, anotherTenant);
assertThat(systemManagement.findTenants(PAGE)).as("Expected number if tenants before deletion is").hasSize(3);
assertThat(listTenants()).as("Expected number if tenants before deletion is").hasSize(3);
systemManagement.deleteTenant(anotherTenant);
assertThat(systemManagement.findTenants(PAGE)).as("Expected number if tenants after deletion is").hasSize(2);
assertThat(listTenants()).as("Expected number if tenants after deletion is").hasSize(2);
}
/**
@@ -202,4 +202,10 @@ class MultiTenancyEntityTest extends AbstractJpaIntegrationTest {
private Slice<? extends DistributionSet> findDistributionSetForTenant(final String tenant) throws Exception {
return runAsTenant(tenant, () -> distributionSetManagement.findAll(PAGE));
}
private List<String> listTenants() {
final List<String> tenants = new ArrayList<>();
asSystem(() -> systemManagement.forEachTenantAsSystem(tenants::add));
return tenants;
}
}