PlanValidateTest.java 11.2 KB
package com.bsth.service.schedule;

import com.bsth.BaseTest_junit4;
import com.bsth.TestApplication;
import com.bsth.service.schedule.plan.process._1_validate._1_timetable.PlanProcessValidateTimetableService;
import com.bsth.service.schedule.plan.process._1_validate._1_timetable.PlanProcessValidateTimetableServiceDroolsImpl;
import com.bsth.service.schedule.plan.process._1_validate._2_rule.PlanProcessValidateRuleService;
import com.bsth.service.schedule.plan.process._1_validate._2_rule.PlanProcessValidateRuleServiceDroolsImpl;
import org.joda.time.format.DateTimeFormat;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.kie.api.KieBase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.*;

/**
 * 排班计划,前置验证测试。
 */
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = {TestApplication.class})
public class PlanValidateTest extends BaseTest_junit4 {
    @Override
    public Map<String, String> getDbunitTestDbFileClassPathMap() {
        Map<String, String> dbFileMap = new HashMap<>();
        dbFileMap.put("_0_validate_timetable_test_case1", "testdata/_0_validate_timetable.xml");
        dbFileMap.put("_1_validate_rule_test_case1", "testdata/_1_validate_rule.xml");
        return dbFileMap;
    }

    @Autowired
    @Qualifier("kBaseValidateTimetable_plus")
    private KieBase kBaseValidateTimetable;

    @Autowired
    @Qualifier("kBaseValidateRule_plus")
    private KieBase kBaseValidateRule;

    @Autowired
    private JdbcTemplate jdbcTemplate;


