feat(services-counter): store each event's team of salespeople
GET /counter/eventSalesperson/findByEventUuid/{eventUuid} and
POST /counter/eventSalesperson/replace (bulk delete then insert, one transaction).
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
814c51da3d
commit
70e0651a75
8 files changed
+429
-1
No files matched your search
+75
@@ -0,0 +1,75 @@
|
||||
package com.solvway.services.counter.controller;
|
||||
|
||||
import com.solvway.services.counter.dto.EventSalespersonReplaceRequest;
|
||||
import com.solvway.services.counter.model.EventSalesperson;
|
||||
import com.solvway.services.counter.service.EventSalespersonService;
|
||||
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.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.anyLong;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* EventSalespersonController delegates to service and returns results.
|
||||
*/
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class EventSalespersonControllerTest {
|
||||
|
||||
private static final long EVENT_UUID = 100L;
|
||||
private static final long COMPANY_ID = 42L;
|
||||
private static final String DONE_BY = "admin";
|
||||
|
||||
@Mock private EventSalespersonService eventSalespersonService;
|
||||
|
||||
private EventSalespersonController controller;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
controller = new EventSalespersonController(eventSalespersonService);
|
||||
}
|
||||
|
||||
@Test
|
||||
void findByEventUuidDelegatesToService() {
|
||||
EventSalesperson row = makeSalesperson(1L, EVENT_UUID, COMPANY_ID, 10L);
|
||||
when(eventSalespersonService.findByEventUuid(EVENT_UUID)).thenReturn(List.of(row));
|
||||
|
||||
List<EventSalesperson> result = controller.findByEventUuid(EVENT_UUID);
|
||||
|
||||
assertThat(result).hasSize(1).first().isSameAs(row);
|
||||
verify(eventSalespersonService).findByEventUuid(EVENT_UUID);
|
||||
}
|
||||
|
||||
@Test
|
||||
void replacePassesBodyFieldsAndReturnsServiceResult() {
|
||||
EventSalesperson row = makeSalesperson(1L, EVENT_UUID, COMPANY_ID, 10L);
|
||||
EventSalespersonReplaceRequest body = new EventSalespersonReplaceRequest(
|
||||
EVENT_UUID, COMPANY_ID, List.of(10L), DONE_BY);
|
||||
when(eventSalespersonService.replace(eq(EVENT_UUID), eq(COMPANY_ID), eq(List.of(10L)), eq(DONE_BY)))
|
||||
.thenReturn(List.of(row));
|
||||
|
||||
List<EventSalesperson> result = controller.replace(body);
|
||||
|
||||
assertThat(result).hasSize(1).first().isSameAs(row);
|
||||
verify(eventSalespersonService).replace(EVENT_UUID, COMPANY_ID, List.of(10L), DONE_BY);
|
||||
}
|
||||
|
||||
private static EventSalesperson makeSalesperson(Long id, long eventUuid, long companyId, long userId) {
|
||||
EventSalesperson e = new EventSalesperson();
|
||||
e.setId(id);
|
||||
e.setEventUuid(eventUuid);
|
||||
e.setCompanyId(companyId);
|
||||
e.setUserId(userId);
|
||||
e.setCreateBy("admin");
|
||||
e.setCreateDateTime(LocalDateTime.now());
|
||||
return e;
|
||||
}
|
||||
}
|
||||
+223
@@ -0,0 +1,223 @@
|
||||
package com.solvway.services.counter.service;
|
||||
|
||||
import com.solvway.services.counter.model.EventSalesperson;
|
||||
import com.solvway.services.counter.repo.EventSalespersonRepository;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.jpa.repository.Modifying;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.inOrder;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* EventSalespersonService.replace() — delete-old, save-distinct, collapse-duplicates.
|
||||
*/
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class EventSalespersonServiceReplaceTest {
|
||||
|
||||
private static final long EVENT_UUID = 100L;
|
||||
private static final long COMPANY_ID = 42L;
|
||||
private static final String DONE_BY = "admin";
|
||||
|
||||
@Mock private EventSalespersonRepository repository;
|
||||
private EventSalespersonService service;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() throws Exception {
|
||||
service = new EventSalespersonService(repository);
|
||||
}
|
||||
|
||||
// --- Test 1: replace deletes existing rows before saving (InOrder) ---
|
||||
@Test
|
||||
void replaceDeletesBeforeSaving() {
|
||||
EventSalesperson row = makeSalesperson(1L, EVENT_UUID, COMPANY_ID, 10L, "admin");
|
||||
when(repository.saveAll(any())).thenReturn(List.of(row));
|
||||
|
||||
service.replace(EVENT_UUID, COMPANY_ID, List.of(10L), DONE_BY);
|
||||
|
||||
org.mockito.InOrder inOrder = inOrder(repository);
|
||||
inOrder.verify(repository).deleteByEventUuid(EVENT_UUID);
|
||||
inOrder.verify(repository).saveAll(any());
|
||||
}
|
||||
|
||||
// --- Test 2: saves one row per distinct user id ---
|
||||
@Test
|
||||
void replaceSavesOneRowPerDistinctId() {
|
||||
when(repository.saveAll(any()))
|
||||
.thenAnswer(inv -> {
|
||||
List<EventSalesperson> args = inv.getArgument(0);
|
||||
return args.stream()
|
||||
.map(e -> {
|
||||
EventSalesperson copy = new EventSalesperson();
|
||||
copy.setId(e.getUserId());
|
||||
copy.setEventUuid(EVENT_UUID);
|
||||
copy.setCompanyId(COMPANY_ID);
|
||||
copy.setUserId(e.getUserId());
|
||||
copy.setCreateBy(DONE_BY);
|
||||
copy.setCreateDateTime(e.getCreateDateTime());
|
||||
return copy;
|
||||
})
|
||||
.toList();
|
||||
});
|
||||
|
||||
List<EventSalesperson> result = service.replace(EVENT_UUID, COMPANY_ID, List.of(10L, 20L), DONE_BY);
|
||||
|
||||
ArgumentCaptor<List<EventSalesperson>> captor = ArgumentCaptor.forClass(List.class);
|
||||
verify(repository).saveAll(captor.capture());
|
||||
assertThat(captor.getValue()).hasSize(2);
|
||||
assertThat(result).hasSize(2);
|
||||
}
|
||||
|
||||
// --- Test 3: collapses duplicate user ids ---
|
||||
@Test
|
||||
void replaceCollapsesDuplicateIds() {
|
||||
when(repository.saveAll(any()))
|
||||
.thenAnswer(inv -> {
|
||||
List<EventSalesperson> args = inv.getArgument(0);
|
||||
return args.stream()
|
||||
.map(e -> {
|
||||
EventSalesperson copy = new EventSalesperson();
|
||||
copy.setId(1L);
|
||||
copy.setEventUuid(EVENT_UUID);
|
||||
copy.setCompanyId(COMPANY_ID);
|
||||
copy.setUserId(e.getUserId());
|
||||
copy.setCreateBy(DONE_BY);
|
||||
copy.setCreateDateTime(e.getCreateDateTime());
|
||||
return copy;
|
||||
})
|
||||
.toList();
|
||||
});
|
||||
|
||||
List<EventSalesperson> result = service.replace(EVENT_UUID, COMPANY_ID, List.of(10L, 10L, 10L), DONE_BY);
|
||||
|
||||
ArgumentCaptor<List<EventSalesperson>> captor = ArgumentCaptor.forClass(List.class);
|
||||
verify(repository).saveAll(captor.capture());
|
||||
assertThat(captor.getValue()).hasSize(1);
|
||||
assertThat(result).hasSize(1);
|
||||
}
|
||||
|
||||
// --- Test 4: null or empty list deletes and saves nothing ---
|
||||
@Test
|
||||
void replaceWithEmptyListDeletesAndSavesNothing() {
|
||||
service.replace(EVENT_UUID, COMPANY_ID, Collections.emptyList(), DONE_BY);
|
||||
|
||||
verify(repository).deleteByEventUuid(EVENT_UUID);
|
||||
verify(repository, never()).saveAll(any());
|
||||
}
|
||||
|
||||
// --- Test 5: replace method carries @Transactional ---
|
||||
@Test
|
||||
void replaceMethodHasTransactionalAnnotation() throws Exception {
|
||||
Method replaceMethod = EventSalespersonService.class.getDeclaredMethod(
|
||||
"replace", long.class, long.class, List.class, String.class);
|
||||
assertThat(replaceMethod.isAnnotationPresent(org.springframework.transaction.annotation.Transactional.class))
|
||||
.isTrue();
|
||||
}
|
||||
|
||||
// --- Test 6: findByEventUuid returns repository rows ---
|
||||
@Test
|
||||
void findByEventUuidReturnsRepositoryRows() {
|
||||
EventSalesperson row = makeSalesperson(1L, EVENT_UUID, COMPANY_ID, 10L, "admin");
|
||||
when(repository.findByEventUuid(EVENT_UUID)).thenReturn(List.of(row));
|
||||
|
||||
List<EventSalesperson> result = service.findByEventUuid(EVENT_UUID);
|
||||
|
||||
assertThat(result).hasSize(1).first().isSameAs(row);
|
||||
}
|
||||
|
||||
// --- Test 7: deleteByEventUuid carries a @Query whose value starts with "delete" ---
|
||||
@Test
|
||||
void deleteByEventUuidHasDeleteQuery() throws Exception {
|
||||
Method deleteMethod = EventSalespersonRepository.class
|
||||
.getDeclaredMethod("deleteByEventUuid", long.class);
|
||||
assertThat(deleteMethod.isAnnotationPresent(Modifying.class)).isTrue();
|
||||
Query queryAnn = deleteMethod.getAnnotation(Query.class);
|
||||
assertThat(queryAnn)
|
||||
.as("deleteByEventUuid must carry @Query to avoid Hibernate derived-delete flush-order issue")
|
||||
.isNotNull();
|
||||
assertThat(queryAnn.value())
|
||||
.as("@Query value must start with 'delete' so Hibernate runs it immediately, not after inserts")
|
||||
.startsWith("delete");
|
||||
}
|
||||
|
||||
// --- Test 8: replace with null userIds deletes existing rows and saves nothing ---
|
||||
@Test
|
||||
void replaceWithNullUserIds() {
|
||||
service.replace(EVENT_UUID, COMPANY_ID, null, DONE_BY);
|
||||
|
||||
verify(repository).deleteByEventUuid(EVENT_UUID);
|
||||
verify(repository, never()).saveAll(any());
|
||||
}
|
||||
|
||||
// --- Test 9: replace with [5, null, 5, 6] saves exactly users 5 and 6 ---
|
||||
@Test
|
||||
void replaceWithNullEntriesInList() {
|
||||
when(repository.saveAll(any()))
|
||||
.thenAnswer(inv -> {
|
||||
List<EventSalesperson> args = inv.getArgument(0);
|
||||
return args.stream()
|
||||
.map(e -> {
|
||||
EventSalesperson copy = new EventSalesperson();
|
||||
copy.setId(1L);
|
||||
copy.setEventUuid(EVENT_UUID);
|
||||
copy.setCompanyId(COMPANY_ID);
|
||||
copy.setUserId(e.getUserId());
|
||||
copy.setCreateBy(DONE_BY);
|
||||
copy.setCreateDateTime(e.getCreateDateTime());
|
||||
return copy;
|
||||
})
|
||||
.toList();
|
||||
});
|
||||
|
||||
List<EventSalesperson> result = service.replace(EVENT_UUID, COMPANY_ID, Arrays.asList(5L, null, 5L, 6L), DONE_BY);
|
||||
|
||||
ArgumentCaptor<List<EventSalesperson>> captor = ArgumentCaptor.forClass(List.class);
|
||||
verify(repository).saveAll(captor.capture());
|
||||
List<EventSalesperson> saved = captor.getValue();
|
||||
assertThat(saved).hasSize(2);
|
||||
assertThat(saved.get(0).getUserId()).isEqualTo(5L);
|
||||
assertThat(saved.get(1).getUserId()).isEqualTo(6L);
|
||||
assertThat(result).hasSize(2);
|
||||
}
|
||||
|
||||
// --- Test 10: createBy column has updatable = false ---
|
||||
@Test
|
||||
void createByHasUpdatableFalse() throws Exception {
|
||||
jakarta.persistence.Column colAnn = EventSalesperson.class
|
||||
.getDeclaredField("createBy")
|
||||
.getAnnotation(jakarta.persistence.Column.class);
|
||||
assertThat(colAnn)
|
||||
.as("createBy must have @Column(updatable = false) to prevent accidental overwrites")
|
||||
.isNotNull();
|
||||
assertThat(colAnn.updatable()).isFalse();
|
||||
}
|
||||
|
||||
private static EventSalesperson makeSalesperson(Long id, long eventUuid, long companyId, long userId, String createBy) {
|
||||
EventSalesperson e = new EventSalesperson();
|
||||
e.setId(id);
|
||||
e.setEventUuid(eventUuid);
|
||||
e.setCompanyId(companyId);
|
||||
e.setUserId(userId);
|
||||
e.setCreateBy(createBy);
|
||||
e.setCreateDateTime(LocalDateTime.now());
|
||||
return e;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user