Complete field constraint documentation in repository API (#565)
* Document constraints in builders. Signed-off-by: kaizimmerm <kai.zimmermann@bosch-si.com> * Completed constraint documentation in repository API Signed-off-by: kaizimmerm <kai.zimmermann@bosch-si.com> * Fix typo. Signed-off-by: kaizimmerm <kai.zimmermann@bosch-si.com>
This commit is contained in:
@@ -10,11 +10,11 @@ package org.eclipse.hawkbit.repository.jpa.model;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.MappedSuperclass;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import javax.validation.constraints.Size;
|
||||
|
||||
import org.eclipse.hawkbit.repository.model.NamedEntity;
|
||||
import org.eclipse.hawkbit.repository.model.NamedVersionedEntity;
|
||||
import org.hibernate.validator.constraints.NotEmpty;
|
||||
|
||||
/**
|
||||
* Extension for {@link NamedEntity} that are versioned.
|
||||
@@ -27,9 +27,9 @@ import org.hibernate.validator.constraints.NotEmpty;
|
||||
public abstract class AbstractJpaNamedVersionedEntity extends AbstractJpaNamedEntity implements NamedVersionedEntity {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Column(name = "version", nullable = false, length = 64)
|
||||
@Size(max = 64)
|
||||
@NotEmpty
|
||||
@Column(name = "version", nullable = false, length = NamedVersionedEntity.VERSION_MAX_SIZE)
|
||||
@Size(min = 1, max = NamedVersionedEntity.VERSION_MAX_SIZE)
|
||||
@NotNull
|
||||
private String version;
|
||||
|
||||
/**
|
||||
|
||||
@@ -22,6 +22,7 @@ import javax.persistence.Index;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.Table;
|
||||
import javax.persistence.UniqueConstraint;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import javax.validation.constraints.Size;
|
||||
|
||||
import org.eclipse.hawkbit.repository.model.DistributionSet;
|
||||
@@ -29,7 +30,6 @@ import org.eclipse.hawkbit.repository.model.DistributionSetType;
|
||||
import org.eclipse.hawkbit.repository.model.SoftwareModule;
|
||||
import org.eclipse.hawkbit.repository.model.SoftwareModuleType;
|
||||
import org.eclipse.persistence.annotations.CascadeOnDelete;
|
||||
import org.hibernate.validator.constraints.NotEmpty;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
/**
|
||||
@@ -54,13 +54,13 @@ public class JpaDistributionSetType extends AbstractJpaNamedEntity implements Di
|
||||
CascadeType.PERSIST }, fetch = FetchType.EAGER, orphanRemoval = true)
|
||||
private Set<DistributionSetTypeElement> elements;
|
||||
|
||||
@Column(name = "type_key", nullable = false, updatable = false, length = 64)
|
||||
@Size(max = 64)
|
||||
@NotEmpty
|
||||
@Column(name = "type_key", nullable = false, updatable = false, length = DistributionSetType.KEY_MAX_SIZE)
|
||||
@Size(min = 1, max = DistributionSetType.KEY_MAX_SIZE)
|
||||
@NotNull
|
||||
private String key;
|
||||
|
||||
@Column(name = "colour", nullable = true, length = 16)
|
||||
@Size(max = 16)
|
||||
@Column(name = "colour", nullable = true, length = DistributionSetType.COLOUR_MAX_SIZE)
|
||||
@Size(max = DistributionSetType.COLOUR_MAX_SIZE)
|
||||
private String colour;
|
||||
|
||||
@Column(name = "deleted")
|
||||
|
||||
@@ -77,8 +77,8 @@ public class JpaSoftwareModule extends AbstractJpaNamedVersionedEntity implement
|
||||
@Column(name = "deleted")
|
||||
private boolean deleted;
|
||||
|
||||
@Column(name = "vendor", nullable = true, length = 256)
|
||||
@Size(max = 256)
|
||||
@Column(name = "vendor", nullable = true, length = SoftwareModule.VENDOR_MAX_SIZE)
|
||||
@Size(max = SoftwareModule.VENDOR_MAX_SIZE)
|
||||
private String vendor;
|
||||
|
||||
@CascadeOnDelete
|
||||
|
||||
@@ -14,10 +14,10 @@ import javax.persistence.Index;
|
||||
import javax.persistence.Table;
|
||||
import javax.persistence.UniqueConstraint;
|
||||
import javax.validation.constraints.Min;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import javax.validation.constraints.Size;
|
||||
|
||||
import org.eclipse.hawkbit.repository.model.SoftwareModuleType;
|
||||
import org.hibernate.validator.constraints.NotEmpty;
|
||||
|
||||
/**
|
||||
* Type of a software modules.
|
||||
@@ -35,17 +35,17 @@ import org.hibernate.validator.constraints.NotEmpty;
|
||||
public class JpaSoftwareModuleType extends AbstractJpaNamedEntity implements SoftwareModuleType {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Column(name = "type_key", nullable = false, length = 64)
|
||||
@Size(max = 64)
|
||||
@NotEmpty
|
||||
@Column(name = "type_key", nullable = false, length = SoftwareModuleType.KEY_MAX_SIZE)
|
||||
@Size(min = 1, max = SoftwareModuleType.KEY_MAX_SIZE)
|
||||
@NotNull
|
||||
private String key;
|
||||
|
||||
@Column(name = "max_ds_assignments", nullable = false)
|
||||
@Min(1)
|
||||
private int maxAssignments;
|
||||
|
||||
@Column(name = "colour", nullable = true, length = 16)
|
||||
@Size(max = 16)
|
||||
@Column(name = "colour", nullable = true, length = SoftwareModuleType.COLOUR_MAX_SIZE)
|
||||
@Size(max = SoftwareModuleType.COLOUR_MAX_SIZE)
|
||||
private String colour;
|
||||
|
||||
@Column(name = "deleted")
|
||||
|
||||
@@ -26,8 +26,8 @@ import org.eclipse.hawkbit.repository.model.Tag;
|
||||
public class JpaTag extends AbstractJpaNamedEntity implements Tag {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Column(name = "colour", nullable = true, length = 16)
|
||||
@Size(max = 16)
|
||||
@Column(name = "colour", nullable = true, length = Tag.COLOUR_MAX_SIZE)
|
||||
@Size(max = Tag.COLOUR_MAX_SIZE)
|
||||
private String colour;
|
||||
|
||||
protected JpaTag() {
|
||||
|
||||
@@ -21,6 +21,7 @@ import javax.persistence.UniqueConstraint;
|
||||
import javax.validation.constraints.Size;
|
||||
|
||||
import org.eclipse.hawkbit.repository.model.DistributionSet;
|
||||
import org.eclipse.hawkbit.repository.model.NamedEntity;
|
||||
import org.eclipse.hawkbit.repository.model.TargetFilterQuery;
|
||||
import org.hibernate.validator.constraints.NotEmpty;
|
||||
|
||||
@@ -38,13 +39,13 @@ import org.hibernate.validator.constraints.NotEmpty;
|
||||
public class JpaTargetFilterQuery extends AbstractJpaTenantAwareBaseEntity implements TargetFilterQuery {
|
||||
private static final long serialVersionUID = 7493966984413479089L;
|
||||
|
||||
@Column(name = "name", length = 64, nullable = false)
|
||||
@Size(max = 64)
|
||||
@Column(name = "name", length = NamedEntity.NAME_MAX_SIZE, nullable = false)
|
||||
@Size(max = NamedEntity.NAME_MAX_SIZE)
|
||||
@NotEmpty
|
||||
private String name;
|
||||
|
||||
@Column(name = "query", length = 1024, nullable = false)
|
||||
@Size(max = 1024)
|
||||
@Column(name = "query", length = TargetFilterQuery.QUERY_MAX_SIZE, nullable = false)
|
||||
@Size(max = TargetFilterQuery.QUERY_MAX_SIZE)
|
||||
@NotEmpty
|
||||
private String query;
|
||||
|
||||
|
||||
@@ -19,11 +19,15 @@ import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import javax.validation.ConstraintViolationException;
|
||||
|
||||
import org.apache.commons.lang3.RandomStringUtils;
|
||||
import org.assertj.core.api.Condition;
|
||||
import org.eclipse.hawkbit.repository.DistributionSetManagement;
|
||||
import org.eclipse.hawkbit.repository.builder.DistributionSetCreate;
|
||||
import org.eclipse.hawkbit.repository.event.remote.entity.DistributionSetCreatedEvent;
|
||||
import org.eclipse.hawkbit.repository.event.remote.entity.DistributionSetTagCreatedEvent;
|
||||
import org.eclipse.hawkbit.repository.event.remote.entity.DistributionSetUpdatedEvent;
|
||||
import org.eclipse.hawkbit.repository.event.remote.entity.SoftwareModuleCreatedEvent;
|
||||
import org.eclipse.hawkbit.repository.exception.EntityAlreadyExistsException;
|
||||
import org.eclipse.hawkbit.repository.exception.EntityNotFoundException;
|
||||
@@ -52,6 +56,7 @@ import com.google.common.collect.Sets;
|
||||
|
||||
import ru.yandex.qatools.allure.annotations.Description;
|
||||
import ru.yandex.qatools.allure.annotations.Features;
|
||||
import ru.yandex.qatools.allure.annotations.Step;
|
||||
import ru.yandex.qatools.allure.annotations.Stories;
|
||||
|
||||
/**
|
||||
@@ -170,6 +175,84 @@ public class DistributionSetManagementTest extends AbstractJpaIntegrationTest {
|
||||
entityFactory.generateMetadata(NOT_EXIST_ID, "xxx")), "DistributionSetMetadata");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Description("Verify that a DistributionSet with invalid properties cannot be created or updated")
|
||||
@ExpectEvents({ @Expect(type = DistributionSetCreatedEvent.class, count = 1),
|
||||
@Expect(type = SoftwareModuleCreatedEvent.class, count = 3),
|
||||
@Expect(type = DistributionSetUpdatedEvent.class, count = 0) })
|
||||
public void createAndUpdateDistributionSetWithInvalidFields() {
|
||||
final DistributionSet set = testdataFactory.createDistributionSet();
|
||||
|
||||
createAndUpdateDistributionSetWithInvalidDescription(set);
|
||||
createAndUpdateDistributionSetWithInvalidName(set);
|
||||
createAndUpdateDistributionSetWithInvalidVersion(set);
|
||||
}
|
||||
|
||||
@Step
|
||||
private void createAndUpdateDistributionSetWithInvalidDescription(final DistributionSet set) {
|
||||
|
||||
assertThatExceptionOfType(ConstraintViolationException.class)
|
||||
.isThrownBy(() -> distributionSetManagement.createDistributionSet(entityFactory.distributionSet()
|
||||
.create().name("a").version("a").description(RandomStringUtils.randomAlphanumeric(513))))
|
||||
.as("entity with too long description should not be created");
|
||||
|
||||
assertThatExceptionOfType(ConstraintViolationException.class)
|
||||
.isThrownBy(() -> distributionSetManagement.updateDistributionSet(entityFactory.distributionSet()
|
||||
.update(set.getId()).description(RandomStringUtils.randomAlphanumeric(513))))
|
||||
.as("entity with too long description should not be updated");
|
||||
|
||||
}
|
||||
|
||||
@Step
|
||||
private void createAndUpdateDistributionSetWithInvalidName(final DistributionSet set) {
|
||||
|
||||
assertThatExceptionOfType(ConstraintViolationException.class)
|
||||
.isThrownBy(() -> distributionSetManagement.createDistributionSet(entityFactory.distributionSet()
|
||||
.create().version("a").name(RandomStringUtils.randomAlphanumeric(65))))
|
||||
.as("entity with too long name should not be created");
|
||||
|
||||
assertThatExceptionOfType(ConstraintViolationException.class)
|
||||
.isThrownBy(() -> distributionSetManagement
|
||||
.createDistributionSet(entityFactory.distributionSet().create().version("a").name("")))
|
||||
.as("entity with too long name should not be created");
|
||||
|
||||
assertThatExceptionOfType(ConstraintViolationException.class)
|
||||
.isThrownBy(() -> distributionSetManagement.updateDistributionSet(entityFactory.distributionSet()
|
||||
.update(set.getId()).name(RandomStringUtils.randomAlphanumeric(65))))
|
||||
.as("entity with too long name should not be updated");
|
||||
|
||||
assertThatExceptionOfType(ConstraintViolationException.class)
|
||||
.isThrownBy(() -> distributionSetManagement
|
||||
.updateDistributionSet(entityFactory.distributionSet().update(set.getId()).name("")))
|
||||
.as("entity with too short name should not be updated");
|
||||
|
||||
}
|
||||
|
||||
@Step
|
||||
private void createAndUpdateDistributionSetWithInvalidVersion(final DistributionSet set) {
|
||||
|
||||
assertThatExceptionOfType(ConstraintViolationException.class)
|
||||
.isThrownBy(() -> distributionSetManagement.createDistributionSet(entityFactory.distributionSet()
|
||||
.create().name("a").version(RandomStringUtils.randomAlphanumeric(65))))
|
||||
.as("entity with too long name should not be created");
|
||||
|
||||
assertThatExceptionOfType(ConstraintViolationException.class)
|
||||
.isThrownBy(() -> distributionSetManagement
|
||||
.createDistributionSet(entityFactory.distributionSet().create().name("a").version("")))
|
||||
.as("entity with too long name should not be created");
|
||||
|
||||
assertThatExceptionOfType(ConstraintViolationException.class)
|
||||
.isThrownBy(() -> distributionSetManagement.updateDistributionSet(entityFactory.distributionSet()
|
||||
.update(set.getId()).version(RandomStringUtils.randomAlphanumeric(65))))
|
||||
.as("entity with too long name should not be updated");
|
||||
|
||||
assertThatExceptionOfType(ConstraintViolationException.class)
|
||||
.isThrownBy(() -> distributionSetManagement
|
||||
.updateDistributionSet(entityFactory.distributionSet().update(set.getId()).version("")))
|
||||
.as("entity with too short name should not be updated");
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@Description("Ensures that it is not possible to create a DS that already exists (unique constraint is on name,version for DS).")
|
||||
public void createDuplicateDistributionSetsFailsWithException() {
|
||||
|
||||
Reference in New Issue
Block a user