From a2a0a5697e89dc8b772944b2dc571930d60e9125 Mon Sep 17 00:00:00 2001 From: Kai Zimmermann Date: Sun, 27 Mar 2016 17:18:32 +0200 Subject: [PATCH] Added tests for DS management. Removed boilerplate. --- .../repository/DistributionSetManagement.java | 12 ++- .../TenantConfigurationManagement.java | 2 +- .../repository/model/DistributionSet.java | 14 ++-- .../repository/model/DistributionSetType.java | 8 +- .../model/DistributionSetTypeElement.java | 49 +++++++++--- ...istributionSetTypeElementCompositeKey.java | 60 ++++++++------ .../DistributionSetManagementTest.java | 78 ++++++++++--------- 7 files changed, 138 insertions(+), 85 deletions(-) diff --git a/hawkbit-repository/src/main/java/org/eclipse/hawkbit/repository/DistributionSetManagement.java b/hawkbit-repository/src/main/java/org/eclipse/hawkbit/repository/DistributionSetManagement.java index 890333697..410021664 100644 --- a/hawkbit-repository/src/main/java/org/eclipse/hawkbit/repository/DistributionSetManagement.java +++ b/hawkbit-repository/src/main/java/org/eclipse/hawkbit/repository/DistributionSetManagement.java @@ -188,11 +188,12 @@ public class DistributionSetManagement { toBeChangedDSs.add(set); } } - result = new DistributionSetTagAssignmentResult(dsIds.size() - toBeChangedDSs.size(), 0, toBeChangedDSs.size(), - Collections.emptyList(), distributionSetRepository.save(toBeChangedDSs), myTag); + result = new DistributionSetTagAssignmentResult(dsIds.size() - toBeChangedDSs.size(), 0, + toBeChangedDSs.size(), Collections.emptyList(), distributionSetRepository.save(toBeChangedDSs), + myTag); } else { - result = new DistributionSetTagAssignmentResult(dsIds.size() - toBeChangedDSs.size(), toBeChangedDSs.size(), 0, - distributionSetRepository.save(toBeChangedDSs), Collections.emptyList(), myTag); + result = new DistributionSetTagAssignmentResult(dsIds.size() - toBeChangedDSs.size(), toBeChangedDSs.size(), + 0, distributionSetRepository.save(toBeChangedDSs), Collections.emptyList(), myTag); } final DistributionSetTagAssignmentResult resultAssignment = result; @@ -348,6 +349,9 @@ public class DistributionSetManagement { public List createDistributionSets(@NotNull final Iterable distributionSets) { for (final DistributionSet ds : distributionSets) { prepareDsSave(ds); + if (ds.getType() == null) { + ds.setType(systemManagement.getTenantMetadata().getDefaultDsType()); + } } return distributionSetRepository.save(distributionSets); } diff --git a/hawkbit-repository/src/main/java/org/eclipse/hawkbit/repository/TenantConfigurationManagement.java b/hawkbit-repository/src/main/java/org/eclipse/hawkbit/repository/TenantConfigurationManagement.java index f6b6afcfc..e311cf288 100644 --- a/hawkbit-repository/src/main/java/org/eclipse/hawkbit/repository/TenantConfigurationManagement.java +++ b/hawkbit-repository/src/main/java/org/eclipse/hawkbit/repository/TenantConfigurationManagement.java @@ -150,7 +150,7 @@ public class TenantConfigurationManagement implements EnvironmentAware { * @param configurationKey * the key of the configuration * @return the converted configuration value either from the tenant specific - * configuration stored or from the fallback default values or + * configuration stored or from the fall back default values or * {@code null} in case key has not been configured and not default * value exists * @throws TenantConfigurationValidatorException diff --git a/hawkbit-repository/src/main/java/org/eclipse/hawkbit/repository/model/DistributionSet.java b/hawkbit-repository/src/main/java/org/eclipse/hawkbit/repository/model/DistributionSet.java index e0444bc28..c3eb49a2d 100644 --- a/hawkbit-repository/src/main/java/org/eclipse/hawkbit/repository/model/DistributionSet.java +++ b/hawkbit-repository/src/main/java/org/eclipse/hawkbit/repository/model/DistributionSet.java @@ -67,14 +67,14 @@ public class DistributionSet extends NamedVersionedEntity { @ManyToMany(targetEntity = SoftwareModule.class, fetch = FetchType.LAZY) @JoinTable(name = "sp_ds_module", joinColumns = { - @JoinColumn(name = "ds_id", foreignKey = @ForeignKey(value = ConstraintMode.CONSTRAINT, name = "fk_ds_module_ds") ) }, inverseJoinColumns = { - @JoinColumn(name = "module_id", foreignKey = @ForeignKey(value = ConstraintMode.CONSTRAINT, name = "fk_ds_module_module") ) }) + @JoinColumn(name = "ds_id", foreignKey = @ForeignKey(value = ConstraintMode.CONSTRAINT, name = "fk_ds_module_ds")) }, inverseJoinColumns = { + @JoinColumn(name = "module_id", foreignKey = @ForeignKey(value = ConstraintMode.CONSTRAINT, name = "fk_ds_module_module")) }) private final Set modules = new HashSet<>(); @ManyToMany(targetEntity = DistributionSetTag.class) @JoinTable(name = "sp_ds_dstag", joinColumns = { - @JoinColumn(name = "ds", foreignKey = @ForeignKey(value = ConstraintMode.CONSTRAINT, name = "fk_ds_dstag_ds") ) }, inverseJoinColumns = { - @JoinColumn(name = "TAG", foreignKey = @ForeignKey(value = ConstraintMode.CONSTRAINT, name = "fk_ds_dstag_tag") ) }) + @JoinColumn(name = "ds", foreignKey = @ForeignKey(value = ConstraintMode.CONSTRAINT, name = "fk_ds_dstag_ds")) }, inverseJoinColumns = { + @JoinColumn(name = "TAG", foreignKey = @ForeignKey(value = ConstraintMode.CONSTRAINT, name = "fk_ds_dstag_tag")) }) private Set tags = new HashSet<>(); @Column(name = "deleted") @@ -95,7 +95,7 @@ public class DistributionSet extends NamedVersionedEntity { private final List metadata = new ArrayList<>(); @ManyToOne(fetch = FetchType.LAZY) - @JoinColumn(name = "ds_id", nullable = false, foreignKey = @ForeignKey(value = ConstraintMode.CONSTRAINT, name = "fk_ds_dstype_ds") ) + @JoinColumn(name = "ds_id", nullable = false, foreignKey = @ForeignKey(value = ConstraintMode.CONSTRAINT, name = "fk_ds_dstype_ds")) private DistributionSetType type; @Column(name = "complete") @@ -130,7 +130,9 @@ public class DistributionSet extends NamedVersionedEntity { if (moduleList != null) { moduleList.forEach(this::addModule); } - complete = type.checkComplete(this); + if (this.type != null) { + complete = this.type.checkComplete(this); + } } public Set getTags() { diff --git a/hawkbit-repository/src/main/java/org/eclipse/hawkbit/repository/model/DistributionSetType.java b/hawkbit-repository/src/main/java/org/eclipse/hawkbit/repository/model/DistributionSetType.java index 30fb73365..1d565f6ac 100644 --- a/hawkbit-repository/src/main/java/org/eclipse/hawkbit/repository/model/DistributionSetType.java +++ b/hawkbit-repository/src/main/java/org/eclipse/hawkbit/repository/model/DistributionSetType.java @@ -27,9 +27,6 @@ import javax.persistence.UniqueConstraint; * A distribution set type defines which software module types can or have to be * {@link DistributionSet}. * - * - * - * */ @Entity @Table(name = "sp_distribution_set_type", indexes = { @@ -55,7 +52,7 @@ public class DistributionSetType extends NamedEntity { private boolean deleted = false; public DistributionSetType() { - // default public constructor + // default public constructor for JPA } /** @@ -307,7 +304,7 @@ public class DistributionSetType extends NamedEntity { if (!super.equals(obj)) { return false; } - if (getClass() != obj.getClass()) { + if (!(obj instanceof DistributionSetType)) { return false; } final DistributionSetType other = (DistributionSetType) obj; @@ -337,4 +334,5 @@ public class DistributionSetType extends NamedEntity { } return true; } + } diff --git a/hawkbit-repository/src/main/java/org/eclipse/hawkbit/repository/model/DistributionSetTypeElement.java b/hawkbit-repository/src/main/java/org/eclipse/hawkbit/repository/model/DistributionSetTypeElement.java index 0d892f6da..38baf52c7 100644 --- a/hawkbit-repository/src/main/java/org/eclipse/hawkbit/repository/model/DistributionSetTypeElement.java +++ b/hawkbit-repository/src/main/java/org/eclipse/hawkbit/repository/model/DistributionSetTypeElement.java @@ -25,17 +25,10 @@ import javax.persistence.Table; * Relation element between a {@link DistributionSetType} and its * {@link SoftwareModuleType} elements. * - * - * - * */ @Entity @Table(name = "sp_ds_type_element") public class DistributionSetTypeElement implements Serializable { - - /** - * - */ private static final long serialVersionUID = 1L; @EmbeddedId @@ -46,18 +39,16 @@ public class DistributionSetTypeElement implements Serializable { @MapsId("dsType") @ManyToOne(optional = false, fetch = FetchType.LAZY) - @JoinColumn(name = "distribution_set_type", foreignKey = @ForeignKey(value = ConstraintMode.CONSTRAINT, name = "fk_ds_type_element_dstype") ) + @JoinColumn(name = "distribution_set_type", foreignKey = @ForeignKey(value = ConstraintMode.CONSTRAINT, name = "fk_ds_type_element_dstype")) private DistributionSetType dsType; @MapsId("smType") @ManyToOne(optional = false, fetch = FetchType.LAZY) - @JoinColumn(name = "software_module_type", foreignKey = @ForeignKey(value = ConstraintMode.CONSTRAINT, name = "fk_ds_type_element_smtype") ) + @JoinColumn(name = "software_module_type", foreignKey = @ForeignKey(value = ConstraintMode.CONSTRAINT, name = "fk_ds_type_element_smtype")) private SoftwareModuleType smType; - /** - * Default constructor. - */ public DistributionSetTypeElement() { + // Default constructor for JPA } /** @@ -108,4 +99,38 @@ public class DistributionSetTypeElement implements Serializable { return key; } + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((key == null) ? 0 : key.hashCode()); + result = prime * result + (mandatory ? 1231 : 1237); + return result; + } + + @Override + public boolean equals(final Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (!(obj instanceof DistributionSetTypeElement)) { + return false; + } + final DistributionSetTypeElement other = (DistributionSetTypeElement) obj; + if (key == null) { + if (other.key != null) { + return false; + } + } else if (!key.equals(other.key)) { + return false; + } + if (mandatory != other.mandatory) { + return false; + } + return true; + } + } diff --git a/hawkbit-repository/src/main/java/org/eclipse/hawkbit/repository/model/DistributionSetTypeElementCompositeKey.java b/hawkbit-repository/src/main/java/org/eclipse/hawkbit/repository/model/DistributionSetTypeElementCompositeKey.java index c819d4e52..1f71d007e 100644 --- a/hawkbit-repository/src/main/java/org/eclipse/hawkbit/repository/model/DistributionSetTypeElementCompositeKey.java +++ b/hawkbit-repository/src/main/java/org/eclipse/hawkbit/repository/model/DistributionSetTypeElementCompositeKey.java @@ -15,17 +15,9 @@ import javax.persistence.Embeddable; /** * Composite key for {@link DistributionSetTypeElement}. - * - * - * - * */ @Embeddable public class DistributionSetTypeElementCompositeKey implements Serializable { - - /** - * - */ private static final long serialVersionUID = 1L; @Column(name = "distribution_set_type", nullable = false) @@ -54,34 +46,58 @@ public class DistributionSetTypeElementCompositeKey implements Serializable { this.smType = smType.getId(); } - /** - * @return the dsType - */ public Long getDsType() { return dsType; } - /** - * @param dsType - * the dsType to set - */ public void setDsType(final Long dsType) { this.dsType = dsType; } - /** - * @return the smType - */ public Long getSmType() { return smType; } - /** - * @param smType - * the smType to set - */ public void setSmType(final Long smType) { this.smType = smType; } + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((dsType == null) ? 0 : dsType.hashCode()); + result = prime * result + ((smType == null) ? 0 : smType.hashCode()); + return result; + } + + @Override + public boolean equals(final Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (!(obj instanceof DistributionSetTypeElementCompositeKey)) { + return false; + } + final DistributionSetTypeElementCompositeKey other = (DistributionSetTypeElementCompositeKey) obj; + if (dsType == null) { + if (other.dsType != null) { + return false; + } + } else if (!dsType.equals(other.dsType)) { + return false; + } + if (smType == null) { + if (other.smType != null) { + return false; + } + } else if (!smType.equals(other.smType)) { + return false; + } + return true; + } + } diff --git a/hawkbit-repository/src/test/java/org/eclipse/hawkbit/repository/DistributionSetManagementTest.java b/hawkbit-repository/src/test/java/org/eclipse/hawkbit/repository/DistributionSetManagementTest.java index 352d28e15..62dc21a3b 100644 --- a/hawkbit-repository/src/test/java/org/eclipse/hawkbit/repository/DistributionSetManagementTest.java +++ b/hawkbit-repository/src/test/java/org/eclipse/hawkbit/repository/DistributionSetManagementTest.java @@ -33,6 +33,7 @@ import org.eclipse.hawkbit.repository.model.DistributionSetTag; import org.eclipse.hawkbit.repository.model.DistributionSetType; import org.eclipse.hawkbit.repository.model.SoftwareModule; import org.eclipse.hawkbit.repository.model.Target; +import org.fest.assertions.core.Condition; import org.junit.Test; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; @@ -149,8 +150,7 @@ public class DistributionSetManagementTest extends AbstractIntegrationTest { .createDistributionSetType(new DistributionSetType("softdeleted", "to be deletd", "")); assertThat(distributionSetTypeRepository.findAll()).contains(softDelete); - final DistributionSet dsNewType = distributionSetManagement - .createDistributionSet(new DistributionSet("newtypesoft", "1", "", softDelete, null)); + distributionSetManagement.createDistributionSet(new DistributionSet("newtypesoft", "1", "", softDelete, null)); distributionSetManagement.deleteDistributionSetType(softDelete); assertThat(distributionSetManagement.findDistributionSetTypeByKey("softdeleted").isDeleted()).isEqualTo(true); @@ -165,6 +165,46 @@ public class DistributionSetManagementTest extends AbstractIntegrationTest { } + @Test + @Description("Verfies that a DS is of default type if not specified explicitly at creation time.") + public void createDistributionSetWithImplicitType() { + final DistributionSet set = distributionSetManagement + .createDistributionSet(new DistributionSet("newtypesoft", "1", "", null, null)); + + assertThat(set.getType()).as("Type should be equal to default type of tenant") + .isEqualTo(systemManagement.getTenantMetadata().getDefaultDsType()); + + } + + @Test + @Description("Verfies that multiple DS are of default type if not specified explicitly at creation time.") + public void createMultipleDistributionSetsWithImplicitType() { + + List sets = new ArrayList<>(); + for (int i = 0; i < 10; i++) { + sets.add(new DistributionSet("another DS" + i, "X" + i, "", null, null)); + } + + sets = distributionSetManagement.createDistributionSets(sets); + + assertThat(sets).as("Type should be equal to default type of tenant").are(new Condition() { + @Override + public boolean matches(final DistributionSet value) { + return value.getType().equals(systemManagement.getTenantMetadata().getDefaultDsType()); + } + }); + + } + + @Test(expected = EntityAlreadyExistsException.class) + @Description("Verfies that a DS entity cannot be used for creation.") + public void createDistributionSetFailsOnExistingEntity() { + final DistributionSet set = distributionSetManagement + .createDistributionSet(new DistributionSet("newtypesoft", "1", "", null, null)); + distributionSetManagement.createDistributionSet(set); + + } + @Test @Description("Checks that metadata for a distribution set can be created.") public void createDistributionSetMetadata() { @@ -725,7 +765,7 @@ public class DistributionSetManagementTest extends AbstractIntegrationTest { dsAssigned.getVersion()); final Target target = new Target("4712"); final Target savedTarget = targetManagement.createTarget(target); - final List toAssign = new ArrayList(); + final List toAssign = new ArrayList<>(); toAssign.add(savedTarget); deploymentManagement.assignDistributionSet(dsAssigned, toAssign); @@ -739,38 +779,6 @@ public class DistributionSetManagementTest extends AbstractIntegrationTest { .getTotalElements()).isEqualTo(2); } - /** - * helper method which re-orders a list as expected. Re-orders the given - * distribution set in the order as given and returns a new list with the - * new order. - * - * @param dsThree - * @param buildDistributionSets - * @return - */ - private List reOrderDSList(final Iterable buildDistributionSets, - final DistributionSet... ds) { - final List reOrderedList = new ArrayList<>(); - - final Iterator iterator = buildDistributionSets.iterator(); - while (iterator.hasNext()) { - final DistributionSet next = iterator.next(); - int reorder = -1; - for (int index = 0; index < ds.length; index++) { - if (next.equals(ds[index])) { - reorder = index; - } - } - if (reorder >= 0) { - reOrderedList.add(reorder, next); - } else { - reOrderedList.add(next); - } - } - - return reOrderedList; - } - private Target sendUpdateActionStatusToTarget(final Status status, final Action updActA, final Target t, final String... msgs) { updActA.setStatus(status);