From e7226e3933d38828874ea29404f6087911c2c9cc Mon Sep 17 00:00:00 2001 From: Stanislav Trailov Date: Thu, 29 Jun 2023 12:24:18 +0300 Subject: [PATCH 1/6] Additional target filtering by target and DS ids Signed-off-by: Stanislav Trailov --- .../DistributionSetTagManagement.java | 3 + .../repository/TargetTagManagement.java | 3 + .../jpa/JpaDistributionSetTagManagement.java | 14 ++++ .../jpa/JpaTargetTagManagement.java | 7 ++ .../MgmtDistributionSetTagResource.java | 2 +- .../rest/resource/MgmtTargetTagResource.java | 5 +- .../MgmtDistributionSetTagResourceTest.java | 62 +++++++++++++++++ .../resource/MgmtTargetTagResourceTest.java | 68 +++++++++++++++++++ hawkbit-runtime/hawkbit-update-server/pom.xml | 2 +- 9 files changed, 163 insertions(+), 3 deletions(-) diff --git a/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/DistributionSetTagManagement.java b/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/DistributionSetTagManagement.java index 4382b099b..762b0a4ef 100644 --- a/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/DistributionSetTagManagement.java +++ b/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/DistributionSetTagManagement.java @@ -70,4 +70,7 @@ public interface DistributionSetTagManagement extends RepositoryManagement findByDistributionSet(@NotNull Pageable pageable, long setId); + @PreAuthorize(SpringEvalExpressions.HAS_AUTH_READ_REPOSITORY) + Page findByRsqlWithDistributionSetTagSpec(@NotNull Pageable pageable, @NotNull String rsqlParam); + } diff --git a/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/TargetTagManagement.java b/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/TargetTagManagement.java index a4fc90e62..7cdec0787 100644 --- a/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/TargetTagManagement.java +++ b/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/TargetTagManagement.java @@ -135,6 +135,9 @@ public interface TargetTagManagement { @PreAuthorize(SpringEvalExpressions.HAS_AUTH_READ_TARGET) Page findByRsql(@NotNull Pageable pageable, @NotNull String rsqlParam); + @PreAuthorize(SpringEvalExpressions.HAS_AUTH_READ_TARGET) + Page findByRsqlWithTargetTagSpec(@NotNull Pageable pageable, @NotNull String rsqlParam); + /** * Find {@link TargetTag} based on given Name. * diff --git a/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/JpaDistributionSetTagManagement.java b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/JpaDistributionSetTagManagement.java index 9ac715853..2e0df2f13 100644 --- a/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/JpaDistributionSetTagManagement.java +++ b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/JpaDistributionSetTagManagement.java @@ -8,12 +8,15 @@ */ package org.eclipse.hawkbit.repository.jpa; +import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.List; import java.util.Optional; import java.util.stream.Collectors; +import org.eclipse.hawkbit.repository.DistributionSetFields; +import org.eclipse.hawkbit.repository.DistributionSetTagFields; import org.eclipse.hawkbit.repository.DistributionSetTagManagement; import org.eclipse.hawkbit.repository.TagFields; import org.eclipse.hawkbit.repository.TargetTagManagement; @@ -23,8 +26,10 @@ import org.eclipse.hawkbit.repository.builder.TagUpdate; import org.eclipse.hawkbit.repository.exception.EntityNotFoundException; import org.eclipse.hawkbit.repository.jpa.builder.JpaTagCreate; import org.eclipse.hawkbit.repository.jpa.configuration.Constants; +import org.eclipse.hawkbit.repository.jpa.model.JpaDistributionSet; import org.eclipse.hawkbit.repository.jpa.model.JpaDistributionSetTag; import org.eclipse.hawkbit.repository.jpa.rsql.RSQLUtility; +import org.eclipse.hawkbit.repository.jpa.specifications.DistributionSetSpecification; import org.eclipse.hawkbit.repository.jpa.specifications.TagSpecification; import org.eclipse.hawkbit.repository.model.DistributionSet; import org.eclipse.hawkbit.repository.model.DistributionSetTag; @@ -146,6 +151,15 @@ public class JpaDistributionSetTagManagement implements DistributionSetTagManage Collections.singletonList(TagSpecification.ofDistributionSet(setId))); } + @Override + public Page findByRsqlWithDistributionSetTagSpec(Pageable pageable, String rsqlParam) { + final Specification spec = RSQLUtility.buildRsqlSpecification(rsqlParam, DistributionSetTagFields.class, + virtualPropertyReplacer, database); + + return JpaManagementHelper.findAllWithCountBySpec(distributionSetTagRepository, pageable, + Collections.singletonList(spec)); + } + @Override @Transactional @Retryable(include = { diff --git a/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/JpaTargetTagManagement.java b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/JpaTargetTagManagement.java index cfa16dd19..1b21da9ca 100644 --- a/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/JpaTargetTagManagement.java +++ b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/JpaTargetTagManagement.java @@ -15,6 +15,7 @@ import java.util.Optional; import java.util.stream.Collectors; import org.eclipse.hawkbit.repository.TagFields; +import org.eclipse.hawkbit.repository.TargetTagFields; import org.eclipse.hawkbit.repository.TargetTagManagement; import org.eclipse.hawkbit.repository.builder.GenericTagUpdate; import org.eclipse.hawkbit.repository.builder.TagCreate; @@ -108,6 +109,12 @@ public class JpaTargetTagManagement implements TargetTagManagement { RSQLUtility.buildRsqlSpecification(rsqlParam, TagFields.class, virtualPropertyReplacer, database))); } + @Override + public Page findByRsqlWithTargetTagSpec(Pageable pageable, String rsqlParam) { + return JpaManagementHelper.findAllWithCountBySpec(targetTagRepository, pageable, Collections.singletonList( + RSQLUtility.buildRsqlSpecification(rsqlParam, TargetTagFields.class, virtualPropertyReplacer, database))); + } + @Override public long count() { return targetTagRepository.count(); diff --git a/hawkbit-rest/hawkbit-mgmt-resource/src/main/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtDistributionSetTagResource.java b/hawkbit-rest/hawkbit-mgmt-resource/src/main/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtDistributionSetTagResource.java index 81a058ab8..3e70d6e00 100644 --- a/hawkbit-rest/hawkbit-mgmt-resource/src/main/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtDistributionSetTagResource.java +++ b/hawkbit-rest/hawkbit-mgmt-resource/src/main/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtDistributionSetTagResource.java @@ -80,7 +80,7 @@ public class MgmtDistributionSetTagResource implements MgmtDistributionSetTagRes count = distributionSetTagManagement.count(); } else { - final Page page = distributionSetTagManagement.findByRsql(pageable, rsqlParam); + final Page page = distributionSetTagManagement.findByRsqlWithDistributionSetTagSpec(pageable, rsqlParam); distributionSetTags = page; count = page.getTotalElements(); diff --git a/hawkbit-rest/hawkbit-mgmt-resource/src/main/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtTargetTagResource.java b/hawkbit-rest/hawkbit-mgmt-resource/src/main/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtTargetTagResource.java index cdf8cd442..f8dcd3d4f 100644 --- a/hawkbit-rest/hawkbit-mgmt-resource/src/main/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtTargetTagResource.java +++ b/hawkbit-rest/hawkbit-mgmt-resource/src/main/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtTargetTagResource.java @@ -11,6 +11,8 @@ package org.eclipse.hawkbit.mgmt.rest.resource; import java.util.List; import java.util.stream.Collectors; +import cz.jirutka.rsql.parser.RSQLParser; +import cz.jirutka.rsql.parser.ast.RSQLOperators; import org.eclipse.hawkbit.mgmt.json.model.PagedList; import org.eclipse.hawkbit.mgmt.json.model.tag.MgmtAssignedTargetRequestBody; import org.eclipse.hawkbit.mgmt.json.model.tag.MgmtTag; @@ -85,7 +87,8 @@ public class MgmtTargetTagResource implements MgmtTargetTagRestApi { findTargetsAll = this.tagManagement.findAll(pageable); } else { - findTargetsAll = this.tagManagement.findByRsql(pageable, rsqlParam); + findTargetsAll = this.tagManagement.findByRsqlWithTargetTagSpec(pageable, rsqlParam); + } final List rest = MgmtTagMapper.toResponse(findTargetsAll.getContent()); diff --git a/hawkbit-rest/hawkbit-mgmt-resource/src/test/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtDistributionSetTagResourceTest.java b/hawkbit-rest/hawkbit-mgmt-resource/src/test/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtDistributionSetTagResourceTest.java index dff042c6b..f863e88f7 100644 --- a/hawkbit-rest/hawkbit-mgmt-resource/src/test/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtDistributionSetTagResourceTest.java +++ b/hawkbit-rest/hawkbit-mgmt-resource/src/test/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtDistributionSetTagResourceTest.java @@ -73,6 +73,68 @@ public class MgmtDistributionSetTagResourceTest extends AbstractManagementApiInt .andExpect(jsonPath(MgmtTargetResourceTest.JSON_PATH_PAGED_LIST_CONTENT, hasSize(2))); } + @Test + @Description("Verfies that a paged result list of DS tags reflects the content on the repository side when filtered by distribution set id.") + public void getDistributionSetTagsByDistributionSetId() throws Exception { + final List tags = testdataFactory.createDistributionSetTags(2); + final DistributionSetTag tag1 = tags.get(0); + final DistributionSetTag tag2 = tags.get(1); + + final DistributionSet distributionSet1 = testdataFactory.createDistributionSet(); + final DistributionSet distributionSet2 = testdataFactory.createDistributionSet(); + distributionSetManagement.toggleTagAssignment(List.of(distributionSet1.getId(), distributionSet2.getId()), tag1.getName()); + distributionSetManagement.toggleTagAssignment(List.of(distributionSet1.getId()), tag2.getName()); + + mvc.perform(get(MgmtRestConstants.DISTRIBUTIONSET_TAG_V1_REQUEST_MAPPING) + .queryParam(MgmtRestConstants.REQUEST_PARAMETER_SEARCH, "distributionset.id==" + distributionSet1.getId()) + .accept(MediaType.APPLICATION_JSON)) + .andDo(MockMvcResultPrinter.print()).andExpect(status().isOk()) + .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE)) + .andExpect(applyBaseEntityMatcherOnPagedResult(tag1)) + .andExpect(applyBaseEntityMatcherOnPagedResult(tag2)) + .andExpect(applySelfLinkMatcherOnPagedResult(tag1, DISTRIBUTIONSETTAGS_ROOT + tag1.getId())) + .andExpect(applySelfLinkMatcherOnPagedResult(tag2, DISTRIBUTIONSETTAGS_ROOT + tag2.getId())) + .andExpect(jsonPath(MgmtTargetResourceTest.JSON_PATH_PAGED_LIST_TOTAL, equalTo(2))) + .andExpect(jsonPath(MgmtTargetResourceTest.JSON_PATH_PAGED_LIST_SIZE, equalTo(2))) + .andExpect(jsonPath(MgmtTargetResourceTest.JSON_PATH_PAGED_LIST_CONTENT, hasSize(2))); + + mvc.perform(get(MgmtRestConstants.DISTRIBUTIONSET_TAG_V1_REQUEST_MAPPING) + .queryParam(MgmtRestConstants.REQUEST_PARAMETER_SEARCH, "distributionset.id==" + distributionSet2.getId()) + .accept(MediaType.APPLICATION_JSON)) + .andDo(MockMvcResultPrinter.print()).andExpect(status().isOk()) + .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE)) + .andExpect(applyBaseEntityMatcherOnPagedResult(tag1)) + .andExpect(applySelfLinkMatcherOnPagedResult(tag1, DISTRIBUTIONSETTAGS_ROOT + tag1.getId())) + .andExpect(jsonPath(MgmtTargetResourceTest.JSON_PATH_PAGED_LIST_TOTAL, equalTo(1))) + .andExpect(jsonPath(MgmtTargetResourceTest.JSON_PATH_PAGED_LIST_SIZE, equalTo(1))) + .andExpect(jsonPath(MgmtTargetResourceTest.JSON_PATH_PAGED_LIST_CONTENT, hasSize(1))); + } + + @Test + @Description("Verfies that a paged result list of DS tags reflects the content on the repository side when filtered by distribution set id field AND tag field.") + public void getDistributionSetTagsByDistributionSetIdAndTagDescription() throws Exception { + final List tags = testdataFactory.createDistributionSetTags(2); + final DistributionSetTag tag1 = tags.get(0); + final DistributionSetTag tag2 = tags.get(1); + + final DistributionSet distributionSet1 = testdataFactory.createDistributionSet(); + final DistributionSet distributionSet2 = testdataFactory.createDistributionSet(); + distributionSetManagement.toggleTagAssignment(List.of(distributionSet1.getId(), distributionSet2.getId()), tag1.getName()); + distributionSetManagement.toggleTagAssignment(List.of(distributionSet1.getId()), tag2.getName()); + + mvc.perform(get(MgmtRestConstants.DISTRIBUTIONSET_TAG_V1_REQUEST_MAPPING + + "?" + MgmtRestConstants.REQUEST_PARAMETER_SEARCH + + "=distributionset.id==" + distributionSet1.getId() + ";description==" + tag1.getDescription()) + .accept(MediaType.APPLICATION_JSON)) + .andDo(MockMvcResultPrinter.print()).andExpect(status().isOk()) + .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE)) + .andExpect(applyBaseEntityMatcherOnPagedResult(tag1)) + .andExpect(applySelfLinkMatcherOnPagedResult(tag1, DISTRIBUTIONSETTAGS_ROOT + tag1.getId())) + .andExpect(jsonPath(MgmtTargetResourceTest.JSON_PATH_PAGED_LIST_TOTAL, equalTo(1))) + .andExpect(jsonPath(MgmtTargetResourceTest.JSON_PATH_PAGED_LIST_SIZE, equalTo(1))) + .andExpect(jsonPath(MgmtTargetResourceTest.JSON_PATH_PAGED_LIST_CONTENT, hasSize(1))); + } + @Test @Description("Verfies that a single result of a DS tag reflects the content on the repository side.") @ExpectEvents({ @Expect(type = DistributionSetTagCreatedEvent.class, count = 2) }) diff --git a/hawkbit-rest/hawkbit-mgmt-resource/src/test/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtTargetTagResourceTest.java b/hawkbit-rest/hawkbit-mgmt-resource/src/test/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtTargetTagResourceTest.java index 6369d698d..2e6476364 100644 --- a/hawkbit-rest/hawkbit-mgmt-resource/src/test/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtTargetTagResourceTest.java +++ b/hawkbit-rest/hawkbit-mgmt-resource/src/test/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtTargetTagResourceTest.java @@ -75,6 +75,74 @@ public class MgmtTargetTagResourceTest extends AbstractManagementApiIntegrationT } + @Test + @Description("Verfies that a paged result list of target tags reflects on the content of assigned tags for specific controller/target ID") + public void getTargetTagsByTargetId() throws Exception { + final String controllerId1 = "controllerTestId1"; + final String controllerId2 = "controllerTestId2"; + testdataFactory.createTarget(controllerId1); + testdataFactory.createTarget(controllerId2); + + final List tags = testdataFactory.createTargetTags(2, ""); + final TargetTag tag1 = tags.get(0); + final TargetTag tag2 = tags.get(1); + + targetManagement.toggleTagAssignment(List.of(controllerId1, controllerId2), tag1.getName()); + targetManagement.toggleTagAssignment(List.of(controllerId2), tag2.getName()); + + mvc.perform(get(MgmtRestConstants.TARGET_TAG_V1_REQUEST_MAPPING) + .queryParam(MgmtRestConstants.REQUEST_PARAMETER_SEARCH, "target.controllerId==" + controllerId2) + .accept(MediaType.APPLICATION_JSON)) + .andDo(MockMvcResultPrinter.print()).andExpect(status().isOk()) + .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE)) + .andExpect(applyTagMatcherOnPagedResult(tag1)) + .andExpect(applyTagMatcherOnPagedResult(tag2)) + .andExpect(applySelfLinkMatcherOnPagedResult(tag1, TARGETTAGS_ROOT + tag1.getId())) + .andExpect(applySelfLinkMatcherOnPagedResult(tag2, TARGETTAGS_ROOT + tag2.getId())) + .andExpect(jsonPath(MgmtTargetResourceTest.JSON_PATH_PAGED_LIST_TOTAL, equalTo(2))) + .andExpect(jsonPath(MgmtTargetResourceTest.JSON_PATH_PAGED_LIST_SIZE, equalTo(2))) + .andExpect(jsonPath(MgmtTargetResourceTest.JSON_PATH_PAGED_LIST_CONTENT, hasSize(2))); + + mvc.perform(get(MgmtRestConstants.TARGET_TAG_V1_REQUEST_MAPPING) + .queryParam(MgmtRestConstants.REQUEST_PARAMETER_SEARCH, "target.controllerId==" + controllerId1) + .accept(MediaType.APPLICATION_JSON)) + .andDo(MockMvcResultPrinter.print()).andExpect(status().isOk()) + .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE)) + .andExpect(applyTagMatcherOnPagedResult(tag1)) + .andExpect(applySelfLinkMatcherOnPagedResult(tag1, TARGETTAGS_ROOT + tag1.getId())) + .andExpect(jsonPath(MgmtTargetResourceTest.JSON_PATH_PAGED_LIST_TOTAL, equalTo(1))) + .andExpect(jsonPath(MgmtTargetResourceTest.JSON_PATH_PAGED_LIST_SIZE, equalTo(1))) + .andExpect(jsonPath(MgmtTargetResourceTest.JSON_PATH_PAGED_LIST_CONTENT, hasSize(1))); + } + + @Test + @Description("Verifies that a page result when listing tags reflects on the content in the repository when filtered by 2 fields - one tag field and one target field") + public void getTargetTagsFilteredByColorAndTargetId() throws Exception { + final String controllerId1 = "controllerTestId1"; + final String controllerId2 = "controllerTestId2"; + testdataFactory.createTarget(controllerId1); + testdataFactory.createTarget(controllerId2); + + final List tags = testdataFactory.createTargetTags(2, ""); + final TargetTag tag1 = tags.get(0); + final TargetTag tag2 = tags.get(1); + + targetManagement.toggleTagAssignment(List.of(controllerId1, controllerId2), tag1.getName()); + targetManagement.toggleTagAssignment(List.of(controllerId2), tag2.getName()); + + // pass here q directly as a pure string because .queryParam method delimiters the parameters in q with , + // which is logical OR, we want AND here + mvc.perform(get(MgmtRestConstants.TARGET_TAG_V1_REQUEST_MAPPING + + "?" + MgmtRestConstants.REQUEST_PARAMETER_SEARCH + "=target.controllerId==" + controllerId2 + ";colour==" + tag1.getColour()) + .accept(MediaType.APPLICATION_JSON)) + .andDo(MockMvcResultPrinter.print()).andExpect(status().isOk()) + .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE)) + .andExpect(applyTagMatcherOnPagedResult(tag1)) + .andExpect(applySelfLinkMatcherOnPagedResult(tag1, TARGETTAGS_ROOT + tag1.getId())) + .andExpect(jsonPath(MgmtTargetResourceTest.JSON_PATH_PAGED_LIST_TOTAL, equalTo(1))) + .andExpect(jsonPath(MgmtTargetResourceTest.JSON_PATH_PAGED_LIST_SIZE, equalTo(1))) + .andExpect(jsonPath(MgmtTargetResourceTest.JSON_PATH_PAGED_LIST_CONTENT, hasSize(1))); + } @Test @Description("Verfies that a single result of a target tag reflects the content on the repository side.") @ExpectEvents({ @Expect(type = TargetTagCreatedEvent.class, count = 2) }) diff --git a/hawkbit-runtime/hawkbit-update-server/pom.xml b/hawkbit-runtime/hawkbit-update-server/pom.xml index b75a5dc20..531c2b53d 100644 --- a/hawkbit-runtime/hawkbit-update-server/pom.xml +++ b/hawkbit-runtime/hawkbit-update-server/pom.xml @@ -96,7 +96,7 @@ org.mariadb.jdbc mariadb-java-client - test + org.eclipse.hawkbit From 0d4f67e3451837e8ee058409fb345d3e9e0391fa Mon Sep 17 00:00:00 2001 From: Stanislav Trailov Date: Thu, 29 Jun 2023 12:29:40 +0300 Subject: [PATCH 2/6] Small refactoring + additional classes Signed-off-by: Stanislav Trailov --- .../repository/DistributionSetTagFields.java | 69 +++++++++++++++++++ .../hawkbit/repository/TargetTagFields.java | 69 +++++++++++++++++++ .../jpa/JpaDistributionSetTagManagement.java | 4 -- .../rest/resource/MgmtTargetTagResource.java | 3 - hawkbit-runtime/hawkbit-update-server/pom.xml | 2 +- 5 files changed, 139 insertions(+), 8 deletions(-) create mode 100644 hawkbit-core/src/main/java/org/eclipse/hawkbit/repository/DistributionSetTagFields.java create mode 100644 hawkbit-core/src/main/java/org/eclipse/hawkbit/repository/TargetTagFields.java diff --git a/hawkbit-core/src/main/java/org/eclipse/hawkbit/repository/DistributionSetTagFields.java b/hawkbit-core/src/main/java/org/eclipse/hawkbit/repository/DistributionSetTagFields.java new file mode 100644 index 000000000..17ae18761 --- /dev/null +++ b/hawkbit-core/src/main/java/org/eclipse/hawkbit/repository/DistributionSetTagFields.java @@ -0,0 +1,69 @@ +/** + * Copyright (c) 2023 Bosch.IO GmbH and others. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + */ +package org.eclipse.hawkbit.repository; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +/** + * Describing the fields of the Tag model which can be used in the REST API e.g. + * for sorting etc. + * Additionally here were added fields for DistributionSet in order + * filtering over distribution set fields also. + */ +public enum DistributionSetTagFields implements FieldNameProvider { + /** + * The id field. + */ + ID("id"), + + /** + * The name field. + */ + NAME("name"), + /** + * The description field. + */ + DESCRIPTION("description"), + /** + * The controllerId field. + */ + COLOUR("colour"), + + /** + * + */ + DISTRIBUTIONSET("assignedToDistributionSet", + DistributionSetFields.ID.getFieldName(), DistributionSetFields.NAME.getFieldName()); + + private final String fieldName; + + private final List subEntityAttributes; + + private DistributionSetTagFields(final String fieldName) { + this.fieldName = fieldName; + this.subEntityAttributes = Collections.emptyList(); + } + + private DistributionSetTagFields(final String fieldName, final String... subEntityAttributes) { + this.fieldName = fieldName; + this.subEntityAttributes = Arrays.asList(subEntityAttributes); + } + + @Override + public List getSubEntityAttributes() { + return Collections.unmodifiableList(subEntityAttributes); + } + + @Override + public String getFieldName() { + return fieldName; + } +} \ No newline at end of file diff --git a/hawkbit-core/src/main/java/org/eclipse/hawkbit/repository/TargetTagFields.java b/hawkbit-core/src/main/java/org/eclipse/hawkbit/repository/TargetTagFields.java new file mode 100644 index 000000000..f0043ea8d --- /dev/null +++ b/hawkbit-core/src/main/java/org/eclipse/hawkbit/repository/TargetTagFields.java @@ -0,0 +1,69 @@ +/** + * Copyright (c) 2023 Bosch.IO GmbH and others. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + */ +package org.eclipse.hawkbit.repository; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +/** + * Describing the fields of the Tag model which can be used in the REST API e.g. + * for sorting etc. + * Additionally here were added fields for Target in order + * filtering over target fields also. + */ +public enum TargetTagFields implements FieldNameProvider { + /** + * The id field. + */ + ID("id"), + + /** + * The name field. + */ + NAME("name"), + /** + * The description field. + */ + DESCRIPTION("description"), + /** + * The controllerId field. + */ + COLOUR("colour"), + + /** + * + */ + TARGET("assignedToTargets", + TargetFields.ID.getFieldName(), TargetFields.NAME.getFieldName()); + + private final String fieldName; + + private final List subEntityAttributes; + + private TargetTagFields(final String fieldName) { + this.fieldName = fieldName; + this.subEntityAttributes = Collections.emptyList(); + } + + private TargetTagFields(final String fieldName, final String... subEntityAttributes) { + this.fieldName = fieldName; + this.subEntityAttributes = Arrays.asList(subEntityAttributes); + } + + @Override + public List getSubEntityAttributes() { + return Collections.unmodifiableList(subEntityAttributes); + } + + @Override + public String getFieldName() { + return fieldName; + } +} \ No newline at end of file diff --git a/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/JpaDistributionSetTagManagement.java b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/JpaDistributionSetTagManagement.java index 2e0df2f13..bc12bcaa7 100644 --- a/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/JpaDistributionSetTagManagement.java +++ b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/JpaDistributionSetTagManagement.java @@ -8,14 +8,12 @@ */ package org.eclipse.hawkbit.repository.jpa; -import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.List; import java.util.Optional; import java.util.stream.Collectors; -import org.eclipse.hawkbit.repository.DistributionSetFields; import org.eclipse.hawkbit.repository.DistributionSetTagFields; import org.eclipse.hawkbit.repository.DistributionSetTagManagement; import org.eclipse.hawkbit.repository.TagFields; @@ -26,10 +24,8 @@ import org.eclipse.hawkbit.repository.builder.TagUpdate; import org.eclipse.hawkbit.repository.exception.EntityNotFoundException; import org.eclipse.hawkbit.repository.jpa.builder.JpaTagCreate; import org.eclipse.hawkbit.repository.jpa.configuration.Constants; -import org.eclipse.hawkbit.repository.jpa.model.JpaDistributionSet; import org.eclipse.hawkbit.repository.jpa.model.JpaDistributionSetTag; import org.eclipse.hawkbit.repository.jpa.rsql.RSQLUtility; -import org.eclipse.hawkbit.repository.jpa.specifications.DistributionSetSpecification; import org.eclipse.hawkbit.repository.jpa.specifications.TagSpecification; import org.eclipse.hawkbit.repository.model.DistributionSet; import org.eclipse.hawkbit.repository.model.DistributionSetTag; diff --git a/hawkbit-rest/hawkbit-mgmt-resource/src/main/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtTargetTagResource.java b/hawkbit-rest/hawkbit-mgmt-resource/src/main/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtTargetTagResource.java index f8dcd3d4f..005381919 100644 --- a/hawkbit-rest/hawkbit-mgmt-resource/src/main/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtTargetTagResource.java +++ b/hawkbit-rest/hawkbit-mgmt-resource/src/main/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtTargetTagResource.java @@ -11,8 +11,6 @@ package org.eclipse.hawkbit.mgmt.rest.resource; import java.util.List; import java.util.stream.Collectors; -import cz.jirutka.rsql.parser.RSQLParser; -import cz.jirutka.rsql.parser.ast.RSQLOperators; import org.eclipse.hawkbit.mgmt.json.model.PagedList; import org.eclipse.hawkbit.mgmt.json.model.tag.MgmtAssignedTargetRequestBody; import org.eclipse.hawkbit.mgmt.json.model.tag.MgmtTag; @@ -35,7 +33,6 @@ import org.eclipse.hawkbit.utils.TenantConfigHelper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.data.domain.Page; -import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; import org.springframework.http.HttpStatus; diff --git a/hawkbit-runtime/hawkbit-update-server/pom.xml b/hawkbit-runtime/hawkbit-update-server/pom.xml index 531c2b53d..b75a5dc20 100644 --- a/hawkbit-runtime/hawkbit-update-server/pom.xml +++ b/hawkbit-runtime/hawkbit-update-server/pom.xml @@ -96,7 +96,7 @@ org.mariadb.jdbc mariadb-java-client - + test org.eclipse.hawkbit From 52d671bdc560aef9ba0ee975b44b9751736c4ac3 Mon Sep 17 00:00:00 2001 From: Stanislav Trailov Date: Thu, 29 Jun 2023 12:31:25 +0300 Subject: [PATCH 3/6] Add some useful comments Signed-off-by: Stanislav Trailov --- .../eclipse/hawkbit/repository/DistributionSetTagFields.java | 2 +- .../java/org/eclipse/hawkbit/repository/TargetTagFields.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/hawkbit-core/src/main/java/org/eclipse/hawkbit/repository/DistributionSetTagFields.java b/hawkbit-core/src/main/java/org/eclipse/hawkbit/repository/DistributionSetTagFields.java index 17ae18761..24beaf6a6 100644 --- a/hawkbit-core/src/main/java/org/eclipse/hawkbit/repository/DistributionSetTagFields.java +++ b/hawkbit-core/src/main/java/org/eclipse/hawkbit/repository/DistributionSetTagFields.java @@ -38,7 +38,7 @@ public enum DistributionSetTagFields implements FieldNameProvider { COLOUR("colour"), /** - * + * Distribution set fields */ DISTRIBUTIONSET("assignedToDistributionSet", DistributionSetFields.ID.getFieldName(), DistributionSetFields.NAME.getFieldName()); diff --git a/hawkbit-core/src/main/java/org/eclipse/hawkbit/repository/TargetTagFields.java b/hawkbit-core/src/main/java/org/eclipse/hawkbit/repository/TargetTagFields.java index f0043ea8d..defc976cb 100644 --- a/hawkbit-core/src/main/java/org/eclipse/hawkbit/repository/TargetTagFields.java +++ b/hawkbit-core/src/main/java/org/eclipse/hawkbit/repository/TargetTagFields.java @@ -38,7 +38,7 @@ public enum TargetTagFields implements FieldNameProvider { COLOUR("colour"), /** - * + * Target fields */ TARGET("assignedToTargets", TargetFields.ID.getFieldName(), TargetFields.NAME.getFieldName()); From 8bbe6df72a20c88774b7258d87f11d2aaec12b3c Mon Sep 17 00:00:00 2001 From: Stanislav Trailov Date: Thu, 29 Jun 2023 12:41:49 +0300 Subject: [PATCH 4/6] Add comment in test for specific query parameter usage Signed-off-by: Stanislav Trailov --- .../mgmt/rest/resource/MgmtDistributionSetTagResourceTest.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/hawkbit-rest/hawkbit-mgmt-resource/src/test/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtDistributionSetTagResourceTest.java b/hawkbit-rest/hawkbit-mgmt-resource/src/test/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtDistributionSetTagResourceTest.java index f863e88f7..e136d606f 100644 --- a/hawkbit-rest/hawkbit-mgmt-resource/src/test/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtDistributionSetTagResourceTest.java +++ b/hawkbit-rest/hawkbit-mgmt-resource/src/test/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtDistributionSetTagResourceTest.java @@ -122,6 +122,8 @@ public class MgmtDistributionSetTagResourceTest extends AbstractManagementApiInt distributionSetManagement.toggleTagAssignment(List.of(distributionSet1.getId(), distributionSet2.getId()), tag1.getName()); distributionSetManagement.toggleTagAssignment(List.of(distributionSet1.getId()), tag2.getName()); + // pass here q directly as a pure string because .queryParam method delimiters the parameters in q with , + // which is logical OR, we want AND here mvc.perform(get(MgmtRestConstants.DISTRIBUTIONSET_TAG_V1_REQUEST_MAPPING + "?" + MgmtRestConstants.REQUEST_PARAMETER_SEARCH + "=distributionset.id==" + distributionSet1.getId() + ";description==" + tag1.getDescription()) From 407ff7a3e5581c9de3b11945817e29acc0aec4f0 Mon Sep 17 00:00:00 2001 From: Stanislav Trailov Date: Thu, 29 Jun 2023 13:09:32 +0300 Subject: [PATCH 5/6] More refactoring Signed-off-by: Stanislav Trailov --- .../repository/DistributionSetTagManagement.java | 3 --- .../hawkbit/repository/TargetTagManagement.java | 3 --- .../jpa/JpaDistributionSetTagManagement.java | 11 +---------- .../repository/jpa/JpaTargetTagManagement.java | 8 +------- .../rest/resource/MgmtDistributionSetTagResource.java | 2 +- .../mgmt/rest/resource/MgmtTargetTagResource.java | 2 +- 6 files changed, 4 insertions(+), 25 deletions(-) diff --git a/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/DistributionSetTagManagement.java b/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/DistributionSetTagManagement.java index 762b0a4ef..4382b099b 100644 --- a/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/DistributionSetTagManagement.java +++ b/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/DistributionSetTagManagement.java @@ -70,7 +70,4 @@ public interface DistributionSetTagManagement extends RepositoryManagement findByDistributionSet(@NotNull Pageable pageable, long setId); - @PreAuthorize(SpringEvalExpressions.HAS_AUTH_READ_REPOSITORY) - Page findByRsqlWithDistributionSetTagSpec(@NotNull Pageable pageable, @NotNull String rsqlParam); - } diff --git a/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/TargetTagManagement.java b/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/TargetTagManagement.java index 7cdec0787..a4fc90e62 100644 --- a/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/TargetTagManagement.java +++ b/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/TargetTagManagement.java @@ -135,9 +135,6 @@ public interface TargetTagManagement { @PreAuthorize(SpringEvalExpressions.HAS_AUTH_READ_TARGET) Page findByRsql(@NotNull Pageable pageable, @NotNull String rsqlParam); - @PreAuthorize(SpringEvalExpressions.HAS_AUTH_READ_TARGET) - Page findByRsqlWithTargetTagSpec(@NotNull Pageable pageable, @NotNull String rsqlParam); - /** * Find {@link TargetTag} based on given Name. * diff --git a/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/JpaDistributionSetTagManagement.java b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/JpaDistributionSetTagManagement.java index bc12bcaa7..4659601d0 100644 --- a/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/JpaDistributionSetTagManagement.java +++ b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/JpaDistributionSetTagManagement.java @@ -130,7 +130,7 @@ public class JpaDistributionSetTagManagement implements DistributionSetTagManage @Override public Page findByRsql(final Pageable pageable, final String rsqlParam) { - final Specification spec = RSQLUtility.buildRsqlSpecification(rsqlParam, TagFields.class, + final Specification spec = RSQLUtility.buildRsqlSpecification(rsqlParam, DistributionSetTagFields.class, virtualPropertyReplacer, database); return JpaManagementHelper.findAllWithCountBySpec(distributionSetTagRepository, pageable, @@ -147,15 +147,6 @@ public class JpaDistributionSetTagManagement implements DistributionSetTagManage Collections.singletonList(TagSpecification.ofDistributionSet(setId))); } - @Override - public Page findByRsqlWithDistributionSetTagSpec(Pageable pageable, String rsqlParam) { - final Specification spec = RSQLUtility.buildRsqlSpecification(rsqlParam, DistributionSetTagFields.class, - virtualPropertyReplacer, database); - - return JpaManagementHelper.findAllWithCountBySpec(distributionSetTagRepository, pageable, - Collections.singletonList(spec)); - } - @Override @Transactional @Retryable(include = { diff --git a/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/JpaTargetTagManagement.java b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/JpaTargetTagManagement.java index 1b21da9ca..c77418c0b 100644 --- a/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/JpaTargetTagManagement.java +++ b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/JpaTargetTagManagement.java @@ -106,13 +106,7 @@ public class JpaTargetTagManagement implements TargetTagManagement { @Override public Page findByRsql(final Pageable pageable, final String rsqlParam) { return JpaManagementHelper.findAllWithCountBySpec(targetTagRepository, pageable, Collections.singletonList( - RSQLUtility.buildRsqlSpecification(rsqlParam, TagFields.class, virtualPropertyReplacer, database))); - } - - @Override - public Page findByRsqlWithTargetTagSpec(Pageable pageable, String rsqlParam) { - return JpaManagementHelper.findAllWithCountBySpec(targetTagRepository, pageable, Collections.singletonList( - RSQLUtility.buildRsqlSpecification(rsqlParam, TargetTagFields.class, virtualPropertyReplacer, database))); + RSQLUtility.buildRsqlSpecification(rsqlParam, TargetTagFields.class, virtualPropertyReplacer, database))); } @Override diff --git a/hawkbit-rest/hawkbit-mgmt-resource/src/main/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtDistributionSetTagResource.java b/hawkbit-rest/hawkbit-mgmt-resource/src/main/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtDistributionSetTagResource.java index 3e70d6e00..81a058ab8 100644 --- a/hawkbit-rest/hawkbit-mgmt-resource/src/main/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtDistributionSetTagResource.java +++ b/hawkbit-rest/hawkbit-mgmt-resource/src/main/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtDistributionSetTagResource.java @@ -80,7 +80,7 @@ public class MgmtDistributionSetTagResource implements MgmtDistributionSetTagRes count = distributionSetTagManagement.count(); } else { - final Page page = distributionSetTagManagement.findByRsqlWithDistributionSetTagSpec(pageable, rsqlParam); + final Page page = distributionSetTagManagement.findByRsql(pageable, rsqlParam); distributionSetTags = page; count = page.getTotalElements(); diff --git a/hawkbit-rest/hawkbit-mgmt-resource/src/main/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtTargetTagResource.java b/hawkbit-rest/hawkbit-mgmt-resource/src/main/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtTargetTagResource.java index 005381919..fb639b121 100644 --- a/hawkbit-rest/hawkbit-mgmt-resource/src/main/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtTargetTagResource.java +++ b/hawkbit-rest/hawkbit-mgmt-resource/src/main/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtTargetTagResource.java @@ -84,7 +84,7 @@ public class MgmtTargetTagResource implements MgmtTargetTagRestApi { findTargetsAll = this.tagManagement.findAll(pageable); } else { - findTargetsAll = this.tagManagement.findByRsqlWithTargetTagSpec(pageable, rsqlParam); + findTargetsAll = this.tagManagement.findByRsql(pageable, rsqlParam); } From 99a0da1c2fb88c9d954d5067eec726f5df354da4 Mon Sep 17 00:00:00 2001 From: Stanislav Trailov Date: Thu, 29 Jun 2023 14:38:12 +0300 Subject: [PATCH 6/6] Apply changes from review Signed-off-by: Stanislav Trailov --- .../hawkbit/repository/DistributionSetTagFields.java | 8 ++++---- .../org/eclipse/hawkbit/repository/TargetTagFields.java | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/hawkbit-core/src/main/java/org/eclipse/hawkbit/repository/DistributionSetTagFields.java b/hawkbit-core/src/main/java/org/eclipse/hawkbit/repository/DistributionSetTagFields.java index 24beaf6a6..db8aaa5de 100644 --- a/hawkbit-core/src/main/java/org/eclipse/hawkbit/repository/DistributionSetTagFields.java +++ b/hawkbit-core/src/main/java/org/eclipse/hawkbit/repository/DistributionSetTagFields.java @@ -22,20 +22,20 @@ public enum DistributionSetTagFields implements FieldNameProvider { /** * The id field. */ - ID("id"), + ID(TagFields.ID.getFieldName()), /** * The name field. */ - NAME("name"), + NAME(TagFields.NAME.getFieldName()), /** * The description field. */ - DESCRIPTION("description"), + DESCRIPTION(TagFields.DESCRIPTION.getFieldName()), /** * The controllerId field. */ - COLOUR("colour"), + COLOUR(TagFields.COLOUR.getFieldName()), /** * Distribution set fields diff --git a/hawkbit-core/src/main/java/org/eclipse/hawkbit/repository/TargetTagFields.java b/hawkbit-core/src/main/java/org/eclipse/hawkbit/repository/TargetTagFields.java index defc976cb..f042b4bb1 100644 --- a/hawkbit-core/src/main/java/org/eclipse/hawkbit/repository/TargetTagFields.java +++ b/hawkbit-core/src/main/java/org/eclipse/hawkbit/repository/TargetTagFields.java @@ -22,20 +22,20 @@ public enum TargetTagFields implements FieldNameProvider { /** * The id field. */ - ID("id"), + ID(TagFields.ID.getFieldName()), /** * The name field. */ - NAME("name"), + NAME(TagFields.NAME.getFieldName()), /** * The description field. */ - DESCRIPTION("description"), + DESCRIPTION(TagFields.DESCRIPTION.getFieldName()), /** * The controllerId field. */ - COLOUR("colour"), + COLOUR(TagFields.COLOUR.getFieldName()), /** * Target fields