Soft Module metadata as complex map value (#2568)
--------- Signed-off-by: Avgustin Marinov <Avgustin.Marinov@bosch.com>
This commit is contained in:
@@ -16,6 +16,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;
|
||||
@@ -35,7 +36,6 @@ import org.eclipse.hawkbit.mgmt.rest.api.MgmtSoftwareModuleRestApi;
|
||||
import org.eclipse.hawkbit.mgmt.rest.resource.mapper.MgmtSoftwareModuleMapper;
|
||||
import org.eclipse.hawkbit.mgmt.rest.resource.util.PagingUtility;
|
||||
import org.eclipse.hawkbit.repository.ArtifactManagement;
|
||||
import org.eclipse.hawkbit.repository.EntityFactory;
|
||||
import org.eclipse.hawkbit.repository.SoftwareModuleManagement;
|
||||
import org.eclipse.hawkbit.repository.SoftwareModuleTypeManagement;
|
||||
import org.eclipse.hawkbit.repository.SystemManagement;
|
||||
@@ -43,7 +43,8 @@ import org.eclipse.hawkbit.repository.exception.EntityNotFoundException;
|
||||
import org.eclipse.hawkbit.repository.model.Artifact;
|
||||
import org.eclipse.hawkbit.repository.model.ArtifactUpload;
|
||||
import org.eclipse.hawkbit.repository.model.SoftwareModule;
|
||||
import org.eclipse.hawkbit.repository.model.SoftwareModuleMetadata;
|
||||
import org.eclipse.hawkbit.repository.model.SoftwareModule.MetadataValue;
|
||||
import org.eclipse.hawkbit.repository.model.SoftwareModule.MetadataValueCreate;
|
||||
import org.eclipse.hawkbit.repository.model.SoftwareModuleType;
|
||||
import org.eclipse.hawkbit.rest.json.model.ResponseList;
|
||||
import org.springframework.data.domain.Page;
|
||||
@@ -67,7 +68,6 @@ public class MgmtSoftwareModuleResource implements MgmtSoftwareModuleRestApi {
|
||||
private final ArtifactUrlHandler artifactUrlHandler;
|
||||
private final MgmtSoftwareModuleMapper mgmtSoftwareModuleMapper;
|
||||
private final SystemManagement systemManagement;
|
||||
private final EntityFactory entityFactory;
|
||||
|
||||
MgmtSoftwareModuleResource(
|
||||
final ArtifactManagement artifactManagement,
|
||||
@@ -75,14 +75,13 @@ public class MgmtSoftwareModuleResource implements MgmtSoftwareModuleRestApi {
|
||||
final SoftwareModuleTypeManagement<? extends SoftwareModuleType> softwareModuleTypeManagement,
|
||||
final ArtifactUrlHandler artifactUrlHandler,
|
||||
final MgmtSoftwareModuleMapper mgmtSoftwareModuleMapper,
|
||||
final SystemManagement systemManagement, final EntityFactory entityFactory) {
|
||||
final SystemManagement systemManagement) {
|
||||
this.artifactManagement = artifactManagement;
|
||||
this.softwareModuleManagement = softwareModuleManagement;
|
||||
this.softwareModuleTypeManagement = softwareModuleTypeManagement;
|
||||
this.artifactUrlHandler = artifactUrlHandler;
|
||||
this.mgmtSoftwareModuleMapper = mgmtSoftwareModuleMapper;
|
||||
this.systemManagement = systemManagement;
|
||||
this.entityFactory = entityFactory;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -138,12 +137,12 @@ public class MgmtSoftwareModuleResource implements MgmtSoftwareModuleRestApi {
|
||||
public ResponseEntity<MgmtArtifact> getArtifact(final Long softwareModuleId, final Long artifactId, final Boolean useArtifactUrlHandler) {
|
||||
final SoftwareModule module = findSoftwareModuleWithExceptionIfNotFound(softwareModuleId, artifactId);
|
||||
|
||||
final MgmtArtifact response = MgmtSoftwareModuleMapper.toResponse(module.getArtifact(artifactId).get());
|
||||
final MgmtArtifact response = MgmtSoftwareModuleMapper.toResponse(module.getArtifact(artifactId).orElseThrow());
|
||||
if (!module.isDeleted()) {
|
||||
if (Boolean.TRUE.equals(useArtifactUrlHandler)) {
|
||||
MgmtSoftwareModuleMapper.addLinks(module.getArtifact(artifactId).get(), response, artifactUrlHandler, systemManagement);
|
||||
MgmtSoftwareModuleMapper.addLinks(module.getArtifact(artifactId).orElseThrow(), response, artifactUrlHandler, systemManagement);
|
||||
} else {
|
||||
MgmtSoftwareModuleMapper.addLinks(module.getArtifact(artifactId).get(), response);
|
||||
MgmtSoftwareModuleMapper.addLinks(module.getArtifact(artifactId).orElseThrow(), response);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -236,7 +235,7 @@ public class MgmtSoftwareModuleResource implements MgmtSoftwareModuleRestApi {
|
||||
|
||||
@Override
|
||||
public void createMetadata(final Long softwareModuleId, final List<MgmtSoftwareModuleMetadata> metadataRest) {
|
||||
softwareModuleManagement.createMetadata(MgmtSoftwareModuleMapper.fromRequestSwMetadata(entityFactory, softwareModuleId, metadataRest));
|
||||
softwareModuleManagement.createMetadata(softwareModuleId, MgmtSoftwareModuleMapper.fromRequestSwMetadata(metadataRest));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -244,27 +243,24 @@ public class MgmtSoftwareModuleResource implements MgmtSoftwareModuleRestApi {
|
||||
// check if software module exists otherwise throw exception immediately
|
||||
findSoftwareModuleWithExceptionIfNotFound(softwareModuleId, null);
|
||||
|
||||
final List<SoftwareModuleMetadata> metadata = softwareModuleManagement.getMetadata(softwareModuleId);
|
||||
final Map<String, MetadataValue> 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.getMetadata(softwareModuleId).stream()
|
||||
.filter(entry -> entry.getKey().equals(metadataKey))
|
||||
.findFirst()
|
||||
.orElseThrow(() -> new EntityNotFoundException("SoftwareModule metadata", softwareModuleId + ":" + metadataKey));
|
||||
final MetadataValue metadataValue = softwareModuleManagement.getMetadata(softwareModuleId).get(metadataKey);
|
||||
if (metadataValue == null) {
|
||||
throw new EntityNotFoundException("SoftwareModule metadata", softwareModuleId + ":" + metadataKey);
|
||||
}
|
||||
|
||||
return ResponseEntity.ok(MgmtSoftwareModuleMapper.toResponseSwMetadata(findOne));
|
||||
return ResponseEntity.ok(MgmtSoftwareModuleMapper.toResponseSwMetadata(metadataKey, metadataValue));
|
||||
}
|
||||
|
||||
@Override
|
||||
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()));
|
||||
softwareModuleManagement.createMetadata(
|
||||
softwareModuleId, metadataKey, new MetadataValueCreate(metadata.getValue(), metadata.getTargetVisible()));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -15,6 +15,8 @@ import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.methodOn;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import jakarta.validation.ValidationException;
|
||||
|
||||
@@ -28,7 +30,6 @@ import org.eclipse.hawkbit.mgmt.rest.api.MgmtSoftwareModuleRestApi;
|
||||
import org.eclipse.hawkbit.mgmt.rest.api.MgmtSoftwareModuleTypeRestApi;
|
||||
import org.eclipse.hawkbit.mgmt.rest.resource.MgmtDownloadArtifactResource;
|
||||
import org.eclipse.hawkbit.mgmt.rest.resource.MgmtSoftwareModuleResource;
|
||||
import org.eclipse.hawkbit.repository.EntityFactory;
|
||||
import org.eclipse.hawkbit.repository.SoftwareModuleManagement;
|
||||
import org.eclipse.hawkbit.repository.SoftwareModuleTypeManagement;
|
||||
import org.eclipse.hawkbit.repository.SystemManagement;
|
||||
@@ -36,11 +37,11 @@ import org.eclipse.hawkbit.repository.artifact.urlhandler.ApiType;
|
||||
import org.eclipse.hawkbit.repository.artifact.urlhandler.ArtifactUrl;
|
||||
import org.eclipse.hawkbit.repository.artifact.urlhandler.ArtifactUrlHandler;
|
||||
import org.eclipse.hawkbit.repository.artifact.urlhandler.URLPlaceholder;
|
||||
import org.eclipse.hawkbit.repository.builder.SoftwareModuleMetadataCreate;
|
||||
import org.eclipse.hawkbit.repository.exception.EntityNotFoundException;
|
||||
import org.eclipse.hawkbit.repository.model.Artifact;
|
||||
import org.eclipse.hawkbit.repository.model.SoftwareModule;
|
||||
import org.eclipse.hawkbit.repository.model.SoftwareModuleMetadata;
|
||||
import org.eclipse.hawkbit.repository.model.SoftwareModule.MetadataValue;
|
||||
import org.eclipse.hawkbit.repository.model.SoftwareModule.MetadataValueCreate;
|
||||
import org.eclipse.hawkbit.repository.model.SoftwareModuleType;
|
||||
import org.eclipse.hawkbit.rest.json.model.ResponseList;
|
||||
import org.springframework.hateoas.Link;
|
||||
@@ -59,17 +60,14 @@ public final class MgmtSoftwareModuleMapper {
|
||||
this.softwareModuleTypeManagement = softwareModuleTypeManagement;
|
||||
}
|
||||
|
||||
public static List<SoftwareModuleMetadataCreate> fromRequestSwMetadata(
|
||||
final EntityFactory entityFactory, final Long softwareModuleId, final Collection<MgmtSoftwareModuleMetadata> metadata) {
|
||||
public static Map<String, MetadataValueCreate> fromRequestSwMetadata(final Collection<MgmtSoftwareModuleMetadata> metadata) {
|
||||
if (metadata == null) {
|
||||
return Collections.emptyList();
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
|
||||
return metadata.stream()
|
||||
.map(metadataRest -> entityFactory.softwareModuleMetadata().create(softwareModuleId)
|
||||
.key(metadataRest.getKey()).value(metadataRest.getValue())
|
||||
.targetVisible(metadataRest.isTargetVisible()))
|
||||
.toList();
|
||||
return metadata.stream().collect(Collectors.toMap(
|
||||
MgmtSoftwareModuleMetadata::getKey,
|
||||
metadataRest -> new MetadataValueCreate(metadataRest.getValue(), metadataRest.isTargetVisible())));
|
||||
}
|
||||
|
||||
public List<SoftwareModuleManagement.Create> smFromRequest(final Collection<MgmtSoftwareModuleRequestBodyPost> smsRest) {
|
||||
@@ -88,17 +86,17 @@ public final class MgmtSoftwareModuleMapper {
|
||||
return new ResponseList<>(softwareModules.stream().map(MgmtSoftwareModuleMapper::toResponse).toList());
|
||||
}
|
||||
|
||||
public static List<MgmtSoftwareModuleMetadata> toResponseSwMetadata(final Collection<SoftwareModuleMetadata> metadata) {
|
||||
public static List<MgmtSoftwareModuleMetadata> toResponseSwMetadata(final Map<String, ? extends MetadataValue> metadata) {
|
||||
if (metadata == null) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
return metadata.stream().map(MgmtSoftwareModuleMapper::toResponseSwMetadata).toList();
|
||||
return metadata.entrySet().stream().map(e -> toResponseSwMetadata(e.getKey(), e.getValue())).toList();
|
||||
}
|
||||
|
||||
public static MgmtSoftwareModuleMetadata toResponseSwMetadata(final SoftwareModuleMetadata metadata) {
|
||||
public static MgmtSoftwareModuleMetadata toResponseSwMetadata(final String key, final MetadataValue metadata) {
|
||||
final MgmtSoftwareModuleMetadata metadataRest = new MgmtSoftwareModuleMetadata();
|
||||
metadataRest.setKey(metadata.getKey());
|
||||
metadataRest.setKey(key);
|
||||
metadataRest.setValue(metadata.getValue());
|
||||
metadataRest.setTargetVisible(metadata.isTargetVisible());
|
||||
return metadataRest;
|
||||
|
||||
@@ -55,7 +55,8 @@ import org.eclipse.hawkbit.repository.model.ArtifactUpload;
|
||||
import org.eclipse.hawkbit.repository.model.DistributionSet;
|
||||
import org.eclipse.hawkbit.repository.model.DistributionSetType;
|
||||
import org.eclipse.hawkbit.repository.model.SoftwareModule;
|
||||
import org.eclipse.hawkbit.repository.model.SoftwareModuleMetadata;
|
||||
import org.eclipse.hawkbit.repository.model.SoftwareModule.MetadataValue;
|
||||
import org.eclipse.hawkbit.repository.model.SoftwareModule.MetadataValueCreate;
|
||||
import org.eclipse.hawkbit.repository.model.SoftwareModuleType;
|
||||
import org.eclipse.hawkbit.repository.model.Target;
|
||||
import org.eclipse.hawkbit.repository.test.util.HashGeneratorUtils;
|
||||
@@ -162,8 +163,7 @@ class MgmtSoftwareModuleResourceTest extends AbstractManagementApiIntegrationTes
|
||||
final SoftwareModule module = findFirstModuleByType(testdataFactory.createDistributionSet("one"), osType).get();
|
||||
|
||||
for (int index = 0; index < totalMetadata; index++) {
|
||||
softwareModuleManagement.updateMetadata(entityFactory.softwareModuleMetadata().create(module.getId())
|
||||
.key(knownKeyPrefix + index).value(knownValuePrefix + index));
|
||||
softwareModuleManagement.createMetadata(module.getId(), knownKeyPrefix + index, new MetadataValueCreate(knownValuePrefix + index));
|
||||
}
|
||||
|
||||
mvc.perform(get(MgmtRestConstants.SOFTWAREMODULE_V1_REQUEST_MAPPING + "/{softwareModuleId}/metadata",
|
||||
@@ -184,8 +184,7 @@ class MgmtSoftwareModuleResourceTest extends AbstractManagementApiIntegrationTes
|
||||
final SoftwareModule module = findFirstModuleByType(testdataFactory.createDistributionSet("one"), osType).orElseThrow();
|
||||
|
||||
for (int index = 0; index < totalMetadata; index++) {
|
||||
softwareModuleManagement.updateMetadata(entityFactory.softwareModuleMetadata().create(module.getId())
|
||||
.key(knownKeyPrefix + index).value(knownValuePrefix + index));
|
||||
softwareModuleManagement.createMetadata(module.getId(), knownKeyPrefix + index, new MetadataValueCreate(knownValuePrefix + index));
|
||||
}
|
||||
|
||||
mvc.perform(get(MgmtRestConstants.SOFTWAREMODULE_V1_REQUEST_MAPPING + "/{softwareModuleId}/metadata",
|
||||
@@ -206,8 +205,7 @@ class MgmtSoftwareModuleResourceTest extends AbstractManagementApiIntegrationTes
|
||||
final String knownKey = "knownKey";
|
||||
final String knownValue = "knownValue";
|
||||
final SoftwareModule module = findFirstModuleByType(testdataFactory.createDistributionSet("one"), osType).orElseThrow();
|
||||
softwareModuleManagement.updateMetadata(
|
||||
entityFactory.softwareModuleMetadata().create(module.getId()).key(knownKey).value(knownValue));
|
||||
softwareModuleManagement.createMetadata(module.getId(), knownKey, new MetadataValueCreate(knownValue));
|
||||
|
||||
mvc.perform(get(MgmtRestConstants.SOFTWAREMODULE_V1_REQUEST_MAPPING + "/{softwareModuleId}/metadata/{metadataKey}",
|
||||
module.getId(), knownKey))
|
||||
@@ -1424,9 +1422,9 @@ class MgmtSoftwareModuleResourceTest extends AbstractManagementApiIntegrationTes
|
||||
.andExpect(status().isCreated());
|
||||
|
||||
assertThat(softwareModuleManagement.getMetadata(sm.getId(), knownKey1))
|
||||
.as("Metadata key is wrong").extracting(SoftwareModuleMetadata::getValue).isEqualTo(knownValue1);
|
||||
.as("Metadata key is wrong").extracting(MetadataValue::getValue).isEqualTo(knownValue1);
|
||||
assertThat(softwareModuleManagement.getMetadata(sm.getId(), knownKey2))
|
||||
.as("Metadata key is wrong").extracting(SoftwareModuleMetadata::getValue).isEqualTo(knownValue2);
|
||||
.as("Metadata key is wrong").extracting(MetadataValue::getValue).isEqualTo(knownValue2);
|
||||
|
||||
// verify quota enforcement
|
||||
final int maxMetaData = quotaManagement.getMaxMetaDataEntriesPerSoftwareModule();
|
||||
@@ -1443,7 +1441,6 @@ class MgmtSoftwareModuleResourceTest extends AbstractManagementApiIntegrationTes
|
||||
|
||||
// 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());
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1457,19 +1454,18 @@ class MgmtSoftwareModuleResourceTest extends AbstractManagementApiIntegrationTes
|
||||
final String updateValue = "valueForUpdate";
|
||||
|
||||
final SoftwareModule sm = testdataFactory.createSoftwareModuleOs();
|
||||
softwareModuleManagement.updateMetadata(
|
||||
entityFactory.softwareModuleMetadata().create(sm.getId()).key(knownKey).value(knownValue));
|
||||
softwareModuleManagement.createMetadata(sm.getId(), knownKey, new MetadataValueCreate(knownValue));
|
||||
|
||||
final JSONObject jsonObject = new JSONObject().put("key", knownKey)
|
||||
.put("value", updateValue)
|
||||
.put("targetVisible", true);
|
||||
|
||||
mvc.perform(put("/rest/v1/softwaremodules/{swId}/metadata/{key}", sm.getId(), knownKey).accept(
|
||||
MediaType.APPLICATION_JSON).contentType(MediaType.APPLICATION_JSON).content(jsonObject.toString()))
|
||||
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());
|
||||
|
||||
final SoftwareModuleMetadata assertDS = softwareModuleManagement.getMetadata(sm.getId(), knownKey);
|
||||
final MetadataValue assertDS = softwareModuleManagement.getMetadata(sm.getId(), knownKey);
|
||||
assertThat(assertDS.getValue()).as("Metadata is wrong").isEqualTo(updateValue);
|
||||
assertThat(assertDS.isTargetVisible()).as("target visible is wrong").isTrue();
|
||||
}
|
||||
@@ -1484,8 +1480,7 @@ class MgmtSoftwareModuleResourceTest extends AbstractManagementApiIntegrationTes
|
||||
final String knownValue = "knownValue";
|
||||
|
||||
final long smId = testdataFactory.createSoftwareModuleOs().getId();
|
||||
softwareModuleManagement.updateMetadata(
|
||||
entityFactory.softwareModuleMetadata().create(smId).key(knownKey).value(knownValue));
|
||||
softwareModuleManagement.createMetadata(smId, knownKey, new MetadataValueCreate(knownValue));
|
||||
|
||||
mvc.perform(delete("/rest/v1/softwaremodules/{swId}/metadata/{key}", smId, knownKey))
|
||||
.andDo(MockMvcResultPrinter.print())
|
||||
@@ -1505,8 +1500,7 @@ class MgmtSoftwareModuleResourceTest extends AbstractManagementApiIntegrationTes
|
||||
final String knownValue = "knownValue";
|
||||
|
||||
final long smId = testdataFactory.createSoftwareModuleOs().getId();
|
||||
softwareModuleManagement.updateMetadata(
|
||||
entityFactory.softwareModuleMetadata().create(smId).key(knownKey).value(knownValue));
|
||||
softwareModuleManagement.createMetadata(smId, knownKey, new MetadataValueCreate(knownValue));
|
||||
|
||||
mvc.perform(delete("/rest/v1/softwaremodules/{swId}/metadata/XXX", smId, knownKey))
|
||||
.andDo(MockMvcResultPrinter.print())
|
||||
|
||||
Reference in New Issue
Block a user