feat(system): message search, guarded saves and the catalog version read

Ported from babyboo message-admin-api: search over the code, English and Chinese
text; a missing-Chinese filter; a save that refuses a code change or a duplicate
code (409, no longer swallowed) and bumps the catalog version.

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 12:42:59 +08:00
1 parent 4622a5219d
commit ebfee582c2
5 files changed
+268 -9

No files matched your search

@@ -0,0 +1,154 @@
package com.solvway.services.system.service;
import com.google.gson.Gson;
import com.solvway.services.system.constant.SystemConstants.Area;
import com.solvway.services.system.model.Message;
import com.solvway.services.system.model.MessageSearchRequest;
import com.solvway.services.system.repo.MessageRepository;
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.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.web.server.ResponseStatusException;
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.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/** Translations rules ported from babyboo message-admin-api: search, missing Chinese, save refusals, the version bump. */
@ExtendWith(MockitoExtension.class)
class MessageServiceTranslationsTest {
@Mock
private MessageRepository repository;
@Mock
private CatalogVersionService catalogVersion;
private MessageService service;
@BeforeEach
void setUp() {
service = new MessageService();
ReflectionTestUtils.setField(service, "messageRepository", repository);
ReflectionTestUtils.setField(service, "gson", new Gson());
ReflectionTestUtils.setField(service, "catalogVersion", catalogVersion);
}
@Test
void searchMatchesCodeEnglishOrChineseIgnoringCase() {
when(repository.findAllByArea(Area.A)).thenReturn(List.of(
msg(1, "Leads", "Leads", "线索"),
msg(2, "Users", "Users", "用户"),
msg(3, "EventLeads", "Event leads", "活动线索")));
assertThat(codes(service.search(req("线索", null, 0, 10)))).containsExactly("EventLeads", "Leads");
assertThat(codes(service.search(req("users", null, 0, 10)))).containsExactly("Users");
assertThat(codes(service.search(req("EVENT", null, 0, 10)))).containsExactly("EventLeads");
}
@Test
void searchNeverMatchesLanguageKeyNames() {
when(repository.findAllByArea(Area.A)).thenReturn(List.of(msg(1, "Leads", "Leads", "线索")));
assertThat(service.search(req("en_US", null, 0, 10)).getTotalElements()).isZero();
}
@Test
void missingChineseMeansAbsentBlankOrCopiedEnglish() {
when(repository.findAllByArea(Area.A)).thenReturn(List.of(
msg(1, "Leads", "Leads", "线索"),
msg(2, "WhatsApp", "WhatsApp", "WhatsApp"),
msg(3, "Users", "Users", null),
msg(4, "Roles", "Roles", " ")));
assertThat(codes(service.search(req(null, "zh_CN", 0, 10)))).containsExactly("Roles", "Users", "WhatsApp");
}
@Test
void searchPagesAndCountsOnlyMatches() {
when(repository.findAllByArea(Area.A)).thenReturn(List.of(
msg(1, "A1", "a", "甲"), msg(2, "A2", "a", "甲"), msg(3, "A3", "a", "甲"), msg(4, "B1", "b", "乙")));
Page<Message> page = service.search(req("a", null, 1, 2));
assertThat(page.getTotalElements()).isEqualTo(3);
assertThat(codes(page)).containsExactly("A3");
}
@Test
void savingAnExistingLabelBumpsTheVersion() {
Message stored = msg(7, "Leads", "Leads", "线索");
when(repository.findById(7L)).thenReturn(Optional.of(stored));
Message edit = msg(7, "Leads", "Leads", "潜在客户");
service.save(edit);
verify(repository).save(edit);
verify(catalogVersion).bump();
}
@Test
void changingALabelsCodeIsRefused() {
when(repository.findById(7L)).thenReturn(Optional.of(msg(7, "Leads", "Leads", "线索")));
assertRefused(() -> service.save(msg(7, "Contacts", "Leads", "线索")), "MsgsCodeAreaLocked");
}
@Test
void creatingADuplicateCodeIgnoringCaseIsRefused() {
when(repository.findByCodeAndArea("leads", Area.A)).thenReturn(msg(7, "Leads", "Leads", "线索"));
assertRefused(() -> service.save(msg(0, "leads", "Leads", null)), "MsgsDuplicateCode");
}
@Test
void creatingANewCodeSavesAndBumps() {
when(repository.findByCodeAndArea("NewLabel", Area.A)).thenReturn(null);
Message created = msg(0, "NewLabel", "New label", "新标签");
service.save(created);
verify(repository).save(created);
verify(catalogVersion).bump();
}
private void assertRefused(Runnable call, String reason) {
assertThatThrownBy(call::run).isInstanceOfSatisfying(ResponseStatusException.class, e -> {
assertThat(e.getStatusCode()).isEqualTo(HttpStatus.CONFLICT);
assertThat(e.getReason()).isEqualTo(reason);
});
verify(repository, never()).save(any());
verify(catalogVersion, never()).bump();
}
private static MessageSearchRequest req(String key, String missing, int page, int size) {
MessageSearchRequest r = new MessageSearchRequest();
r.setSearchKey(key);
r.setMissingLocale(missing);
r.setArea(Area.A);
r.setPage(page);
r.setSize(size);
return r;
}
private static List<String> codes(Page<Message> page) {
return page.getContent().stream().map(Message::getCode).toList();
}
private static Message msg(long id, String code, String en, String zh) {
String content = zh == null ? "{\"en_US\":\"" + en + "\"}" : "{\"en_US\":\"" + en + "\",\"zh_CN\":\"" + zh + "\"}";
Message m = new Message(code, content, Area.A, null);
m.setId(id);
return m;
}
}