JpaSoftwareModule#metadata made map (#2412)

Signed-off-by: Avgustin Marinov <Avgustin.Marinov@bosch.com>
This commit is contained in:
Avgustin Marinov
2025-05-21 16:59:25 +03:00
committed by GitHub
parent 452d8618d7
commit 64ee8b9906
37 changed files with 413 additions and 895 deletions

View File

@@ -33,7 +33,6 @@ import org.eclipse.hawkbit.repository.EntityFactory;
import org.eclipse.hawkbit.repository.builder.DistributionSetCreate;
import org.eclipse.hawkbit.repository.model.DistributionSet;
import org.eclipse.hawkbit.repository.model.DistributionSetAssignmentResult;
import org.eclipse.hawkbit.repository.model.MetaData;
import org.eclipse.hawkbit.rest.json.model.ResponseList;
/**

View File

@@ -117,15 +117,10 @@ public final class MgmtSoftwareModuleMapper {
static void addLinks(final SoftwareModule softwareModule, final MgmtSoftwareModule response) {
response.add(linkTo(methodOn(MgmtSoftwareModuleRestApi.class).getArtifacts(response.getId(), null, null))
.withRel(MgmtRestConstants.SOFTWAREMODULE_V1_ARTIFACT).expand());
response.add(linkTo(
methodOn(MgmtSoftwareModuleTypeRestApi.class).getSoftwareModuleType(softwareModule.getType().getId()))
response.add(linkTo(methodOn(MgmtSoftwareModuleTypeRestApi.class).getSoftwareModuleType(softwareModule.getType().getId()))
.withRel(MgmtRestConstants.SOFTWAREMODULE_V1_TYPE).expand());
response.add(linkTo(methodOn(MgmtSoftwareModuleResource.class).getMetadata(response.getId(),
MgmtRestConstants.REQUEST_PARAMETER_PAGING_DEFAULT_OFFSET_VALUE,
MgmtRestConstants.REQUEST_PARAMETER_PAGING_DEFAULT_LIMIT_VALUE, null, null)).withRel("metadata")
.expand().expand());
response.add(linkTo(methodOn(MgmtSoftwareModuleResource.class).getMetadata(response.getId()))
.withRel("metadata").expand());
}
static MgmtArtifact toResponse(final Artifact artifact) {

View File

@@ -14,6 +14,7 @@ import java.io.InputStream;
import java.text.MessageFormat;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import jakarta.validation.ValidationException;
@@ -234,62 +235,42 @@ public class MgmtSoftwareModuleResource implements MgmtSoftwareModuleRestApi {
}
@Override
public ResponseEntity<PagedList<MgmtSoftwareModuleMetadata>> getMetadata(
final Long softwareModuleId,
final int pagingOffsetParam, final int pagingLimitParam, final String sortParam, final String rsqlParam) {
public void createMetadata(final Long softwareModuleId, final List<MgmtSoftwareModuleMetadata> metadataRest) {
softwareModuleManagement.createMetadata(MgmtSoftwareModuleMapper.fromRequestSwMetadata(entityFactory, softwareModuleId, metadataRest));
}
@Override
public ResponseEntity<PagedList<MgmtSoftwareModuleMetadata>> getMetadata(final Long softwareModuleId) {
// check if software module exists otherwise throw exception immediately
findSoftwareModuleWithExceptionIfNotFound(softwareModuleId, null);
final int sanitizedOffsetParam = PagingUtility.sanitizeOffsetParam(pagingOffsetParam);
final int sanitizedLimitParam = PagingUtility.sanitizePageLimitParam(pagingLimitParam);
final Sort sorting = PagingUtility.sanitizeSoftwareModuleMetadataSortParam(sortParam);
final Pageable pageable = new OffsetBasedPageRequest(sanitizedOffsetParam, sanitizedLimitParam, sorting);
final Page<SoftwareModuleMetadata> metaDataPage;
if (rsqlParam != null) {
metaDataPage = softwareModuleManagement.findMetaDataByRsql(softwareModuleId, rsqlParam, pageable);
} else {
metaDataPage = softwareModuleManagement.findMetaDataBySoftwareModuleId(pageable, softwareModuleId);
}
return ResponseEntity
.ok(new PagedList<>(MgmtSoftwareModuleMapper.toResponseSwMetadata(metaDataPage.getContent()), metaDataPage.getTotalElements()));
final List<SoftwareModuleMetadata> metadata = softwareModuleManagement.getMetadata(softwareModuleId);
return ResponseEntity.ok(new PagedList<>(MgmtSoftwareModuleMapper.toResponseSwMetadata(metadata), metadata.size()));
}
@Override
public ResponseEntity<MgmtSoftwareModuleMetadata> getMetadataValue(final Long softwareModuleId, final String metadataKey) {
final SoftwareModuleMetadata findOne = softwareModuleManagement
.findMetaDataBySoftwareModuleId(softwareModuleId, metadataKey).orElseThrow(
() -> new EntityNotFoundException(SoftwareModuleMetadata.class, softwareModuleId, metadataKey));
final SoftwareModuleMetadata findOne = softwareModuleManagement.getMetadata(softwareModuleId).stream()
.filter(entry -> entry.getKey().equals(metadataKey))
.findFirst()
.orElseThrow(() -> new EntityNotFoundException("SoftwareModule metadata", softwareModuleId + ":" + metadataKey));
return ResponseEntity.ok(MgmtSoftwareModuleMapper.toResponseSwMetadata(findOne));
}
@Override
public ResponseEntity<MgmtSoftwareModuleMetadata> updateMetadata(
final Long softwareModuleId, final String metadataKey, final MgmtSoftwareModuleMetadataBodyPut metadata) {
final SoftwareModuleMetadata updated = softwareModuleManagement
.updateMetaData(entityFactory.softwareModuleMetadata().update(softwareModuleId, metadataKey)
.value(metadata.getValue()).targetVisible(metadata.getTargetVisible()));
return ResponseEntity.ok(MgmtSoftwareModuleMapper.toResponseSwMetadata(updated));
public void updateMetadata(final Long softwareModuleId, final String metadataKey, final MgmtSoftwareModuleMetadataBodyPut metadata) {
softwareModuleManagement.updateMetadata(
entityFactory.softwareModuleMetadata()
.update(softwareModuleId, metadataKey)
.value(metadata.getValue())
.targetVisible(metadata.getTargetVisible()));
}
@Override
@AuditLog(entity = "SoftwareModule", type = AuditLog.Type.DELETE, description = "Delete Software Module Metadata")
public ResponseEntity<Void> deleteMetadata(final Long softwareModuleId, final String metadataKey) {
softwareModuleManagement.deleteMetaData(softwareModuleId, metadataKey);
return ResponseEntity.ok().build();
}
@Override
public ResponseEntity<List<MgmtSoftwareModuleMetadata>> createMetadata(
final Long softwareModuleId, final List<MgmtSoftwareModuleMetadata> metadataRest) {
final List<SoftwareModuleMetadata> created = softwareModuleManagement.putMetaData(
MgmtSoftwareModuleMapper.fromRequestSwMetadata(entityFactory, softwareModuleId, metadataRest));
return ResponseEntity.status(HttpStatus.CREATED).body(MgmtSoftwareModuleMapper.toResponseSwMetadata(created));
public void deleteMetadata(final Long softwareModuleId, final String metadataKey) {
softwareModuleManagement.deleteMetadata(softwareModuleId, metadataKey);
}
private static MgmtRepresentationMode parseRepresentationMode(final String representationModeParam) {

View File

@@ -19,7 +19,6 @@ import org.eclipse.hawkbit.repository.DistributionSetTypeFields;
import org.eclipse.hawkbit.repository.RolloutFields;
import org.eclipse.hawkbit.repository.RolloutGroupFields;
import org.eclipse.hawkbit.repository.SoftwareModuleFields;
import org.eclipse.hawkbit.repository.SoftwareModuleMetadataFields;
import org.eclipse.hawkbit.repository.SoftwareModuleTypeFields;
import org.eclipse.hawkbit.repository.TagFields;
import org.eclipse.hawkbit.repository.TargetFields;
@@ -132,22 +131,6 @@ public final class PagingUtility {
return Sort.by(SortUtility.parse(ActionStatusFields.class, sortParam));
}
public static Sort sanitizeMetadataSortParam(final String sortParam) {
if (sortParam == null) {
// default
return Sort.by(Direction.ASC, SoftwareModuleMetadataFields.KEY.getJpaEntityFieldName());
}
return Sort.by(SortUtility.parse(SoftwareModuleMetadataFields.class, sortParam));
}
public static Sort sanitizeSoftwareModuleMetadataSortParam(final String sortParam) {
if (sortParam == null) {
// default
return Sort.by(Direction.ASC, SoftwareModuleMetadataFields.KEY.getJpaEntityFieldName());
}
return Sort.by(SortUtility.parse(SoftwareModuleMetadataFields.class, sortParam));
}
public static Sort sanitizeRolloutSortParam(final String sortParam) {
if (sortParam == null) {
// default

View File

@@ -10,6 +10,7 @@
package org.eclipse.hawkbit.mgmt.rest.resource;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.Matchers.contains;
@@ -74,7 +75,6 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.data.domain.PageRequest;
import org.springframework.hateoas.MediaTypes;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockMultipartFile;
@@ -162,7 +162,7 @@ class MgmtSoftwareModuleResourceTest extends AbstractManagementApiIntegrationTes
final SoftwareModule module = testdataFactory.createDistributionSet("one").findFirstModuleByType(osType).get();
for (int index = 0; index < totalMetadata; index++) {
softwareModuleManagement.updateMetaData(entityFactory.softwareModuleMetadata().create(module.getId())
softwareModuleManagement.updateMetadata(entityFactory.softwareModuleMetadata().create(module.getId())
.key(knownKeyPrefix + index).value(knownValuePrefix + index));
}
@@ -182,7 +182,7 @@ class MgmtSoftwareModuleResourceTest extends AbstractManagementApiIntegrationTes
final SoftwareModule module = testdataFactory.createDistributionSet("one").findFirstModuleByType(osType).get();
for (int index = 0; index < totalMetadata; index++) {
softwareModuleManagement.updateMetaData(entityFactory.softwareModuleMetadata().create(module.getId())
softwareModuleManagement.updateMetadata(entityFactory.softwareModuleMetadata().create(module.getId())
.key(knownKeyPrefix + index).value(knownValuePrefix + index));
}
@@ -202,7 +202,7 @@ class MgmtSoftwareModuleResourceTest extends AbstractManagementApiIntegrationTes
final String knownKey = "knownKey";
final String knownValue = "knownValue";
final SoftwareModule module = testdataFactory.createDistributionSet("one").findFirstModuleByType(osType).get();
softwareModuleManagement.updateMetaData(
softwareModuleManagement.updateMetadata(
entityFactory.softwareModuleMetadata().create(module.getId()).key(knownKey).value(knownValue));
mvc.perform(get(MgmtRestConstants.SOFTWAREMODULE_V1_REQUEST_MAPPING + "/{softwareModuleId}/metadata/{metadataKey}",
@@ -1202,8 +1202,7 @@ class MgmtSoftwareModuleResourceTest extends AbstractManagementApiIntegrationTes
.andExpect(jsonPath("$.createdBy", equalTo("uploadTester")))
.andExpect(jsonPath("$.createdAt", equalTo(os.getCreatedAt())))
.andExpect(jsonPath("$._links.metadata.href",
equalTo("http://localhost/rest/v1/softwaremodules/" + os.getId()
+ "/metadata?offset=0&limit=50")))
equalTo("http://localhost/rest/v1/softwaremodules/" + os.getId() + "/metadata")))
.andExpect(jsonPath("$._links.type.href",
equalTo("http://localhost/rest/v1/softwaremoduletypes/" + osType.getId())))
.andExpect(jsonPath("$._links.artifacts.href",
@@ -1346,7 +1345,6 @@ class MgmtSoftwareModuleResourceTest extends AbstractManagementApiIntegrationTes
@Test
@Description("Verifies the successful creation of metadata and the enforcement of the meta data quota.")
void createMetadata() throws Exception {
final String knownKey1 = "knownKey1";
final String knownValue1 = "knownValue1";
final String knownKey2 = "knownKey2";
@@ -1361,19 +1359,12 @@ class MgmtSoftwareModuleResourceTest extends AbstractManagementApiIntegrationTes
mvc.perform(post("/rest/v1/softwaremodules/{swId}/metadata", sm.getId()).accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON).content(metaData1.toString()))
.andDo(MockMvcResultPrinter.print())
.andExpect(status().isCreated())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(jsonPath("[0].key", equalTo(knownKey1)))
.andExpect(jsonPath("[0].value", equalTo(knownValue1)))
.andExpect(jsonPath("[0].targetVisible", equalTo(false)))
.andExpect(jsonPath("[1].key", equalTo(knownKey2)))
.andExpect(jsonPath("[1].value", equalTo(knownValue2)))
.andExpect(jsonPath("[1].targetVisible", equalTo(true)));
.andExpect(status().isCreated());
assertThat(softwareModuleManagement.findMetaDataBySoftwareModuleId(sm.getId(), knownKey1))
.as("Metadata key is wrong").get().extracting(SoftwareModuleMetadata::getValue).isEqualTo(knownValue1);
assertThat(softwareModuleManagement.findMetaDataBySoftwareModuleId(sm.getId(), knownKey2))
.as("Metadata key is wrong").get().extracting(SoftwareModuleMetadata::getValue).isEqualTo(knownValue2);
assertThat(softwareModuleManagement.getMetadata(sm.getId(), knownKey1))
.as("Metadata key is wrong").extracting(SoftwareModuleMetadata::getValue).isEqualTo(knownValue1);
assertThat(softwareModuleManagement.getMetadata(sm.getId(), knownKey2))
.as("Metadata key is wrong").extracting(SoftwareModuleMetadata::getValue).isEqualTo(knownValue2);
// verify quota enforcement
final int maxMetaData = quotaManagement.getMaxMetaDataEntriesPerSoftwareModule();
@@ -1388,11 +1379,8 @@ class MgmtSoftwareModuleResourceTest extends AbstractManagementApiIntegrationTes
.andDo(MockMvcResultPrinter.print())
.andExpect(status().isForbidden());
// verify that the number of meta data entries has not changed
// (we cannot use the PAGE constant here as it tries to sort by ID)
assertThat(softwareModuleManagement
.findMetaDataBySoftwareModuleId(PageRequest.of(0, Integer.MAX_VALUE), sm.getId()).getTotalElements())
.isEqualTo(metaData1.length());
// verify that the number of meta data entries has not changed (we cannot use the PAGE constant here as it tries to sort by ID)
assertThat(softwareModuleManagement.getMetadata(sm.getId()).size()).isEqualTo(metaData1.length());
}
@@ -1405,7 +1393,7 @@ class MgmtSoftwareModuleResourceTest extends AbstractManagementApiIntegrationTes
final String updateValue = "valueForUpdate";
final SoftwareModule sm = testdataFactory.createSoftwareModuleOs();
softwareModuleManagement.updateMetaData(
softwareModuleManagement.updateMetadata(
entityFactory.softwareModuleMetadata().create(sm.getId()).key(knownKey).value(knownValue));
final JSONObject jsonObject = new JSONObject().put("key", knownKey)
@@ -1415,13 +1403,9 @@ class MgmtSoftwareModuleResourceTest extends AbstractManagementApiIntegrationTes
mvc.perform(put("/rest/v1/softwaremodules/{swId}/metadata/{key}", sm.getId(), knownKey).accept(
MediaType.APPLICATION_JSON).contentType(MediaType.APPLICATION_JSON).content(jsonObject.toString()))
.andDo(MockMvcResultPrinter.print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(jsonPath("key", equalTo(knownKey)))
.andExpect(jsonPath("value", equalTo(updateValue)));
.andExpect(status().isOk());
final SoftwareModuleMetadata assertDS = softwareModuleManagement.findMetaDataBySoftwareModuleId(sm.getId(),
knownKey).get();
final SoftwareModuleMetadata assertDS = softwareModuleManagement.getMetadata(sm.getId(), knownKey);
assertThat(assertDS.getValue()).as("Metadata is wrong").isEqualTo(updateValue);
assertThat(assertDS.isTargetVisible()).as("target visible is wrong").isTrue();
}
@@ -1433,15 +1417,16 @@ class MgmtSoftwareModuleResourceTest extends AbstractManagementApiIntegrationTes
final String knownKey = "knownKey";
final String knownValue = "knownValue";
final SoftwareModule sm = testdataFactory.createSoftwareModuleOs();
softwareModuleManagement.updateMetaData(
entityFactory.softwareModuleMetadata().create(sm.getId()).key(knownKey).value(knownValue));
final long smId = testdataFactory.createSoftwareModuleOs().getId();
softwareModuleManagement.updateMetadata(
entityFactory.softwareModuleMetadata().create(smId).key(knownKey).value(knownValue));
mvc.perform(delete("/rest/v1/softwaremodules/{swId}/metadata/{key}", sm.getId(), knownKey))
mvc.perform(delete("/rest/v1/softwaremodules/{swId}/metadata/{key}", smId, knownKey))
.andDo(MockMvcResultPrinter.print())
.andExpect(status().isOk());
assertThat(softwareModuleManagement.findMetaDataBySoftwareModuleId(sm.getId(), knownKey)).isNotPresent();
assertThatExceptionOfType(EntityNotFoundException.class)
.isThrownBy(() -> softwareModuleManagement.getMetadata(smId, knownKey));
}
@Test
@@ -1451,11 +1436,11 @@ class MgmtSoftwareModuleResourceTest extends AbstractManagementApiIntegrationTes
final String knownKey = "knownKey";
final String knownValue = "knownValue";
final SoftwareModule sm = testdataFactory.createSoftwareModuleOs();
softwareModuleManagement.updateMetaData(
entityFactory.softwareModuleMetadata().create(sm.getId()).key(knownKey).value(knownValue));
final long smId = testdataFactory.createSoftwareModuleOs().getId();
softwareModuleManagement.updateMetadata(
entityFactory.softwareModuleMetadata().create(smId).key(knownKey).value(knownValue));
mvc.perform(delete("/rest/v1/softwaremodules/{swId}/metadata/XXX", sm.getId(), knownKey))
mvc.perform(delete("/rest/v1/softwaremodules/{swId}/metadata/XXX", smId, knownKey))
.andDo(MockMvcResultPrinter.print())
.andExpect(status().isNotFound());
@@ -1463,7 +1448,7 @@ class MgmtSoftwareModuleResourceTest extends AbstractManagementApiIntegrationTes
.andDo(MockMvcResultPrinter.print())
.andExpect(status().isNotFound());
assertThat(softwareModuleManagement.findMetaDataBySoftwareModuleId(sm.getId(), knownKey)).isPresent();
assertThat(softwareModuleManagement.getMetadata(smId, knownKey)).isNotNull();
}
@Test
@@ -1474,32 +1459,6 @@ class MgmtSoftwareModuleResourceTest extends AbstractManagementApiIntegrationTes
.andExpect(status().isNotFound());
}
@Test
@Description("Verifies the successful search of a metadata entry based on value.")
void searchSoftwareModuleMetadataRsql() throws Exception {
final int totalMetadata = 10;
final String knownKeyPrefix = "knownKey";
final String knownValuePrefix = "knownValue";
final SoftwareModule sm = testdataFactory.createSoftwareModuleOs();
for (int index = 0; index < totalMetadata; index++) {
softwareModuleManagement.updateMetaData(entityFactory.softwareModuleMetadata()
.create(sm.getId())
.key(knownKeyPrefix + index)
.value(knownValuePrefix + index));
}
final String rsqlSearchValue1 = "value==knownValue1";
mvc.perform(get("/rest/v1/softwaremodules/{swId}/metadata?q=" + rsqlSearchValue1, sm.getId()))
.andDo(MockMvcResultPrinter.print())
.andExpect(status().isOk())
.andExpect(jsonPath("size", equalTo(1)))
.andExpect(jsonPath("total", equalTo(1)))
.andExpect(jsonPath("content[0].key", equalTo("knownKey1")))
.andExpect(jsonPath("content[0].value", equalTo("knownValue1")));
}
private void assertArtifact(final SoftwareModule sm, final byte[] random) throws IOException {
// check result in db...
// repo