Skip to content

Commit

Permalink
Add existsByInstalledOrAssignedDistributionSet (#1064)
Browse files Browse the repository at this point in the history
Signed-off-by: Florian Ruschbaschan <[email protected]>
  • Loading branch information
floruschbaschan authored Jan 12, 2021
1 parent 2191db4 commit 8816396
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,20 @@ long countByFilters(Collection<TargetUpdateStatus> status, Boolean overdueState,
+ SpringEvalExpressions.HAS_AUTH_READ_REPOSITORY)
long countByInstalledDistributionSet(long distId);

/**
* Checks if there is already a {@link Target} that has the given distribution set Id assigned or installed.
*
* @param distId
* to search for
* @return <code>true</code> if a {@link Target} exists.
*
* @throws EntityNotFoundException
* if distribution set with given ID does not exist
*/
@PreAuthorize(SpringEvalExpressions.HAS_AUTH_READ_TARGET + SpringEvalExpressions.HAS_AUTH_OR
+ SpringEvalExpressions.HAS_AUTH_READ_REPOSITORY)
boolean existsByInstalledOrAssignedDistributionSet(long distId);

/**
* Count {@link TargetFilterQuery}s for given target filter query.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,13 @@ public long countByInstalledDistributionSet(final long distId) {
return targetRepository.countByInstalledDistributionSetId(distId);
}

@Override
public boolean existsByInstalledOrAssignedDistributionSet(final long distId) {
throwEntityNotFoundIfDsDoesNotExist(distId);

return targetRepository.existsByInstalledOrAssignedDistributionSet(distId);
}

@Override
public Page<Target> findByTargetFilterQueryAndNonDS(final Pageable pageRequest, final long distributionSetId,
final String targetFilterQuery) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,16 @@ List<JpaTarget> findByTagNameAndControllerIdIn(@Param("tagname") String tag,
*/
Long countByInstalledDistributionSetId(Long distId);

/**
* Checks if there is already a {@link Target} that has the given distribution set Id assigned or installed.
*
* @param distId
* to check
* @return <code>true</code> if a {@link Target} exists.
*/
@Query("SELECT CASE WHEN COUNT(t)>0 THEN 'true' ELSE 'false' END FROM JpaTarget t WHERE t.installedDistributionSet.id=:distId OR t.assignedDistributionSet.id=:distId")
boolean existsByInstalledOrAssignedDistributionSet(@Param("distId") Long distId);

/**
* Finds all {@link Target}s in the repository.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@
import org.eclipse.hawkbit.repository.event.remote.entity.TargetCreatedEvent;
import org.eclipse.hawkbit.repository.event.remote.entity.TargetTagCreatedEvent;
import org.eclipse.hawkbit.repository.event.remote.entity.TargetUpdatedEvent;
import org.eclipse.hawkbit.repository.exception.AssignmentQuotaExceededException;
import org.eclipse.hawkbit.repository.exception.EntityAlreadyExistsException;
import org.eclipse.hawkbit.repository.exception.InvalidTargetAddressException;
import org.eclipse.hawkbit.repository.exception.AssignmentQuotaExceededException;
import org.eclipse.hawkbit.repository.exception.TenantNotExistException;
import org.eclipse.hawkbit.repository.jpa.model.JpaTarget;
import org.eclipse.hawkbit.repository.jpa.model.JpaTargetMetadata;
Expand Down Expand Up @@ -108,6 +108,8 @@ public void entityQueriesReferringToNotExistingEntitiesThrowsException() {
"DistributionSet");
verifyThrownExceptionBy(() -> targetManagement.countByInstalledDistributionSet(NOT_EXIST_IDL),
"DistributionSet");
verifyThrownExceptionBy(() -> targetManagement.existsByInstalledOrAssignedDistributionSet(NOT_EXIST_IDL),
"DistributionSet");

verifyThrownExceptionBy(() -> targetManagement.countByTargetFilterQuery(NOT_EXIST_IDL), "TargetFilterQuery");
verifyThrownExceptionBy(() -> targetManagement.countByRsqlAndNonDS(NOT_EXIST_IDL, "name==*"),
Expand Down Expand Up @@ -465,10 +467,14 @@ public void findTargetByControllerIDWithDetails() {
.isEqualTo(0);
assertThat(targetManagement.countByInstalledDistributionSet(set.getId())).as("Target count is wrong")
.isEqualTo(0);
assertThat(targetManagement.existsByInstalledOrAssignedDistributionSet(set.getId())).as("Target count is wrong")
.isFalse();
assertThat(targetManagement.countByAssignedDistributionSet(set2.getId())).as("Target count is wrong")
.isEqualTo(0);
assertThat(targetManagement.countByInstalledDistributionSet(set2.getId())).as("Target count is wrong")
.isEqualTo(0);
assertThat(targetManagement.existsByInstalledOrAssignedDistributionSet(set2.getId())).as("Target count is wrong")
.isFalse();

Target target = createTargetWithAttributes("4711");

Expand All @@ -488,10 +494,14 @@ public void findTargetByControllerIDWithDetails() {
.isEqualTo(0);
assertThat(targetManagement.countByInstalledDistributionSet(set.getId())).as("Target count is wrong")
.isEqualTo(1);
assertThat(targetManagement.existsByInstalledOrAssignedDistributionSet(set.getId())).as("Target count is wrong")
.isTrue();
assertThat(targetManagement.countByAssignedDistributionSet(set2.getId())).as("Target count is wrong")
.isEqualTo(1);
assertThat(targetManagement.countByInstalledDistributionSet(set2.getId())).as("Target count is wrong")
.isEqualTo(0);
assertThat(targetManagement.existsByInstalledOrAssignedDistributionSet(set2.getId())).as("Target count is wrong")
.isTrue();
assertThat(target.getLastTargetQuery()).as("Target query is not work").isGreaterThanOrEqualTo(current);
assertThat(deploymentManagement.getAssignedDistributionSet("4711").get()).as("Assigned ds size is wrong")
.isEqualTo(set2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ private boolean isTargetDsValid(final ProxyDistributionSet ds, final Distributio
return false;
}

if (targetManagement.countByFilters(null, null, null, ds.getId(), Boolean.FALSE, "") > 0) {
/* Distribution is already assigned */
if (targetManagement.existsByInstalledOrAssignedDistributionSet(ds.getId())) {
/* Distribution is already assigned/installed */
notification.displayValidationError(i18n.getMessage("message.dist.inuse", ds.getNameVersion()));
return false;
}
Expand Down

0 comments on commit 8816396

Please sign in to comment.