From d951cee6b223d6bb81a5860ae2e476296f89856d Mon Sep 17 00:00:00 2001 From: Avgustin Marinov Date: Wed, 13 Dec 2023 16:42:10 +0200 Subject: [PATCH] [#1509] Sync Target type with SW and DS types / getByKey (Mgmt Layer) (#1516) Signed-off-by: Marinov Avgustin --- .../repository/TargetTypeManagement.java | 8 ++ .../management/JpaTargetTypeManagement.java | 5 ++ .../jpa/repository/TargetTypeRepository.java | 13 +--- .../TargetTypeSpecification.java | 12 +++ .../management/TargetTypeManagementTest.java | 75 +++++++++++++++---- 5 files changed, 85 insertions(+), 28 deletions(-) diff --git a/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/TargetTypeManagement.java b/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/TargetTypeManagement.java index f530ea2a83..d131662e94 100644 --- a/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/TargetTypeManagement.java +++ b/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/TargetTypeManagement.java @@ -32,6 +32,14 @@ */ public interface TargetTypeManagement { + /** + * @param key + * as {@link TargetType#getKey()} + * @return {@link TargetType} + */ + @PreAuthorize(SpPermission.SpringEvalExpressions.HAS_AUTH_READ_TARGET) + Optional getByKey(@NotEmpty String key); + /** * @param name * as {@link TargetType#getName()} diff --git a/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/management/JpaTargetTypeManagement.java b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/management/JpaTargetTypeManagement.java index 44df24a0e3..49b6caf438 100644 --- a/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/management/JpaTargetTypeManagement.java +++ b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/management/JpaTargetTypeManagement.java @@ -89,6 +89,11 @@ public JpaTargetTypeManagement(final TargetTypeRepository targetTypeRepository, this.quotaManagement = quotaManagement; } + @Override + public Optional getByKey(final String key) { + return targetTypeRepository.findOne(TargetTypeSpecification.hasKey(key)).map(TargetType.class::cast); + } + @Override public Optional getByName(final String name) { return targetTypeRepository.findOne(TargetTypeSpecification.hasName(name)).map(TargetType.class::cast); diff --git a/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/repository/TargetTypeRepository.java b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/repository/TargetTypeRepository.java index 277e0cbe67..2b99809e55 100644 --- a/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/repository/TargetTypeRepository.java +++ b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/repository/TargetTypeRepository.java @@ -50,18 +50,7 @@ public interface TargetTypeRepository * {@link TargetType#getName()} */ default List 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 findByName(final String name) { - return this.findOne(Specification.where(TargetTypeSpecification.hasName(name))); + return findAll(Specification.where(TargetTypeSpecification.hasDsSetType(dsTypeId))); } /** diff --git a/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/specifications/TargetTypeSpecification.java b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/specifications/TargetTypeSpecification.java index ade97e6bb8..007a31f266 100644 --- a/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/specifications/TargetTypeSpecification.java +++ b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/specifications/TargetTypeSpecification.java @@ -133,6 +133,18 @@ public static Specification 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 hasKey(final String key) { + return (targetRoot, query, cb) -> cb.equal(targetRoot.get(JpaTargetType_.key), key); + } /** * {@link Specification} for retrieving {@link TargetType} with diff --git a/hawkbit-repository/hawkbit-repository-jpa/src/test/java/org/eclipse/hawkbit/repository/jpa/management/TargetTypeManagementTest.java b/hawkbit-repository/hawkbit-repository-jpa/src/test/java/org/eclipse/hawkbit/repository/jpa/management/TargetTypeManagementTest.java index b97a757be5..252cbbcd81 100644 --- a/hawkbit-repository/hawkbit-repository-jpa/src/test/java/org/eclipse/hawkbit/repository/jpa/management/TargetTypeManagementTest.java +++ b/hawkbit-repository/hawkbit-repository-jpa/src/test/java/org/eclipse/hawkbit/repository/jpa/management/TargetTypeManagementTest.java @@ -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; @@ -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( @@ -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( @@ -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 @@ -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())); @@ -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 targetTypeWithDsTypes = targetTypeRepository.findById(targetType.getId()); @@ -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 @@ -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(); @@ -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 @@ -235,4 +271,11 @@ void failedDuplicateTargetTypeNameExceptionAfterUpdate() { assertThrows(EntityAlreadyExistsException.class, () -> targetTypeManagement.update(entityFactory.targetType().update(targetType.getId()).name("targettype1234"))); } + private Optional findByName(final String name) { + return targetTypeManagement.getByName(name).map(JpaTargetType.class::cast); + } + + private Optional findByKey(final String key) { + return targetTypeManagement.getByKey(key).map(JpaTargetType.class::cast); + } }