Refactor Management interfaces: find/get pattern (#2609)

Signed-off-by: Avgustin Marinov <Avgustin.Marinov@bosch.com>
This commit is contained in:
Avgustin Marinov
2025-08-15 16:18:32 +03:00
committed by GitHub
parent fa4dea75a3
commit b4edde8cc3
100 changed files with 713 additions and 986 deletions

View File

@@ -132,7 +132,7 @@ public class MgmtDistributionSetResource implements MgmtDistributionSetRestApi {
@Override
public ResponseEntity<MgmtDistributionSet> getDistributionSet(final Long distributionSetId) {
final DistributionSet foundDs = distributionSetManagement.getOrElseThrowException(distributionSetId);
final DistributionSet foundDs = distributionSetManagement.get(distributionSetId);
final MgmtDistributionSet response = MgmtDistributionSetMapper.toResponse(foundDs);
MgmtDistributionSetMapper.addLinks(foundDs, response);
@@ -211,7 +211,7 @@ public class MgmtDistributionSetResource implements MgmtDistributionSetRestApi {
final Long distributionSetId, final String rsqlParam,
final int pagingOffsetParam, final int pagingLimitParam, final String sortParam) {
// check if distribution set exists otherwise throw exception immediately
distributionSetManagement.getOrElseThrowException(distributionSetId);
distributionSetManagement.get(distributionSetId);
final Pageable pageable = PagingUtility.toPageable(pagingOffsetParam, pagingLimitParam, sanitizeDistributionSetSortParam(sortParam));
final Page<Target> targetsInstalledDS;
if (rsqlParam != null) {

View File

@@ -160,7 +160,7 @@ class MgmtDistributionSetTagResource implements MgmtDistributionSetTagRestApi {
}
private DistributionSetTag findDistributionTagById(final Long distributionsetTagId) {
return distributionSetTagManagement.get(distributionsetTagId)
return distributionSetTagManagement.find(distributionsetTagId)
.orElseThrow(() -> new EntityNotFoundException(DistributionSetTag.class, distributionsetTagId));
}
}

View File

@@ -180,12 +180,12 @@ public class MgmtDistributionSetTypeResource implements MgmtDistributionSetTypeR
}
private DistributionSetType findDistributionSetTypeWithExceptionIfNotFound(final Long distributionSetTypeId) {
return distributionSetTypeManagement.get(distributionSetTypeId)
return distributionSetTypeManagement.find(distributionSetTypeId)
.orElseThrow(() -> new EntityNotFoundException(DistributionSetType.class, distributionSetTypeId));
}
private SoftwareModuleType findSoftwareModuleTypeWithExceptionIfNotFound(final Long softwareModuleTypeId) {
return softwareModuleTypeManagement.get(softwareModuleTypeId)
return softwareModuleTypeManagement.find(softwareModuleTypeId)
.orElseThrow(() -> new EntityNotFoundException(SoftwareModuleType.class, softwareModuleTypeId));
}
}

View File

@@ -13,12 +13,11 @@ import java.io.InputStream;
import jakarta.servlet.http.HttpServletRequest;
import org.eclipse.hawkbit.repository.artifact.exception.ArtifactBinaryNoLongerExistsException;
import org.eclipse.hawkbit.repository.artifact.model.DbArtifact;
import org.eclipse.hawkbit.mgmt.rest.api.MgmtDownloadArtifactRestApi;
import org.eclipse.hawkbit.repository.ArtifactManagement;
import org.eclipse.hawkbit.repository.SoftwareModuleManagement;
import org.eclipse.hawkbit.repository.exception.ArtifactBinaryNoLongerExistsException;
import org.eclipse.hawkbit.repository.exception.ArtifactBinaryNotFoundException;
import org.eclipse.hawkbit.repository.exception.EntityNotFoundException;
import org.eclipse.hawkbit.repository.model.Artifact;
import org.eclipse.hawkbit.repository.model.SoftwareModule;
@@ -53,7 +52,7 @@ public class MgmtDownloadArtifactResource implements MgmtDownloadArtifactRestApi
*/
@Override
public ResponseEntity<InputStream> downloadArtifact(final Long softwareModuleId, final Long artifactId) {
final SoftwareModule module = softwareModuleManagement.get(softwareModuleId)
final SoftwareModule module = softwareModuleManagement.find(softwareModuleId)
.orElseThrow(() -> new EntityNotFoundException(SoftwareModule.class, softwareModuleId));
if (module.isDeleted()) {
throw new ArtifactBinaryNoLongerExistsException();
@@ -61,9 +60,7 @@ public class MgmtDownloadArtifactResource implements MgmtDownloadArtifactRestApi
final Artifact artifact = module.getArtifact(artifactId)
.orElseThrow(() -> new EntityNotFoundException(Artifact.class, artifactId));
final DbArtifact file = artifactManagement
.loadArtifactBinary(artifact.getSha1Hash(), module.getId(), module.isEncrypted())
.orElseThrow(() -> new ArtifactBinaryNotFoundException(artifact.getSha1Hash()));
final DbArtifact file = artifactManagement.loadArtifactBinary(artifact.getSha1Hash(), module.getId(), module.isEncrypted());
final HttpServletRequest request = RequestResponseContextHolder.getHttpServletRequest();
final String ifMatch = request.getHeader(HttpHeaders.IF_MATCH);
if (ifMatch != null && !HttpUtil.matchesHttpHeader(ifMatch, artifact.getSha1Hash())) {

View File

@@ -273,7 +273,7 @@ public class MgmtRolloutResource implements MgmtRolloutRestApi {
@Override
public ResponseEntity<MgmtRolloutResponseBody> retryRollout(final Long rolloutId) {
final Rollout rolloutForRetry = rolloutManagement.get(rolloutId)
final Rollout rolloutForRetry = rolloutManagement.find(rolloutId)
.orElseThrow(() -> new EntityNotFoundException(Rollout.class, rolloutId));
if (rolloutForRetry.isDeleted()) {
throw new EntityNotFoundException(Rollout.class, rolloutId);

View File

@@ -278,7 +278,7 @@ public class MgmtSoftwareModuleResource implements MgmtSoftwareModuleRestApi {
}
private SoftwareModule findSoftwareModuleWithExceptionIfNotFound(final Long softwareModuleId, final Long artifactId) {
final SoftwareModule module = softwareModuleManagement.get(softwareModuleId)
final SoftwareModule module = softwareModuleManagement.find(softwareModuleId)
.orElseThrow(() -> new EntityNotFoundException(SoftwareModule.class, softwareModuleId));
if (artifactId != null && module.getArtifact(artifactId).isEmpty()) {

View File

@@ -96,7 +96,7 @@ public class MgmtSoftwareModuleTypeResource implements MgmtSoftwareModuleTypeRes
}
private SoftwareModuleType findSoftwareModuleTypeWithExceptionIfNotFound(final Long softwareModuleTypeId) {
return softwareModuleTypeManagement.get(softwareModuleTypeId)
return softwareModuleTypeManagement.find(softwareModuleTypeId)
.orElseThrow(() -> new EntityNotFoundException(SoftwareModuleType.class, softwareModuleTypeId));
}
}

View File

@@ -172,7 +172,7 @@ public class MgmtTargetFilterQueryResource implements MgmtTargetFilterQueryRestA
}
private TargetFilterQuery findFilterWithExceptionIfNotFound(final Long filterId) {
return filterManagement.get(filterId)
return filterManagement.find(filterId)
.orElseThrow(() -> new EntityNotFoundException(TargetFilterQuery.class, filterId));
}
}

View File

@@ -154,7 +154,7 @@ public class MgmtTargetResource implements MgmtTargetRestApi {
targetManagement.unassignType(targetId);
return null;
} else {
return targetTypeManagement.get(targetRest.getTargetType())
return targetTypeManagement.find(targetRest.getTargetType())
.orElseThrow(() -> new EntityNotFoundException(TargetType.class, targetRest.getTargetType()));
}
})

View File

@@ -193,6 +193,6 @@ public class MgmtTargetTagResource implements MgmtTargetTagRestApi {
}
private TargetTag findTargetTagById(final Long targetTagId) {
return tagManagement.get(targetTagId).orElseThrow(() -> new EntityNotFoundException(TargetTag.class, targetTagId));
return tagManagement.find(targetTagId).orElseThrow(() -> new EntityNotFoundException(TargetTag.class, targetTagId));
}
}

View File

@@ -122,7 +122,7 @@ public class MgmtTargetTypeResource implements MgmtTargetTypeRestApi {
}
private TargetType findTargetTypeWithExceptionIfNotFound(final Long targetTypeId) {
return targetTypeManagement.get(targetTypeId)
return targetTypeManagement.find(targetTypeId)
.orElseThrow(() -> new EntityNotFoundException(TargetType.class, targetTypeId));
}
}

View File

@@ -345,7 +345,7 @@ public final class MgmtTargetMapper {
.description(targetRest.getDescription()).securityToken(targetRest.getSecurityToken())
.address(targetRest.getAddress())
.targetType(Optional.ofNullable(targetRest.getTargetType())
.map(targetTypeId -> targetTypeManagement.get(targetTypeId)
.map(targetTypeId -> targetTypeManagement.find(targetTypeId)
.orElseThrow(() -> new EntityNotFoundException(TargetType.class, targetTypeId)))
.map(TargetType.class::cast)
.orElse(null))

View File

@@ -50,7 +50,6 @@ import org.eclipse.hawkbit.repository.TargetManagement;
import org.eclipse.hawkbit.repository.exception.AssignmentQuotaExceededException;
import org.eclipse.hawkbit.repository.exception.EntityNotFoundException;
import org.eclipse.hawkbit.repository.jpa.repository.ActionRepository;
import org.eclipse.hawkbit.repository.jpa.specifications.ActionSpecifications;
import org.eclipse.hawkbit.repository.model.Action;
import org.eclipse.hawkbit.repository.model.Action.Status;
import org.eclipse.hawkbit.repository.model.DistributionSet;
@@ -1067,7 +1066,7 @@ class MgmtDistributionSetResourceTest extends AbstractManagementApiIntegrationTe
.andExpect(jsonPath("$.locked", equalTo(false)))
.andExpect(jsonPath("$.deleted", equalTo(false)));
final DistributionSet setupdated = distributionSetManagement.get(set.getId()).get();
final DistributionSet setupdated = distributionSetManagement.find(set.getId()).get();
assertThat(setupdated.isRequiredMigrationStep()).isTrue();
assertThat(setupdated.getVersion()).isEqualTo("anotherVersion");
@@ -1079,8 +1078,7 @@ class MgmtDistributionSetResourceTest extends AbstractManagementApiIntegrationTe
* Ensures that DS property update on requiredMigrationStep fails if DS is assigned to a target.
*/
@Test
void updateRequiredMigrationStepFailsIfDistributionSetisInUse() throws Exception {
void updateRequiredMigrationStepFailsIfDistributionSetIsInUse() throws Exception {
// prepare test data
assertThat(distributionSetManagement.findAll(PAGE)).isEmpty();
@@ -1095,7 +1093,7 @@ class MgmtDistributionSetResourceTest extends AbstractManagementApiIntegrationTe
.andDo(MockMvcResultPrinter.print())
.andExpect(status().isForbidden());
final DistributionSet setupdated = distributionSetManagement.get(set.getId()).get();
final DistributionSet setupdated = distributionSetManagement.find(set.getId()).get();
assertThat(setupdated.isRequiredMigrationStep()).isFalse();
assertThat(setupdated.getVersion()).isEqualTo(set.getVersion());
@@ -1702,16 +1700,16 @@ class MgmtDistributionSetResourceTest extends AbstractManagementApiIntegrationTe
.content(jsonObject.toString()).contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
assertThat(targetFilterQueryManagement.get(targetFilterQuery.getId()).get().getAutoAssignDistributionSet())
assertThat(targetFilterQueryManagement.find(targetFilterQuery.getId()).get().getAutoAssignDistributionSet())
.isNull();
assertThat(rolloutManagement.get(rollout.getId()).get().getStatus()).isIn(RolloutStatus.STOPPING,
assertThat(rolloutManagement.find(rollout.getId()).get().getStatus()).isIn(RolloutStatus.STOPPING,
RolloutStatus.STOPPED);
//then enforce executor to stop the rollout and check
rolloutHandler.handleAll();
assertThat(rolloutManagement.get(rollout.getId()).get().getStatus()).isIn(RolloutStatus.STOPPED);
assertThat(rolloutManagement.find(rollout.getId()).get().getStatus()).isIn(RolloutStatus.STOPPED);
for (final Target target : targets) {
assertThat(targetManagement.get(target.getId()).get().getUpdateStatus())
assertThat(targetManagement.find(target.getId()).get().getUpdateStatus())
.isEqualTo(TargetUpdateStatus.PENDING);
assertThat(deploymentManagement.findActionsByTarget(target.getControllerId(), PageRequest.of(0, 100))
.getNumberOfElements()).isEqualTo(1);
@@ -1737,17 +1735,17 @@ class MgmtDistributionSetResourceTest extends AbstractManagementApiIntegrationTe
.content(jsonObject.toString()).contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
assertThat(targetFilterQueryManagement.get(targetFilterQuery.getId()).get().getAutoAssignDistributionSet())
assertThat(targetFilterQueryManagement.find(targetFilterQuery.getId()).get().getAutoAssignDistributionSet())
.isNull();
assertThat(rolloutManagement.get(rollout.getId()).get().getStatus()).isIn(RolloutStatus.DELETING,
assertThat(rolloutManagement.find(rollout.getId()).get().getStatus()).isIn(RolloutStatus.DELETING,
RolloutStatus.DELETED);
//then enforce executor to stop the rollout and check
rolloutHandler.handleAll();
// assert rollout is deleted
assertThat(rolloutManagement.get(rollout.getId())).isEmpty();
assertThat(rolloutManagement.find(rollout.getId())).isEmpty();
for (final Target target : targets) {
assertThat(targetManagement.get(target.getId()).get().getUpdateStatus())
assertThat(targetManagement.find(target.getId()).get().getUpdateStatus())
.isEqualTo(TargetUpdateStatus.IN_SYNC);
assertThat(deploymentManagement.findActionsByTarget(target.getControllerId(), PageRequest.of(0, 100))
.getNumberOfElements()).isEqualTo(1);
@@ -1771,10 +1769,10 @@ class MgmtDistributionSetResourceTest extends AbstractManagementApiIntegrationTe
.content(jsonObject.toString()).contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
assertThat(rolloutManagement.get(rollout.getId()).get().getStatus()).isIn(RolloutStatus.RUNNING);
assertThat(rolloutManagement.find(rollout.getId()).get().getStatus()).isIn(RolloutStatus.RUNNING);
for (final Target target : targets) {
assertThat(targetManagement.get(target.getId()).get().getUpdateStatus())
assertThat(targetManagement.find(target.getId()).get().getUpdateStatus())
.isEqualTo(TargetUpdateStatus.PENDING);
assertThat(deploymentManagement.findActionsByTarget(target.getControllerId(), PageRequest.of(0, 100))
.getNumberOfElements()).isEqualTo(1);
@@ -1803,7 +1801,7 @@ class MgmtDistributionSetResourceTest extends AbstractManagementApiIntegrationTe
.andExpect(status().isOk())
.andExpect(jsonPath("$.locked", equalTo(true)));
final DistributionSet updatedSet = distributionSetManagement.get(set.getId()).get();
final DistributionSet updatedSet = distributionSetManagement.find(set.getId()).get();
assertThat(updatedSet.isLocked()).isTrue();
}
@@ -1818,7 +1816,7 @@ class MgmtDistributionSetResourceTest extends AbstractManagementApiIntegrationTe
final DistributionSet set = testdataFactory.createDistributionSet("one");
assertThat(distributionSetManagement.count()).isEqualTo(1);
distributionSetManagement.lock(set);
assertThat(distributionSetManagement.get(set.getId())
assertThat(distributionSetManagement.find(set.getId())
.orElseThrow(() -> new EntityNotFoundException(SoftwareModule.class, set.getId())).isLocked())
.as("Distribution set should be locked")
.isTrue();
@@ -1831,7 +1829,7 @@ class MgmtDistributionSetResourceTest extends AbstractManagementApiIntegrationTe
.andExpect(status().isOk())
.andExpect(jsonPath("$.locked", equalTo(false)));
final DistributionSet updatedSet = distributionSetManagement.get(set.getId()).get();
final DistributionSet updatedSet = distributionSetManagement.find(set.getId()).get();
assertThat(updatedSet.isLocked()).isFalse();
}

View File

@@ -261,7 +261,7 @@ class MgmtDistributionSetTagResourceTest extends AbstractManagementApiIntegratio
.andDo(MockMvcResultPrinter.print())
.andExpect(status().isOk());
assertThat(distributionSetTagManagement.get(original.getId())).isNotPresent();
assertThat(distributionSetTagManagement.find(original.getId())).isNotPresent();
}
/**

View File

@@ -181,7 +181,7 @@ class MgmtDistributionSetTypeResourceTest extends AbstractManagementApiIntegrati
.andDo(MockMvcResultPrinter.print())
.andExpect(status().isOk());
testType = distributionSetTypeManagement.get(testType.getId()).get();
testType = distributionSetTypeManagement.find(testType.getId()).get();
assertThat(testType.getLastModifiedBy()).isEqualTo("uploadTester");
assertThat(testType.getMandatoryModuleTypes()).containsExactly(osType);
assertThat(testType.getOptionalModuleTypes()).isEmpty();
@@ -207,7 +207,7 @@ class MgmtDistributionSetTypeResourceTest extends AbstractManagementApiIntegrati
.andDo(MockMvcResultPrinter.print())
.andExpect(status().isOk());
testType = distributionSetTypeManagement.get(testType.getId()).get();
testType = distributionSetTypeManagement.find(testType.getId()).get();
assertThat(testType.getLastModifiedBy()).isEqualTo("uploadTester");
assertThat(testType.getOptionalModuleTypes()).containsExactly(osType);
assertThat(testType.getMandatoryModuleTypes()).isEmpty();
@@ -366,7 +366,7 @@ class MgmtDistributionSetTypeResourceTest extends AbstractManagementApiIntegrati
.andDo(MockMvcResultPrinter.print())
.andExpect(status().isOk());
testType = distributionSetTypeManagement.get(testType.getId()).get();
testType = distributionSetTypeManagement.find(testType.getId()).get();
assertThat(testType.getLastModifiedBy()).isEqualTo("uploadTester");
assertThat(testType.getOptionalModuleTypes()).containsExactly(appType);
assertThat(testType.getMandatoryModuleTypes()).isEmpty();
@@ -385,7 +385,7 @@ class MgmtDistributionSetTypeResourceTest extends AbstractManagementApiIntegrati
.andDo(MockMvcResultPrinter.print())
.andExpect(status().isOk());
testType = distributionSetTypeManagement.get(testType.getId()).get();
testType = distributionSetTypeManagement.find(testType.getId()).get();
assertThat(testType.getLastModifiedBy()).isEqualTo("uploadTester");
assertThat(testType.getOptionalModuleTypes()).isEmpty();
assertThat(testType.getMandatoryModuleTypes()).containsExactly(osType);

View File

@@ -1946,7 +1946,7 @@ class MgmtRolloutResourceTest extends AbstractManagementApiIntegrationTest {
private void awaitRunningState(final Long rolloutId) {
awaitRollout().until(() -> SecurityContextSwitch
.callAsPrivileged(() -> rolloutManagement.get(rolloutId).orElseThrow(NoSuchElementException::new))
.callAsPrivileged(() -> rolloutManagement.find(rolloutId).orElseThrow(NoSuchElementException::new))
.getStatus().equals(RolloutStatus.RUNNING));
}
@@ -1966,7 +1966,7 @@ class MgmtRolloutResourceTest extends AbstractManagementApiIntegrationTest {
}
private void assertStatusIs(final Rollout rollout, final RolloutStatus expected) {
final Optional<Rollout> updatedRollout = rolloutManagement.get(rollout.getId());
final Optional<Rollout> updatedRollout = rolloutManagement.find(rollout.getId());
assertThat(updatedRollout).get().extracting(Rollout::getStatus).isEqualTo(expected);
}
@@ -2039,7 +2039,7 @@ class MgmtRolloutResourceTest extends AbstractManagementApiIntegrationTest {
// Run here, because Scheduler is disabled during tests
rolloutHandler.handleAll();
return rolloutManagement.get(rollout.getId()).orElseThrow(NoSuchElementException::new);
return rolloutManagement.find(rollout.getId()).orElseThrow(NoSuchElementException::new);
}
private void triggerNextGroupAndExpect(final Rollout rollout, final ResultMatcher expect) throws Exception {

View File

@@ -47,6 +47,9 @@ import org.eclipse.hawkbit.mgmt.rest.api.MgmtRestConstants;
import org.eclipse.hawkbit.mgmt.rest.resource.util.ResourceUtility;
import org.eclipse.hawkbit.repository.Constants;
import org.eclipse.hawkbit.repository.SoftwareModuleManagement;
import org.eclipse.hawkbit.repository.artifact.exception.ArtifactBinaryNotFoundException;
import org.eclipse.hawkbit.repository.artifact.model.DbArtifact;
import org.eclipse.hawkbit.repository.artifact.model.DbArtifactHash;
import org.eclipse.hawkbit.repository.exception.AssignmentQuotaExceededException;
import org.eclipse.hawkbit.repository.exception.EntityNotFoundException;
import org.eclipse.hawkbit.repository.exception.FileSizeQuotaExceededException;
@@ -258,7 +261,7 @@ class MgmtSoftwareModuleResourceTest extends AbstractManagementApiIntegrationTes
.andExpect(jsonPath("$.locked", equalTo(false)))
.andReturn();
final SoftwareModule updatedSm = softwareModuleManagement.get(sm.getId()).get();
final SoftwareModule updatedSm = softwareModuleManagement.find(sm.getId()).get();
assertThat(updatedSm.getName()).isEqualTo(knownSWName);
assertThat(updatedSm.getVendor()).isEqualTo(updateVendor);
assertThat(updatedSm.getLastModifiedBy()).isEqualTo("smUpdateTester");
@@ -294,7 +297,7 @@ class MgmtSoftwareModuleResourceTest extends AbstractManagementApiIntegrationTes
.andExpect(jsonPath("$.lastModifiedAt", equalTo(sm.getLastModifiedAt())))
.andExpect(jsonPath("$.deleted", equalTo(false)));
softwareModuleManagement.get(sm.getId());
softwareModuleManagement.find(sm.getId());
assertThat(sm.getLastModifiedBy()).isEqualTo("smUpdateTester");
assertThat(sm.getLastModifiedAt()).isEqualTo(sm.getLastModifiedAt());
assertThat(sm.isDeleted()).isFalse();
@@ -319,7 +322,7 @@ class MgmtSoftwareModuleResourceTest extends AbstractManagementApiIntegrationTes
mvc.perform(put("/rest/v1/softwaremodules/{smId}", sm.getId()).content(body)
.contentType(MediaType.APPLICATION_JSON));
final SoftwareModule updatedSm = softwareModuleManagement.get(sm.getId()).get();
final SoftwareModule updatedSm = softwareModuleManagement.find(sm.getId()).get();
assertThat(updatedSm.getLastModifiedBy()).isEqualTo("smUpdateTester");
assertThat(updatedSm.isLocked()).isTrue();
@@ -341,7 +344,7 @@ class MgmtSoftwareModuleResourceTest extends AbstractManagementApiIntegrationTes
final SoftwareModule sm = softwareModuleManagement.create(
SoftwareModuleManagement.Create.builder().type(osType).name("name1").version("version1").build());
softwareModuleManagement.lock(sm);
assertThat(softwareModuleManagement.get(sm.getId())
assertThat(softwareModuleManagement.find(sm.getId())
.orElseThrow(() -> new EntityNotFoundException(SoftwareModule.class, sm.getId())).isLocked())
.as("Software module is locked")
.isTrue();
@@ -354,7 +357,7 @@ class MgmtSoftwareModuleResourceTest extends AbstractManagementApiIntegrationTes
mvc.perform(put("/rest/v1/softwaremodules/{smId}", sm.getId()).content(body)
.contentType(MediaType.APPLICATION_JSON));
final SoftwareModule updatedSm = softwareModuleManagement.get(sm.getId()).get();
final SoftwareModule updatedSm = softwareModuleManagement.find(sm.getId()).get();
assertThat(updatedSm.getLastModifiedBy()).isEqualTo("smUpdateTester");
assertThat(updatedSm.isLocked()).isFalse(); // not unlocked
@@ -397,7 +400,7 @@ class MgmtSoftwareModuleResourceTest extends AbstractManagementApiIntegrationTes
// check rest of response compared to DB
final MgmtArtifact artResult = ResourceUtility.convertArtifactResponse(
mvcResult.getResponse().getContentAsString());
final Long artId = softwareModuleManagement.get(sm.getId()).get().getArtifacts().get(0).getId();
final Long artId = softwareModuleManagement.find(sm.getId()).get().getArtifacts().get(0).getId();
assertThat(artResult.getId()).as("Wrong artifact id").isEqualTo(artId);
assertThat((Object) JsonPath.compile("$._links.self.href").read(mvcResult.getResponse().getContentAsString()))
.as("Link contains no self url")
@@ -455,14 +458,10 @@ class MgmtSoftwareModuleResourceTest extends AbstractManagementApiIntegrationTes
@Test
void emptyUploadArtifact() throws Exception {
assertThat(softwareModuleManagement.findAll(PAGE)).isEmpty();
assertThat(artifactManagement.count()).isZero();
final SoftwareModule sm = testdataFactory.createSoftwareModuleOs();
final MockMultipartFile file = new MockMultipartFile("file", "orig", null, new byte[0]);
mvc.perform(multipart("/rest/v1/softwaremodules/{smId}/artifacts", sm.getId()).file(file)
.accept(MediaType.APPLICATION_JSON))
mvc.perform(multipart("/rest/v1/softwaremodules/{smId}/artifacts", sm.getId()).file(file).accept(MediaType.APPLICATION_JSON))
.andDo(MockMvcResultPrinter.print())
.andExpect(status().isBadRequest());
}
@@ -502,7 +501,6 @@ class MgmtSoftwareModuleResourceTest extends AbstractManagementApiIntegrationTes
@Test
void uploadArtifactWithCustomName() throws Exception {
final SoftwareModule sm = testdataFactory.createSoftwareModuleOs();
assertThat(artifactManagement.count()).isZero();
// create test file
final byte[] random = randomBytes(5 * 1024);
@@ -515,14 +513,11 @@ class MgmtSoftwareModuleResourceTest extends AbstractManagementApiIntegrationTes
.andExpect(status().isCreated())
.andExpect(content().contentType(MediaTypes.HAL_JSON))
.andExpect(jsonPath("$.providedFilename", equalTo("customFilename")))
.andExpect(status().isCreated());
// check result in db...
// repo
assertThat(artifactManagement.count()).isEqualTo(1);
// hashes
assertThat(artifactManagement.getByFilename("customFilename")).as("Local artifact is wrong").isPresent();
.andExpect(status().isCreated())
.andDo(result -> {
final MgmtArtifact mgmtArtifact = OBJECT_MAPPER.readerFor(MgmtArtifact.class).readValue(result.getResponse().getContentAsString());
assertThat(artifactManagement.loadArtifactBinary(mgmtArtifact.getHashes().getSha1(), sm.getId(), sm.isEncrypted())).isNotNull();
});
}
/**
@@ -531,7 +526,6 @@ class MgmtSoftwareModuleResourceTest extends AbstractManagementApiIntegrationTes
@Test
void uploadArtifactWithHashCheck() throws Exception {
final SoftwareModule sm = testdataFactory.createSoftwareModuleOs();
assertThat(artifactManagement.count()).isZero();
// create test file
final byte[] random = randomBytes(5 * 1024);
@@ -586,7 +580,6 @@ class MgmtSoftwareModuleResourceTest extends AbstractManagementApiIntegrationTes
.andExpect(status().isCreated());
assertArtifact(sm, random);
}
/**
@@ -700,9 +693,6 @@ class MgmtSoftwareModuleResourceTest extends AbstractManagementApiIntegrationTes
downloadAndVerify(sm, random, artifact);
downloadAndVerify(sm, random, artifact2);
assertThat(softwareModuleManagement.findAll(PAGE)).as("Softwaremodule size is wrong").hasSize(1);
assertThat(artifactManagement.count()).isEqualTo(2);
}
/**
@@ -783,7 +773,7 @@ class MgmtSoftwareModuleResourceTest extends AbstractManagementApiIntegrationTes
SoftwareModule sm = testdataFactory.createSoftwareModuleOs("softDeleted");
final Artifact artifact = testdataFactory.createArtifacts(sm.getId()).get(0);
// the sm is changed by artifact creation, necessary to get the latest version for Hibernate
sm = softwareModuleManagement.get(sm.getId()).orElseThrow();
sm = softwareModuleManagement.find(sm.getId()).orElseThrow();
testdataFactory.createDistributionSet(List.of(sm));
softwareModuleManagement.delete(sm.getId());
@@ -904,7 +894,7 @@ class MgmtSoftwareModuleResourceTest extends AbstractManagementApiIntegrationTes
SoftwareModule smSoftDeleted = testdataFactory.createSoftwareModuleOs("softDeleted");
final Artifact artifactSoftDeleted = testdataFactory.createArtifacts(smSoftDeleted.getId()).get(0);
// the smSoftDeleted is changed by artifact creation, necessary to get the latest version for Hibernate
smSoftDeleted = softwareModuleManagement.get(smSoftDeleted.getId()).orElseThrow();
smSoftDeleted = softwareModuleManagement.find(smSoftDeleted.getId()).orElseThrow();
testdataFactory.createDistributionSet(List.of(smSoftDeleted));
softwareModuleManagement.delete(smSoftDeleted.getId());
@@ -959,31 +949,34 @@ class MgmtSoftwareModuleResourceTest extends AbstractManagementApiIntegrationTes
final SoftwareModule sm = testdataFactory.createSoftwareModuleOs();
final int artifactSize = 5 * 1024;
final byte[] random = randomBytes(artifactSize);
// Create 2 artifacts
final long moduleId = sm.getId();
final byte[] random = randomBytes(artifactSize);
final Artifact artifact = artifactManagement.create(
new ArtifactUpload(new ByteArrayInputStream(random), sm.getId(), "file1", false, artifactSize));
new ArtifactUpload(new ByteArrayInputStream(random), moduleId, "file1", false, artifactSize));
final byte[] random2 = randomBytes(artifactSize);
artifactManagement.create(
new ArtifactUpload(new ByteArrayInputStream(random), sm.getId(), "file2", false, artifactSize));
new ArtifactUpload(new ByteArrayInputStream(random2), moduleId, "file2", false, artifactSize));
// check repo before delete
assertThat(softwareModuleManagement.findAll(PAGE)).hasSize(1);
assertThat(softwareModuleManagement.get(sm.getId()).get().getArtifacts()).hasSize(2);
assertThat(artifactManagement.count()).isEqualTo(2);
assertThat(softwareModuleManagement.find(moduleId).get().getArtifacts()).hasSize(2);
// delete
mvc.perform(delete("/rest/v1/softwaremodules/{smId}/artifacts/{artId}", sm.getId(), artifact.getId()))
mvc.perform(delete("/rest/v1/softwaremodules/{smId}/artifacts/{artId}", moduleId, artifact.getId()))
.andDo(MockMvcResultPrinter.print())
.andExpect(status().isOk());
// check that only one artifact is still alive and still assigned
assertThat(softwareModuleManagement.findAll(PAGE)).as("After the sm should be marked as deleted").hasSize(1);
assertThat(artifactManagement.count()).isEqualTo(1);
assertThat(softwareModuleManagement.get(sm.getId()).get().getArtifacts())
final boolean encrypted = sm.isEncrypted();
final String sha1Hash = artifact.getSha1Hash();
assertThatExceptionOfType(ArtifactBinaryNotFoundException.class)
.isThrownBy(() -> artifactManagement.loadArtifactBinary(sha1Hash, moduleId, encrypted));
assertThat(softwareModuleManagement.find(moduleId).get().getArtifacts())
.as("After delete artifact should available for marked as deleted sm's").hasSize(1);
}
/**
@@ -1332,8 +1325,8 @@ class MgmtSoftwareModuleResourceTest extends AbstractManagementApiIntegrationTes
.isEqualTo("http://localhost/rest/v1/softwaremodules/" + appCreatedId);
assertThat(softwareModuleManagement.findAll(PAGE)).as("Wrong softwaremodule size").hasSize(2);
final SoftwareModule osCreated = softwareModuleManagement.get(osCreatedId).orElseThrow();
final SoftwareModule appCreated = softwareModuleManagement.get(appCreatedId).orElseThrow();
final SoftwareModule osCreated = softwareModuleManagement.find(osCreatedId).orElseThrow();
final SoftwareModule appCreated = softwareModuleManagement.find(appCreatedId).orElseThrow();
assertThat(osCreated.getName()).as("Softwaremoudle name is wrong").isEqualTo(os.getName());
assertThat(osCreated.getCreatedBy()).as("Softwaremoudle created by is wrong").isEqualTo("uploadTester");
assertThat(osCreated.getCreatedAt()).as("Softwaremoudle created at is wrong").isGreaterThanOrEqualTo(current);
@@ -1345,29 +1338,32 @@ class MgmtSoftwareModuleResourceTest extends AbstractManagementApiIntegrationTes
*/
@Test
void deleteUnassignedSoftwareModule() throws Exception {
final SoftwareModule sm = testdataFactory.createSoftwareModuleOs();
final int artifactSize = 5 * 1024;
final byte[] random = randomBytes(artifactSize);
artifactManagement.create(
final Artifact artifact = artifactManagement.create(
new ArtifactUpload(new ByteArrayInputStream(random), sm.getId(), "file1", false, artifactSize));
assertThat(softwareModuleManagement.findAll(PAGE)).as("Softwaremoudle size is wrong").hasSize(1);
assertThat(artifactManagement.count()).isEqualTo(1);
final Long smId = sm.getId();
final String sha1Hash = artifact.getSha1Hash();
final boolean encrypted = sm.isEncrypted();
assertThat(artifactManagement.loadArtifactBinary(sha1Hash, smId, encrypted)).isNotNull();
mvc.perform(delete("/rest/v1/softwaremodules/{smId}", sm.getId()))
.andDo(MockMvcResultPrinter.print())
.andExpect(status().isOk());
assertThat(softwareModuleManagement.findAll(PAGE)).as("After delete no softwarmodule should be available")
.isEmpty();
assertThat(artifactManagement.count()).isZero();
assertThat(softwareModuleManagement.findAll(PAGE)).as("After delete no softwarmodule should be available").isEmpty();
assertThatExceptionOfType(EntityNotFoundException.class) // sm doesn't exists
.isThrownBy(() -> artifactManagement.loadArtifactBinary(sha1Hash, smId, encrypted));
}
/**
* Verifies successfull deletion of a software module that is in use, i.e. assigned to a DS which should result in movinf the module to the archive.
* Verifies successful deletion of a software module that is in use, i.e. assigned to a DS which should result in movinf the module to the archive.
*/
@Test
void deleteAssignedSoftwareModule() throws Exception {
@@ -1376,13 +1372,16 @@ class MgmtSoftwareModuleResourceTest extends AbstractManagementApiIntegrationTes
final int artifactSize = 5 * 1024;
final byte[] random = randomBytes(artifactSize);
final Long appTypeSmId = findFirstModuleByType(ds1, appType).get().getId();
final SoftwareModule appTypeSm = findFirstModuleByType(ds1, appType).get();
final Long appTypeSmId = appTypeSm.getId();
artifactManagement.create(
final Artifact artifact = artifactManagement.create(
new ArtifactUpload(new ByteArrayInputStream(random), appTypeSmId, "file1", false, artifactSize));
assertThat(softwareModuleManagement.count()).isEqualTo(3);
assertThat(artifactManagement.count()).isEqualTo(1);
final String sha1Hash = artifact.getSha1Hash();
final boolean encrypted = appTypeSm.isEncrypted();
assertThat(artifactManagement.loadArtifactBinary(sha1Hash, appTypeSmId, encrypted)).isNotNull();
mvc.perform(get("/rest/v1/softwaremodules/{smId}", appTypeSmId))
.andDo(MockMvcResultPrinter.print())
@@ -1399,7 +1398,8 @@ class MgmtSoftwareModuleResourceTest extends AbstractManagementApiIntegrationTes
.andExpect(jsonPath("$.deleted", equalTo(true)));
assertThat(softwareModuleManagement.count()).isEqualTo(2);
assertThat(artifactManagement.count()).isEqualTo(1);
assertThatExceptionOfType(ArtifactBinaryNotFoundException.class)
.isThrownBy(() -> artifactManagement.loadArtifactBinary(sha1Hash, appTypeSmId, encrypted));
}
/**
@@ -1526,31 +1526,25 @@ class MgmtSoftwareModuleResourceTest extends AbstractManagementApiIntegrationTes
}
private void assertArtifact(final SoftwareModule sm, final byte[] random) throws IOException {
// check result in db...
// repo
assertThat(artifactManagement.count()).as("Wrong artifact size").isEqualTo(1);
final DbArtifact artifact = artifactManagement
.loadArtifactBinary(
softwareModuleManagement.find(
sm.getId()).orElseThrow().getArtifacts().get(0).getSha1Hash(), sm.getId(), sm.isEncrypted());
// binary
try (final InputStream fileInputStream = artifactManagement
.loadArtifactBinary(softwareModuleManagement.get(sm.getId()).orElseThrow().getArtifacts().get(0).getSha1Hash(),
sm.getId(), sm.isEncrypted())
.get().getFileInputStream()) {
try (final InputStream fileInputStream = artifact.getFileInputStream()) {
assertTrue(IOUtils.contentEquals(new ByteArrayInputStream(random), fileInputStream),
"Wrong artifact content");
}
// hashes
assertThat(artifactManagement.getByFilename("origFilename").orElseThrow().getSha1Hash()).as("Wrong sha1 hash")
.isEqualTo(HashGeneratorUtils.generateSHA1(random));
assertThat(artifactManagement.getByFilename("origFilename").orElseThrow().getMd5Hash()).as("Wrong md5 hash")
.isEqualTo(HashGeneratorUtils.generateMD5(random));
assertThat(artifactManagement.getByFilename("origFilename").orElseThrow().getSha256Hash()).as("Wrong sha256 hash")
.isEqualTo(HashGeneratorUtils.generateSHA256(random));
final DbArtifactHash hash = artifact.getHashes();
assertThat(hash.getSha1()).as("Wrong sha1 hash").isEqualTo(HashGeneratorUtils.generateSHA1(random));
// sha1 hashes are not used via loaded artifact
// assertThat(hash.getMd5()).as("Wrong md5 hash").isEqualTo(HashGeneratorUtils.generateMD5(random));
// assertThat(hash.getSha256()).as("Wrong sha256 hash").isEqualTo(HashGeneratorUtils.generateSHA256(random));
// metadata
assertThat(softwareModuleManagement.get(sm.getId()).orElseThrow().getArtifacts().get(0).getFilename())
assertThat(softwareModuleManagement.find(sm.getId()).orElseThrow().getArtifacts().get(0).getFilename())
.as("wrong metadata of the filename").isEqualTo("origFilename");
}

View File

@@ -349,7 +349,7 @@ public class MgmtSoftwareModuleTypeResourceTest extends AbstractManagementApiInt
.andExpect(jsonPath("$.lastModifiedAt", equalTo(testType.getLastModifiedAt())))
.andExpect(jsonPath("$.deleted", equalTo(false)));
testType = softwareModuleTypeManagement.get(testType.getId()).get();
testType = softwareModuleTypeManagement.find(testType.getId()).get();
assertThat(testType.getLastModifiedAt()).isEqualTo(testType.getLastModifiedAt());
assertThat(testType.isDeleted()).isFalse();
}

View File

@@ -144,7 +144,7 @@ public class MgmtTargetFilterQueryResourceTest extends AbstractManagementApiInte
mvc.perform(delete(MgmtRestConstants.TARGET_FILTER_V1_REQUEST_MAPPING + "/" + filterQuery.getId()))
.andExpect(status().isOk());
assertThat(targetFilterQueryManagement.get(filterQuery.getId())).isNotPresent();
assertThat(targetFilterQueryManagement.find(filterQuery.getId())).isNotPresent();
}
/**
@@ -192,7 +192,7 @@ public class MgmtTargetFilterQueryResourceTest extends AbstractManagementApiInte
.andExpect(jsonPath(JSON_PATH_NAME, equalTo(filterName)))
.andExpect(jsonPath(JSON_PATH_CONFIRMATION_REQUIRED).doesNotExist());
final TargetFilterQuery tfqCheck = targetFilterQueryManagement.get(tfq.getId()).get();
final TargetFilterQuery tfqCheck = targetFilterQueryManagement.find(tfq.getId()).get();
assertThat(tfqCheck.getQuery()).isEqualTo(filterQuery2);
assertThat(tfqCheck.getName()).isEqualTo(filterName);
}
@@ -219,7 +219,7 @@ public class MgmtTargetFilterQueryResourceTest extends AbstractManagementApiInte
.andExpect(jsonPath(JSON_PATH_NAME, equalTo(filterName2)))
.andExpect(jsonPath(JSON_PATH_CONFIRMATION_REQUIRED).doesNotExist());
final TargetFilterQuery tfqCheck = targetFilterQueryManagement.get(tfq.getId()).get();
final TargetFilterQuery tfqCheck = targetFilterQueryManagement.find(tfq.getId()).get();
assertThat(tfqCheck.getQuery()).isEqualTo(filterQuery);
assertThat(tfqCheck.getName()).isEqualTo(filterName2);
}
@@ -493,7 +493,7 @@ public class MgmtTargetFilterQueryResourceTest extends AbstractManagementApiInte
.andExpect(status().isOk());
implicitLock(set);
final TargetFilterQuery updatedFilterQuery = targetFilterQueryManagement.get(filterQuery.getId()).get();
final TargetFilterQuery updatedFilterQuery = targetFilterQueryManagement.find(filterQuery.getId()).get();
assertThat(updatedFilterQuery.getAutoAssignDistributionSet()).isEqualTo(set);
assertThat(updatedFilterQuery.getAutoAssignActionType()).isEqualTo(ActionType.FORCED);
@@ -603,7 +603,7 @@ public class MgmtTargetFilterQueryResourceTest extends AbstractManagementApiInte
targetFilterQueryManagement.updateAutoAssignDS(new AutoAssignDistributionSetUpdate(tfq.getId()).ds(set.getId()));
implicitLock(set);
final TargetFilterQuery updatedFilterQuery = targetFilterQueryManagement.get(tfq.getId()).orElseThrow();
final TargetFilterQuery updatedFilterQuery = targetFilterQueryManagement.find(tfq.getId()).orElseThrow();
assertThat(updatedFilterQuery.getAutoAssignDistributionSet()).isEqualTo(set);
assertThat(updatedFilterQuery.getAutoAssignActionType()).isEqualTo(ActionType.FORCED);
@@ -615,7 +615,7 @@ public class MgmtTargetFilterQueryResourceTest extends AbstractManagementApiInte
mvc.perform(delete(MgmtRestConstants.TARGET_FILTER_V1_REQUEST_MAPPING + "/" + tfq.getId() + "/autoAssignDS"))
.andExpect(status().isNoContent());
final TargetFilterQuery filterQueryWithDeletedDs = targetFilterQueryManagement.get(tfq.getId()).get();
final TargetFilterQuery filterQueryWithDeletedDs = targetFilterQueryManagement.find(tfq.getId()).get();
assertThat(filterQueryWithDeletedDs.getAutoAssignDistributionSet()).isNull();
assertThat(filterQueryWithDeletedDs.getAutoAssignActionType()).isNull();
@@ -676,7 +676,7 @@ public class MgmtTargetFilterQueryResourceTest extends AbstractManagementApiInte
// do not provide something about the confirmation
verifyAutoAssignmentByActionType(tfq, set, null, null);
assertThat(targetFilterQueryManagement.get(tfq.getId())).hasValueSatisfying(filter ->
assertThat(targetFilterQueryManagement.find(tfq.getId())).hasValueSatisfying(filter ->
assertThat(filter.isConfirmationRequired()).isEqualTo(confirmationFlowActive));
}
@@ -720,7 +720,7 @@ public class MgmtTargetFilterQueryResourceTest extends AbstractManagementApiInte
.andExpect(status().isOk());
implicitLock(set);
final TargetFilterQuery updatedFilterQuery = targetFilterQueryManagement.get(tfq.getId()).get();
final TargetFilterQuery updatedFilterQuery = targetFilterQueryManagement.find(tfq.getId()).get();
final MgmtActionType expectedActionType = actionType != null ? actionType : MgmtActionType.FORCED;
assertThat(updatedFilterQuery.getAutoAssignDistributionSet()).isEqualTo(set);

View File

@@ -2576,7 +2576,7 @@ class MgmtTargetResourceTest extends AbstractManagementApiIntegrationTest {
final long unknownTargetTypeId = 999;
final String errorMsg = String.format("TargetType with given identifier {%s} does not exist.", unknownTargetTypeId);
final Optional<? extends TargetType> targetType = targetTypeManagement.get(unknownTargetTypeId);
final Optional<? extends TargetType> targetType = targetTypeManagement.find(unknownTargetTypeId);
assertThat(targetType).isNotPresent();
final String controllerId = "targetcontroller";

View File

@@ -227,7 +227,7 @@ public class MgmtTargetTagResourceTest extends AbstractManagementApiIntegrationT
.andDo(MockMvcResultPrinter.print())
.andExpect(status().isOk());
assertThat(targetTagManagement.get(original.getId())).isNotPresent();
assertThat(targetTagManagement.find(original.getId())).isNotPresent();
}
/**

View File

@@ -349,7 +349,7 @@ class MgmtTargetTypeResourceTest extends AbstractManagementApiIntegrationTest {
.andDo(MockMvcResultPrinter.print())
.andExpect(status().isOk());
testType = targetTypeManagement.get(testType.getId()).get();
testType = targetTypeManagement.find(testType.getId()).get();
assertThat(testType.getLastModifiedBy()).isEqualTo(TEST_USER);
assertThat(testType.getOptLockRevision()).isEqualTo(2);
assertThat(testType.getDistributionSetTypes()).containsExactly(standardDsType);
@@ -405,7 +405,7 @@ class MgmtTargetTypeResourceTest extends AbstractManagementApiIntegrationTest {
.andDo(MockMvcResultPrinter.print())
.andExpect(status().isOk());
testType = targetTypeManagement.get(testType.getId()).get();
testType = targetTypeManagement.find(testType.getId()).get();
assertThat(testType.getLastModifiedBy()).isEqualTo(TEST_USER);
assertThat(testType.getOptLockRevision()).isEqualTo(2);
assertThat(testType.getDistributionSetTypes()).isEmpty();
@@ -425,7 +425,7 @@ class MgmtTargetTypeResourceTest extends AbstractManagementApiIntegrationTest {
.andDo(MockMvcResultPrinter.print())
.andExpect(status().isOk());
testType = targetTypeManagement.get(testType.getId()).get();
testType = targetTypeManagement.find(testType.getId()).get();
assertThat(testType.getLastModifiedBy()).isEqualTo(TEST_USER);
assertThat(testType.getOptLockRevision()).isEqualTo(2);
assertThat(testType.getDistributionSetTypes()).isEmpty();