Skip to content

Commit

Permalink
[#1509] Sync Target type with SW and DS types / getByKey (Mgmt Layer) (
Browse files Browse the repository at this point in the history
…#1516)

Signed-off-by: Marinov Avgustin <[email protected]>
  • Loading branch information
avgustinmm authored Dec 13, 2023
1 parent 4ac42c8 commit d951cee
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@
*/
public interface TargetTypeManagement {

/**
* @param key
* as {@link TargetType#getKey()}
* @return {@link TargetType}
*/
@PreAuthorize(SpPermission.SpringEvalExpressions.HAS_AUTH_READ_TARGET)
Optional<TargetType> getByKey(@NotEmpty String key);

/**
* @param name
* as {@link TargetType#getName()}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ public JpaTargetTypeManagement(final TargetTypeRepository targetTypeRepository,
this.quotaManagement = quotaManagement;
}

@Override
public Optional<TargetType> getByKey(final String key) {
return targetTypeRepository.findOne(TargetTypeSpecification.hasKey(key)).map(TargetType.class::cast);
}

@Override
public Optional<TargetType> getByName(final String name) {
return targetTypeRepository.findOne(TargetTypeSpecification.hasName(name)).map(TargetType.class::cast);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,7 @@ public interface TargetTypeRepository
* {@link TargetType#getName()}
*/
default List<JpaTargetType> findByDsType(@Param("id") final Long dsTypeId) {
return this.findAll(Specification.where(TargetTypeSpecification.hasDsSetType(dsTypeId)));
}

/**
*
* @param name
* to search for
* @return all {@link TargetType}s in the repository with given
* {@link TargetType#getName()}
*/
default Optional<JpaTargetType> findByName(final String name) {
return this.findOne(Specification.where(TargetTypeSpecification.hasName(name)));
return findAll(Specification.where(TargetTypeSpecification.hasDsSetType(dsTypeId)));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,18 @@ public static Specification<JpaTargetType> hasDsSetType(final Long dsTypeId) {
};
}

/**
* {@link Specification} for retrieving {@link TargetType} with
* given {@link TargetType#getKey()} including fetching the
* elements list.
*
* @param key
* to search
* @return the {@link TargetType} {@link Specification}
*/
public static Specification<JpaTargetType> hasKey(final String key) {
return (targetRoot, query, cb) -> cb.equal(targetRoot.get(JpaTargetType_.key), key);
}

/**
* {@link Specification} for retrieving {@link TargetType} with
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.eclipse.hawkbit.repository.model.DistributionSetType;
import org.eclipse.hawkbit.repository.model.NamedEntity;
import org.eclipse.hawkbit.repository.model.TargetType;
import org.eclipse.hawkbit.repository.model.Type;
import org.eclipse.hawkbit.repository.test.matcher.Expect;
import org.eclipse.hawkbit.repository.test.matcher.ExpectEvents;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -63,16 +64,18 @@ void entityQueriesReferringToNotExistingEntitiesThrowsException() {
@Description("Verify that a target type with invalid properties cannot be created or updated")
void createAndUpdateTargetTypeWithInvalidFields() {
final TargetType targetType = targetTypeManagement
.create(entityFactory.targetType().create().name("targettype1").description("targettypedes1"));
.create(entityFactory.targetType().create()
.name("targettype1").description("targettypedes1")
.key("targettype1.key"));

createAndUpdateTargetTypeWithInvalidDescription(targetType);
createAndUpdateTargetTypeWithInvalidColour(targetType);
createTargetTypeWithInvalidKey();
createAndUpdateTargetTypeWithInvalidName(targetType);
}

@Step
void createAndUpdateTargetTypeWithInvalidDescription(final TargetType targetType) {

assertThatExceptionOfType(ConstraintViolationException.class)
.as("targetType with too long description should not be created")
.isThrownBy(() -> targetTypeManagement.create(
Expand All @@ -97,7 +100,6 @@ void createAndUpdateTargetTypeWithInvalidDescription(final TargetType targetType

@Step
private void createAndUpdateTargetTypeWithInvalidColour(final TargetType targetType) {

assertThatExceptionOfType(ConstraintViolationException.class)
.as("targetType with too long colour should not be created")
.isThrownBy(() -> targetTypeManagement.create(
Expand All @@ -120,8 +122,20 @@ private void createAndUpdateTargetTypeWithInvalidColour(final TargetType targetT
}

@Step
private void createAndUpdateTargetTypeWithInvalidName(final TargetType targetType) {
private void createTargetTypeWithInvalidKey() {
assertThatExceptionOfType(ConstraintViolationException.class)
.as("targetType with too long key should not be created")
.isThrownBy(() -> targetTypeManagement
.create(entityFactory.targetType().create().name(RandomStringUtils.randomAlphanumeric(
Type.KEY_MAX_SIZE + 1))));

assertThatExceptionOfType(ConstraintViolationException.class)
.as("targetType with invalid key should not be created").isThrownBy(
() -> targetTypeManagement.create(entityFactory.targetType().create().name(INVALID_TEXT_HTML)));
}

@Step
private void createAndUpdateTargetTypeWithInvalidName(final TargetType targetType) {
assertThatExceptionOfType(ConstraintViolationException.class)
.as("targetType with too long name should not be created")
.isThrownBy(() -> targetTypeManagement
Expand Down Expand Up @@ -152,7 +166,9 @@ private void createAndUpdateTargetTypeWithInvalidName(final TargetType targetTyp
@Description("Tests the successful assignment of compatible distribution set types to a target type")
void assignCompatibleDistributionSetTypesToTargetType() {
final TargetType targetType = targetTypeManagement
.create(entityFactory.targetType().create().name("targettype1").description("targettypedes1"));
.create(entityFactory.targetType().create()
.name("targettype1").description("targettypedes1")
.key("targettyp1.key"));
DistributionSetType distributionSetType = testdataFactory.findOrCreateDistributionSetType("testDst", "dst1");
targetTypeManagement.assignCompatibleDistributionSetTypes(targetType.getId(), Collections.singletonList(distributionSetType.getId()));

Expand All @@ -165,7 +181,9 @@ void assignCompatibleDistributionSetTypesToTargetType() {
@Description("Tests the successful removal of compatible distribution set types to a target type")
void unassignCompatibleDistributionSetTypesToTargetType() {
final TargetType targetType = targetTypeManagement
.create(entityFactory.targetType().create().name("targettype11").description("targettypedes11"));
.create(entityFactory.targetType().create()
.name("targettype1").description("targettypedes1")
.key("targettyp1.key"));
DistributionSetType distributionSetType = testdataFactory.findOrCreateDistributionSetType("testDst1", "dst11");
targetTypeManagement.assignCompatibleDistributionSetTypes(targetType.getId(), Collections.singletonList(distributionSetType.getId()));
Optional<JpaTargetType> targetTypeWithDsTypes = targetTypeRepository.findById(targetType.getId());
Expand All @@ -187,13 +205,31 @@ void findAllTargetTypes() {
@Test
@Description("Ensures that a created target type is persisted in the repository as defined.")
void createTargetType() {
final TargetType targetType = targetTypeManagement
.create(entityFactory.targetType().create().name("targettype1").description("targettypedes1").colour("colour1"));

assertThat(targetTypeRepository.findByName("targettype1").get().getDescription()).as("type found")
.isEqualTo("targettypedes1");
assertThat(targetTypeManagement.getByName("targettype1").get().getColour()).as("type found").isEqualTo("colour1");
assertThat(targetTypeManagement.get(targetType.getId()).get().getColour()).as("type found").isEqualTo("colour1");
final String name = "targettype1";
final String key = "targettype1.key";
targetTypeManagement
.create(entityFactory.targetType().create()
.name(name)
.description("targettypedes1")
.key(key)
.colour("colour1"));

assertThat(findByName(name).map(JpaTargetType::getName).orElse(null)).as("type found (name)")
.isEqualTo(name);
assertThat(findByName(name).map(JpaTargetType::getDescription).orElse(null))
.as("type found (des)").isEqualTo("targettypedes1");
assertThat(findByName(name).map(JpaTargetType::getKey).orElse(null)).as("type found (key)")
.isEqualTo(key);
assertThat(findByName(name).map(JpaTargetType::getColour).orElse(null))
.as("type found (colour)").isEqualTo("colour1");
assertThat(findByKey(key).map(JpaTargetType::getName).orElse(null)).as("type found (name)")
.isEqualTo(name);
assertThat(findByKey(key).map(JpaTargetType::getDescription).orElse(null))
.as("type found (des)").isEqualTo("targettypedes1");
assertThat(findByKey(key).map(JpaTargetType::getKey).orElse(null)).as("type found (key)")
.isEqualTo(key);
assertThat(findByKey(key).map(JpaTargetType::getColour).orElse(null)).as("type found (colour)")
.isEqualTo("colour1");
}

@Test
Expand All @@ -202,7 +238,7 @@ void deleteTargetType() {
// create test data
final TargetType targetType = targetTypeManagement
.create(entityFactory.targetType().create().name("targettype11").description("targettypedes11"));
assertThat(targetTypeRepository.findByName("targettype11").get().getDescription()).as("type found")
assertThat(findByName("targettype11").get().getDescription()).as("type found")
.isEqualTo("targettypedes11");
targetTypeManagement.delete(targetType.getId());
assertThat(targetTypeRepository.findById(targetType.getId())).as("No target type should be found").isNotPresent();
Expand All @@ -214,10 +250,10 @@ void deleteTargetType() {
void updateTargetType() {
final TargetType targetType = targetTypeManagement
.create(entityFactory.targetType().create().name("targettype111").description("targettypedes111"));
assertThat(targetTypeRepository.findByName("targettype111").get().getDescription()).as("type found")
assertThat(findByName("targettype111").get().getDescription()).as("type found")
.isEqualTo("targettypedes111");
targetTypeManagement.update(entityFactory.targetType().update(targetType.getId()).name("updatedtargettype111"));
assertThat(targetTypeRepository.findByName("updatedtargettype111")).as("Updated target type should be found").isPresent();
assertThat(findByName("updatedtargettype111")).as("Updated target type should be found").isPresent();
}

@Test
Expand All @@ -235,4 +271,11 @@ void failedDuplicateTargetTypeNameExceptionAfterUpdate() {
assertThrows(EntityAlreadyExistsException.class, () -> targetTypeManagement.update(entityFactory.targetType().update(targetType.getId()).name("targettype1234")));
}

private Optional<JpaTargetType> findByName(final String name) {
return targetTypeManagement.getByName(name).map(JpaTargetType.class::cast);
}

private Optional<JpaTargetType> findByKey(final String key) {
return targetTypeManagement.getByKey(key).map(JpaTargetType.class::cast);
}
}

0 comments on commit d951cee

Please sign in to comment.