feat(services-counter): store each company's interest list
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
4c853c428e
commit
b75d636886
8 files changed
+598
-1
No files matched your search
@@ -15,7 +15,7 @@ Design: `design.md` (decision numbers below refer to it). Every task runs serial
|
||||
|
||||
## 2. services-counter
|
||||
|
||||
- [ ] 2.1 Add the interests backend (design decision 8):
|
||||
- [x] 2.1 Add the interests backend (design decision 8):
|
||||
- `Interest` entity and repository.
|
||||
- `GET /counter/interest/findByCompany/{companyId}`, ordered by sort order then name.
|
||||
- `POST /counter/interest/save`, which creates or updates by id within the company.
|
||||
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
package com.solvway.services.counter.controller;
|
||||
|
||||
import com.solvway.services.counter.dto.InterestFindByIdsRequest;
|
||||
import com.solvway.services.counter.model.Interest;
|
||||
import com.solvway.services.counter.service.InterestService;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/counter/interest")
|
||||
@AllArgsConstructor
|
||||
public class InterestController {
|
||||
private final InterestService interestService;
|
||||
|
||||
@GetMapping("/findByCompany/{companyId}")
|
||||
public List<Interest> findByCompany(@PathVariable("companyId") long companyId) {
|
||||
return interestService.findByCompany(companyId);
|
||||
}
|
||||
|
||||
@PostMapping("/save")
|
||||
public Interest save(@RequestBody Interest interest) {
|
||||
return interestService.save(interest);
|
||||
}
|
||||
|
||||
@PostMapping("/findByIds")
|
||||
public List<Interest> findByIds(@RequestBody InterestFindByIdsRequest body) {
|
||||
return interestService.findByIds(body.companyId(), body.ids());
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
package com.solvway.services.counter.dto;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public record InterestFindByIdsRequest(
|
||||
long companyId,
|
||||
List<Long> ids
|
||||
) {}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
package com.solvway.services.counter.model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@Entity
|
||||
@Table(name = "interest", uniqueConstraints = @UniqueConstraint(columnNames = {"company_id", "name"}))
|
||||
public class Interest {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
private long companyId;
|
||||
|
||||
private String name;
|
||||
|
||||
private boolean active = true;
|
||||
|
||||
private int sortOrder;
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package com.solvway.services.counter.repo;
|
||||
|
||||
import com.solvway.services.counter.model.Interest;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
@Repository
|
||||
public interface InterestRepository extends JpaRepository<Interest, Long> {
|
||||
List<Interest> findByCompanyIdOrderBySortOrderAscNameAsc(long companyId);
|
||||
|
||||
boolean existsByCompanyIdAndNameIgnoreCase(long companyId, String name);
|
||||
|
||||
List<Interest> findByIdInAndCompanyId(Collection<Long> ids, long companyId);
|
||||
}
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
package com.solvway.services.counter.service;
|
||||
|
||||
import com.solvway.services.counter.model.Interest;
|
||||
import com.solvway.services.counter.repo.InterestRepository;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.dao.DataIntegrityViolationException;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
@AllArgsConstructor
|
||||
public class InterestService {
|
||||
private final InterestRepository interestRepository;
|
||||
|
||||
public List<Interest> findByCompany(long companyId) {
|
||||
return interestRepository.findByCompanyIdOrderBySortOrderAscNameAsc(companyId);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Interest save(Interest interest) {
|
||||
String name = interest.getName();
|
||||
String trimmedName = name != null ? name.trim() : null;
|
||||
interest.setName(trimmedName);
|
||||
|
||||
// Validate name
|
||||
if (trimmedName == null || trimmedName.isEmpty()) {
|
||||
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "REQUIRED_FIELD");
|
||||
}
|
||||
if (trimmedName.length() > 100) {
|
||||
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "TOO_LONG");
|
||||
}
|
||||
|
||||
try {
|
||||
if (interest.getId() == null) {
|
||||
// Create: check duplicate name for this company
|
||||
if (interestRepository.existsByCompanyIdAndNameIgnoreCase(interest.getCompanyId(), trimmedName)) {
|
||||
throw new ResponseStatusException(HttpStatus.CONFLICT, "DUPLICATE_NAME");
|
||||
}
|
||||
return interestRepository.saveAndFlush(interest);
|
||||
} else {
|
||||
// Update: verify the row belongs to this company
|
||||
Interest existing = interestRepository.findById(interest.getId())
|
||||
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND));
|
||||
if (existing.getCompanyId() != interest.getCompanyId()) {
|
||||
throw new ResponseStatusException(HttpStatus.NOT_FOUND);
|
||||
}
|
||||
// Check duplicate name within the same company, excluding this row
|
||||
if (!existing.getName().equalsIgnoreCase(trimmedName)
|
||||
&& interestRepository.existsByCompanyIdAndNameIgnoreCase(interest.getCompanyId(), trimmedName)) {
|
||||
throw new ResponseStatusException(HttpStatus.CONFLICT, "DUPLICATE_NAME");
|
||||
}
|
||||
return interestRepository.saveAndFlush(interest);
|
||||
}
|
||||
} catch (DataIntegrityViolationException e) {
|
||||
throw new ResponseStatusException(HttpStatus.CONFLICT, "DUPLICATE_NAME");
|
||||
}
|
||||
}
|
||||
|
||||
public List<Interest> findByIds(long companyId, List<Long> ids) {
|
||||
if (ids == null || ids.isEmpty()) {
|
||||
return List.of();
|
||||
}
|
||||
return interestRepository.findByIdInAndCompanyId(ids, companyId);
|
||||
}
|
||||
}
|
||||
+82
@@ -0,0 +1,82 @@
|
||||
package com.solvway.services.counter.controller;
|
||||
|
||||
import com.solvway.services.counter.dto.InterestFindByIdsRequest;
|
||||
import com.solvway.services.counter.model.Interest;
|
||||
import com.solvway.services.counter.service.InterestService;
|
||||
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 java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* InterestController delegates to service and returns results.
|
||||
*/
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class InterestControllerTest {
|
||||
|
||||
private static final long COMPANY_A = 1L;
|
||||
|
||||
@Mock
|
||||
private InterestService interestService;
|
||||
|
||||
private InterestController controller;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
controller = new InterestController(interestService);
|
||||
}
|
||||
|
||||
@Test
|
||||
void findByCompanyDelegatesToService() {
|
||||
Interest row = makeInterest(1L, COMPANY_A, "tech", true, 0);
|
||||
when(interestService.findByCompany(COMPANY_A)).thenReturn(List.of(row));
|
||||
|
||||
List<Interest> result = controller.findByCompany(COMPANY_A);
|
||||
|
||||
assertThat(result).hasSize(1).first().isSameAs(row);
|
||||
verify(interestService).findByCompany(COMPANY_A);
|
||||
}
|
||||
|
||||
@Test
|
||||
void saveDelegatesToService() {
|
||||
Interest input = makeInterest(null, COMPANY_A, "tech", true, 0);
|
||||
Interest saved = makeInterest(99L, COMPANY_A, "tech", true, 0);
|
||||
when(interestService.save(input)).thenReturn(saved);
|
||||
|
||||
Interest result = controller.save(input);
|
||||
|
||||
assertThat(result.getId()).isEqualTo(99L);
|
||||
verify(interestService).save(input);
|
||||
}
|
||||
|
||||
@Test
|
||||
void findByIdsDelegatesToService() {
|
||||
Interest row = makeInterest(5L, COMPANY_A, "filter", true, 0);
|
||||
InterestFindByIdsRequest body = new InterestFindByIdsRequest(COMPANY_A, List.of(5L));
|
||||
when(interestService.findByIds(COMPANY_A, List.of(5L))).thenReturn(List.of(row));
|
||||
|
||||
List<Interest> result = controller.findByIds(body);
|
||||
|
||||
assertThat(result).hasSize(1).first().isSameAs(row);
|
||||
verify(interestService).findByIds(COMPANY_A, List.of(5L));
|
||||
}
|
||||
|
||||
private static Interest makeInterest(Long id, long companyId, String name, boolean active, int sortOrder) {
|
||||
Interest i = new Interest();
|
||||
if (id != null) {
|
||||
i.setId(id);
|
||||
}
|
||||
i.setCompanyId(companyId);
|
||||
i.setName(name);
|
||||
i.setActive(active);
|
||||
i.setSortOrder(sortOrder);
|
||||
return i;
|
||||
}
|
||||
}
|
||||
+367
@@ -0,0 +1,367 @@
|
||||
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.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.dao.DataIntegrityViolationException;
|
||||
|
||||
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.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* InterestService — findByCompany ordering, save create/update/duplicate, findByIds.
|
||||
*/
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class InterestServiceTest {
|
||||
|
||||
private static final long COMPANY_A = 1L;
|
||||
private static final long COMPANY_B = 2L;
|
||||
|
||||
@Mock
|
||||
private InterestRepository repository;
|
||||
|
||||
private InterestService service;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
service = new InterestService(repository);
|
||||
}
|
||||
|
||||
// --- Test 1: findByCompany orders by sortOrder asc, then name asc ---
|
||||
@Test
|
||||
void findByCompanyOrdersBySortOrderThenName() {
|
||||
Interest b = makeInterest(2L, COMPANY_A, "banana", true, 5);
|
||||
Interest a = makeInterest(1L, COMPANY_A, "apple", true, 10);
|
||||
Interest c = makeInterest(3L, COMPANY_A, "apricot", true, 10);
|
||||
when(repository.findByCompanyIdOrderBySortOrderAscNameAsc(COMPANY_A))
|
||||
.thenReturn(List.of(b, a, c));
|
||||
|
||||
List<Interest> result = service.findByCompany(COMPANY_A);
|
||||
|
||||
// Repo method already orders; service passes through
|
||||
assertThat(result).hasSize(3);
|
||||
assertThat(result.get(0).getName()).isEqualTo("banana");
|
||||
assertThat(result.get(1).getName()).isEqualTo("apple");
|
||||
assertThat(result.get(2).getName()).isEqualTo("apricot");
|
||||
verify(repository).findByCompanyIdOrderBySortOrderAscNameAsc(COMPANY_A);
|
||||
}
|
||||
|
||||
// --- Test 2: save creates a new row (id is null) ---
|
||||
@Test
|
||||
void saveCreatesNewRow() {
|
||||
Interest input = new Interest();
|
||||
input.setCompanyId(COMPANY_A);
|
||||
input.setName(" tech ");
|
||||
input.setActive(true);
|
||||
input.setSortOrder(0);
|
||||
when(repository.existsByCompanyIdAndNameIgnoreCase(COMPANY_A, "tech")).thenReturn(false);
|
||||
when(repository.saveAndFlush(any())).thenAnswer(inv -> {
|
||||
Interest saved = inv.getArgument(0);
|
||||
saved.setId(99L);
|
||||
return saved;
|
||||
});
|
||||
|
||||
Interest result = service.save(input);
|
||||
|
||||
assertThat(result.getId()).isEqualTo(99L);
|
||||
assertThat(result.getName()).isEqualTo("tech");
|
||||
verify(repository).saveAndFlush(any());
|
||||
}
|
||||
|
||||
// --- Test 3: save updates existing row with matching company ---
|
||||
@Test
|
||||
void saveUpdatesOwnRow() {
|
||||
Interest existing = makeInterest(10L, COMPANY_A, "old", true, 1);
|
||||
when(repository.findById(10L)).thenReturn(Optional.of(existing));
|
||||
|
||||
Interest input = new Interest();
|
||||
input.setId(10L);
|
||||
input.setCompanyId(COMPANY_A);
|
||||
input.setName(" new ");
|
||||
input.setActive(false);
|
||||
input.setSortOrder(5);
|
||||
when(repository.existsByCompanyIdAndNameIgnoreCase(COMPANY_A, "new")).thenReturn(false);
|
||||
when(repository.saveAndFlush(any())).thenAnswer(inv -> inv.getArgument(0));
|
||||
|
||||
Interest result = service.save(input);
|
||||
|
||||
assertThat(result.getName()).isEqualTo("new");
|
||||
assertThat(result.isActive()).isFalse();
|
||||
assertThat(result.getSortOrder()).isEqualTo(5);
|
||||
verify(repository).saveAndFlush(any());
|
||||
}
|
||||
|
||||
// --- Test 4: save updates other company's row -> 404 ---
|
||||
@Test
|
||||
void saveUpdatesOtherCompanyRow() {
|
||||
Interest existing = makeInterest(10L, COMPANY_B, "other", true, 1);
|
||||
when(repository.findById(10L)).thenReturn(Optional.of(existing));
|
||||
|
||||
Interest input = new Interest();
|
||||
input.setId(10L);
|
||||
input.setCompanyId(COMPANY_A);
|
||||
input.setName(" mine ");
|
||||
|
||||
assertThatThrownBy(() -> service.save(input))
|
||||
.isInstanceOf(ResponseStatusException.class)
|
||||
.hasFieldOrPropertyWithValue("statusCode", HttpStatus.NOT_FOUND);
|
||||
}
|
||||
|
||||
// --- Test 5: save duplicate name ignoring case in same company -> 409 ---
|
||||
@Test
|
||||
void saveDuplicateNameCaseInsensitive() {
|
||||
Interest input = new Interest();
|
||||
input.setCompanyId(COMPANY_A);
|
||||
input.setName(" Tech ");
|
||||
when(repository.existsByCompanyIdAndNameIgnoreCase(eq(COMPANY_A), any())).thenReturn(true);
|
||||
|
||||
assertThatThrownBy(() -> service.save(input))
|
||||
.isInstanceOf(ResponseStatusException.class)
|
||||
.hasFieldOrPropertyWithValue("statusCode", HttpStatus.CONFLICT)
|
||||
.hasMessageContaining("DUPLICATE_NAME");
|
||||
}
|
||||
|
||||
// --- Test 6: same name in different company allowed ---
|
||||
@Test
|
||||
void sameNameDifferentCompanyAllowed() {
|
||||
Interest inputA = new Interest();
|
||||
inputA.setCompanyId(COMPANY_A);
|
||||
inputA.setName("tech");
|
||||
when(repository.existsByCompanyIdAndNameIgnoreCase(COMPANY_A, "tech")).thenReturn(false);
|
||||
when(repository.saveAndFlush(any())).thenAnswer(inv -> inv.getArgument(0));
|
||||
|
||||
Interest inputB = new Interest();
|
||||
inputB.setCompanyId(COMPANY_B);
|
||||
inputB.setName("tech");
|
||||
when(repository.existsByCompanyIdAndNameIgnoreCase(COMPANY_B, "tech")).thenReturn(false);
|
||||
when(repository.saveAndFlush(any())).thenAnswer(inv -> inv.getArgument(0));
|
||||
|
||||
// Company A: no duplicate -> succeeds
|
||||
service.save(inputA);
|
||||
// Company B: no duplicate for B -> succeeds
|
||||
service.save(inputB);
|
||||
|
||||
verify(repository).existsByCompanyIdAndNameIgnoreCase(COMPANY_A, "tech");
|
||||
verify(repository).existsByCompanyIdAndNameIgnoreCase(COMPANY_B, "tech");
|
||||
}
|
||||
|
||||
// --- Test 7: updating row with same name unchanged is allowed ---
|
||||
@Test
|
||||
void updateWithSameNameUnchanged() {
|
||||
Interest existing = makeInterest(10L, COMPANY_A, "tech", true, 1);
|
||||
when(repository.findById(10L)).thenReturn(Optional.of(existing));
|
||||
|
||||
Interest input = new Interest();
|
||||
input.setId(10L);
|
||||
input.setCompanyId(COMPANY_A);
|
||||
input.setName("tech");
|
||||
when(repository.saveAndFlush(any())).thenAnswer(inv -> inv.getArgument(0));
|
||||
|
||||
Interest result = service.save(input);
|
||||
|
||||
assertThat(result.getId()).isEqualTo(10L);
|
||||
verify(repository).saveAndFlush(any());
|
||||
}
|
||||
|
||||
// --- Test 8: update changing name that conflicts with another row -> 409 ---
|
||||
@Test
|
||||
void updateNameConflictsWithAnotherRow() {
|
||||
Interest existing = makeInterest(10L, COMPANY_A, "original", true, 1);
|
||||
when(repository.findById(10L)).thenReturn(Optional.of(existing));
|
||||
|
||||
Interest input = new Interest();
|
||||
input.setId(10L);
|
||||
input.setCompanyId(COMPANY_A);
|
||||
input.setName(" tech ");
|
||||
when(repository.existsByCompanyIdAndNameIgnoreCase(COMPANY_A, "tech")).thenReturn(true);
|
||||
|
||||
assertThatThrownBy(() -> service.save(input))
|
||||
.isInstanceOf(ResponseStatusException.class)
|
||||
.hasFieldOrPropertyWithValue("statusCode", HttpStatus.CONFLICT)
|
||||
.hasMessageContaining("DUPLICATE_NAME");
|
||||
}
|
||||
|
||||
// --- Test 9: findByIds with null ids returns empty ---
|
||||
@Test
|
||||
void findByIdsWithNullIdsReturnsEmpty() {
|
||||
List<Interest> result = service.findByIds(COMPANY_A, null);
|
||||
|
||||
assertThat(result).isEmpty();
|
||||
verify(repository, org.mockito.Mockito.never()).findByIdInAndCompanyId(any(), eq(COMPANY_A));
|
||||
}
|
||||
|
||||
// --- Test 10: findByIds with empty ids returns empty ---
|
||||
@Test
|
||||
void findByIdsWithEmptyIdsReturnsEmpty() {
|
||||
List<Interest> result = service.findByIds(COMPANY_A, Collections.emptyList());
|
||||
|
||||
assertThat(result).isEmpty();
|
||||
verify(repository, org.mockito.Mockito.never()).findByIdInAndCompanyId(any(), eq(COMPANY_A));
|
||||
}
|
||||
|
||||
// --- Test 11: findByIds returns only company's rows ---
|
||||
@Test
|
||||
void findByIdsFiltersByCompany() {
|
||||
Interest row = makeInterest(5L, COMPANY_A, "filter", true, 0);
|
||||
when(repository.findByIdInAndCompanyId(List.of(5L), COMPANY_A))
|
||||
.thenReturn(List.of(row));
|
||||
|
||||
List<Interest> result = service.findByIds(COMPANY_A, List.of(5L));
|
||||
|
||||
assertThat(result).hasSize(1).first().isSameAs(row);
|
||||
verify(repository).findByIdInAndCompanyId(List.of(5L), COMPANY_A);
|
||||
}
|
||||
|
||||
// --- Test 12: findByIds filters by company — row not in company ---
|
||||
@Test
|
||||
void findByIdsExcludesOtherCompany() {
|
||||
Interest row = makeInterest(5L, COMPANY_B, "other", true, 0);
|
||||
when(repository.findByIdInAndCompanyId(List.of(5L), COMPANY_A))
|
||||
.thenReturn(List.of());
|
||||
|
||||
List<Interest> result = service.findByIds(COMPANY_A, List.of(5L));
|
||||
|
||||
assertThat(result).isEmpty();
|
||||
verify(repository).findByIdInAndCompanyId(List.of(5L), COMPANY_A);
|
||||
}
|
||||
|
||||
// --- Test 13: creating interest with active left unset defaults to true ---
|
||||
@Test
|
||||
void defaultActiveIsTrue() {
|
||||
Interest input = new Interest();
|
||||
input.setCompanyId(COMPANY_A);
|
||||
input.setName("default");
|
||||
// active is left at JVM default (false)
|
||||
when(repository.existsByCompanyIdAndNameIgnoreCase(COMPANY_A, "default")).thenReturn(false);
|
||||
when(repository.saveAndFlush(any())).thenAnswer(inv -> {
|
||||
Interest saved = inv.getArgument(0);
|
||||
saved.setId(99L);
|
||||
return saved;
|
||||
});
|
||||
|
||||
Interest result = service.save(input);
|
||||
|
||||
assertThat(result.isActive()).isTrue();
|
||||
}
|
||||
|
||||
// --- Test 14: save then find returns saved interest with trimmed name ---
|
||||
@Test
|
||||
void saveThenFindByCompanyReturnsTrimmedName() {
|
||||
Interest input = new Interest();
|
||||
input.setCompanyId(COMPANY_A);
|
||||
input.setName(" tech ");
|
||||
when(repository.existsByCompanyIdAndNameIgnoreCase(COMPANY_A, "tech")).thenReturn(false);
|
||||
when(repository.saveAndFlush(any())).thenAnswer(inv -> {
|
||||
Interest saved = inv.getArgument(0);
|
||||
saved.setId(99L);
|
||||
return saved;
|
||||
});
|
||||
when(repository.findByCompanyIdOrderBySortOrderAscNameAsc(COMPANY_A))
|
||||
.thenAnswer(inv -> List.of(input));
|
||||
|
||||
service.save(input);
|
||||
|
||||
List<Interest> found = service.findByCompany(COMPANY_A);
|
||||
assertThat(found).hasSize(1);
|
||||
assertThat(found.get(0).getName()).isEqualTo("tech");
|
||||
assertThat(found.get(0).getId()).isEqualTo(99L);
|
||||
}
|
||||
|
||||
// --- Test 15: null name -> 400 REQUIRED_FIELD ---
|
||||
@Test
|
||||
void saveNullNameThrows400() {
|
||||
Interest input = new Interest();
|
||||
input.setCompanyId(COMPANY_A);
|
||||
input.setName(null);
|
||||
|
||||
assertThatThrownBy(() -> service.save(input))
|
||||
.isInstanceOf(ResponseStatusException.class)
|
||||
.hasFieldOrPropertyWithValue("statusCode", HttpStatus.BAD_REQUEST)
|
||||
.hasMessageContaining("REQUIRED_FIELD");
|
||||
}
|
||||
|
||||
// --- Test 16: blank name -> 400 REQUIRED_FIELD ---
|
||||
@Test
|
||||
void saveBlankNameThrows400() {
|
||||
Interest input = new Interest();
|
||||
input.setCompanyId(COMPANY_A);
|
||||
input.setName(" ");
|
||||
|
||||
assertThatThrownBy(() -> service.save(input))
|
||||
.isInstanceOf(ResponseStatusException.class)
|
||||
.hasFieldOrPropertyWithValue("statusCode", HttpStatus.BAD_REQUEST)
|
||||
.hasMessageContaining("REQUIRED_FIELD");
|
||||
}
|
||||
|
||||
// --- Test 17: name over 100 chars after trim -> 400 TOO_LONG ---
|
||||
@Test
|
||||
void saveNameTooLongThrows400() {
|
||||
Interest input = new Interest();
|
||||
input.setCompanyId(COMPANY_A);
|
||||
input.setName("a".repeat(101));
|
||||
|
||||
assertThatThrownBy(() -> service.save(input))
|
||||
.isInstanceOf(ResponseStatusException.class)
|
||||
.hasFieldOrPropertyWithValue("statusCode", HttpStatus.BAD_REQUEST)
|
||||
.hasMessageContaining("TOO_LONG");
|
||||
}
|
||||
|
||||
// --- Test 18: DataIntegrityViolationException on save -> 409 DUPLICATE_NAME ---
|
||||
@Test
|
||||
void saveDataIntegrityViolationReturns409() {
|
||||
Interest input = new Interest();
|
||||
input.setCompanyId(COMPANY_A);
|
||||
input.setName("raced");
|
||||
when(repository.existsByCompanyIdAndNameIgnoreCase(eq(COMPANY_A), any())).thenReturn(false);
|
||||
when(repository.saveAndFlush(any())).thenThrow(new DataIntegrityViolationException("duplicate key"));
|
||||
|
||||
assertThatThrownBy(() -> service.save(input))
|
||||
.isInstanceOf(ResponseStatusException.class)
|
||||
.hasFieldOrPropertyWithValue("statusCode", HttpStatus.CONFLICT)
|
||||
.hasMessageContaining("DUPLICATE_NAME");
|
||||
}
|
||||
|
||||
// --- Update race: the unique key fires at flush, which must still answer 409 ---
|
||||
@Test
|
||||
void updateRaceOnUniqueKeyReturns409() {
|
||||
Interest existing = makeInterest(10L, COMPANY_A, "original", true, 1);
|
||||
when(repository.findById(10L)).thenReturn(Optional.of(existing));
|
||||
when(repository.existsByCompanyIdAndNameIgnoreCase(COMPANY_A, "tech")).thenReturn(false);
|
||||
when(repository.saveAndFlush(any())).thenThrow(new DataIntegrityViolationException("duplicate key"));
|
||||
|
||||
Interest input = new Interest();
|
||||
input.setId(10L);
|
||||
input.setCompanyId(COMPANY_A);
|
||||
input.setName("tech");
|
||||
|
||||
assertThatThrownBy(() -> service.save(input))
|
||||
.isInstanceOf(ResponseStatusException.class)
|
||||
.hasFieldOrPropertyWithValue("statusCode", HttpStatus.CONFLICT)
|
||||
.hasMessageContaining("DUPLICATE_NAME");
|
||||
}
|
||||
|
||||
private static Interest makeInterest(Long id, long companyId, String name, boolean active, int sortOrder) {
|
||||
Interest i = new Interest();
|
||||
i.setId(id);
|
||||
i.setCompanyId(companyId);
|
||||
i.setName(name);
|
||||
i.setActive(active);
|
||||
i.setSortOrder(sortOrder);
|
||||
return i;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user