SpringStateMachine狀態機之八-整合測試

weixin_34208283發表於2019-01-11

測試程式碼

弄了個全集的測試程式碼,如下:

存在逾期、部分還款
    /**
     *
     * 從建立、放款、逾期到部分還款
     *
     * @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驅動整個過程。

相關文章