From 2ae08ffaef7e1407e3869cd04896b289034a60cc Mon Sep 17 00:00:00 2001 From: Melanie Retter Date: Tue, 23 Aug 2016 16:10:27 +0200 Subject: [PATCH] Add constraint to the database column maxAssignment Signed-off-by: Melanie Retter --- .../exception/AbstractServerRtException.java | 4 -- .../hawkbit/exception/SpServerError.java | 9 ++- .../MgmtSoftwareModuleTypeResource.java | 6 -- .../ConstraintViolationException.java | 61 +++++++++++++++++++ .../EntityAlreadyExistsException.java | 7 +-- .../repository/jpa/JpaSoftwareManagement.java | 5 ++ .../jpa/model/JpaSoftwareModuleType.java | 2 + .../exception/ResponseExceptionHandler.java | 7 ++- 8 files changed, 78 insertions(+), 23 deletions(-) create mode 100644 hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/exception/ConstraintViolationException.java diff --git a/hawkbit-core/src/main/java/org/eclipse/hawkbit/exception/AbstractServerRtException.java b/hawkbit-core/src/main/java/org/eclipse/hawkbit/exception/AbstractServerRtException.java index cb695d2f7..429df6ed5 100644 --- a/hawkbit-core/src/main/java/org/eclipse/hawkbit/exception/AbstractServerRtException.java +++ b/hawkbit-core/src/main/java/org/eclipse/hawkbit/exception/AbstractServerRtException.java @@ -9,11 +9,7 @@ package org.eclipse.hawkbit.exception; /** - * - * - * * Generic Custom Exception to wrap the Runtime and checked exception - * */ public abstract class AbstractServerRtException extends RuntimeException { diff --git a/hawkbit-core/src/main/java/org/eclipse/hawkbit/exception/SpServerError.java b/hawkbit-core/src/main/java/org/eclipse/hawkbit/exception/SpServerError.java index 5acd11463..82044ff54 100644 --- a/hawkbit-core/src/main/java/org/eclipse/hawkbit/exception/SpServerError.java +++ b/hawkbit-core/src/main/java/org/eclipse/hawkbit/exception/SpServerError.java @@ -9,11 +9,7 @@ package org.eclipse.hawkbit.exception; /** - * - * - * * Define the Error code for Error handling - * */ public enum SpServerError { @@ -25,7 +21,10 @@ public enum SpServerError { * */ SP_REPO_ENTITY_ALRREADY_EXISTS("hawkbit.server.error.repo.entitiyAlreayExists", "The given entity already exists in database"), - + /** + * + */ + SP_REPO_CONSTRAINT_VIOLATION("hawkbit.server.error.repo.constraintViolation", "The given entity violates a constraint"), /** * */ diff --git a/hawkbit-mgmt-resource/src/main/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtSoftwareModuleTypeResource.java b/hawkbit-mgmt-resource/src/main/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtSoftwareModuleTypeResource.java index 74829df31..353cf16e5 100644 --- a/hawkbit-mgmt-resource/src/main/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtSoftwareModuleTypeResource.java +++ b/hawkbit-mgmt-resource/src/main/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtSoftwareModuleTypeResource.java @@ -113,12 +113,6 @@ public class MgmtSoftwareModuleTypeResource implements MgmtSoftwareModuleTypeRes public ResponseEntity> createSoftwareModuleTypes( @RequestBody final List softwareModuleTypes) { - for (final MgmtSoftwareModuleTypeRequestBodyPost softwareModuleType : softwareModuleTypes) { - if (softwareModuleType.getMaxAssignments() <= 0) { - return new ResponseEntity<>(HttpStatus.BAD_REQUEST); - } - } - final List createdSoftwareModules = this.softwareManagement.createSoftwareModuleType( MgmtSoftwareModuleTypeMapper.smFromRequest(entityFactory, softwareModuleTypes)); diff --git a/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/exception/ConstraintViolationException.java b/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/exception/ConstraintViolationException.java new file mode 100644 index 000000000..4c349a806 --- /dev/null +++ b/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/exception/ConstraintViolationException.java @@ -0,0 +1,61 @@ +/** + * Copyright (c) 2015 Bosch Software Innovations GmbH and others. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + */ +package org.eclipse.hawkbit.repository.exception; + +import org.eclipse.hawkbit.exception.AbstractServerRtException; +import org.eclipse.hawkbit.exception.SpServerError; + +/** + * the {@link ConstraintViolationException} is thrown when an entity is tried to + * be saved and violates a constraints, like value > 0 + */ +public class ConstraintViolationException extends AbstractServerRtException { + + private static final long serialVersionUID = 1L; + private static final SpServerError THIS_ERROR = SpServerError.SP_REPO_CONSTRAINT_VIOLATION; + + /** + * Default constructor. + */ + public ConstraintViolationException() { + super(THIS_ERROR); + } + + /** + * Parameterized constructor. + * + * @param cause + * of the exception + */ + public ConstraintViolationException(final Throwable cause) { + super(THIS_ERROR, cause); + } + + /** + * Parameterized constructor. + * + * @param message + * of the exception + * @param cause + * of the exception + */ + public ConstraintViolationException(final String message, final Throwable cause) { + super(message, THIS_ERROR, cause); + } + + /** + * Parameterized constructor. + * + * @param message + * of the exception + */ + public ConstraintViolationException(final String message) { + super(message, THIS_ERROR); + } +} diff --git a/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/exception/EntityAlreadyExistsException.java b/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/exception/EntityAlreadyExistsException.java index 73b14c57a..cf2c36cee 100644 --- a/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/exception/EntityAlreadyExistsException.java +++ b/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/exception/EntityAlreadyExistsException.java @@ -8,15 +8,12 @@ */ package org.eclipse.hawkbit.repository.exception; -import org.eclipse.hawkbit.exception.SpServerError; import org.eclipse.hawkbit.exception.AbstractServerRtException; +import org.eclipse.hawkbit.exception.SpServerError; /** - * the {@link EntityAlreadyExistsException} is thrown when a entity is tried to + * the {@link EntityAlreadyExistsException} is thrown when an entity is tried to * be saved which already exists or which violates unique key constraints. - * - * - * */ public class EntityAlreadyExistsException extends AbstractServerRtException { diff --git a/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/JpaSoftwareManagement.java b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/JpaSoftwareManagement.java index 3f8c1a54b..c0cad26cc 100644 --- a/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/JpaSoftwareManagement.java +++ b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/JpaSoftwareManagement.java @@ -31,6 +31,7 @@ import org.eclipse.hawkbit.repository.SoftwareManagement; import org.eclipse.hawkbit.repository.SoftwareModuleFields; import org.eclipse.hawkbit.repository.SoftwareModuleMetadataFields; import org.eclipse.hawkbit.repository.SoftwareModuleTypeFields; +import org.eclipse.hawkbit.repository.exception.ConstraintViolationException; import org.eclipse.hawkbit.repository.exception.EntityAlreadyExistsException; import org.eclipse.hawkbit.repository.exception.EntityNotFoundException; import org.eclipse.hawkbit.repository.jpa.model.JpaDistributionSet; @@ -514,6 +515,10 @@ public class JpaSoftwareManagement implements SoftwareManagement { throw new EntityAlreadyExistsException("Given type contains an Id!"); } + if (type.getMaxAssignments() <= 0) { + throw new ConstraintViolationException("The value for max assignments has to be greater than 0!"); + } + return softwareModuleTypeRepository.save((JpaSoftwareModuleType) type); } diff --git a/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/model/JpaSoftwareModuleType.java b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/model/JpaSoftwareModuleType.java index f2a716c40..f521922b6 100644 --- a/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/model/JpaSoftwareModuleType.java +++ b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/model/JpaSoftwareModuleType.java @@ -13,6 +13,7 @@ import javax.persistence.Entity; import javax.persistence.Index; import javax.persistence.Table; import javax.persistence.UniqueConstraint; +import javax.validation.constraints.Min; import org.eclipse.hawkbit.repository.model.SoftwareModuleType; @@ -36,6 +37,7 @@ public class JpaSoftwareModuleType extends AbstractJpaNamedEntity implements Sof private String key; @Column(name = "max_ds_assignments", nullable = false) + @Min(1) private int maxAssignments; @Column(name = "colour", nullable = true, length = 16) diff --git a/hawkbit-rest-core/src/main/java/org/eclipse/hawkbit/rest/exception/ResponseExceptionHandler.java b/hawkbit-rest-core/src/main/java/org/eclipse/hawkbit/rest/exception/ResponseExceptionHandler.java index 81d70103b..744624667 100644 --- a/hawkbit-rest-core/src/main/java/org/eclipse/hawkbit/rest/exception/ResponseExceptionHandler.java +++ b/hawkbit-rest-core/src/main/java/org/eclipse/hawkbit/rest/exception/ResponseExceptionHandler.java @@ -15,8 +15,8 @@ import java.util.Map; import javax.servlet.http.HttpServletRequest; import org.apache.commons.lang3.exception.ExceptionUtils; -import org.eclipse.hawkbit.exception.SpServerError; import org.eclipse.hawkbit.exception.AbstractServerRtException; +import org.eclipse.hawkbit.exception.SpServerError; import org.eclipse.hawkbit.repository.exception.MultiPartFileUploadException; import org.eclipse.hawkbit.rest.json.model.ExceptionInfo; import org.slf4j.Logger; @@ -63,6 +63,7 @@ public class ResponseExceptionHandler { ERROR_TO_HTTP_STATUS.put(SpServerError.SP_DS_MODULE_UNSUPPORTED, HttpStatus.BAD_REQUEST); ERROR_TO_HTTP_STATUS.put(SpServerError.SP_DS_TYPE_UNDEFINED, HttpStatus.BAD_REQUEST); ERROR_TO_HTTP_STATUS.put(SpServerError.SP_REPO_TENANT_NOT_EXISTS, HttpStatus.BAD_REQUEST); + ERROR_TO_HTTP_STATUS.put(SpServerError.SP_REPO_CONSTRAINT_VIOLATION, HttpStatus.BAD_REQUEST); ERROR_TO_HTTP_STATUS.put(SpServerError.SP_ENTITY_LOCKED, HttpStatus.LOCKED); ERROR_TO_HTTP_STATUS.put(SpServerError.SP_ROLLOUT_ILLEGAL_STATE, HttpStatus.BAD_REQUEST); ERROR_TO_HTTP_STATUS.put(SpServerError.SP_CONFIGURATION_VALUE_INVALID, HttpStatus.BAD_REQUEST); @@ -75,8 +76,8 @@ public class ResponseExceptionHandler { } /** - * method for handling exception of type AbstractServerRtException. Called by the - * Spring-Framework for exception handling. + * method for handling exception of type AbstractServerRtException. Called + * by the Spring-Framework for exception handling. * * @param request * the Http request