Merge branch 'dev'

This commit is contained in:
2025-08-10 14:33:46 +08:00
185 changed files with 6016 additions and 6452 deletions

View File

@@ -32,7 +32,7 @@ public class TestMain {
public void testGenJwtKey() {
BigDecimal bigDecimal = new BigDecimal(NumberUtil.decimalFormat("0.00", 1 * 0.01));
System.out.println("实际付款金额 = " + bigDecimal);
System.out.println("实际付款金额111111111 = " + bigDecimal);
final HjmCar byGpsNo = hjmCarService.getByGpsNo("gps1");
System.out.println("byGpsNo = " + byGpsNo.getSpeed());

View File

@@ -0,0 +1,56 @@
package com.gxwebsoft.bszx;
import com.gxwebsoft.bszx.service.BszxPayService;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import javax.annotation.Resource;
import java.math.BigDecimal;
import static org.junit.jupiter.api.Assertions.*;
/**
* 百色中学订单总金额统计测试
*
* @author 科技小王子
* @since 2025-07-31
*/
@SpringBootTest
@ActiveProfiles("test")
public class BszxOrderTotalTest {
@Resource
private BszxPayService bszxPayService;
@Test
void testBszxOrderTotal() {
// 测试百色中学订单总金额统计
BigDecimal total = bszxPayService.total();
// 验证返回值不为null
assertNotNull(total, "百色中学订单总金额不应该为null");
// 验证返回值大于等于0
assertTrue(total.compareTo(BigDecimal.ZERO) >= 0, "百色中学订单总金额应该大于等于0");
System.out.println("百色中学订单总金额统计结果:" + total);
}
@Test
void testBszxOrderTotalPerformance() {
// 测试性能
long startTime = System.currentTimeMillis();
BigDecimal total = bszxPayService.total();
long endTime = System.currentTimeMillis();
long duration = endTime - startTime;
System.out.println("百色中学订单总金额统计耗时:" + duration + "ms");
System.out.println("统计结果:" + total);
// 验证查询时间在合理范围内小于5秒
assertTrue(duration < 5000, "查询时间应该在5秒以内");
}
}

View File

@@ -27,9 +27,9 @@ public class ShopGenerator {
// 输出目录
private static final String OUTPUT_DIR = "/src/main/java";
// Vue文件输出位置
private static final String OUTPUT_LOCATION_VUE = "/Users/gxwebsoft/VUE/shop";
private static final String OUTPUT_LOCATION_VUE = "/Users/gxwebsoft/VUE/mp-vue";
// Vue文件输出目录
private static final String OUTPUT_LOCATION_UNIAPP = "/Users/gxwebsoft/APP/shop";
private static final String OUTPUT_LOCATION_UNIAPP = "/Users/gxwebsoft/VUE/template-10550";
// Vue文件输出目录
private static final String OUTPUT_DIR_VUE = "/src";
// 作者名称
@@ -55,7 +55,7 @@ public class ShopGenerator {
// "shop_goods_spec",
// "shop_goods_sku",
// "shop_goods_category",
// "shop_goods_coupon",
"shop_coupon",
// "shop_goods_description",
// "shop_goods_log",
// "shop_goods_relation",
@@ -89,6 +89,7 @@ public class ShopGenerator {
// "shop_dealer_user",
// "shop_user_referee",
// "shop_dealer_withdraw",
"shop_user_coupon",
// "shop_cart",
// "shop_count",
// "shop_express",

View File

@@ -23,8 +23,8 @@ import ${cfg.packageName!}.common.core.web.PageParam;
import ${cfg.packageName!}.common.core.web.BatchParam;
import ${cfg.packageName!}.common.core.annotation.OperationLog;
import ${cfg.packageName!}.common.system.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
<% if(!restControllerStyle) { %>
@@ -41,7 +41,7 @@ import java.util.List;
* @since ${date(), 'yyyy-MM-dd HH:mm:ss'}
*/
<% if(swagger2) { %>
@Api(tags = "${table.comment!}管理")
@Tag(name = "${table.comment!}管理")
<% } %>
<% if(restControllerStyle) { %>
@RestController
@@ -68,7 +68,7 @@ public class ${table.controllerName} {
@PreAuthorize("hasAuthority('${authPre}:list')")
<% } %>
<% if(swagger2) { %>
@ApiOperation("分页查询${table.comment!}")
@Operation(summary = "分页查询${table.comment!}")
<% } %>
<% if(!restControllerStyle) { %>
@ResponseBody
@@ -88,7 +88,7 @@ public class ${table.controllerName} {
@PreAuthorize("hasAuthority('${authPre}:list')")
<% } %>
<% if(swagger2) { %>
@ApiOperation("查询全部${table.comment!}")
@Operation(summary = "查询全部${table.comment!}")
<% } %>
<% if(!restControllerStyle) { %>
@ResponseBody
@@ -105,7 +105,7 @@ public class ${table.controllerName} {
*/
<% } %>
@PreAuthorize("hasAuthority('${authPre}:list')")
@ApiOperation("根据id查询${table.comment!}")
@Operation(summary = "根据id查询${table.comment!}")
@GetMapping("/{id}")
public ApiResult<${entity}> get(@PathVariable("id") Integer id) {
// 使用关联查询
@@ -124,7 +124,7 @@ public class ${table.controllerName} {
@OperationLog
<% } %>
<% if(swagger2) { %>
@ApiOperation("添加${table.comment!}")
@Operation(summary = "添加${table.comment!}")
<% } %>
<% if(!restControllerStyle) { %>
@ResponseBody
@@ -154,7 +154,7 @@ public class ${table.controllerName} {
@OperationLog
<% } %>
<% if(swagger2) { %>
@ApiOperation("修改${table.comment!}")
@Operation(summary = "修改${table.comment!}")
<% } %>
<% if(!restControllerStyle) { %>
@ResponseBody
@@ -179,7 +179,7 @@ public class ${table.controllerName} {
@OperationLog
<% } %>
<% if(swagger2) { %>
@ApiOperation("删除${table.comment!}")
@Operation(summary = "删除${table.comment!}")
<% } %>
<% if(!restControllerStyle) { %>
@ResponseBody
@@ -204,7 +204,7 @@ public class ${table.controllerName} {
@OperationLog
<% } %>
<% if(swagger2) { %>
@ApiOperation("批量添加${table.comment!}")
@Operation(summary = "批量添加${table.comment!}")
<% } %>
<% if(!restControllerStyle) { %>
@ResponseBody
@@ -229,7 +229,7 @@ public class ${table.controllerName} {
@OperationLog
<% } %>
<% if(swagger2) { %>
@ApiOperation("批量修改${table.comment!}")
@Operation(summary = "批量修改${table.comment!}")
<% } %>
<% if(!restControllerStyle) { %>
@ResponseBody
@@ -254,7 +254,7 @@ public class ${table.controllerName} {
@OperationLog
<% } %>
<% if(swagger2) { %>
@ApiOperation("批量删除${table.comment!}")
@Operation(summary = "批量删除${table.comment!}")
<% } %>
<% if(!restControllerStyle) { %>
@ResponseBody

View File

@@ -4,8 +4,7 @@ package ${package.Entity};
import ${pkg};
<% } %>
<% if(swagger2) { %>
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.v3.oas.annotations.media.Schema;
<% } %>
<% if(entityLombokModel) { %>
import lombok.Data;
@@ -33,7 +32,7 @@ import lombok.experimental.Accessors;
<% } %>
<% } %>
<% if(swagger2) { %>
@ApiModel(value = "${entity}对象", description = "${table.comment!''}")
@Schema(name = "${entity}对象", description = "${table.comment!''}")
<% } %>
<% if(table.convert) { %>
@TableName("${table.name}")
@@ -59,7 +58,7 @@ public class ${entity} implements Serializable {
<% if(isNotEmpty(field.comment)) { %>
<% if(swagger2) { %>
@ApiModelProperty(value = "${field.comment}")
@Schema(description = "${field.comment}")
<% }else{ %>
/**
* ${field.comment}

View File

@@ -1,5 +1,5 @@
import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
import type { ApiResult, PageResult } from '@/api/index';
import type { ${entity}, ${entity}Param } from './model';
import { MODULES_API_URL } from '@/config/setting';

View File

@@ -1,6 +1,5 @@
<template>
<div class="page">
<div class="ele-body">
<a-page-header :title="getPageTitle()" @back="() => $router.go(-1)">
<a-card :bordered="false" :body-style="{ padding: '16px' }">
<ele-pro-table
ref="tableRef"
@@ -46,8 +45,7 @@
<!-- 编辑弹窗 -->
<${entity}Edit v-model:visible="showEdit" :data="current" @done="reload" />
</div>
</div>
</a-page-header>
</template>
<script lang="ts" setup>
@@ -61,6 +59,7 @@
ColumnItem
} from 'ele-admin-pro/es/ele-pro-table/types';
import Search from './components/search.vue';
import {getPageTitle} from '@/utils/common';
import ${entity}Edit from './components/${table.entityPath}Edit.vue';
import { page${entity}, remove${entity}, removeBatch${entity} } from '@/api/${package.ModuleName}/${table.entityPath}';
import type { ${entity}, ${entity}Param } from '@/api/${package.ModuleName}/${table.entityPath}/model';

View File

@@ -1,4 +1,4 @@
import type { PageParam } from '@/api';
import type { PageParam } from '@/api/index';
/**
* ${table.comment!}

View File

@@ -6,8 +6,7 @@ import ${cfg.packageName!}.common.core.annotation.QueryType;
import ${cfg.packageName!}.common.core.web.BaseParam;
import com.fasterxml.jackson.annotation.JsonInclude;
<% if(swagger2) { %>
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.v3.oas.annotations.media.Schema;
<% } %>
<% if(entityLombokModel) { %>
import lombok.Data;
@@ -36,7 +35,7 @@ import lombok.experimental.Accessors;
<% } %>
@JsonInclude(JsonInclude.Include.NON_NULL)
<% if(swagger2) { %>
@ApiModel(value = "${entity}Param对象", description = "${table.comment!''}查询参数")
@Schema(name = "${entity}Param对象", description = "${table.comment!''}查询参数")
<% } %>
public class ${entity}Param extends BaseParam {
<% if(entitySerialVersionUID) { %>
@@ -57,7 +56,7 @@ public class ${entity}Param extends BaseParam {
<% if(isNotEmpty(field.comment)) { %>
<% if(swagger2) { %>
@ApiModelProperty(value = "${field.comment}")
@Schema(description = "${field.comment}")
<% }else{ %>
/**
* ${field.comment}

View File

@@ -0,0 +1,38 @@
package com.gxwebsoft.house.util;
/**
* SortSceneUtil手动测试类
* 用于验证URL解码功能
*
* @author 科技小王子
* @since 2025-08-04
*/
public class SortSceneUtilManualTest {
public static void main(String[] args) {
// 测试URL编码的参数
String urlEncoded = "%E4%BB%B7%E6%A0%BC(%E4%BD%8E-%E9%AB%98)";
System.out.println("原始URL编码参数: " + urlEncoded);
String result = SortSceneUtil.normalizeSortScene(urlEncoded);
System.out.println("标准化后的参数: " + result);
System.out.println("是否为价格升序: " + SortSceneUtil.isPriceAsc(urlEncoded));
// 测试其他格式
String[] testCases = {
"价格(低-高)",
"价格(高-低)",
"%E4%BB%B7%E6%A0%BC(%E9%AB%98-%E4%BD%8E)",
"最新发布",
"综合排序",
"面积(小-大)",
"面积(大-小)"
};
System.out.println("\n=== 测试各种排序场景 ===");
for (String testCase : testCases) {
String normalized = SortSceneUtil.normalizeSortScene(testCase);
System.out.println("输入: " + testCase + " -> 输出: " + normalized);
}
}
}

View File

@@ -0,0 +1,63 @@
package com.gxwebsoft.house.util;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
/**
* SortSceneUtil测试类
*
* @author 科技小王子
* @since 2025-08-04
*/
public class SortSceneUtilTest {
@Test
public void testNormalizeSortScene() {
// 测试URL编码的参数
String urlEncoded = "%E4%BB%B7%E6%A0%BC(%E4%BD%8E-%E9%AB%98)";
String result = SortSceneUtil.normalizeSortScene(urlEncoded);
assertEquals("价格(低-高)", result);
// 测试已解码的参数
String decoded = "价格(低-高)";
result = SortSceneUtil.normalizeSortScene(decoded);
assertEquals("价格(低-高)", result);
// 测试空值
result = SortSceneUtil.normalizeSortScene(null);
assertNull(result);
result = SortSceneUtil.normalizeSortScene("");
assertNull(result);
result = SortSceneUtil.normalizeSortScene(" ");
assertNull(result);
}
@Test
public void testIsPriceAsc() {
assertTrue(SortSceneUtil.isPriceAsc("价格(低-高)"));
assertTrue(SortSceneUtil.isPriceAsc("%E4%BB%B7%E6%A0%BC(%E4%BD%8E-%E9%AB%98)"));
assertFalse(SortSceneUtil.isPriceAsc("价格(高-低)"));
assertFalse(SortSceneUtil.isPriceAsc("最新发布"));
}
@Test
public void testIsPriceDesc() {
assertTrue(SortSceneUtil.isPriceDesc("价格(高-低)"));
assertFalse(SortSceneUtil.isPriceDesc("价格(低-高)"));
assertFalse(SortSceneUtil.isPriceDesc("最新发布"));
}
@Test
public void testIsLatest() {
assertTrue(SortSceneUtil.isLatest("最新发布"));
assertFalse(SortSceneUtil.isLatest("价格(低-高)"));
}
@Test
public void testIsComprehensive() {
assertTrue(SortSceneUtil.isComprehensive("综合排序"));
assertFalse(SortSceneUtil.isComprehensive("价格(低-高)"));
}
}

View File

@@ -0,0 +1,174 @@
package com.gxwebsoft.shop;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.shop.dto.OrderCreateRequest;
import com.gxwebsoft.shop.entity.ShopGoods;
import com.gxwebsoft.shop.entity.ShopGoodsSku;
import com.gxwebsoft.shop.service.OrderBusinessService;
import com.gxwebsoft.shop.service.ShopGoodsService;
import com.gxwebsoft.shop.service.ShopGoodsSkuService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
/**
* 多规格订单测试类
*
* @author 科技小王子
* @since 2025-07-30
*/
@ExtendWith(MockitoExtension.class)
public class MultiSpecOrderTest {
@Mock
private ShopGoodsService shopGoodsService;
@Mock
private ShopGoodsSkuService shopGoodsSkuService;
@InjectMocks
private OrderBusinessService orderBusinessService;
private User testUser;
private ShopGoods testGoods;
private ShopGoodsSku testSku;
@BeforeEach
void setUp() {
// 创建测试用户
testUser = new User();
testUser.setUserId(1);
testUser.setTenantId(1);
testUser.setOpenid("test_openid");
testUser.setPhone("13800138000");
// 创建测试商品
testGoods = new ShopGoods();
testGoods.setGoodsId(1);
testGoods.setName("测试商品");
testGoods.setPrice(new BigDecimal("100.00"));
testGoods.setStock(50);
testGoods.setStatus(0); // 正常状态
testGoods.setImage("test.jpg");
// 创建测试SKU
testSku = new ShopGoodsSku();
testSku.setId(1);
testSku.setGoodsId(1);
testSku.setPrice(new BigDecimal("120.00"));
testSku.setStock(20);
testSku.setStatus(0); // 正常状态
testSku.setSku("颜色:红色|尺寸:L");
testSku.setImage("sku_test.jpg");
}
@Test
void testCreateOrderWithSingleSpec() {
// 测试单规格商品下单
when(shopGoodsService.getById(1)).thenReturn(testGoods);
OrderCreateRequest request = createOrderRequest(false);
// 这里需要mock其他依赖服务实际测试中需要完整的Spring上下文
// 此测试主要验证多规格逻辑的正确性
assertNotNull(request);
assertEquals(1, request.getGoodsItems().size());
assertNull(request.getGoodsItems().get(0).getSkuId());
}
@Test
void testCreateOrderWithMultiSpec() {
// 测试多规格商品下单
when(shopGoodsService.getById(1)).thenReturn(testGoods);
when(shopGoodsSkuService.getById(1)).thenReturn(testSku);
OrderCreateRequest request = createOrderRequest(true);
assertNotNull(request);
assertEquals(1, request.getGoodsItems().size());
assertEquals(Integer.valueOf(1), request.getGoodsItems().get(0).getSkuId());
assertEquals("颜色:红色|尺寸:L", request.getGoodsItems().get(0).getSpecInfo());
}
@Test
void testSkuValidation() {
// 测试SKU验证逻辑
when(shopGoodsService.getById(1)).thenReturn(testGoods);
// 测试SKU不存在的情况
when(shopGoodsSkuService.getById(999)).thenReturn(null);
OrderCreateRequest request = createOrderRequest(true);
request.getGoodsItems().get(0).setSkuId(999); // 不存在的SKU ID
// 在实际测试中这里应该抛出BusinessException
// assertThrows(BusinessException.class, () -> orderBusinessService.createOrder(request, testUser));
}
@Test
void testStockValidation() {
// 测试库存验证
testSku.setStock(1); // 设置库存为1
when(shopGoodsService.getById(1)).thenReturn(testGoods);
when(shopGoodsSkuService.getById(1)).thenReturn(testSku);
OrderCreateRequest request = createOrderRequest(true);
request.getGoodsItems().get(0).setQuantity(5); // 购买数量超过库存
// 在实际测试中这里应该抛出BusinessException
// assertThrows(BusinessException.class, () -> orderBusinessService.createOrder(request, testUser));
}
@Test
void testPriceCalculation() {
// 测试价格计算
when(shopGoodsService.getById(1)).thenReturn(testGoods);
when(shopGoodsSkuService.getById(1)).thenReturn(testSku);
// 多规格商品应该使用SKU价格120.00而不是商品价格100.00
OrderCreateRequest request = createOrderRequest(true);
request.getGoodsItems().get(0).setQuantity(2);
// 期望总价格 = SKU价格(120.00) * 数量(2) = 240.00
BigDecimal expectedTotal = new BigDecimal("240.00");
request.setTotalPrice(expectedTotal);
assertEquals(expectedTotal, request.getTotalPrice());
}
/**
* 创建订单请求对象
*/
private OrderCreateRequest createOrderRequest(boolean withSku) {
OrderCreateRequest request = new OrderCreateRequest();
request.setType(0);
request.setTotalPrice(new BigDecimal("100.00"));
request.setPayPrice(new BigDecimal("100.00"));
request.setTotalNum(1);
request.setTenantId(1);
OrderCreateRequest.OrderGoodsItem item = new OrderCreateRequest.OrderGoodsItem();
item.setGoodsId(1);
item.setQuantity(1);
if (withSku) {
item.setSkuId(1);
item.setSpecInfo("颜色:红色|尺寸:L");
}
request.setGoodsItems(Arrays.asList(item));
return request;
}
}

View File

@@ -0,0 +1,170 @@
package com.gxwebsoft.shop;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.shop.dto.OrderCreateRequest;
import com.gxwebsoft.shop.entity.ShopGoods;
import com.gxwebsoft.shop.entity.ShopOrder;
import com.gxwebsoft.shop.entity.ShopOrderGoods;
import com.gxwebsoft.shop.service.OrderBusinessService;
import com.gxwebsoft.shop.service.ShopGoodsService;
import com.gxwebsoft.shop.service.ShopOrderGoodsService;
import com.gxwebsoft.shop.service.ShopOrderService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
/**
* 订单业务服务测试类
*
* @author 科技小王子
* @since 2025-01-26
*/
@ExtendWith(MockitoExtension.class)
public class OrderBusinessServiceTest {
@Mock
private ShopOrderService shopOrderService;
@Mock
private ShopOrderGoodsService shopOrderGoodsService;
@Mock
private ShopGoodsService shopGoodsService;
@InjectMocks
private OrderBusinessService orderBusinessService;
private User testUser;
private OrderCreateRequest testRequest;
private ShopGoods testGoods;
@BeforeEach
void setUp() {
// 准备测试用户
testUser = new User();
testUser.setUserId(1);
testUser.setOpenid("test_openid");
testUser.setPhone("13800138000");
// 准备测试商品
testGoods = new ShopGoods();
testGoods.setGoodsId(10018);
testGoods.setName("科技小王子大米年卡套餐2.5kg");
testGoods.setPrice(new BigDecimal("99.00"));
testGoods.setImage("test_image.jpg");
// 准备测试订单请求
testRequest = new OrderCreateRequest();
testRequest.setType(0);
testRequest.setTotalPrice(new BigDecimal("99.00"));
testRequest.setPayPrice(new BigDecimal("99.00"));
testRequest.setTotalNum(1);
testRequest.setPayType(1);
testRequest.setTenantId(1);
testRequest.setAddressId(10832);
testRequest.setComments("科技小王子大米年卡套餐2.5kg");
testRequest.setDeliveryType(0);
// 准备商品项列表
OrderCreateRequest.OrderGoodsItem goodsItem = new OrderCreateRequest.OrderGoodsItem();
goodsItem.setGoodsId(10018);
goodsItem.setQuantity(1);
goodsItem.setPayType(1);
testRequest.setGoodsItems(Arrays.asList(goodsItem));
}
@Test
void testCreateOrderWithGoods() {
// Mock 商品查询
when(shopGoodsService.getById(10018)).thenReturn(testGoods);
// Mock 订单保存
when(shopOrderService.save(any(ShopOrder.class))).thenAnswer(invocation -> {
ShopOrder order = invocation.getArgument(0);
order.setOrderId(1); // 模拟数据库生成的ID
return true;
});
// Mock 订单商品批量保存
when(shopOrderGoodsService.saveBatch(anyList())).thenReturn(true);
// Mock 微信支付订单创建
HashMap<String, String> wxOrderInfo = new HashMap<>();
wxOrderInfo.put("prepay_id", "test_prepay_id");
when(shopOrderService.createWxOrder(any(ShopOrder.class))).thenReturn(wxOrderInfo);
// 执行测试
Map<String, String> result = orderBusinessService.createOrder(testRequest, testUser);
// 验证结果
assert result != null;
assert result.containsKey("prepay_id");
// 验证方法调用
verify(shopGoodsService, times(1)).getById(10018);
verify(shopOrderService, times(1)).save(any(ShopOrder.class));
verify(shopOrderGoodsService, times(1)).saveBatch(anyList());
verify(shopOrderService, times(1)).createWxOrder(any(ShopOrder.class));
}
@Test
void testCreateOrderWithMultipleGoods() {
// 准备多个商品项
OrderCreateRequest.OrderGoodsItem goodsItem1 = new OrderCreateRequest.OrderGoodsItem();
goodsItem1.setGoodsId(10018);
goodsItem1.setQuantity(1);
goodsItem1.setPayType(1);
OrderCreateRequest.OrderGoodsItem goodsItem2 = new OrderCreateRequest.OrderGoodsItem();
goodsItem2.setGoodsId(10019);
goodsItem2.setQuantity(2);
goodsItem2.setPayType(1);
testRequest.setGoodsItems(Arrays.asList(goodsItem1, goodsItem2));
testRequest.setTotalPrice(new BigDecimal("297.00")); // 99 + 99*2
// Mock 商品查询
when(shopGoodsService.getById(10018)).thenReturn(testGoods);
ShopGoods testGoods2 = new ShopGoods();
testGoods2.setGoodsId(10019);
testGoods2.setName("测试商品2");
testGoods2.setPrice(new BigDecimal("99.00"));
testGoods2.setImage("test_image2.jpg");
when(shopGoodsService.getById(10019)).thenReturn(testGoods2);
// Mock 其他服务
when(shopOrderService.save(any(ShopOrder.class))).thenAnswer(invocation -> {
ShopOrder order = invocation.getArgument(0);
order.setOrderId(1);
return true;
});
when(shopOrderGoodsService.saveBatch(anyList())).thenReturn(true);
when(shopOrderService.createWxOrder(any(ShopOrder.class))).thenReturn(new HashMap<>());
// 执行测试
orderBusinessService.createOrder(testRequest, testUser);
// 验证商品查询次数
verify(shopGoodsService, times(1)).getById(10018);
verify(shopGoodsService, times(1)).getById(10019);
// 验证保存的商品项数量
verify(shopOrderGoodsService, times(1)).saveBatch(argThat(list ->
((List<ShopOrderGoods>) list).size() == 2
));
}
}

View File

@@ -0,0 +1,56 @@
package com.gxwebsoft.shop;
import com.gxwebsoft.shop.service.ShopOrderService;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import javax.annotation.Resource;
import java.math.BigDecimal;
import static org.junit.jupiter.api.Assertions.*;
/**
* 订单总金额统计测试
*
* @author 科技小王子
* @since 2025-07-30
*/
@SpringBootTest
@ActiveProfiles("test")
public class OrderTotalTest {
@Resource
private ShopOrderService shopOrderService;
@Test
void testOrderTotal() {
// 测试订单总金额统计
BigDecimal total = shopOrderService.total();
// 验证返回值不为null
assertNotNull(total, "订单总金额不应该为null");
// 验证返回值大于等于0
assertTrue(total.compareTo(BigDecimal.ZERO) >= 0, "订单总金额应该大于等于0");
System.out.println("订单总金额统计结果:" + total);
}
@Test
void testOrderTotalPerformance() {
// 测试性能
long startTime = System.currentTimeMillis();
BigDecimal total = shopOrderService.total();
long endTime = System.currentTimeMillis();
long duration = endTime - startTime;
System.out.println("订单总金额统计耗时:" + duration + "ms");
System.out.println("统计结果:" + total);
// 验证查询时间在合理范围内小于5秒
assertTrue(duration < 5000, "查询时间应该在5秒以内");
}
}

View File

@@ -0,0 +1,315 @@
package com.gxwebsoft.shop;
import com.gxwebsoft.common.core.exception.BusinessException;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.shop.config.OrderConfigProperties;
import com.gxwebsoft.shop.dto.OrderCreateRequest;
import com.gxwebsoft.shop.entity.ShopGoods;
import com.gxwebsoft.shop.entity.ShopOrder;
import com.gxwebsoft.shop.service.OrderBusinessService;
import com.gxwebsoft.shop.service.ShopGoodsService;
import com.gxwebsoft.shop.service.ShopOrderGoodsService;
import com.gxwebsoft.shop.service.ShopOrderService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import java.math.BigDecimal;
import java.util.Arrays;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
/**
* 订单验证测试类
* 测试商品信息后台验证逻辑
*/
@ExtendWith(MockitoExtension.class)
class OrderValidationTest {
@Mock
private ShopOrderService shopOrderService;
@Mock
private ShopOrderGoodsService shopOrderGoodsService;
@Mock
private ShopGoodsService shopGoodsService;
@Mock
private OrderConfigProperties orderConfig;
@InjectMocks
private OrderBusinessService orderBusinessService;
private User testUser;
private OrderCreateRequest testRequest;
private ShopGoods testGoods;
@BeforeEach
void setUp() {
// 准备测试用户
testUser = new User();
testUser.setUserId(1);
testUser.setNickname("测试用户");
testUser.setPhone("13800138000");
// 准备测试商品
testGoods = new ShopGoods();
testGoods.setGoodsId(10018);
testGoods.setName("测试商品");
testGoods.setPrice(new BigDecimal("99.00"));
testGoods.setStatus(0); // 上架状态
testGoods.setStock(100); // 库存100
testGoods.setCanBuyNumber(10); // 最大购买数量10
testGoods.setCode("TEST001");
// 准备测试订单请求
testRequest = new OrderCreateRequest();
testRequest.setType(0);
testRequest.setTitle("测试订单");
testRequest.setTotalPrice(new BigDecimal("99.00"));
testRequest.setTenantId(1);
// 准备商品项
OrderCreateRequest.OrderGoodsItem goodsItem = new OrderCreateRequest.OrderGoodsItem();
goodsItem.setGoodsId(10018);
goodsItem.setQuantity(1);
testRequest.setGoodsItems(Arrays.asList(goodsItem));
}
@Test
void testValidateOrderRequest_Success() {
// Mock 商品查询
when(shopGoodsService.getById(10018)).thenReturn(testGoods);
when(orderConfig.getTenantRule(1)).thenReturn(null);
// 执行验证 - 应该成功
assertDoesNotThrow(() -> {
// 使用反射调用私有方法进行测试
java.lang.reflect.Method method = OrderBusinessService.class
.getDeclaredMethod("validateOrderRequest", OrderCreateRequest.class, User.class);
method.setAccessible(true);
method.invoke(orderBusinessService, testRequest, testUser);
});
// 验证总金额被正确设置
assertEquals(new BigDecimal("99.00"), testRequest.getTotalPrice());
}
@Test
void testValidateOrderRequest_GoodsNotFound() {
// Mock 商品不存在
when(shopGoodsService.getById(10018)).thenReturn(null);
// 执行验证 - 应该抛出异常
Exception exception = assertThrows(Exception.class, () -> {
java.lang.reflect.Method method = OrderBusinessService.class
.getDeclaredMethod("validateOrderRequest", OrderCreateRequest.class, User.class);
method.setAccessible(true);
method.invoke(orderBusinessService, testRequest, testUser);
});
// 检查是否是 InvocationTargetException 包装的 BusinessException
assertTrue(exception instanceof java.lang.reflect.InvocationTargetException);
Throwable cause = exception.getCause();
assertTrue(cause instanceof BusinessException);
assertTrue(cause.getMessage().contains("商品不存在"));
}
@Test
void testValidateOrderRequest_GoodsOffShelf() {
// 设置商品为下架状态
testGoods.setStatus(1);
when(shopGoodsService.getById(10018)).thenReturn(testGoods);
// 执行验证 - 应该抛出异常
Exception exception = assertThrows(Exception.class, () -> {
java.lang.reflect.Method method = OrderBusinessService.class
.getDeclaredMethod("validateOrderRequest", OrderCreateRequest.class, User.class);
method.setAccessible(true);
method.invoke(orderBusinessService, testRequest, testUser);
});
// 检查是否是 InvocationTargetException 包装的 BusinessException
assertTrue(exception instanceof java.lang.reflect.InvocationTargetException);
Throwable cause = exception.getCause();
assertTrue(cause instanceof BusinessException);
assertTrue(cause.getMessage().contains("商品已下架"));
}
@Test
void testValidateOrderRequest_InsufficientStock() {
// 设置库存不足
testGoods.setStock(0);
when(shopGoodsService.getById(10018)).thenReturn(testGoods);
// 执行验证 - 应该抛出异常
Exception exception = assertThrows(Exception.class, () -> {
java.lang.reflect.Method method = OrderBusinessService.class
.getDeclaredMethod("validateOrderRequest", OrderCreateRequest.class, User.class);
method.setAccessible(true);
method.invoke(orderBusinessService, testRequest, testUser);
});
// 检查是否是 InvocationTargetException 包装的 BusinessException
assertTrue(exception instanceof java.lang.reflect.InvocationTargetException);
Throwable cause = exception.getCause();
assertTrue(cause instanceof BusinessException);
assertTrue(cause.getMessage().contains("商品库存不足"));
}
@Test
void testValidateOrderRequest_ExceedBuyLimit() {
// 设置购买数量超过限制
testRequest.getGoodsItems().get(0).setQuantity(15); // 超过最大购买数量10
when(shopGoodsService.getById(10018)).thenReturn(testGoods);
// 执行验证 - 应该抛出异常
Exception exception = assertThrows(Exception.class, () -> {
java.lang.reflect.Method method = OrderBusinessService.class
.getDeclaredMethod("validateOrderRequest", OrderCreateRequest.class, User.class);
method.setAccessible(true);
method.invoke(orderBusinessService, testRequest, testUser);
});
// 检查是否是 InvocationTargetException 包装的 BusinessException
assertTrue(exception instanceof java.lang.reflect.InvocationTargetException);
Throwable cause = exception.getCause();
assertTrue(cause instanceof BusinessException);
assertTrue(cause.getMessage().contains("购买数量超过限制"));
}
@Test
void testValidateOrderRequest_PriceCalculation() {
// 设置多个商品项
OrderCreateRequest.OrderGoodsItem goodsItem1 = new OrderCreateRequest.OrderGoodsItem();
goodsItem1.setGoodsId(10018);
goodsItem1.setQuantity(2);
OrderCreateRequest.OrderGoodsItem goodsItem2 = new OrderCreateRequest.OrderGoodsItem();
goodsItem2.setGoodsId(10019);
goodsItem2.setQuantity(1);
testRequest.setGoodsItems(Arrays.asList(goodsItem1, goodsItem2));
testRequest.setTotalPrice(new BigDecimal("297.00")); // 99*2 + 99*1
// 准备第二个商品
ShopGoods testGoods2 = new ShopGoods();
testGoods2.setGoodsId(10019);
testGoods2.setName("测试商品2");
testGoods2.setPrice(new BigDecimal("99.00"));
testGoods2.setStatus(0);
testGoods2.setStock(100);
when(shopGoodsService.getById(10018)).thenReturn(testGoods);
when(shopGoodsService.getById(10019)).thenReturn(testGoods2);
when(orderConfig.getTenantRule(1)).thenReturn(null);
// 执行验证 - 应该成功
assertDoesNotThrow(() -> {
java.lang.reflect.Method method = OrderBusinessService.class
.getDeclaredMethod("validateOrderRequest", OrderCreateRequest.class, User.class);
method.setAccessible(true);
method.invoke(orderBusinessService, testRequest, testUser);
});
// 验证总金额计算正确
assertEquals(new BigDecimal("297.00"), testRequest.getTotalPrice());
}
@Test
void testValidateOrderRequest_PriceDiscrepancy() {
// 设置前端传入的金额与后台计算不一致
testRequest.setTotalPrice(new BigDecimal("88.00")); // 错误的金额
when(shopGoodsService.getById(10018)).thenReturn(testGoods);
// 执行验证 - 应该抛出异常
Exception exception = assertThrows(Exception.class, () -> {
java.lang.reflect.Method method = OrderBusinessService.class
.getDeclaredMethod("validateOrderRequest", OrderCreateRequest.class, User.class);
method.setAccessible(true);
method.invoke(orderBusinessService, testRequest, testUser);
});
// 检查是否是 InvocationTargetException 包装的 BusinessException
assertTrue(exception instanceof java.lang.reflect.InvocationTargetException);
Throwable cause = exception.getCause();
assertTrue(cause instanceof BusinessException);
assertTrue(cause.getMessage().contains("订单金额计算错误"));
}
@Test
void testBuildShopOrder_RequiredFields() throws Exception {
// Mock 配置
OrderConfigProperties.DefaultConfig defaultConfig = new OrderConfigProperties.DefaultConfig();
defaultConfig.setDefaultComments("默认备注");
when(orderConfig.getDefaultConfig()).thenReturn(defaultConfig);
// 使用反射调用 buildShopOrder 方法
java.lang.reflect.Method buildMethod = OrderBusinessService.class
.getDeclaredMethod("buildShopOrder", OrderCreateRequest.class, User.class);
buildMethod.setAccessible(true);
ShopOrder result = (ShopOrder) buildMethod.invoke(orderBusinessService, testRequest, testUser);
// 验证必需字段都已设置
assertNotNull(result.getPayPrice(), "payPrice 不能为空");
assertNotNull(result.getPrice(), "price 不能为空");
assertNotNull(result.getReducePrice(), "reducePrice 不能为空");
assertNotNull(result.getMoney(), "money 不能为空");
assertNotNull(result.getPayStatus(), "payStatus 不能为空");
assertNotNull(result.getOrderStatus(), "orderStatus 不能为空");
assertNotNull(result.getDeliveryStatus(), "deliveryStatus 不能为空");
assertNotNull(result.getPayType(), "payType 不能为空");
// 验证默认值
assertEquals(testRequest.getTotalPrice(), result.getPayPrice());
assertEquals(testRequest.getTotalPrice(), result.getPrice());
assertEquals(BigDecimal.ZERO, result.getReducePrice());
assertEquals(testRequest.getTotalPrice(), result.getMoney());
assertEquals(false, result.getPayStatus());
assertEquals(Integer.valueOf(0), result.getOrderStatus());
assertEquals(Integer.valueOf(10), result.getDeliveryStatus());
assertEquals(Integer.valueOf(1), result.getPayType());
// 验证关键字段 - 租户ID影响微信支付证书路径
assertNotNull(result.getTenantId(), "tenantId 不能为空");
assertEquals(testRequest.getTenantId(), result.getTenantId(), "tenantId 必须正确设置");
}
@Test
void testBuildShopOrder_TenantIdValidation() throws Exception {
// 创建一个新的请求对象租户ID为空
OrderCreateRequest requestWithoutTenant = new OrderCreateRequest();
requestWithoutTenant.setType(0);
requestWithoutTenant.setTitle("测试订单");
requestWithoutTenant.setTotalPrice(new BigDecimal("99.00"));
requestWithoutTenant.setTenantId(null); // 设置为空
// 准备商品项
OrderCreateRequest.OrderGoodsItem goodsItem = new OrderCreateRequest.OrderGoodsItem();
goodsItem.setGoodsId(10018);
goodsItem.setQuantity(1);
requestWithoutTenant.setGoodsItems(Arrays.asList(goodsItem));
// 使用反射调用 buildShopOrder 方法
java.lang.reflect.Method buildMethod = OrderBusinessService.class
.getDeclaredMethod("buildShopOrder", OrderCreateRequest.class, User.class);
buildMethod.setAccessible(true);
// 执行验证 - 应该抛出异常
Exception exception = assertThrows(Exception.class, () -> {
buildMethod.invoke(orderBusinessService, requestWithoutTenant, testUser);
});
// 检查是否是 InvocationTargetException 包装的 BusinessException
assertTrue(exception instanceof java.lang.reflect.InvocationTargetException);
Throwable cause = exception.getCause();
assertTrue(cause instanceof BusinessException);
assertTrue(cause.getMessage().contains("租户ID不能为空"));
}
}

View File

@@ -0,0 +1,75 @@
package com.gxwebsoft.shop;
import com.gxwebsoft.common.core.utils.WechatPayUtils;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import java.nio.charset.StandardCharsets;
import static org.junit.jupiter.api.Assertions.*;
/**
* 微信支付商品描述字段测试
* 验证字节长度限制处理
*/
@SpringBootTest
public class WechatPayDescriptionTest {
@Test
public void testTruncateToByteLimit() {
// 测试正常情况 - 字符串长度在限制内
String shortText = "正常商品描述";
String result1 = WechatPayUtils.truncateToByteLimit(shortText, 127);
assertEquals(shortText, result1);
assertTrue(result1.getBytes(StandardCharsets.UTF_8).length <= 127);
// 测试超长中文字符串
String longText = "【湾区认证】【百千万工程帮扶产品 通过远方320项检测 湾区认证 丰江桥佛手瓜面】独特风味低脂轻食便捷速食非油炸 720g/袋";
String result2 = WechatPayUtils.truncateToByteLimit(longText, 127);
assertNotNull(result2);
assertTrue(result2.getBytes(StandardCharsets.UTF_8).length <= 127);
System.out.println("原始文本字节数: " + longText.getBytes(StandardCharsets.UTF_8).length);
System.out.println("截断后文本: " + result2);
System.out.println("截断后字节数: " + result2.getBytes(StandardCharsets.UTF_8).length);
// 测试null和空字符串
assertNull(WechatPayUtils.truncateToByteLimit(null, 127));
assertEquals("", WechatPayUtils.truncateToByteLimit("", 127));
// 测试英文字符串
String englishText = "This is a very long English description that might exceed the byte limit for WeChat Pay description field";
String result3 = WechatPayUtils.truncateToByteLimit(englishText, 127);
assertNotNull(result3);
assertTrue(result3.getBytes(StandardCharsets.UTF_8).length <= 127);
// 测试混合字符串
String mixedText = "Product Name 产品名称 with special characters @#$%^&*()";
String result4 = WechatPayUtils.truncateToByteLimit(mixedText, 50);
assertNotNull(result4);
assertTrue(result4.getBytes(StandardCharsets.UTF_8).length <= 50);
}
@Test
public void testSpecificErrorCase() {
// 测试错误信息中的具体案例
String errorText = "【湾区认证】【百千万工程帮扶产品 通过远方320项检测 湾区认证 丰江桥佛手瓜面】独特风味低脂轻食便捷速食非油炸 720g/袋";
// 验证原始文本确实超过127字节
int originalBytes = errorText.getBytes(StandardCharsets.UTF_8).length;
System.out.println("原始文本: " + errorText);
System.out.println("原始字节数: " + originalBytes);
assertTrue(originalBytes > 127, "原始文本应该超过127字节");
// 测试截断
String truncated = WechatPayUtils.processDescription(errorText);
int truncatedBytes = truncated.getBytes(StandardCharsets.UTF_8).length;
System.out.println("截断后文本: " + truncated);
System.out.println("截断后字节数: " + truncatedBytes);
// 验证截断后的文本符合要求
assertNotNull(truncated);
assertTrue(truncatedBytes <= 127, "截断后字节数应该不超过127");
assertFalse(truncated.contains("\uFFFD"), "截断后的文本不应包含无效字符");
}
}

View File

@@ -0,0 +1,156 @@
package com.gxwebsoft.shop.utils;
import com.gxwebsoft.shop.entity.ShopUserCoupon;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import java.math.BigDecimal;
import java.time.LocalDateTime;
/**
* CouponUtils 测试类
*/
public class CouponUtilsTest {
@Test
public void testGetTypeName() {
assertEquals("满减券", CouponUtils.getTypeName(ShopUserCoupon.TYPE_REDUCE));
assertEquals("折扣券", CouponUtils.getTypeName(ShopUserCoupon.TYPE_DISCOUNT));
assertEquals("免费券", CouponUtils.getTypeName(ShopUserCoupon.TYPE_FREE));
assertEquals("未知", CouponUtils.getTypeName(null));
assertEquals("未知", CouponUtils.getTypeName(99));
}
@Test
public void testGetStatusName() {
assertEquals("未使用", CouponUtils.getStatusName(ShopUserCoupon.STATUS_UNUSED));
assertEquals("已使用", CouponUtils.getStatusName(ShopUserCoupon.STATUS_USED));
assertEquals("已过期", CouponUtils.getStatusName(ShopUserCoupon.STATUS_EXPIRED));
assertEquals("未知", CouponUtils.getStatusName(null));
assertEquals("未知", CouponUtils.getStatusName(99));
}
@Test
public void testGetApplyRangeName() {
assertEquals("全部商品", CouponUtils.getApplyRangeName(ShopUserCoupon.APPLY_ALL));
assertEquals("指定商品", CouponUtils.getApplyRangeName(ShopUserCoupon.APPLY_GOODS));
assertEquals("指定分类", CouponUtils.getApplyRangeName(ShopUserCoupon.APPLY_CATEGORY));
assertEquals("未知", CouponUtils.getApplyRangeName(null));
assertEquals("未知", CouponUtils.getApplyRangeName(99));
}
@Test
public void testCalculateDiscountAmount() {
// 测试满减券
ShopUserCoupon reduceCoupon = new ShopUserCoupon();
reduceCoupon.setType(ShopUserCoupon.TYPE_REDUCE);
reduceCoupon.setReducePrice(new BigDecimal("10.00"));
reduceCoupon.setMinPrice(new BigDecimal("50.00"));
BigDecimal discount = CouponUtils.calculateDiscountAmount(reduceCoupon, new BigDecimal("100.00"));
assertEquals(0, new BigDecimal("10.00").compareTo(discount));
// 测试不满足最低消费
discount = CouponUtils.calculateDiscountAmount(reduceCoupon, new BigDecimal("30.00"));
assertEquals(0, BigDecimal.ZERO.compareTo(discount));
// 测试折扣券
ShopUserCoupon discountCoupon = new ShopUserCoupon();
discountCoupon.setType(ShopUserCoupon.TYPE_DISCOUNT);
discountCoupon.setDiscount(80); // 8折
discountCoupon.setMinPrice(new BigDecimal("50.00"));
discount = CouponUtils.calculateDiscountAmount(discountCoupon, new BigDecimal("100.00"));
assertEquals(0, new BigDecimal("20.0000").compareTo(discount));
// 测试免费券
ShopUserCoupon freeCoupon = new ShopUserCoupon();
freeCoupon.setType(ShopUserCoupon.TYPE_FREE);
discount = CouponUtils.calculateDiscountAmount(freeCoupon, new BigDecimal("100.00"));
assertEquals(0, new BigDecimal("100.00").compareTo(discount));
}
@Test
public void testIsApplicableToGoods() {
// 测试全部商品可用
ShopUserCoupon allCoupon = new ShopUserCoupon();
allCoupon.setApplyRange(ShopUserCoupon.APPLY_ALL);
assertTrue(CouponUtils.isApplicableToGoods(allCoupon, 123, 456));
// 测试指定商品可用
ShopUserCoupon goodsCoupon = new ShopUserCoupon();
goodsCoupon.setApplyRange(ShopUserCoupon.APPLY_GOODS);
goodsCoupon.setApplyRangeConfig("123,456,789");
assertTrue(CouponUtils.isApplicableToGoods(goodsCoupon, 123, 999));
assertFalse(CouponUtils.isApplicableToGoods(goodsCoupon, 999, 999));
// 测试指定分类可用
ShopUserCoupon categoryCoupon = new ShopUserCoupon();
categoryCoupon.setApplyRange(ShopUserCoupon.APPLY_CATEGORY);
categoryCoupon.setApplyRangeConfig("10,20,30");
assertTrue(CouponUtils.isApplicableToGoods(categoryCoupon, 999, 20));
assertFalse(CouponUtils.isApplicableToGoods(categoryCoupon, 999, 99));
}
@Test
public void testIsExpired() {
ShopUserCoupon coupon = new ShopUserCoupon();
// 测试未过期
coupon.setEndTime(LocalDateTime.now().plusDays(1));
assertFalse(CouponUtils.isExpired(coupon));
// 测试已过期
coupon.setEndTime(LocalDateTime.now().minusDays(1));
assertTrue(CouponUtils.isExpired(coupon));
// 测试无结束时间
coupon.setEndTime(null);
assertFalse(CouponUtils.isExpired(coupon));
}
@Test
public void testIsAvailable() {
ShopUserCoupon coupon = new ShopUserCoupon();
coupon.setStatus(ShopUserCoupon.STATUS_UNUSED);
coupon.setStartTime(LocalDateTime.now().minusDays(1));
coupon.setEndTime(LocalDateTime.now().plusDays(1));
assertTrue(CouponUtils.isAvailable(coupon));
// 测试已使用
coupon.setStatus(ShopUserCoupon.STATUS_USED);
assertFalse(CouponUtils.isAvailable(coupon));
// 测试已过期
coupon.setStatus(ShopUserCoupon.STATUS_UNUSED);
coupon.setEndTime(LocalDateTime.now().minusDays(1));
assertFalse(CouponUtils.isAvailable(coupon));
// 测试还未开始
coupon.setEndTime(LocalDateTime.now().plusDays(1));
coupon.setStartTime(LocalDateTime.now().plusDays(1));
assertFalse(CouponUtils.isAvailable(coupon));
}
@Test
public void testIsValidCouponCode() {
assertTrue(CouponUtils.isValidCouponCode("CPN00000001000001123456"));
assertFalse(CouponUtils.isValidCouponCode("CPN123"));
assertFalse(CouponUtils.isValidCouponCode("ABC00000001000001123456"));
assertFalse(CouponUtils.isValidCouponCode(null));
}
@Test
public void testGenerateCouponCode() {
String code = CouponUtils.generateCouponCode(1, 1);
assertNotNull(code);
assertTrue(code.startsWith("CPN"));
assertEquals(23, code.length());
assertTrue(CouponUtils.isValidCouponCode(code));
}
}

View File

@@ -0,0 +1,155 @@
package com.gxwebsoft.test;
import com.gxwebsoft.common.core.config.ConfigProperties;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.beans.factory.annotation.Value;
import javax.annotation.Resource;
import java.io.File;
/**
* 证书路径拼接测试
* 验证开发环境的路径拼接规则配置文件upload-path + dev/wechat/ + 租户ID
*
* @author 科技小王子
* @since 2025-08-09
*/
@SpringBootTest
@ActiveProfiles("dev")
public class CertificatePathConcatenationTest {
@Value("${spring.profiles.active:prod}")
private String activeProfile;
@Resource
private ConfigProperties configProperties;
@Test
public void testCertificatePathConcatenation() {
System.out.println("=== 证书路径拼接测试 ===");
System.out.println("当前环境: " + activeProfile);
if ("dev".equals(activeProfile)) {
testDevEnvironmentPathConcatenation();
} else {
testProdEnvironmentPathConcatenation();
}
System.out.println("=== 证书路径拼接测试完成 ===");
}
private void testDevEnvironmentPathConcatenation() {
System.out.println("--- 开发环境路径拼接测试 ---");
// 获取配置文件中的upload-path
String uploadPath = configProperties.getUploadPath();
System.out.println("配置文件upload-path: " + uploadPath);
// 拼接规则配置文件upload-path + dev/wechat/ + 租户ID
String tenantId = "10550";
String certBasePath = uploadPath + "dev/wechat/" + tenantId + "/";
String privateKeyPath = certBasePath + "apiclient_key.pem";
String certPath = certBasePath + "apiclient_cert.pem";
System.out.println("拼接规则: upload-path + dev/wechat/ + 租户ID");
System.out.println("租户ID: " + tenantId);
System.out.println("证书基础路径: " + certBasePath);
System.out.println("私钥文件路径: " + privateKeyPath);
System.out.println("证书文件路径: " + certPath);
// 验证路径是否正确
File privateKeyFile = new File(privateKeyPath);
File certFile = new File(certPath);
System.out.println("--- 文件存在性验证 ---");
System.out.println("私钥文件存在: " + privateKeyFile.exists());
System.out.println("证书文件存在: " + certFile.exists());
if (privateKeyFile.exists()) {
System.out.println("✅ 私钥文件路径拼接正确");
System.out.println(" 文件大小: " + privateKeyFile.length() + " bytes");
} else {
System.out.println("❌ 私钥文件路径拼接错误或文件不存在");
}
if (certFile.exists()) {
System.out.println("✅ 证书文件路径拼接正确");
System.out.println(" 文件大小: " + certFile.length() + " bytes");
} else {
System.out.println("❌ 证书文件路径拼接错误或文件不存在");
}
// 验证期望的路径
String expectedPath = "/Users/gxwebsoft/JAVA/cms-java-code/src/main/resources/dev/wechat/10550/";
System.out.println("--- 路径验证 ---");
System.out.println("期望的证书路径: " + expectedPath);
System.out.println("实际拼接路径: " + certBasePath);
System.out.println("路径匹配: " + expectedPath.equals(certBasePath));
if (expectedPath.equals(certBasePath)) {
System.out.println("✅ 路径拼接规则正确");
} else {
System.out.println("❌ 路径拼接规则需要调整");
System.out.println(" 请检查配置文件中的upload-path设置");
}
}
private void testProdEnvironmentPathConcatenation() {
System.out.println("--- 生产环境路径配置测试 ---");
System.out.println("生产环境使用数据库配置的证书路径");
System.out.println("路径格式: {uploadPath}/file/{relativePath}");
String uploadPath = configProperties.getUploadPath();
System.out.println("配置的upload-path: " + uploadPath);
// 模拟生产环境路径拼接
String relativePath = "wechat/10550/apiclient_key.pem";
String prodPath = uploadPath + "file/" + relativePath;
System.out.println("生产环境示例路径: " + prodPath);
System.out.println("✅ 生产环境路径配置逻辑正确");
}
@Test
public void testMultipleTenantPaths() {
System.out.println("=== 多租户路径拼接测试 ===");
if (!"dev".equals(activeProfile)) {
System.out.println("跳过:仅在开发环境测试多租户路径");
return;
}
String uploadPath = configProperties.getUploadPath();
String[] tenantIds = {"10324", "10398", "10547", "10549", "10550"};
System.out.println("配置文件upload-path: " + uploadPath);
System.out.println("测试多个租户的证书路径拼接:");
for (String tenantId : tenantIds) {
String certBasePath = uploadPath + "dev/wechat/" + tenantId + "/";
String privateKeyPath = certBasePath + "apiclient_key.pem";
File privateKeyFile = new File(privateKeyPath);
System.out.println("租户 " + tenantId + ": " + (privateKeyFile.exists() ? "" : "") + " " + privateKeyPath);
}
System.out.println("=== 多租户路径拼接测试完成 ===");
}
@Test
public void testConfigurationProperties() {
System.out.println("=== 配置属性测试 ===");
System.out.println("当前环境: " + activeProfile);
System.out.println("ConfigProperties注入: " + (configProperties != null ? "" : ""));
if (configProperties != null) {
System.out.println("upload-path: " + configProperties.getUploadPath());
System.out.println("upload-location: " + configProperties.getUploadLocation());
System.out.println("server-url: " + configProperties.getServerUrl());
}
System.out.println("=== 配置属性测试完成 ===");
}
}

View File

@@ -0,0 +1,99 @@
package com.gxwebsoft.test;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
/**
* 证书路径修复验证测试
*
* @author 科技小王子
* @since 2025-08-09
*/
@SpringBootTest
@ActiveProfiles("dev")
public class CertificatePathFixTest {
private static final String CERT_BASE_PATH = "/Users/gxwebsoft/JAVA/cms-java-code/src/main/resources/dev/wechat/10550";
@Test
public void testCertificatePathFix() {
System.out.println("=== 证书路径修复验证测试 ===");
// 验证证书目录存在
File certDir = new File(CERT_BASE_PATH);
assert certDir.exists() && certDir.isDirectory() : "证书目录不存在: " + CERT_BASE_PATH;
System.out.println("✅ 证书目录存在: " + CERT_BASE_PATH);
// 验证私钥文件
String privateKeyPath = CERT_BASE_PATH + "/apiclient_key.pem";
File privateKeyFile = new File(privateKeyPath);
assert privateKeyFile.exists() && privateKeyFile.isFile() : "私钥文件不存在: " + privateKeyPath;
System.out.println("✅ 私钥文件存在: " + privateKeyPath);
System.out.println(" 文件大小: " + privateKeyFile.length() + " bytes");
// 验证证书文件
String certPath = CERT_BASE_PATH + "/apiclient_cert.pem";
File certFile = new File(certPath);
assert certFile.exists() && certFile.isFile() : "证书文件不存在: " + certPath;
System.out.println("✅ 证书文件存在: " + certPath);
System.out.println(" 文件大小: " + certFile.length() + " bytes");
// 验证文件内容格式
try {
String privateKeyContent = Files.readString(Paths.get(privateKeyPath));
assert privateKeyContent.contains("-----BEGIN PRIVATE KEY-----") : "私钥文件格式错误";
System.out.println("✅ 私钥文件格式正确");
String certContent = Files.readString(Paths.get(certPath));
assert certContent.contains("-----BEGIN CERTIFICATE-----") : "证书文件格式错误";
System.out.println("✅ 证书文件格式正确");
} catch (Exception e) {
throw new RuntimeException("读取证书文件失败: " + e.getMessage(), e);
}
System.out.println("=== 证书路径修复验证完成 ===");
System.out.println("🎉 所有证书文件验证通过!");
System.out.println();
System.out.println("📋 修复内容总结:");
System.out.println("1. 修复了 SettingServiceImpl 中的硬编码证书路径");
System.out.println("2. 更新了 WechatCertAutoConfig 中的默认开发环境配置");
System.out.println("3. 证书路径已指向正确位置: " + CERT_BASE_PATH);
System.out.println();
System.out.println("🔧 下一步建议:");
System.out.println("1. 重启应用程序以使配置生效");
System.out.println("2. 测试微信支付功能是否正常工作");
System.out.println("3. 检查应用日志确认证书加载成功");
}
@Test
public void testCertificatePathStructure() {
System.out.println("=== 证书目录结构验证 ===");
File baseDir = new File("/Users/gxwebsoft/JAVA/cms-java-code/src/main/resources/dev/wechat");
if (baseDir.exists()) {
File[] tenantDirs = baseDir.listFiles(File::isDirectory);
if (tenantDirs != null) {
System.out.println("发现租户证书目录:");
for (File tenantDir : tenantDirs) {
System.out.println(" - 租户ID: " + tenantDir.getName());
File privateKey = new File(tenantDir, "apiclient_key.pem");
File cert = new File(tenantDir, "apiclient_cert.pem");
File p12 = new File(tenantDir, "apiclient_cert.p12");
System.out.println(" 私钥文件: " + (privateKey.exists() ? "" : ""));
System.out.println(" 证书文件: " + (cert.exists() ? "" : ""));
System.out.println(" P12文件: " + (p12.exists() ? "" : ""));
}
}
}
System.out.println("=== 目录结构验证完成 ===");
}
}

View File

@@ -0,0 +1,119 @@
package com.gxwebsoft.test;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.beans.factory.annotation.Value;
import javax.annotation.Resource;
import java.io.File;
/**
* 基于环境的证书路径配置测试
*
* @author 科技小王子
* @since 2025-08-09
*/
@SpringBootTest
@ActiveProfiles("dev")
public class EnvironmentBasedCertificateTest {
@Value("${spring.profiles.active:prod}")
private String activeProfile;
@Test
public void testEnvironmentBasedCertificateConfig() {
System.out.println("=== 环境基础证书配置测试 ===");
System.out.println("当前激活的环境: " + activeProfile);
if ("dev".equals(activeProfile)) {
System.out.println("✅ 检测到开发环境");
testDevEnvironmentCertificates();
} else {
System.out.println("✅ 检测到生产环境");
testProdEnvironmentCertificates();
}
System.out.println("=== 环境基础证书配置测试完成 ===");
}
private void testDevEnvironmentCertificates() {
System.out.println("--- 开发环境证书路径测试 ---");
String devCertPath = "/Users/gxwebsoft/JAVA/cms-java-code/src/main/resources/dev/wechat/10550";
String privateKeyPath = devCertPath + "/apiclient_key.pem";
String certPath = devCertPath + "/apiclient_cert.pem";
System.out.println("开发环境证书目录: " + devCertPath);
System.out.println("私钥文件路径: " + privateKeyPath);
System.out.println("证书文件路径: " + certPath);
// 验证文件存在
File privateKeyFile = new File(privateKeyPath);
File certFile = new File(certPath);
assert privateKeyFile.exists() : "开发环境私钥文件不存在: " + privateKeyPath;
assert certFile.exists() : "开发环境证书文件不存在: " + certPath;
System.out.println("✅ 开发环境证书文件验证通过");
System.out.println(" - 私钥文件大小: " + privateKeyFile.length() + " bytes");
System.out.println(" - 证书文件大小: " + certFile.length() + " bytes");
}
private void testProdEnvironmentCertificates() {
System.out.println("--- 生产环境证书路径测试 ---");
System.out.println("生产环境将使用数据库配置的证书路径");
System.out.println("证书路径格式: {uploadPath}/file/{relativePath}");
System.out.println("✅ 生产环境配置逻辑正确");
}
@Test
public void testCertificatePathLogic() {
System.out.println("=== 证书路径逻辑测试 ===");
// 模拟不同环境的路径构建逻辑
String uploadPath = "/www/wwwroot/file.ws/";
String relativePath = "wechat/10550/apiclient_key.pem";
if ("dev".equals(activeProfile)) {
// 开发环境:使用固定的本地路径
String devPath = "/Users/gxwebsoft/JAVA/cms-java-code/src/main/resources/dev/wechat/10550/apiclient_key.pem";
System.out.println("开发环境路径: " + devPath);
File devFile = new File(devPath);
System.out.println("开发环境文件存在: " + devFile.exists());
} else {
// 生产环境:使用数据库配置的路径
String prodPath = uploadPath + "file/" + relativePath;
System.out.println("生产环境路径: " + prodPath);
System.out.println("生产环境路径构建逻辑正确");
}
System.out.println("=== 证书路径逻辑测试完成 ===");
}
@Test
public void testEnvironmentSwitching() {
System.out.println("=== 环境切换测试 ===");
// 测试环境判断逻辑
System.out.println("当前环境: " + activeProfile);
if ("dev".equals(activeProfile)) {
System.out.println("✅ 开发环境配置:");
System.out.println(" - 使用本地固定证书路径");
System.out.println(" - 路径: /Users/gxwebsoft/JAVA/cms-java-code/src/main/resources/dev/wechat/10550/");
System.out.println(" - 优点: 开发便利,无需配置数据库路径");
} else if ("prod".equals(activeProfile)) {
System.out.println("✅ 生产环境配置:");
System.out.println(" - 使用数据库存储的证书路径");
System.out.println(" - 路径格式: {uploadPath}/file/{relativePath}");
System.out.println(" - 优点: 灵活配置,支持多租户");
} else {
System.out.println("⚠️ 未知环境: " + activeProfile);
System.out.println(" - 默认使用生产环境配置");
}
System.out.println("=== 环境切换测试完成 ===");
}
}