Sonar Fixes (11) (#2223)

Signed-off-by: Avgustin Marinov <Avgustin.Marinov@bosch.com>
This commit is contained in:
Avgustin Marinov
2025-01-23 17:56:58 +02:00
committed by GitHub
parent e608996e43
commit 1104256c4c
5 changed files with 136 additions and 123 deletions

View File

@@ -57,8 +57,8 @@ public interface DistributionSetCreate {
* @param type for {@link DistributionSet#getType()} * @param type for {@link DistributionSet#getType()}
* @return updated builder instance * @return updated builder instance
*/ */
default DistributionSetCreate type(final DistributionSetType type) { default DistributionSetCreate type(@NotNull final DistributionSetType type) {
return type(Optional.ofNullable(type).map(DistributionSetType::getKey).orElse(null)); return type(type.getKey());
} }
/** /**

View File

@@ -45,7 +45,7 @@ public class JpaDistributionSetCreate extends AbstractDistributionSetUpdateCreat
@Override @Override
public DistributionSetCreate type(final String type) { public DistributionSetCreate type(final String type) {
this.type = type == null ? null : type.strip(); this.type = type.strip();
return this; return this;
} }

View File

@@ -168,16 +168,17 @@ class DistributionSetInvalidationManagementTest extends AbstractJpaIntegrationTe
void verifyInvalidateIncompleteDistributionSetThrowsException() { void verifyInvalidateIncompleteDistributionSetThrowsException() {
final DistributionSet distributionSet = testdataFactory.createIncompleteDistributionSet(); final DistributionSet distributionSet = testdataFactory.createIncompleteDistributionSet();
final DistributionSetInvalidation distributionSetInvalidation = new DistributionSetInvalidation(
List.of(distributionSet.getId()), CancelationType.SOFT, true);
assertThatExceptionOfType(IncompleteDistributionSetException.class) assertThatExceptionOfType(IncompleteDistributionSetException.class)
.as("Incomplete distributionSet should throw an exception") .as("Incomplete distributionSet should throw an exception")
.isThrownBy(() -> distributionSetInvalidationManagement.invalidateDistributionSet( .isThrownBy(() -> distributionSetInvalidationManagement.invalidateDistributionSet(distributionSetInvalidation));
new DistributionSetInvalidation(Collections.singletonList(distributionSet.getId()),
CancelationType.SOFT, true)));
} }
@Test @Test
@Description("Verify that invalidating an invalidated distribution set don't throws an exception" + @Description("Verify that invalidating an invalidated distribution set don't throws an exception" +
" -> should be able to cancel actions again (if previous time there was a problem") " -> should be able to cancel actions again (if previous time there was a problem")
@SuppressWarnings("java:S2699") // test that no exception is thrown
void verifyInvalidateInvalidatedDistributionSetDontThrowsException() { void verifyInvalidateInvalidatedDistributionSetDontThrowsException() {
final DistributionSet distributionSet = testdataFactory.createAndInvalidateDistributionSet(); final DistributionSet distributionSet = testdataFactory.createAndInvalidateDistributionSet();
distributionSetInvalidationManagement.invalidateDistributionSet( distributionSetInvalidationManagement.invalidateDistributionSet(
@@ -207,12 +208,11 @@ class DistributionSetInvalidationManagementTest extends AbstractJpaIntegrationTe
final InvalidationTestData invalidationTestData = systemSecurityContext.runAsSystem( final InvalidationTestData invalidationTestData = systemSecurityContext.runAsSystem(
() -> createInvalidationTestData("verifyInvalidateWithUpdateRepoAndUpdateTargetAuthority")); () -> createInvalidationTestData("verifyInvalidateWithUpdateRepoAndUpdateTargetAuthority"));
final DistributionSetInvalidation distributionSetInvalidation = new DistributionSetInvalidation(
List.of(invalidationTestData.getDistributionSet().getId()), CancelationType.SOFT, true);
assertThatExceptionOfType(InsufficientPermissionException.class) assertThatExceptionOfType(InsufficientPermissionException.class)
.as("Insufficient permission exception expected") .as("Insufficient permission exception expected")
.isThrownBy(() -> distributionSetInvalidationManagement .isThrownBy(() -> distributionSetInvalidationManagement.invalidateDistributionSet(distributionSetInvalidation));
.invalidateDistributionSet(new DistributionSetInvalidation(
Collections.singletonList(invalidationTestData.getDistributionSet().getId()),
CancelationType.SOFT, true)));
distributionSetInvalidationManagement.invalidateDistributionSet(new DistributionSetInvalidation( distributionSetInvalidationManagement.invalidateDistributionSet(new DistributionSetInvalidation(
Collections.singletonList(invalidationTestData.getDistributionSet().getId()), CancelationType.NONE, Collections.singletonList(invalidationTestData.getDistributionSet().getId()), CancelationType.NONE,
@@ -245,6 +245,7 @@ class DistributionSetInvalidationManagementTest extends AbstractJpaIntegrationTe
private void assertRolloutGroupsAreFinished(final Rollout rollout) { private void assertRolloutGroupsAreFinished(final Rollout rollout) {
assertThat(rolloutGroupRepository.findByRolloutId(rollout.getId(), PAGE)) assertThat(rolloutGroupRepository.findByRolloutId(rollout.getId(), PAGE))
.isNotEmpty()
.allMatch(rolloutGroup -> rolloutGroup.getStatus().equals(RolloutGroupStatus.FINISHED)); .allMatch(rolloutGroup -> rolloutGroup.getStatus().equals(RolloutGroupStatus.FINISHED));
} }

View File

@@ -22,7 +22,6 @@ import java.util.List;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.Optional; import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
import jakarta.validation.ConstraintViolationException; import jakarta.validation.ConstraintViolationException;
@@ -35,6 +34,7 @@ import org.assertj.core.api.Condition;
import org.eclipse.hawkbit.repository.DistributionSetManagement; import org.eclipse.hawkbit.repository.DistributionSetManagement;
import org.eclipse.hawkbit.repository.RepositoryProperties; import org.eclipse.hawkbit.repository.RepositoryProperties;
import org.eclipse.hawkbit.repository.builder.DistributionSetCreate; import org.eclipse.hawkbit.repository.builder.DistributionSetCreate;
import org.eclipse.hawkbit.repository.builder.DistributionSetUpdate;
import org.eclipse.hawkbit.repository.event.remote.entity.DistributionSetCreatedEvent; 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.DistributionSetTagCreatedEvent;
import org.eclipse.hawkbit.repository.event.remote.entity.DistributionSetUpdatedEvent; import org.eclipse.hawkbit.repository.event.remote.entity.DistributionSetUpdatedEvent;
@@ -220,11 +220,10 @@ class DistributionSetManagementTest extends AbstractJpaIntegrationTest {
@Test @Test
@Description("Verifies that a DS cannot be created if another DS with same name and version exists.") @Description("Verifies that a DS cannot be created if another DS with same name and version exists.")
void createDistributionSetWithDuplicateNameAndVersionFails() { void createDistributionSetWithDuplicateNameAndVersionFails() {
distributionSetManagement.create(entityFactory.distributionSet().create().name("newtypesoft").version("1")); final DistributionSetCreate distributionSetCreate = entityFactory.distributionSet().create().name("newtypesoft").version("1");
distributionSetManagement.create(distributionSetCreate);
assertThatExceptionOfType(EntityAlreadyExistsException.class).isThrownBy(() -> distributionSetManagement
.create(entityFactory.distributionSet().create().name("newtypesoft").version("1")));
assertThatExceptionOfType(EntityAlreadyExistsException.class).isThrownBy(() -> distributionSetManagement.create(distributionSetCreate));
} }
@Test @Test
@@ -277,9 +276,10 @@ class DistributionSetManagementTest extends AbstractJpaIntegrationTest {
} }
// quota exceeded // quota exceeded
final Long ds1Id = ds1.getId();
final JpaDistributionSetMetadata jpaMaxMetaData = new JpaDistributionSetMetadata("k" + maxMetaData, ds1, "v" + maxMetaData);
assertThatExceptionOfType(AssignmentQuotaExceededException.class) assertThatExceptionOfType(AssignmentQuotaExceededException.class)
.isThrownBy(() -> createDistributionSetMetadata(ds1.getId(), createDistributionSetMetadata(ds1.getId(), .isThrownBy(() -> createDistributionSetMetadata(ds1Id, jpaMaxMetaData));
new JpaDistributionSetMetadata("k" + maxMetaData, ds1, "v" + maxMetaData))));
// add multiple meta data entries at once // add multiple meta data entries at once
final DistributionSet ds2 = testdataFactory.createDistributionSet("ds2"); final DistributionSet ds2 = testdataFactory.createDistributionSet("ds2");
@@ -288,14 +288,16 @@ class DistributionSetManagementTest extends AbstractJpaIntegrationTest {
metaData2.add(new JpaDistributionSetMetadata("k" + i, ds2, "v" + i)); metaData2.add(new JpaDistributionSetMetadata("k" + i, ds2, "v" + i));
} }
// verify quota is exceeded // verify quota is exceeded
final Long ds2Id = ds2.getId();
assertThatExceptionOfType(AssignmentQuotaExceededException.class) assertThatExceptionOfType(AssignmentQuotaExceededException.class)
.isThrownBy(() -> createDistributionSetMetadata(ds2.getId(), metaData2)); .isThrownBy(() -> createDistributionSetMetadata(ds2Id, metaData2));
// add some meta data entries // add some meta data entries
final DistributionSet ds3 = testdataFactory.createDistributionSet("ds3"); final DistributionSet ds3 = testdataFactory.createDistributionSet("ds3");
final int firstHalf = Math.round((maxMetaData) / 2.f); final int firstHalf = Math.round((maxMetaData) / 2.f);
final Long ds3Id = ds3.getId();
for (int i = 0; i < firstHalf; ++i) { for (int i = 0; i < firstHalf; ++i) {
createDistributionSetMetadata(ds3.getId(), new JpaDistributionSetMetadata("k" + i, ds3, "v" + i)); createDistributionSetMetadata(ds3Id, new JpaDistributionSetMetadata("k" + i, ds3, "v" + i));
} }
// add too many data entries // add too many data entries
final int secondHalf = maxMetaData - firstHalf; final int secondHalf = maxMetaData - firstHalf;
@@ -305,7 +307,7 @@ class DistributionSetManagementTest extends AbstractJpaIntegrationTest {
} }
// verify quota is exceeded // verify quota is exceeded
assertThatExceptionOfType(AssignmentQuotaExceededException.class) assertThatExceptionOfType(AssignmentQuotaExceededException.class)
.isThrownBy(() -> createDistributionSetMetadata(ds3.getId(), metaData3)); .isThrownBy(() -> createDistributionSetMetadata(ds3Id, metaData3));
} }
@@ -354,7 +356,7 @@ class DistributionSetManagementTest extends AbstractJpaIntegrationTest {
DistributionSet ds = testdataFactory.createDistributionSet("ds-1"); DistributionSet ds = testdataFactory.createDistributionSet("ds-1");
final SoftwareModule ah2 = testdataFactory.createSoftwareModuleApp(); final SoftwareModule ah2 = testdataFactory.createSoftwareModuleApp();
final SoftwareModule os2 = testdataFactory.createSoftwareModuleOs(); final Set<Long> os2Id = Set.of(testdataFactory.createSoftwareModuleOs().getId());
// update is allowed as it is still not assigned to a target // update is allowed as it is still not assigned to a target
ds = distributionSetManagement.assignSoftwareModules(ds.getId(), Set.of(ah2.getId())); ds = distributionSetManagement.assignSoftwareModules(ds.getId(), Set.of(ah2.getId()));
@@ -365,7 +367,7 @@ class DistributionSetManagementTest extends AbstractJpaIntegrationTest {
final Long dsId = ds.getId(); final Long dsId = ds.getId();
// not allowed as it is assigned now // not allowed as it is assigned now
assertThatThrownBy(() -> distributionSetManagement.assignSoftwareModules(dsId, Set.of(os2.getId()))) assertThatThrownBy(() -> distributionSetManagement.assignSoftwareModules(dsId, os2Id))
.isInstanceOf(EntityReadOnlyException.class); .isInstanceOf(EntityReadOnlyException.class);
// not allowed as it is assigned now // not allowed as it is assigned now
@@ -377,19 +379,21 @@ class DistributionSetManagementTest extends AbstractJpaIntegrationTest {
@Test @Test
@Description("Ensures that it is not possible to add a software module that is not defined of the DS's type.") @Description("Ensures that it is not possible to add a software module that is not defined of the DS's type.")
void updateDistributionSetUnsupportedModuleFails() { void updateDistributionSetUnsupportedModuleFails() {
final DistributionSet set = distributionSetManagement final Long setId = distributionSetManagement.create(
.create(entityFactory entityFactory.distributionSet().create()
.distributionSet().create().name("agent-hub2").version( .type(distributionSetTypeManagement.create(
"1.0.5") entityFactory.distributionSetType().create()
.type(distributionSetTypeManagement.create(entityFactory.distributionSetType().create() .key("test")
.key("test").name("test").mandatory(singletonList(osType.getId()))).getKey())); .name("test")
.mandatory(singletonList(osType.getId()))).getKey())
.name("agent-hub2")
.version("1.0.5")).getId();
final SoftwareModule module = softwareModuleManagement.create( final Set<Long> moduleId = Set.of(softwareModuleManagement.create(
entityFactory.softwareModule().create().name("agent-hub2").version("1.0.5").type(appType.getKey())); entityFactory.softwareModule().create().name("agent-hub2").version("1.0.5").type(appType.getKey())).getId());
// update data // update data
assertThatThrownBy( assertThatThrownBy(() -> distributionSetManagement.assignSoftwareModules(setId, moduleId))
() -> distributionSetManagement.assignSoftwareModules(set.getId(), Set.of(module.getId())))
.isInstanceOf(UnsupportedSoftwareModuleForThisDistributionSetException.class); .isInstanceOf(UnsupportedSoftwareModuleForThisDistributionSetException.class);
} }
@@ -427,9 +431,9 @@ class DistributionSetManagementTest extends AbstractJpaIntegrationTest {
void updateInvalidDistributionSet() { void updateInvalidDistributionSet() {
final DistributionSet distributionSet = testdataFactory.createAndInvalidateDistributionSet(); final DistributionSet distributionSet = testdataFactory.createAndInvalidateDistributionSet();
final DistributionSetUpdate update = entityFactory.distributionSet().update(distributionSet.getId()).name("new_name");
assertThatExceptionOfType(InvalidDistributionSetException.class) assertThatExceptionOfType(InvalidDistributionSetException.class)
.as("Invalid distributionSet should throw an exception").isThrownBy(() -> distributionSetManagement .as("Invalid distributionSet should throw an exception").isThrownBy(() -> distributionSetManagement.update(update));
.update(entityFactory.distributionSet().update(distributionSet.getId()).name("new_name")));
} }
@Test @Test
@@ -445,54 +449,59 @@ class DistributionSetManagementTest extends AbstractJpaIntegrationTest {
// assign software modules one by one // assign software modules one by one
final DistributionSet ds1 = testdataFactory.createDistributionSetWithNoSoftwareModules("ds1", "1.0"); final DistributionSet ds1 = testdataFactory.createDistributionSetWithNoSoftwareModules("ds1", "1.0");
final Long ds1Id = ds1.getId();
for (int i = 0; i < maxModules; ++i) { for (int i = 0; i < maxModules; ++i) {
distributionSetManagement.assignSoftwareModules(ds1.getId(), singletonList(modules.get(i))); distributionSetManagement.assignSoftwareModules(ds1Id, singletonList(modules.get(i)));
} }
// add one more to cause the quota to be exceeded // add one more to cause the quota to be exceeded
assertThatExceptionOfType(AssignmentQuotaExceededException.class).isThrownBy(() -> distributionSetManagement final List<Long> maxModulsList = singletonList(modules.get(maxModules));
.assignSoftwareModules(ds1.getId(), singletonList(modules.get(maxModules)))); assertThatExceptionOfType(AssignmentQuotaExceededException.class)
.isThrownBy(() -> distributionSetManagement.assignSoftwareModules(ds1Id, maxModulsList));
// assign all software modules at once // assign all software modules at once
final DistributionSet ds2 = testdataFactory.createDistributionSetWithNoSoftwareModules("ds2", "1.0"); final DistributionSet ds2 = testdataFactory.createDistributionSetWithNoSoftwareModules("ds2", "1.0");
// verify quota is exceeded // verify quota is exceeded
final Long ds2Id = ds2.getId();
assertThatExceptionOfType(AssignmentQuotaExceededException.class) assertThatExceptionOfType(AssignmentQuotaExceededException.class)
.isThrownBy(() -> distributionSetManagement.assignSoftwareModules(ds2.getId(), modules)); .isThrownBy(() -> distributionSetManagement.assignSoftwareModules(ds2Id, modules));
// assign some software modules // assign some software modules
final DistributionSet ds3 = testdataFactory.createDistributionSetWithNoSoftwareModules("ds3", "1.0"); final DistributionSet ds3 = testdataFactory.createDistributionSetWithNoSoftwareModules("ds3", "1.0");
final int firstHalf = Math.round((maxModules) / 2.f); final int firstHalf = Math.round((maxModules) / 2.f);
final Long ds3Id = ds3.getId();
for (int i = 0; i < firstHalf; ++i) { for (int i = 0; i < firstHalf; ++i) {
distributionSetManagement.assignSoftwareModules(ds3.getId(), singletonList(modules.get(i))); distributionSetManagement.assignSoftwareModules(ds3Id, singletonList(modules.get(i)));
} }
// assign the remaining modules to cause the quota to be exceeded // assign the remaining modules to cause the quota to be exceeded
assertThatExceptionOfType(AssignmentQuotaExceededException.class).isThrownBy(() -> distributionSetManagement final List<Long> firstHalfAndModules = modules.subList(firstHalf, modules.size());
.assignSoftwareModules(ds3.getId(), modules.subList(firstHalf, modules.size()))); assertThatExceptionOfType(AssignmentQuotaExceededException.class)
.isThrownBy(() -> distributionSetManagement.assignSoftwareModules(ds3Id, firstHalfAndModules));
} }
@Test @Test
@Description("Verifies that an exception is thrown when trying to assign software modules to an invalidated distribution set.") @Description("Verifies that an exception is thrown when trying to assign software modules to an invalidated distribution set.")
void verifyAssignSoftwareModulesToInvalidDistributionSet() { void verifyAssignSoftwareModulesToInvalidDistributionSet() {
final DistributionSet distributionSet = testdataFactory.createAndInvalidateDistributionSet(); final Long distributionSetId = testdataFactory.createAndInvalidateDistributionSet().getId();
final SoftwareModule softwareModule = testdataFactory.createSoftwareModuleOs(); final List<Long> softwareModuleIds = List.of(testdataFactory.createSoftwareModuleOs().getId());
assertThatExceptionOfType(InvalidDistributionSetException.class) assertThatExceptionOfType(InvalidDistributionSetException.class)
.as("Invalid distributionSet should throw an exception").isThrownBy(() -> distributionSetManagement .as("Invalid distributionSet should throw an exception")
.assignSoftwareModules(distributionSet.getId(), singletonList(softwareModule.getId()))); .isThrownBy(() -> distributionSetManagement.assignSoftwareModules(distributionSetId, softwareModuleIds));
} }
@Test @Test
@Description("Verifies that an exception is thrown when trying to unassign a software module from an invalidated distribution set.") @Description("Verifies that an exception is thrown when trying to unassign a software module from an invalidated distribution set.")
void verifyUnassignSoftwareModulesToInvalidDistributionSet() { void verifyUnassignSoftwareModulesToInvalidDistributionSet() {
final DistributionSet distributionSet = testdataFactory.createDistributionSet(); final Long distributionSetId = testdataFactory.createDistributionSet().getId();
final SoftwareModule softwareModule = testdataFactory.createSoftwareModuleOs(); final Long softwareModuleId = testdataFactory.createSoftwareModuleOs().getId();
distributionSetManagement.assignSoftwareModules(distributionSet.getId(), singletonList(softwareModule.getId())); distributionSetManagement.assignSoftwareModules(distributionSetId, singletonList(softwareModuleId));
distributionSetInvalidationManagement.invalidateDistributionSet( distributionSetInvalidationManagement.invalidateDistributionSet(
new DistributionSetInvalidation(singletonList(distributionSet.getId()), CancelationType.NONE, false)); new DistributionSetInvalidation(singletonList(distributionSetId), CancelationType.NONE, false));
assertThatExceptionOfType(InvalidDistributionSetException.class) assertThatExceptionOfType(InvalidDistributionSetException.class)
.as("Invalid distributionSet should throw an exception").isThrownBy(() -> distributionSetManagement .as("Invalid distributionSet should throw an exception").isThrownBy(() -> distributionSetManagement
.unassignSoftwareModule(distributionSet.getId(), softwareModule.getId())); .unassignSoftwareModule(distributionSetId, softwareModuleId));
} }
@Test @Test
@@ -575,9 +584,9 @@ class DistributionSetManagementTest extends AbstractJpaIntegrationTest {
final List<DistributionSet> allDistributionSets = Stream final List<DistributionSet> allDistributionSets = Stream
.of(dsGroup1, dsGroup2, Arrays.asList(dsDeleted, dsInComplete, dsNewType)).flatMap(Collection::stream) .of(dsGroup1, dsGroup2, Arrays.asList(dsDeleted, dsInComplete, dsNewType)).flatMap(Collection::stream)
.collect(Collectors.toList()); .toList();
final List<DistributionSet> dsGroup1WithGroup2 = Stream.of(dsGroup1, dsGroup2).flatMap(Collection::stream) final List<DistributionSet> dsGroup1WithGroup2 = Stream.of(dsGroup1, dsGroup2).flatMap(Collection::stream)
.collect(Collectors.toList()); .toList();
final int sizeOfAllDistributionSets = allDistributionSets.size(); final int sizeOfAllDistributionSets = allDistributionSets.size();
// check setup // check setup
@@ -679,26 +688,25 @@ class DistributionSetManagementTest extends AbstractJpaIntegrationTest {
final DistributionSet distributionSet = testdataFactory.createDistributionSet("ds-1"); final DistributionSet distributionSet = testdataFactory.createDistributionSet("ds-1");
final int softwareModuleCount = distributionSet.getModules().size(); final int softwareModuleCount = distributionSet.getModules().size();
assertThat(softwareModuleCount).isNotZero(); assertThat(softwareModuleCount).isNotZero();
distributionSetManagement.lock(distributionSet.getId()); final Long distributionSetId = distributionSet.getId();
assertThat( distributionSetManagement.lock(distributionSetId);
distributionSetManagement.get(distributionSet.getId()).map(DistributionSet::isLocked).orElse(false)) assertThat(distributionSetManagement.get(distributionSetId).map(DistributionSet::isLocked).orElse(false)).isTrue();
.isTrue();
// try add // try add
final List<Long> moduleIds = List.of(testdataFactory.createSoftwareModule("sm-1").getId());
assertThatExceptionOfType(LockedException.class) assertThatExceptionOfType(LockedException.class)
.as("Attempt to modify a locked DS software modules should throw an exception") .as("Attempt to modify a locked DS software modules should throw an exception")
.isThrownBy(() -> distributionSetManagement.assignSoftwareModules( .isThrownBy(() -> distributionSetManagement.assignSoftwareModules(distributionSetId, moduleIds));
distributionSet.getId(), List.of(testdataFactory.createSoftwareModule("sm-1").getId()))); assertThat(distributionSetManagement.getWithDetails(distributionSetId).get().getModules())
assertThat(distributionSetManagement.getWithDetails(distributionSet.getId()).get().getModules())
.as("Software module shall not be added to a locked DS.") .as("Software module shall not be added to a locked DS.")
.hasSize(softwareModuleCount); .hasSize(softwareModuleCount);
// try remove // try remove
final Long fisrtModuleId = distributionSet.getModules().stream().findFirst().get().getId();
assertThatExceptionOfType(LockedException.class) assertThatExceptionOfType(LockedException.class)
.as("Attempt to modify a locked DS software modules should throw an exception") .as("Attempt to modify a locked DS software modules should throw an exception")
.isThrownBy(() -> distributionSetManagement.unassignSoftwareModule( .isThrownBy(() -> distributionSetManagement.unassignSoftwareModule(distributionSetId, fisrtModuleId));
distributionSet.getId(), distributionSet.getModules().stream().findFirst().get().getId())); assertThat(distributionSetManagement.getWithDetails(distributionSetId).get().getModules())
assertThat(distributionSetManagement.getWithDetails(distributionSet.getId()).get().getModules())
.as("Software module shall not be removed from a locked DS.") .as("Software module shall not be removed from a locked DS.")
.hasSize(softwareModuleCount); .hasSize(softwareModuleCount);
} }
@@ -861,24 +869,23 @@ class DistributionSetManagementTest extends AbstractJpaIntegrationTest {
@Test @Test
@Description("Verify that an exception is thrown when trying to get an invalid distribution set") @Description("Verify that an exception is thrown when trying to get an invalid distribution set")
void verifyGetValid() { void verifyGetValid() {
final DistributionSet distributionSet = testdataFactory.createAndInvalidateDistributionSet(); final Long distributionSetId = testdataFactory.createAndInvalidateDistributionSet().getId();
assertThatExceptionOfType(InvalidDistributionSetException.class) assertThatExceptionOfType(InvalidDistributionSetException.class)
.as("Invalid distributionSet should throw an exception") .as("Invalid distributionSet should throw an exception")
.isThrownBy(() -> distributionSetManagement.getValid(distributionSet.getId())); .isThrownBy(() -> distributionSetManagement.getValid(distributionSetId));
assertThatExceptionOfType(InvalidDistributionSetException.class) assertThatExceptionOfType(InvalidDistributionSetException.class)
.as("Invalid distributionSet should throw an exception") .as("Invalid distributionSet should throw an exception")
.isThrownBy(() -> distributionSetManagement.getValidAndComplete(distributionSet.getId())); .isThrownBy(() -> distributionSetManagement.getValidAndComplete(distributionSetId));
} }
@Test @Test
@Description("Verify that an exception is thrown when trying to get an incomplete distribution set") @Description("Verify that an exception is thrown when trying to get an incomplete distribution set")
void verifyGetValidAndComplete() { void verifyGetValidAndComplete() {
final DistributionSet distributionSet = testdataFactory.createIncompleteDistributionSet(); final Long distributionSetId = testdataFactory.createIncompleteDistributionSet().getId();
assertThatExceptionOfType(IncompleteDistributionSetException.class) assertThatExceptionOfType(IncompleteDistributionSetException.class)
.as("Incomplete distributionSet should throw an exception") .as("Incomplete distributionSet should throw an exception")
.isThrownBy(() -> distributionSetManagement.getValidAndComplete(distributionSet.getId())); .isThrownBy(() -> distributionSetManagement.getValidAndComplete(distributionSetId));
} }
@Test @Test
@@ -889,23 +896,24 @@ class DistributionSetManagementTest extends AbstractJpaIntegrationTest {
final String knownValue = "myKnownValue"; final String knownValue = "myKnownValue";
final String knownUpdateValue = "knownUpdateValue"; final String knownUpdateValue = "knownUpdateValue";
final DistributionSet ds = testdataFactory.createDistributionSet(); final Long dsId = testdataFactory.createDistributionSet().getId();
distributionSetManagement.putMetaData(ds.getId(), distributionSetManagement.putMetaData(dsId,
singletonList(entityFactory.generateDsMetadata(knownKey1, knownValue))); singletonList(entityFactory.generateDsMetadata(knownKey1, knownValue)));
distributionSetInvalidationManagement.invalidateDistributionSet( distributionSetInvalidationManagement.invalidateDistributionSet(
new DistributionSetInvalidation(singletonList(ds.getId()), CancelationType.NONE, false)); new DistributionSetInvalidation(singletonList(dsId), CancelationType.NONE, false));
// assert that no new metadata can be created // assert that no new metadata can be created
final List<MetaData> metadata = singletonList(entityFactory.generateDsMetadata(knownKey2, knownValue));
assertThatExceptionOfType(InvalidDistributionSetException.class) assertThatExceptionOfType(InvalidDistributionSetException.class)
.as("Invalid distributionSet should throw an exception") .as("Invalid distributionSet should throw an exception")
.isThrownBy(() -> distributionSetManagement.putMetaData(ds.getId(), .isThrownBy(() -> distributionSetManagement.putMetaData(dsId, metadata));
singletonList(entityFactory.generateDsMetadata(knownKey2, knownValue))));
// assert that an existing metadata can not be updated // assert that an existing metadata can not be updated
final MetaData metadata2 = entityFactory.generateDsMetadata(knownKey1, knownUpdateValue);
assertThatExceptionOfType(InvalidDistributionSetException.class) assertThatExceptionOfType(InvalidDistributionSetException.class)
.as("Invalid distributionSet should throw an exception").isThrownBy(() -> distributionSetManagement .as("Invalid distributionSet should throw an exception")
.updateMetaData(ds.getId(), entityFactory.generateDsMetadata(knownKey1, knownUpdateValue))); .isThrownBy(() -> distributionSetManagement.updateMetaData(dsId, metadata2));
} }
@Test @Test
@@ -974,80 +982,88 @@ class DistributionSetManagementTest extends AbstractJpaIntegrationTest {
@Step @Step
private void createAndUpdateDistributionSetWithInvalidDescription(final DistributionSet set) { private void createAndUpdateDistributionSetWithInvalidDescription(final DistributionSet set) {
final DistributionSetCreate distributionSetCreate =
entityFactory.distributionSet().create().name("a").version("a").description(randomString(513));
assertThatExceptionOfType(ConstraintViolationException.class) assertThatExceptionOfType(ConstraintViolationException.class)
.as("entity with too long description should not be created") .as("entity with too long description should not be created")
.isThrownBy(() -> distributionSetManagement.create(entityFactory.distributionSet().create().name("a") .isThrownBy(() -> distributionSetManagement.create(distributionSetCreate));
.version("a").description(randomString(513))));
final DistributionSetCreate distributionSetCreate2 =
entityFactory.distributionSet().create().name("a").version("a").description(INVALID_TEXT_HTML);
assertThatExceptionOfType(ConstraintViolationException.class) assertThatExceptionOfType(ConstraintViolationException.class)
.as("entity with invalid description should not be created") .as("entity with invalid description should not be created")
.isThrownBy(() -> distributionSetManagement.create(entityFactory.distributionSet().create().name("a") .isThrownBy(() -> distributionSetManagement.create(distributionSetCreate2));
.version("a").description(INVALID_TEXT_HTML)));
final DistributionSetUpdate distributionSetUpdate =
entityFactory.distributionSet().update(set.getId()).description(randomString(513));
assertThatExceptionOfType(ConstraintViolationException.class) assertThatExceptionOfType(ConstraintViolationException.class)
.as("entity with too long description should not be updated") .as("entity with too long description should not be updated")
.isThrownBy(() -> distributionSetManagement.update(entityFactory.distributionSet().update(set.getId()) .isThrownBy(() -> distributionSetManagement.update(distributionSetUpdate));
.description(randomString(513))));
final DistributionSetUpdate distributionSetUpdate2 = entityFactory.distributionSet().update(set.getId()).description(INVALID_TEXT_HTML);
assertThatExceptionOfType(ConstraintViolationException.class) assertThatExceptionOfType(ConstraintViolationException.class)
.as("entity with invalid characters should not be updated").isThrownBy(() -> distributionSetManagement .as("entity with invalid characters should not be updated")
.update(entityFactory.distributionSet().update(set.getId()).description(INVALID_TEXT_HTML))); .isThrownBy(() -> distributionSetManagement.update(distributionSetUpdate2));
} }
@Step @Step
private void createAndUpdateDistributionSetWithInvalidName(final DistributionSet set) { private void createAndUpdateDistributionSetWithInvalidName(final DistributionSet set) {
final DistributionSetCreate distributionSetCreate = entityFactory.distributionSet().create()
.version("a").name(randomString(NamedEntity.NAME_MAX_SIZE + 1));
assertThatExceptionOfType(ConstraintViolationException.class) assertThatExceptionOfType(ConstraintViolationException.class)
.as("entity with too long name should not be created") .as("entity with too long name should not be created")
.isThrownBy(() -> distributionSetManagement.create(entityFactory.distributionSet().create() .isThrownBy(() -> distributionSetManagement.create(distributionSetCreate));
.version("a")
.name(randomString(NamedEntity.NAME_MAX_SIZE + 1))));
final DistributionSetCreate distributionSetCreate2 = entityFactory.distributionSet().create().version("a").name("");
assertThatExceptionOfType(ConstraintViolationException.class) assertThatExceptionOfType(ConstraintViolationException.class)
.as("entity with too short name should not be created").isThrownBy(() -> distributionSetManagement .as("entity with too short name should not be created")
.create(entityFactory.distributionSet().create().version("a").name(""))); .isThrownBy(() -> distributionSetManagement.create(distributionSetCreate2));
final DistributionSetCreate distributionSetCreate3 = entityFactory.distributionSet().create().version("a").name(INVALID_TEXT_HTML);
assertThatExceptionOfType(ConstraintViolationException.class) assertThatExceptionOfType(ConstraintViolationException.class)
.as("entity with invalid characters in name should not be created") .as("entity with invalid characters in name should not be created")
.isThrownBy(() -> distributionSetManagement .isThrownBy(() -> distributionSetManagement.create(distributionSetCreate3));
.create(entityFactory.distributionSet().create().version("a").name(INVALID_TEXT_HTML)));
final DistributionSetUpdate distributionSetUpdate = entityFactory.distributionSet().update(set.getId())
.name(randomString(NamedEntity.NAME_MAX_SIZE + 1));
assertThatExceptionOfType(ConstraintViolationException.class) assertThatExceptionOfType(ConstraintViolationException.class)
.as("entity with too long name should not be updated") .as("entity with too long name should not be updated")
.isThrownBy(() -> distributionSetManagement.update(entityFactory.distributionSet().update(set.getId()) .isThrownBy(() -> distributionSetManagement.update(distributionSetUpdate));
.name(randomString(NamedEntity.NAME_MAX_SIZE + 1))));
final DistributionSetUpdate distributionSetUpdate2 = entityFactory.distributionSet().update(set.getId()).name(INVALID_TEXT_HTML);
assertThatExceptionOfType(ConstraintViolationException.class) assertThatExceptionOfType(ConstraintViolationException.class)
.as("entity with invalid characters should not be updated").isThrownBy(() -> distributionSetManagement .as("entity with invalid characters should not be updated")
.update(entityFactory.distributionSet().update(set.getId()).name(INVALID_TEXT_HTML))); .isThrownBy(() -> distributionSetManagement.update(distributionSetUpdate2));
final DistributionSetUpdate distributionSetUpdate3 = entityFactory.distributionSet().update(set.getId()).name("");
assertThatExceptionOfType(ConstraintViolationException.class) assertThatExceptionOfType(ConstraintViolationException.class)
.as("entity with too short name should not be updated").isThrownBy(() -> distributionSetManagement .as("entity with too short name should not be updated")
.update(entityFactory.distributionSet().update(set.getId()).name(""))); .isThrownBy(() -> distributionSetManagement.update(distributionSetUpdate3));
} }
@Step @Step
private void createAndUpdateDistributionSetWithInvalidVersion(final DistributionSet set) { private void createAndUpdateDistributionSetWithInvalidVersion(final DistributionSet set) {
final DistributionSetCreate distributionSetCreate = entityFactory.distributionSet().create()
.name("a").version(randomString(NamedVersionedEntity.VERSION_MAX_SIZE + 1));
assertThatExceptionOfType(ConstraintViolationException.class) assertThatExceptionOfType(ConstraintViolationException.class)
.as("entity with too long version should not be created") .as("entity with too long version should not be created")
.isThrownBy(() -> distributionSetManagement.create(entityFactory.distributionSet().create().name("a") .isThrownBy(() -> distributionSetManagement.create(distributionSetCreate));
.version(randomString(NamedVersionedEntity.VERSION_MAX_SIZE + 1))));
final DistributionSetCreate distributionSetCreate2 = entityFactory.distributionSet().create().name("a").version("");
assertThatExceptionOfType(ConstraintViolationException.class) assertThatExceptionOfType(ConstraintViolationException.class)
.as("entity with too short version should not be created").isThrownBy(() -> distributionSetManagement .as("entity with too short version should not be created")
.create(entityFactory.distributionSet().create().name("a").version(""))); .isThrownBy(() -> distributionSetManagement.create(distributionSetCreate2));
final DistributionSetUpdate distributionSetUpdate = entityFactory.distributionSet().update(set.getId())
.version(randomString(NamedVersionedEntity.VERSION_MAX_SIZE + 1));
assertThatExceptionOfType(ConstraintViolationException.class) assertThatExceptionOfType(ConstraintViolationException.class)
.as("entity with too long version should not be updated") .as("entity with too long version should not be updated")
.isThrownBy(() -> distributionSetManagement.update(entityFactory.distributionSet().update(set.getId()) .isThrownBy(() -> distributionSetManagement.update(distributionSetUpdate));
.version(randomString(NamedVersionedEntity.VERSION_MAX_SIZE + 1))));
final DistributionSetUpdate distributionSetUpdate2 = entityFactory.distributionSet().update(set.getId()).version("");
assertThatExceptionOfType(ConstraintViolationException.class) assertThatExceptionOfType(ConstraintViolationException.class)
.as("entity with too short version should not be updated").isThrownBy(() -> distributionSetManagement .as("entity with too short version should not be updated")
.update(entityFactory.distributionSet().update(set.getId()).version(""))); .isThrownBy(() -> distributionSetManagement.update(distributionSetUpdate2));
} }
@Step @Step
@@ -1089,34 +1105,34 @@ class DistributionSetManagementTest extends AbstractJpaIntegrationTest {
private void validateSearchText(final List<DistributionSet> allDistributionSets, final String dsNamePrefix) { private void validateSearchText(final List<DistributionSet> allDistributionSets, final String dsNamePrefix) {
final List<DistributionSet> withTestNamePrefix = allDistributionSets.stream() final List<DistributionSet> withTestNamePrefix = allDistributionSets.stream()
.filter(ds -> ds.getName().startsWith(dsNamePrefix)).collect(Collectors.toList()); .filter(ds -> ds.getName().startsWith(dsNamePrefix)).toList();
assertThatFilterContainsOnlyGivenDistributionSets(DistributionSetFilter.builder().searchText(dsNamePrefix), assertThatFilterContainsOnlyGivenDistributionSets(DistributionSetFilter.builder().searchText(dsNamePrefix),
withTestNamePrefix); withTestNamePrefix);
final List<DistributionSet> withTestNameExact = withTestNamePrefix.stream() final List<DistributionSet> withTestNameExact = withTestNamePrefix.stream()
.filter(ds -> ds.getName().equals(dsNamePrefix)).collect(Collectors.toList()); .filter(ds -> ds.getName().equals(dsNamePrefix)).toList();
assertThatFilterContainsOnlyGivenDistributionSets( assertThatFilterContainsOnlyGivenDistributionSets(
DistributionSetFilter.builder().searchText(dsNamePrefix + ":"), withTestNameExact); DistributionSetFilter.builder().searchText(dsNamePrefix + ":"), withTestNameExact);
final List<DistributionSet> withTestNameExactAndVersionPrefix = withTestNameExact.stream() final List<DistributionSet> withTestNameExactAndVersionPrefix = withTestNameExact.stream()
.filter(ds -> ds.getVersion().startsWith("1")).collect(Collectors.toList()); .filter(ds -> ds.getVersion().startsWith("1")).toList();
assertThatFilterContainsOnlyGivenDistributionSets( assertThatFilterContainsOnlyGivenDistributionSets(
DistributionSetFilter.builder().searchText(dsNamePrefix + ":1"), DistributionSetFilter.builder().searchText(dsNamePrefix + ":1"),
withTestNameExactAndVersionPrefix); withTestNameExactAndVersionPrefix);
final List<DistributionSet> dsWithExactNameAndVersion = withTestNameExactAndVersionPrefix.stream() final List<DistributionSet> dsWithExactNameAndVersion = withTestNameExactAndVersionPrefix.stream()
.filter(ds -> ds.getVersion().equals("1.0.0")).collect(Collectors.toList()); .filter(ds -> ds.getVersion().equals("1.0.0")).toList();
assertThat(dsWithExactNameAndVersion).hasSize(1); assertThat(dsWithExactNameAndVersion).hasSize(1);
assertThatFilterContainsOnlyGivenDistributionSets( assertThatFilterContainsOnlyGivenDistributionSets(
DistributionSetFilter.builder().searchText(dsNamePrefix + ":1.0.0"), dsWithExactNameAndVersion); DistributionSetFilter.builder().searchText(dsNamePrefix + ":1.0.0"), dsWithExactNameAndVersion);
final List<DistributionSet> withVersionPrefix = allDistributionSets.stream() final List<DistributionSet> withVersionPrefix = allDistributionSets.stream()
.filter(ds -> ds.getVersion().startsWith("1.0.")).collect(Collectors.toList()); .filter(ds -> ds.getVersion().startsWith("1.0.")).toList();
assertThatFilterContainsOnlyGivenDistributionSets(DistributionSetFilter.builder().searchText(":1.0."), assertThatFilterContainsOnlyGivenDistributionSets(DistributionSetFilter.builder().searchText(":1.0."),
withVersionPrefix); withVersionPrefix);
final List<DistributionSet> withVersionExact = withVersionPrefix.stream() final List<DistributionSet> withVersionExact = withVersionPrefix.stream()
.filter(ds -> ds.getVersion().equals("1.0.0")).collect(Collectors.toList()); .filter(ds -> ds.getVersion().equals("1.0.0")).toList();
assertThatFilterContainsOnlyGivenDistributionSets(DistributionSetFilter.builder().searchText(":1.0.0"), assertThatFilterContainsOnlyGivenDistributionSets(DistributionSetFilter.builder().searchText(":1.0.0"),
withVersionExact); withVersionExact);

View File

@@ -831,7 +831,7 @@ class TargetManagementTest extends AbstractJpaIntegrationTest {
@Test @Test
@WithUser(allSpPermissions = true) @WithUser(allSpPermissions = true)
@Description("Checks that metadata for a target can be updated.") @Description("Checks that metadata for a target can be updated.")
void updateTargetMetadata() throws InterruptedException { void updateTargetMetadata() {
final String knownKey = "myKnownKey"; final String knownKey = "myKnownKey";
final String knownValue = "myKnownValue"; final String knownValue = "myKnownValue";
final String knownUpdateValue = "myNewUpdatedValue"; final String knownUpdateValue = "myNewUpdatedValue";
@@ -844,17 +844,13 @@ class TargetManagementTest extends AbstractJpaIntegrationTest {
// create target meta data entry // create target meta data entry
insertTargetMetadata(knownKey, knownValue, target); insertTargetMetadata(knownKey, knownValue, target);
Target changedLockRevisionTarget = targetManagement.get(target.getId()) Target changedLockRevisionTarget = targetManagement.get(target.getId()).orElseThrow(NoSuchElementException::new);
.orElseThrow(NoSuchElementException::new);
assertThat(changedLockRevisionTarget.getOptLockRevision()).isEqualTo(2); assertThat(changedLockRevisionTarget.getOptLockRevision()).isEqualTo(2);
// Unsure if needed maybe to wait for a db flush?
// Thread.sleep(100);
// update the target metadata // update the target metadata
final JpaTargetMetadata updated = (JpaTargetMetadata) targetManagement.updateMetadata(target.getControllerId(), final JpaTargetMetadata updated = (JpaTargetMetadata) targetManagement.updateMetadata(
entityFactory.generateTargetMetadata(knownKey, knownUpdateValue)); target.getControllerId(), entityFactory.generateTargetMetadata(knownKey, knownUpdateValue));
// we are updating the target meta data so also modifying the base // we are updating the target meta-data so also modifying the base
// software module so opt lock revision must be three // software module so opt lock revision must be three
changedLockRevisionTarget = targetManagement.get(target.getId()).orElseThrow(NoSuchElementException::new); changedLockRevisionTarget = targetManagement.get(target.getId()).orElseThrow(NoSuchElementException::new);
assertThat(changedLockRevisionTarget.getOptLockRevision()).isEqualTo(3); assertThat(changedLockRevisionTarget.getOptLockRevision()).isEqualTo(3);