package com.ruoyi.web;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.core.domain.entity.SysDictData;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

class SysDictDataJsonTest
{
    @Test
    void parseArrayIgnoresLegacyDefaultField()
    {
        String json = "[{\"dictLabel\":\"正常\",\"dictValue\":\"0\",\"isDefault\":\"Y\",\"default\":false,\"status\":\"0\"},"
                + "{\"dictLabel\":\"停用\",\"dictValue\":\"1\",\"isDefault\":\"N\",\"default\":false,\"status\":\"0\"}]";
        List<SysDictData> list = JSON.parseArray(json, SysDictData.class);
        Assertions.assertEquals(2, list.size());
        Assertions.assertEquals("正常", list.get(0).getDictLabel());
    }

    @Test
    void roundTripDoesNotEmitDefaultField()
    {
        SysDictData data = new SysDictData();
        data.setDictLabel("正常");
        data.setDictValue("0");
        data.setIsDefault("Y");
        data.setStatus("0");

        String json = JSON.toJSONString(Collections.singletonList(data));
        Assertions.assertFalse(json.contains("\"default\""));

        List<SysDictData> list = JSON.parseArray(json, SysDictData.class);
        Assertions.assertEquals(1, list.size());
        Assertions.assertEquals("0", list.get(0).getDictValue());
    }

    @Test
    void parseFromLinkedHashMapList()
    {
        LinkedHashMap<String, Object> row = new LinkedHashMap<>();
        row.put("dictLabel", "正常");
        row.put("dictValue", "0");
        row.put("isDefault", "Y");
        row.put("default", false);
        row.put("status", "0");

        String json = JSON.toJSONString(Collections.singletonList(row));
        List<SysDictData> list = JSON.parseArray(json, SysDictData.class);
        Assertions.assertEquals(1, list.size());
        Assertions.assertEquals("正常", list.get(0).getDictLabel());
    }
}
