Skip to content

Commit

Permalink
[#464] implement extended soft-delete functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
david-monichi-bp committed Dec 14, 2024
1 parent 83e9175 commit da2cece
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -308,13 +308,22 @@ insert into scl_label(scl_id, major_version, minor_version, patch_version, label

@Override
@Transactional(REQUIRED)
public void delete(SclFileType type, UUID id) {
var sql = """
public void delete(SclFileType type, UUID id, boolean softDelete) {
String sql;
if (softDelete) {
sql = """
UPDATE scl_file
SET scl_file.is_deleted = false
SET scl_file.is_deleted = true
where scl_file.id = ?
and scl_file.type = ?
""";
} else {
sql = """
delete from scl_file
where scl_file.id = ?
and scl_file.type = ?
""";
}

try (var connection = dataSource.getConnection();
var stmt = connection.prepareStatement(sql)) {
Expand All @@ -328,16 +337,28 @@ public void delete(SclFileType type, UUID id) {

@Override
@Transactional(REQUIRED)
public void delete(SclFileType type, UUID id, Version version) {
var sql = """
public void delete(SclFileType type, UUID id, Version version, boolean softDelete) {
String sql;
if (softDelete) {
sql = """
UPDATE scl_file
SET scl_file.is_deleted = false
SET scl_file.is_deleted = true
where scl_file.id = ?
and scl_file.type = ?
and scl_file.major_version = ?
and scl_file.minor_version = ?
and scl_file.patch_version = ?
""";
} else {
sql = """
delete from scl_file
where scl_file.id = ?
and scl_file.type = ?
and scl_file.major_version = ?
and scl_file.minor_version = ?
and scl_file.patch_version = ?
""";
}

try (var connection = dataSource.getConnection();
var stmt = connection.prepareStatement(sql)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public interface CompasSclDataRepository {
* @param type The type of SCL where to find the SCL File
* @param id The ID of the SCL File to delete.
*/
void delete(SclFileType type, UUID id);
void delete(SclFileType type, UUID id, boolean softDelete);

/**
* Delete passed versions for a specific SCL File using its ID.
Expand All @@ -100,5 +100,5 @@ public interface CompasSclDataRepository {
* @param id The ID of the SCL File to delete.
* @param version The version of that SCL File to delete.
*/
void delete(SclFileType type, UUID id, Version version);
void delete(SclFileType type, UUID id, Version version, boolean softDelete);
}
4 changes: 4 additions & 0 deletions service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ SPDX-License-Identifier: Apache-2.0
<artifactId>log4j-core</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.eclipse.microprofile.config</groupId>
<artifactId>microprofile-config-api</artifactId>
</dependency>

<!-- Test Dependencies -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,15 @@ public class CompasSclDataService {
private final CompasSclDataRepository repository;
private final ElementConverter converter;
private final SclElementProcessor sclElementProcessor;
private final FeatureFlagsConfiguration featureFlagsConfiguration;

@Inject
public CompasSclDataService(CompasSclDataRepository repository, ElementConverter converter,
SclElementProcessor sclElementProcessor) {
SclElementProcessor sclElementProcessor, FeatureFlagsConfiguration featureFlagsConfiguration) {
this.repository = repository;
this.converter = converter;
this.sclElementProcessor = sclElementProcessor;
this.featureFlagsConfiguration = featureFlagsConfiguration;
}

/**
Expand Down Expand Up @@ -215,7 +217,7 @@ public String update(SclFileType type, UUID id, ChangeSetType changeSetType, Str
*/
@Transactional(REQUIRED)
public void delete(SclFileType type, UUID id) {
repository.delete(type, id);
repository.delete(type, id, featureFlagsConfiguration.isSoftDeleteEnabled());
}

/**
Expand All @@ -227,7 +229,7 @@ public void delete(SclFileType type, UUID id) {
*/
@Transactional(REQUIRED)
public void delete(SclFileType type, UUID id, Version version) {
repository.delete(type, id, version);
repository.delete(type, id, version, featureFlagsConfiguration.isSoftDeleteEnabled());
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.lfenergy.compas.scl.data.service;

import jakarta.enterprise.context.ApplicationScoped;
import org.eclipse.microprofile.config.inject.ConfigProperty;

@ApplicationScoped
public class FeatureFlagsConfiguration {

@ConfigProperty(name = "compas.scl-data-service.features.soft-delete-enabled", defaultValue = "false")
boolean softDeleteEnabled;

public boolean isSoftDeleteEnabled() {
return softDeleteEnabled;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@ class CompasSclDataServiceTest {

private final ElementConverter converter = new ElementConverter();
private final SclElementProcessor processor = new SclElementProcessor();
private final FeatureFlagsConfiguration featureFlagsConfiguration = new FeatureFlagsConfiguration();

@BeforeEach
void beforeEach() {
compasSclDataService = new CompasSclDataService(compasSclDataRepository, converter, processor);
compasSclDataService = new CompasSclDataService(compasSclDataRepository, converter, processor, featureFlagsConfiguration);
}

@Test
Expand Down Expand Up @@ -304,23 +305,50 @@ void update_WhenCalledWithXMLStringWithoutSCL_ThenCompasExceptionThrown() {
void delete_WhenCalledWithoutVersion_ThenRepositoryIsCalled() {
var uuid = UUID.randomUUID();

doNothing().when(compasSclDataRepository).delete(SCL_TYPE, uuid);
doNothing().when(compasSclDataRepository).delete(SCL_TYPE, uuid, Boolean.FALSE);

featureFlagsConfiguration.softDeleteEnabled = Boolean.FALSE;
compasSclDataService.delete(SCL_TYPE, uuid);

verify(compasSclDataRepository).delete(SCL_TYPE, uuid);
verify(compasSclDataRepository).delete(SCL_TYPE, uuid, Boolean.FALSE);
}

@Test
void delete_WhenCalledWithVersion_ThenRepositoryIsCalled() {
var uuid = UUID.randomUUID();
var version = new Version(1, 0, 0);

doNothing().when(compasSclDataRepository).delete(SCL_TYPE, uuid, version);
doNothing().when(compasSclDataRepository).delete(SCL_TYPE, uuid, version, Boolean.FALSE);

featureFlagsConfiguration.softDeleteEnabled = Boolean.FALSE;
compasSclDataService.delete(SCL_TYPE, uuid, version);

verify(compasSclDataRepository).delete(SCL_TYPE, uuid, version);
verify(compasSclDataRepository).delete(SCL_TYPE, uuid, version, Boolean.FALSE);
}

@Test
void deleteWithSoftDelete_WhenCalledWithoutVersion_ThenRepositoryIsCalled() {
var uuid = UUID.randomUUID();

doNothing().when(compasSclDataRepository).delete(SCL_TYPE, uuid, Boolean.TRUE);

featureFlagsConfiguration.softDeleteEnabled = Boolean.TRUE;
compasSclDataService.delete(SCL_TYPE, uuid);

verify(compasSclDataRepository).delete(SCL_TYPE, uuid, Boolean.TRUE);
}

@Test
void deleteWithSoftDelete_WhenCalledWithVersion_ThenRepositoryIsCalled() {
var uuid = UUID.randomUUID();
var version = new Version(1, 0, 0);

doNothing().when(compasSclDataRepository).delete(SCL_TYPE, uuid, version, Boolean.TRUE);

featureFlagsConfiguration.softDeleteEnabled = Boolean.TRUE;
compasSclDataService.delete(SCL_TYPE, uuid, version);

verify(compasSclDataRepository).delete(SCL_TYPE, uuid, version, Boolean.TRUE);
}

@Test
Expand Down

0 comments on commit da2cece

Please sign in to comment.