feat: admin guest, invite email, check-in and attendance endpoints
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
4e938bb6a9
commit
87dfc48632
19 files changed
+1361
-7
No files matched your search
+444
@@ -0,0 +1,444 @@
|
||||
package com.solvway.mobile.api.service;
|
||||
|
||||
import com.solvway.core.model.Email;
|
||||
import com.solvway.core.model.Role;
|
||||
import com.solvway.core.model.User;
|
||||
import com.solvway.core.utils.HttpReqRespUtils;
|
||||
import com.solvway.mobile.api.dto.admin.AdminGuestSendRequest;
|
||||
import com.solvway.mobile.api.dto.admin.AdminGuestsRequest;
|
||||
import com.solvway.mobile.api.dto.admin.CompanyEvent;
|
||||
import com.solvway.mobile.api.errorhandler.CodedResponseStatusException;
|
||||
import com.solvway.mobile.api.useraccess.AdminCompanyScope;
|
||||
import com.solvway.mobile.api.util.RestTemplateCustomUtil;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.MockedStatic;
|
||||
import org.springframework.core.ParameterizedTypeReference;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.client.HttpClientErrorException;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
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.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.mockStatic;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||
class AdminEventGuestServiceTest {
|
||||
|
||||
private static final String EVENT = "/counter/event/findByUuid/{uuid}";
|
||||
private static final String USER = "/user/findById/{id}";
|
||||
private static final String LIST = "/counter/eventGuest/list";
|
||||
private static final String ADD = "/counter/eventGuest/add";
|
||||
private static final String REMOVE = "/counter/eventGuest/remove";
|
||||
private static final String MARK_SENT = "/counter/eventGuest/markSent";
|
||||
private static final String ENSURE_TOKEN = "/counter/checkin/ensureToken";
|
||||
private static final String ATTENDANCE = "/counter/event/attendance";
|
||||
private static final long EV = 3312726793093644288L;
|
||||
private static final long COMPANY = 5L;
|
||||
|
||||
private RestTemplateCustomUtil rest;
|
||||
private EmailService emails;
|
||||
private AdminAuthenticationService auth;
|
||||
private AdminEventGuestService service;
|
||||
private MockedStatic<HttpReqRespUtils> http;
|
||||
private User caller;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
rest = mock(RestTemplateCustomUtil.class);
|
||||
emails = mock(EmailService.class);
|
||||
auth = mock(AdminAuthenticationService.class);
|
||||
AdminCompanyScope scope = new AdminCompanyScope();
|
||||
service = new AdminEventGuestService(rest, new AdminEventLeadService(rest, auth, scope), emails, "https://portal.test/");
|
||||
when(rest.getURLforApiService(any())).thenAnswer(i -> i.getArgument(0));
|
||||
http = mockStatic(HttpReqRespUtils.class);
|
||||
http.when(HttpReqRespUtils::getJwtToken).thenReturn("tok");
|
||||
caller = new User();
|
||||
caller.setId(21L);
|
||||
caller.setUsername("boss");
|
||||
caller.setCompanyId(COMPANY);
|
||||
when(auth.loadUserByToken("tok")).thenReturn(caller);
|
||||
stubEvent(COMPANY);
|
||||
stubUser(21L, "Sales One");
|
||||
when(emails.send(any())).thenAnswer(i -> Optional.of(i.getArgument(0)));
|
||||
grantGuestsModule(true);
|
||||
}
|
||||
|
||||
private static final String MODULES = "/module/findAllByArea/{area}";
|
||||
|
||||
private void grantGuestsModule(boolean granted) {
|
||||
Role staff = new Role();
|
||||
staff.setId(3L);
|
||||
caller.setRoles(List.of(staff));
|
||||
when(rest.getForEntity(eq(MODULES), any(ParameterizedTypeReference.class), eq("BO")))
|
||||
.thenReturn(ResponseEntity.ok(List.of(Map.of("name", "BO_EVENT_GUESTS", "roles", List.of(Map.of("id", granted ? 3L : 99L))))));
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void close() { http.close(); }
|
||||
|
||||
private void stubEvent(long company) {
|
||||
CompanyEvent e = new CompanyEvent();
|
||||
e.setCompanyId(company);
|
||||
e.setUuid(EV);
|
||||
e.setName("Fair <b>&Co");
|
||||
e.setLocation("MITEC \"Hall\"");
|
||||
e.setDateStart(LocalDateTime.of(2027, 1, 15, 0, 0));
|
||||
e.setDateEnd(LocalDateTime.of(2027, 1, 17, 23, 59, 59));
|
||||
when(rest.getForEntity(eq(EVENT), any(ParameterizedTypeReference.class), eq(EV))).thenReturn(ResponseEntity.ok(e));
|
||||
}
|
||||
|
||||
private void stubUser(long id, String name) {
|
||||
Map<String, Object> u = new HashMap<>();
|
||||
u.put("id", id);
|
||||
u.put("name", name);
|
||||
when(rest.getForEntity(eq(USER), any(ParameterizedTypeReference.class), eq(id))).thenReturn(ResponseEntity.ok(u));
|
||||
}
|
||||
|
||||
private void stubGuests(Map<String, Object>... guests) {
|
||||
when(rest.postForEntity(eq(LIST), any(ParameterizedTypeReference.class), any())).thenReturn(ResponseEntity.ok(List.of(guests)));
|
||||
}
|
||||
|
||||
private static Map<String, Object> guest(long id, String name, String email, int sentCount) {
|
||||
Map<String, Object> g = new LinkedHashMap<>();
|
||||
g.put("id", id);
|
||||
g.put("name", name);
|
||||
g.put("email", email);
|
||||
g.put("token", "tok" + id);
|
||||
g.put("inviteSentCount", sentCount);
|
||||
g.put("salespersonUserId", 21L);
|
||||
return g;
|
||||
}
|
||||
|
||||
private Map<String, Object> sent(String api) {
|
||||
ArgumentCaptor<Object> c = ArgumentCaptor.forClass(Object.class);
|
||||
verify(rest).postForEntity(eq(api), any(ParameterizedTypeReference.class), c.capture());
|
||||
return (Map<String, Object>) c.getValue();
|
||||
}
|
||||
|
||||
private void assertNothingSent(String api) {
|
||||
verify(rest, never()).postForEntity(eq(api), any(ParameterizedTypeReference.class), any());
|
||||
}
|
||||
|
||||
private static AdminGuestsRequest.Guest line(String name, String mobile, String email) {
|
||||
return new AdminGuestsRequest.Guest(name, mobile, email, "Acme", 21L);
|
||||
}
|
||||
|
||||
private static void assertCoded(Runnable r, HttpStatus status, String code) {
|
||||
assertThatThrownBy(r::run).isInstanceOfSatisfying(CodedResponseStatusException.class, e -> {
|
||||
assertThat(e.getStatusCode()).isEqualTo(status);
|
||||
assertThat(e.getCode()).isEqualTo(code);
|
||||
});
|
||||
}
|
||||
|
||||
// --- scope ---
|
||||
|
||||
@Test
|
||||
void anotherCompanysEventIsNotFoundOnEveryOperation() {
|
||||
stubEvent(99L);
|
||||
AdminGuestsRequest one = new AdminGuestsRequest(List.of(line("Amy", "0123456789", null)));
|
||||
String ev = String.valueOf(EV);
|
||||
assertCoded(() -> service.list(ev), HttpStatus.NOT_FOUND, "EVENT_NOT_FOUND");
|
||||
assertCoded(() -> service.add(ev, one, false), HttpStatus.NOT_FOUND, "EVENT_NOT_FOUND");
|
||||
assertCoded(() -> service.remove(ev, "1"), HttpStatus.NOT_FOUND, "EVENT_NOT_FOUND");
|
||||
assertCoded(() -> service.send(ev, new AdminGuestSendRequest(null, true)), HttpStatus.NOT_FOUND, "EVENT_NOT_FOUND");
|
||||
assertCoded(() -> service.checkin(ev), HttpStatus.NOT_FOUND, "EVENT_NOT_FOUND");
|
||||
assertCoded(() -> service.attendance(ev), HttpStatus.NOT_FOUND, "EVENT_NOT_FOUND");
|
||||
assertNothingSent(LIST);
|
||||
assertNothingSent(ADD);
|
||||
assertNothingSent(REMOVE);
|
||||
assertNothingSent(ENSURE_TOKEN);
|
||||
assertNothingSent(ATTENDANCE);
|
||||
}
|
||||
|
||||
// --- list ---
|
||||
|
||||
@Test
|
||||
void list_resolvesSalespersonName() {
|
||||
stubGuests(guest(1, "Amy", "amy@x.co", 0));
|
||||
List<Map<String, Object>> out = service.list(String.valueOf(EV));
|
||||
assertThat(out).hasSize(1);
|
||||
assertThat(out.get(0)).containsEntry("name", "Amy").containsEntry("salespersonName", "Sales One");
|
||||
assertThat(sent(LIST)).containsEntry("companyId", COMPANY).containsEntry("eventUuid", EV);
|
||||
}
|
||||
|
||||
// --- add ---
|
||||
|
||||
@Test
|
||||
void add_normalisesValidLinesAndRejectsInvalidOnesWithFieldAndIndex() {
|
||||
when(rest.postForEntity(eq(ADD), any(ParameterizedTypeReference.class), any()))
|
||||
.thenReturn(ResponseEntity.ok(Map.of("added", List.of(Map.of("id", 1)), "rejected", List.of())));
|
||||
AdminGuestsRequest r = new AdminGuestsRequest(List.of(
|
||||
line(" Amy ", "0123456789", " Amy@X.co "),
|
||||
line(" ", "0123456789", null),
|
||||
line("Bob", null, null),
|
||||
line("Cat", "12345", null),
|
||||
line("Dan", null, "dan@"),
|
||||
line("E".repeat(101), "0123456789", null)));
|
||||
|
||||
Map<String, Object> out = service.add(String.valueOf(EV), r, false);
|
||||
|
||||
Map<String, Object> body = sent(ADD);
|
||||
assertThat(body).containsEntry("companyId", COMPANY).containsEntry("eventUuid", EV).containsEntry("doneBy", "boss");
|
||||
List<Map<String, Object>> guests = (List<Map<String, Object>>) body.get("guests");
|
||||
assertThat(guests).hasSize(1);
|
||||
assertThat(guests.get(0)).containsEntry("name", "Amy").containsEntry("mobileE164", "+60123456789")
|
||||
.containsEntry("email", "amy@x.co").containsEntry("companyName", "Acme").containsEntry("salespersonUserId", 21L);
|
||||
List<Map<String, Object>> rejected = (List<Map<String, Object>>) out.get("rejected");
|
||||
assertThat(rejected).extracting(m -> m.get("index")).containsExactly(1, 2, 3, 4, 5);
|
||||
assertThat(rejected).extracting(m -> m.get("field")).containsExactly("name", "mobile", "mobile", "email", "name");
|
||||
assertThat(rejected).extracting(m -> m.get("code")).containsExactly("REQUIRED_FIELD", "REQUIRED_FIELD", "INVALID_MOBILE", "INVALID_EMAIL", "TOO_LONG");
|
||||
assertThat(rejected).allSatisfy(m -> assertThat(m.get("message")).isNotNull());
|
||||
assertThat((List<?>) out.get("added")).hasSize(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
void add_mergesCounterRejectionsKeepingOriginalIndexes() {
|
||||
Map<String, Object> counterReject = Map.of("index", 1, "code", "DUPLICATE", "field", "mobile", "message", "already invited");
|
||||
when(rest.postForEntity(eq(ADD), any(ParameterizedTypeReference.class), any()))
|
||||
.thenReturn(ResponseEntity.ok(Map.of("added", List.of(Map.of("id", 1)), "rejected", List.of(counterReject))));
|
||||
// original indexes: 0 valid, 1 invalid (no name), 2 valid (counter sees it as index 1), 3 invalid
|
||||
AdminGuestsRequest r = new AdminGuestsRequest(List.of(
|
||||
line("Amy", "0123456789", null), line("", "0123456789", null), line("Bob", "0129876543", null), line("Cat", "x", null)));
|
||||
|
||||
Map<String, Object> out = service.add(String.valueOf(EV), r, false);
|
||||
|
||||
List<Map<String, Object>> rejected = (List<Map<String, Object>>) out.get("rejected");
|
||||
assertThat(rejected).extracting(m -> m.get("index")).containsExactly(1, 2, 3);
|
||||
assertThat(rejected.get(1)).containsEntry("code", "DUPLICATE").containsEntry("field", "mobile");
|
||||
}
|
||||
|
||||
@Test
|
||||
void add_preview_validatesOnlyAndSavesNothing() {
|
||||
AdminGuestsRequest r = new AdminGuestsRequest(List.of(line("Amy", "0123456789", null), line("", null, null)));
|
||||
|
||||
Map<String, Object> out = service.add(String.valueOf(EV), r, true);
|
||||
|
||||
assertNothingSent(ADD);
|
||||
assertThat((List<?>) out.get("added")).hasSize(1);
|
||||
assertThat((List<Map<String, Object>>) out.get("rejected")).extracting(m -> m.get("index")).containsExactly(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
void add_allInvalid_skipsTheCounterCall() {
|
||||
Map<String, Object> out = service.add(String.valueOf(EV), new AdminGuestsRequest(List.of(line("", null, null))), false);
|
||||
assertNothingSent(ADD);
|
||||
assertThat((List<?>) out.get("rejected")).hasSize(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
void add_emptyList_isBadRequest() {
|
||||
assertCoded(() -> service.add(String.valueOf(EV), new AdminGuestsRequest(List.of()), false), HttpStatus.BAD_REQUEST, "REQUIRED_FIELD");
|
||||
}
|
||||
|
||||
// --- remove ---
|
||||
|
||||
@Test
|
||||
void remove_registeredGuestIsConflict() {
|
||||
when(rest.postForEntity(eq(REMOVE), any(ParameterizedTypeReference.class), any()))
|
||||
.thenThrow(HttpClientErrorException.create(HttpStatus.CONFLICT, "Conflict", null, null, null));
|
||||
assertCoded(() -> service.remove(String.valueOf(EV), "9"), HttpStatus.CONFLICT, "GUEST_REGISTERED");
|
||||
}
|
||||
|
||||
@Test
|
||||
void remove_sendsCompanyEventGuestAndUser() {
|
||||
service.remove(String.valueOf(EV), "9");
|
||||
assertThat(sent(REMOVE)).containsEntry("companyId", COMPANY).containsEntry("eventUuid", EV).containsEntry("guestId", 9L).containsEntry("doneBy", "boss");
|
||||
}
|
||||
|
||||
// --- send ---
|
||||
|
||||
@Test
|
||||
void send_buildsOneBilingualEscapedEmailPerGuestAndMarksOnlyThoseSent() {
|
||||
stubGuests(guest(1, "Amy <script>", "amy@x.co", 0), guest(2, "Bob", "bob@x.co", 1), guest(3, "Cat", "cat@x.co", 0));
|
||||
|
||||
Map<String, Object> out = service.send(String.valueOf(EV), new AdminGuestSendRequest(List.of(1L, 3L), null));
|
||||
|
||||
ArgumentCaptor<Email> c = ArgumentCaptor.forClass(Email.class);
|
||||
verify(emails, times(2)).send(c.capture());
|
||||
Email first = c.getAllValues().get(0);
|
||||
assertThat(first.getCompanyId()).isEqualTo(COMPANY);
|
||||
assertThat(first.getRecipients()).extracting(r -> r.getRecipient()).containsExactly("amy@x.co");
|
||||
String body = first.getBody();
|
||||
assertThat(body).contains("https://portal.test/i/tok1").contains("Fair <b>&Co").contains("Amy <script>")
|
||||
.contains("MITEC "Hall"").contains("2027-01-15").contains("2027-01-17");
|
||||
assertThat(body).doesNotContain("<script>").doesNotContain("<b>");
|
||||
assertThat(body.indexOf("Bahasa Malaysia")).isGreaterThan(0);
|
||||
assertThat(body.indexOf("You are invited")).isLessThan(body.indexOf("Bahasa Malaysia"));
|
||||
assertThat(c.getAllValues().get(1).getBody()).contains("https://portal.test/i/tok3");
|
||||
assertThat(out).containsEntry("sent", 2).containsEntry("skippedNoEmail", 0);
|
||||
assertThat(sent(MARK_SENT)).containsEntry("companyId", COMPANY).containsEntry("eventUuid", EV).containsEntry("guestIds", List.of(1L, 3L))
|
||||
.containsEntry("doneBy", "boss");
|
||||
}
|
||||
|
||||
@Test
|
||||
void send_allUnsent_skipsAlreadySentAndGuestsWithoutEmail() {
|
||||
stubGuests(guest(1, "Amy", "amy@x.co", 0), guest(2, "Bob", "bob@x.co", 2), guest(3, "Cat", null, 0), guest(4, "Dan", " ", 0));
|
||||
|
||||
Map<String, Object> out = service.send(String.valueOf(EV), new AdminGuestSendRequest(null, true));
|
||||
|
||||
verify(emails, times(1)).send(any());
|
||||
assertThat(out).containsEntry("sent", 1).containsEntry("skippedNoEmail", 2);
|
||||
assertThat(sent(MARK_SENT)).containsEntry("guestIds", List.of(1L));
|
||||
}
|
||||
|
||||
@Test
|
||||
void send_failedEmailIsNotCountedNorMarked() {
|
||||
stubGuests(guest(1, "Amy", "amy@x.co", 0), guest(2, "Bob", "bob@x.co", 0));
|
||||
org.mockito.Mockito.doReturn(Optional.empty()).doAnswer(i -> Optional.of(i.getArgument(0))).when(emails).send(any());
|
||||
|
||||
Map<String, Object> out = service.send(String.valueOf(EV), new AdminGuestSendRequest(null, true));
|
||||
|
||||
assertThat(out).containsEntry("sent", 1);
|
||||
assertThat(sent(MARK_SENT)).containsEntry("guestIds", List.of(2L));
|
||||
}
|
||||
|
||||
@Test
|
||||
void send_nothingSent_doesNotCallMarkSent() {
|
||||
stubGuests(guest(1, "Amy", null, 0));
|
||||
Map<String, Object> out = service.send(String.valueOf(EV), new AdminGuestSendRequest(null, true));
|
||||
assertThat(out).containsEntry("sent", 0).containsEntry("skippedNoEmail", 1);
|
||||
assertNothingSent(MARK_SENT);
|
||||
verify(emails, never()).send(any());
|
||||
}
|
||||
|
||||
// --- checkin and attendance ---
|
||||
|
||||
@Test
|
||||
void checkin_returnsTheEnsuredToken() {
|
||||
when(rest.postForEntity(eq(ENSURE_TOKEN), any(ParameterizedTypeReference.class), any())).thenReturn(ResponseEntity.ok(Map.of("token", "abc")));
|
||||
assertThat(service.checkin(String.valueOf(EV))).isEqualTo(Map.of("token", "abc"));
|
||||
assertThat(sent(ENSURE_TOKEN)).containsEntry("companyId", COMPANY).containsEntry("eventUuid", EV);
|
||||
}
|
||||
|
||||
@Test
|
||||
void attendance_returnsTheCounterNumbers() {
|
||||
Map<String, Object> n = new LinkedHashMap<>(Map.of("invited", 5, "registered", 3, "attended", 2, "walkIns", 1, "total", 3));
|
||||
when(rest.postForEntity(eq(ATTENDANCE), any(ParameterizedTypeReference.class), any())).thenReturn(ResponseEntity.ok(n));
|
||||
assertThat(service.attendance(String.valueOf(EV))).isEqualTo(n);
|
||||
assertThat(sent(ATTENDANCE)).containsEntry("companyId", COMPANY).containsEntry("eventUuid", EV);
|
||||
}
|
||||
|
||||
// --- review fixes ---
|
||||
|
||||
@Test
|
||||
void list_withoutGuestsModule_stripsTokens_withModule_keepsThem() {
|
||||
stubGuests(guest(1, "Amy", "amy@x.co", 0));
|
||||
grantGuestsModule(false);
|
||||
assertThat(service.list(String.valueOf(EV)).get(0)).doesNotContainKey("token").containsEntry("name", "Amy");
|
||||
grantGuestsModule(true);
|
||||
assertThat(service.list(String.valueOf(EV)).get(0)).containsEntry("token", "tok1");
|
||||
}
|
||||
|
||||
@Test
|
||||
void list_superAdminKeepsTokens_failedModuleLookupStripsThem() {
|
||||
stubGuests(guest(1, "Amy", "amy@x.co", 0));
|
||||
Role superRole = new Role();
|
||||
superRole.setId(1L);
|
||||
caller.setCompanyId(null);
|
||||
caller.setRoles(List.of(superRole));
|
||||
assertThat(service.list(String.valueOf(EV)).get(0)).containsEntry("token", "tok1");
|
||||
grantGuestsModule(true);
|
||||
when(rest.getForEntity(eq(MODULES), any(ParameterizedTypeReference.class), eq("BO"))).thenThrow(new IllegalStateException("down"));
|
||||
caller.setCompanyId(COMPANY);
|
||||
assertThat(service.list(String.valueOf(EV)).get(0)).doesNotContainKey("token");
|
||||
}
|
||||
|
||||
@Test
|
||||
void send_readsTokensEvenWhenTheCallerHasNoModuleAndSkipsSalespersonLookup() {
|
||||
stubGuests(guest(1, "Amy", "amy@x.co", 0));
|
||||
grantGuestsModule(false);
|
||||
service.send(String.valueOf(EV), new AdminGuestSendRequest(List.of(1L), null));
|
||||
ArgumentCaptor<Email> c = ArgumentCaptor.forClass(Email.class);
|
||||
verify(emails).send(c.capture());
|
||||
assertThat(c.getValue().getBody()).contains("/i/tok1");
|
||||
verify(rest, never()).getForEntity(eq(USER), any(ParameterizedTypeReference.class), any());
|
||||
verify(rest, times(1)).getForEntity(eq(EVENT), any(ParameterizedTypeReference.class), eq(EV));
|
||||
}
|
||||
|
||||
@Test
|
||||
void add_companyNameOver150IsRejectedPerLine() {
|
||||
Map<String, Object> out = service.add(String.valueOf(EV), new AdminGuestsRequest(List.of(
|
||||
new AdminGuestsRequest.Guest("Amy", "0[login redacted]789", null, "C".repeat(151), 21L),
|
||||
new AdminGuestsRequest.Guest("Bob", "0129876543", null, "C".repeat(150), 21L))), true);
|
||||
List<Map<String, Object>> rejected = (List<Map<String, Object>>) out.get("rejected");
|
||||
assertThat(rejected).hasSize(1);
|
||||
assertThat(rejected.get(0)).containsEntry("index", 0).containsEntry("field", "companyName").containsEntry("code", "TOO_LONG");
|
||||
assertThat((List<?>) out.get("added")).hasSize(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
void add_noMobileNoEmail_namesBothFieldsLikeStaffCapture() {
|
||||
Map<String, Object> out = service.add(String.valueOf(EV), new AdminGuestsRequest(List.of(line("Bob", null, null))), true);
|
||||
Map<String, Object> r = ((List<Map<String, Object>>) out.get("rejected")).get(0);
|
||||
assertThat(r).containsEntry("field", "mobile").containsEntry("code", "REQUIRED_FIELD");
|
||||
assertThat((String) r.get("message")).contains("mobile").contains("email");
|
||||
}
|
||||
|
||||
@Test
|
||||
void add_counterRejectionIndexOutsideTheRequestIsIgnored() {
|
||||
Map<String, Object> bad = Map.of("index", 7, "code", "DUPLICATE", "field", "mobile", "message", "x");
|
||||
Map<String, Object> neg = Map.of("index", -1, "code", "DUPLICATE", "field", "mobile", "message", "x");
|
||||
when(rest.postForEntity(eq(ADD), any(ParameterizedTypeReference.class), any()))
|
||||
.thenReturn(ResponseEntity.ok(Map.of("added", List.of(Map.of("id", 1)), "rejected", List.of(bad, neg))));
|
||||
Map<String, Object> out = service.add(String.valueOf(EV), new AdminGuestsRequest(List.of(line("Amy", "0129876543", null))), false);
|
||||
assertThat((List<?>) out.get("rejected")).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void send_oneFailingGuestDoesNotStopTheOthersAndAllQueuedAreMarked() {
|
||||
stubGuests(guest(1, "Amy", "amy@x.co", 0), guest(2, "Bob", "bob@x.co", 0), guest(3, "Cat", "cat@x.co", 0));
|
||||
org.mockito.Mockito.doAnswer(i -> Optional.of(i.getArgument(0))).doThrow(new IllegalStateException("smtp down"))
|
||||
.doAnswer(i -> Optional.of(i.getArgument(0))).when(emails).send(any());
|
||||
Map<String, Object> out = service.send(String.valueOf(EV), new AdminGuestSendRequest(null, true));
|
||||
assertThat(out).containsEntry("sent", 2);
|
||||
assertThat(sent(MARK_SENT)).containsEntry("guestIds", List.of(1L, 3L));
|
||||
}
|
||||
|
||||
@Test
|
||||
void send_eventWithoutDatesStillSends() {
|
||||
stubGuests(guest(1, "Amy", "amy@x.co", 0));
|
||||
CompanyEvent e = new CompanyEvent();
|
||||
e.setCompanyId(COMPANY);
|
||||
e.setUuid(EV);
|
||||
e.setName("Open");
|
||||
when(rest.getForEntity(eq(EVENT), any(ParameterizedTypeReference.class), eq(EV))).thenReturn(ResponseEntity.ok(e));
|
||||
assertThat(service.send(String.valueOf(EV), new AdminGuestSendRequest(null, true))).containsEntry("sent", 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
void superAdmin_addAndSendUseTheEventsCompany() {
|
||||
Role superRole = new Role();
|
||||
superRole.setId(1L);
|
||||
caller.setCompanyId(null);
|
||||
caller.setRoles(List.of(superRole));
|
||||
stubEvent(8L);
|
||||
stubGuests(guest(1, "Amy", "amy@x.co", 0));
|
||||
when(rest.postForEntity(eq(ADD), any(ParameterizedTypeReference.class), any()))
|
||||
.thenReturn(ResponseEntity.ok(Map.of("added", List.of(Map.of("id", 1)), "rejected", List.of())));
|
||||
service.add(String.valueOf(EV), new AdminGuestsRequest(List.of(line("Amy", "0129876543", null))), false);
|
||||
assertThat(sent(ADD)).containsEntry("companyId", 8L);
|
||||
service.send(String.valueOf(EV), new AdminGuestSendRequest(null, true));
|
||||
assertThat(sent(MARK_SENT)).containsEntry("companyId", 8L);
|
||||
ArgumentCaptor<Email> c = ArgumentCaptor.forClass(Email.class);
|
||||
verify(emails).send(c.capture());
|
||||
assertThat(c.getValue().getCompanyId()).isEqualTo(8L);
|
||||
}
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
package com.solvway.mobile.api.service;
|
||||
|
||||
import com.solvway.mobile.api.useraccess.AdminCompanyScope;
|
||||
import com.solvway.mobile.api.util.RestTemplateCustomUtil;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.core.env.MapPropertySource;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/** Spring must be able to build the guest service from its beans and the ADMIN_PORTAL_BASE_URL property, or mobile-api fails to start. */
|
||||
class AdminEventGuestServiceWiringTest {
|
||||
|
||||
@Test
|
||||
void springCreatesTheService() {
|
||||
try (AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext()) {
|
||||
ctx.getEnvironment().getPropertySources().addFirst(new MapPropertySource("t", Map.of("app.admin-portal-base-url", "http://portal.test")));
|
||||
ctx.registerBean(RestTemplateCustomUtil.class, () -> mock(RestTemplateCustomUtil.class));
|
||||
ctx.registerBean(AdminAuthenticationService.class, () -> mock(AdminAuthenticationService.class));
|
||||
ctx.registerBean(EmailService.class, () -> mock(EmailService.class));
|
||||
ctx.registerBean(AdminCompanyScope.class);
|
||||
ctx.register(AdminEventLeadService.class);
|
||||
ctx.register(AdminEventGuestService.class);
|
||||
ctx.refresh();
|
||||
assertThat(ctx.getBean(AdminEventGuestService.class)).isNotNull();
|
||||
}
|
||||
}
|
||||
}
|
||||
+95
@@ -0,0 +1,95 @@
|
||||
package com.solvway.mobile.api.service;
|
||||
|
||||
import com.solvway.core.model.User;
|
||||
import com.solvway.core.utils.HttpReqRespUtils;
|
||||
import com.solvway.mobile.api.dto.admin.AdminEventLeadUpdateRequest;
|
||||
import com.solvway.mobile.api.dto.admin.CompanyEvent;
|
||||
import com.solvway.mobile.api.useraccess.AdminCompanyScope;
|
||||
import com.solvway.mobile.api.util.RestTemplateCustomUtil;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.MockedStatic;
|
||||
import org.springframework.core.ParameterizedTypeReference;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
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.mock;
|
||||
import static org.mockito.Mockito.mockStatic;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||
class AdminEventLeadAttendedTest {
|
||||
|
||||
private static final String EVENT = "/counter/event/findByUuid/{uuid}";
|
||||
private static final String INTERESTS = "/counter/interest/findByCompany/{companyId}";
|
||||
private static final String FIND = "/counter/eventLead/findByUuid/{eventLeadUuid}";
|
||||
private static final String UPDATE = "/counter/eventLead/update";
|
||||
private static final long EV = 3312726793093644288L;
|
||||
private static final long EL = 3312853897594494976L;
|
||||
|
||||
private RestTemplateCustomUtil rest;
|
||||
private AdminEventLeadService service;
|
||||
private MockedStatic<HttpReqRespUtils> http;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
rest = mock(RestTemplateCustomUtil.class);
|
||||
AdminAuthenticationService auth = mock(AdminAuthenticationService.class);
|
||||
service = new AdminEventLeadService(rest, auth, new AdminCompanyScope());
|
||||
when(rest.getURLforApiService(any())).thenAnswer(i -> i.getArgument(0));
|
||||
http = mockStatic(HttpReqRespUtils.class);
|
||||
http.when(HttpReqRespUtils::getJwtToken).thenReturn("tok");
|
||||
User caller = new User();
|
||||
caller.setId(21L);
|
||||
caller.setUsername("sales1");
|
||||
caller.setCompanyId(5L);
|
||||
when(auth.loadUserByToken("tok")).thenReturn(caller);
|
||||
CompanyEvent e = new CompanyEvent();
|
||||
e.setCompanyId(5L);
|
||||
e.setUuid(EV);
|
||||
e.setName("Fair");
|
||||
when(rest.getForEntity(eq(EVENT), any(ParameterizedTypeReference.class), eq(EV))).thenReturn(ResponseEntity.ok(e));
|
||||
when(rest.getForEntity(eq(INTERESTS), any(ParameterizedTypeReference.class), eq(5L))).thenReturn(ResponseEntity.ok(List.of()));
|
||||
Map<String, Object> stored = new LinkedHashMap<>();
|
||||
stored.put("eventLeadUuid", EL);
|
||||
stored.put("companyId", 5L);
|
||||
stored.put("eventUuid", EV);
|
||||
stored.put("name", "Aisyah");
|
||||
stored.put("status", "OPEN");
|
||||
stored.put("interestIds", List.of());
|
||||
when(rest.getForEntity(eq(FIND), any(ParameterizedTypeReference.class), eq(EL))).thenReturn(ResponseEntity.ok(stored));
|
||||
when(rest.postForEntity(eq(UPDATE), any(ParameterizedTypeReference.class), any())).thenAnswer(i -> ResponseEntity.ok(i.getArgument(2)));
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void close() { http.close(); }
|
||||
|
||||
private Map<String, Object> updateBody() {
|
||||
ArgumentCaptor<Object> c = ArgumentCaptor.forClass(Object.class);
|
||||
verify(rest).postForEntity(eq(UPDATE), any(ParameterizedTypeReference.class), c.capture());
|
||||
return (Map<String, Object>) c.getValue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void update_passesAttendedToTheCounter() {
|
||||
service.update(String.valueOf(EV), String.valueOf(EL),
|
||||
new AdminEventLeadUpdateRequest(null, null, null, List.of(), null, null, true));
|
||||
assertThat(updateBody()).containsEntry("attended", true);
|
||||
}
|
||||
|
||||
@Test
|
||||
void update_withoutAttendedSendsNull() {
|
||||
service.update(String.valueOf(EV), String.valueOf(EL),
|
||||
new AdminEventLeadUpdateRequest(null, null, null, List.of(), null, null));
|
||||
assertThat(updateBody()).containsEntry("attended", null);
|
||||
}
|
||||
}
|
||||
+181
@@ -0,0 +1,181 @@
|
||||
package com.solvway.mobile.api.service;
|
||||
|
||||
import com.solvway.core.model.Role;
|
||||
import com.solvway.core.model.User;
|
||||
import com.solvway.core.utils.HttpReqRespUtils;
|
||||
import com.solvway.mobile.api.dto.admin.AdminEventRequest;
|
||||
import com.solvway.mobile.api.dto.admin.CompanyEvent;
|
||||
import com.solvway.mobile.api.errorhandler.CodedResponseStatusException;
|
||||
import com.solvway.mobile.api.useraccess.AdminCompanyScope;
|
||||
import com.solvway.mobile.api.util.RestTemplateCustomUtil;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.MockedStatic;
|
||||
import org.springframework.core.ParameterizedTypeReference;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.client.HttpClientErrorException;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
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.mock;
|
||||
import static org.mockito.Mockito.mockStatic;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||
class AdminEventServiceCapacityTest {
|
||||
|
||||
private static final String BY_UUID = "/counter/event/findByUuid/{uuid}";
|
||||
private static final String SAVE = "/counter/event/save";
|
||||
private static final String TEAM = "/counter/eventSalesperson/findByEventUuid/{eventUuid}";
|
||||
private static final String MODULES = "/module/findAllByArea/{area}";
|
||||
private static final long UUID = 7123456789012345678L;
|
||||
|
||||
private RestTemplateCustomUtil rest;
|
||||
private AdminEventService service;
|
||||
private User caller;
|
||||
private MockedStatic<HttpReqRespUtils> http;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
rest = mock(RestTemplateCustomUtil.class);
|
||||
AdminAuthenticationService auth = mock(AdminAuthenticationService.class);
|
||||
service = new AdminEventService(rest, auth, new AdminCompanyScope());
|
||||
when(rest.getURLforApiService(any())).thenAnswer(i -> i.getArgument(0));
|
||||
http = mockStatic(HttpReqRespUtils.class);
|
||||
http.when(HttpReqRespUtils::getJwtToken).thenReturn("tok");
|
||||
caller = new User();
|
||||
caller.setUsername("boss");
|
||||
caller.setCompanyId(5L);
|
||||
Role staff = new Role();
|
||||
staff.setId(3L);
|
||||
caller.setRoles(List.of(staff));
|
||||
when(auth.loadUserByToken("tok")).thenReturn(caller);
|
||||
when(rest.postForEntity(eq(SAVE), any(ParameterizedTypeReference.class), any())).thenAnswer(i -> ResponseEntity.ok(i.getArgument(2)));
|
||||
when(rest.getForEntity(eq(TEAM), any(ParameterizedTypeReference.class), any())).thenReturn(ResponseEntity.ok(List.of()));
|
||||
CompanyEvent e = new CompanyEvent();
|
||||
e.setCompanyId(5L);
|
||||
e.setUuid(UUID);
|
||||
e.setName("Old");
|
||||
e.setLocation("Old place");
|
||||
e.setDateStart(LocalDateTime.of(2026, 5, 1, 0, 0));
|
||||
e.setDateEnd(LocalDateTime.of(2026, 5, 2, 23, 59, 59));
|
||||
e.setCapacity(500);
|
||||
e.setInvitationCap(40);
|
||||
when(rest.getForEntity(eq(BY_UUID), any(ParameterizedTypeReference.class), eq(UUID))).thenReturn(ResponseEntity.ok(e));
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void close() { http.close(); }
|
||||
|
||||
private void grantGuestsModuleToRole(long roleId) {
|
||||
when(rest.getForEntity(eq(MODULES), any(ParameterizedTypeReference.class), eq("BO")))
|
||||
.thenReturn(ResponseEntity.ok(List.of(Map.of("name", "BO_EVENT_GUESTS", "roles", List.of(Map.of("id", roleId))))));
|
||||
}
|
||||
|
||||
private static AdminEventRequest req(Integer capacity, Integer cap) {
|
||||
return new AdminEventRequest("New", "Hall", "2026-06-01", "2026-06-02", null, null, null, capacity, cap);
|
||||
}
|
||||
|
||||
private CompanyEvent saved() {
|
||||
ArgumentCaptor<Object> c = ArgumentCaptor.forClass(Object.class);
|
||||
verify(rest).postForEntity(eq(SAVE), any(ParameterizedTypeReference.class), c.capture());
|
||||
return (CompanyEvent) c.getValue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void get_showsCapacityAndInvitationCap() {
|
||||
assertThat(service.get(String.valueOf(UUID))).containsEntry("capacity", 500).containsEntry("invitationCap", 40);
|
||||
}
|
||||
|
||||
@Test
|
||||
void update_withGuestsModule_savesCapacityAndInvitationCap() {
|
||||
grantGuestsModuleToRole(3L);
|
||||
service.update(String.valueOf(UUID), req(800, 60));
|
||||
assertThat(saved().getCapacity()).isEqualTo(800);
|
||||
assertThat(saved().getInvitationCap()).isEqualTo(60);
|
||||
}
|
||||
|
||||
@Test
|
||||
void update_withGuestsModule_nullClearsTheLimits() {
|
||||
grantGuestsModuleToRole(3L);
|
||||
service.update(String.valueOf(UUID), req(null, null));
|
||||
assertThat(saved().getCapacity()).isNull();
|
||||
assertThat(saved().getInvitationCap()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void update_withoutGuestsModule_keepsExistingLimits() {
|
||||
grantGuestsModuleToRole(99L);
|
||||
service.update(String.valueOf(UUID), req(800, 60));
|
||||
assertThat(saved().getCapacity()).isEqualTo(500);
|
||||
assertThat(saved().getInvitationCap()).isEqualTo(40);
|
||||
}
|
||||
|
||||
@Test
|
||||
void update_withGuestsModule_rejectsLimitsBelowOne() {
|
||||
grantGuestsModuleToRole(3L);
|
||||
assertThatThrownBy(() -> service.update(String.valueOf(UUID), req(0, 5)))
|
||||
.isInstanceOfSatisfying(CodedResponseStatusException.class, e -> {
|
||||
assertThat(e.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST);
|
||||
assertThat((Map<String, Object>) e.getBody().getProperties().get("details")).containsEntry("field", "capacity");
|
||||
});
|
||||
assertThatThrownBy(() -> service.update(String.valueOf(UUID), req(5, -1)))
|
||||
.isInstanceOfSatisfying(CodedResponseStatusException.class, e ->
|
||||
assertThat((Map<String, Object>) e.getBody().getProperties().get("details")).containsEntry("field", "invitationCap"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void update_counterCapBelowGuests_isPassedThroughAsConflict() {
|
||||
grantGuestsModuleToRole(3L);
|
||||
when(rest.postForEntity(eq(SAVE), any(ParameterizedTypeReference.class), any()))
|
||||
.thenThrow(HttpClientErrorException.create(HttpStatus.CONFLICT, "Conflict", null, "{\"code\":\"CAP_BELOW_GUESTS\"}".getBytes(), null));
|
||||
assertThatThrownBy(() -> service.update(String.valueOf(UUID), req(800, 1)))
|
||||
.isInstanceOfSatisfying(CodedResponseStatusException.class, e -> {
|
||||
assertThat(e.getStatusCode()).isEqualTo(HttpStatus.CONFLICT);
|
||||
assertThat(e.getCode()).isEqualTo("CAP_BELOW_GUESTS");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void update_failedModuleLookup_failsClosedAndKeepsLimits() {
|
||||
when(rest.getForEntity(eq(MODULES), any(ParameterizedTypeReference.class), eq("BO"))).thenThrow(new IllegalStateException("down"));
|
||||
service.update(String.valueOf(UUID), req(800, 60));
|
||||
assertThat(saved().getCapacity()).isEqualTo(500);
|
||||
assertThat(saved().getInvitationCap()).isEqualTo(40);
|
||||
}
|
||||
|
||||
@Test
|
||||
void update_superAdminMaySetLimitsWithoutAModuleLookup() {
|
||||
Role superRole = new Role();
|
||||
superRole.setId(1L);
|
||||
caller.setCompanyId(null);
|
||||
caller.setRoles(List.of(superRole));
|
||||
service.update(String.valueOf(UUID), req(800, 60));
|
||||
assertThat(saved().getCapacity()).isEqualTo(800);
|
||||
assertThat(saved().getInvitationCap()).isEqualTo(60);
|
||||
verify(rest, org.mockito.Mockito.never()).getForEntity(eq(MODULES), any(ParameterizedTypeReference.class), eq("BO"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void update_otherCounterConflictKeepsItsOwnCodeAndMessage() {
|
||||
grantGuestsModuleToRole(3L);
|
||||
when(rest.postForEntity(eq(SAVE), any(ParameterizedTypeReference.class), any()))
|
||||
.thenThrow(HttpClientErrorException.create(HttpStatus.CONFLICT, "Conflict", null,
|
||||
"{\"code\":\"NAME_TAKEN\",\"message\":\"Name taken\"}".getBytes(), null));
|
||||
assertThatThrownBy(() -> service.update(String.valueOf(UUID), req(800, 60)))
|
||||
.isInstanceOfSatisfying(CodedResponseStatusException.class, e -> {
|
||||
assertThat(e.getCode()).isEqualTo("NAME_TAKEN");
|
||||
assertThat(e.getReason()).isEqualTo("Name taken");
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user