Skip to content

Commit

Permalink
Hibernate versioning made to start from 1 to be EclipseLink compatible (
Browse files Browse the repository at this point in the history
#2156)

Signed-off-by: Avgustin Marinov <[email protected]>
  • Loading branch information
avgustinmm authored Dec 20, 2024
1 parent c38f28d commit a783037
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

import lombok.SneakyThrows;
import org.eclipse.hawkbit.mgmt.json.model.distributionset.MgmtActionType;
import org.eclipse.hawkbit.repository.jpa.Jpa;
import org.eclipse.hawkbit.repository.jpa.RepositoryApplicationConfiguration;
import org.eclipse.hawkbit.repository.jpa.model.JpaDistributionSet;
import org.eclipse.hawkbit.repository.model.BaseEntity;
Expand Down Expand Up @@ -205,12 +204,4 @@ static void implicitLock(final DistributionSet set) {
((JpaDistributionSet) set).lock();
}
}

// version is 1, 2 ... based
protected int version(final int version) {
return switch (Jpa.JPA_VENDOR) {
case ECLIPSELINK -> version;
case HIBERNATE -> version - 1;
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ void assignModuleTypesToDistributionSetTypeUntilQuotaExceeded() throws Exception

final DistributionSetType testType = distributionSetTypeManagement.create(entityFactory.distributionSetType()
.create().key("testType").name("testType").description("testType").colour("col12"));
assertThat(testType.getOptLockRevision()).isEqualTo(version(1));
assertThat(testType.getOptLockRevision()).isEqualTo(1);

for (int i = 0; i < moduleTypeIds.size() - 1; ++i) {
mvc.perform(post("/rest/v1/distributionsettypes/{dstID}/optionalmoduletypes", testType.getId())
Expand All @@ -235,7 +235,7 @@ void assignModuleTypesToDistributionSetTypeUntilQuotaExceeded() throws Exception

final DistributionSetType testType2 = distributionSetTypeManagement.create(entityFactory.distributionSetType()
.create().key("testType2").name("testType2").description("testType2").colour("col12"));
assertThat(testType2.getOptLockRevision()).isEqualTo(version(1));
assertThat(testType2.getOptLockRevision()).isEqualTo(1);

for (int i = 0; i < moduleTypeIds.size() - 1; ++i) {
mvc.perform(post("/rest/v1/distributionsettypes/{dstID}/mandatorymoduletypes", testType2.getId())
Expand Down Expand Up @@ -741,7 +741,7 @@ private DistributionSetType generateTestType() {
final DistributionSetType testType = distributionSetTypeManagement.create(entityFactory.distributionSetType()
.create().key("test123").name("TestName123").description("Desc123").colour("col")
.mandatory(Collections.singletonList(osType.getId())).optional(Collections.singletonList(appType.getId())));
assertThat(testType.getOptLockRevision()).isEqualTo(version(1));
assertThat(testType.getOptLockRevision()).isEqualTo(1);
assertThat(testType.getOptionalModuleTypes()).containsExactly(appType);
assertThat(testType.getMandatoryModuleTypes()).containsExactly(osType);
return testType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ void createTargetTypes() throws Exception {
void addDistributionSetTypeToTargetType() throws Exception {
String typeName = "TestTypeAddDs";
TargetType testType = createTestTargetTypeInDB(typeName);
assertThat(testType.getOptLockRevision()).isEqualTo(version(1));
assertThat(testType.getOptLockRevision()).isEqualTo(1);

mvc.perform(post(TARGETTYPE_DSTYPES_ENDPOINT, testType.getId())
.content("[{\"id\":" + standardDsType.getId() + "}]").contentType(MediaType.APPLICATION_JSON))
Expand All @@ -330,7 +330,7 @@ void addDistributionSetTypeToTargetType() throws Exception {

testType = targetTypeManagement.get(testType.getId()).get();
assertThat(testType.getLastModifiedBy()).isEqualTo(TEST_USER);
assertThat(testType.getOptLockRevision()).isEqualTo(version(2));
assertThat(testType.getOptLockRevision()).isEqualTo(2);
assertThat(testType.getCompatibleDistributionSetTypes()).containsExactly(standardDsType);
}

Expand Down Expand Up @@ -380,7 +380,7 @@ void removeDsTypeFromTargetType() throws Exception {

testType = targetTypeManagement.get(testType.getId()).get();
assertThat(testType.getLastModifiedBy()).isEqualTo(TEST_USER);
assertThat(testType.getOptLockRevision()).isEqualTo(version(2));
assertThat(testType.getOptLockRevision()).isEqualTo(2);
assertThat(testType.getCompatibleDistributionSetTypes()).isEmpty();
}

Expand All @@ -398,7 +398,7 @@ void deletingDsTypeRemovesAssignmentFromTargetType() throws Exception {

testType = targetTypeManagement.get(testType.getId()).get();
assertThat(testType.getLastModifiedBy()).isEqualTo(TEST_USER);
assertThat(testType.getOptLockRevision()).isEqualTo(version(2));
assertThat(testType.getOptLockRevision()).isEqualTo(2);
assertThat(testType.getCompatibleDistributionSetTypes()).isEmpty();
assertThat(distributionSetTypeManagement.getByKey(standardDsType.getKey())).isEmpty();
}
Expand Down Expand Up @@ -641,7 +641,7 @@ private TargetType createTestTargetTypeInDB(String name) {
@Step
private TargetType createTestTargetTypeInDB(String name, List<DistributionSetType> dsTypes) {
TargetType targetType = testdataFactory.createTargetType(name, dsTypes);
assertThat(targetType.getOptLockRevision()).isEqualTo(version(1));
assertThat(targetType.getOptLockRevision()).isEqualTo(1);
return targetType;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public abstract class AbstractJpaBaseEntity extends AbstractBaseEntity {
@Getter
@Version
@Column(name = "optlock_revision")
private int optLockRevision;
private int optLockRevision = 1;

// Audit fields. use property access to ensure that setters will be called and checked for modification
// (touch implementation depends on setLastModifiedAt(1).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,14 +244,6 @@ protected JpaRolloutGroup refresh(final RolloutGroup group) {
return rolloutGroupRepository.findById(group.getId()).get();
}

// version is 1, 2 ... based
protected int version(final int version) {
return switch (Jpa.JPA_VENDOR) {
case ECLIPSELINK -> version;
case HIBERNATE -> version - 1;
};
}

private JpaRollout refresh(final Rollout rollout) {
return rolloutRepository.findById(rollout.getId()).get();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -505,21 +505,21 @@ void updateDistributionSetMetadata() {
// create a DS
final DistributionSet ds = testdataFactory.createDistributionSet("testDs");
// initial opt lock revision must be zero
assertThat(ds.getOptLockRevision()).isEqualTo(version(1));
assertThat(ds.getOptLockRevision()).isEqualTo(1);

// create an DS meta data entry
createDistributionSetMetadata(ds.getId(), new JpaDistributionSetMetadata(knownKey, ds, knownValue));

final DistributionSet changedLockRevisionDS = getOrThrow(distributionSetManagement.get(ds.getId()));
assertThat(changedLockRevisionDS.getOptLockRevision()).isEqualTo(version(2));
assertThat(changedLockRevisionDS.getOptLockRevision()).isEqualTo(2);

// update the DS metadata
final JpaDistributionSetMetadata updated = (JpaDistributionSetMetadata) distributionSetManagement
.updateMetaData(ds.getId(), entityFactory.generateDsMetadata(knownKey, knownUpdateValue));
// we are updating the sw metadata so also modifying the base software
// module so opt lock revision must be three
final DistributionSet reloadedDS = getOrThrow(distributionSetManagement.get(ds.getId()));
assertThat(reloadedDS.getOptLockRevision()).isEqualTo(version(3));
assertThat(reloadedDS.getOptLockRevision()).isEqualTo(3);
assertThat(reloadedDS.getLastModifiedAt()).isPositive();

// verify updated meta data contains the updated value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ void createSoftwareModuleMetadata() {

final SoftwareModule ah = testdataFactory.createSoftwareModuleApp();

assertThat(ah.getOptLockRevision()).isEqualTo(version(1));
assertThat(ah.getOptLockRevision()).isEqualTo(1);

final SoftwareModuleMetadataCreate swMetadata1 = entityFactory.softwareModuleMetadata().create(ah.getId())
.key(knownKey1).value(knownValue1);
Expand All @@ -511,7 +511,7 @@ void createSoftwareModuleMetadata() {
.createMetaData(Arrays.asList(swMetadata1, swMetadata2));

final SoftwareModule changedLockRevisionModule = softwareModuleManagement.get(ah.getId()).get();
assertThat(changedLockRevisionModule.getOptLockRevision()).isEqualTo(version(2));
assertThat(changedLockRevisionModule.getOptLockRevision()).isEqualTo(2);

assertThat(softwareModuleMetadata).hasSize(2);
assertThat(softwareModuleMetadata.get(0)).isNotNull();
Expand Down Expand Up @@ -604,7 +604,7 @@ void updateSoftwareModuleMetadata() {
// create a base software module
final SoftwareModule ah = testdataFactory.createSoftwareModuleApp();
// initial opt lock revision must be 1
assertThat(ah.getOptLockRevision()).isEqualTo(version(1));
assertThat(ah.getOptLockRevision()).isEqualTo(1);

// create an software module meta data entry
final SoftwareModuleMetadata softwareModuleMetadata = softwareModuleManagement.createMetaData(
Expand All @@ -615,7 +615,7 @@ void updateSoftwareModuleMetadata() {
// base software module should have now the opt lock revision one
// because we are modifying the base software module
SoftwareModule changedLockRevisionModule = softwareModuleManagement.get(ah.getId()).get();
assertThat(changedLockRevisionModule.getOptLockRevision()).isEqualTo(version(2));
assertThat(changedLockRevisionModule.getOptLockRevision()).isEqualTo(2);

// update the software module metadata
final SoftwareModuleMetadata updated = softwareModuleManagement.updateMetaData(entityFactory
Expand All @@ -624,7 +624,7 @@ void updateSoftwareModuleMetadata() {
// we are updating the sw metadata so also modifying the base software
// module so opt lock revision must be two
changedLockRevisionModule = softwareModuleManagement.get(ah.getId()).get();
assertThat(changedLockRevisionModule.getOptLockRevision()).isEqualTo(version(3));
assertThat(changedLockRevisionModule.getOptLockRevision()).isEqualTo(3);

// verify updated meta data contains the updated value
assertThat(updated).isNotNull();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -840,14 +840,14 @@ void updateTargetMetadata() throws InterruptedException {
// create a target
final Target target = testdataFactory.createTarget("target1");
// initial opt lock revision must be zero
assertThat(target.getOptLockRevision()).isEqualTo(version(1));
assertThat(target.getOptLockRevision()).isEqualTo(1);

// create target meta data entry
insertTargetMetadata(knownKey, knownValue, target);

Target changedLockRevisionTarget = targetManagement.get(target.getId())
.orElseThrow(NoSuchElementException::new);
assertThat(changedLockRevisionTarget.getOptLockRevision()).isEqualTo(version(2));
assertThat(changedLockRevisionTarget.getOptLockRevision()).isEqualTo(2);

// Unsure if needed maybe to wait for a db flush?
// Thread.sleep(100);
Expand All @@ -858,7 +858,7 @@ void updateTargetMetadata() throws InterruptedException {
// we are updating the target meta data so also modifying the base
// software module so opt lock revision must be three
changedLockRevisionTarget = targetManagement.get(target.getId()).orElseThrow(NoSuchElementException::new);
assertThat(changedLockRevisionTarget.getOptLockRevision()).isEqualTo(version(3));
assertThat(changedLockRevisionTarget.getOptLockRevision()).isEqualTo(3);
assertThat(changedLockRevisionTarget.getLastModifiedAt()).isPositive();

// verify updated meta data contains the updated value
Expand All @@ -881,7 +881,7 @@ void createAndUpdateTargetTypeInTarget() {
// initial opt lock revision must be one
final Optional<JpaTarget> targetFound = targetRepository.findById(target.getId());
assertThat(targetFound).isPresent();
assertThat(targetFound.get().getOptLockRevision()).isEqualTo(version(1));
assertThat(targetFound.get().getOptLockRevision()).isEqualTo(1);
assertThat(targetFound.get().getTargetType().getId()).isEqualTo(targetTypes.get(0).getId());

// update the target type
Expand All @@ -892,7 +892,7 @@ void createAndUpdateTargetTypeInTarget() {
// opt lock revision must be changed
final Optional<JpaTarget> targetFound1 = targetRepository.findById(target.getId());
assertThat(targetFound1).isPresent();
assertThat(targetFound1.get().getOptLockRevision()).isEqualTo(version(2));
assertThat(targetFound1.get().getOptLockRevision()).isEqualTo(2);
assertThat(targetFound1.get().getTargetType().getId()).isEqualTo(targetTypes.get(1).getId());

// unassign the target type
Expand All @@ -901,7 +901,7 @@ void createAndUpdateTargetTypeInTarget() {
// opt lock revision must be changed
final Optional<JpaTarget> targetFound2 = targetRepository.findById(target.getId());
assertThat(targetFound2).isPresent();
assertThat(targetFound2.get().getOptLockRevision()).isEqualTo(version(3));
assertThat(targetFound2.get().getOptLockRevision()).isEqualTo(3);
assertThat(targetFound2.get().getTargetType()).isNull();
}

Expand All @@ -914,7 +914,7 @@ void assignTargetTypeInTarget() {
// initial opt lock revision must be one
final Optional<JpaTarget> targetFound = targetRepository.findById(target.getId());
assertThat(targetFound).isPresent();
assertThat(targetFound.get().getOptLockRevision()).isEqualTo(version(1));
assertThat(targetFound.get().getOptLockRevision()).isEqualTo(1);
assertThat(targetFound.get().getTargetType()).isNull();

// create a target type
Expand All @@ -927,7 +927,7 @@ void assignTargetTypeInTarget() {
// opt lock revision must be changed
final Optional<JpaTarget> targetFound1 = targetRepository.findById(target.getId());
assertThat(targetFound1).isPresent();
assertThat(targetFound1.get().getOptLockRevision()).isEqualTo(version(2));
assertThat(targetFound1.get().getOptLockRevision()).isEqualTo(2);
assertThat(targetFound1.get().getTargetType().getId()).isEqualTo(targetType.getId());
}

Expand Down Expand Up @@ -1003,7 +1003,7 @@ void assignInvalidTargetTypeToTarget() {
// initial opt lock revision must be one
final Optional<JpaTarget> targetFound = targetRepository.findById(target.getId());
assertThat(targetFound).isPresent();
assertThat(targetFound.get().getOptLockRevision()).isEqualTo(version(1));
assertThat(targetFound.get().getOptLockRevision()).isEqualTo(1);
assertThat(targetFound.get().getTargetType()).isNull();

// assign target type to target
Expand All @@ -1017,7 +1017,7 @@ void assignInvalidTargetTypeToTarget() {
// opt lock revision is not changed
final Optional<JpaTarget> targetFound1 = targetRepository.findById(target.getId());
assertThat(targetFound1).isPresent();
assertThat(targetFound1.get().getOptLockRevision()).isEqualTo(version(1));
assertThat(targetFound1.get().getOptLockRevision()).isEqualTo(1);
}

@Test
Expand All @@ -1032,7 +1032,7 @@ void unAssignTargetTypeFromTarget() {
// initial opt lock revision must be one
final Optional<JpaTarget> targetFound = targetRepository.findById(target.getId());
assertThat(targetFound).isPresent();
assertThat(targetFound.get().getOptLockRevision()).isEqualTo(version(1));
assertThat(targetFound.get().getOptLockRevision()).isEqualTo(1);
assertThat(targetFound.get().getTargetType().getName()).isEqualTo(targetType.getName());

// un-assign target type from target
Expand All @@ -1041,7 +1041,7 @@ void unAssignTargetTypeFromTarget() {
// opt lock revision must be changed
final Optional<JpaTarget> targetFound1 = targetRepository.findById(target.getId());
assertThat(targetFound1).isPresent();
assertThat(targetFound1.get().getOptLockRevision()).isEqualTo(version(2));
assertThat(targetFound1.get().getOptLockRevision()).isEqualTo(2);
assertThat(targetFound1.get().getTargetType()).isNull();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ void updateTargetTag() {
.isEqualTo("test123");
assertThat(targetTagRepository.findById(savedAssigned.getId()).get().getOptLockRevision())
.as("wrong target tag is saved")
.isEqualTo(version(2));
.isEqualTo(2);
}

@Test
Expand Down

0 comments on commit a783037

Please sign in to comment.