Spring StateMachine状态机引擎在项目中的应用(七)-集成测试

测试代码

弄了个全集的测试代码,如下:

存在逾期、部分还款
    /**
     *
     * 从创建、放款、逾期到部分还款
     *
     * @return
     * @throws OspException
     */
    @Test
    public void testFromOverdueToRepaid() throws OspException {
        // a. 创建已签约接口
        String bizOrderId = createSignOrder();

        // b.构造放款请求
        BizOrderStatusRequest loanReq = genStatusRequestFromCondition(bizOrderId,
                BizOrderOperationTypeEnum.LOAN,
                SIGNED,
                null  // 可填LOANING,也可不填,会有默认值。但是不能填别的状态,状态机会禁止随意改变状态
        );
        OrderBaseResponse response = bizOrderStatusService.loan(loanReq);
        System.out.println(response);
        Assert.assertNull(response.getMsg());
        Assert.assertEquals(BizOrderErrorCode.SUCCESS.name(), response.getResultCode());

        // c. 放款成功请求
        BizOrderStatusRequest loanSuccReq = genStatusRequestFromCondition(bizOrderId,
                BizOrderOperationTypeEnum.LOAN,  // 放款失败,直接关单
                LOANING,
                LOANED  // 可不传,会默认
        );
        OrderBaseResponse loanSuccResp = bizOrderStatusService.loanSucc(loanSuccReq);
        System.out.println(loanSuccResp);
        Assert.assertNull(loanSuccResp.getMsg());
        Assert.assertEquals(BizOrderErrorCode.SUCCESS.name(), loanSuccResp.getResultCode());

        // d.生成账单请求
        BizOrderStatusRequest genBillReq = genStatusRequestFromCondition(bizOrderId,
                BizOrderOperationTypeEnum.GEN_BILL,
                LOANED,
                null  // 可以不传,也可以传BILL_BILL
        );

        OrderBaseResponse genBillResp = bizOrderStatusService.genBill(genBillReq);
        System.out.println(genBillResp);
        Assert.assertNull(genBillResp.getMsg());
        Assert.assertEquals(BizOrderErrorCode.SUCCESS.name(), genBillResp.getResultCode());

        // e. 到逾期
        BizOrderStatusRequest overdueReq = genStatusRequestFromCondition(bizOrderId,
                BizOrderOperationTypeEnum.OVERDUE,
                BILL_GEN,
                OVERDUE
        );
        OrderBaseResponse overdueResp = bizOrderStatusService.overdue(overdueReq);
        System.out.println(overdueResp);
        Assert.assertNull(overdueResp.getMsg());
        Assert.assertEquals(BizOrderErrorCode.SUCCESS.name(), overdueResp.getResultCode());

        // f. 从逾期到部分还款
        BizOrderStatusRequest partRepayReq = genStatusRequestFromCondition(bizOrderId,
                BizOrderOperationTypeEnum.REPAY,
                OVERDUE,
                REPAID  // 置为还款中,必传
        );
        partRepayReq.getBizOrderStatusModel().setEffectAmount(2.5);

        OrderBaseResponse partRepayResp = bizOrderStatusService.repay(partRepayReq);
        System.out.println(partRepayResp);
        Assert.assertNull(partRepayResp.getMsg());
        Assert.assertEquals(BizOrderErrorCode.SUCCESS.name(), partRepayResp.getResultCode());

        // g. 校验状态是否为部分还款
        SingleOrderResponse queryResponse = bizOrderQueryService.findOrderByBizOrderId(bizOrderId);
        Assert.assertNull(queryResponse.getMsg());
        Assert.assertEquals(BizOrderErrorCode.SUCCESS.name(), queryResponse.getResultCode());
        Assert.assertEquals(BizOrderStatusEnum.REPAID.getStatus(), queryResponse.getOrderModel().getOrderStatus());

    }