    @Test
    public void _0_validate_timetable_test_case1() {
        LOG.info("---------------时刻表验证测试--------------");

        // 使用builder创建服务
        PlanProcessValidateTimetableService planProcessValidateTimetableService =
                PlanProcessValidateTimetableServiceDroolsImpl.getBuilder()
                        .setkBaseValidateTimetable(kBaseValidateTimetable)
                        .setJdbcTemplate(jdbcTemplate)
                        .build();

        /*
            验证时刻表信息服务:
            1、计算指定时间内匹配的所有时刻表的详细信息
            2、计算时刻表冲突信息(同一天多张时刻表)
            3、计算时刻表空信息(当天没有时刻表)
         */
        PlanProcessValidateTimetableService.Result result = planProcessValidateTimetableService.validateTTInfovalidateTTInfo(
                1,
                DateTimeFormat.forPattern("yyyy-MM-dd").parseDateTime("2019-01-01").toDate(),
                DateTimeFormat.forPattern("yyyy-MM-dd").parseDateTime("2019-01-14").toDate()
        );

        //--------------- 测试1 ----------------//
        Assert.assertArrayEquals(
                "时刻表数量不一致",
                new Object[] {4},
                new Object[] {result.getInfos().size()});

        //--------------- 测试2 ----------------//
        PlanProcessValidateTimetableService.ConflictStatInfo expectConflict1 = new PlanProcessValidateTimetableService.ConflictStatInfo();
        expectConflict1.setDateDesc("2019年01月04日");
        expectConflict1.getTtIdList().addAll(Arrays.asList(1L, 2L));
        expectConflict1.getTtNameList().addAll(Arrays.asList("时刻表1", "时刻表2"));
        PlanProcessValidateTimetableService.ConflictStatInfo expectConflict2 = new PlanProcessValidateTimetableService.ConflictStatInfo();
        expectConflict2.setDateDesc("2019年01月05日");
        expectConflict2.getTtIdList().addAll(Arrays.asList(3L, 4L));
        expectConflict2.getTtNameList().addAll(Arrays.asList("时刻表3", "时刻表4"));
        PlanProcessValidateTimetableService.ConflictStatInfo expectConflict3 = new PlanProcessValidateTimetableService.ConflictStatInfo();
        expectConflict3.setDateDesc("2019年01月06日");
        expectConflict3.getTtIdList().addAll(Arrays.asList(3L, 4L));
        expectConflict3.getTtNameList().addAll(Arrays.asList("时刻表3", "时刻表4"));
        PlanProcessValidateTimetableService.ConflictStatInfo expectConflict4 = new PlanProcessValidateTimetableService.ConflictStatInfo();
        expectConflict4.setDateDesc("2019年01月07日");
        expectConflict4.getTtIdList().addAll(Arrays.asList(3L, 4L));
        expectConflict4.getTtNameList().addAll(Arrays.asList("时刻表3", "时刻表4"));
        PlanProcessValidateTimetableService.ConflictStatInfo expectConflict5 = new PlanProcessValidateTimetableService.ConflictStatInfo();
        expectConflict5.setDateDesc("2019年01月11日");
        expectConflict5.getTtIdList().addAll(Arrays.asList(1L, 2L));
        expectConflict5.getTtNameList().addAll(Arrays.asList("时刻表1", "时刻表2"));

        // 注意:排序是为了测试需要
        List<PlanProcessValidateTimetableService.ConflictStatInfo> expectConflictList = new ArrayList<>();
        expectConflictList.add(expectConflict1);
        expectConflictList.add(expectConflict2);
        expectConflictList.add(expectConflict3);
        expectConflictList.add(expectConflict4);
        expectConflictList.add(expectConflict5);
        for (PlanProcessValidateTimetableService.ConflictStatInfo cInfo : expectConflictList) {
            Collections.sort(cInfo.getTtIdList());
            Collections.sort(cInfo.getTtNameList());
        }
        Collections.sort(expectConflictList, new Comparator<PlanProcessValidateTimetableService.ConflictStatInfo>() {
            @Override
            public int compare(PlanProcessValidateTimetableService.ConflictStatInfo o1, PlanProcessValidateTimetableService.ConflictStatInfo o2) {
                return o1.getDateDesc().compareTo(o2.getDateDesc());
            }
        });

        List<PlanProcessValidateTimetableService.ConflictStatInfo> actualConflictList = result.getConflictStatInfos();
        for (PlanProcessValidateTimetableService.ConflictStatInfo cInfo : actualConflictList) {
            Collections.sort(cInfo.getTtIdList());
            Collections.sort(cInfo.getTtNameList());
        }
        Collections.sort(actualConflictList, new Comparator<PlanProcessValidateTimetableService.ConflictStatInfo>() {
            @Override
            public int compare(PlanProcessValidateTimetableService.ConflictStatInfo o1, PlanProcessValidateTimetableService.ConflictStatInfo o2) {
                return o1.getDateDesc().compareTo(o2.getDateDesc());
            }
        });

        Assert.assertArrayEquals(
                "时刻表冲突信息数量不一致",
                new Object[] {expectConflictList.size()},
                new Object[] {actualConflictList.size()});
        for (int i = 0; i < expectConflictList.size(); i++) {
            PlanProcessValidateTimetableService.ConflictStatInfo expectConflictInfo = expectConflictList.get(i);
            PlanProcessValidateTimetableService.ConflictStatInfo actualConflictInfo = actualConflictList.get(i);

            // 日期描述
            Assert.assertEquals("日期描述不一致",
                    expectConflictInfo.getDateDesc(), actualConflictInfo.getDateDesc());
            // 时刻表Id信息
            Assert.assertEquals(expectConflictInfo.getDateDesc() + "时刻表Id列表信息不一致",
                    expectConflictInfo.getTtIdList(), actualConflictInfo.getTtIdList());
            // 时刻表名字信息
            Assert.assertEquals(expectConflictInfo.getDateDesc() + "时刻表名称列表信息不一致",
                    expectConflictInfo.getTtNameList(), actualConflictInfo.getTtNameList());
        }

        //-------------- 测试3 --------------//
        PlanProcessValidateTimetableService.EmptyStatInfo expectEmptyInfo1 = new PlanProcessValidateTimetableService.EmptyStatInfo();
        expectEmptyInfo1.setDateDesc("2019年01月01日");
        PlanProcessValidateTimetableService.EmptyStatInfo expectEmptyInfo2 = new PlanProcessValidateTimetableService.EmptyStatInfo();
        expectEmptyInfo2.setDateDesc("2019年01月08日");

        // 注意,排序是为了测试需要
        List<PlanProcessValidateTimetableService.EmptyStatInfo> expectEmptyInfoList = new ArrayList<>();
        expectEmptyInfoList.add(expectEmptyInfo1);
        expectEmptyInfoList.add(expectEmptyInfo2);
        Collections.sort(expectEmptyInfoList, new Comparator<PlanProcessValidateTimetableService.EmptyStatInfo>() {
            @Override
            public int compare(PlanProcessValidateTimetableService.EmptyStatInfo o1, PlanProcessValidateTimetableService.EmptyStatInfo o2) {
                return o1.getDateDesc().compareTo(o2.getDateDesc());
            }
        });

        List<PlanProcessValidateTimetableService.EmptyStatInfo> actualEmptyInfoList = result.getEmptyStatInfos();
        Collections.sort(actualEmptyInfoList, new Comparator<PlanProcessValidateTimetableService.EmptyStatInfo>() {
            @Override
            public int compare(PlanProcessValidateTimetableService.EmptyStatInfo o1, PlanProcessValidateTimetableService.EmptyStatInfo o2) {
                return o1.getDateDesc().compareTo(o2.getDateDesc());
            }
        });

        Assert.assertEquals("空信息数量不一致", expectEmptyInfoList.size(), actualEmptyInfoList.size());
        for (int i = 0; i < expectEmptyInfoList.size(); i++) {
            PlanProcessValidateTimetableService.EmptyStatInfo expectEmptyInfo = expectEmptyInfoList.get(i);
            PlanProcessValidateTimetableService.EmptyStatInfo actualEmptyInfo = actualEmptyInfoList.get(i);
            Assert.assertEquals("日期描述不一致",
                    expectEmptyInfo.getDateDesc(), actualEmptyInfo.getDateDesc());
        }
    }

    @Test
    public void _1_validate_rule_test_case1() {
        LOG.info("---------------规则验证测试--------------");

        // 使用builder创建服务
        PlanProcessValidateRuleService planProcessValidateRuleService =
                PlanProcessValidateRuleServiceDroolsImpl.getBuilder()
                        .setkBaseValidateRule(kBaseValidateRule)
                        .setJdbcTemplate(jdbcTemplate)
                        .build();

        PlanProcessValidateRuleService.Result validateRuleResult = planProcessValidateRuleService.validateRule(
                1,
                DateTimeFormat.forPattern("yyyy-MM-dd").parseDateTime("2019-01-01").toDate(),
                DateTimeFormat.forPattern("yyyy-MM-dd").parseDateTime("2019-01-14").toDate()
        );

        Assert.assertArrayEquals(
                "验证结果不一致!",
                new Object[] {
                        1,
                        3,
                        3,
                        0,
                        0
                },
                new Object[] {
                        validateRuleResult.getXlId(),
                        validateRuleResult.getCount(),
                        validateRuleResult.getQyCount(),
                        validateRuleResult.getQyErrorCount(),
                        validateRuleResult.getErrorInfos().size()
                });
    }

    // TODO:有空再把排班的逻辑移植过来
}