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("线索"));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user