从放款到全部还款,不存在逾期及部分还款
    /**
     * 从还款到最终订单结束
     *
     * 测试顺序 --- 2
     *
     * @return
     * @throws OspException
     */
    @Test
    public void testFromRepaidToSuccess() throws OspException {
        // a. 创建已签约接口
        String bizOrderId = createSignOrder();

        // b.构造放款请求
        BizOrderStatusRequest loanReq = genStatusRequestFromCondition(bizOrderId,
                BizOrderOperationTypeEnum.LOAN,
                SIGNED,
                null  // 可填LOANING,也可不填,会有默认值。但是不能填别的状态,状态机会禁止随意改变状态
        );
        OrderBaseResponse response = bizOrderStatusService.loan(loanReq);
        System.out.println(response);
        Assert.assertNull(response.getMsg());
        Assert.assertEquals(BizOrderErrorCode.SUCCESS.name(), response.getResultCode());

        // c. 放款成功请求
        BizOrderStatusRequest loanSuccReq = genStatusRequestFromCondition(bizOrderId,
                BizOrderOperationTypeEnum.LOAN,  // 放款失败,直接关单
                LOANING,
                LOANED  // 可不传,会默认
        );
        OrderBaseResponse loanSuccResp = bizOrderStatusService.loanSucc(loanSuccReq);
        System.out.println(loanSuccResp);
        Assert.assertNull(loanSuccResp.getMsg());
        Assert.assertEquals(BizOrderErrorCode.SUCCESS.name(), loanSuccResp.getResultCode());

        // d.生成账单请求
        BizOrderStatusRequest genBillReq = genStatusRequestFromCondition(bizOrderId,
                BizOrderOperationTypeEnum.GEN_BILL,
                LOANED,
                null  // 可以不传,也可以传BILL_BILL
        );

        OrderBaseResponse genBillResp = bizOrderStatusService.genBill(genBillReq);
        System.out.println(genBillResp);
        Assert.assertNull(genBillResp.getMsg());
        Assert.assertEquals(BizOrderErrorCode.SUCCESS.name(), genBillResp.getResultCode());

        // e. 全部还款
        BizOrderStatusRequest repayReq = genStatusRequestFromCondition(bizOrderId,
                BizOrderOperationTypeEnum.REPAY,
                BILL_GEN,
                REPAID  // 置为全部还款,必传
        );
        repayReq.getBizOrderStatusModel().setEffectAmount(6.5);

        OrderBaseResponse repayResp = bizOrderStatusService.repay(repayReq);
        System.out.println(repayResp);
        Assert.assertNull(repayResp.getMsg());
        Assert.assertEquals(BizOrderErrorCode.SUCCESS.name(), repayResp.getResultCode());

        // f. 从还款到销账
        BizOrderStatusRequest successReq = genStatusRequestFromCondition(bizOrderId,
                BizOrderOperationTypeEnum.SUCCESS,
                REPAID,
                SUCCESS  // 置为全部还款,必传
        );

        OrderBaseResponse succResp = bizOrderStatusService.succ(successReq);
        System.out.println(succResp);
        Assert.assertNull(succResp.getMsg());
        Assert.assertEquals(BizOrderErrorCode.SUCCESS.name(), succResp.getResultCode());

        // g. 校验状态是否为全部还款
        SingleOrderResponse queryResponse = bizOrderQueryService.findOrderByBizOrderId(bizOrderId);
        Assert.assertNull(queryResponse.getMsg());
        Assert.assertEquals(BizOrderErrorCode.SUCCESS.name(), queryResponse.getResultCode());
        Assert.assertEquals(BizOrderStatusEnum.SUCCESS.getStatus(), queryResponse.getOrderModel().getOrderStatus());

    }
构造订单变化请求,私有方法
    /**
     * 从创建订单开始构造状态变化请求
     *
     * @param initStatus
     * @param operationTypeEnum
     * @param curStatus
     * @param targetStatus
     * @return
     * @throws OspException
     */
    private BizOrderStatusRequest genStatusRequestFromCreate(BizOrderStatusEnum initStatus,
                                                             BizOrderOperationTypeEnum operationTypeEnum,
                                                             BizOrderStatusEnum curStatus,
                                                             BizOrderStatusEnum targetStatus
    ) throws OspException {

        BizOrderStatusRequest statusRequest = new BizOrderStatusRequest();

        statusRequest.setBizCode(createOrder(initStatus));
        statusRequest.setOperationType(operationTypeEnum.getOperationType());
        statusRequest.setSourceId(IDGenerater.getUniId());

        BizOrderStatusModel statusModel = new BizOrderStatusModel();
        statusModel.setBizType(BizOrderBizTypeEnum.EMPLOAN.getOrderBizType());
        statusModel.setBizOrderId(statusRequest.getBizCode());
        statusModel.setCurrentOrderStatus(curStatus.getStatus());
        if (null != targetStatus) {
            statusModel.setTargetOrderStatus(targetStatus.getStatus());
        }
        statusRequest.setBizOrderStatusModel(statusModel);
        statusRequest.setCallSystem("vip-2");

        return statusRequest;
    }

