From d2693e2f4907601c4ece1dfb15ab1eedfac91711 Mon Sep 17 00:00:00 2001 From: Melanie Retter Date: Tue, 23 Aug 2016 11:29:24 +0200 Subject: [PATCH 01/15] MaxAssignment smaller than 0 is not allowed -> BadRequest Status code Signed-off-by: Melanie Retter --- .../resource/MgmtSoftwareModuleTypeResource.java | 6 ++++++ .../MgmtSoftwareModuleTypeResourceTest.java | 13 +++++++++++++ 2 files changed, 19 insertions(+) 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 353cf16e5..74829df31 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,6 +113,12 @@ 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-mgmt-resource/src/test/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtSoftwareModuleTypeResourceTest.java b/hawkbit-mgmt-resource/src/test/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtSoftwareModuleTypeResourceTest.java index 36d975138..af5b18ade 100644 --- a/hawkbit-mgmt-resource/src/test/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtSoftwareModuleTypeResourceTest.java +++ b/hawkbit-mgmt-resource/src/test/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtSoftwareModuleTypeResourceTest.java @@ -133,6 +133,19 @@ public class MgmtSoftwareModuleTypeResourceTest extends AbstractRestIntegrationT .andExpect(jsonPath("$content.[2].key", equalTo("test123"))).andExpect(jsonPath("$total", equalTo(4))); } + @Test + @WithUser(principal = "uploadTester", allSpPermissions = true) + @Description("Checks the correct behaviour of /rest/v1/softwaremoduletypes POST requests when max assignment is <= 0") + public void createSoftwareModuleTypesInvalidAssignmentBadRequest() throws JSONException, Exception { + + final List types = new ArrayList<>(); + types.add(entityFactory.generateSoftwareModuleType("test1", "TestName1", "Desc1", -1)); + + mvc.perform(post("/rest/v1/softwaremoduletypes/").content(JsonBuilder.softwareModuleTypes(types)) + .contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON)) + .andDo(MockMvcResultPrinter.print()).andExpect(status().isBadRequest()); + } + @Test @WithUser(principal = "uploadTester", allSpPermissions = true) @Description("Checks the correct behaviour of /rest/v1/softwaremoduletypes POST requests.") From 42b21d6308c2f175f59af2667480bfe7efefacb4 Mon Sep 17 00:00:00 2001 From: Melanie Retter Date: Tue, 23 Aug 2016 13:35:03 +0200 Subject: [PATCH 02/15] Extend test for invalid assignment of SoftwareModuleType Signed-off-by: Melanie Retter --- .../resource/MgmtSoftwareModuleTypeResourceTest.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/hawkbit-mgmt-resource/src/test/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtSoftwareModuleTypeResourceTest.java b/hawkbit-mgmt-resource/src/test/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtSoftwareModuleTypeResourceTest.java index af5b18ade..a073fff0e 100644 --- a/hawkbit-mgmt-resource/src/test/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtSoftwareModuleTypeResourceTest.java +++ b/hawkbit-mgmt-resource/src/test/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtSoftwareModuleTypeResourceTest.java @@ -135,11 +135,18 @@ public class MgmtSoftwareModuleTypeResourceTest extends AbstractRestIntegrationT @Test @WithUser(principal = "uploadTester", allSpPermissions = true) - @Description("Checks the correct behaviour of /rest/v1/softwaremoduletypes POST requests when max assignment is <= 0") + @Description("Checks the correct behaviour of /rest/v1/softwaremoduletypes POST requests when max assignment is smaller than 0") public void createSoftwareModuleTypesInvalidAssignmentBadRequest() throws JSONException, Exception { final List types = new ArrayList<>(); - types.add(entityFactory.generateSoftwareModuleType("test1", "TestName1", "Desc1", -1)); + types.add(entityFactory.generateSoftwareModuleType("test-1", "TestName-1", "Desc-1", -1)); + + mvc.perform(post("/rest/v1/softwaremoduletypes/").content(JsonBuilder.softwareModuleTypes(types)) + .contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON)) + .andDo(MockMvcResultPrinter.print()).andExpect(status().isBadRequest()); + + types.clear(); + types.add(entityFactory.generateSoftwareModuleType("test0", "TestName0", "Desc0", 0)); mvc.perform(post("/rest/v1/softwaremoduletypes/").content(JsonBuilder.softwareModuleTypes(types)) .contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON)) From 2ae08ffaef7e1407e3869cd04896b289034a60cc Mon Sep 17 00:00:00 2001 From: Melanie Retter Date: Tue, 23 Aug 2016 16:10:27 +0200 Subject: [PATCH 03/15] 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 From 8f35f5495d162e8560cb4ee23a6be7c911d8a4a8 Mon Sep 17 00:00:00 2001 From: Melanie Retter Date: Tue, 23 Aug 2016 16:43:18 +0200 Subject: [PATCH 04/15] Add db migration skripts Signed-off-by: Melanie Retter --- .../H2/V1_9_0__software_module_type_constraint__H2.sql | 2 ++ .../MYSQL/V1_9_0__software_module_type_constraint__MYSQL.sql | 2 ++ 2 files changed, 4 insertions(+) create mode 100644 hawkbit-repository/hawkbit-repository-jpa/src/main/resources/db/migration/H2/V1_9_0__software_module_type_constraint__H2.sql create mode 100644 hawkbit-repository/hawkbit-repository-jpa/src/main/resources/db/migration/MYSQL/V1_9_0__software_module_type_constraint__MYSQL.sql diff --git a/hawkbit-repository/hawkbit-repository-jpa/src/main/resources/db/migration/H2/V1_9_0__software_module_type_constraint__H2.sql b/hawkbit-repository/hawkbit-repository-jpa/src/main/resources/db/migration/H2/V1_9_0__software_module_type_constraint__H2.sql new file mode 100644 index 000000000..88f12d65d --- /dev/null +++ b/hawkbit-repository/hawkbit-repository-jpa/src/main/resources/db/migration/H2/V1_9_0__software_module_type_constraint__H2.sql @@ -0,0 +1,2 @@ +alter table sp_software_module_type + add constraint maxAssignmentCheck check (maxAssignments > 0); \ No newline at end of file diff --git a/hawkbit-repository/hawkbit-repository-jpa/src/main/resources/db/migration/MYSQL/V1_9_0__software_module_type_constraint__MYSQL.sql b/hawkbit-repository/hawkbit-repository-jpa/src/main/resources/db/migration/MYSQL/V1_9_0__software_module_type_constraint__MYSQL.sql new file mode 100644 index 000000000..88f12d65d --- /dev/null +++ b/hawkbit-repository/hawkbit-repository-jpa/src/main/resources/db/migration/MYSQL/V1_9_0__software_module_type_constraint__MYSQL.sql @@ -0,0 +1,2 @@ +alter table sp_software_module_type + add constraint maxAssignmentCheck check (maxAssignments > 0); \ No newline at end of file From 9f5a2ea3d51839d0b756f36693e19d75389a9066 Mon Sep 17 00:00:00 2001 From: Melanie Retter Date: Tue, 23 Aug 2016 17:11:33 +0200 Subject: [PATCH 05/15] Delete ConstraintViolationException, correct db migration script Signed-off-by: Melanie Retter --- .../ConstraintViolationException.java | 61 ------------------- .../repository/jpa/JpaSoftwareManagement.java | 5 -- ...0__software_module_type_constraint__H2.sql | 2 +- ...software_module_type_constraint__MYSQL.sql | 2 +- .../exception/ResponseExceptionHandler.java | 23 ++++++- 5 files changed, 24 insertions(+), 69 deletions(-) delete mode 100644 hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/exception/ConstraintViolationException.java 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 deleted file mode 100644 index 4c349a806..000000000 --- a/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/exception/ConstraintViolationException.java +++ /dev/null @@ -1,61 +0,0 @@ -/** - * 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-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 c0cad26cc..3f8c1a54b 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,7 +31,6 @@ 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; @@ -515,10 +514,6 @@ 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/resources/db/migration/H2/V1_9_0__software_module_type_constraint__H2.sql b/hawkbit-repository/hawkbit-repository-jpa/src/main/resources/db/migration/H2/V1_9_0__software_module_type_constraint__H2.sql index 88f12d65d..dfdb6ee1d 100644 --- a/hawkbit-repository/hawkbit-repository-jpa/src/main/resources/db/migration/H2/V1_9_0__software_module_type_constraint__H2.sql +++ b/hawkbit-repository/hawkbit-repository-jpa/src/main/resources/db/migration/H2/V1_9_0__software_module_type_constraint__H2.sql @@ -1,2 +1,2 @@ alter table sp_software_module_type - add constraint maxAssignmentCheck check (maxAssignments > 0); \ No newline at end of file + add constraint maxAssignmentCheck check (max_ds_assignments > 0); \ No newline at end of file diff --git a/hawkbit-repository/hawkbit-repository-jpa/src/main/resources/db/migration/MYSQL/V1_9_0__software_module_type_constraint__MYSQL.sql b/hawkbit-repository/hawkbit-repository-jpa/src/main/resources/db/migration/MYSQL/V1_9_0__software_module_type_constraint__MYSQL.sql index 88f12d65d..dfdb6ee1d 100644 --- a/hawkbit-repository/hawkbit-repository-jpa/src/main/resources/db/migration/MYSQL/V1_9_0__software_module_type_constraint__MYSQL.sql +++ b/hawkbit-repository/hawkbit-repository-jpa/src/main/resources/db/migration/MYSQL/V1_9_0__software_module_type_constraint__MYSQL.sql @@ -1,2 +1,2 @@ alter table sp_software_module_type - add constraint maxAssignmentCheck check (maxAssignments > 0); \ No newline at end of file + add constraint maxAssignmentCheck check (max_ds_assignments > 0); \ No newline at end of file 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 744624667..0c9f7cf68 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 @@ -13,6 +13,7 @@ import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; +import javax.validation.ConstraintViolationException; import org.apache.commons.lang3.exception.ExceptionUtils; import org.eclipse.hawkbit.exception.AbstractServerRtException; @@ -63,7 +64,6 @@ 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); @@ -121,6 +121,27 @@ public class ResponseExceptionHandler { return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST); } + /** + * Method for handling exception of type ConstraintViolationException which + * is thrown in case the request is rejected due to a constraint violation. + * Called by the Spring-Framework for exception handling. + * + * @param request + * the Http request + * @param ex + * the exception which occurred + * @return the entity to be responded containing the exception information + * as entity. + */ + @ExceptionHandler(ConstraintViolationException.class) + public ResponseEntity handleConstraintViolationException(final HttpServletRequest request, + final Exception ex) { + logRequest(request, ex); + final ExceptionInfo response = createExceptionInfo( + new ConstraintViolationException(((ConstraintViolationException) ex).getConstraintViolations())); + return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST); + } + /** * Method for handling exception of type {@link MultipartException} which is * thrown in case the request body is not well formed and cannot be From 693eaad9779c76e01b96a58b403ccab1b0d3d304 Mon Sep 17 00:00:00 2001 From: Melanie Retter Date: Tue, 23 Aug 2016 17:32:07 +0200 Subject: [PATCH 06/15] Delete SP_REPO_CONSTRAINT_VIOLATION Signed-off-by: Melanie Retter --- .../java/org/eclipse/hawkbit/exception/SpServerError.java | 4 ---- 1 file changed, 4 deletions(-) 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 82044ff54..29ab123e5 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 @@ -21,10 +21,6 @@ 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"), /** * */ From 3ac8dc93a3e96ae3fc13243960bf33f23d85eb49 Mon Sep 17 00:00:00 2001 From: Melanie Retter Date: Wed, 24 Aug 2016 09:49:05 +0200 Subject: [PATCH 07/15] Add Test for SoftwareManagement#createSoftwareModuleType Signed-off-by: Melanie Retter --- ...__software_module_type_constraint__H2.sql} | 0 ...oftware_module_type_constraint__MYSQL.sql} | 0 .../jpa/SoftwareManagementTest.java | 19 +++++++++++++++---- 3 files changed, 15 insertions(+), 4 deletions(-) rename hawkbit-repository/hawkbit-repository-jpa/src/main/resources/db/migration/H2/{V1_9_0__software_module_type_constraint__H2.sql => V1_7_0__software_module_type_constraint__H2.sql} (100%) rename hawkbit-repository/hawkbit-repository-jpa/src/main/resources/db/migration/MYSQL/{V1_9_0__software_module_type_constraint__MYSQL.sql => V1_7_0__software_module_type_constraint__MYSQL.sql} (100%) diff --git a/hawkbit-repository/hawkbit-repository-jpa/src/main/resources/db/migration/H2/V1_9_0__software_module_type_constraint__H2.sql b/hawkbit-repository/hawkbit-repository-jpa/src/main/resources/db/migration/H2/V1_7_0__software_module_type_constraint__H2.sql similarity index 100% rename from hawkbit-repository/hawkbit-repository-jpa/src/main/resources/db/migration/H2/V1_9_0__software_module_type_constraint__H2.sql rename to hawkbit-repository/hawkbit-repository-jpa/src/main/resources/db/migration/H2/V1_7_0__software_module_type_constraint__H2.sql diff --git a/hawkbit-repository/hawkbit-repository-jpa/src/main/resources/db/migration/MYSQL/V1_9_0__software_module_type_constraint__MYSQL.sql b/hawkbit-repository/hawkbit-repository-jpa/src/main/resources/db/migration/MYSQL/V1_7_0__software_module_type_constraint__MYSQL.sql similarity index 100% rename from hawkbit-repository/hawkbit-repository-jpa/src/main/resources/db/migration/MYSQL/V1_9_0__software_module_type_constraint__MYSQL.sql rename to hawkbit-repository/hawkbit-repository-jpa/src/main/resources/db/migration/MYSQL/V1_7_0__software_module_type_constraint__MYSQL.sql diff --git a/hawkbit-repository/hawkbit-repository-jpa/src/test/java/org/eclipse/hawkbit/repository/jpa/SoftwareManagementTest.java b/hawkbit-repository/hawkbit-repository-jpa/src/test/java/org/eclipse/hawkbit/repository/jpa/SoftwareManagementTest.java index 75633a85a..739cf7f64 100644 --- a/hawkbit-repository/hawkbit-repository-jpa/src/test/java/org/eclipse/hawkbit/repository/jpa/SoftwareManagementTest.java +++ b/hawkbit-repository/hawkbit-repository-jpa/src/test/java/org/eclipse/hawkbit/repository/jpa/SoftwareManagementTest.java @@ -19,6 +19,8 @@ import java.util.Collections; import java.util.Iterator; import java.util.List; +import javax.validation.ConstraintViolationException; + import org.apache.commons.lang3.RandomUtils; import org.eclipse.hawkbit.repository.exception.EntityAlreadyExistsException; import org.eclipse.hawkbit.repository.exception.EntityNotFoundException; @@ -785,9 +787,20 @@ public class SoftwareManagementTest extends AbstractJpaIntegrationTestWithMongoD } } + @Test + @Description("Verifies that the creation of a softwareModuleType is failing because of invalid max assignment") + public void createSoftwareModuleTypesFailsWithInvalidMaxAssignment() { + try { + softwareManagement.createSoftwareModuleType(new JpaSoftwareModuleType("type", "name", "desc", 0)); + fail("should not have worked as max assignment is invalid. Should be greater than 0."); + } catch (final ConstraintViolationException e) { + + } + } + @Test @Description("Verfies that multiple types are created as requested.") - public void createMultipleoftwareModuleTypes() { + public void createMultipleSoftwareModuleTypes() { final List created = softwareManagement.createSoftwareModuleType( Lists.newArrayList(new JpaSoftwareModuleType("thetype", "thename", "desc", 100), new JpaSoftwareModuleType("thetype2", "thename2", "desc2", 100))); @@ -797,7 +810,7 @@ public class SoftwareManagementTest extends AbstractJpaIntegrationTestWithMongoD } @Test - @Description("Verfies that sofwtare modules are resturned that are assigned to given DS.") + @Description("Verfies that software modules are resturned that are assigned to given DS.") public void findSoftwareModuleByAssignedTo() { // test meta data final SoftwareModuleType testType = softwareManagement @@ -810,8 +823,6 @@ public class SoftwareManagementTest extends AbstractJpaIntegrationTestWithMongoD softwareManagement.createSoftwareModule(new JpaSoftwareModule(testType, "asis", "found", null, "")); final SoftwareModule one = softwareManagement .createSoftwareModule(new JpaSoftwareModule(testType, "found", "b", null, "")); - final SoftwareModule two = softwareManagement - .createSoftwareModule(new JpaSoftwareModule(testType, "found", "c", null, "")); // one soft deleted final SoftwareModule deleted = softwareManagement From 679f465db1739a53a903d458d7a0abb678e36cc2 Mon Sep 17 00:00:00 2001 From: Melanie Retter Date: Wed, 24 Aug 2016 13:07:37 +0200 Subject: [PATCH 08/15] Insert custom constraintViolationException Signed-off-by: Melanie Retter --- .../hawkbit/exception/SpServerError.java | 4 +++ .../ConstraintViolationException.java | 33 +++++++++++++++++++ .../exception/ResponseExceptionHandler.java | 16 +++++++-- 3 files changed, 51 insertions(+), 2 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/SpServerError.java b/hawkbit-core/src/main/java/org/eclipse/hawkbit/exception/SpServerError.java index 29ab123e5..bda7a5d77 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 @@ -21,6 +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 cannot be saved due to Constraint Violation"), /** * */ 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..7e1564e3d --- /dev/null +++ b/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/exception/ConstraintViolationException.java @@ -0,0 +1,33 @@ +/** + * 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 which has constraint violations + * + */ +public class ConstraintViolationException extends AbstractServerRtException { + + private static final long serialVersionUID = 1L; + + /** + * Constructor for {@link ConstraintViolationException} + * + * @param message + * the message to be displayed as exception message + */ + public ConstraintViolationException(final String message) { + super(message, SpServerError.SP_REPO_CONSTRAINT_VIOLATION); + } + +} 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 0c9f7cf68..89beaa4d7 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 @@ -8,11 +8,14 @@ */ package org.eclipse.hawkbit.rest.exception; +import java.util.ArrayList; import java.util.EnumMap; import java.util.List; import java.util.Map; +import java.util.Set; import javax.servlet.http.HttpServletRequest; +import javax.validation.ConstraintViolation; import javax.validation.ConstraintViolationException; import org.apache.commons.lang3.exception.ExceptionUtils; @@ -137,8 +140,17 @@ public class ResponseExceptionHandler { public ResponseEntity handleConstraintViolationException(final HttpServletRequest request, final Exception ex) { logRequest(request, ex); - final ExceptionInfo response = createExceptionInfo( - new ConstraintViolationException(((ConstraintViolationException) ex).getConstraintViolations())); + + final ExceptionInfo response = null; + final Set> violations = ((ConstraintViolationException) ex).getConstraintViolations(); + + final List messages = new ArrayList<>(); + violations.stream() + .forEach(violation -> messages.add(violation.getPropertyPath() + " " + violation.getMessage() + ". ")); + + // response = createExceptionInfo(new + // org.eclipse.hawkbit.repository.exception.ConstraintViolationException( + // messages.forEach(StringBuilder::append(this)))); return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST); } From 50a86cdb7bb72e5d37c8c8eac63b4ca82428b837 Mon Sep 17 00:00:00 2001 From: Melanie Retter Date: Wed, 24 Aug 2016 13:48:18 +0200 Subject: [PATCH 09/15] ConstraintViolationException can contain mulitple violation messages Signed-off-by: Melanie Retter --- .../exception/ConstraintViolationException.java | 4 ++-- .../rest/exception/ResponseExceptionHandler.java | 11 ++++++----- 2 files changed, 8 insertions(+), 7 deletions(-) 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 index 7e1564e3d..f2e50728b 100644 --- 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 @@ -12,8 +12,8 @@ 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 which has constraint violations + * the {@link ConstraintViolationException} is thrown when an entity is tried to + * be saved which has constraint violations * */ public class ConstraintViolationException extends AbstractServerRtException { 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 89beaa4d7..ed2bbe17e 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 @@ -13,6 +13,7 @@ import java.util.EnumMap; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.stream.Collectors; import javax.servlet.http.HttpServletRequest; import javax.validation.ConstraintViolation; @@ -141,16 +142,16 @@ public class ResponseExceptionHandler { final Exception ex) { logRequest(request, ex); - final ExceptionInfo response = null; final Set> violations = ((ConstraintViolationException) ex).getConstraintViolations(); final List messages = new ArrayList<>(); violations.stream() - .forEach(violation -> messages.add(violation.getPropertyPath() + " " + violation.getMessage() + ". ")); + .forEach(violation -> messages.add(violation.getPropertyPath() + " " + violation.getMessage() + ".")); + + final ExceptionInfo response = createExceptionInfo( + new org.eclipse.hawkbit.repository.exception.ConstraintViolationException( + messages.stream().collect(Collectors.joining(" ")))); - // response = createExceptionInfo(new - // org.eclipse.hawkbit.repository.exception.ConstraintViolationException( - // messages.forEach(StringBuilder::append(this)))); return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST); } From d323f676487cb726afeb4ca13c788bf10ff4aaec Mon Sep 17 00:00:00 2001 From: Melanie Retter Date: Wed, 24 Aug 2016 16:38:18 +0200 Subject: [PATCH 10/15] Refactor ConstraintViolationException Signed-off-by: Melanie Retter --- .../ConstraintViolationException.java | 26 +++++++++++++++---- .../exception/ResponseExceptionHandler.java | 12 +-------- 2 files changed, 22 insertions(+), 16 deletions(-) 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 index f2e50728b..b0956ba7e 100644 --- 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 @@ -8,13 +8,19 @@ */ package org.eclipse.hawkbit.repository.exception; +import java.util.ArrayList; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; + +import javax.validation.ConstraintViolation; + 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 which has constraint violations - * */ public class ConstraintViolationException extends AbstractServerRtException { @@ -23,11 +29,21 @@ public class ConstraintViolationException extends AbstractServerRtException { /** * Constructor for {@link ConstraintViolationException} * - * @param message - * the message to be displayed as exception message + * @param ex + * the javax.validation.ConstraintViolationException which is + * thrown */ - public ConstraintViolationException(final String message) { - super(message, SpServerError.SP_REPO_CONSTRAINT_VIOLATION); + public ConstraintViolationException(final javax.validation.ConstraintViolationException ex) { + super(setExceptionMessage(ex), SpServerError.SP_REPO_CONSTRAINT_VIOLATION); + } + + public static String setExceptionMessage(final javax.validation.ConstraintViolationException ex) { + final Set> violations = ex.getConstraintViolations(); + final List messages = new ArrayList<>(); + violations.stream() + .forEach(violation -> messages.add(violation.getPropertyPath() + " " + violation.getMessage() + ".")); + + return messages.stream().collect(Collectors.joining(" ")); } } 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 ed2bbe17e..41313d6ff 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 @@ -8,15 +8,11 @@ */ package org.eclipse.hawkbit.rest.exception; -import java.util.ArrayList; import java.util.EnumMap; import java.util.List; import java.util.Map; -import java.util.Set; -import java.util.stream.Collectors; import javax.servlet.http.HttpServletRequest; -import javax.validation.ConstraintViolation; import javax.validation.ConstraintViolationException; import org.apache.commons.lang3.exception.ExceptionUtils; @@ -142,15 +138,9 @@ public class ResponseExceptionHandler { final Exception ex) { logRequest(request, ex); - final Set> violations = ((ConstraintViolationException) ex).getConstraintViolations(); - - final List messages = new ArrayList<>(); - violations.stream() - .forEach(violation -> messages.add(violation.getPropertyPath() + " " + violation.getMessage() + ".")); - final ExceptionInfo response = createExceptionInfo( new org.eclipse.hawkbit.repository.exception.ConstraintViolationException( - messages.stream().collect(Collectors.joining(" ")))); + (ConstraintViolationException) ex)); return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST); } From d437d86f26debad1b1954078be409059d301ad7f Mon Sep 17 00:00:00 2001 From: Melanie Retter Date: Wed, 24 Aug 2016 16:55:17 +0200 Subject: [PATCH 11/15] Correct test description Signed-off-by: Melanie Retter --- .../mgmt/rest/resource/MgmtSoftwareModuleTypeResourceTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hawkbit-mgmt-resource/src/test/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtSoftwareModuleTypeResourceTest.java b/hawkbit-mgmt-resource/src/test/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtSoftwareModuleTypeResourceTest.java index a073fff0e..137628e35 100644 --- a/hawkbit-mgmt-resource/src/test/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtSoftwareModuleTypeResourceTest.java +++ b/hawkbit-mgmt-resource/src/test/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtSoftwareModuleTypeResourceTest.java @@ -135,7 +135,7 @@ public class MgmtSoftwareModuleTypeResourceTest extends AbstractRestIntegrationT @Test @WithUser(principal = "uploadTester", allSpPermissions = true) - @Description("Checks the correct behaviour of /rest/v1/softwaremoduletypes POST requests when max assignment is smaller than 0") + @Description("Checks the correct behaviour of /rest/v1/softwaremoduletypes POST requests when max assignment is smaller than 1") public void createSoftwareModuleTypesInvalidAssignmentBadRequest() throws JSONException, Exception { final List types = new ArrayList<>(); From d84365e3d2e53e186ea506b215da684bf1af89bc Mon Sep 17 00:00:00 2001 From: Melanie Retter Date: Thu, 25 Aug 2016 09:51:16 +0200 Subject: [PATCH 12/15] Refactor Signed-off-by: Melanie Retter --- .../ConstraintViolationException.java | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) 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 index b0956ba7e..7b4c87771 100644 --- 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 @@ -26,6 +26,8 @@ public class ConstraintViolationException extends AbstractServerRtException { private static final long serialVersionUID = 1L; + private static final String WHITESPACE = " "; + /** * Constructor for {@link ConstraintViolationException} * @@ -34,16 +36,25 @@ public class ConstraintViolationException extends AbstractServerRtException { * thrown */ public ConstraintViolationException(final javax.validation.ConstraintViolationException ex) { - super(setExceptionMessage(ex), SpServerError.SP_REPO_CONSTRAINT_VIOLATION); + super(getExceptionMessage(ex), SpServerError.SP_REPO_CONSTRAINT_VIOLATION); } - public static String setExceptionMessage(final javax.validation.ConstraintViolationException ex) { + /** + * Uses the information of + * {@link javax.validation.ConstraintViolationException} to provide a proper + * error message for {@link ConstraintViolationException} + * + * @param ex + * javax.validation.ConstraintViolationException which is thrown + * @return message String with proper error information + */ + public static String getExceptionMessage(final javax.validation.ConstraintViolationException ex) { final Set> violations = ex.getConstraintViolations(); final List messages = new ArrayList<>(); - violations.stream() - .forEach(violation -> messages.add(violation.getPropertyPath() + " " + violation.getMessage() + ".")); + violations.stream().forEach( + violation -> messages.add(violation.getPropertyPath() + WHITESPACE + violation.getMessage() + ".")); - return messages.stream().collect(Collectors.joining(" ")); + return messages.stream().collect(Collectors.joining(WHITESPACE)); } } From 34daea319c2da0b741c2aab0866d95df12b701ce Mon Sep 17 00:00:00 2001 From: Melanie Retter Date: Thu, 25 Aug 2016 10:39:50 +0200 Subject: [PATCH 13/15] Rename constant WHITESPACE Signed-off-by: Melanie Retter --- .../repository/exception/ConstraintViolationException.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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 index 7b4c87771..5150226a0 100644 --- 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 @@ -26,7 +26,7 @@ public class ConstraintViolationException extends AbstractServerRtException { private static final long serialVersionUID = 1L; - private static final String WHITESPACE = " "; + private static final String MESSAGE_FORMATTER_SEPARATOR = " "; /** * Constructor for {@link ConstraintViolationException} @@ -52,9 +52,9 @@ public class ConstraintViolationException extends AbstractServerRtException { final Set> violations = ex.getConstraintViolations(); final List messages = new ArrayList<>(); violations.stream().forEach( - violation -> messages.add(violation.getPropertyPath() + WHITESPACE + violation.getMessage() + ".")); + violation -> messages.add(violation.getPropertyPath() + MESSAGE_FORMATTER_SEPARATOR + violation.getMessage() + ".")); - return messages.stream().collect(Collectors.joining(WHITESPACE)); + return messages.stream().collect(Collectors.joining(MESSAGE_FORMATTER_SEPARATOR)); } } From 171f96f30905ad805e23f2f7d8169e8afe970d17 Mon Sep 17 00:00:00 2001 From: Melanie Retter Date: Thu, 25 Aug 2016 17:27:18 +0200 Subject: [PATCH 14/15] Remove DB migration skripts Signed-off-by: Melanie Retter --- .../H2/V1_7_0__software_module_type_constraint__H2.sql | 2 -- .../MYSQL/V1_7_0__software_module_type_constraint__MYSQL.sql | 2 -- 2 files changed, 4 deletions(-) delete mode 100644 hawkbit-repository/hawkbit-repository-jpa/src/main/resources/db/migration/H2/V1_7_0__software_module_type_constraint__H2.sql delete mode 100644 hawkbit-repository/hawkbit-repository-jpa/src/main/resources/db/migration/MYSQL/V1_7_0__software_module_type_constraint__MYSQL.sql diff --git a/hawkbit-repository/hawkbit-repository-jpa/src/main/resources/db/migration/H2/V1_7_0__software_module_type_constraint__H2.sql b/hawkbit-repository/hawkbit-repository-jpa/src/main/resources/db/migration/H2/V1_7_0__software_module_type_constraint__H2.sql deleted file mode 100644 index dfdb6ee1d..000000000 --- a/hawkbit-repository/hawkbit-repository-jpa/src/main/resources/db/migration/H2/V1_7_0__software_module_type_constraint__H2.sql +++ /dev/null @@ -1,2 +0,0 @@ -alter table sp_software_module_type - add constraint maxAssignmentCheck check (max_ds_assignments > 0); \ No newline at end of file diff --git a/hawkbit-repository/hawkbit-repository-jpa/src/main/resources/db/migration/MYSQL/V1_7_0__software_module_type_constraint__MYSQL.sql b/hawkbit-repository/hawkbit-repository-jpa/src/main/resources/db/migration/MYSQL/V1_7_0__software_module_type_constraint__MYSQL.sql deleted file mode 100644 index dfdb6ee1d..000000000 --- a/hawkbit-repository/hawkbit-repository-jpa/src/main/resources/db/migration/MYSQL/V1_7_0__software_module_type_constraint__MYSQL.sql +++ /dev/null @@ -1,2 +0,0 @@ -alter table sp_software_module_type - add constraint maxAssignmentCheck check (max_ds_assignments > 0); \ No newline at end of file From f3534bc4d9a9cdb0c74d19b1e72271662611df29 Mon Sep 17 00:00:00 2001 From: Melanie Retter Date: Fri, 26 Aug 2016 10:32:48 +0200 Subject: [PATCH 15/15] Refactor, add db clean script Signed-off-by: Melanie Retter --- .../exception/ConstraintViolationException.java | 15 ++++----------- ...1_7_0__swmType_maxAssignment_greater_0__H2.sql | 1 + ..._0__swmType_maxAssignment_greater_0__MYSQL.sql | 1 + 3 files changed, 6 insertions(+), 11 deletions(-) create mode 100644 hawkbit-repository/hawkbit-repository-jpa/src/main/resources/db/migration/H2/V1_7_0__swmType_maxAssignment_greater_0__H2.sql create mode 100644 hawkbit-repository/hawkbit-repository-jpa/src/main/resources/db/migration/MYSQL/V1_7_0__swmType_maxAssignment_greater_0__MYSQL.sql 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 index 5150226a0..f7a7660cf 100644 --- 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 @@ -8,13 +8,8 @@ */ package org.eclipse.hawkbit.repository.exception; -import java.util.ArrayList; -import java.util.List; -import java.util.Set; import java.util.stream.Collectors; -import javax.validation.ConstraintViolation; - import org.eclipse.hawkbit.exception.AbstractServerRtException; import org.eclipse.hawkbit.exception.SpServerError; @@ -49,12 +44,10 @@ public class ConstraintViolationException extends AbstractServerRtException { * @return message String with proper error information */ public static String getExceptionMessage(final javax.validation.ConstraintViolationException ex) { - final Set> violations = ex.getConstraintViolations(); - final List messages = new ArrayList<>(); - violations.stream().forEach( - violation -> messages.add(violation.getPropertyPath() + MESSAGE_FORMATTER_SEPARATOR + violation.getMessage() + ".")); - - return messages.stream().collect(Collectors.joining(MESSAGE_FORMATTER_SEPARATOR)); + return ex + .getConstraintViolations().stream().map(violation -> violation.getPropertyPath() + + MESSAGE_FORMATTER_SEPARATOR + violation.getMessage() + ".") + .collect(Collectors.joining(MESSAGE_FORMATTER_SEPARATOR)); } } diff --git a/hawkbit-repository/hawkbit-repository-jpa/src/main/resources/db/migration/H2/V1_7_0__swmType_maxAssignment_greater_0__H2.sql b/hawkbit-repository/hawkbit-repository-jpa/src/main/resources/db/migration/H2/V1_7_0__swmType_maxAssignment_greater_0__H2.sql new file mode 100644 index 000000000..f79c9375c --- /dev/null +++ b/hawkbit-repository/hawkbit-repository-jpa/src/main/resources/db/migration/H2/V1_7_0__swmType_maxAssignment_greater_0__H2.sql @@ -0,0 +1 @@ +Update sp_software_module_type set max_ds_assignments = 1 where max_ds_assignments < 1; \ No newline at end of file diff --git a/hawkbit-repository/hawkbit-repository-jpa/src/main/resources/db/migration/MYSQL/V1_7_0__swmType_maxAssignment_greater_0__MYSQL.sql b/hawkbit-repository/hawkbit-repository-jpa/src/main/resources/db/migration/MYSQL/V1_7_0__swmType_maxAssignment_greater_0__MYSQL.sql new file mode 100644 index 000000000..f79c9375c --- /dev/null +++ b/hawkbit-repository/hawkbit-repository-jpa/src/main/resources/db/migration/MYSQL/V1_7_0__swmType_maxAssignment_greater_0__MYSQL.sql @@ -0,0 +1 @@ +Update sp_software_module_type set max_ds_assignments = 1 where max_ds_assignments < 1; \ No newline at end of file