Fix constraint violation exception when creating target with whitespaces (#363)
* Added a function to validate if controller id is empty Signed-off-by: Jonathan Philip Knoblauch <JonathanPhilip.Knoblauch@bosch-si.com> * Changed how the controller id gets past to other methods Signed-off-by: Jonathan Philip Knoblauch <JonathanPhilip.Knoblauch@bosch-si.com> * Changed the way controllderId gets validated in UI and repro - added junit test Signed-off-by: Jonathan Philip Knoblauch <JonathanPhilip.Knoblauch@bosch-si.com> * Fixed sonar issue Signed-off-by: Jonathan Philip Knoblauch <JonathanPhilip.Knoblauch@bosch-si.com> * Switched to use the RegexpValidator Signed-off-by: Jonathan Philip Knoblauch <JonathanPhilip.Knoblauch@bosch-si.com>
This commit is contained in:
committed by
Michael Hirsch
parent
a57165686e
commit
b30f2bdb1f
@@ -33,6 +33,7 @@ import javax.persistence.PrimaryKeyJoinColumn;
|
|||||||
import javax.persistence.Table;
|
import javax.persistence.Table;
|
||||||
import javax.persistence.Transient;
|
import javax.persistence.Transient;
|
||||||
import javax.persistence.UniqueConstraint;
|
import javax.persistence.UniqueConstraint;
|
||||||
|
import javax.validation.constraints.Pattern;
|
||||||
import javax.validation.constraints.Size;
|
import javax.validation.constraints.Size;
|
||||||
|
|
||||||
import org.eclipse.hawkbit.im.authentication.SpPermission;
|
import org.eclipse.hawkbit.im.authentication.SpPermission;
|
||||||
@@ -77,6 +78,7 @@ public class JpaTarget extends AbstractJpaNamedEntity implements Persistable<Lon
|
|||||||
@Column(name = "controller_id", length = 64)
|
@Column(name = "controller_id", length = 64)
|
||||||
@Size(min = 1, max = 64)
|
@Size(min = 1, max = 64)
|
||||||
@NotEmpty
|
@NotEmpty
|
||||||
|
@Pattern(regexp = "[.\\S]*", message = "has whitespaces which are not allowed")
|
||||||
private String controllerId;
|
private String controllerId;
|
||||||
|
|
||||||
@Transient
|
@Transient
|
||||||
|
|||||||
@@ -125,6 +125,54 @@ public class TargetManagementTest extends AbstractJpaIntegrationTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Description("Verify that a target with whitespaces in controller id cannot be created")
|
||||||
|
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class, count = 0) })
|
||||||
|
public void createTargetWithWhitespaces() {
|
||||||
|
try {
|
||||||
|
targetManagement.createTarget(entityFactory.target().create().controllerId(" "));
|
||||||
|
fail("target with whitespaces in controller id should not be created");
|
||||||
|
} catch (final ConstraintViolationException e) {
|
||||||
|
// ok
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
targetManagement.createTarget(entityFactory.target().create().controllerId(" a"));
|
||||||
|
fail("target with whitespaces in controller id should not be created");
|
||||||
|
} catch (final ConstraintViolationException e) {
|
||||||
|
// ok
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
targetManagement.createTarget(entityFactory.target().create().controllerId("a "));
|
||||||
|
fail("target with whitespaces in controller id should not be created");
|
||||||
|
} catch (final ConstraintViolationException e) {
|
||||||
|
// ok
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
targetManagement.createTarget(entityFactory.target().create().controllerId("a b"));
|
||||||
|
fail("target with whitespaces in controller id should not be created");
|
||||||
|
} catch (final ConstraintViolationException e) {
|
||||||
|
// ok
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
targetManagement.createTarget(entityFactory.target().create().controllerId(" "));
|
||||||
|
fail("target with whitespaces in controller id should not be created");
|
||||||
|
} catch (final ConstraintViolationException e) {
|
||||||
|
// ok
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
targetManagement.createTarget(entityFactory.target().create().controllerId("aaa bbb"));
|
||||||
|
fail("target with whitespaces in controller id should not be created");
|
||||||
|
} catch (final ConstraintViolationException e) {
|
||||||
|
// ok
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Description("Ensures that targets can assigned and unassigned to a target tag. Not exists target will be ignored for the assignment.")
|
@Description("Ensures that targets can assigned and unassigned to a target tag. Not exists target will be ignored for the assignment.")
|
||||||
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class, count = 4),
|
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class, count = 4),
|
||||||
|
|||||||
@@ -33,6 +33,7 @@ import org.eclipse.hawkbit.ui.utils.UINotification;
|
|||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.vaadin.spring.events.EventBus;
|
import org.vaadin.spring.events.EventBus;
|
||||||
|
|
||||||
|
import com.vaadin.data.validator.RegexpValidator;
|
||||||
import com.vaadin.spring.annotation.SpringComponent;
|
import com.vaadin.spring.annotation.SpringComponent;
|
||||||
import com.vaadin.spring.annotation.ViewScope;
|
import com.vaadin.spring.annotation.ViewScope;
|
||||||
import com.vaadin.ui.CustomComponent;
|
import com.vaadin.ui.CustomComponent;
|
||||||
@@ -104,6 +105,7 @@ public class TargetAddUpdateWindowLayout extends CustomComponent {
|
|||||||
|
|
||||||
private void createRequiredComponents() {
|
private void createRequiredComponents() {
|
||||||
controllerIDTextField = createTextField("prompt.target.id", UIComponentIdProvider.TARGET_ADD_CONTROLLER_ID);
|
controllerIDTextField = createTextField("prompt.target.id", UIComponentIdProvider.TARGET_ADD_CONTROLLER_ID);
|
||||||
|
controllerIDTextField.addValidator(new RegexpValidator("[.\\S]*", i18n.get("message.target.whitespace.check")));
|
||||||
nameTextField = createTextField("textfield.name", UIComponentIdProvider.TARGET_ADD_NAME);
|
nameTextField = createTextField("textfield.name", UIComponentIdProvider.TARGET_ADD_NAME);
|
||||||
nameTextField.setRequired(false);
|
nameTextField.setRequired(false);
|
||||||
|
|
||||||
|
|||||||
@@ -273,6 +273,7 @@ message.no.available = --No messages available--
|
|||||||
message.no.actionupdateds.available = No other updates available for this action
|
message.no.actionupdateds.available = No other updates available for this action
|
||||||
message.mandatory.check = Mandatory details are missing
|
message.mandatory.check = Mandatory details are missing
|
||||||
message.target.duplicate.check = Target [ {0} ] must be unique, entered value already exists.
|
message.target.duplicate.check = Target [ {0} ] must be unique, entered value already exists.
|
||||||
|
message.target.whitespace.check = Please enter a valid controller ID with no whitespaces
|
||||||
message.permission.insufficient = Insufficient permissions to perform this action.
|
message.permission.insufficient = Insufficient permissions to perform this action.
|
||||||
message.error.temp = The operation cannot be fulfilled due to {0}. Please contact administrator
|
message.error.temp = The operation cannot be fulfilled due to {0}. Please contact administrator
|
||||||
message.dist.alreadyassigned = {0} : {1} is already assigned/installed, cannot be updated
|
message.dist.alreadyassigned = {0} : {1} is already assigned/installed, cannot be updated
|
||||||
|
|||||||
Reference in New Issue
Block a user