Complete repo exception tests (#452)
Signed-off-by: kaizimmerm <kai.zimmermann@bosch-si.com>
This commit is contained in:
@@ -111,7 +111,7 @@ public class JpaDeploymentManagement implements DeploymentManagement {
|
||||
private ActionRepository actionRepository;
|
||||
|
||||
@Autowired
|
||||
private DistributionSetRepository distributoinSetRepository;
|
||||
private DistributionSetRepository distributionSetRepository;
|
||||
|
||||
@Autowired
|
||||
private TargetRepository targetRepository;
|
||||
@@ -165,7 +165,7 @@ public class JpaDeploymentManagement implements DeploymentManagement {
|
||||
@Transactional(isolation = Isolation.READ_COMMITTED)
|
||||
public DistributionSetAssignmentResult assignDistributionSet(final Long dsID,
|
||||
final Collection<TargetWithActionType> targets, final String actionMessage) {
|
||||
final JpaDistributionSet set = distributoinSetRepository.findOne(dsID);
|
||||
final JpaDistributionSet set = distributionSetRepository.findOne(dsID);
|
||||
if (set == null) {
|
||||
throw new EntityNotFoundException(DistributionSet.class, dsID);
|
||||
}
|
||||
@@ -535,11 +535,14 @@ public class JpaDeploymentManagement implements DeploymentManagement {
|
||||
|
||||
@Override
|
||||
public Slice<Action> findActionsByTarget(final String controllerId, final Pageable pageable) {
|
||||
throwExceptionIfTargetDoesNotExist(controllerId);
|
||||
return actionRepository.findByTargetControllerId(pageable, controllerId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ActionWithStatusCount> findActionsWithStatusCountByTargetOrderByIdDesc(final String controllerId) {
|
||||
throwExceptionIfTargetDoesNotExist(controllerId);
|
||||
|
||||
final CriteriaBuilder cb = entityManager.getCriteriaBuilder();
|
||||
final CriteriaQuery<JpaActionWithStatusCount> query = cb.createQuery(JpaActionWithStatusCount.class);
|
||||
final Root<JpaAction> actionRoot = query.from(JpaAction.class);
|
||||
@@ -564,6 +567,7 @@ public class JpaDeploymentManagement implements DeploymentManagement {
|
||||
@Override
|
||||
public Page<Action> findActionsByTarget(final String rsqlParam, final String controllerId,
|
||||
final Pageable pageable) {
|
||||
throwExceptionIfTargetDoesNotExist(controllerId);
|
||||
|
||||
final Specification<JpaAction> byTargetSpec = createSpecificationFor(controllerId, rsqlParam);
|
||||
final Page<JpaAction> actions = actionRepository.findAll(byTargetSpec, pageable);
|
||||
@@ -614,6 +618,12 @@ public class JpaDeploymentManagement implements DeploymentManagement {
|
||||
}
|
||||
}
|
||||
|
||||
private void throwExceptionIfDistributionSetDoesNotExist(final Long dsId) {
|
||||
if (!distributionSetRepository.exists(dsId)) {
|
||||
throw new EntityNotFoundException(DistributionSet.class, dsId);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@Modifying
|
||||
@Transactional(isolation = Isolation.READ_UNCOMMITTED)
|
||||
@@ -667,6 +677,8 @@ public class JpaDeploymentManagement implements DeploymentManagement {
|
||||
|
||||
@Override
|
||||
public Slice<Action> findActionsByDistributionSet(final Pageable pageable, final Long dsId) {
|
||||
throwExceptionIfDistributionSetDoesNotExist(dsId);
|
||||
|
||||
return actionRepository.findByDistributionSetId(pageable, dsId);
|
||||
}
|
||||
|
||||
@@ -679,13 +691,13 @@ public class JpaDeploymentManagement implements DeploymentManagement {
|
||||
public Optional<DistributionSet> getAssignedDistributionSet(final String controllerId) {
|
||||
throwExceptionIfTargetDoesNotExist(controllerId);
|
||||
|
||||
return distributoinSetRepository.findAssignedToTarget(controllerId);
|
||||
return distributionSetRepository.findAssignedToTarget(controllerId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<DistributionSet> getInstalledDistributionSet(final String controllerId) {
|
||||
throwExceptionIfTargetDoesNotExist(controllerId);
|
||||
|
||||
return distributoinSetRepository.findInstalledAtTarget(controllerId);
|
||||
return distributionSetRepository.findInstalledAtTarget(controllerId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -693,9 +693,7 @@ public class JpaDistributionSetManagement implements DistributionSetManagement {
|
||||
@Override
|
||||
public Page<DistributionSetMetadata> findDistributionSetMetadataByDistributionSetId(final Long distributionSetId,
|
||||
final Pageable pageable) {
|
||||
if (!distributionSetRepository.exists(distributionSetId)) {
|
||||
throw new EntityNotFoundException(DistributionSet.class, distributionSetId);
|
||||
}
|
||||
throwExceptionIfDistributionSetDoesNotExist(distributionSetId);
|
||||
|
||||
return convertMdPage(distributionSetMetadataRepository
|
||||
.findAll((Specification<JpaDistributionSetMetadata>) (root, query, cb) -> cb.equal(
|
||||
@@ -708,9 +706,7 @@ public class JpaDistributionSetManagement implements DistributionSetManagement {
|
||||
public Page<DistributionSetMetadata> findDistributionSetMetadataByDistributionSetId(final Long distributionSetId,
|
||||
final String rsqlParam, final Pageable pageable) {
|
||||
|
||||
if (!distributionSetRepository.exists(distributionSetId)) {
|
||||
throw new EntityNotFoundException(DistributionSet.class, distributionSetId);
|
||||
}
|
||||
throwExceptionIfDistributionSetDoesNotExist(distributionSetId);
|
||||
|
||||
final Specification<JpaDistributionSetMetadata> spec = RSQLUtility.parse(rsqlParam,
|
||||
DistributionSetMetadataFields.class, virtualPropertyReplacer);
|
||||
@@ -730,9 +726,10 @@ public class JpaDistributionSetManagement implements DistributionSetManagement {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<DistributionSetMetadata> findDistributionSetMetadata(final Long distributionSet, final String key) {
|
||||
return Optional.ofNullable(
|
||||
distributionSetMetadataRepository.findOne(new DsMetadataCompositeKey(distributionSet, key)));
|
||||
public Optional<DistributionSetMetadata> findDistributionSetMetadata(final Long setId, final String key) {
|
||||
throwExceptionIfDistributionSetDoesNotExist(setId);
|
||||
|
||||
return Optional.ofNullable(distributionSetMetadataRepository.findOne(new DsMetadataCompositeKey(setId, key)));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -746,6 +743,8 @@ public class JpaDistributionSetManagement implements DistributionSetManagement {
|
||||
|
||||
@Override
|
||||
public boolean isDistributionSetInUse(final Long setId) {
|
||||
throwExceptionIfDistributionSetDoesNotExist(setId);
|
||||
|
||||
return actionRepository.countByDistributionSetId(setId) > 0;
|
||||
}
|
||||
|
||||
@@ -903,11 +902,15 @@ public class JpaDistributionSetManagement implements DistributionSetManagement {
|
||||
@Modifying
|
||||
@Transactional(isolation = Isolation.READ_UNCOMMITTED)
|
||||
public void deleteDistributionSet(final Long setId) {
|
||||
throwExceptionIfDistributionSetDoesNotExist(setId);
|
||||
|
||||
deleteDistributionSet(Lists.newArrayList(setId));
|
||||
}
|
||||
|
||||
private void throwExceptionIfDistributionSetDoesNotExist(final Long setId) {
|
||||
if (!distributionSetRepository.exists(setId)) {
|
||||
throw new EntityNotFoundException(DistributionSet.class, setId);
|
||||
}
|
||||
|
||||
deleteDistributionSet(Lists.newArrayList(setId));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -301,14 +301,14 @@ public class JpaRolloutManagement extends AbstractRolloutManagement {
|
||||
int readyGroups = 0;
|
||||
int totalTargets = 0;
|
||||
for (final RolloutGroup group : rolloutGroups) {
|
||||
if (group.getStatus() != RolloutGroupStatus.CREATING) {
|
||||
if (RolloutGroupStatus.READY.equals(group.getStatus())) {
|
||||
readyGroups++;
|
||||
totalTargets += group.getTotalTargets();
|
||||
continue;
|
||||
}
|
||||
|
||||
final RolloutGroup filledGroup = fillRolloutGroupWithTargets(rollout, group);
|
||||
if (filledGroup.getStatus() == RolloutGroupStatus.READY) {
|
||||
if (RolloutGroupStatus.READY.equals(filledGroup.getStatus())) {
|
||||
readyGroups++;
|
||||
totalTargets += filledGroup.getTotalTargets();
|
||||
}
|
||||
@@ -788,6 +788,11 @@ public class JpaRolloutManagement extends AbstractRolloutManagement {
|
||||
@Modifying
|
||||
public void deleteRollout(final long rolloutId) {
|
||||
final JpaRollout jpaRollout = rolloutRepository.findOne(rolloutId);
|
||||
|
||||
if (jpaRollout == null) {
|
||||
throw new EntityNotFoundException(Rollout.class, rolloutId);
|
||||
}
|
||||
|
||||
if (RolloutStatus.DELETING.equals(jpaRollout.getStatus())) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -655,6 +655,8 @@ public class JpaSoftwareManagement implements SoftwareManagement {
|
||||
|
||||
@Override
|
||||
public Optional<SoftwareModuleMetadata> findSoftwareModuleMetadata(final Long moduleId, final String key) {
|
||||
throwExceptionIfSoftwareModuleDoesNotExist(moduleId);
|
||||
|
||||
return Optional.ofNullable(softwareModuleMetadataRepository.findOne(new SwMetadataCompositeKey(moduleId, key)));
|
||||
}
|
||||
|
||||
|
||||
@@ -9,7 +9,6 @@
|
||||
package org.eclipse.hawkbit.repository.jpa;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
@@ -327,21 +326,25 @@ public class JpaTargetManagement implements TargetManagement {
|
||||
@Override
|
||||
@Modifying
|
||||
@Transactional(isolation = Isolation.READ_UNCOMMITTED)
|
||||
public TargetTagAssignmentResult toggleTagAssignment(final Collection<String> targetIds, final String tagName) {
|
||||
public TargetTagAssignmentResult toggleTagAssignment(final Collection<String> controllerIds, final String tagName) {
|
||||
final TargetTag tag = targetTagRepository.findByNameEquals(tagName)
|
||||
.orElseThrow(() -> new EntityNotFoundException(TargetTag.class, tagName));
|
||||
final List<JpaTarget> alreadyAssignedTargets = targetRepository.findByTagNameAndControllerIdIn(tagName,
|
||||
targetIds);
|
||||
final List<JpaTarget> allTargets = targetRepository
|
||||
.findAll(TargetSpecifications.byControllerIdWithStatusAndTagsInJoin(targetIds));
|
||||
.findAll(TargetSpecifications.byControllerIdWithStatusAndTagsInJoin(controllerIds));
|
||||
|
||||
if (allTargets.size() < controllerIds.size()) {
|
||||
throw new EntityNotFoundException(Target.class, controllerIds,
|
||||
allTargets.stream().map(Target::getControllerId).collect(Collectors.toList()));
|
||||
}
|
||||
|
||||
final List<JpaTarget> alreadyAssignedTargets = targetRepository.findByTagNameAndControllerIdIn(tagName,
|
||||
controllerIds);
|
||||
|
||||
// all are already assigned -> unassign
|
||||
if (alreadyAssignedTargets.size() == allTargets.size()) {
|
||||
alreadyAssignedTargets.forEach(target -> target.removeTag(tag));
|
||||
final TargetTagAssignmentResult result = new TargetTagAssignmentResult(0, 0, alreadyAssignedTargets.size(),
|
||||
Collections.emptyList(), Collections.unmodifiableList(alreadyAssignedTargets), tag);
|
||||
|
||||
return result;
|
||||
return new TargetTagAssignmentResult(0, 0, alreadyAssignedTargets.size(), Collections.emptyList(),
|
||||
Collections.unmodifiableList(alreadyAssignedTargets), tag);
|
||||
}
|
||||
|
||||
allTargets.removeAll(alreadyAssignedTargets);
|
||||
@@ -365,6 +368,11 @@ public class JpaTargetManagement implements TargetManagement {
|
||||
final List<JpaTarget> allTargets = targetRepository
|
||||
.findAll(TargetSpecifications.byControllerIdWithStatusAndTagsInJoin(controllerIds));
|
||||
|
||||
if (allTargets.size() < controllerIds.size()) {
|
||||
throw new EntityNotFoundException(Target.class, controllerIds,
|
||||
allTargets.stream().map(Target::getControllerId).collect(Collectors.toList()));
|
||||
}
|
||||
|
||||
final JpaTargetTag tag = targetTagRepository.findById(tagId)
|
||||
.orElseThrow(() -> new EntityNotFoundException(TargetTag.class, tagId));
|
||||
|
||||
@@ -402,13 +410,13 @@ public class JpaTargetManagement implements TargetManagement {
|
||||
@Modifying
|
||||
@Transactional(isolation = Isolation.READ_UNCOMMITTED)
|
||||
public Target unAssignTag(final String controllerID, final Long targetTagId) {
|
||||
final List<Target> allTargets = Collections.unmodifiableList(targetRepository
|
||||
.findAll(TargetSpecifications.byControllerIdWithStatusAndTagsInJoin(Arrays.asList(controllerID))));
|
||||
final Target target = targetRepository.findByControllerId(controllerID)
|
||||
.orElseThrow(() -> new EntityNotFoundException(Target.class, controllerID));
|
||||
|
||||
final TargetTag tag = targetTagRepository.findById(targetTagId)
|
||||
.orElseThrow(() -> new EntityNotFoundException(TargetTag.class, targetTagId));
|
||||
|
||||
final List<Target> unAssignTag = unAssignTag(allTargets, tag);
|
||||
final List<Target> unAssignTag = unAssignTag(Lists.newArrayList(target), tag);
|
||||
return unAssignTag.isEmpty() ? null : unAssignTag.get(0);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user