feat(notifications): staff notification store in services-system

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfiCVCgCVJZq4NDEqZs
This commit is contained in:
ngqileandClaude Opus 5.5 committed 2026-12-02 19:40:32 +08:00
1 parent 26b285cc0a
commit d5384c6ac0
10 files changed
+411 -47

No files matched your search

@@ -0,0 +1,73 @@
package com.solvway.services.system.controller;
import com.solvway.services.system.service.NotificationService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.server.ResponseStatusException;
import java.util.List;
import java.util.Map;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
// add-lead-notifications: the store's internal endpoints, with explicit methods and real error statuses.
@ExtendWith(MockitoExtension.class)
class NotificationControllerTest {
@Mock private NotificationService service;
@InjectMocks private NotificationController controller;
private MockMvc mvc;
@BeforeEach
void setUp() {
mvc = MockMvcBuilders.standaloneSetup(controller).build();
}
@Test
void sendStoresTheRows() throws Exception {
when(service.send(anyList())).thenReturn(1);
mvc.perform(post("/notification/send").contentType(MediaType.APPLICATION_JSON)
.content("[{\"receiver\":\"11\",\"companyId\":2,\"title\":\"T\",\"message\":\"{}\",\"type\":\"LEAD_ASSIGNED\",\"link\":\"/event-leads/9\"}]"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.count").value(1));
verify(service).send(List.of(new NotificationService.Send("11", 2L, "T", "{}", "LEAD_ASSIGNED", "/event-leads/9", null)));
}
@Test
void inboxCountAndMarks() throws Exception {
when(service.inbox("11", 0, 20)).thenReturn(Map.of("items", List.of(), "total", 0L));
when(service.unreadCount("11")).thenReturn(3L);
when(service.markAllRead("11")).thenReturn(2);
mvc.perform(get("/notification/inbox/11/0/20")).andExpect(status().isOk()).andExpect(jsonPath("$.total").value(0));
mvc.perform(get("/notification/unreadCount/11")).andExpect(status().isOk()).andExpect(jsonPath("$.count").value(3));
mvc.perform(post("/notification/markRead/11/5")).andExpect(status().isOk());
verify(service).markRead("11", 5L);
mvc.perform(post("/notification/markAllRead/11")).andExpect(status().isOk()).andExpect(jsonPath("$.count").value(2));
}
@Test
void aForeignRowIs404() throws Exception {
doThrow(new ResponseStatusException(HttpStatus.NOT_FOUND)).when(service).markRead("12", 5L);
mvc.perform(post("/notification/markRead/12/5")).andExpect(status().isNotFound());
}
@Test
void theOldUncheckedEndpointsAreGone() throws Exception {
mvc.perform(post("/notification/save").contentType(MediaType.APPLICATION_JSON).content("{}")).andExpect(status().isNotFound());
}
}
@@ -0,0 +1,134 @@
package com.solvway.services.system.service;
import com.solvway.services.system.model.Notification;
import com.solvway.services.system.repo.NotificationRepository;
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.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.server.ResponseStatusException;
import java.time.LocalDateTime;
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.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;
// add-lead-notifications: the staff bell's store. Rows come from other services; reads and marks are the caller's own.
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
class NotificationServiceTest {
@Mock private NotificationRepository repo;
private NotificationService service;
@BeforeEach
void setUp() {
service = new NotificationService(repo);
when(repo.save(any(Notification.class))).thenAnswer(i -> i.getArgument(0));
}
private static NotificationService.Send send(String receiver, String collapseKey) {
return new NotificationService.Send(receiver, 2L, "NoticeLeadAssignedTitle", "{\"lead\":\"Tan\"}", "LEAD_ASSIGNED", "/event-leads/9", collapseKey);
}
@Test
void storesOneUnreadStaffRowPerRecipient() {
assertThat(service.send(List.of(send("11", null), send("12", null)))).isEqualTo(2);
ArgumentCaptor<Notification> saved = ArgumentCaptor.forClass(Notification.class);
verify(repo, org.mockito.Mockito.times(2)).save(saved.capture());
Notification n = saved.getAllValues().get(0);
assertThat(n.getRecipientType()).isEqualTo("A");
assertThat(n.getReceiver()).isEqualTo("11");
assertThat(n.getCompanyId()).isEqualTo(2L);
assertThat(n.getTitle()).isEqualTo("NoticeLeadAssignedTitle");
assertThat(n.getMessage()).isEqualTo("{\"lead\":\"Tan\"}");
assertThat(n.getType()).isEqualTo("LEAD_ASSIGNED");
assertThat(n.getLink()).isEqualTo("/event-leads/9");
assertThat(n.isSeen()).isFalse();
assertThat(n.getCreateDateTime()).isNotNull();
}
@Test
void anUnreadRowWithTheSameCollapseKeyIsUpdatedNotDuplicated() {
Notification unread = new Notification();
unread.setReceiver("11");
unread.setMessage("old");
unread.setCreateDateTime(LocalDateTime.of(2026, 12, 1, 9, 0));
when(repo.findFirstByRecipientTypeAndReceiverAndCollapseKeyAndSeenFalse("A", "11", "WA:5")).thenReturn(Optional.of(unread));
service.send(List.of(send("11", "WA:5")));
verify(repo).save(unread);
assertThat(unread.getMessage()).isEqualTo("{\"lead\":\"Tan\"}");
assertThat(unread.getCreateDateTime()).isAfter(LocalDateTime.of(2026, 12, 1, 9, 0));
}
@Test
void aCollapseKeyWithNoUnreadRowInsertsOne() {
when(repo.findFirstByRecipientTypeAndReceiverAndCollapseKeyAndSeenFalse("A", "11", "WA:5")).thenReturn(Optional.empty());
service.send(List.of(send("11", "WA:5")));
ArgumentCaptor<Notification> saved = ArgumentCaptor.forClass(Notification.class);
verify(repo).save(saved.capture());
assertThat(saved.getValue().getCollapseKey()).isEqualTo("WA:5");
}
@Test
void refusesRowsWithoutARecipientTypeCodeOrCompany() {
for (NotificationService.Send bad : List.of(
new NotificationService.Send(" ", 2L, "T", "{}", "LEAD_ASSIGNED", "/x", null),
new NotificationService.Send("11", null, "T", "{}", "LEAD_ASSIGNED", "/x", null),
new NotificationService.Send("11", 2L, null, "{}", "LEAD_ASSIGNED", "/x", null),
new NotificationService.Send("11", 2L, "T", "{}", "OTHER", "/x", null),
new NotificationService.Send("11", 2L, "T", "{}", "LEAD_ASSIGNED", "https://evil.example/x", null))) {
assertThatThrownBy(() -> service.send(List.of(bad))).isInstanceOf(ResponseStatusException.class);
}
verify(repo, never()).save(any());
}
@Test
void readsAndCountsOnlyTheCallersStaffRows() {
when(repo.findAllByRecipientTypeAndReceiverOrderByCreateDateTimeDescIdDesc(eq("A"), eq("11"), any()))
.thenReturn(new PageImpl<>(List.of(new Notification()), PageRequest.of(0, 20), 21));
when(repo.countByRecipientTypeAndReceiverAndSeenFalse("A", "11")).thenReturn(3L);
Map<String, Object> page = service.inbox("11", 0, 20);
assertThat(page.get("total")).isEqualTo(21L);
assertThat((List<?>) page.get("items")).hasSize(1);
assertThat(service.unreadCount("11")).isEqualTo(3L);
}
@Test
void markingAnotherUsersRowIsNotFound() {
when(repo.markSeen(eq(5L), eq("A"), eq("12"), any())).thenReturn(0);
when(repo.markSeen(eq(5L), eq("A"), eq("11"), any())).thenReturn(1);
service.markRead("11", 5L);
assertThatThrownBy(() -> service.markRead("12", 5L))
.isInstanceOfSatisfying(ResponseStatusException.class, e -> assertThat(e.getStatusCode().value()).isEqualTo(404));
}
@Test
void markAllReadTouchesOnlyTheCallersRows() {
when(repo.markAllSeen(eq("A"), eq("11"), any())).thenReturn(4);
assertThat(service.markAllRead("11")).isEqualTo(4);
verify(repo, never()).markSeen(anyLong(), any(), any(), any());
}
}