feat(mobile-api): admin translations and languages APIs
Locales list, create and update; public enabled list; message catalog, version, search, detail, update, export and import. Refusals from services-system pass through as 409 with their reason code. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfiCVCgCVJZq4NDEqZs
This commit is contained in:
1 parent
45bd81cdbb
commit
27e0ec7bff
8 files changed
+617
-2
No files matched your search
+92
@@ -0,0 +1,92 @@
|
||||
package com.solvway.mobile.api.controller.admin;
|
||||
|
||||
import com.solvway.core.annotation.AppAuth;
|
||||
import com.solvway.mobile.api.controller.PublicLocaleController;
|
||||
import com.solvway.mobile.api.service.AdminTranslationService;
|
||||
import com.solvway.mobile.api.service.SystemService;
|
||||
import com.solvway.mobile.api.useraccess.AdminModule;
|
||||
import com.solvway.mobile.api.useraccess.Audited;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
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.put;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
class AdminTranslationControllersTest {
|
||||
|
||||
private AdminTranslationService translations;
|
||||
private SystemService system;
|
||||
private MockMvc mvc;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
translations = mock(AdminTranslationService.class);
|
||||
system = mock(SystemService.class);
|
||||
mvc = MockMvcBuilders.standaloneSetup(new AdminMessageController(translations, system), new AdminLocaleController(translations)).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
void catalogAndVersionNeedOnlyTheApiKeyEverythingElseAModule() throws Exception {
|
||||
for (Method m : AdminMessageController.class.getDeclaredMethods()) {
|
||||
if (!m.isAnnotationPresent(AppAuth.class)) continue;
|
||||
boolean open = m.getName().startsWith("catalog");
|
||||
assertThat(m.getAnnotation(AppAuth.class).skipValidate()).as(m.getName()).isEqualTo(open);
|
||||
if (!open) assertThat(m.getAnnotation(AdminModule.class).value()).as(m.getName()).containsExactly("BO_MESSAGES");
|
||||
}
|
||||
assertThat(AdminLocaleController.class.getMethod("list").getAnnotation(AdminModule.class).value()).containsExactly(AdminModule.ANY_ADMIN);
|
||||
assertThat(AdminLocaleController.class.getMethod("create", Map.class).getAnnotation(AdminModule.class).value()).containsExactly("BO_LOCALES");
|
||||
assertThat(AdminLocaleController.class.getMethod("update", Long.class, Map.class).getAnnotation(AdminModule.class).value()).containsExactly("BO_LOCALES");
|
||||
}
|
||||
|
||||
@Test
|
||||
void labelsCannotBeAddedOrDeletedAndExportIsAudited() throws Exception {
|
||||
assertThat(Arrays.stream(AdminMessageController.class.getDeclaredMethods())
|
||||
.filter(m -> m.isAnnotationPresent(DeleteMapping.class)
|
||||
|| (m.isAnnotationPresent(PostMapping.class) && !m.getName().equals("importMessages")))).isEmpty();
|
||||
assertThat(AdminMessageController.class.getMethod("export").getAnnotation(Audited.class)).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void listPassesFiltersWithFirstPageOfTwentyByDefault() throws Exception {
|
||||
when(translations.search(null, "zh_CN", 0, 20)).thenReturn(Map.of("content", List.of(), "totalElements", 0));
|
||||
mvc.perform(get("/api/admin/messages").param("missingLocale", "zh_CN")).andExpect(status().isOk()).andExpect(jsonPath("$.totalElements").value(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateUsesThePathId() throws Exception {
|
||||
mvc.perform(put("/api/admin/messages/7").contentType(MediaType.APPLICATION_JSON).content("{\"id\":9,\"content\":\"{}\"}"))
|
||||
.andExpect(status().isNoContent());
|
||||
verify(translations).updateMessage(eq(7L), eq(Map.of("id", 9, "content", "{}")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void publicLocalesAreEnabledOnlyAndNeedNoSession() throws Exception {
|
||||
assertThat(PublicLocaleController.class.getMethod("enabled").getAnnotation(AppAuth.class).skipValidate()).isTrue();
|
||||
when(translations.locales(true)).thenReturn(List.of(Map.of("code", "zh_CN")));
|
||||
MockMvcBuilders.standaloneSetup(new PublicLocaleController(translations)).build()
|
||||
.perform(get("/api/public/locales")).andExpect(status().isOk()).andExpect(jsonPath("$[0].code").value("zh_CN"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void catalogReadsAreaA() throws Exception {
|
||||
when(system.findMessageCatalog("A", "zh_CN")).thenReturn(Map.of("Leads", "线索"));
|
||||
mvc.perform(get("/api/admin/messages/catalog/zh_CN")).andExpect(status().isOk()).andExpect(jsonPath("$.Leads").value("线索"));
|
||||
}
|
||||
}
|
||||
+178
@@ -0,0 +1,178 @@
|
||||
package com.solvway.mobile.api.service;
|
||||
|
||||
import com.solvway.mobile.api.errorhandler.CodedResponseStatusException;
|
||||
import com.solvway.mobile.api.util.RestTemplateCustomUtil;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.springframework.core.ParameterizedTypeReference;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.client.HttpClientErrorException;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.LinkedHashMap;
|
||||
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.doThrow;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/** Translations and languages admin rules ported from babyboo message-admin-api and locale-registry. */
|
||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||
class AdminTranslationServiceTest {
|
||||
|
||||
private RestTemplateCustomUtil rest;
|
||||
private AdminTranslationService service;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
rest = mock(RestTemplateCustomUtil.class);
|
||||
service = new AdminTranslationService(rest);
|
||||
when(rest.getURLforApiService(any())).thenAnswer(i -> i.getArgument(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateUsesThePathIdAndKeepsTheStoredCodeAndArea() {
|
||||
stored(7, "Leads", "A");
|
||||
Map<String, Object> body = new LinkedHashMap<>();
|
||||
body.put("id", 99);
|
||||
body.put("code", "Contacts");
|
||||
body.put("content", "{\"en_US\":\"Leads\",\"zh_CN\":\"线索\"}");
|
||||
|
||||
service.updateMessage(7L, body);
|
||||
|
||||
Map<String, Object> sent = savedBody();
|
||||
assertThat(sent).containsEntry("id", 7L).containsEntry("code", "Leads").containsEntry("area", "A")
|
||||
.containsEntry("content", "{\"en_US\":\"Leads\",\"zh_CN\":\"线索\"}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void aRefusalReachesThePortalWithItsCode() {
|
||||
stored(7, "Leads", "A");
|
||||
doThrow(conflict("MsgsCodeAreaLocked")).when(rest).postForEntity(eq("/message/save"), eq(Void.class), any());
|
||||
|
||||
assertThatThrownBy(() -> service.updateMessage(7L, Map.of("content", "{}")))
|
||||
.isInstanceOfSatisfying(CodedResponseStatusException.class, e -> {
|
||||
assertThat(e.getStatusCode()).isEqualTo(HttpStatus.CONFLICT);
|
||||
assertThat(e.getCode()).isEqualTo("MsgsCodeAreaLocked");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void onlyBackOfficeLabelsCanBeReadOrEdited() {
|
||||
stored(8, "Cart", "AP");
|
||||
|
||||
assertThatThrownBy(() -> service.message(8L))
|
||||
.isInstanceOfSatisfying(ResponseStatusException.class, e -> assertThat(e.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND));
|
||||
assertThatThrownBy(() -> service.updateMessage(8L, Map.of("content", "{}")))
|
||||
.isInstanceOfSatisfying(ResponseStatusException.class, e -> assertThat(e.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND));
|
||||
}
|
||||
|
||||
@Test
|
||||
void searchAsksForBackOfficeLabelsWithTheFilters() {
|
||||
when(rest.postForEntity(eq("/message/search"), any(ParameterizedTypeReference.class), any()))
|
||||
.thenReturn(ResponseEntity.ok(Map.of("content", List.of(), "totalElements", 0)));
|
||||
|
||||
service.search("线索", "zh_CN", 2, 20);
|
||||
|
||||
ArgumentCaptor<Object> body = ArgumentCaptor.forClass(Object.class);
|
||||
verify(rest).postForEntity(eq("/message/search"), any(ParameterizedTypeReference.class), body.capture());
|
||||
assertThat((Map<String, Object>) body.getValue()).containsEntry("searchKey", "线索").containsEntry("missingLocale", "zh_CN")
|
||||
.containsEntry("area", "A").containsEntry("page", 2).containsEntry("size", 20);
|
||||
}
|
||||
|
||||
@Test
|
||||
void importMatchesByCodeIgnoringCaseNeverByIdAndKeepsGoingAfterAFailure() {
|
||||
when(rest.getForEntity(eq("/message/getAllMessages/{area}"), any(ParameterizedTypeReference.class), eq("A")))
|
||||
.thenReturn(ResponseEntity.ok(List.of(row(7, "Leads", "A"))));
|
||||
when(rest.postForEntity(eq("/message/save"), eq(Void.class), any())).thenAnswer(i -> {
|
||||
Map<String, Object> b = i.getArgument(2);
|
||||
if ("Broken".equals(b.get("code"))) throw conflict("MsgsDuplicateCode");
|
||||
return ResponseEntity.ok().build();
|
||||
});
|
||||
List<Map<String, Object>> rows = List.of(
|
||||
Map.of("id", 123, "code", "leads", "content", "{\"zh_CN\":\"线索\"}"),
|
||||
Map.of("code", "Broken", "content", "{}"),
|
||||
Map.of("code", "NewLabel", "content", "{\"en_US\":\"New\"}"));
|
||||
|
||||
Map<String, Object> summary = service.importMessages(rows);
|
||||
|
||||
assertThat(summary).containsEntry("created", 1).containsEntry("updated", 1);
|
||||
assertThat((List<Map<String, Object>>) summary.get("errors"))
|
||||
.containsExactly(Map.of("index", 1, "code", "Broken", "error", "MsgsDuplicateCode"));
|
||||
ArgumentCaptor<Object> bodies = ArgumentCaptor.forClass(Object.class);
|
||||
verify(rest, times(3)).postForEntity(eq("/message/save"), eq(Void.class), bodies.capture());
|
||||
Map<String, Object> first = (Map<String, Object>) bodies.getAllValues().get(0);
|
||||
assertThat(first).containsEntry("id", 7L).containsEntry("code", "Leads").containsEntry("area", "A");
|
||||
assertThat((Map<String, Object>) bodies.getAllValues().get(2)).containsEntry("id", 0L).containsEntry("area", "A");
|
||||
}
|
||||
|
||||
@Test
|
||||
void aRowWithoutACodeIsReportedNotSent() {
|
||||
when(rest.getForEntity(eq("/message/getAllMessages/{area}"), any(ParameterizedTypeReference.class), eq("A")))
|
||||
.thenReturn(ResponseEntity.ok(List.of()));
|
||||
|
||||
Map<String, Object> summary = service.importMessages(List.of(Map.of("content", "{}")));
|
||||
|
||||
assertThat((List<?>) summary.get("errors")).hasSize(1);
|
||||
verify(rest, times(0)).postForEntity(eq("/message/save"), eq(Void.class), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void languageCreateRelaysADuplicate() {
|
||||
when(rest.postForEntity(eq("/locale/save"), any(ParameterizedTypeReference.class), any())).thenThrow(conflict("LocalesDuplicateCode"));
|
||||
|
||||
assertThatThrownBy(() -> service.createLocale(Map.of("code", "zh_CN", "label", "x")))
|
||||
.isInstanceOfSatisfying(CodedResponseStatusException.class, e -> assertThat(e.getCode()).isEqualTo("LocalesDuplicateCode"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void languageUpdateUsesThePathId() {
|
||||
when(rest.postForEntity(eq("/locale/update"), any(ParameterizedTypeReference.class), any())).thenReturn(ResponseEntity.ok(Map.of()));
|
||||
Map<String, Object> body = new LinkedHashMap<>();
|
||||
body.put("id", 5);
|
||||
body.put("enabled", false);
|
||||
|
||||
service.updateLocale(2L, body);
|
||||
|
||||
ArgumentCaptor<Object> sent = ArgumentCaptor.forClass(Object.class);
|
||||
verify(rest).postForEntity(eq("/locale/update"), any(ParameterizedTypeReference.class), sent.capture());
|
||||
assertThat((Map<String, Object>) sent.getValue()).containsEntry("id", 2L).containsEntry("enabled", false);
|
||||
}
|
||||
|
||||
private void stored(long id, String code, String area) {
|
||||
when(rest.getForEntity(eq("/message/findById/{id}"), any(ParameterizedTypeReference.class), eq(id)))
|
||||
.thenReturn(ResponseEntity.ok(row(id, code, area)));
|
||||
}
|
||||
|
||||
private Map<String, Object> savedBody() {
|
||||
ArgumentCaptor<Object> body = ArgumentCaptor.forClass(Object.class);
|
||||
verify(rest).postForEntity(eq("/message/save"), eq(Void.class), body.capture());
|
||||
return (Map<String, Object>) body.getValue();
|
||||
}
|
||||
|
||||
private static Map<String, Object> row(long id, String code, String area) {
|
||||
Map<String, Object> m = new LinkedHashMap<>();
|
||||
m.put("id", id);
|
||||
m.put("code", code);
|
||||
m.put("area", area);
|
||||
m.put("content", "{\"en_US\":\"" + code + "\"}");
|
||||
m.put("description", null);
|
||||
return m;
|
||||
}
|
||||
|
||||
private static HttpClientErrorException conflict(String reason) {
|
||||
String body = "{\"type\":\"about:blank\",\"title\":\"Conflict\",\"status\":409,\"detail\":\"" + reason + "\"}";
|
||||
return HttpClientErrorException.create(HttpStatus.CONFLICT, "Conflict", new HttpHeaders(), body.getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user