[#1580] Software Module & Distribution Set lock: add lock at rest level (#1647)

+ tests added

Signed-off-by: Marinov Avgustin <Avgustin.Marinov@bosch.com>
This commit is contained in:
Avgustin Marinov
2024-02-15 13:28:01 +02:00
committed by GitHub
parent 850fa3507f
commit 5c38af2772
2 changed files with 119 additions and 3 deletions

View File

@@ -41,6 +41,7 @@ import org.eclipse.hawkbit.exception.SpServerError;
import org.eclipse.hawkbit.mgmt.json.model.distributionset.MgmtActionType;
import org.eclipse.hawkbit.mgmt.rest.api.MgmtRestConstants;
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;
@@ -1035,12 +1036,10 @@ public class MgmtDistributionSetResourceTest extends AbstractManagementApiIntegr
@Test
@Description("Ensures that DS property update request to API is reflected by the repository.")
public void updateDistributionSet() throws Exception {
// prepare test data
assertThat(distributionSetManagement.findByCompleted(PAGE, true)).hasSize(0);
final DistributionSet set = testdataFactory.createDistributionSet("one");
assertThat(distributionSetManagement.count()).isEqualTo(1);
final String body = new JSONObject().put("version", "anotherVersion").put("requiredMigrationStep", true)
@@ -1051,6 +1050,7 @@ public class MgmtDistributionSetResourceTest extends AbstractManagementApiIntegr
.andDo(MockMvcResultPrinter.print()).andExpect(status().isOk())
.andExpect(jsonPath("$.version", equalTo("anotherVersion")))
.andExpect(jsonPath("$.requiredMigrationStep", equalTo(true)))
.andExpect(jsonPath("$.locked", equalTo(false)))
.andExpect(jsonPath("$.deleted", equalTo(false)));
final DistributionSet setupdated = distributionSetManagement.get(set.getId()).get();
@@ -1061,6 +1061,54 @@ public class MgmtDistributionSetResourceTest extends AbstractManagementApiIntegr
assertThat(setupdated.isDeleted()).isEqualTo(false);
}
@Test
@Description("Tests the lock. It is verified that the distribution set can be marked as locked through update operation.")
void lockDistributionSet() throws Exception {
// prepare test data
assertThat(distributionSetManagement.findByCompleted(PAGE, true)).hasSize(0);
final DistributionSet set = testdataFactory.createDistributionSet("one");
assertThat(distributionSetManagement.count()).isEqualTo(1);
assertThat(set.isLocked()).as("Created distribution set should not be locked").isFalse();
final String body = new JSONObject().put("locked", true).toString();
mvc.perform(put("/rest/v1/distributionsets/{dsId}", set.getId()).content(body)
.contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON))
.andDo(MockMvcResultPrinter.print())
.andExpect(status().isOk())
.andExpect(jsonPath("$.locked", equalTo(true)));
final DistributionSet updatedSet = distributionSetManagement.get(set.getId()).get();
assertThat(updatedSet.isLocked()).isEqualTo(true);
}
@Test
@Description("Tests the unlock. It is verified that the distribution set can't be unmarked as locked through update operation.")
void unlockDistributionSetSkippedSilently() throws Exception {
// prepare test data
assertThat(distributionSetManagement.findByCompleted(PAGE, true)).hasSize(0);
final DistributionSet set = testdataFactory.createDistributionSet("one");
assertThat(distributionSetManagement.count()).isEqualTo(1);
distributionSetManagement.lock(set.getId());
assertThat(distributionSetManagement.get(set.getId())
.orElseThrow(() -> new EntityNotFoundException(SoftwareModule.class, set.getId())).isLocked())
.as("Created software module should not be locked")
.isTrue();
final String body = new JSONObject().put("locked", false).toString();
mvc.perform(put("/rest/v1/distributionsets/{dsId}", set.getId()).content(body)
.contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON))
.andDo(MockMvcResultPrinter.print())
.andExpect(status().isOk())
.andExpect(jsonPath("$.locked", equalTo(true)));
final DistributionSet updatedSet = distributionSetManagement.get(set.getId()).get();
assertThat(updatedSet.isLocked()).isEqualTo(true);
}
@Test
@Description("Ensures that DS property update on requiredMigrationStep fails if DS is assigned to a target.")
public void updateRequiredMigrationStepFailsIfDistributionSetisInUse() throws Exception {

View File

@@ -46,6 +46,7 @@ import org.eclipse.hawkbit.mgmt.rest.api.MgmtRepresentationMode;
import org.eclipse.hawkbit.mgmt.rest.api.MgmtRestConstants;
import org.eclipse.hawkbit.repository.Constants;
import org.eclipse.hawkbit.repository.exception.AssignmentQuotaExceededException;
import org.eclipse.hawkbit.repository.exception.EntityNotFoundException;
import org.eclipse.hawkbit.repository.exception.FileSizeQuotaExceededException;
import org.eclipse.hawkbit.repository.exception.StorageQuotaExceededException;
import org.eclipse.hawkbit.repository.model.Artifact;
@@ -74,6 +75,7 @@ import org.springframework.http.MediaType;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RestController;
@@ -140,6 +142,7 @@ class MgmtSoftwareModuleResourceTest extends AbstractManagementApiIntegrationTes
.andExpect(jsonPath("$.lastModifiedAt", not(equalTo(sm.getLastModifiedAt()))))
.andExpect(jsonPath("$.description", equalTo(updateDescription)))
.andExpect(jsonPath("$.name", equalTo(knownSWName)))
.andExpect(jsonPath("$.locked", equalTo(false)))
.andReturn();
final SoftwareModule updatedSm = softwareModuleManagement.get(sm.getId()).get();
@@ -151,7 +154,7 @@ class MgmtSoftwareModuleResourceTest extends AbstractManagementApiIntegrationTes
}
@Test
@Description("Tests the update of the deletion flag. It is verfied that the software module can't be marked as deleted through update operation.")
@Description("Tests the update of the deletion flag. It is verified that the software module can't be marked as deleted through update operation.")
@WithUser(principal = "smUpdateTester", allSpPermissions = true)
void updateSoftwareModuleDeletedFlag() throws Exception {
final String knownSWName = "name1";
@@ -186,6 +189,71 @@ class MgmtSoftwareModuleResourceTest extends AbstractManagementApiIntegrationTes
}
@Test
@Description("Tests the lock. It is verified that the software module can be marked as locked through update operation.")
@WithUser(principal = "smUpdateTester", allSpPermissions = true)
void lockSoftwareModule() throws Exception {
final SoftwareModule sm = softwareModuleManagement.create(
entityFactory.softwareModule().create().type(osType).name("name1").version("version1"));
assertThat(sm.isLocked()).as("Created software module should not be locked").isFalse();
// ensures that we are not to fast so that last modified is not set correctly
Awaitility.await()
.atMost(Duration.ofMillis(100))
.pollInterval(10L, TimeUnit.MILLISECONDS)
.until(() -> sm.getLastModifiedAt() > 0L && sm.getLastModifiedBy() != null);
final String body = new JSONObject().put("locked", true).toString();
final ResultActions resultActions =
mvc.perform(put("/rest/v1/softwaremodules/{smId}", sm.getId()).content(body)
.contentType(MediaType.APPLICATION_JSON));
final SoftwareModule updatedSm = softwareModuleManagement.get(sm.getId()).get();
assertThat(updatedSm.getLastModifiedBy()).isEqualTo("smUpdateTester");
assertThat(updatedSm.isLocked()).isTrue();
resultActions
.andDo(MockMvcResultPrinter.print())
.andExpect(status().isOk())
.andExpect(jsonPath("$.id", equalTo(sm.getId().intValue())))
.andExpect(jsonPath("$.lastModifiedBy", equalTo("smUpdateTester")))
.andExpect(jsonPath("$.lastModifiedAt", equalTo(updatedSm.getLastModifiedAt())))
.andExpect(jsonPath("$.locked", equalTo(true)));
}
@Test
@Description("Tests the unlock. It is verified that the software module can't be unmarked as locked through update operation.")
@WithUser(principal = "smUpdateTester", allSpPermissions = true)
void unlockSoftwareModuleSkippedSilently() throws Exception {
final SoftwareModule sm = softwareModuleManagement.create(
entityFactory.softwareModule().create().type(osType).name("name1").version("version1"));
softwareModuleManagement.lock(sm.getId());
assertThat(softwareModuleManagement.get(sm.getId())
.orElseThrow(() -> new EntityNotFoundException(SoftwareModule.class, sm.getId())).isLocked())
.as("Created software module should not be locked")
.isTrue();
// ensures that we are not to fast so that last modified is not set correctly
Awaitility.await()
.atMost(Duration.ofMillis(100))
.pollInterval(10L, TimeUnit.MILLISECONDS)
.until(() -> sm.getLastModifiedAt() > 0L && sm.getLastModifiedBy() != null);
final String body = new JSONObject().put("locked", false).toString();
final ResultActions resultActions =
mvc.perform(put("/rest/v1/softwaremodules/{smId}", sm.getId()).content(body)
.contentType(MediaType.APPLICATION_JSON));
final SoftwareModule updatedSm = softwareModuleManagement.get(sm.getId()).get();
assertThat(updatedSm.getLastModifiedBy()).isEqualTo("smUpdateTester");
assertThat(updatedSm.isLocked()).isTrue(); // not unlocked
resultActions
.andExpect(status().isOk())
.andExpect(jsonPath("$.id", equalTo(sm.getId().intValue())))
.andExpect(jsonPath("$.lastModifiedBy", equalTo("smUpdateTester")))
.andExpect(jsonPath("$.lastModifiedAt", equalTo(updatedSm.getLastModifiedAt())))
.andExpect(jsonPath("$.locked", equalTo(true)));
}
@Test
@Description("Tests the upload of an artifact binary. The upload is executed and the content checked in the repository for completeness.")
void uploadArtifact() throws Exception {