Fix exception handling on repository (#546)
* Fix constraint violation handling (400 instead of 500). Signed-off-by: kaizimmerm <kai.zimmermann@bosch-si.com> * Dont map constraintvioalation Signed-off-by: kaizimmerm <kai.zimmermann@bosch-si.com> * Added test in target repo. Signed-off-by: kaizimmerm <kai.zimmermann@bosch-si.com> * Extended dialect handler. Signed-off-by: kaizimmerm <kai.zimmermann@bosch-si.com> * Fix broken constraint handling. Added target tests and docs. Signed-off-by: kaizimmerm <kai.zimmermann@bosch-si.com> * Further restricted aspect. Signed-off-by: kaizimmerm <kai.zimmermann@bosch-si.com> * Add macro test. Signed-off-by: kaizimmerm <kai.zimmermann@bosch-si.com> * Reduce duplicate code. Signed-off-by: kaizimmerm <kai.zimmermann@bosch-si.com> * No need to open a new transaction here. Signed-off-by: kaizimmerm <kai.zimmermann@bosch-si.com> * Remove comment. Signed-off-by: kaizimmerm <kai.zimmermann@bosch-si.com> * Remove flush from assign DS. Signed-off-by: kaizimmerm <kai.zimmermann@bosch-si.com> * Remove commented line Signed-off-by: kaizimmerm <kai.zimmermann@bosch-si.com> * Fix exception handling for non-SQL cause. Signed-off-by: kaizimmerm <kai.zimmermann@bosch-si.com> * Remove deprecated comment. Signed-off-by: kaizimmerm <kai.zimmermann@bosch-si.com> * Documentation Signed-off-by: kaizimmerm <kai.zimmermann@bosch-si.com> * More tests and documentation. Signed-off-by: kaizimmerm <kai.zimmermann@bosch-si.com> * Private final. Signed-off-by: kaizimmerm <kai.zimmermann@bosch-si.com> * Fix loop skip. Signed-off-by: kaizimmerm <kai.zimmermann@bosch-si.com> * Fix test description. Signed-off-by: kaizimmerm <kai.zimmermann@bosch-si.com> * Completed test coverage. Signed-off-by: kaizimmerm <kai.zimmermann@bosch-si.com>
This commit is contained in:
@@ -13,6 +13,7 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
import javax.validation.ConstraintViolationException;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
import org.eclipse.hawkbit.im.authentication.SpPermission.SpringEvalExpressions;
|
||||
@@ -158,6 +159,12 @@ public interface TargetManagement {
|
||||
* @param create
|
||||
* to be created
|
||||
* @return the created {@link Target}
|
||||
*
|
||||
* @throws EntityAlreadyExistsException
|
||||
* given target already exists.
|
||||
* @throws ConstraintViolationException
|
||||
* if fields are not filled as specified. Check
|
||||
* {@link TargetCreate} for field constraints.
|
||||
*
|
||||
*/
|
||||
@PreAuthorize(SpringEvalExpressions.HAS_AUTH_CREATE_TARGET + SpringEvalExpressions.HAS_AUTH_OR
|
||||
@@ -176,6 +183,9 @@ public interface TargetManagement {
|
||||
*
|
||||
* @throws EntityAlreadyExistsException
|
||||
* of one of the given targets already exist.
|
||||
* @throws ConstraintViolationException
|
||||
* if fields are not filled as specified. Check
|
||||
* {@link TargetCreate} for field constraints.
|
||||
*/
|
||||
@PreAuthorize(SpringEvalExpressions.HAS_AUTH_CREATE_TARGET)
|
||||
List<Target> createTargets(@NotNull Collection<TargetCreate> creates);
|
||||
@@ -612,6 +622,9 @@ public interface TargetManagement {
|
||||
*
|
||||
* @throws EntityNotFoundException
|
||||
* if given target does not exist
|
||||
* @throws ConstraintViolationException
|
||||
* if fields are not filled as specified. Check
|
||||
* {@link TargetUpdate} for field constraints.
|
||||
*/
|
||||
@PreAuthorize(SpringEvalExpressions.HAS_AUTH_UPDATE_TARGET + SpringEvalExpressions.HAS_AUTH_OR
|
||||
+ SpringEvalExpressions.IS_CONTROLLER)
|
||||
|
||||
@@ -10,6 +10,8 @@ package org.eclipse.hawkbit.repository.builder;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
import org.eclipse.hawkbit.repository.model.Action.Status;
|
||||
import org.eclipse.hawkbit.repository.model.ActionStatus;
|
||||
import org.eclipse.hawkbit.repository.model.BaseEntity;
|
||||
@@ -26,7 +28,7 @@ public interface ActionStatusCreate {
|
||||
* {@link ActionStatus#getStatus()}
|
||||
* @return updated {@link ActionStatusCreate} object
|
||||
*/
|
||||
ActionStatusCreate status(Status status);
|
||||
ActionStatusCreate status(@NotNull Status status);
|
||||
|
||||
/**
|
||||
* @param occurredAt
|
||||
|
||||
@@ -8,10 +8,13 @@
|
||||
*/
|
||||
package org.eclipse.hawkbit.repository.builder;
|
||||
|
||||
import javax.validation.constraints.NotNull;
|
||||
import javax.validation.constraints.Size;
|
||||
|
||||
import org.eclipse.hawkbit.repository.model.BaseEntity;
|
||||
import org.eclipse.hawkbit.repository.model.NamedEntity;
|
||||
import org.eclipse.hawkbit.repository.model.Target;
|
||||
import org.eclipse.hawkbit.repository.model.TargetUpdateStatus;
|
||||
import org.hibernate.validator.constraints.NotEmpty;
|
||||
|
||||
/**
|
||||
* Builder to create a new {@link Target} entry. Defines all fields that can be
|
||||
@@ -26,28 +29,30 @@ public interface TargetCreate {
|
||||
* for {@link Target#getControllerId()}
|
||||
* @return updated builder instance
|
||||
*/
|
||||
TargetCreate controllerId(@NotEmpty String controllerId);
|
||||
TargetCreate controllerId(@Size(min = 1, max = Target.CONTROLLER_ID_MAX_SIZE) @NotNull String controllerId);
|
||||
|
||||
/**
|
||||
* @param name
|
||||
* for {@link Target#getName()}
|
||||
* for {@link Target#getName()} filled with
|
||||
* {@link #controllerId(String)} as default if not set explicitly
|
||||
* @return updated builder instance
|
||||
*/
|
||||
TargetCreate name(@NotEmpty String name);
|
||||
TargetCreate name(@Size(min = 1, max = NamedEntity.NAME_MAX_SIZE) @NotNull String name);
|
||||
|
||||
/**
|
||||
* @param description
|
||||
* for {@link Target#getDescription()}
|
||||
* @return updated builder instance
|
||||
*/
|
||||
TargetCreate description(String description);
|
||||
TargetCreate description(@Size(max = NamedEntity.DESCRIPTION_MAX_SIZE) String description);
|
||||
|
||||
/**
|
||||
* @param securityToken
|
||||
* for {@link Target#getSecurityToken()}
|
||||
* for {@link Target#getSecurityToken()} is generated with a
|
||||
* random sequence as default if not set explicitly
|
||||
* @return updated builder instance
|
||||
*/
|
||||
TargetCreate securityToken(String securityToken);
|
||||
TargetCreate securityToken(@Size(min = 1, max = Target.SECURITY_TOKEN_MAX_SIZE) @NotNull String securityToken);
|
||||
|
||||
/**
|
||||
* @param address
|
||||
@@ -58,7 +63,7 @@ public interface TargetCreate {
|
||||
*
|
||||
* @return updated builder instance
|
||||
*/
|
||||
TargetCreate address(String address);
|
||||
TargetCreate address(@Size(max = Target.ADDRESS_MAX_SIZE) String address);
|
||||
|
||||
/**
|
||||
* @param lastTargetQuery
|
||||
@@ -69,10 +74,12 @@ public interface TargetCreate {
|
||||
|
||||
/**
|
||||
* @param status
|
||||
* for {@link Target#getUpdateStatus()}
|
||||
* for {@link Target#getUpdateStatus()} is
|
||||
* {@link TargetUpdateStatus#UNKNOWN} as default if not set
|
||||
* explicitly
|
||||
* @return updated builder instance
|
||||
*/
|
||||
TargetCreate status(TargetUpdateStatus status);
|
||||
TargetCreate status(@NotNull TargetUpdateStatus status);
|
||||
|
||||
/**
|
||||
* @return peek on current state of {@link Target} in the builder
|
||||
|
||||
@@ -8,9 +8,12 @@
|
||||
*/
|
||||
package org.eclipse.hawkbit.repository.builder;
|
||||
|
||||
import javax.validation.constraints.NotNull;
|
||||
import javax.validation.constraints.Size;
|
||||
|
||||
import org.eclipse.hawkbit.repository.model.NamedEntity;
|
||||
import org.eclipse.hawkbit.repository.model.Target;
|
||||
import org.eclipse.hawkbit.repository.model.TargetUpdateStatus;
|
||||
import org.hibernate.validator.constraints.NotEmpty;
|
||||
|
||||
/**
|
||||
* Builder to update an existing {@link Target} entry. Defines all fields that
|
||||
@@ -24,21 +27,21 @@ public interface TargetUpdate {
|
||||
* for {@link Target#getName()}
|
||||
* @return updated builder instance
|
||||
*/
|
||||
TargetUpdate name(@NotEmpty String name);
|
||||
TargetUpdate name(@Size(min = 1, max = NamedEntity.NAME_MAX_SIZE) @NotNull String name);
|
||||
|
||||
/**
|
||||
* @param description
|
||||
* for {@link Target#getDescription()}
|
||||
* @return updated builder instance
|
||||
*/
|
||||
TargetUpdate description(String description);
|
||||
TargetUpdate description(@Size(max = NamedEntity.DESCRIPTION_MAX_SIZE) String description);
|
||||
|
||||
/**
|
||||
* @param securityToken
|
||||
* for {@link Target#getSecurityToken()}
|
||||
* @return updated builder instance
|
||||
*/
|
||||
TargetUpdate securityToken(@NotEmpty String securityToken);
|
||||
TargetUpdate securityToken(@Size(min = 1, max = Target.SECURITY_TOKEN_MAX_SIZE) @NotNull String securityToken);
|
||||
|
||||
/**
|
||||
* @param address
|
||||
@@ -49,7 +52,7 @@ public interface TargetUpdate {
|
||||
*
|
||||
* @return updated builder instance
|
||||
*/
|
||||
TargetUpdate address(String address);
|
||||
TargetUpdate address(@Size(max = Target.ADDRESS_MAX_SIZE) String address);
|
||||
|
||||
/**
|
||||
* @param lastTargetQuery
|
||||
@@ -63,5 +66,5 @@ public interface TargetUpdate {
|
||||
* for {@link Target#getUpdateStatus()}
|
||||
* @return updated builder instance
|
||||
*/
|
||||
TargetUpdate status(TargetUpdateStatus status);
|
||||
TargetUpdate status(@NotNull TargetUpdateStatus status);
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ package org.eclipse.hawkbit.repository.model;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.eclipse.hawkbit.repository.TargetManagement;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
@@ -23,7 +24,7 @@ import org.springframework.util.CollectionUtils;
|
||||
public class DistributionSetAssignmentResult extends AssignmentResult<Target> {
|
||||
|
||||
private final List<String> assignedTargets;
|
||||
private final List<Long> actions;
|
||||
private final List<Action> actions;
|
||||
|
||||
private final TargetManagement targetManagement;
|
||||
|
||||
@@ -45,7 +46,7 @@ public class DistributionSetAssignmentResult extends AssignmentResult<Target> {
|
||||
*
|
||||
*/
|
||||
public DistributionSetAssignmentResult(final List<String> assignedTargets, final int assigned,
|
||||
final int alreadyAssigned, final List<Long> actions, final TargetManagement targetManagement) {
|
||||
final int alreadyAssigned, final List<Action> actions, final TargetManagement targetManagement) {
|
||||
super(assigned, alreadyAssigned, 0, Collections.emptyList(), Collections.emptyList());
|
||||
this.assignedTargets = assignedTargets;
|
||||
this.actions = actions;
|
||||
@@ -60,7 +61,7 @@ public class DistributionSetAssignmentResult extends AssignmentResult<Target> {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
return Collections.unmodifiableList(actions);
|
||||
return actions.stream().map(Action::getId).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -8,19 +8,34 @@
|
||||
*/
|
||||
package org.eclipse.hawkbit.repository.model;
|
||||
|
||||
import javax.validation.constraints.NotNull;
|
||||
import javax.validation.constraints.Size;
|
||||
|
||||
/**
|
||||
* Entities that have a name and description.
|
||||
*
|
||||
*/
|
||||
public interface NamedEntity extends TenantAwareBaseEntity {
|
||||
/**
|
||||
* Maximum length of name.
|
||||
*/
|
||||
public static final int NAME_MAX_SIZE = 64;
|
||||
|
||||
/**
|
||||
* Maximum length of description.
|
||||
*/
|
||||
public static final int DESCRIPTION_MAX_SIZE = 512;
|
||||
|
||||
/**
|
||||
* @return the description of the entity.
|
||||
*/
|
||||
@Size(max = DESCRIPTION_MAX_SIZE)
|
||||
String getDescription();
|
||||
|
||||
/**
|
||||
* @return the name of the entity.
|
||||
*/
|
||||
@Size(min = 1, max = NAME_MAX_SIZE)
|
||||
@NotNull
|
||||
String getName();
|
||||
}
|
||||
|
||||
@@ -20,6 +20,20 @@ import java.util.concurrent.TimeUnit;
|
||||
* </p>
|
||||
*/
|
||||
public interface Target extends NamedEntity {
|
||||
/**
|
||||
* Maximum length of controllerId.
|
||||
*/
|
||||
public static final int CONTROLLER_ID_MAX_SIZE = 64;
|
||||
|
||||
/**
|
||||
* Maximum length of securityToken.
|
||||
*/
|
||||
public static final int SECURITY_TOKEN_MAX_SIZE = 128;
|
||||
|
||||
/**
|
||||
* Maximum length of address.
|
||||
*/
|
||||
public static final int ADDRESS_MAX_SIZE = 512;
|
||||
|
||||
/**
|
||||
* @return business identifier of the {@link Target}
|
||||
|
||||
Reference in New Issue
Block a user