从制定状态 构造 状态变化请求
    /**
     * 从指定条件构造状态变化请求
     *
     * @param bizOrderId
     * @param operationTypeEnum
     * @param currentStatus
     * @param targetStatus
     * @return
     */
    private BizOrderStatusRequest genStatusRequestFromCondition(String bizOrderId,
                                                                BizOrderOperationTypeEnum operationTypeEnum,
                                                                BizOrderStatusEnum currentStatus,
                                                                BizOrderStatusEnum targetStatus) {

        BizOrderStatusRequest statusRequest = new BizOrderStatusRequest();

        statusRequest.setBizCode(bizOrderId);
        statusRequest.setOperationType(operationTypeEnum.getOperationType());
        statusRequest.setSourceId(IDGenerater.getUniId());

        BizOrderStatusModel statusModel = new BizOrderStatusModel();
        statusModel.setBizType(BizOrderBizTypeEnum.EMPLOAN.getOrderBizType());
        statusModel.setBizOrderId(statusRequest.getBizCode());
        statusModel.setCurrentOrderStatus(currentStatus.getStatus());
        if (null != targetStatus) {
            statusModel.setTargetOrderStatus(targetStatus.getStatus());
        }
        statusRequest.setBizOrderStatusModel(statusModel);
        statusRequest.setCallSystem("wyd-new");

        return statusRequest;
    }

    /**
     * 创建订单,并入库
     *
     * @param targetStatus
     * @return
     * @throws OspException
     */
    private String createOrder(BizOrderStatusEnum targetStatus) throws OspException {
        init();
        BizOrderCreateRequest createRequest = new BizOrderCreateRequest();

        createRequest.setBizCode("001"); // 创建时指定传输内容001
        createRequest.setSourceId(IDGenerater.getUniId());
        createRequest.setOperationType(BizOrderOperationTypeEnum.CREATE_ORDER.getOperationType());
        createRequest.setCallSystem("vip-pay");

        BizOrderCreateModel createModel = new BizOrderCreateModel();
        createRequest.setBizOrderCreateModel(createModel);

        createModel.setUserId(131415161788l);
        createModel.setMerchantId(8l);
        createModel.setContractId(3l);
        createModel.setProdId(20l);
//        createModel.setProdCode(); 可为空
        createModel.setBizType(BizOrderBizTypeEnum.EMPLOAN.getOrderBizType()); // 无忧贷
        createModel.setOrderStatus(targetStatus.getStatus());
        createModel.setOrderSrc("h6");

        BizOrderExtendsCreateModel extendsModel = new BizOrderExtendsCreateModel();
        createRequest.setExtendsCreateModel(extendsModel);

        extendsModel.setUserRealname("张小强");

        if (log.isDebugEnabled()) {
            log.debug("debug log level");
        }

        // 创建订单
        BizOrderCreateResponse response = bizOrderCreateService.create(createRequest);
        System.out.println(response);
        Assert.assertNull(response.getMsg());
        Assert.assertEquals(BizOrderErrorCode.SUCCESS.name(), response.getResultCode());

        SingleOrderResponse queryResponse = bizOrderQueryService.findOrderByBizOrderId(response.getBizOrderId());
        Assert.assertNull(queryResponse.getMsg());
        Assert.assertEquals(BizOrderErrorCode.SUCCESS.name(), queryResponse.getResultCode());
        Assert.assertEquals(targetStatus.getStatus(), queryResponse.getOrderModel().getOrderStatus());

        return response.getBizOrderId();
    }

    /**
     * 构造已签约完成的订单,并返回订单ID
     *
     * @return
     * @throws OspException
     */
    private String createSignOrder() throws OspException {
        // a. 创建状态为WAIT_BORROW的订单
        BizOrderCreateRequest createRequest = wrapCreateRequest(BizOrderStatusEnum.WAIT_BORROW);

        BizOrderCreateResponse response = bizOrderCreateService.create(createRequest);
        System.out.println(response);
        Assert.assertNull(response.getMsg());
        Assert.assertEquals(BizOrderErrorCode.SUCCESS.name(), response.getResultCode());

        SingleOrderResponse queryResponse1 = bizOrderQueryService.findOrderByBizOrderId(response.getBizOrderId());
        BizOrderModel bizOrderModel = queryResponse1.getOrderModel();


        // a.1 构造审核成功对应请求
        BizOrderStatusRequest inDealRiskAuditSuccReq = genStatusRequestFromCondition(response.getBizOrderId(),
                BizOrderOperationTypeEnum.AUDIT,
                WAIT_BORROW,
                WAIT_SIGN);

        OrderBaseResponse auditResp = bizOrderStatusService.audit(inDealRiskAuditSuccReq);
        System.out.println(auditResp);
        Assert.assertNull(auditResp.getMsg());
        Assert.assertEquals(BizOrderErrorCode.SUCCESS.name(), auditResp.getResultCode());

        // b. 签约,处理签约过程中的请求数据,这里最好把所有数据都传入,数据一定要做校验的

        // b.1 最外层数据
        BizOrderCreateRequest bizOrderCreateRequest = new BizOrderCreateRequest();
        bizOrderCreateRequest.setBizCode(response.getBizOrderId());
        bizOrderCreateRequest.setOperationType(BizOrderOperationTypeEnum.SIGN.getOperationType());
        bizOrderCreateRequest.setSourceId(IDGenerater.getUniId());
        bizOrderCreateRequest.setCallSystem("wyd");

        // b.2 订单主数据
        BizOrderCreateModel createModel = new BizOrderCreateModel();
        createModel.setBizOrderId(bizOrderCreateRequest.getBizCode());
//        createModel.setOrderStatus(BizOrderStatusEnum.SIGNED.getStatus());
        createModel.setBizType(BizOrderBizTypeEnum.EMPLOAN.getOrderBizType());
        createModel.setUserId(bizOrderModel.getUserId());
        createModel.setContractId(bizOrderModel.getContractId());
        createModel.setMerchantId(bizOrderModel.getMerchantId());
        createModel.setProdId(bizOrderModel.getProdId());
        createModel.setProdCode(bizOrderModel.getProdCode());

        // b.3 资金相关  --- 注意,资金信息需要保持平衡
        createModel.setAmount(14.5);
        createModel.setRealAmount(10.89);
        createModel.setRepayType("等额本金");
        createModel.setScene("个人消费");
        createModel.setRate(0.0035);
        createModel.setRateType(RateTypeEnum.DAY.getRateType());
        createModel.setOverdueRate(0.007);
        createModel.setOverdueRateType(RateTypeEnum.DAY.getRateType());
        createModel.setLoanStartDate(Date.from(Instant.now()));
        createModel.setLoanEndDate(Date.from(Instant.now().plus(30, ChronoUnit.DAYS)));

        bizOrderCreateRequest.setBizOrderCreateModel(createModel);

        // b.4 上传贷款合同  -- 可省略,本次在测试,构造全量数据
        BizOrderLoanContractUploadModel model1 = new BizOrderLoanContractUploadModel();
        model1.setContractNo("ab0011005");
        model1.setContractContent("https://www.vipshop.com/files/contentab0011aa00.pdf");

        BizOrderLoanContractUploadModel model2 = new BizOrderLoanContractUploadModel();
        model2.setContractNo("abc000111");
        model2.setContractContent("http://sina.com/fileserver/contentabc000111.txt");

        bizOrderCreateRequest.setContractModels(Lists.newArrayList(
                model1, model2
        ));

        // b.5 活动信息  -- 可不传,本次测试,构造全量数据
        BizOrderActivityCreateModel activity1 = new BizOrderActivityCreateModel();
        activity1.setActivityCode("ac001001");
        activity1.setActivityName("优惠券");
        activity1.setActivityType("COUPON");
        activity1.setActivitySrc("唯品会419大促金融分会馆");
        activity1.setCouponAmount(1.22);
        activity1.setEffectTime(Date.from(Instant.now().plus(-1, ChronoUnit.DAYS)));
        activity1.setSerialNo("c293573272572385782937592759237");

        BizOrderActivityCreateModel activity2 = new BizOrderActivityCreateModel();
        activity2.setActivitySrc("唯品会419大促金融冲击波");
        activity2.setActivityType("DISCOUNT");
        activity2.setActivityName("活动优惠立减 1%利息");
        activity2.setActivityCode("DS00120101");
        activity2.setDiscountRate(0.01);
        activity2.setDiscountRateSrc("唯品会");
        activity2.setDiscountRateType(RateTypeEnum.DAY.getRateType());
        activity2.setSerialNo("DS0010101010438503038930894");

        bizOrderCreateRequest.setActivityModels(Lists.newArrayList(
                activity1, activity2
        ));

        // b.6 放款渠道
        BizOrderChannelCreateModel channelCreateModel1 = new BizOrderChannelCreateModel();
        channelCreateModel1.setChannelNo("CASH0001");
        channelCreateModel1.setChannelAmount(5);
        channelCreateModel1.setChannelName("自有资金");

        BizOrderChannelCreateModel channelCreateModel2 = new BizOrderChannelCreateModel();
        channelCreateModel2.setChannelNo("CASH001-BANK");
        channelCreateModel2.setChannelAmount(5.89);
        channelCreateModel2.setChannelName("1号银行渠道");

        bizOrderCreateRequest.setChannelModels(Lists.newArrayList(channelCreateModel1, channelCreateModel2));

        // c.6 扩展信息更新
        BizOrderExtendsCreateModel extendsCreateModel = new BizOrderExtendsCreateModel();
        extendsCreateModel.setRepayDate(Date.from(Instant.now().plus(-15, ChronoUnit.DAYS)));
        extendsCreateModel.setGoodsName("update-ada");
        bizOrderCreateRequest.setExtendsCreateModel(extendsCreateModel);


        // c. 签约
        OrderBaseResponse signResp = bizOrderCreateService.createFromSign(bizOrderCreateRequest);
        System.out.println(signResp);
        Assert.assertNull(signResp.getMsg());
        Assert.assertEquals(BizOrderErrorCode.SUCCESS.name(), signResp.getResultCode());

        // d. 校验
        SingleOrderResponse queryResponse = bizOrderQueryService.findOrderByBizOrderId(response.getBizOrderId());
        Assert.assertNull(queryResponse.getMsg());
        Assert.assertEquals(BizOrderErrorCode.SUCCESS.name(), queryResponse.getResultCode());
        Assert.assertEquals(BizOrderStatusEnum.SIGNED.getStatus(), queryResponse.getOrderModel().getOrderStatus());

        return response.getBizOrderId();
    }

    private BizOrderCreateRequest wrapCreateRequest(BizOrderStatusEnum targetStatus) {
        init();
        BizOrderCreateRequest createRequest = new BizOrderCreateRequest();

        createRequest.setBizCode("001"); // 创建时指定传输内容001
        createRequest.setSourceId(IDGenerater.getUniId());
        createRequest.setOperationType(BizOrderOperationTypeEnum.CREATE_ORDER.getOperationType());
        createRequest.setCallSystem("vip-pay-2");

        BizOrderCreateModel createModel = new BizOrderCreateModel();
        createRequest.setBizOrderCreateModel(createModel);

        createModel.setUserId(131415161788l);
        createModel.setMerchantId(8l);
        createModel.setContractId(3l);
        createModel.setProdId(20l);
//        createModel.setProdCode(); 可为空
        createModel.setBizType(BizOrderBizTypeEnum.EMPLOAN.getOrderBizType()); // 无忧贷
        createModel.setOrderStatus(targetStatus.getStatus());
        createModel.setOrderSrc("vipfins-ios");

        BizOrderExtendsCreateModel extendsModel = new BizOrderExtendsCreateModel();
        createRequest.setExtendsCreateModel(extendsModel);

        extendsModel.setUserRealname("张小强");
        // 10天之后还款
        extendsModel.setRepayDate(Date.from(Instant.now().plus(10, ChronoUnit.DAYS)));


        if (log.isDebugEnabled()) {
            log.debug("debug log level");
        }

        return createRequest;
    }

这里使用junit,模拟整个订单的生命周期,发送对应的事件event驱动整个过程。

你可能感兴趣的:(Spring StateMachine状态机引擎在项目中的应用(七)-集成测试)