|
|
@@ -0,0 +1,87 @@
|
|
|
+package com.ruoyi.app.common;
|
|
|
+
|
|
|
+import com.ruoyi.common.core.domain.entity.SysDictData;
|
|
|
+import com.ruoyi.common.utils.MessageUtils;
|
|
|
+import com.ruoyi.system.service.ISysDictTypeService;
|
|
|
+import com.ruoyi.system.utils.Auth;
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
+import org.junit.jupiter.api.BeforeEach;
|
|
|
+import org.junit.jupiter.api.AfterEach;
|
|
|
+import org.mockito.MockedStatic;
|
|
|
+import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
|
|
+
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+import static org.junit.jupiter.api.Assertions.*;
|
|
|
+import static org.mockito.Mockito.*;
|
|
|
+import static org.mockito.ArgumentMatchers.anyString;
|
|
|
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
|
|
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
|
|
+
|
|
|
+class AppDictControllerTest {
|
|
|
+ private final ISysDictTypeService dictionaries = mock(ISysDictTypeService.class);
|
|
|
+ private final AppDictController controller = new AppDictController(dictionaries);
|
|
|
+
|
|
|
+ private MockedStatic<MessageUtils> messages;
|
|
|
+
|
|
|
+ @BeforeEach void setUpMessages() {
|
|
|
+ messages = mockStatic(MessageUtils.class);
|
|
|
+ messages.when(() -> MessageUtils.message(anyString())).thenAnswer(call -> call.getArgument(0));
|
|
|
+ }
|
|
|
+
|
|
|
+ @AfterEach void closeMessages() { messages.close(); }
|
|
|
+
|
|
|
+ @Test void returnsConfiguredOptionsWithoutAdministrativeMetadata() throws Exception {
|
|
|
+ var row = option("分成", "0", "0");
|
|
|
+ row.setRemark("internal note");
|
|
|
+ when(dictionaries.selectDictDataByType("sys_billing_type")).thenReturn(List.of(row));
|
|
|
+ MockMvcBuilders.standaloneSetup(controller).build()
|
|
|
+ .perform(get("/app/dict/data").header("token", "client-token")
|
|
|
+ .param("dictType", " sys_billing_type "))
|
|
|
+ .andExpect(status().isOk()).andExpect(jsonPath("$.code").value(200))
|
|
|
+ .andExpect(jsonPath("$.data[0].dictLabel").value("分成"))
|
|
|
+ .andExpect(jsonPath("$.data[0].dictValue").value("0"))
|
|
|
+ .andExpect(jsonPath("$.data[0].dictSort").value(1))
|
|
|
+ .andExpect(jsonPath("$.data[0].isDefault").value("N"))
|
|
|
+ .andExpect(jsonPath("$.data[0].remark").doesNotExist());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test void supportsAnyConfiguredTypeAndOmitsDisabledItems() {
|
|
|
+ when(dictionaries.selectDictDataByType("custom_client_options"))
|
|
|
+ .thenReturn(List.of(option("A", "a", "0"), option("B", "b", "1")));
|
|
|
+ assertEquals(List.of(new AppDictController.DictOption("A", "a", 1L, "N")),
|
|
|
+ controller.data("token", "custom_client_options").get("data"));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test void missingOrEmptyDictionaryReturnsArray() {
|
|
|
+ assertEquals(List.of(), controller.data("token", "unknown_type").get("data"));
|
|
|
+ when(dictionaries.selectDictDataByType("empty_type")).thenReturn(List.of());
|
|
|
+ assertEquals(List.of(), controller.data("token", "empty_type").get("data"));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test void invalidTypeDoesNotQueryStorage() {
|
|
|
+ for (String value : new String[]{null, "", " ", "x".repeat(101)}) {
|
|
|
+ assertEquals(500, controller.data("token", value).get("code"));
|
|
|
+ }
|
|
|
+ verifyNoInteractions(dictionaries);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test void missingTokenHeaderIsRejectedBeforeQuery() throws Exception {
|
|
|
+ MockMvcBuilders.standaloneSetup(controller).build()
|
|
|
+ .perform(get("/app/dict/data").param("dictType", "sys_billing_type"))
|
|
|
+ .andExpect(status().isBadRequest());
|
|
|
+ verifyNoInteractions(dictionaries);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test void endpointUsesClientTokenSessionAuthentication() throws Exception {
|
|
|
+ var method = AppDictController.class.getMethod("data", String.class, String.class);
|
|
|
+ assertTrue(method.getAnnotation(Auth.class).token());
|
|
|
+ }
|
|
|
+
|
|
|
+ private static SysDictData option(String label, String value, String status) {
|
|
|
+ var row = new SysDictData();
|
|
|
+ row.setDictLabel(label); row.setDictValue(value); row.setStatus(status);
|
|
|
+ row.setDictSort(1L); row.setIsDefault("N");
|
|
|
+ return row;
|
|
|
+ }
|
|
|
+}
|