JPA Refactoring (3) (#2109)
Signed-off-by: Avgustin Marinov <Avgustin.Marinov@bosch.com>
This commit is contained in:
@@ -109,7 +109,4 @@ public interface SystemManagement {
|
||||
|
||||
@PreAuthorize(SpringEvalExpressions.IS_SYSTEM_CODE)
|
||||
TenantMetaData getTenantMetadata(long tenantId);
|
||||
|
||||
@PreAuthorize(SpringEvalExpressions.IS_SYSTEM_CODE)
|
||||
boolean tenantExists(String tenant);
|
||||
}
|
||||
|
||||
@@ -154,7 +154,6 @@ import org.eclipse.hawkbit.repository.model.Target;
|
||||
import org.eclipse.hawkbit.repository.model.TargetFilterQuery;
|
||||
import org.eclipse.hawkbit.repository.model.TargetType;
|
||||
import org.eclipse.hawkbit.repository.model.helper.EventPublisherHolder;
|
||||
import org.eclipse.hawkbit.repository.model.helper.SystemManagementHolder;
|
||||
import org.eclipse.hawkbit.repository.model.helper.SystemSecurityContextHolder;
|
||||
import org.eclipse.hawkbit.repository.model.helper.TenantConfigurationManagementHolder;
|
||||
import org.eclipse.hawkbit.repository.rsql.RsqlConfigHolder;
|
||||
@@ -488,16 +487,6 @@ public class RepositoryApplicationConfiguration extends JpaBaseConfiguration {
|
||||
return TenantConfigurationManagementHolder.getInstance();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the {@link SystemManagementHolder} singleton bean which holds the
|
||||
* current {@link SystemManagement} service and make it accessible in
|
||||
* beans which cannot access the service directly, e.g. JPA entities.
|
||||
*/
|
||||
@Bean
|
||||
SystemManagementHolder systemManagementHolder() {
|
||||
return SystemManagementHolder.getInstance();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the {@link TenantAwareHolder} singleton bean which holds the current
|
||||
* {@link TenantAware} service and make it accessible in beans which
|
||||
|
||||
@@ -24,7 +24,6 @@ import org.eclipse.hawkbit.repository.RepositoryConstants;
|
||||
import org.eclipse.hawkbit.repository.RepositoryProperties;
|
||||
import org.eclipse.hawkbit.repository.builder.ActionStatusCreate;
|
||||
import org.eclipse.hawkbit.repository.exception.AutoConfirmationAlreadyActiveException;
|
||||
import org.eclipse.hawkbit.repository.exception.EntityNotFoundException;
|
||||
import org.eclipse.hawkbit.repository.exception.InvalidConfirmationFeedbackException;
|
||||
import org.eclipse.hawkbit.repository.jpa.builder.JpaActionStatusCreate;
|
||||
import org.eclipse.hawkbit.repository.jpa.configuration.Constants;
|
||||
@@ -35,7 +34,6 @@ import org.eclipse.hawkbit.repository.jpa.model.JpaTarget;
|
||||
import org.eclipse.hawkbit.repository.jpa.repository.ActionRepository;
|
||||
import org.eclipse.hawkbit.repository.jpa.repository.ActionStatusRepository;
|
||||
import org.eclipse.hawkbit.repository.jpa.repository.TargetRepository;
|
||||
import org.eclipse.hawkbit.repository.jpa.specifications.TargetSpecifications;
|
||||
import org.eclipse.hawkbit.repository.model.Action;
|
||||
import org.eclipse.hawkbit.repository.model.Action.Status;
|
||||
import org.eclipse.hawkbit.repository.model.AutoConfirmationStatus;
|
||||
@@ -81,7 +79,7 @@ public class JpaConfirmationManagement extends JpaActionManagement implements Co
|
||||
public AutoConfirmationStatus activateAutoConfirmation(final String controllerId, final String initiator, final String remark) {
|
||||
log.trace(
|
||||
"'activateAutoConfirmation' was called with values: controllerId={}; initiator={}; remark={}", controllerId, initiator, remark);
|
||||
final JpaTarget target = getTargetByControllerIdAndThrowIfNotFound(controllerId);
|
||||
final JpaTarget target = targetRepository.getByControllerId(controllerId);
|
||||
if (target.getAutoConfirmationStatus() != null) {
|
||||
log.debug("'activateAutoConfirmation' was called for an controller id {} with active auto confirmation.", controllerId);
|
||||
throw new AutoConfirmationAlreadyActiveException(controllerId);
|
||||
@@ -104,13 +102,13 @@ public class JpaConfirmationManagement extends JpaActionManagement implements Co
|
||||
|
||||
@Override
|
||||
public Optional<AutoConfirmationStatus> getStatus(final String controllerId) {
|
||||
return Optional.of(getTargetByControllerIdAndThrowIfNotFound(controllerId)).map(JpaTarget::getAutoConfirmationStatus);
|
||||
return Optional.of(targetRepository.getByControllerId(controllerId)).map(JpaTarget::getAutoConfirmationStatus);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public List<Action> autoConfirmActiveActions(final String controllerId) {
|
||||
final JpaTarget target = getTargetByControllerIdAndThrowIfNotFound(controllerId);
|
||||
final JpaTarget target = targetRepository.getByControllerId(controllerId);
|
||||
if (target.getAutoConfirmationStatus() == null) {
|
||||
// auto-confirmation is not active
|
||||
return Collections.emptyList();
|
||||
@@ -159,7 +157,7 @@ public class JpaConfirmationManagement extends JpaActionManagement implements Co
|
||||
@Transactional
|
||||
public void deactivateAutoConfirmation(String controllerId) {
|
||||
log.debug("Deactivate auto confirmation for controllerId '{}'", controllerId);
|
||||
final JpaTarget target = getTargetByControllerIdAndThrowIfNotFound(controllerId);
|
||||
final JpaTarget target = targetRepository.getByControllerId(controllerId);
|
||||
target.setAutoConfirmationStatus(null);
|
||||
targetRepository.save(target);
|
||||
}
|
||||
@@ -220,19 +218,12 @@ public class JpaConfirmationManagement extends JpaActionManagement implements Co
|
||||
"Automatically confirm actionId '{}' due to active auto-confirmation initiated by '{}' and rollouts system user '{}'",
|
||||
action.getId(), autoConfirmationStatus.getInitiator(), autoConfirmationStatus.getCreatedBy());
|
||||
|
||||
// do not make use of
|
||||
// org.eclipse.hawkbit.repository.jpa.management.JpaActionManagement.handleAddUpdateActionStatus
|
||||
// to bypass the quota check. Otherwise, the action will not be confirmed in case
|
||||
// of exceeded action status quota.
|
||||
// do not make use of org.eclipse.hawkbit.repository.jpa.management.JpaActionManagement.handleAddUpdateActionStatus
|
||||
// to bypass the quota check. Otherwise, the action will not be confirmed in case of exceeded action status quota.
|
||||
action.setStatus(Status.RUNNING);
|
||||
actionStatus.setAction(action);
|
||||
|
||||
actionStatusRepository.save(actionStatus);
|
||||
return actionRepository.save(action);
|
||||
}
|
||||
|
||||
private JpaTarget getTargetByControllerIdAndThrowIfNotFound(final String controllerId) {
|
||||
return targetRepository.findOne(TargetSpecifications.hasControllerId(controllerId))
|
||||
.orElseThrow(() -> new EntityNotFoundException(Target.class, controllerId));
|
||||
}
|
||||
}
|
||||
@@ -414,8 +414,7 @@ public class JpaControllerManagement extends JpaActionManagement implements Cont
|
||||
throw new InvalidTargetAttributeException();
|
||||
}
|
||||
|
||||
final JpaTarget target = targetRepository.findOne(TargetSpecifications.hasControllerId(controllerId))
|
||||
.orElseThrow(() -> new EntityNotFoundException(Target.class, controllerId));
|
||||
final JpaTarget target = targetRepository.getByControllerId(controllerId);
|
||||
|
||||
// get the modifiable attribute map
|
||||
final Map<String, String> controllerAttributes = target.getControllerAttributes();
|
||||
@@ -447,7 +446,7 @@ public class JpaControllerManagement extends JpaActionManagement implements Cont
|
||||
|
||||
@Override
|
||||
public Optional<Target> getByControllerId(final String controllerId) {
|
||||
return targetRepository.findOne(TargetSpecifications.hasControllerId(controllerId)).map(Target.class::cast);
|
||||
return targetRepository.findByControllerId(controllerId).map(Target.class::cast);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -541,20 +540,16 @@ public class JpaControllerManagement extends JpaActionManagement implements Cont
|
||||
|
||||
@Override
|
||||
public void deleteExistingTarget(@NotEmpty final String controllerId) {
|
||||
final Target target = targetRepository.findOne(TargetSpecifications.hasControllerId(controllerId))
|
||||
.orElseThrow(() -> new EntityNotFoundException(Target.class, controllerId));
|
||||
targetRepository.deleteById(target.getId());
|
||||
targetRepository.deleteById(targetRepository.getByControllerId(controllerId).getId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Action> getInstalledActionByTarget(final String controllerId) {
|
||||
final JpaTarget jpaTarget = targetRepository.findOne(TargetSpecifications.hasControllerId(controllerId))
|
||||
.orElseThrow(() -> new EntityNotFoundException(Target.class, controllerId));
|
||||
|
||||
final JpaDistributionSet installedDistributionSet = jpaTarget.getInstalledDistributionSet();
|
||||
if (null != installedDistributionSet) {
|
||||
return actionRepository.findFirstByTargetIdAndDistributionSetIdAndStatusOrderByIdDesc(jpaTarget.getId(),
|
||||
installedDistributionSet.getId(), FINISHED);
|
||||
final JpaDistributionSet installedDistributionSet = targetRepository.getByControllerId(controllerId).getInstalledDistributionSet();
|
||||
if (installedDistributionSet != null) {
|
||||
final JpaTarget jpaTarget = targetRepository.getByControllerId(controllerId);
|
||||
return actionRepository.findFirstByTargetIdAndDistributionSetIdAndStatusOrderByIdDesc(
|
||||
jpaTarget.getId(), installedDistributionSet.getId(), FINISHED);
|
||||
} else {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
@@ -486,16 +486,12 @@ public class JpaDeploymentManagement extends JpaActionManagement implements Depl
|
||||
|
||||
@Override
|
||||
public Optional<DistributionSet> getAssignedDistributionSet(final String controllerId) {
|
||||
return targetRepository
|
||||
.findOne(TargetSpecifications.hasControllerId(controllerId))
|
||||
.map(JpaTarget::getAssignedDistributionSet);
|
||||
return targetRepository.findByControllerId(controllerId).map(JpaTarget::getAssignedDistributionSet);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<DistributionSet> getInstalledDistributionSet(final String controllerId) {
|
||||
return targetRepository
|
||||
.findOne(TargetSpecifications.hasControllerId(controllerId))
|
||||
.map(JpaTarget::getInstalledDistributionSet);
|
||||
return targetRepository.findByControllerId(controllerId).map(JpaTarget::getInstalledDistributionSet);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -710,8 +706,7 @@ public class JpaDeploymentManagement extends JpaActionManagement implements Depl
|
||||
}
|
||||
|
||||
private void checkCompatibilityForMultiDsAssignment(final String controllerId, final List<Long> distSetIds) {
|
||||
final Target target = targetRepository.findOne(TargetSpecifications.hasControllerId(controllerId))
|
||||
.orElseThrow(() -> new EntityNotFoundException(Target.class, controllerId));
|
||||
final Target target = targetRepository.getByControllerId(controllerId);
|
||||
|
||||
if (target.getTargetType() != null) {
|
||||
// we assume that list of assigned DS is less than
|
||||
|
||||
@@ -73,8 +73,10 @@ import org.springframework.validation.annotation.Validated;
|
||||
public class JpaSystemManagement implements CurrentTenantCacheKeyGenerator, SystemManagement {
|
||||
|
||||
private static final int MAX_TENANTS_QUERY = 1000;
|
||||
|
||||
private final String countArtifactQuery;
|
||||
private final String countSoftwareModulesQuery;
|
||||
|
||||
@Autowired
|
||||
private EntityManager entityManager;
|
||||
@Autowired
|
||||
@@ -284,12 +286,6 @@ public class JpaSystemManagement implements CurrentTenantCacheKeyGenerator, Syst
|
||||
.orElseThrow(() -> new EntityNotFoundException(TenantMetaData.class, tenantId));
|
||||
}
|
||||
|
||||
@Override
|
||||
@Cacheable(value = "currentTenant", keyGenerator = "currentTenantKeyGenerator", cacheManager = "directCacheManager", unless = "#result == null")
|
||||
public boolean tenantExists(final String tenant) {
|
||||
return tenantMetaDataRepository.findByTenantIgnoreCase(tenant) != null;
|
||||
}
|
||||
|
||||
private static boolean isPostgreSql(final JpaProperties properties) {
|
||||
return Database.POSTGRESQL == properties.getDatabase();
|
||||
}
|
||||
|
||||
@@ -410,7 +410,7 @@ public class JpaTargetManagement implements TargetManagement {
|
||||
|
||||
@Override
|
||||
public Optional<Target> getByControllerID(final String controllerId) {
|
||||
return targetRepository.findOne(TargetSpecifications.hasControllerId(controllerId)).map(Target.class::cast);
|
||||
return targetRepository.findByControllerId(controllerId).map(Target.class::cast);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -892,8 +892,7 @@ public class JpaTargetManagement implements TargetManagement {
|
||||
}
|
||||
|
||||
private JpaTarget getByControllerIdAndThrowIfNotFound(final String controllerId) {
|
||||
return targetRepository.findOne(TargetSpecifications.hasControllerId(controllerId))
|
||||
.orElseThrow(() -> new EntityNotFoundException(Target.class, controllerId));
|
||||
return targetRepository.getByControllerId(controllerId);
|
||||
}
|
||||
|
||||
private JpaTargetType getTargetTypeByIdAndThrowIfNotFound(final long id) {
|
||||
@@ -960,7 +959,7 @@ public class JpaTargetManagement implements TargetManagement {
|
||||
final JpaTargetTag tag = targetTagRepository.findById(targetTagId)
|
||||
.orElseThrow(() -> new EntityNotFoundException(TargetTag.class, targetTagId));
|
||||
final List<JpaTarget> targets = controllerIds.size() == 1 ?
|
||||
targetRepository.findOne(TargetSpecifications.hasControllerId(controllerIds.iterator().next()))
|
||||
targetRepository.findByControllerId(controllerIds.iterator().next())
|
||||
.map(List::of)
|
||||
.orElseGet(Collections::emptyList) :
|
||||
targetRepository
|
||||
|
||||
@@ -24,11 +24,9 @@ import lombok.Setter;
|
||||
import org.eclipse.hawkbit.repository.exception.TenantNotExistException;
|
||||
import org.eclipse.hawkbit.repository.jpa.model.helper.TenantAwareHolder;
|
||||
import org.eclipse.hawkbit.repository.model.TenantAwareBaseEntity;
|
||||
import org.eclipse.hawkbit.repository.model.helper.SystemManagementHolder;
|
||||
import org.eclipse.persistence.annotations.Multitenant;
|
||||
import org.eclipse.persistence.annotations.MultitenantType;
|
||||
import org.eclipse.persistence.annotations.TenantDiscriminatorColumn;
|
||||
import org.hibernate.annotations.TenantId;
|
||||
|
||||
/**
|
||||
* Holder of the base attributes common to all tenant aware entities.
|
||||
|
||||
@@ -37,15 +37,13 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
@NoRepositoryBean
|
||||
@Transactional(readOnly = true)
|
||||
public interface BaseEntityRepository<T extends AbstractJpaTenantAwareBaseEntity>
|
||||
extends PagingAndSortingRepository<T, Long>, CrudRepository<T, Long>, JpaSpecificationExecutor<T>,
|
||||
NoCountSliceRepository<T>, ACMRepository<T> {
|
||||
extends PagingAndSortingRepository<T, Long>, CrudRepository<T, Long>, JpaSpecificationExecutor<T>, NoCountSliceRepository<T>,
|
||||
ACMRepository<T> {
|
||||
|
||||
/**
|
||||
* Overrides
|
||||
* {@link org.springframework.data.repository.CrudRepository#saveAll(Iterable)}
|
||||
* to return a list of created entities instead of an instance of
|
||||
* {@link Iterable} to be able to work with it directly in further code
|
||||
* processing instead of converting the {@link Iterable}.
|
||||
* Overrides {@link org.springframework.data.repository.CrudRepository#saveAll(Iterable)} to return a list of created entities instead
|
||||
* of an instance of {@link Iterable} to be able to work with it directly in further code processing instead of converting the
|
||||
* {@link Iterable}.
|
||||
*
|
||||
* @param entities to persist in the database
|
||||
* @return the created entities
|
||||
@@ -55,11 +53,9 @@ public interface BaseEntityRepository<T extends AbstractJpaTenantAwareBaseEntity
|
||||
<S extends T> List<S> saveAll(Iterable<S> entities);
|
||||
|
||||
/**
|
||||
* Overrides
|
||||
* {@link org.springframework.data.repository.CrudRepository#findAll()}
|
||||
* to return a list of found entities instead of an instance of
|
||||
* {@link Iterable} to be able to work with it directly in further code
|
||||
* processing instead of converting the {@link Iterable}.
|
||||
* Overrides {@link org.springframework.data.repository.CrudRepository#findAll()} to return a list of found entities instead of
|
||||
* an instance of {@link Iterable} to be able to work with it directly in further code processing instead of converting the
|
||||
* {@link Iterable}.
|
||||
*
|
||||
* @return the found entities
|
||||
*/
|
||||
@@ -67,22 +63,9 @@ public interface BaseEntityRepository<T extends AbstractJpaTenantAwareBaseEntity
|
||||
List<T> findAll();
|
||||
|
||||
/**
|
||||
* Gets by id requiring entity to exists
|
||||
*
|
||||
* @param id the entity id
|
||||
* @return the existing entity.
|
||||
* @throws EntityNotFoundException if the entity doesn't exist
|
||||
*/
|
||||
default T getById(final Long id) {
|
||||
return findById(id).orElseThrow(() -> new EntityNotFoundException(getDomainClass(), id));
|
||||
}
|
||||
|
||||
/**
|
||||
* Overrides
|
||||
* {@link org.springframework.data.repository.CrudRepository#findAllById(Iterable)}
|
||||
* to return a list of found entities instead of an instance of
|
||||
* {@link Iterable} to be able to work with it directly in further code
|
||||
* processing instead of converting the {@link Iterable}.
|
||||
* Overrides {@link org.springframework.data.repository.CrudRepository#findAllById(Iterable)} to return a list of found entities instead
|
||||
* of an instance of {@link Iterable} to be able to work with it directly in further code processing instead of converting the
|
||||
* {@link Iterable}.
|
||||
*
|
||||
* @param ids to search in the database for
|
||||
* @return the found entities
|
||||
@@ -92,10 +75,8 @@ public interface BaseEntityRepository<T extends AbstractJpaTenantAwareBaseEntity
|
||||
|
||||
// TODO To be considered if this method is needed at all
|
||||
/**
|
||||
* Deletes all entities of a given tenant from this repository. For safety
|
||||
* reasons (this is a "delete everything" query after all) we add the tenant
|
||||
* manually to query even if this will be done by {@link EntityManager}
|
||||
* anyhow. The DB should take care of optimizing this away.
|
||||
* Deletes all entities of a given tenant from this repository. For safety reasons (this is a "delete everything" query after all) we add
|
||||
* the tenant manually to query even if this will be done by {@link EntityManager} anyhow. The DB should take care of optimizing this away.
|
||||
* <p/>
|
||||
*
|
||||
* @param tenant to delete data from
|
||||
@@ -103,11 +84,9 @@ public interface BaseEntityRepository<T extends AbstractJpaTenantAwareBaseEntity
|
||||
void deleteByTenant(String tenant);
|
||||
|
||||
/**
|
||||
* Returns a wrapper (or the same instance if access controller is <code>null</code> of this repository that
|
||||
* supports ACM.
|
||||
* Returns a wrapper (or the same instance if access controller is <code>null</code> of this repository that supports ACM.
|
||||
* <p/>
|
||||
* Note: To use ACM support the returned object shall be used! <code>this</code> object will not achieve ACM
|
||||
* support!
|
||||
* Note: To use ACM support the returned object shall be used! <code>this</code> object will not achieve ACM support!
|
||||
* <p/>
|
||||
* Notes on ACM support (if enabled, i.e. <code>accessController</code> is not <code>null</code>):
|
||||
* <ul>
|
||||
@@ -123,7 +102,6 @@ public interface BaseEntityRepository<T extends AbstractJpaTenantAwareBaseEntity
|
||||
* </ul>
|
||||
*
|
||||
* @param accessController access controller to be applied to the result
|
||||
* @param entityType the entity type of the repository
|
||||
* @return a repository that supports ACM.
|
||||
*/
|
||||
default BaseEntityRepository<T> withACM(@Nullable final AccessController<T> accessController) {
|
||||
@@ -152,4 +130,4 @@ public interface BaseEntityRepository<T extends AbstractJpaTenantAwareBaseEntity
|
||||
default Optional<AccessController<T>> getAccessController() {
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -322,7 +322,11 @@ public class BaseEntityRepositoryACM<T extends AbstractJpaTenantAwareBaseEntity>
|
||||
.filter(value -> repository.getDomainClass().isAssignableFrom(value.getClass()))
|
||||
.isPresent()) {
|
||||
return ((Optional<T>) result).filter(
|
||||
t -> isOperationAllowed(AccessController.Operation.READ, t, accessController));
|
||||
t -> {
|
||||
// if not accessible - throws exception (as for iterables or single entities)
|
||||
accessController.assertOperationAllowed(AccessController.Operation.READ, t);
|
||||
return true;
|
||||
});
|
||||
} else if (repository.getDomainClass().isAssignableFrom(method.getReturnType())) {
|
||||
accessController.assertOperationAllowed(AccessController.Operation.READ, (T) result);
|
||||
}
|
||||
|
||||
@@ -10,11 +10,14 @@
|
||||
package org.eclipse.hawkbit.repository.jpa.repository;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Optional;
|
||||
|
||||
import jakarta.persistence.EntityManager;
|
||||
|
||||
import org.eclipse.hawkbit.repository.exception.EntityNotFoundException;
|
||||
import org.eclipse.hawkbit.repository.jpa.model.JpaDistributionSet;
|
||||
import org.eclipse.hawkbit.repository.jpa.model.JpaTarget;
|
||||
import org.eclipse.hawkbit.repository.jpa.specifications.TargetSpecifications;
|
||||
import org.eclipse.hawkbit.repository.model.Target;
|
||||
import org.eclipse.hawkbit.repository.model.TargetUpdateStatus;
|
||||
import org.eclipse.hawkbit.repository.model.TenantAwareBaseEntity;
|
||||
@@ -29,6 +32,15 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
@Transactional(readOnly = true)
|
||||
public interface TargetRepository extends BaseEntityRepository<JpaTarget> {
|
||||
|
||||
default Optional<JpaTarget> findByControllerId(final String controllerId) {
|
||||
return findOne(TargetSpecifications.hasControllerId(controllerId));
|
||||
}
|
||||
|
||||
default JpaTarget getByControllerId(final String controllerId) {
|
||||
return findOne(TargetSpecifications.hasControllerId(controllerId))
|
||||
.orElseThrow(() -> new EntityNotFoundException(Target.class, controllerId));
|
||||
}
|
||||
|
||||
// TODO AC - remove it and use specification
|
||||
|
||||
/**
|
||||
|
||||
@@ -21,7 +21,6 @@ import io.qameta.allure.Feature;
|
||||
import io.qameta.allure.Story;
|
||||
import org.eclipse.hawkbit.repository.FilterParams;
|
||||
import org.eclipse.hawkbit.repository.Identifiable;
|
||||
import org.eclipse.hawkbit.repository.exception.EntityNotFoundException;
|
||||
import org.eclipse.hawkbit.repository.exception.InsufficientPermissionException;
|
||||
import org.eclipse.hawkbit.repository.jpa.acm.AccessController;
|
||||
import org.eclipse.hawkbit.repository.jpa.autoassign.AutoAssignChecker;
|
||||
@@ -76,7 +75,9 @@ class TargetAccessControllerTest extends AbstractAccessControllerTest {
|
||||
|
||||
// verify targetManagement#getByControllerID
|
||||
assertThat(targetManagement.getByControllerID(permittedTarget.getControllerId())).isPresent();
|
||||
assertThat(targetManagement.getByControllerID(hiddenTarget.getControllerId())).isEmpty();
|
||||
assertThatThrownBy(() -> targetManagement.getByControllerID(hiddenTarget.getControllerId()))
|
||||
.as("Missing read permissions for hidden target.")
|
||||
.isInstanceOf(InsufficientPermissionException.class);
|
||||
|
||||
// verify targetManagement#getByControllerID
|
||||
assertThat(targetManagement
|
||||
@@ -95,7 +96,7 @@ class TargetAccessControllerTest extends AbstractAccessControllerTest {
|
||||
assertThat(targetManagement.getControllerAttributes(permittedTarget.getControllerId())).isEmpty();
|
||||
assertThatThrownBy(() -> {
|
||||
assertThat(targetManagement.getControllerAttributes(hiddenTarget.getControllerId())).isEmpty();
|
||||
}).as("Target should not be found.").isInstanceOf(EntityNotFoundException.class);
|
||||
}).as("Target should not be found.").isInstanceOf(InsufficientPermissionException.class);
|
||||
|
||||
final TargetFilterQuery targetFilterQuery = targetFilterQueryManagement
|
||||
.create(entityFactory.targetFilterQuery().create().name("test").query("id==*"));
|
||||
@@ -176,7 +177,7 @@ class TargetAccessControllerTest extends AbstractAccessControllerTest {
|
||||
assertThatThrownBy(() -> {
|
||||
targetManagement.assignTag(Collections.singletonList(readOnlyTarget.getControllerId()), myTag.getId());
|
||||
}).as("Missing update permissions for target to toggle tag assignment.")
|
||||
.isInstanceOfAny(InsufficientPermissionException.class, EntityNotFoundException.class);
|
||||
.isInstanceOfAny(InsufficientPermissionException.class);
|
||||
|
||||
// assignment is denied for readOnlyTarget (read, but no update permissions)
|
||||
assertThatThrownBy(() -> {
|
||||
@@ -190,19 +191,18 @@ class TargetAccessControllerTest extends AbstractAccessControllerTest {
|
||||
.assignTag(Collections.singletonList(hiddenTarget.getControllerId()), myTag.getId())
|
||||
.size();
|
||||
}).as("Missing update permissions for target to toggle tag assignment.")
|
||||
.isInstanceOf(EntityNotFoundException.class);
|
||||
.isInstanceOf(InsufficientPermissionException.class);
|
||||
|
||||
// assignment is denied for hiddenTarget since it's hidden
|
||||
assertThatThrownBy(() -> {
|
||||
targetManagement.assignTag(Collections.singletonList(hiddenTarget.getControllerId()), myTag.getId());
|
||||
}).as("Missing update permissions for target to toggle tag assignment.")
|
||||
.isInstanceOf(EntityNotFoundException.class);
|
||||
.isInstanceOf(InsufficientPermissionException.class);
|
||||
|
||||
// assignment is denied for hiddenTarget since it's hidden
|
||||
assertThatThrownBy(() -> {
|
||||
targetManagement.unassignTag(hiddenTarget.getControllerId(), myTag.getId());
|
||||
}).as("Missing update permissions for target to toggle tag assignment.")
|
||||
.isInstanceOf(EntityNotFoundException.class);
|
||||
assertThatThrownBy(() -> targetManagement.unassignTag(hiddenTarget.getControllerId(), myTag.getId()))
|
||||
.as("Missing update permissions for target to toggle tag assignment.")
|
||||
.isInstanceOf(InsufficientPermissionException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -57,7 +57,6 @@ public class TestAccessControlManger {
|
||||
"Access to " + ruleClass.getName() + "/" + entity + " not allowed by checker!");
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -68,8 +68,7 @@ public class ArtifactManagementTest extends AbstractJpaIntegrationTest {
|
||||
|
||||
@Test
|
||||
@Description("Verifies that management get access react as specfied on calls for non existing entities by means of Optional not present.")
|
||||
@ExpectEvents({
|
||||
@Expect(type = SoftwareModuleCreatedEvent.class, count = 1) })
|
||||
@ExpectEvents({ @Expect(type = SoftwareModuleCreatedEvent.class, count = 1) })
|
||||
public void nonExistingEntityAccessReturnsNotPresent() {
|
||||
final SoftwareModule module = testdataFactory.createSoftwareModuleOs();
|
||||
|
||||
@@ -85,8 +84,7 @@ public class ArtifactManagementTest extends AbstractJpaIntegrationTest {
|
||||
@Test
|
||||
@Description("Verifies that management queries react as specfied on calls for non existing entities "
|
||||
+ " by means of throwing EntityNotFoundException.")
|
||||
@ExpectEvents({
|
||||
@Expect(type = SoftwareModuleDeletedEvent.class, count = 0) })
|
||||
@ExpectEvents({ @Expect(type = SoftwareModuleDeletedEvent.class, count = 0) })
|
||||
public void entityQueriesReferringToNotExistingEntitiesThrowsException() throws URISyntaxException {
|
||||
|
||||
final String artifactData = "test";
|
||||
|
||||
@@ -146,8 +146,7 @@ class DeploymentManagementTest extends AbstractJpaIntegrationTest {
|
||||
@Test
|
||||
@Description("Verifies that management get access react as specified on calls for non existing entities by means " +
|
||||
"of Optional not present.")
|
||||
@ExpectEvents({
|
||||
@Expect(type = TargetCreatedEvent.class, count = 0) })
|
||||
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class, count = 0) })
|
||||
void nonExistingEntityAccessReturnsNotPresent() {
|
||||
assertThat(deploymentManagement.findAction(1234L)).isNotPresent();
|
||||
assertThat(deploymentManagement.findActionWithDetails(NOT_EXIST_IDL)).isNotPresent();
|
||||
@@ -156,8 +155,7 @@ class DeploymentManagementTest extends AbstractJpaIntegrationTest {
|
||||
@Test
|
||||
@Description("Verifies that management queries react as specified on calls for non existing entities " +
|
||||
" by means of throwing EntityNotFoundException.")
|
||||
@ExpectEvents({
|
||||
@Expect(type = TargetCreatedEvent.class, count = 1) })
|
||||
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class, count = 1) })
|
||||
void entityQueriesReferringToNotExistingEntitiesThrowsException() {
|
||||
final Target target = testdataFactory.createTarget();
|
||||
final String dsName = "DistributionSet";
|
||||
|
||||
@@ -54,8 +54,7 @@ class DistributionSetTagManagementTest extends AbstractJpaIntegrationTest {
|
||||
|
||||
@Test
|
||||
@Description("Verifies that management get access reacts as specified on calls for non existing entities by means of Optional not present.")
|
||||
@ExpectEvents({
|
||||
@Expect(type = TargetCreatedEvent.class, count = 0) })
|
||||
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class, count = 0) })
|
||||
void nonExistingEntityAccessReturnsNotPresent() {
|
||||
assertThat(distributionSetTagManagement.getByName(NOT_EXIST_ID)).isNotPresent();
|
||||
assertThat(distributionSetTagManagement.get(NOT_EXIST_IDL)).isNotPresent();
|
||||
|
||||
@@ -55,8 +55,7 @@ public class DistributionSetTypeManagementTest extends AbstractJpaIntegrationTes
|
||||
@Test
|
||||
@Description("Verifies that management get access react as specfied on calls for non existing entities by means "
|
||||
+ "of Optional not present.")
|
||||
@ExpectEvents({
|
||||
@Expect(type = DistributionSetCreatedEvent.class, count = 0) })
|
||||
@ExpectEvents({ @Expect(type = DistributionSetCreatedEvent.class, count = 0) })
|
||||
public void nonExistingEntityAccessReturnsNotPresent() {
|
||||
assertThat(distributionSetTypeManagement.get(NOT_EXIST_IDL)).isNotPresent();
|
||||
assertThat(distributionSetTypeManagement.getByKey(NOT_EXIST_ID)).isNotPresent();
|
||||
|
||||
@@ -49,8 +49,7 @@ class RolloutGroupManagementTest extends AbstractJpaIntegrationTest {
|
||||
@Test
|
||||
@Description("Verifies that management get access reacts as specified on calls for non existing entities by means " +
|
||||
"of Optional not present.")
|
||||
@ExpectEvents({
|
||||
@Expect(type = TargetCreatedEvent.class, count = 0) })
|
||||
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class, count = 0) })
|
||||
void nonExistingEntityAccessReturnsNotPresent() {
|
||||
assertThat(rolloutGroupManagement.get(NOT_EXIST_IDL)).isNotPresent();
|
||||
assertThat(rolloutGroupManagement.getWithDetailedStatus(NOT_EXIST_IDL)).isNotPresent();
|
||||
|
||||
@@ -283,8 +283,7 @@ class RolloutManagementTest extends AbstractJpaIntegrationTest {
|
||||
@Test
|
||||
@Description("Verifies that management get access reacts as specified on calls for non existing entities by means "
|
||||
+ "of Optional not present.")
|
||||
@ExpectEvents({
|
||||
@Expect(type = TargetCreatedEvent.class) })
|
||||
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class) })
|
||||
void nonExistingEntityAccessReturnsNotPresent() {
|
||||
assertThat(rolloutManagement.get(NOT_EXIST_IDL)).isNotPresent();
|
||||
assertThat(rolloutManagement.getByName(NOT_EXIST_ID)).isNotPresent();
|
||||
|
||||
@@ -62,8 +62,7 @@ public class SoftwareModuleManagementTest extends AbstractJpaIntegrationTest {
|
||||
@Test
|
||||
@Description("Verifies that management get access reacts as specified on calls for non existing entities by means "
|
||||
+ "of Optional not present.")
|
||||
@ExpectEvents({
|
||||
@Expect(type = SoftwareModuleCreatedEvent.class, count = 1) })
|
||||
@ExpectEvents({ @Expect(type = SoftwareModuleCreatedEvent.class, count = 1) })
|
||||
public void nonExistingEntityAccessReturnsNotPresent() {
|
||||
final SoftwareModule module = testdataFactory.createSoftwareModuleApp();
|
||||
|
||||
@@ -78,8 +77,7 @@ public class SoftwareModuleManagementTest extends AbstractJpaIntegrationTest {
|
||||
@Test
|
||||
@Description("Verifies that management queries react as specfied on calls for non existing entities "
|
||||
+ " by means of throwing EntityNotFoundException.")
|
||||
@ExpectEvents({
|
||||
@Expect(type = SoftwareModuleCreatedEvent.class, count = 1) })
|
||||
@ExpectEvents({ @Expect(type = SoftwareModuleCreatedEvent.class, count = 1) })
|
||||
public void entityQueriesReferringToNotExistingEntitiesThrowsException() {
|
||||
final SoftwareModule module = testdataFactory.createSoftwareModuleApp();
|
||||
|
||||
|
||||
@@ -37,8 +37,7 @@ public class SoftwareModuleTypeManagementTest extends AbstractJpaIntegrationTest
|
||||
@Test
|
||||
@Description("Verifies that management get access reacts as specfied on calls for non existing entities by means "
|
||||
+ "of Optional not present.")
|
||||
@ExpectEvents({
|
||||
@Expect(type = SoftwareModuleCreatedEvent.class, count = 0) })
|
||||
@ExpectEvents({ @Expect(type = SoftwareModuleCreatedEvent.class, count = 0) })
|
||||
public void nonExistingEntityAccessReturnsNotPresent() {
|
||||
|
||||
assertThat(softwareModuleTypeManagement.get(NOT_EXIST_IDL)).isNotPresent();
|
||||
@@ -49,8 +48,7 @@ public class SoftwareModuleTypeManagementTest extends AbstractJpaIntegrationTest
|
||||
@Test
|
||||
@Description("Verifies that management queries react as specfied on calls for non existing entities "
|
||||
+ " by means of throwing EntityNotFoundException.")
|
||||
@ExpectEvents({
|
||||
@Expect(type = SoftwareModuleCreatedEvent.class, count = 0) })
|
||||
@ExpectEvents({ @Expect(type = SoftwareModuleCreatedEvent.class, count = 0) })
|
||||
public void entityQueriesReferringToNotExistingEntitiesThrowsException() {
|
||||
verifyThrownExceptionBy(() -> softwareModuleTypeManagement.delete(NOT_EXIST_IDL), "SoftwareModuleType");
|
||||
|
||||
|
||||
@@ -66,8 +66,7 @@ public class TargetFilterQueryManagementTest extends AbstractJpaIntegrationTest
|
||||
@Test
|
||||
@Description("Verifies that management get access reacts as specfied on calls for non existing entities by means "
|
||||
+ "of Optional not present.")
|
||||
@ExpectEvents({
|
||||
@Expect(type = TargetCreatedEvent.class, count = 0) })
|
||||
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class, count = 0) })
|
||||
public void nonExistingEntityAccessReturnsNotPresent() {
|
||||
assertThat(targetFilterQueryManagement.get(NOT_EXIST_IDL)).isNotPresent();
|
||||
assertThat(targetFilterQueryManagement.getByName(NOT_EXIST_ID)).isNotPresent();
|
||||
|
||||
@@ -96,8 +96,7 @@ class TargetManagementTest extends AbstractJpaIntegrationTest {
|
||||
@Test
|
||||
@Description("Verifies that management get access react as specified on calls for non existing entities by means "
|
||||
+ "of Optional not present.")
|
||||
@ExpectEvents({
|
||||
@Expect(type = TargetCreatedEvent.class, count = 1) })
|
||||
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class, count = 1) })
|
||||
void nonExistingEntityAccessReturnsNotPresent() {
|
||||
final Target target = testdataFactory.createTarget();
|
||||
assertThat(targetManagement.getByControllerID(NOT_EXIST_ID)).isNotPresent();
|
||||
@@ -184,8 +183,7 @@ class TargetManagementTest extends AbstractJpaIntegrationTest {
|
||||
|
||||
@Test
|
||||
@Description("Ensures that retrieving the target security is only permitted with the necessary permissions.")
|
||||
@ExpectEvents({
|
||||
@Expect(type = TargetCreatedEvent.class, count = 1) })
|
||||
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class, count = 1) })
|
||||
void getTargetSecurityTokenOnlyWithCorrectPermission() throws Exception {
|
||||
final Target createdTarget = targetManagement
|
||||
.create(entityFactory.target().create().controllerId("targetWithSecurityToken").securityToken("token"));
|
||||
@@ -221,8 +219,7 @@ class TargetManagementTest extends AbstractJpaIntegrationTest {
|
||||
|
||||
@Test
|
||||
@Description("Verify that a target with same controller ID than another device cannot be created.")
|
||||
@ExpectEvents({
|
||||
@Expect(type = TargetCreatedEvent.class, count = 1) })
|
||||
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class, count = 1) })
|
||||
void createTargetThatViolatesUniqueConstraintFails() {
|
||||
targetManagement.create(entityFactory.target().create().controllerId("123"));
|
||||
|
||||
@@ -388,8 +385,7 @@ class TargetManagementTest extends AbstractJpaIntegrationTest {
|
||||
|
||||
@Test
|
||||
@Description("Checks if the EntityAlreadyExistsException is thrown if the targets with the same controller ID are created twice.")
|
||||
@ExpectEvents({
|
||||
@Expect(type = TargetCreatedEvent.class, count = 5) })
|
||||
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class, count = 5) })
|
||||
void createMultipleTargetsDuplicate() {
|
||||
testdataFactory.createTargets(5, "mySimpleTargs", "my simple targets");
|
||||
try {
|
||||
@@ -402,8 +398,7 @@ class TargetManagementTest extends AbstractJpaIntegrationTest {
|
||||
|
||||
@Test
|
||||
@Description("Checks if the EntityAlreadyExistsException is thrown if a single target with the same controller ID are created twice.")
|
||||
@ExpectEvents({
|
||||
@Expect(type = TargetCreatedEvent.class, count = 1) })
|
||||
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class, count = 1) })
|
||||
void createTargetDuplicate() {
|
||||
targetManagement.create(entityFactory.target().create().controllerId("4711"));
|
||||
try {
|
||||
@@ -743,8 +738,7 @@ class TargetManagementTest extends AbstractJpaIntegrationTest {
|
||||
|
||||
@Test
|
||||
@Description("Verify that the find all targets by ids method contains the entities that we are looking for")
|
||||
@ExpectEvents({
|
||||
@Expect(type = TargetCreatedEvent.class, count = 12) })
|
||||
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class, count = 12) })
|
||||
void verifyFindTargetAllById() {
|
||||
final List<Long> searchIds = Arrays.asList(testdataFactory.createTarget("target-4").getId(),
|
||||
testdataFactory.createTarget("target-5").getId(), testdataFactory.createTarget("target-6").getId());
|
||||
|
||||
@@ -90,8 +90,7 @@ class TargetTagManagementTest extends AbstractJpaIntegrationTest {
|
||||
@Test
|
||||
@Description("Verifies that management get access reacts as specfied on calls for non existing entities by means " +
|
||||
"of Optional not present.")
|
||||
@ExpectEvents({
|
||||
@Expect(type = TargetCreatedEvent.class) })
|
||||
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class) })
|
||||
void nonExistingEntityAccessReturnsNotPresent() {
|
||||
assertThat(targetTagManagement.getByName(NOT_EXIST_ID)).isNotPresent();
|
||||
assertThat(targetTagManagement.get(NOT_EXIST_IDL)).isNotPresent();
|
||||
|
||||
@@ -43,8 +43,7 @@ class TargetTypeManagementTest extends AbstractJpaIntegrationTest {
|
||||
@Test
|
||||
@Description("Verifies that management get access react as specified on calls for non existing entities by means "
|
||||
+ "of Optional not present.")
|
||||
@ExpectEvents({
|
||||
@Expect(type = TargetTypeCreatedEvent.class) })
|
||||
@ExpectEvents({ @Expect(type = TargetTypeCreatedEvent.class) })
|
||||
void nonExistingEntityAccessReturnsNotPresent() {
|
||||
assertThat(targetTypeManagement.get(NOT_EXIST_IDL)).isNotPresent();
|
||||
assertThat(targetTypeManagement.getByName(NOT_EXIST_ID)).isNotPresent();
|
||||
@@ -53,8 +52,7 @@ class TargetTypeManagementTest extends AbstractJpaIntegrationTest {
|
||||
@Test
|
||||
@Description("Verifies that management queries react as specified on calls for non existing entities "
|
||||
+ " by means of throwing EntityNotFoundException.")
|
||||
@ExpectEvents({
|
||||
@Expect(type = TargetTypeUpdatedEvent.class) })
|
||||
@ExpectEvents({ @Expect(type = TargetTypeUpdatedEvent.class) })
|
||||
void entityQueriesReferringToNotExistingEntitiesThrowsException() {
|
||||
verifyThrownExceptionBy(() -> targetTypeManagement.delete(NOT_EXIST_IDL), "TargetType");
|
||||
verifyThrownExceptionBy(() -> targetTypeManagement.update(entityFactory.targetType().update(NOT_EXIST_IDL)),
|
||||
|
||||
@@ -27,11 +27,13 @@ import org.eclipse.hawkbit.event.BusProtoStuffMessageConverter;
|
||||
import org.eclipse.hawkbit.im.authentication.SpRole;
|
||||
import org.eclipse.hawkbit.repository.RolloutApprovalStrategy;
|
||||
import org.eclipse.hawkbit.repository.RolloutStatusCache;
|
||||
import org.eclipse.hawkbit.repository.SystemManagement;
|
||||
import org.eclipse.hawkbit.repository.event.ApplicationEventFilter;
|
||||
import org.eclipse.hawkbit.repository.model.helper.EventPublisherHolder;
|
||||
import org.eclipse.hawkbit.repository.rsql.VirtualPropertyReplacer;
|
||||
import org.eclipse.hawkbit.repository.rsql.VirtualPropertyResolver;
|
||||
import org.eclipse.hawkbit.repository.test.util.RolloutTestApprovalStrategy;
|
||||
import org.eclipse.hawkbit.repository.test.util.SystemManagementHolder;
|
||||
import org.eclipse.hawkbit.repository.test.util.TestdataFactory;
|
||||
import org.eclipse.hawkbit.security.DdiSecurityProperties;
|
||||
import org.eclipse.hawkbit.security.HawkbitSecurityProperties;
|
||||
@@ -131,6 +133,16 @@ public class TestConfiguration implements AsyncConfigurer {
|
||||
return new ArtifactFilesystemRepository(artifactFilesystemProperties);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the {@link org.eclipse.hawkbit.repository.test.util.SystemManagementHolder} singleton bean which holds the
|
||||
* current {@link SystemManagement} service and make it accessible in
|
||||
* beans which cannot access the service directly, e.g. JPA entities.
|
||||
*/
|
||||
@Bean
|
||||
SystemManagementHolder systemManagementHolder() {
|
||||
return SystemManagementHolder.getInstance();
|
||||
}
|
||||
|
||||
@Bean
|
||||
TestdataFactory testdataFactory() {
|
||||
return new TestdataFactory();
|
||||
|
||||
@@ -18,7 +18,6 @@ import java.util.Objects;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
import org.eclipse.hawkbit.im.authentication.SpPermission;
|
||||
import org.eclipse.hawkbit.repository.model.helper.SystemManagementHolder;
|
||||
import org.eclipse.hawkbit.tenancy.TenantAwareAuthenticationDetails;
|
||||
import org.eclipse.hawkbit.tenancy.TenantAwareUser;
|
||||
import org.springframework.security.authentication.TestingAuthenticationToken;
|
||||
@@ -29,6 +28,7 @@ import org.springframework.security.core.context.SecurityContextHolder;
|
||||
public class SecurityContextSwitch {
|
||||
|
||||
public static final String DEFAULT_TENANT = "DEFAULT";
|
||||
|
||||
private static final WithUser PRIVILEDGED_USER =
|
||||
createWithUser("bumlux", DEFAULT_TENANT, false, true, false, "ROLE_CONTROLLER", "ROLE_SYSTEM_CODE");
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.eclipse.hawkbit.repository.model.helper;
|
||||
package org.eclipse.hawkbit.repository.test.util;
|
||||
|
||||
import lombok.AccessLevel;
|
||||
import lombok.Getter;
|
||||
Reference in New Issue
Block a user