From e61586cd0665f94618d0d0489052dc87a1aea61c Mon Sep 17 00:00:00 2001 From: qiaozhanwei Date: Sun, 29 Dec 2019 13:12:29 +0800 Subject: [PATCH] CommandMapperTest UT modify #1465 (#1625) * CommandMapperTest UT modify --- .../dolphinscheduler/dao/entity/Command.java | 74 ++++- .../dao/entity/CommandCount.java | 40 ++- .../dao/mapper/AlertMapperTest.java | 3 + .../dao/mapper/CommandMapperTest.java | 264 ++++++++++++------ pom.xml | 1 + 5 files changed, 292 insertions(+), 90 deletions(-) diff --git a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/entity/Command.java b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/entity/Command.java index a180a433bc..2d47b7874a 100644 --- a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/entity/Command.java +++ b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/entity/Command.java @@ -28,7 +28,6 @@ import java.util.Date; /** * command */ -@Data @TableName("t_ds_command") public class Command { @@ -265,6 +264,79 @@ public class Command { this.workerGroupId = workerGroupId; } + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + + Command command = (Command) o; + + if (id != command.id) { + return false; + } + if (processDefinitionId != command.processDefinitionId) { + return false; + } + if (executorId != command.executorId) { + return false; + } + if (workerGroupId != command.workerGroupId) { + return false; + } + if (commandType != command.commandType) { + return false; + } + if (commandParam != null ? !commandParam.equals(command.commandParam) : command.commandParam != null) { + return false; + } + if (taskDependType != command.taskDependType) { + return false; + } + if (failureStrategy != command.failureStrategy) { + return false; + } + if (warningType != command.warningType) { + return false; + } + if (warningGroupId != null ? !warningGroupId.equals(command.warningGroupId) : command.warningGroupId != null) { + return false; + } + if (scheduleTime != null ? !scheduleTime.equals(command.scheduleTime) : command.scheduleTime != null) { + return false; + } + if (startTime != null ? !startTime.equals(command.startTime) : command.startTime != null) { + return false; + } + if (processInstancePriority != command.processInstancePriority) { + return false; + } + return !(updateTime != null ? !updateTime.equals(command.updateTime) : command.updateTime != null); + + } + + @Override + public int hashCode() { + int result = id; + result = 31 * result + (commandType != null ? commandType.hashCode() : 0); + result = 31 * result + processDefinitionId; + result = 31 * result + executorId; + result = 31 * result + (commandParam != null ? commandParam.hashCode() : 0); + result = 31 * result + (taskDependType != null ? taskDependType.hashCode() : 0); + result = 31 * result + (failureStrategy != null ? failureStrategy.hashCode() : 0); + result = 31 * result + (warningType != null ? warningType.hashCode() : 0); + result = 31 * result + (warningGroupId != null ? warningGroupId.hashCode() : 0); + result = 31 * result + (scheduleTime != null ? scheduleTime.hashCode() : 0); + result = 31 * result + (startTime != null ? startTime.hashCode() : 0); + result = 31 * result + (processInstancePriority != null ? processInstancePriority.hashCode() : 0); + result = 31 * result + (updateTime != null ? updateTime.hashCode() : 0); + result = 31 * result + workerGroupId; + return result; + } + @Override public String toString() { return "Command{" + diff --git a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/entity/CommandCount.java b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/entity/CommandCount.java index 158169b727..df99b54dbe 100644 --- a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/entity/CommandCount.java +++ b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/entity/CommandCount.java @@ -33,13 +33,6 @@ public class CommandCount { private int count; - @Override - public String toString(){ - return "command count:" + - " commandType: "+ commandType.toString() + - " count: "+ count; - } - public CommandType getCommandType() { return commandType; } @@ -55,4 +48,37 @@ public class CommandCount { public void setCount(int count) { this.count = count; } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + + CommandCount that = (CommandCount) o; + + if (count != that.count) { + return false; + } + return commandType == that.commandType; + + } + + @Override + public int hashCode() { + int result = commandType != null ? commandType.hashCode() : 0; + result = 31 * result + count; + return result; + } + + @Override + public String toString() { + return "CommandCount{" + + "commandType=" + commandType + + ", count=" + count + + '}'; + } } diff --git a/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/AlertMapperTest.java b/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/AlertMapperTest.java index 5f10a7586d..4e298e3aa3 100644 --- a/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/AlertMapperTest.java +++ b/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/AlertMapperTest.java @@ -35,6 +35,9 @@ import java.util.*; import static org.hamcrest.Matchers.*; import static org.junit.Assert.*; +/** + * alert mapper test + */ @RunWith(SpringRunner.class) @SpringBootTest @Transactional diff --git a/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/CommandMapperTest.java b/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/CommandMapperTest.java index 2af44576b5..c35ce7e8ce 100644 --- a/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/CommandMapperTest.java +++ b/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/CommandMapperTest.java @@ -16,6 +16,7 @@ */ package org.apache.dolphinscheduler.dao.mapper; +import org.apache.dolphinscheduler.common.utils.DateUtils; import org.apache.dolphinscheduler.dao.entity.Command; import org.apache.dolphinscheduler.dao.entity.CommandCount; import org.apache.dolphinscheduler.dao.entity.ProcessDefinition; @@ -25,13 +26,25 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.annotation.Rollback; import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.transaction.annotation.Transactional; import java.util.Date; +import java.util.HashMap; import java.util.List; +import java.util.Map; +import static org.hamcrest.Matchers.*; +import static org.junit.Assert.*; + +/** + * command mapper test + */ @RunWith(SpringRunner.class) @SpringBootTest +@Transactional +@Rollback(true) public class CommandMapperTest { @@ -41,71 +54,85 @@ public class CommandMapperTest { @Autowired ProcessDefinitionMapper processDefinitionMapper; + /** - * insert - * @return Command + * test insert */ - private Command insertOne(){ - //insertOne - Command command = new Command(); - command.setCommandType(CommandType.START_PROCESS); - command.setProcessDefinitionId(1); - command.setExecutorId(4); - command.setProcessInstancePriority(Priority.MEDIUM); - command.setFailureStrategy(FailureStrategy.CONTINUE); - command.setWorkerGroupId(-1); - command.setWarningGroupId(1); - command.setUpdateTime(new Date()); - commandMapper.insert(command); - return command; + @Test + public void testInsert(){ + Command command = createCommand(); + assertNotNull(command.getId()); + assertThat(command.getId(),greaterThan(0)); } + /** - * test update + * test select by id */ @Test - public void testUpdate(){ - //insertOne - Command command = insertOne(); - //update - command.setStartTime(new Date()); - int update = commandMapper.updateById(command); - Assert.assertEquals(update, 1); - commandMapper.deleteById(command.getId()); + public void testSelectById() { + Command expectedCommand = createCommand(); + //query + Command actualCommand = commandMapper.selectById(expectedCommand.getId()); + + assertEquals(expectedCommand, actualCommand); } /** - * test delete + * test update */ @Test - public void testDelete(){ + public void testUpdate(){ + + Command expectedCommand = createCommand(); + + // update the command time if current command if recover from waiting + expectedCommand.setUpdateTime(DateUtils.getCurrentDate()); + + commandMapper.updateById(expectedCommand); + + Command actualCommand = commandMapper.selectById(expectedCommand.getId()); + + assertEquals(expectedCommand,actualCommand); - Command Command = insertOne(); - int delete = commandMapper.deleteById(Command.getId()); - Assert.assertEquals(delete, 1); } /** - * test query + * test delete */ @Test - public void testQuery() { - Command command = insertOne(); - //query - List commands = commandMapper.selectList(null); - Assert.assertNotEquals(commands.size(), 0); - commandMapper.deleteById(command.getId()); + public void testDelete(){ + Command expectedCommand = createCommand(); + + commandMapper.deleteById(expectedCommand.getId()); + + Command actualCommand = commandMapper.selectById(expectedCommand.getId()); + + assertNull(actualCommand); } + + /** * test query all */ @Test public void testGetAll() { - Command command = insertOne(); - List commands = commandMapper.selectList(null); - Assert.assertNotEquals(commands.size(), 0); - commandMapper.deleteById(command.getId()); + Integer count = 10; + + Map commandMap = createCommandMap(count); + + + List actualCommands = commandMapper.selectList(null); + + assertThat(actualCommands.size(), greaterThanOrEqualTo(count)); + + for (Command actualCommand : actualCommands){ + Command expectedCommand = commandMap.get(actualCommand.getId()); + if (expectedCommand != null){ + assertEquals(expectedCommand,actualCommand); + } + } } /** @@ -113,28 +140,14 @@ public class CommandMapperTest { */ @Test public void testGetOneToRun() { - ProcessDefinition processDefinition = new ProcessDefinition(); - processDefinition.setReleaseState(ReleaseState.ONLINE); - processDefinition.setName("ut test"); - processDefinition.setProjectId(1); - processDefinition.setFlag(Flag.YES); - processDefinitionMapper.insert(processDefinition); - Command command = new Command(); - command.setCommandType(CommandType.START_PROCESS); - command.setProcessDefinitionId(processDefinition.getId()); - command.setExecutorId(4); - command.setProcessInstancePriority(Priority.MEDIUM); - command.setFailureStrategy(FailureStrategy.CONTINUE); - command.setWorkerGroupId(-1); - command.setWarningGroupId(1); - command.setUpdateTime(new Date()); - commandMapper.insert(command); + ProcessDefinition processDefinition = createProcessDefinition(); + + Command expectedCommand = createCommand(CommandType.START_PROCESS,processDefinition.getId()); - Command command2 = commandMapper.getOneToRun(); - Assert.assertNotEquals(command2, null); - commandMapper.deleteById(command.getId()); - processDefinitionMapper.deleteById(processDefinition.getId()); + Command actualCommand = commandMapper.getOneToRun(); + + assertEquals(expectedCommand, actualCommand); } /** @@ -142,35 +155,122 @@ public class CommandMapperTest { */ @Test public void testCountCommandState() { - Command command = insertOne(); + Integer count = 10; + + ProcessDefinition processDefinition = createProcessDefinition(); + + CommandCount expectedCommandCount = createCommandMap(count, CommandType.START_PROCESS, processDefinition.getId()); + + Integer[] projectIdArray = {processDefinition.getProjectId()}; + + Date startTime = DateUtils.stringToDate("2019-12-29 00:10:00"); + + Date endTime = DateUtils.stringToDate("2019-12-29 23:59:59"); + + List actualCommandCounts = commandMapper.countCommandState(0, startTime, endTime, projectIdArray); + + assertThat(actualCommandCounts.size(),greaterThanOrEqualTo(1)); + + Boolean flag = false; + for (CommandCount actualCommandCount : actualCommandCounts){ + if (actualCommandCount.getCommandType().equals(expectedCommandCount.getCommandType())){ + assertEquals(expectedCommandCount,actualCommandCount); + flag = true; + } + } + + assertTrue(flag); + } - //insertOne + + /** + * create command map + * @param count map count + * @param commandType comman type + * @param processDefinitionId process definition id + * @return command map + */ + private CommandCount createCommandMap( + Integer count, + CommandType commandType, + Integer processDefinitionId){ + + CommandCount commandCount = new CommandCount(); + + for (int i = 0 ;i < count ;i++){ + createCommand(commandType,processDefinitionId); + } + commandCount.setCommandType(commandType); + commandCount.setCount(count); + + return commandCount; + } + + /** + * create process definition + * @return process definition + */ + private ProcessDefinition createProcessDefinition(){ ProcessDefinition processDefinition = new ProcessDefinition(); - processDefinition.setName("def 1"); - processDefinition.setProjectId(1010); - processDefinition.setUserId(101); - processDefinition.setUpdateTime(new Date()); - processDefinition.setCreateTime(new Date()); + processDefinition.setReleaseState(ReleaseState.ONLINE); + processDefinition.setName("ut test"); + processDefinition.setProjectId(1); + processDefinition.setFlag(Flag.YES); + processDefinitionMapper.insert(processDefinition); - command.setProcessDefinitionId(processDefinition.getId()); - commandMapper.updateById(command); + return processDefinition; + } + /** + * create command map + * @param count map count + * @return command map + */ + private Map createCommandMap(Integer count){ + Map commandMap = new HashMap<>(); + + for (int i = 0; i < count ;i++){ + Command command = createCommand(); + commandMap.put(command.getId(),command); + } + return commandMap; + } - List commandCounts = commandMapper.countCommandState( - 4, null, null, new Integer[0] - ); - Integer[] projectIdArray = new Integer[2]; - projectIdArray[0] = processDefinition.getProjectId(); - projectIdArray[1] = 200; - List commandCounts2 = commandMapper.countCommandState( - 4, null, null, projectIdArray - ); + /** + * create command + * @return + */ + private Command createCommand(){ + return createCommand(CommandType.START_PROCESS,1); + } + + /** + * create command + * @return Command + */ + private Command createCommand(CommandType commandType,Integer processDefinitionId){ + + Command command = new Command(); + command.setCommandType(commandType); + command.setProcessDefinitionId(processDefinitionId); + command.setExecutorId(4); + command.setCommandParam("test command param"); + command.setTaskDependType(TaskDependType.TASK_ONLY); + command.setFailureStrategy(FailureStrategy.CONTINUE); + command.setWarningType(WarningType.ALL); + command.setWarningGroupId(1); + command.setScheduleTime(DateUtils.stringToDate("2019-12-29 12:10:00")); + command.setProcessInstancePriority(Priority.MEDIUM); + command.setStartTime(DateUtils.stringToDate("2019-12-29 10:10:00")); + command.setUpdateTime(DateUtils.stringToDate("2019-12-29 10:10:00")); + command.setWorkerGroupId(-1); + commandMapper.insert(command); - commandMapper.deleteById(command.getId()); - processDefinitionMapper.deleteById(processDefinition.getId()); - Assert.assertNotEquals(commandCounts.size(), 0); - Assert.assertNotEquals(commandCounts2.size(), 0); + return command; } + + + } \ No newline at end of file diff --git a/pom.xml b/pom.xml index 796d2b8228..7936bf7dc8 100644 --- a/pom.xml +++ b/pom.xml @@ -675,6 +675,7 @@ **/dao/mapper/AccessTokenMapperTest.java **/dao/mapper/AlertGroupMapperTest.java **/dao/mapper/AlertMapperTest.java + **/dao/mapper/CommandMapperTest.java