巴青农资商城

FastJson2OpenStatsCacheTest.java 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package com.ruoyi.web;
  2. import com.alibaba.fastjson2.JSONObject;
  3. import com.ruoyi.framework.config.FastJson2JsonRedisSerializer;
  4. import com.ruoyi.web.modules.openstats.vo.CategorySalesItemVO;
  5. import com.ruoyi.web.modules.openstats.vo.CategorySalesVO;
  6. import java.nio.charset.StandardCharsets;
  7. import java.util.Collections;
  8. import org.junit.jupiter.api.Assertions;
  9. import org.junit.jupiter.api.Test;
  10. class FastJson2OpenStatsCacheTest
  11. {
  12. private static final String CATEGORY_SALES_JSON =
  13. "{\"@type\":\"com.ruoyi.web.modules.openstats.vo.CategorySalesVO\",\"items\":[{\"categoryId\":1L,\"categoryName\":\"兽药\",\"qty\":15L,\"ratio\":100.0}],\"statYear\":2026,\"totalQty\":15L}";
  14. @Test
  15. void deserializeCategorySalesAsTypedObject()
  16. {
  17. FastJson2JsonRedisSerializer<Object> serializer = new FastJson2JsonRedisSerializer<>(Object.class);
  18. Object cached = serializer.deserialize(CATEGORY_SALES_JSON.getBytes(StandardCharsets.UTF_8));
  19. Assertions.assertNotNull(cached);
  20. Assertions.assertTrue(cached instanceof CategorySalesVO);
  21. Assertions.assertEquals(2026, ((CategorySalesVO) cached).getStatYear());
  22. }
  23. @Test
  24. void jsonObjectCanConvertToCategorySales()
  25. {
  26. JSONObject jsonObject = JSONObject.parseObject(CATEGORY_SALES_JSON);
  27. CategorySalesVO vo = jsonObject.to(CategorySalesVO.class);
  28. Assertions.assertEquals(15L, vo.getTotalQty());
  29. Assertions.assertEquals("兽药", vo.getItems().get(0).getCategoryName());
  30. }
  31. @Test
  32. void roundTripCategorySales()
  33. {
  34. CategorySalesItemVO item = new CategorySalesItemVO();
  35. item.setCategoryId(1L);
  36. item.setCategoryName("兽药");
  37. item.setQty(15L);
  38. item.setRatio(100.0);
  39. CategorySalesVO vo = new CategorySalesVO();
  40. vo.setStatYear(2026);
  41. vo.setTotalQty(15L);
  42. vo.setItems(Collections.singletonList(item));
  43. FastJson2JsonRedisSerializer<Object> serializer = new FastJson2JsonRedisSerializer<>(Object.class);
  44. Object cached = serializer.deserialize(serializer.serialize(vo));
  45. Assertions.assertTrue(cached instanceof CategorySalesVO);
  46. }
  47. }