feat(services-counter): capture, match, list and edit event leads
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
b75d636886
commit
48507979bd
20 files changed
+1183
-1
No files matched your search
+71
@@ -0,0 +1,71 @@
|
||||
package com.solvway.services.counter.controller;
|
||||
|
||||
import com.solvway.services.counter.dto.EventLeadCaptureRequest;
|
||||
import com.solvway.services.counter.dto.EventLeadDetail;
|
||||
import com.solvway.services.counter.dto.EventLeadSearchRequest;
|
||||
import com.solvway.services.counter.dto.EventLeadUpdateRequest;
|
||||
import com.solvway.services.counter.dto.LeadMatch;
|
||||
import com.solvway.services.counter.dto.LeadMatchRequest;
|
||||
import com.solvway.services.counter.service.EventLeadService;
|
||||
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.data.domain.Page;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/** EventLeadController delegates to the service and returns its result. */
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class EventLeadControllerTest {
|
||||
|
||||
@Mock private EventLeadService service;
|
||||
|
||||
@Test
|
||||
void findMatchesDelegates() {
|
||||
LeadMatchRequest req = new LeadMatchRequest(42L, 1L, "+60123456789", null);
|
||||
List<LeadMatch> out = List.of(new LeadMatch(2L, "Aisyah", "+60123456789", null, null, null, null));
|
||||
when(service.findMatches(req)).thenReturn(out);
|
||||
|
||||
assertThat(new EventLeadController(service).findMatches(req)).isSameAs(out);
|
||||
}
|
||||
|
||||
@Test
|
||||
void captureReturnsTheCapturedEventLead() {
|
||||
EventLeadCaptureRequest req = mock(EventLeadCaptureRequest.class);
|
||||
EventLeadDetail out = mock(EventLeadDetail.class);
|
||||
when(service.capture(req)).thenReturn(out);
|
||||
|
||||
assertThat(new EventLeadController(service).capture(req)).isSameAs(out);
|
||||
}
|
||||
|
||||
@Test
|
||||
void findByUuidDelegates() {
|
||||
EventLeadDetail out = mock(EventLeadDetail.class);
|
||||
when(service.findByUuid(42L, 9L)).thenReturn(out);
|
||||
|
||||
assertThat(new EventLeadController(service).findByUuid(42L, 9L)).isSameAs(out);
|
||||
}
|
||||
|
||||
@Test
|
||||
void findByEventDelegates() {
|
||||
EventLeadSearchRequest req = new EventLeadSearchRequest(42L, 1L, null, null, null, null, null, 0, 20);
|
||||
Page<EventLeadDetail> out = Page.empty();
|
||||
when(service.search(req)).thenReturn(out);
|
||||
|
||||
assertThat(new EventLeadController(service).findByEvent(req)).isSameAs(out);
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateDelegates() {
|
||||
EventLeadUpdateRequest req = mock(EventLeadUpdateRequest.class);
|
||||
EventLeadDetail out = mock(EventLeadDetail.class);
|
||||
when(service.update(req)).thenReturn(out);
|
||||
|
||||
assertThat(new EventLeadController(service).update(req)).isSameAs(out);
|
||||
}
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
package com.solvway.services.counter.model;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Table;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Lead's column names that the default naming strategy gets wrong: mobileE164 would map to `mobilee164`,
|
||||
* and LEAD is a MySQL 8 reserved word, so the table name must stay quoted.
|
||||
*/
|
||||
class LeadMappingTest {
|
||||
|
||||
@Test
|
||||
void mobileMapsToMobileE164Column() throws Exception {
|
||||
Column column = Lead.class.getDeclaredField("mobileE164").getAnnotation(Column.class);
|
||||
assertThat(column).isNotNull();
|
||||
assertThat(column.name()).isEqualTo("mobile_e164");
|
||||
}
|
||||
|
||||
@Test
|
||||
void tableNameIsQuoted() {
|
||||
assertThat(Lead.class.getAnnotation(Table.class).name()).isEqualTo("`lead`");
|
||||
}
|
||||
|
||||
@Test
|
||||
void eventLeadInterestRowsAreAlwaysInsertedWithoutALookup() {
|
||||
// Rows are only ever inserted (replacing the set deletes in bulk first), so saveAll must persist, not merge.
|
||||
assertThat(new EventLeadInterest().isNew()).isTrue();
|
||||
}
|
||||
}
|
||||
+212
@@ -0,0 +1,212 @@
|
||||
package com.solvway.services.counter.service;
|
||||
|
||||
import com.solvway.services.counter.dto.EventLeadDetail;
|
||||
import com.solvway.services.counter.dto.EventLeadSearchRequest;
|
||||
import com.solvway.services.counter.dto.EventLeadUpdateRequest;
|
||||
import com.solvway.services.counter.model.EventLead;
|
||||
import com.solvway.services.counter.model.EventLeadInterest;
|
||||
import com.solvway.services.counter.model.EventLeadInterestId;
|
||||
import com.solvway.services.counter.model.Lead;
|
||||
import com.solvway.services.counter.repo.EventLeadInterestRepository;
|
||||
import com.solvway.services.counter.repo.EventLeadRepository;
|
||||
import com.solvway.services.counter.repo.LeadRepository;
|
||||
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.InOrder;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageImpl;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
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.ArgumentMatchers.anyCollection;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.inOrder;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/** EventLeadService: the event leads listing and the event lead edit. */
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class EventLeadListAndUpdateTest {
|
||||
|
||||
private static final long COMPANY = 42L;
|
||||
private static final long EVENT = 1000L;
|
||||
|
||||
@Mock private LeadRepository leadRepository;
|
||||
@Mock private EventLeadRepository eventLeadRepository;
|
||||
@Mock private EventLeadInterestRepository eventLeadInterestRepository;
|
||||
|
||||
private EventLeadService service;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
service = new EventLeadService(leadRepository, eventLeadRepository, eventLeadInterestRepository);
|
||||
}
|
||||
|
||||
@Test
|
||||
void listingPassesEveryFilterAndPagesNewestCaptureFirstWithIdTieBreak() {
|
||||
when(eventLeadRepository.search(eq(COMPANY), eq(EVENT), eq(11L), eq("HOT"), eq("OPEN"), eq(3L), eq("NEW"), any()))
|
||||
.thenReturn(Page.empty());
|
||||
|
||||
service.search(new EventLeadSearchRequest(COMPANY, EVENT, 11L, "HOT", "OPEN", 3L, "NEW", 2, 25));
|
||||
|
||||
ArgumentCaptor<Pageable> pageable = ArgumentCaptor.forClass(Pageable.class);
|
||||
verify(eventLeadRepository).search(eq(COMPANY), eq(EVENT), eq(11L), eq("HOT"), eq("OPEN"), eq(3L), eq("NEW"), pageable.capture());
|
||||
assertThat(pageable.getValue().getPageNumber()).isEqualTo(2);
|
||||
assertThat(pageable.getValue().getPageSize()).isEqualTo(25);
|
||||
assertThat(pageable.getValue().getSort().toString()).isEqualTo("capturedAt: DESC,id: DESC");
|
||||
}
|
||||
|
||||
@Test
|
||||
void blankFiltersAreIgnoredAndPageSizeIsBounded() {
|
||||
when(eventLeadRepository.search(eq(COMPANY), eq(EVENT), eq(null), eq(null), eq(null), eq(null), eq(null), any()))
|
||||
.thenReturn(Page.empty());
|
||||
|
||||
service.search(new EventLeadSearchRequest(COMPANY, EVENT, null, " ", "", null, null, -1, 1000));
|
||||
|
||||
ArgumentCaptor<Pageable> pageable = ArgumentCaptor.forClass(Pageable.class);
|
||||
verify(eventLeadRepository).search(eq(COMPANY), eq(EVENT), eq(null), eq(null), eq(null), eq(null), eq(null), pageable.capture());
|
||||
assertThat(pageable.getValue().getPageNumber()).isZero();
|
||||
assertThat(pageable.getValue().getPageSize()).isEqualTo(100);
|
||||
}
|
||||
|
||||
@Test
|
||||
void listingRowsCarryLeadDetailsAndInterestsFetchedInOneQuery() {
|
||||
EventLead a = eventLead(1L, 501L);
|
||||
EventLead b = eventLead(2L, 502L);
|
||||
Lead la = lead(501L, "Aisyah");
|
||||
Lead lb = lead(502L, "Ahmad");
|
||||
when(eventLeadRepository.search(eq(COMPANY), eq(EVENT), any(), any(), any(), any(), any(), any()))
|
||||
.thenReturn(new PageImpl<>(List.of(new Object[]{a, la}, new Object[]{b, lb}), PageRequest.of(0, 20), 2));
|
||||
when(eventLeadInterestRepository.findByIdEventLeadIdIn(anyCollection()))
|
||||
.thenReturn(List.of(row(1L, 3L), row(1L, 4L), row(2L, 3L)));
|
||||
|
||||
Page<EventLeadDetail> page = service.search(new EventLeadSearchRequest(COMPANY, EVENT, null, null, null, null, null, 0, 20));
|
||||
|
||||
assertThat(page.getTotalElements()).isEqualTo(2);
|
||||
assertThat(page.getContent()).extracting(EventLeadDetail::name).containsExactly("Aisyah", "Ahmad");
|
||||
assertThat(page.getContent().get(0).interestIds()).containsExactlyInAnyOrder(3L, 4L);
|
||||
assertThat(page.getContent().get(1).interestIds()).containsExactly(3L);
|
||||
verify(eventLeadInterestRepository).findByIdEventLeadIdIn(anyCollection());
|
||||
}
|
||||
|
||||
@Test
|
||||
void emptyPageSkipsTheInterestQuery() {
|
||||
when(eventLeadRepository.search(eq(COMPANY), eq(EVENT), any(), any(), any(), any(), any(), any())).thenReturn(Page.empty());
|
||||
|
||||
assertThat(service.search(new EventLeadSearchRequest(COMPANY, EVENT, null, null, null, null, null, 0, 20)).getContent()).isEmpty();
|
||||
verify(eventLeadInterestRepository, never()).findByIdEventLeadIdIn(anyCollection());
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateReplacesWorkingFieldsAndTheInterestSet() {
|
||||
EventLead el = eventLead(7L, 501L);
|
||||
when(eventLeadRepository.findByUuidAndCompanyId(el.getUuid(), COMPANY)).thenReturn(Optional.of(el));
|
||||
when(eventLeadRepository.saveAndFlush(el)).thenReturn(el);
|
||||
when(leadRepository.findByUuidAndCompanyId(501L, COMPANY)).thenReturn(Optional.of(lead(501L, "Aisyah")));
|
||||
|
||||
EventLeadDetail detail = service.update(new EventLeadUpdateRequest(COMPANY, el.getUuid(), 12L, "COLD", "CONTACTED",
|
||||
List.of(4L, 4L, 5L), LocalDate.of(2027, 1, 20), "called back", "sales2"));
|
||||
|
||||
assertThat(el.getSalespersonUserId()).isEqualTo(12L);
|
||||
assertThat(el.getTemperature()).isEqualTo("COLD");
|
||||
assertThat(el.getStatus()).isEqualTo("CONTACTED");
|
||||
assertThat(el.getNextFollowUpDate()).isEqualTo(LocalDate.of(2027, 1, 20));
|
||||
assertThat(el.getNotes()).isEqualTo("called back");
|
||||
assertThat(el.getUpdatedBy()).isEqualTo("sales2");
|
||||
assertThat(el.getUpdatedAt()).isNotNull();
|
||||
assertThat(detail.interestIds()).containsExactlyInAnyOrder(4L, 5L);
|
||||
|
||||
InOrder order = inOrder(eventLeadInterestRepository);
|
||||
order.verify(eventLeadInterestRepository).deleteByEventLeadId(7L);
|
||||
@SuppressWarnings("unchecked")
|
||||
ArgumentCaptor<List<EventLeadInterest>> rows = ArgumentCaptor.forClass(List.class);
|
||||
order.verify(eventLeadInterestRepository).saveAll(rows.capture());
|
||||
assertThat(rows.getValue()).extracting(r -> r.getId().getInterestId()).containsExactlyInAnyOrder(4L, 5L);
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateCanClearTemperatureSalespersonAndInterests() {
|
||||
EventLead el = eventLead(7L, 501L);
|
||||
el.setTemperature("HOT");
|
||||
el.setSalespersonUserId(11L);
|
||||
when(eventLeadRepository.findByUuidAndCompanyId(el.getUuid(), COMPANY)).thenReturn(Optional.of(el));
|
||||
when(eventLeadRepository.saveAndFlush(el)).thenReturn(el);
|
||||
when(leadRepository.findByUuidAndCompanyId(501L, COMPANY)).thenReturn(Optional.of(lead(501L, "Aisyah")));
|
||||
|
||||
service.update(new EventLeadUpdateRequest(COMPANY, el.getUuid(), null, null, "OPEN", List.of(), null, null, "sales2"));
|
||||
|
||||
assertThat(el.getTemperature()).isNull();
|
||||
assertThat(el.getSalespersonUserId()).isNull();
|
||||
verify(eventLeadInterestRepository).deleteByEventLeadId(7L);
|
||||
verify(eventLeadInterestRepository, never()).saveAll(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateOfUnknownOrOtherCompanyEventLeadIsNotFound() {
|
||||
when(eventLeadRepository.findByUuidAndCompanyId(9L, COMPANY)).thenReturn(Optional.empty());
|
||||
|
||||
assertThatThrownBy(() -> service.update(new EventLeadUpdateRequest(COMPANY, 9L, null, null, "OPEN", List.of(), null, null, "x")))
|
||||
.isInstanceOf(ResponseStatusException.class)
|
||||
.hasFieldOrPropertyWithValue("statusCode", HttpStatus.NOT_FOUND);
|
||||
verify(eventLeadInterestRepository, never()).deleteByEventLeadId(any(Long.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateWithoutStatusKeepsTheCurrentStatus() {
|
||||
EventLead el = eventLead(7L, 501L);
|
||||
el.setStatus("WON");
|
||||
when(eventLeadRepository.findByUuidAndCompanyId(el.getUuid(), COMPANY)).thenReturn(Optional.of(el));
|
||||
when(eventLeadRepository.saveAndFlush(el)).thenReturn(el);
|
||||
when(leadRepository.findByUuidAndCompanyId(501L, COMPANY)).thenReturn(Optional.of(lead(501L, "Aisyah")));
|
||||
|
||||
service.update(new EventLeadUpdateRequest(COMPANY, el.getUuid(), null, null, null, List.of(), null, null, "sales2"));
|
||||
|
||||
assertThat(el.getStatus()).isEqualTo("WON");
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateIsOneTransaction() throws Exception {
|
||||
assertThat(EventLeadService.class.getMethod("update", EventLeadUpdateRequest.class)
|
||||
.isAnnotationPresent(Transactional.class)).isTrue();
|
||||
}
|
||||
|
||||
private static EventLead eventLead(long id, long leadUuid) {
|
||||
EventLead e = new EventLead();
|
||||
e.setId(id);
|
||||
e.setCompanyId(COMPANY);
|
||||
e.setEventUuid(EVENT);
|
||||
e.setLeadUuid(leadUuid);
|
||||
e.setLeadType("NEW");
|
||||
e.setStatus("OPEN");
|
||||
return e;
|
||||
}
|
||||
|
||||
private static Lead lead(long uuid, String name) {
|
||||
Lead l = new Lead();
|
||||
l.setUuid(uuid);
|
||||
l.setCompanyId(COMPANY);
|
||||
l.setName(name);
|
||||
return l;
|
||||
}
|
||||
|
||||
private static EventLeadInterest row(long eventLeadId, long interestId) {
|
||||
EventLeadInterest r = new EventLeadInterest();
|
||||
r.setId(new EventLeadInterestId(eventLeadId, interestId));
|
||||
return r;
|
||||
}
|
||||
}
|
||||
+299
@@ -0,0 +1,299 @@
|
||||
package com.solvway.services.counter.service;
|
||||
|
||||
import com.solvway.services.counter.dto.EventLeadCaptureRequest;
|
||||
import com.solvway.services.counter.dto.EventLeadDetail;
|
||||
import com.solvway.services.counter.dto.LeadMatch;
|
||||
import com.solvway.services.counter.dto.LeadMatchRequest;
|
||||
import com.solvway.services.counter.model.EventLead;
|
||||
import com.solvway.services.counter.model.EventLeadInterest;
|
||||
import com.solvway.services.counter.model.EventLeadInterestId;
|
||||
import com.solvway.services.counter.model.Lead;
|
||||
import com.solvway.services.counter.repo.EventLeadInterestRepository;
|
||||
import com.solvway.services.counter.repo.EventLeadRepository;
|
||||
import com.solvway.services.counter.repo.LeadRepository;
|
||||
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 org.springframework.dao.DataIntegrityViolationException;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
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.ArgumentMatchers.anyCollection;
|
||||
import static org.mockito.ArgumentMatchers.anyLong;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/** EventLeadService: matching, capture (NEW / EXISTING, fill blanks only, already captured) and find. */
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class EventLeadServiceTest {
|
||||
|
||||
private static final long COMPANY = 42L;
|
||||
private static final long EVENT = 1000L;
|
||||
private static final long LEAD_UUID = 2000L;
|
||||
private static final String MOBILE = "+60123456789";
|
||||
private static final String EMAIL = "aisyah@example.com";
|
||||
|
||||
@Mock private LeadRepository leadRepository;
|
||||
@Mock private EventLeadRepository eventLeadRepository;
|
||||
@Mock private EventLeadInterestRepository eventLeadInterestRepository;
|
||||
|
||||
private EventLeadService service;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
service = new EventLeadService(leadRepository, eventLeadRepository, eventLeadInterestRepository);
|
||||
}
|
||||
|
||||
// --- findMatches ---
|
||||
|
||||
@Test
|
||||
void matchesByMobileOrEmailNewestFirstAtMostTwenty() {
|
||||
Lead a = lead(LEAD_UUID, "Aisyah", MOBILE, null, null);
|
||||
when(leadRepository.findMatches(COMPANY, MOBILE, EMAIL, PageRequest.of(0, 20))).thenReturn(List.of(a));
|
||||
|
||||
List<LeadMatch> matches = service.findMatches(new LeadMatchRequest(COMPANY, EVENT, MOBILE, EMAIL));
|
||||
|
||||
assertThat(matches).extracting(LeadMatch::leadUuid).containsExactly(LEAD_UUID);
|
||||
assertThat(matches.get(0).eventLeadUuid()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void blankMobileIsNotMatchedOn() {
|
||||
when(leadRepository.findMatches(eq(COMPANY), eq(null), eq(EMAIL), any())).thenReturn(List.of());
|
||||
|
||||
service.findMatches(new LeadMatchRequest(COMPANY, EVENT, " ", EMAIL));
|
||||
|
||||
verify(leadRepository).findMatches(eq(COMPANY), eq(null), eq(EMAIL), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void blankEmailIsNotMatchedOn() {
|
||||
when(leadRepository.findMatches(eq(COMPANY), eq(MOBILE), eq(null), any())).thenReturn(List.of());
|
||||
|
||||
service.findMatches(new LeadMatchRequest(COMPANY, EVENT, MOBILE, ""));
|
||||
|
||||
verify(leadRepository).findMatches(eq(COMPANY), eq(MOBILE), eq(null), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void bothBlankFindsNothingWithoutQuerying() {
|
||||
assertThat(service.findMatches(new LeadMatchRequest(COMPANY, EVENT, null, " "))).isEmpty();
|
||||
verify(leadRepository, never()).findMatches(anyLong(), any(), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void matchAlreadyCapturedAtThisEventCarriesItsEventLeadUuid() {
|
||||
Lead a = lead(LEAD_UUID, "Aisyah", MOBILE, null, null);
|
||||
Lead b = lead(LEAD_UUID + 1, "Ahmad", MOBILE, null, null);
|
||||
when(leadRepository.findMatches(eq(COMPANY), eq(MOBILE), eq(null), any())).thenReturn(List.of(a, b));
|
||||
EventLead captured = eventLead(LEAD_UUID, "NEW");
|
||||
when(eventLeadRepository.findByEventUuidAndLeadUuidIn(eq(EVENT), anyCollection())).thenReturn(List.of(captured));
|
||||
|
||||
List<LeadMatch> matches = service.findMatches(new LeadMatchRequest(COMPANY, EVENT, MOBILE, null));
|
||||
|
||||
assertThat(matches.get(0).eventLeadUuid()).isEqualTo(captured.getUuid());
|
||||
assertThat(matches.get(1).eventLeadUuid()).isNull();
|
||||
}
|
||||
|
||||
// --- capture ---
|
||||
|
||||
@Test
|
||||
void newPersonCreatesLeadAndNewEventLeadWithInterests() {
|
||||
when(leadRepository.saveAndFlush(any())).thenAnswer(inv -> inv.getArgument(0));
|
||||
when(eventLeadRepository.saveAndFlush(any())).thenAnswer(inv -> withId(inv.getArgument(0), 7L));
|
||||
|
||||
EventLeadDetail detail = service.capture(request(null, "Aisyah", MOBILE, EMAIL, "Acme", List.of(3L, 3L, 4L)));
|
||||
|
||||
ArgumentCaptor<Lead> lead = ArgumentCaptor.forClass(Lead.class);
|
||||
verify(leadRepository).saveAndFlush(lead.capture());
|
||||
assertThat(lead.getValue().getCompanyId()).isEqualTo(COMPANY);
|
||||
assertThat(lead.getValue().getName()).isEqualTo("Aisyah");
|
||||
assertThat(lead.getValue().getMobileE164()).isEqualTo(MOBILE);
|
||||
assertThat(lead.getValue().getEmailNormalised()).isEqualTo(EMAIL);
|
||||
assertThat(lead.getValue().getNotes()).isNull();
|
||||
assertThat(detail.leadType()).isEqualTo("NEW");
|
||||
assertThat(detail.status()).isEqualTo("OPEN");
|
||||
assertThat(detail.leadUuid()).isEqualTo(lead.getValue().getUuid());
|
||||
assertThat(detail.interestIds()).containsExactlyInAnyOrder(3L, 4L);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
ArgumentCaptor<List<EventLeadInterest>> rows = ArgumentCaptor.forClass(List.class);
|
||||
verify(eventLeadInterestRepository).saveAll(rows.capture());
|
||||
assertThat(rows.getValue()).extracting(r -> r.getId().getEventLeadId()).containsOnly(7L);
|
||||
assertThat(rows.getValue()).extracting(EventLeadInterest::getCompanyId).containsOnly(COMPANY);
|
||||
}
|
||||
|
||||
@Test
|
||||
void pickedLeadIsExistingAndOnlyBlankFieldsAreFilled() {
|
||||
Lead stored = lead(LEAD_UUID, "Aisyah", MOBILE, null, "Acme");
|
||||
stored.setNotes("met at expo");
|
||||
when(leadRepository.findByUuidAndCompanyId(LEAD_UUID, COMPANY)).thenReturn(Optional.of(stored));
|
||||
when(eventLeadRepository.findByEventUuidAndLeadUuid(EVENT, LEAD_UUID)).thenReturn(Optional.empty());
|
||||
when(eventLeadRepository.saveAndFlush(any())).thenAnswer(inv -> withId(inv.getArgument(0), 8L));
|
||||
|
||||
EventLeadDetail detail = service.capture(request(LEAD_UUID, "Aisyah Binti Ali", "+60199999999", EMAIL, "Other Co", List.of()));
|
||||
|
||||
assertThat(detail.leadType()).isEqualTo("EXISTING");
|
||||
assertThat(stored.getName()).isEqualTo("Aisyah");
|
||||
assertThat(stored.getMobileE164()).isEqualTo(MOBILE);
|
||||
assertThat(stored.getEmailNormalised()).isEqualTo(EMAIL);
|
||||
assertThat(stored.getCompanyName()).isEqualTo("Acme");
|
||||
assertThat(stored.getJobTitle()).isEqualTo("Buyer");
|
||||
assertThat(stored.getNotes()).isEqualTo("met at expo");
|
||||
assertThat(stored.getUpdatedBy()).isEqualTo("sales1");
|
||||
verify(leadRepository).saveAndFlush(stored);
|
||||
verify(eventLeadInterestRepository, never()).saveAll(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void alreadyCapturedAtThisEventIsRefusedAndNothingIsWritten() {
|
||||
Lead stored = lead(LEAD_UUID, "Aisyah", MOBILE, null, null);
|
||||
when(leadRepository.findByUuidAndCompanyId(LEAD_UUID, COMPANY)).thenReturn(Optional.of(stored));
|
||||
EventLead already = eventLead(LEAD_UUID, "NEW");
|
||||
long capturedUuid = already.getUuid();
|
||||
when(eventLeadRepository.findByEventUuidAndLeadUuid(EVENT, LEAD_UUID)).thenReturn(Optional.of(already));
|
||||
|
||||
assertThatThrownBy(() -> service.capture(request(LEAD_UUID, "Aisyah", MOBILE, EMAIL, null, List.of(3L))))
|
||||
.isInstanceOf(ResponseStatusException.class)
|
||||
.hasFieldOrPropertyWithValue("statusCode", HttpStatus.CONFLICT)
|
||||
.hasMessageContaining("ALREADY_CAPTURED")
|
||||
.satisfies(e -> assertThat(((ResponseStatusException) e).getBody().getProperties())
|
||||
.containsEntry("eventLeadUuid", String.valueOf(capturedUuid)));
|
||||
verify(leadRepository, never()).saveAndFlush(any());
|
||||
verify(eventLeadRepository, never()).saveAndFlush(any());
|
||||
verify(eventLeadInterestRepository, never()).saveAll(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void concurrentCaptureHittingTheUniqueKeyIsAlreadyCaptured() {
|
||||
Lead stored = lead(LEAD_UUID, "Aisyah", MOBILE, null, null);
|
||||
when(leadRepository.findByUuidAndCompanyId(LEAD_UUID, COMPANY)).thenReturn(Optional.of(stored));
|
||||
when(eventLeadRepository.findByEventUuidAndLeadUuid(EVENT, LEAD_UUID)).thenReturn(Optional.empty());
|
||||
when(eventLeadRepository.saveAndFlush(any())).thenThrow(new DataIntegrityViolationException("uk_event_lead_event_lead"));
|
||||
|
||||
assertThatThrownBy(() -> service.capture(request(LEAD_UUID, "Aisyah", MOBILE, null, null, List.of())))
|
||||
.isInstanceOf(ResponseStatusException.class)
|
||||
.hasFieldOrPropertyWithValue("statusCode", HttpStatus.CONFLICT)
|
||||
.hasMessageContaining("ALREADY_CAPTURED");
|
||||
}
|
||||
|
||||
@Test
|
||||
void leadOfAnotherCompanyIsNotFound() {
|
||||
when(leadRepository.findByUuidAndCompanyId(LEAD_UUID, COMPANY)).thenReturn(Optional.empty());
|
||||
|
||||
assertThatThrownBy(() -> service.capture(request(LEAD_UUID, "Aisyah", MOBILE, null, null, List.of())))
|
||||
.isInstanceOf(ResponseStatusException.class)
|
||||
.hasFieldOrPropertyWithValue("statusCode", HttpStatus.NOT_FOUND);
|
||||
verify(eventLeadRepository, never()).saveAndFlush(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void captureIsOneTransaction() throws Exception {
|
||||
assertThat(EventLeadService.class.getMethod("capture", EventLeadCaptureRequest.class)
|
||||
.isAnnotationPresent(Transactional.class)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void mobileAndEmailMatchingDifferentLeadsReturnsBothUnmerged() {
|
||||
Lead byMobile = lead(LEAD_UUID, "A", MOBILE, null, null);
|
||||
Lead byEmail = lead(LEAD_UUID + 1, "B", null, "b@example.com", null);
|
||||
when(leadRepository.findMatches(eq(COMPANY), eq(MOBILE), eq("b@example.com"), any())).thenReturn(List.of(byEmail, byMobile));
|
||||
EventLead bHere = eventLead(LEAD_UUID + 1, "NEW");
|
||||
when(eventLeadRepository.findByEventUuidAndLeadUuidIn(eq(EVENT), anyCollection())).thenReturn(List.of(bHere));
|
||||
|
||||
List<LeadMatch> matches = service.findMatches(new LeadMatchRequest(COMPANY, EVENT, MOBILE, "b@example.com"));
|
||||
|
||||
assertThat(matches).extracting(LeadMatch::name).containsExactly("B", "A");
|
||||
assertThat(matches).extracting(LeadMatch::mobileE164).containsExactly(null, MOBILE);
|
||||
assertThat(matches).extracting(LeadMatch::email).containsExactly("b@example.com", null);
|
||||
assertThat(matches.get(0).eventLeadUuid()).isEqualTo(bHere.getUuid());
|
||||
assertThat(matches.get(1).eventLeadUuid()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void failureSavingInterestsPropagatesSoTheTransactionRollsBack() {
|
||||
when(leadRepository.saveAndFlush(any())).thenAnswer(inv -> inv.getArgument(0));
|
||||
when(eventLeadRepository.saveAndFlush(any())).thenAnswer(inv -> withId(inv.getArgument(0), 7L));
|
||||
RuntimeException boom = new org.springframework.dao.DataIntegrityViolationException("interest insert failed");
|
||||
when(eventLeadInterestRepository.saveAll(any())).thenThrow(boom);
|
||||
|
||||
assertThatThrownBy(() -> service.capture(request(null, "Aisyah", MOBILE, null, null, List.of(3L)))).isSameAs(boom);
|
||||
}
|
||||
|
||||
// --- findByUuid ---
|
||||
|
||||
@Test
|
||||
void findByUuidReturnsEventLeadWithLeadAndInterests() {
|
||||
EventLead el = withId(eventLead(LEAD_UUID, "EXISTING"), 9L);
|
||||
when(eventLeadRepository.findByUuidAndCompanyId(el.getUuid(), COMPANY)).thenReturn(Optional.of(el));
|
||||
when(leadRepository.findByUuidAndCompanyId(LEAD_UUID, COMPANY)).thenReturn(Optional.of(lead(LEAD_UUID, "Aisyah", MOBILE, EMAIL, "Acme")));
|
||||
EventLeadInterest row = new EventLeadInterest();
|
||||
row.setId(new EventLeadInterestId(9L, 3L));
|
||||
when(eventLeadInterestRepository.findByIdEventLeadId(9L)).thenReturn(List.of(row));
|
||||
|
||||
EventLeadDetail detail = service.findByUuid(COMPANY, el.getUuid());
|
||||
|
||||
assertThat(detail.name()).isEqualTo("Aisyah");
|
||||
assertThat(detail.companyName()).isEqualTo("Acme");
|
||||
assertThat(detail.leadType()).isEqualTo("EXISTING");
|
||||
assertThat(detail.eventUuid()).isEqualTo(EVENT);
|
||||
assertThat(detail.interestIds()).containsExactly(3L);
|
||||
}
|
||||
|
||||
@Test
|
||||
void findByUuidOfAnotherCompanyIsNotFound() {
|
||||
when(eventLeadRepository.findByUuidAndCompanyId(5L, COMPANY)).thenReturn(Optional.empty());
|
||||
|
||||
assertThatThrownBy(() -> service.findByUuid(COMPANY, 5L))
|
||||
.isInstanceOf(ResponseStatusException.class)
|
||||
.hasFieldOrPropertyWithValue("statusCode", HttpStatus.NOT_FOUND);
|
||||
}
|
||||
|
||||
// --- helpers ---
|
||||
|
||||
private static EventLeadCaptureRequest request(Long leadUuid, String name, String mobile, String email,
|
||||
String companyName, List<Long> interestIds) {
|
||||
return new EventLeadCaptureRequest(COMPANY, EVENT, leadUuid, name, mobile, email, companyName, "Buyer",
|
||||
11L, "HOT", null, interestIds, LocalDate.of(2027, 1, 20), "wants a demo", "sales1");
|
||||
}
|
||||
|
||||
private static Lead lead(long uuid, String name, String mobile, String email, String companyName) {
|
||||
Lead l = new Lead();
|
||||
l.setUuid(uuid);
|
||||
l.setCompanyId(COMPANY);
|
||||
l.setName(name);
|
||||
l.setMobileE164(mobile);
|
||||
l.setEmailNormalised(email);
|
||||
l.setCompanyName(companyName);
|
||||
return l;
|
||||
}
|
||||
|
||||
private static EventLead eventLead(long leadUuid, String type) {
|
||||
EventLead e = new EventLead();
|
||||
e.setCompanyId(COMPANY);
|
||||
e.setEventUuid(EVENT);
|
||||
e.setLeadUuid(leadUuid);
|
||||
e.setLeadType(type);
|
||||
e.setStatus("OPEN");
|
||||
return e;
|
||||
}
|
||||
|
||||
private static EventLead withId(EventLead e, long id) {
|
||||
e.setId(id);
|
||||
return e;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user