| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- package com.ruoyi.web;
- import com.alibaba.fastjson2.JSONObject;
- import com.ruoyi.framework.config.FastJson2JsonRedisSerializer;
- import com.ruoyi.web.modules.openstats.vo.CategorySalesItemVO;
- import com.ruoyi.web.modules.openstats.vo.CategorySalesVO;
- import java.nio.charset.StandardCharsets;
- import java.util.Collections;
- import org.junit.jupiter.api.Assertions;
- import org.junit.jupiter.api.Test;
- class FastJson2OpenStatsCacheTest
- {
- private static final String CATEGORY_SALES_JSON =
- "{\"@type\":\"com.ruoyi.web.modules.openstats.vo.CategorySalesVO\",\"items\":[{\"categoryId\":1L,\"categoryName\":\"兽药\",\"qty\":15L,\"ratio\":100.0}],\"statYear\":2026,\"totalQty\":15L}";
- @Test
- void deserializeCategorySalesAsTypedObject()
- {
- FastJson2JsonRedisSerializer<Object> serializer = new FastJson2JsonRedisSerializer<>(Object.class);
- Object cached = serializer.deserialize(CATEGORY_SALES_JSON.getBytes(StandardCharsets.UTF_8));
- Assertions.assertNotNull(cached);
- Assertions.assertTrue(cached instanceof CategorySalesVO);
- Assertions.assertEquals(2026, ((CategorySalesVO) cached).getStatYear());
- }
- @Test
- void jsonObjectCanConvertToCategorySales()
- {
- JSONObject jsonObject = JSONObject.parseObject(CATEGORY_SALES_JSON);
- CategorySalesVO vo = jsonObject.to(CategorySalesVO.class);
- Assertions.assertEquals(15L, vo.getTotalQty());
- Assertions.assertEquals("兽药", vo.getItems().get(0).getCategoryName());
- }
- @Test
- void roundTripCategorySales()
- {
- CategorySalesItemVO item = new CategorySalesItemVO();
- item.setCategoryId(1L);
- item.setCategoryName("兽药");
- item.setQty(15L);
- item.setRatio(100.0);
- CategorySalesVO vo = new CategorySalesVO();
- vo.setStatYear(2026);
- vo.setTotalQty(15L);
- vo.setItems(Collections.singletonList(item));
- FastJson2JsonRedisSerializer<Object> serializer = new FastJson2JsonRedisSerializer<>(Object.class);
- Object cached = serializer.deserialize(serializer.serialize(vo));
- Assertions.assertTrue(cached instanceof CategorySalesVO);
- }
- }
|