diff --git a/sf-common/src/main/java/com/sf/common/utils/JSONUtils.java b/sf-common/src/main/java/com/sf/common/utils/JSONUtils.java new file mode 100644 index 0000000..9c6a7c6 --- /dev/null +++ b/sf-common/src/main/java/com/sf/common/utils/JSONUtils.java @@ -0,0 +1,61 @@ + +package com.sf.common.utils; + + +import com.alibaba.fastjson2.JSON; +import com.alibaba.fastjson2.JSONArray; +import com.alibaba.fastjson2.JSONObject; +import lombok.experimental.UtilityClass; +import lombok.extern.slf4j.Slf4j; + + +/** + * + * @author zoukun + */ +@Slf4j +@UtilityClass +public class JSONUtils { + + /** + * 判断是否为JSONObject + * @param obj instanceof String ? parseObject + * @return + */ + public static boolean isJSONObject(Object obj) { + if (obj instanceof JSONObject) { + return true; + } + if (obj instanceof String) { + try { + JSONObject json = JSON.parseObject((String) obj); + return json != null; + } catch (Exception e) { + log.error( "JSONUtils.isJSONObject catch \n" + e.getMessage()); + } + } + + return false; + } + /** + * 判断是否为JSONArray + * @param obj instanceof String ? parseArray + * @return + */ + public static boolean isJSONArray(Object obj) { + if (obj instanceof JSONArray) { + return true; + } + if (obj instanceof String) { + try { + JSONArray json = JSON.parseArray((String) obj); + return json != null; + } catch (Exception e) { + log.error("JSONUtils.isJSONArray catch \n" + e.getMessage()); + } + } + + return false; + } + +} diff --git a/sf-service/src/main/java/com/sf/service/gateway/controller/GatewayInterfaceInfoController.java b/sf-service/src/main/java/com/sf/service/gateway/controller/GatewayInterfaceInfoController.java index 8f0bffb..605f175 100644 --- a/sf-service/src/main/java/com/sf/service/gateway/controller/GatewayInterfaceInfoController.java +++ b/sf-service/src/main/java/com/sf/service/gateway/controller/GatewayInterfaceInfoController.java @@ -5,16 +5,13 @@ import java.util.function.Function; import java.util.stream.Collectors; import javax.servlet.http.HttpServletResponse; +import cn.hutool.core.lang.Assert; +import com.sf.service.gateway.domain.*; import com.sf.service.index.utils.AppUtils; import com.sf.file.domain.SysOss; import com.sf.file.service.ISysOssService; -import com.sf.service.gateway.domain.GatewayInterfaceInfo; -import com.sf.service.gateway.domain.GatewayInterfaceLinkStrategy; -import com.sf.service.gateway.domain.GatewayServer; -import com.sf.service.gateway.domain.GatewayStrategy; -import com.sf.service.gateway.domain.dto.InsertGatewayInterfaceInfoDTO; import com.sf.service.gateway.domain.dto.UpdateDataStatusDTO; -import com.sf.service.gateway.domain.dto.UpdateGatewayInterfaceInfoDTO; +import com.sf.service.gateway.domain.dto.InsertOrUpdateGatewayInterfaceInfoDTO; import com.sf.service.gateway.domain.vo.GatewayInterfaceInfoDetailedVO; import com.sf.service.gateway.domain.vo.GatewayInterfaceInfoListVO; import com.sf.service.gateway.enums.GatewayDataStatus; @@ -100,10 +97,11 @@ public class GatewayInterfaceInfoController extends BaseController { public AjaxResult getInfo(@PathVariable("id") Long id) { GatewayInterfaceInfo gatewayInterfaceInfo = gatewayInterfaceInfoService.selectGatewayInterfaceInfoById(id); List linkList = gatewayInterfaceInfoService.selectGatewayInterfaceLinkStrategyByInterfaceIds(Collections.singleton(id)); + List mockExpectations = gatewayInterfaceInfoService.selectGatewayInterfaceMockListByInterfaceIds(Collections.singleton(id)); GatewayServer gatewayServer = gatewayServerService.selectGatewayServerById(gatewayInterfaceInfo.getServerId()); List strategyList = gatewayStrategyService.selectGatewayStrategyByIds(linkList.stream().map(GatewayInterfaceLinkStrategy::getStrategyId).collect(Collectors.toSet())); SysOss document = sysOssService.selectSysOssById(gatewayInterfaceInfo.getDocument()); - GatewayInterfaceInfoDetailedVO vo = GatewayInterfaceInfoDetailedVO.convert(gatewayInterfaceInfo,gatewayServer,strategyList,document); + GatewayInterfaceInfoDetailedVO vo = GatewayInterfaceInfoDetailedVO.convert(gatewayInterfaceInfo,gatewayServer,strategyList,document,mockExpectations); // TODO 调用次数和平均时间暂时没有来源 vo.setNumberOfCalls(0L); vo.setAverageResponseTime(0L); @@ -114,7 +112,7 @@ public class GatewayInterfaceInfoController extends BaseController { @PreAuthorize("@ss.hasPermi('gateway:interface:add')") @Log(title = "接口管理", businessType = BusinessType.INSERT) @PostMapping - public AjaxResult add(@Validated @RequestBody InsertGatewayInterfaceInfoDTO dto) { + public AjaxResult add(@Validated @RequestBody InsertOrUpdateGatewayInterfaceInfoDTO dto) { return success(gatewayInterfaceInfoService.insertGatewayInterfaceInfoAndBindStrategy(dto)); } @@ -122,7 +120,8 @@ public class GatewayInterfaceInfoController extends BaseController { @PreAuthorize("@ss.hasPermi('gateway:interface:edit')") @Log(title = "接口管理", businessType = BusinessType.UPDATE) @PutMapping - public AjaxResult edit(@Validated @RequestBody UpdateGatewayInterfaceInfoDTO dto) { + public AjaxResult edit(@Validated @RequestBody InsertOrUpdateGatewayInterfaceInfoDTO dto) { + Assert.notNull(dto.getId(),"接口ID不能为空"); return success(gatewayInterfaceInfoService.updateGatewayInterfaceInfoAndBindStrategy(dto)); } diff --git a/sf-service/src/main/java/com/sf/service/gateway/domain/GatewayInterfaceInfo.java b/sf-service/src/main/java/com/sf/service/gateway/domain/GatewayInterfaceInfo.java index 7875d7b..5bfc28a 100644 --- a/sf-service/src/main/java/com/sf/service/gateway/domain/GatewayInterfaceInfo.java +++ b/sf-service/src/main/java/com/sf/service/gateway/domain/GatewayInterfaceInfo.java @@ -85,8 +85,11 @@ public class GatewayInterfaceInfo extends BaseEntity @ApiModelProperty("Mock状态(0=停用,1=启用)") private String mockStatus; - @ApiModelProperty("Mock响应,json格式") - private String mockResponse; + @ApiModelProperty("Mock默认响应HTTP状态码") + private Integer mockDefaultHttpStatus; + + @ApiModelProperty("Mock默认响应,json格式") + private String mockDefaultResponse; diff --git a/sf-service/src/main/java/com/sf/service/gateway/domain/GatewayInterfaceMock.java b/sf-service/src/main/java/com/sf/service/gateway/domain/GatewayInterfaceMock.java new file mode 100644 index 0000000..37df6df --- /dev/null +++ b/sf-service/src/main/java/com/sf/service/gateway/domain/GatewayInterfaceMock.java @@ -0,0 +1,51 @@ +package com.sf.service.gateway.domain; + +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; + +/** +* 网关服务-接口Mock配置表 +* @TableName Gateway_interface_mock +*/ +@Data +public class GatewayInterfaceMock { + + /** + * ID + */ + @ApiModelProperty("ID") + private Long id; + /** + * 接口id + */ + @ApiModelProperty("接口id") + private Long interfaceId; + /** + * http状态码 + */ + @ApiModelProperty("http状态码") + private Integer httpStatus; + /** + * 匹配条件 + */ + @ApiModelProperty("匹配条件") + private String matchConditions; + /** + * Mock响应,JSON字符串 + */ + @ApiModelProperty("Mock响应,JSON字符串") + private String mockResponse; + /** + * 状态(0停用 1启用) + */ + @ApiModelProperty("状态(0停用 1启用)") + private String status; + /** + * 排序 + */ + @ApiModelProperty("排序") + private Integer orderNum; + + + +} diff --git a/sf-service/src/main/java/com/sf/service/gateway/domain/dto/InsertGatewayInterfaceInfoDTO.java b/sf-service/src/main/java/com/sf/service/gateway/domain/dto/InsertGatewayInterfaceInfoDTO.java deleted file mode 100644 index c751dc3..0000000 --- a/sf-service/src/main/java/com/sf/service/gateway/domain/dto/InsertGatewayInterfaceInfoDTO.java +++ /dev/null @@ -1,76 +0,0 @@ -package com.sf.service.gateway.domain.dto; - -import com.sf.common.annotation.Excel; -import com.sf.common.core.domain.BaseEntity; -import com.sf.file.domain.SysOss; -import io.swagger.annotations.ApiModel; -import io.swagger.annotations.ApiModelProperty; -import lombok.Data; -import lombok.EqualsAndHashCode; -import org.hibernate.validator.constraints.Length; - -import javax.validation.constraints.*; -import java.util.List; - - -/** - * 添加接口信息对象 Gateway_interface_info - * - * @author zoukun - * @date 2024-04-22 - */ -@Data -@EqualsAndHashCode(callSuper = false) -@ApiModel(value = "InsertGatewayInterfaceInfoDTO", description = "添加接口信息对象") -public class InsertGatewayInterfaceInfoDTO { - private static final long serialVersionUID = 1L; - - @Length(max = 50,message = "接口名称不能超过50个字符") - @NotBlank(message = "接口名称不能为空") - @ApiModelProperty("接口名称") - private String interfaceName; - - /** api编码 */ - @Excel(name = "接口编码") - @NotBlank(message = "接口编码不能为空") - @Pattern(regexp = "^[a-zA-Z][a-zA-Z0-9.-]{0,63}$",message = "接口编码只能以字母开头,只能包含字母、数字和.-,不超过64个字符") - private String apiCode; - - @Length(max = 200,message = "接口名称不能超过200个字符") - @NotBlank(message = "接口路径不能为空") - @ApiModelProperty("接口路径") - private String interfacePath; - - @NotBlank(message = "请求方式不能为空") - @ApiModelProperty("请求方式") - private String requestMethod; - - @Length(max = 200,message = "接口描述不能超过200个字符") - @ApiModelProperty("接口描述") - private String description; - - @Length(max = 20,message = "接口版本不能超过20个字符") - @ApiModelProperty("接口版本") - private String version; - - @ApiModelProperty("接口文档(文档地址)") - private SysOss document; - - @NotNull(message = "网关服务不能为空") - @ApiModelProperty("网关服务id") - private Long serverId; - - @DecimalMin(value = "1",message = "超时时间为大于等于1的正整数") - @DecimalMax(value = "150000",message = "超时时间不能大于150000") - @ApiModelProperty("超时时间,单位毫秒,1-150000之间") - private Integer timeout; - - @ApiModelProperty("策略ID集合") - private List strategyIds; - - @ApiModelProperty("Mock状态(0=停用,1=启用)") - private String mockStatus; - - @ApiModelProperty("Mock响应,json格式") - private String mockResponse; -} diff --git a/sf-service/src/main/java/com/sf/service/gateway/domain/dto/InsertGatewayInterfaceMockDTO.java b/sf-service/src/main/java/com/sf/service/gateway/domain/dto/InsertGatewayInterfaceMockDTO.java new file mode 100644 index 0000000..98b1e0e --- /dev/null +++ b/sf-service/src/main/java/com/sf/service/gateway/domain/dto/InsertGatewayInterfaceMockDTO.java @@ -0,0 +1,57 @@ +package com.sf.service.gateway.domain.dto; + +import com.sf.service.gateway.domain.GatewayInterfaceInfo; +import com.sf.service.gateway.domain.GatewayInterfaceMock; +import com.sf.vertx.api.pojo.MockMatchCondition; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; + +import java.util.List; + +import static org.apache.commons.lang3.StringUtils.EMPTY; + +/** +* 网关服务-接口Mock配置表 +* @TableName Gateway_interface_mock +*/ +@Data +public class InsertGatewayInterfaceMockDTO { + + /** + * ID + */ + @ApiModelProperty("ID") + private Long id; + /** + * 接口id + */ + @ApiModelProperty("接口id") + private Long interfaceId; + /** + * http状态码 + */ + @ApiModelProperty("http状态码") + private Integer httpStatus; + /** + * 匹配条件 + */ + @ApiModelProperty("匹配条件") + private List matchConditions; + /** + * Mock响应,JSON字符串 + */ + @ApiModelProperty("Mock响应,JSON字符串") + private String mockResponse; + /** + * 状态(0停用 1启用) + */ + @ApiModelProperty("状态(0停用 1启用)") + private String status; + /** + * 排序 + */ + @ApiModelProperty("排序") + private Integer orderNum; + + +} diff --git a/sf-service/src/main/java/com/sf/service/gateway/domain/dto/UpdateGatewayInterfaceInfoDTO.java b/sf-service/src/main/java/com/sf/service/gateway/domain/dto/InsertOrUpdateGatewayInterfaceInfoDTO.java similarity index 54% rename from sf-service/src/main/java/com/sf/service/gateway/domain/dto/UpdateGatewayInterfaceInfoDTO.java rename to sf-service/src/main/java/com/sf/service/gateway/domain/dto/InsertOrUpdateGatewayInterfaceInfoDTO.java index b943ccb..883a004 100644 --- a/sf-service/src/main/java/com/sf/service/gateway/domain/dto/UpdateGatewayInterfaceInfoDTO.java +++ b/sf-service/src/main/java/com/sf/service/gateway/domain/dto/InsertOrUpdateGatewayInterfaceInfoDTO.java @@ -2,6 +2,10 @@ package com.sf.service.gateway.domain.dto; import com.sf.common.annotation.Excel; import com.sf.file.domain.SysOss; +import com.sf.service.gateway.domain.GatewayInterfaceInfo; +import com.sf.service.gateway.domain.GatewayInterfaceMock; +import com.sf.service.gateway.domain.GatewayServer; +import com.sf.service.gateway.domain.vo.GatewayInterfaceInfoListVO; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.Data; @@ -11,20 +15,21 @@ import org.hibernate.validator.constraints.Length; import javax.validation.constraints.*; import java.util.List; +import static org.apache.commons.lang3.StringUtils.EMPTY; + /** - * 修改接口信息对象 Gateway_interface_info + * 新增或修改接口信息对象 Gateway_interface_info * * @author zoukun * @date 2024-04-22 */ @Data @EqualsAndHashCode(callSuper = false) -@ApiModel(value = "UpdateGatewayInterfaceInfoDTO", description = "修改接口信息对象") -public class UpdateGatewayInterfaceInfoDTO { +@ApiModel(value = "InsertOrUpdateGatewayInterfaceInfoDTO", description = "新增或修改接口信息对象") +public class InsertOrUpdateGatewayInterfaceInfoDTO { private static final long serialVersionUID = 1L; - @NotNull(message = "ID不能为空") @ApiModelProperty("ID") private Long id; @@ -74,6 +79,32 @@ public class UpdateGatewayInterfaceInfoDTO { @ApiModelProperty("Mock状态(0=停用,1=启用)") private String mockStatus; - @ApiModelProperty("Mock响应,json格式") - private String mockResponse; + @ApiModelProperty("Mock默认响应HTTP状态码") + private Integer mockDefaultHttpStatus; + + @ApiModelProperty("Mock默认响应,json格式") + private String mockDefaultResponse; + + @ApiModelProperty("Mock默认响应,json格式") + private List mockExpectations; + + public GatewayInterfaceInfo convertToGatewayInterfaceInfo() { + GatewayInterfaceInfo gatewayInterfaceInfo = new GatewayInterfaceInfo(); + gatewayInterfaceInfo.setId(this.id); + gatewayInterfaceInfo.setInterfaceName(this.interfaceName); + gatewayInterfaceInfo.setApiCode(this.apiCode); + gatewayInterfaceInfo.setInterfacePath(this.interfacePath); + gatewayInterfaceInfo.setRequestMethod(this.requestMethod); + gatewayInterfaceInfo.setDescription(this.description); + gatewayInterfaceInfo.setVersion(this.version); + String document = this.document == null ? EMPTY : this.document.getId(); + gatewayInterfaceInfo.setDocument(document); + gatewayInterfaceInfo.setServerId(this.serverId); + gatewayInterfaceInfo.setTimeout(this.timeout); + gatewayInterfaceInfo.setMockStatus(this.mockStatus); + gatewayInterfaceInfo.setMockDefaultHttpStatus(this.mockDefaultHttpStatus); + gatewayInterfaceInfo.setMockDefaultResponse(this.mockDefaultResponse); + return gatewayInterfaceInfo; + } + } diff --git a/sf-service/src/main/java/com/sf/service/gateway/domain/vo/GatewayInterfaceInfoDetailedVO.java b/sf-service/src/main/java/com/sf/service/gateway/domain/vo/GatewayInterfaceInfoDetailedVO.java index d3e5d3d..560959f 100644 --- a/sf-service/src/main/java/com/sf/service/gateway/domain/vo/GatewayInterfaceInfoDetailedVO.java +++ b/sf-service/src/main/java/com/sf/service/gateway/domain/vo/GatewayInterfaceInfoDetailedVO.java @@ -4,6 +4,7 @@ import com.fasterxml.jackson.annotation.JsonFormat; import com.sf.common.annotation.Excel; import com.sf.file.domain.SysOss; import com.sf.service.gateway.domain.GatewayInterfaceInfo; +import com.sf.service.gateway.domain.GatewayInterfaceMock; import com.sf.service.gateway.domain.GatewayServer; import com.sf.service.gateway.domain.GatewayStrategy; import io.swagger.annotations.ApiModel; @@ -118,13 +119,22 @@ public class GatewayInterfaceInfoDetailedVO { @ApiModelProperty("Mock状态(0=停用,1=启用)") private String mockStatus; - @ApiModelProperty("Mock响应,json格式") - private String mockResponse; + @ApiModelProperty("Mock默认响应HTTP状态码") + private Integer mockDefaultHttpStatus; + + @ApiModelProperty("Mock默认响应,json格式") + private String mockDefaultResponse; + + @ApiModelProperty("Mock默认响应,json格式") + private List mockExpectations; + public static GatewayInterfaceInfoDetailedVO convert(GatewayInterfaceInfo gatewayInterfaceInfo , GatewayServer gatewayServer , List strategyList , SysOss document + , List mockExpectations + ) { GatewayInterfaceInfoDetailedVO vo = new GatewayInterfaceInfoDetailedVO(); vo.setId(gatewayInterfaceInfo.getId()); @@ -142,7 +152,10 @@ public class GatewayInterfaceInfoDetailedVO { vo.setServerName(gatewayServer.getServerName()); vo.setTimeout(gatewayInterfaceInfo.getTimeout()); vo.setMockStatus(gatewayInterfaceInfo.getMockStatus()); - vo.setMockResponse(gatewayInterfaceInfo.getMockResponse()); + vo.setMockDefaultHttpStatus(gatewayInterfaceInfo.getMockDefaultHttpStatus()); + vo.setMockDefaultResponse(gatewayInterfaceInfo.getMockDefaultResponse()); + // mock配置 + vo.setMockExpectations(mockExpectations); // 策略信息 List strategyListVOList = strategyList.stream() .map(GatewayStrategyListVO::convert) diff --git a/sf-service/src/main/java/com/sf/service/gateway/domain/vo/GatewayInterfaceInfoListVO.java b/sf-service/src/main/java/com/sf/service/gateway/domain/vo/GatewayInterfaceInfoListVO.java index 71d86b9..551db60 100644 --- a/sf-service/src/main/java/com/sf/service/gateway/domain/vo/GatewayInterfaceInfoListVO.java +++ b/sf-service/src/main/java/com/sf/service/gateway/domain/vo/GatewayInterfaceInfoListVO.java @@ -110,12 +110,6 @@ public class GatewayInterfaceInfoListVO { @ApiModelProperty private List strategyList; - @ApiModelProperty("Mock状态(0=停用,1=启用)") - private String mockStatus; - - @ApiModelProperty("Mock响应,json格式") - private String mockResponse; - public static GatewayInterfaceInfoListVO convert(GatewayInterfaceInfo info, GatewayServer gatewayServer) { GatewayInterfaceInfoListVO vo = new GatewayInterfaceInfoListVO(); vo.setId(info.getId()); @@ -131,8 +125,6 @@ public class GatewayInterfaceInfoListVO { vo.setServerId(info.getServerId()); vo.setServerName(gatewayServer != null ? gatewayServer.getServerName() : EMPTY); vo.setTimeout(info.getTimeout()); - vo.setMockStatus(info.getMockStatus()); - vo.setMockResponse(info.getMockResponse()); return vo; } } diff --git a/sf-service/src/main/java/com/sf/service/gateway/mapper/GatewayInterfaceMockMapper.java b/sf-service/src/main/java/com/sf/service/gateway/mapper/GatewayInterfaceMockMapper.java new file mode 100644 index 0000000..a1672cb --- /dev/null +++ b/sf-service/src/main/java/com/sf/service/gateway/mapper/GatewayInterfaceMockMapper.java @@ -0,0 +1,36 @@ +package com.sf.service.gateway.mapper; + +import com.sf.service.gateway.domain.GatewayInterfaceMock; +import org.apache.ibatis.annotations.Param; + +import java.util.Collection; +import java.util.List; +import java.util.Set; + +/** + * @Entity com.sf.service.gateway.domain.GatewayInterfaceMock + */ +public interface GatewayInterfaceMockMapper { + + int deleteByPrimaryKey(Long id); + + int insert(GatewayInterfaceMock record); + + int insertSelective(GatewayInterfaceMock record); + + GatewayInterfaceMock selectByPrimaryKey(Long id); + + int updateByPrimaryKeySelective(GatewayInterfaceMock record); + + int updateByPrimaryKey(GatewayInterfaceMock record); + + List selectGatewayInterfaceMockByInterfaceIds(@Param("interfaceIds") Collection interfaceIds); + + void batchInsertGatewayInterfaceMock(@Param("mockList") List mockList); + + void deleteByInterfaceIds(@Param("interfaceIds") Set interfaceIds); +} + + + + diff --git a/sf-service/src/main/java/com/sf/service/gateway/service/IGatewayInterfaceInfoService.java b/sf-service/src/main/java/com/sf/service/gateway/service/IGatewayInterfaceInfoService.java index 4560e32..62bcba3 100644 --- a/sf-service/src/main/java/com/sf/service/gateway/service/IGatewayInterfaceInfoService.java +++ b/sf-service/src/main/java/com/sf/service/gateway/service/IGatewayInterfaceInfoService.java @@ -2,12 +2,13 @@ package com.sf.service.gateway.service; import com.sf.service.gateway.domain.GatewayInterfaceInfo; import com.sf.service.gateway.domain.GatewayInterfaceLinkStrategy; -import com.sf.service.gateway.domain.dto.InsertGatewayInterfaceInfoDTO; -import com.sf.service.gateway.domain.dto.UpdateGatewayInterfaceInfoDTO; +import com.sf.service.gateway.domain.GatewayInterfaceMock; +import com.sf.service.gateway.domain.dto.InsertOrUpdateGatewayInterfaceInfoDTO; import com.sf.service.gateway.enums.GatewayDataStatus; import java.util.Collection; import java.util.List; +import java.util.Set; /** * 接口管理Service接口 @@ -77,14 +78,16 @@ public interface IGatewayInterfaceInfoService * @param dto * @return */ - Long insertGatewayInterfaceInfoAndBindStrategy(InsertGatewayInterfaceInfoDTO dto); + Long insertGatewayInterfaceInfoAndBindStrategy(InsertOrUpdateGatewayInterfaceInfoDTO dto); /** * 修改接口信息及绑定的策略信息 * @param dto * @return */ - Long updateGatewayInterfaceInfoAndBindStrategy(UpdateGatewayInterfaceInfoDTO dto); + Long updateGatewayInterfaceInfoAndBindStrategy(InsertOrUpdateGatewayInterfaceInfoDTO dto); List selectGatewayInterfaceLinkStrategyByInterfaceIds(Collection interfaceIds); + + List selectGatewayInterfaceMockListByInterfaceIds(Collection interfaceIds); } diff --git a/sf-service/src/main/java/com/sf/service/gateway/service/impl/GatewayConfigServiceImpl.java b/sf-service/src/main/java/com/sf/service/gateway/service/impl/GatewayConfigServiceImpl.java index 9bf9084..6aafb7c 100644 --- a/sf-service/src/main/java/com/sf/service/gateway/service/impl/GatewayConfigServiceImpl.java +++ b/sf-service/src/main/java/com/sf/service/gateway/service/impl/GatewayConfigServiceImpl.java @@ -6,6 +6,7 @@ import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSONObject; import com.sf.common.constant.HttpStatus; import com.sf.common.exception.ServiceException; +import com.sf.common.utils.JSONUtils; import com.sf.service.index.utils.AppUtils; import com.sf.common.utils.DateUtils; import com.sf.common.utils.SecurityUtils; @@ -145,7 +146,8 @@ public class GatewayConfigServiceImpl implements IGatewayConfigService { return service; } Map> groupByServerInterfaceMap = interfaceInfoList.stream().collect(Collectors.groupingBy(GatewayInterfaceInfo::getServerId)); - List linkList = gatewayInterfaceInfoService.selectGatewayInterfaceLinkStrategyByInterfaceIds(interfaceInfoList.stream().map(GatewayInterfaceInfo::getId).collect(Collectors.toSet())); + Set interfaceIds = interfaceInfoList.stream().map(GatewayInterfaceInfo::getId).collect(Collectors.toSet()); + List linkList = gatewayInterfaceInfoService.selectGatewayInterfaceLinkStrategyByInterfaceIds(interfaceIds); Map> groupByInterfaceLinkMap = linkList.stream().collect(Collectors.groupingBy(GatewayInterfaceLinkStrategy::getInterfaceId)); // 如果是路由模式的服务,查询对应的路由服务信息 Collection routeIds = serverList.stream().filter(item -> GatewayServiceModel.ROUTE.getCode().equals(item.getServiceModel())) @@ -158,6 +160,11 @@ public class GatewayConfigServiceImpl implements IGatewayConfigService { // 启用的 strategyList = strategyList.stream().filter(strategy -> GatewayDataStatus.ENABLE.getCode().equals(strategy.getStatus())).collect(Collectors.toList()); Map strategyMap = strategyList.stream().collect(Collectors.toMap(GatewayStrategy::getId, Function.identity())); + // mock + List mockList = gatewayInterfaceInfoService.selectGatewayInterfaceMockListByInterfaceIds(interfaceIds); + Map> groupByInterfaceMockMap = mockList.stream() + .filter(mock -> GatewayDataStatus.ENABLE.getCode().equals(mock.getStatus())) + .collect(Collectors.groupingBy(GatewayInterfaceMock::getInterfaceId)); return serverList.stream() .filter(server -> CollUtil.isNotEmpty(groupByServerInterfaceMap.getOrDefault(server.getId(), Collections.emptyList()))) .map(server -> { @@ -179,8 +186,22 @@ public class GatewayConfigServiceImpl implements IGatewayConfigService { apiConfig.setMethod(api.getRequestMethod()); apiConfig.setTimeout(api.getTimeout() != null ? api.getTimeout().longValue() : serviceTimeout); apiConfig.setStrategy(currentApiStrategyList); + // Mock if (GatewayDataStatus.ENABLE.getCode().equals(api.getMockStatus())) { - apiConfig.setMockResponse(api.getMockResponse()); + apiConfig.setMockDefaultHttpStatus(api.getMockDefaultHttpStatus()); + apiConfig.setMockDefaultResponse(api.getMockDefaultResponse()); + List currentMocks = groupByInterfaceMockMap.getOrDefault(api.getId(), Collections.emptyList()); + List mockExpectations = currentMocks.stream() + .sorted(Comparator.comparingInt(GatewayInterfaceMock::getOrderNum)) + .map(mock ->{ + MockExpectation mockExpectation = new MockExpectation(); + mockExpectation.setHttpStatus(mock.getHttpStatus()); + mockExpectation.setMockResponse(mock.getMockResponse()); + mockExpectation.setMatchConditions(JSON.parseArray(mock.getMatchConditions(),MockMatchCondition.class)); + return mockExpectation; + }) + .collect(Collectors.toList()); + apiConfig.setMockExpectations(mockExpectations); } return apiConfig; }).collect(Collectors.toList()); @@ -307,7 +328,7 @@ public class GatewayConfigServiceImpl implements IGatewayConfigService { } else { Assert.isTrue(gatewayConfig.getApiCurrentLimitingTimeWindow() >= 0, "限流窗口值>=0"); } - Assert.isTrue(apijson.JSON.isJSONObject(gatewayConfig.getApiCurrentLimitingDefaultResponse()), "请输入正确的JSON格式限流响应信息"); + Assert.isTrue(JSONUtils.isJSONObject(gatewayConfig.getApiCurrentLimitingDefaultResponse()), "请输入正确的JSON格式限流响应信息"); } if (GatewayDataStatus.ENABLE.getCode().equals(gatewayConfig.getAppCurrentLimitingStatus())) { Assert.notNull(gatewayConfig.getAppCurrentLimitingThreshold(), "限流值不能为空"); @@ -318,7 +339,7 @@ public class GatewayConfigServiceImpl implements IGatewayConfigService { } else { Assert.isTrue(gatewayConfig.getAppCurrentLimitingTimeWindow() >= 0, "限流窗口值>=0"); } - Assert.isTrue(apijson.JSON.isJSONObject(gatewayConfig.getAppCurrentLimitingDefaultResponse()), "请输入正确的JSON格式限流响应信息"); + Assert.isTrue(JSONUtils.isJSONObject(gatewayConfig.getAppCurrentLimitingDefaultResponse()), "请输入正确的JSON格式限流响应信息"); } } } diff --git a/sf-service/src/main/java/com/sf/service/gateway/service/impl/GatewayInterfaceInfoServiceImpl.java b/sf-service/src/main/java/com/sf/service/gateway/service/impl/GatewayInterfaceInfoServiceImpl.java index 8bf78ae..f268a12 100644 --- a/sf-service/src/main/java/com/sf/service/gateway/service/impl/GatewayInterfaceInfoServiceImpl.java +++ b/sf-service/src/main/java/com/sf/service/gateway/service/impl/GatewayInterfaceInfoServiceImpl.java @@ -3,21 +3,25 @@ package com.sf.service.gateway.service.impl; import java.util.*; import java.util.stream.Collectors; -import apijson.JSON; import cn.hutool.core.collection.CollUtil; +import com.alibaba.fastjson2.JSON; import com.sf.common.utils.*; import com.sf.service.gateway.domain.GatewayInterfaceInfo; import com.sf.service.gateway.domain.GatewayInterfaceLinkStrategy; +import com.sf.service.gateway.domain.GatewayInterfaceMock; import com.sf.service.gateway.domain.GatewayStrategy; -import com.sf.service.gateway.domain.dto.InsertGatewayInterfaceInfoDTO; -import com.sf.service.gateway.domain.dto.UpdateGatewayInterfaceInfoDTO; +import com.sf.service.gateway.domain.dto.InsertGatewayInterfaceMockDTO; +import com.sf.vertx.api.pojo.MockMatchCondition; +import com.sf.service.gateway.domain.dto.InsertOrUpdateGatewayInterfaceInfoDTO; import com.sf.service.gateway.enums.GatewayDataStatus; import com.sf.service.gateway.enums.GatewayStrategyType; import com.sf.service.gateway.mapper.GatewayInterfaceInfoMapper; import com.sf.service.gateway.mapper.GatewayInterfaceLinkStrategyMapper; +import com.sf.service.gateway.mapper.GatewayInterfaceMockMapper; import com.sf.service.gateway.service.IGatewayInterfaceInfoService; import com.sf.service.gateway.service.IGatewayStrategyService; import com.sf.service.index.utils.AppUtils; +import com.sf.vertx.enums.MatchType; import lombok.extern.slf4j.Slf4j; import org.springframework.dao.DuplicateKeyException; import org.springframework.stereotype.Service; @@ -27,8 +31,6 @@ import org.springframework.util.CollectionUtils; import javax.annotation.Resource; -import static org.apache.commons.lang3.StringUtils.EMPTY; - /** * 接口管理Service业务层处理 * @@ -45,6 +47,9 @@ public class GatewayInterfaceInfoServiceImpl implements IGatewayInterfaceInfoSer @Resource private GatewayInterfaceLinkStrategyMapper gatewayInterfaceLinkStrategyMapper; + @Resource + private GatewayInterfaceMockMapper gatewayInterfaceMockMapper; + @Resource private IGatewayStrategyService gatewayStrategyService; @@ -98,7 +103,8 @@ public class GatewayInterfaceInfoServiceImpl implements IGatewayInterfaceInfoSer private void checkGatewayInterfaceInfoCanInsertOrUpdate(GatewayInterfaceInfo gatewayInterfaceInfo) { if (GatewayDataStatus.ENABLE.getCode().equals(gatewayInterfaceInfo.getMockStatus())) { - Assert.isTrue(JSON.isJSONObject(gatewayInterfaceInfo.getMockResponse()), "请输入正确格式的响应信息"); + Assert.notNull(gatewayInterfaceInfo.getMockDefaultHttpStatus(), "请输入默认响应HTTP状态码"); + Assert.isTrue(JSONUtils.isJSONObject(gatewayInterfaceInfo.getMockDefaultResponse()), "请输入正确格式的默认响应信息"); } } @@ -180,21 +186,9 @@ public class GatewayInterfaceInfoServiceImpl implements IGatewayInterfaceInfoSer @Transactional(rollbackFor = Exception.class) @Override - public Long insertGatewayInterfaceInfoAndBindStrategy(InsertGatewayInterfaceInfoDTO dto) { - GatewayInterfaceInfo gatewayInterfaceInfo = new GatewayInterfaceInfo(); - gatewayInterfaceInfo.setInterfaceName(dto.getInterfaceName()); - gatewayInterfaceInfo.setApiCode(dto.getApiCode()); - gatewayInterfaceInfo.setInterfacePath(dto.getInterfacePath()); - gatewayInterfaceInfo.setRequestMethod(dto.getRequestMethod()); - gatewayInterfaceInfo.setDescription(dto.getDescription()); - gatewayInterfaceInfo.setVersion(dto.getVersion()); + public Long insertGatewayInterfaceInfoAndBindStrategy(InsertOrUpdateGatewayInterfaceInfoDTO dto) { + GatewayInterfaceInfo gatewayInterfaceInfo = dto.convertToGatewayInterfaceInfo(); gatewayInterfaceInfo.setStatus(GatewayDataStatus.DISABLE.getCode()); - String document = dto.getDocument() == null ? EMPTY : dto.getDocument().getId(); - gatewayInterfaceInfo.setDocument(document); - gatewayInterfaceInfo.setServerId(dto.getServerId()); - gatewayInterfaceInfo.setTimeout(dto.getTimeout()); - gatewayInterfaceInfo.setMockStatus(dto.getMockStatus()); - gatewayInterfaceInfo.setMockResponse(dto.getMockResponse()); Long interfaceInfoId = insertGatewayInterfaceInfo(gatewayInterfaceInfo); if (CollUtil.isNotEmpty(dto.getStrategyIds())) { checkGatewayStrategyTypeCannotBeRepeated(dto.getStrategyIds()); @@ -203,26 +197,52 @@ public class GatewayInterfaceInfoServiceImpl implements IGatewayInterfaceInfoSer .collect(Collectors.toList()); gatewayInterfaceLinkStrategyMapper.batchInsertGatewayInterfaceLinkStrategy(linkStrategyList); } + if (CollUtil.isNotEmpty(dto.getMockExpectations())) { + checkInsertGatewayInterfaceMockList(dto.getMockExpectations()); + List mockList = dto.getMockExpectations().stream().map(mockDTO ->{ + GatewayInterfaceMock mock = new GatewayInterfaceMock(); + mock.setInterfaceId(interfaceInfoId); + mock.setHttpStatus(mockDTO.getHttpStatus()); + mock.setMatchConditions(JSON.toJSONString(mockDTO.getMatchConditions())); + mock.setMockResponse(mockDTO.getMockResponse()); + mock.setStatus(mockDTO.getStatus()); + mock.setOrderNum(mockDTO.getOrderNum()); + return mock; + }).collect(Collectors.toList()); + gatewayInterfaceMockMapper.batchInsertGatewayInterfaceMock(mockList); + } return interfaceInfoId; } + private void checkInsertGatewayInterfaceMockList(List gatewayInterfaceMockList) { + gatewayInterfaceMockList.forEach(mock ->{ + Assert.notNull(mock.getHttpStatus(),"HTTP状态码不能为空"); + Assert.isTrue(JSONUtils.isJSONObject(mock.getMockResponse()), "请输入正确格式的默认响应信息"); + try { + List conditionDTOList = mock.getMatchConditions(); + Assert.notEmpty(conditionDTOList,"至少存在一条匹配条件"); + conditionDTOList.forEach(condition -> { + MatchType matchType = MatchType.getByCode(condition.getMatchType()); + Assert.notNull(matchType,"不支持的匹配类型"); + switch (matchType){ + case IS_NULL: + case NOT_NULL: + break; + default: + Assert.notEmpty(condition.getParameterValue(),"请输入参数值"); + break; + } + }); + } catch (Exception e) { + log.error("JSONUtils.isJSONArray catch \n" + e.getMessage()); + } + }); + } + @Transactional(rollbackFor = Exception.class) @Override - public Long updateGatewayInterfaceInfoAndBindStrategy(UpdateGatewayInterfaceInfoDTO dto) { - GatewayInterfaceInfo gatewayInterfaceInfo = new GatewayInterfaceInfo(); - gatewayInterfaceInfo.setId(dto.getId()); - gatewayInterfaceInfo.setInterfaceName(dto.getInterfaceName()); - gatewayInterfaceInfo.setApiCode(dto.getApiCode()); - gatewayInterfaceInfo.setInterfacePath(dto.getInterfacePath()); - gatewayInterfaceInfo.setRequestMethod(dto.getRequestMethod()); - gatewayInterfaceInfo.setDescription(dto.getDescription()); - gatewayInterfaceInfo.setVersion(dto.getVersion()); - String document = dto.getDocument() == null ? EMPTY : dto.getDocument().getId(); - gatewayInterfaceInfo.setDocument(document); - gatewayInterfaceInfo.setServerId(dto.getServerId()); - gatewayInterfaceInfo.setTimeout(dto.getTimeout()); - gatewayInterfaceInfo.setMockStatus(dto.getMockStatus()); - gatewayInterfaceInfo.setMockResponse(dto.getMockResponse()); + public Long updateGatewayInterfaceInfoAndBindStrategy(InsertOrUpdateGatewayInterfaceInfoDTO dto) { + GatewayInterfaceInfo gatewayInterfaceInfo = dto.convertToGatewayInterfaceInfo(); updateGatewayInterfaceInfo(gatewayInterfaceInfo); // 先删除 gatewayInterfaceLinkStrategyMapper.deleteByInterfaceIds(Collections.singleton(dto.getId())); @@ -233,6 +253,21 @@ public class GatewayInterfaceInfoServiceImpl implements IGatewayInterfaceInfoSer .collect(Collectors.toList()); gatewayInterfaceLinkStrategyMapper.batchInsertGatewayInterfaceLinkStrategy(linkStrategyList); } + gatewayInterfaceMockMapper.deleteByInterfaceIds(Collections.singleton(dto.getId())); + if (CollUtil.isNotEmpty(dto.getMockExpectations())) { + checkInsertGatewayInterfaceMockList(dto.getMockExpectations()); + List mockList = dto.getMockExpectations().stream().map(mockDTO -> { + GatewayInterfaceMock mock = new GatewayInterfaceMock(); + mock.setInterfaceId(dto.getId()); + mock.setHttpStatus(mockDTO.getHttpStatus()); + mock.setMatchConditions(JSON.toJSONString(mockDTO.getMatchConditions())); + mock.setMockResponse(mockDTO.getMockResponse()); + mock.setStatus(mockDTO.getStatus()); + mock.setOrderNum(mockDTO.getOrderNum()); + return mock; + }).collect(Collectors.toList()); + gatewayInterfaceMockMapper.batchInsertGatewayInterfaceMock(mockList); + } return gatewayInterfaceInfo.getId(); } @@ -244,6 +279,15 @@ public class GatewayInterfaceInfoServiceImpl implements IGatewayInterfaceInfoSer return gatewayInterfaceLinkStrategyMapper.selectGatewayInterfaceLinkStrategyByInterfaceIds(interfaceIds); } + @Override + public List selectGatewayInterfaceMockListByInterfaceIds(Collection interfaceIds) { + if (CollectionUtils.isEmpty(interfaceIds)) { + return Collections.emptyList(); + } + return gatewayInterfaceMockMapper.selectGatewayInterfaceMockByInterfaceIds(interfaceIds); + + } + private void checkGatewayStrategyTypeCannotBeRepeated(List strategyIds) { List strategyList = gatewayStrategyService.selectGatewayStrategyByIds(strategyIds); Map> groupByStrategyTypeMap = strategyList.stream().collect(Collectors.groupingBy(GatewayStrategy::getStrategyType)); diff --git a/sf-service/src/main/java/com/sf/service/gateway/service/impl/GatewayRouteServiceImpl.java b/sf-service/src/main/java/com/sf/service/gateway/service/impl/GatewayRouteServiceImpl.java index 24f9321..fc26adc 100644 --- a/sf-service/src/main/java/com/sf/service/gateway/service/impl/GatewayRouteServiceImpl.java +++ b/sf-service/src/main/java/com/sf/service/gateway/service/impl/GatewayRouteServiceImpl.java @@ -17,6 +17,7 @@ import com.sf.service.gateway.enums.*; import com.sf.service.gateway.mapper.GatewayRouteMapper; import com.sf.service.gateway.service.IGatewayRouteService; import com.sf.service.gateway.service.IGatewayServerService; +import com.sf.vertx.enums.MatchType; import org.springframework.dao.DuplicateKeyException; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; diff --git a/sf-service/src/main/java/com/sf/service/gateway/service/impl/GatewayStrategyServiceImpl.java b/sf-service/src/main/java/com/sf/service/gateway/service/impl/GatewayStrategyServiceImpl.java index b1ebadb..1620004 100644 --- a/sf-service/src/main/java/com/sf/service/gateway/service/impl/GatewayStrategyServiceImpl.java +++ b/sf-service/src/main/java/com/sf/service/gateway/service/impl/GatewayStrategyServiceImpl.java @@ -8,6 +8,7 @@ import java.util.List; import apijson.JSON; import cn.hutool.core.collection.CollUtil; import com.sf.common.utils.DateUtils; +import com.sf.common.utils.JSONUtils; import com.sf.service.gateway.domain.GatewayInterfaceLinkStrategy; import com.sf.service.gateway.domain.GatewayServer; import com.sf.service.gateway.domain.GatewayStrategy; @@ -142,7 +143,7 @@ public class GatewayStrategyServiceImpl implements IGatewayStrategyService }else { Assert.isTrue(currentLimitingTimeWindow >= 0,"限流窗口值>=0"); } - Assert.isTrue(JSON.isJSONObject(currentLimitingResponse),"请输入正确的JSON格式限流响应信息"); + Assert.isTrue(JSONUtils.isJSONObject(currentLimitingResponse),"请输入正确的JSON格式限流响应信息"); } /** @@ -161,7 +162,7 @@ public class GatewayStrategyServiceImpl implements IGatewayStrategyService Assert.isTrue(circuitBreakerTimeWindow >= 0,"调用失败阈值对应的单位时间>=0"); Assert.notNull(circuitBreakerRecoveryInterval,"熔断后恢复的时间间隔不能为空"); Assert.isTrue(circuitBreakerRecoveryInterval >= 0,"熔断后恢复的时间间隔>=0"); - Assert.isTrue(JSON.isJSONObject(circuitBreakerResponse),"请输入正确的JSON格式熔断响应信息"); + Assert.isTrue(JSONUtils.isJSONObject(circuitBreakerResponse),"请输入正确的JSON格式熔断响应信息"); } /** diff --git a/sf-service/src/main/resources/mapper/gateway/GatewayInterfaceInfoMapper.xml b/sf-service/src/main/resources/mapper/gateway/GatewayInterfaceInfoMapper.xml index 491d459..0629e8a 100644 --- a/sf-service/src/main/resources/mapper/gateway/GatewayInterfaceInfoMapper.xml +++ b/sf-service/src/main/resources/mapper/gateway/GatewayInterfaceInfoMapper.xml @@ -20,7 +20,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" - + + @@ -28,7 +29,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" select id, interface_name,api_code, interface_path, request_method, description, version, status, document, created, create_time, modified, update_time , - server_id,mock_status,mock_response,timeout + server_id,mock_status,mock_default_http_status,mock_default_response,timeout from Gateway_interface_info @@ -81,7 +82,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" modified, server_id, mock_status, - mock_response, + mock_default_http_status, + mock_default_response, timeout, @@ -100,7 +102,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" #{modified}, #{serverId}, #{mockStatus}, - #{mockResponse}, + #{mockDefaultHttpStatus}, + #{mockDefaultResponse}, #{timeout}, @@ -120,7 +123,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" update_time = #{updateTime}, server_id = #{serverId}, mock_status = #{mockStatus}, - mock_response = #{mockResponse}, + mock_default_http_status = #{mockDefaultHttpStatus}, + mock_default_response = #{mockDefaultResponse}, timeout = #{timeout} where id = #{id} diff --git a/sf-service/src/main/resources/mapper/gateway/GatewayInterfaceMockMapper.xml b/sf-service/src/main/resources/mapper/gateway/GatewayInterfaceMockMapper.xml new file mode 100644 index 0000000..be550ad --- /dev/null +++ b/sf-service/src/main/resources/mapper/gateway/GatewayInterfaceMockMapper.xml @@ -0,0 +1,137 @@ + + + + + + + + + + + + + + + + id,interface_id,http_status, + match_conditions,mock_response,status, + order_num + + + + + + + + delete from Gateway_interface_mock + where id = #{id,jdbcType=BIGINT} + + + + delete from Gateway_interface_mock + where interface_id in + + #{id} + + + + + insert into Gateway_interface_mock + ( id,interface_id,http_status + ,match_conditions,mock_response,status + ,order_num) + values (#{id,jdbcType=BIGINT},#{interfaceId,jdbcType=BIGINT},#{httpStatus,jdbcType=INTEGER} + ,#{matchConditions,jdbcType=VARCHAR},#{mockResponse,jdbcType=VARCHAR},#{status,jdbcType=CHAR} + ,#{orderNum,jdbcType=INTEGER}) + + + insert into Gateway_interface_mock + + id, + interfaceId, + httpStatus, + matchConditions, + mockResponse, + status, + orderNum, + + + id = #{id,jdbcType=BIGINT}, + interface_id = #{interfaceId,jdbcType=BIGINT}, + http_status = #{httpStatus,jdbcType=INTEGER}, + match_conditions = #{matchConditions,jdbcType=VARCHAR}, + mock_response = #{mockResponse,jdbcType=VARCHAR}, + status = #{status,jdbcType=CHAR}, + order_num = #{orderNum,jdbcType=INTEGER}, + + + + + insert into Gateway_interface_mock + ( interface_id,http_status + ,match_conditions,mock_response,status + ,order_num) + values + + ( + #{mock.interfaceId,jdbcType=BIGINT}, + #{mock.httpStatus,jdbcType=INTEGER}, + #{mock.matchConditions,jdbcType=VARCHAR}, + #{mock.mockResponse,jdbcType=VARCHAR}, + #{mock.status,jdbcType=CHAR}, + #{mock.orderNum,jdbcType=INTEGER} + ) + + + + + update Gateway_interface_mock + + + interface_id = #{interfaceId,jdbcType=BIGINT}, + + + http_status = #{httpStatus,jdbcType=INTEGER}, + + + match_conditions = #{matchConditions,jdbcType=VARCHAR}, + + + mock_response = #{mockResponse,jdbcType=VARCHAR}, + + + status = #{status,jdbcType=CHAR}, + + + order_num = #{orderNum,jdbcType=INTEGER}, + + + where id = #{id,jdbcType=BIGINT} + + + update Gateway_interface_mock + set + interface_id = #{interfaceId,jdbcType=BIGINT}, + http_status = #{httpStatus,jdbcType=INTEGER}, + match_conditions = #{matchConditions,jdbcType=VARCHAR}, + mock_response = #{mockResponse,jdbcType=VARCHAR}, + status = #{status,jdbcType=CHAR}, + order_num = #{orderNum,jdbcType=INTEGER} + where id = #{id,jdbcType=BIGINT} + + diff --git a/sf-vertx-api/src/main/java/com/sf/vertx/api/pojo/ApiConfig.java b/sf-vertx-api/src/main/java/com/sf/vertx/api/pojo/ApiConfig.java index e8b4a7b..3121eb4 100644 --- a/sf-vertx-api/src/main/java/com/sf/vertx/api/pojo/ApiConfig.java +++ b/sf-vertx-api/src/main/java/com/sf/vertx/api/pojo/ApiConfig.java @@ -12,6 +12,8 @@ public class ApiConfig implements Serializable { private String uri; private String method; // 大写 private Long timeout = 3000L; // 超时时间,单位毫秒 - private String mockResponse; + private Integer mockDefaultHttpStatus; + private String mockDefaultResponse; private List strategy; // 策略 + private List mockExpectations; // mock期望 } diff --git a/sf-vertx-api/src/main/java/com/sf/vertx/api/pojo/MockExpectation.java b/sf-vertx-api/src/main/java/com/sf/vertx/api/pojo/MockExpectation.java new file mode 100644 index 0000000..c3d2169 --- /dev/null +++ b/sf-vertx-api/src/main/java/com/sf/vertx/api/pojo/MockExpectation.java @@ -0,0 +1,28 @@ +package com.sf.vertx.api.pojo; + +import lombok.Data; + +import java.util.List; + +/** +* 网关服务-接口Mock期望 +*/ +@Data +public class MockExpectation { + + /** + * http状态码 + */ + private Integer httpStatus; + + /** + * Mock响应,JSON字符串 + */ + private String mockResponse; + + /** + * 匹配条件 + */ + private List matchConditions; + +} diff --git a/sf-vertx-api/src/main/java/com/sf/vertx/api/pojo/MockMatchCondition.java b/sf-vertx-api/src/main/java/com/sf/vertx/api/pojo/MockMatchCondition.java new file mode 100644 index 0000000..be735f0 --- /dev/null +++ b/sf-vertx-api/src/main/java/com/sf/vertx/api/pojo/MockMatchCondition.java @@ -0,0 +1,35 @@ +package com.sf.vertx.api.pojo; + +import lombok.Data; + +import java.util.List; + +/** +* 接口Mock匹配条件 +*/ +@Data +public class MockMatchCondition { + + /** + * 参数位置,header、query、body + */ + private String parameterPosition; + + /** + * 参数键值 + */ + private String parameterKey; + + /** + * 参数值,包含、不包含类型允许多个值,所以使用数组,其余类型都只有一个值。 + */ + private List parameterValue; + + /** + * 匹配类型 + */ + private String matchType; + + + +} diff --git a/sf-service/src/main/java/com/sf/service/gateway/enums/MatchType.java b/sf-vertx-api/src/main/java/com/sf/vertx/enums/MatchType.java similarity index 58% rename from sf-service/src/main/java/com/sf/service/gateway/enums/MatchType.java rename to sf-vertx-api/src/main/java/com/sf/vertx/enums/MatchType.java index ba838e3..3b93cc7 100644 --- a/sf-service/src/main/java/com/sf/service/gateway/enums/MatchType.java +++ b/sf-vertx-api/src/main/java/com/sf/vertx/enums/MatchType.java @@ -1,4 +1,4 @@ -package com.sf.service.gateway.enums; +package com.sf.vertx.enums; /** * 匹配方式 @@ -7,8 +7,16 @@ package com.sf.service.gateway.enums; */ public enum MatchType { - EQ("EQ", "完全匹配"), - IN("IN", "包含匹配"), + EQ("EQ", "等于"), + NOT_EQ("NOT_EQ", "不等于"), + GT("GT", "大于"), + GE("GE", "大于或等于"), + LT("LT", "小于"), + LE("LE", "小于或等于"), + IN("IN", "包含"), + NOT_IN("NOT_IN", "不包含"), + IS_NULL("IS_NULL", "等于空"), + NOT_NULL("NOT_NULL", "不等于空"), ; private final String code; @@ -32,7 +40,7 @@ public enum MatchType public static MatchType getByCode(String code){ for (MatchType value : MatchType.values()) { - if (value.code.equals(code)){ + if (value.code.equalsIgnoreCase(code)){ return value; } }