feat: soft delete for interests and roles
A super admin can delete an interest or a role. Deleted rows leave every list, picker and filter, keep their names on existing leads, and keep their names reserved. A role held by any user, and the protected roles, cannot be deleted. Deletes are audited. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
This commit is contained in:
1 parent
0f83e9cc08
commit
0b201a51ca
25 files changed
+765
-9
No files matched your search
+23
@@ -0,0 +1,23 @@
|
||||
package com.solvway.services.counter.controller;
|
||||
|
||||
import com.solvway.services.counter.service.InterestService;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class InterestControllerDeleteTest {
|
||||
|
||||
@Mock
|
||||
private InterestService interestService;
|
||||
|
||||
@Test
|
||||
void deleteDelegatesToService() {
|
||||
new InterestController(interestService).delete(7L);
|
||||
|
||||
verify(interestService).delete(7L);
|
||||
}
|
||||
}
|
||||
+109
@@ -0,0 +1,109 @@
|
||||
package com.solvway.services.counter.service;
|
||||
|
||||
import com.solvway.services.counter.model.Interest;
|
||||
import com.solvway.services.counter.repo.InterestRepository;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/** Soft delete of interests: delete, 404s, and names of deleted interests staying reserved. */
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class InterestServiceDeleteTest {
|
||||
|
||||
@Mock
|
||||
private InterestRepository repository;
|
||||
|
||||
private InterestService service;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
service = new InterestService(repository);
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteStampsDeletedAt() {
|
||||
Interest row = interest(5L, "Cookware");
|
||||
when(repository.findById(5L)).thenReturn(Optional.of(row));
|
||||
|
||||
service.delete(5L);
|
||||
|
||||
assertThat(row.getDeletedAt()).isNotNull();
|
||||
verify(repository).save(row);
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteUnknownIs404() {
|
||||
when(repository.findById(9L)).thenReturn(Optional.empty());
|
||||
|
||||
assertThatThrownBy(() -> service.delete(9L))
|
||||
.isInstanceOf(ResponseStatusException.class)
|
||||
.hasFieldOrPropertyWithValue("statusCode", HttpStatus.NOT_FOUND);
|
||||
verify(repository, never()).save(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteAlreadyDeletedIs404() {
|
||||
Interest row = interest(5L, "Cookware");
|
||||
LocalDateTime when = LocalDateTime.of(2026, 1, 1, 0, 0);
|
||||
row.setDeletedAt(when);
|
||||
when(repository.findById(5L)).thenReturn(Optional.of(row));
|
||||
|
||||
assertThatThrownBy(() -> service.delete(5L))
|
||||
.isInstanceOf(ResponseStatusException.class)
|
||||
.hasFieldOrPropertyWithValue("statusCode", HttpStatus.NOT_FOUND);
|
||||
assertThat(row.getDeletedAt()).isEqualTo(when);
|
||||
verify(repository, never()).save(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createWithDeletedNameIsRefusedAsNameDeleted() {
|
||||
when(repository.existsByNameIgnoreCase("Cookware")).thenReturn(true);
|
||||
when(repository.existsByNameIgnoreCaseAndDeletedAtIsNotNull("Cookware")).thenReturn(true);
|
||||
|
||||
assertThatThrownBy(() -> service.save(interest(null, "Cookware")))
|
||||
.isInstanceOf(ResponseStatusException.class)
|
||||
.hasFieldOrPropertyWithValue("statusCode", HttpStatus.CONFLICT)
|
||||
.hasMessageContaining("INTEREST_NAME_DELETED");
|
||||
}
|
||||
|
||||
@Test
|
||||
void renameToDeletedNameIsRefusedAsNameDeleted() {
|
||||
when(repository.findById(10L)).thenReturn(Optional.of(interest(10L, "Old")));
|
||||
when(repository.existsByNameIgnoreCase("Cookware")).thenReturn(true);
|
||||
when(repository.existsByNameIgnoreCaseAndDeletedAtIsNotNull("Cookware")).thenReturn(true);
|
||||
|
||||
assertThatThrownBy(() -> service.save(interest(10L, "Cookware")))
|
||||
.isInstanceOf(ResponseStatusException.class)
|
||||
.hasMessageContaining("INTEREST_NAME_DELETED");
|
||||
}
|
||||
|
||||
@Test
|
||||
void liveDuplicateStaysDuplicateName() {
|
||||
when(repository.existsByNameIgnoreCase("Cookware")).thenReturn(true);
|
||||
|
||||
assertThatThrownBy(() -> service.save(interest(null, "Cookware")))
|
||||
.isInstanceOf(ResponseStatusException.class)
|
||||
.hasMessageContaining("DUPLICATE_NAME");
|
||||
}
|
||||
|
||||
private static Interest interest(Long id, String name) {
|
||||
Interest i = new Interest();
|
||||
i.setId(id);
|
||||
i.setName(name);
|
||||
return i;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user