CommandMapperTest.java 8.1 KB
Newer Older
L
ligang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
17

Q
qiaozhanwei 已提交
18
package org.apache.dolphinscheduler.dao.mapper;
L
ligang 已提交
19

20 21 22 23 24 25 26
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;

L
lgcareer 已提交
27
import org.apache.dolphinscheduler.common.Constants;
28 29 30 31 32 33 34
import org.apache.dolphinscheduler.common.enums.CommandType;
import org.apache.dolphinscheduler.common.enums.FailureStrategy;
import org.apache.dolphinscheduler.common.enums.Flag;
import org.apache.dolphinscheduler.common.enums.Priority;
import org.apache.dolphinscheduler.common.enums.ReleaseState;
import org.apache.dolphinscheduler.common.enums.TaskDependType;
import org.apache.dolphinscheduler.common.enums.WarningType;
35
import org.apache.dolphinscheduler.common.utils.DateUtils;
Q
qiaozhanwei 已提交
36 37 38
import org.apache.dolphinscheduler.dao.entity.Command;
import org.apache.dolphinscheduler.dao.entity.CommandCount;
import org.apache.dolphinscheduler.dao.entity.ProcessDefinition;
39 40 41 42 43 44

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

L
ligang 已提交
45
import org.junit.Test;
B
bao liang 已提交
46 47 48
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
49
import org.springframework.test.annotation.Rollback;
B
bao liang 已提交
50
import org.springframework.test.context.junit4.SpringRunner;
51
import org.springframework.transaction.annotation.Transactional;
L
ligang 已提交
52

53 54 55
/**
 *  command mapper test
 */
B
bao liang 已提交
56 57
@RunWith(SpringRunner.class)
@SpringBootTest
58 59
@Transactional
@Rollback(true)
L
ligang 已提交
60 61
public class CommandMapperTest {

B
bao liang 已提交
62 63
    @Autowired
    CommandMapper commandMapper;
L
ligang 已提交
64

B
bao liang 已提交
65 66
    @Autowired
    ProcessDefinitionMapper processDefinitionMapper;
L
ligang 已提交
67

68
    /**
69
     * test insert
70
     */
71
    @Test
72
    public void testInsert() {
73 74
        Command command = createCommand();
        assertThat(command.getId(),greaterThan(0));
B
bao liang 已提交
75
    }
L
ligang 已提交
76

77
    /**
78
     * test select by id
79
     */
B
bao liang 已提交
80
    @Test
81 82 83 84 85
    public void testSelectById() {
        Command expectedCommand = createCommand();
        //query
        Command actualCommand = commandMapper.selectById(expectedCommand.getId());

86
        assertNotNull(actualCommand);
87
        assertEquals(expectedCommand.getProcessDefinitionCode(), actualCommand.getProcessDefinitionCode());
B
bao liang 已提交
88 89
    }

90
    /**
91
     * test update
92
     */
B
bao liang 已提交
93
    @Test
94
    public void testUpdate() {
95 96 97 98 99 100 101 102 103 104

        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());

105 106
        assertNotNull(actualCommand);
        assertEquals(expectedCommand.getUpdateTime(),actualCommand.getUpdateTime());
L
ligang 已提交
107 108 109

    }

110
    /**
111
     * test delete
112
     */
L
ligang 已提交
113
    @Test
114
    public void testDelete() {
115 116 117 118 119 120 121
        Command expectedCommand = createCommand();

        commandMapper.deleteById(expectedCommand.getId());

        Command actualCommand = commandMapper.selectById(expectedCommand.getId());

        assertNull(actualCommand);
B
bao liang 已提交
122
    }
123

124 125


126 127 128
    /**
     * test query all
     */
B
bao liang 已提交
129 130
    @Test
    public void testGetAll() {
131 132 133 134 135 136 137
        Integer count = 10;

        Map<Integer, Command> commandMap = createCommandMap(count);

        List<Command> actualCommands = commandMapper.selectList(null);

        assertThat(actualCommands.size(), greaterThanOrEqualTo(count));
B
bao liang 已提交
138 139
    }

140 141 142
    /**
     * test get on command to run
     */
B
bao liang 已提交
143 144 145
    @Test
    public void testGetOneToRun() {

146 147
        ProcessDefinition processDefinition = createProcessDefinition();

148
        createCommand(CommandType.START_PROCESS, processDefinition.getCode());
B
bao liang 已提交
149

150 151
        Command actualCommand = commandMapper.getOneToRun();

Q
qiaozhanwei 已提交
152
        assertNotNull(actualCommand);
L
ligang 已提交
153 154
    }

155 156 157
    /**
     * test count command state
     */
B
bao liang 已提交
158 159
    @Test
    public void testCountCommandState() {
160 161 162 163
        Integer count = 10;

        ProcessDefinition processDefinition = createProcessDefinition();

164
        createCommandMap(count, CommandType.START_PROCESS, processDefinition.getCode());
165

166
        Long[] projectCodeArray = {processDefinition.getProjectCode()};
167 168 169 170 171

        Date startTime = DateUtils.stringToDate("2019-12-29 00:10:00");

        Date endTime = DateUtils.stringToDate("2019-12-29 23:59:59");

172
        List<CommandCount> actualCommandCounts = commandMapper.countCommandState(0, startTime, endTime, projectCodeArray);
173

Q
qiaozhanwei 已提交
174
        assertThat(actualCommandCounts.size(),greaterThanOrEqualTo(1));
175
    }
B
bao liang 已提交
176

177 178 179 180
    /**
     * create command map
     * @param count map count
     * @param commandType comman type
181
     * @param processDefinitionCode process definition code
182 183 184 185 186
     * @return command map
     */
    private CommandCount createCommandMap(
            Integer count,
            CommandType commandType,
187
            long processDefinitionCode) {
188 189 190

        CommandCount commandCount = new CommandCount();

191 192
        for (int i = 0;i < count;i++) {
            createCommand(commandType, processDefinitionCode);
193 194 195 196 197 198 199 200 201 202 203
        }
        commandCount.setCommandType(commandType);
        commandCount.setCount(count);

        return commandCount;
    }

    /**
     *  create process definition
     * @return process definition
     */
204
    private ProcessDefinition createProcessDefinition() {
B
bao liang 已提交
205
        ProcessDefinition processDefinition = new ProcessDefinition();
206
        processDefinition.setCode(1L);
207 208
        processDefinition.setReleaseState(ReleaseState.ONLINE);
        processDefinition.setName("ut test");
209
        processDefinition.setProjectCode(1L);
210
        processDefinition.setFlag(Flag.YES);
211 212
        processDefinition.setCreateTime(new Date());
        processDefinition.setUpdateTime(new Date());
213

B
bao liang 已提交
214 215
        processDefinitionMapper.insert(processDefinition);

216 217
        return processDefinition;
    }
B
bao liang 已提交
218

219 220 221 222 223
    /**
     * create command map
     * @param count map count
     * @return command map
     */
224
    private Map<Integer,Command> createCommandMap(Integer count) {
225 226
        Map<Integer,Command> commandMap = new HashMap<>();

227
        for (int i = 0;i < count;i++) {
228 229 230 231 232
            Command command = createCommand();
            commandMap.put(command.getId(),command);
        }
        return commandMap;
    }
L
ligang 已提交
233

234 235 236 237
    /**
     * create command
     * @return
     */
238
    private Command createCommand() {
239 240 241 242 243 244 245
        return createCommand(CommandType.START_PROCESS,1);
    }

    /**
     * create command
     * @return Command
     */
246
    private Command createCommand(CommandType commandType, long processDefinitionCode) {
247 248 249

        Command command = new Command();
        command.setCommandType(commandType);
250
        command.setProcessDefinitionCode(processDefinitionCode);
251 252 253 254 255 256 257 258 259 260
        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"));
L
lgcareer 已提交
261
        command.setWorkerGroup(Constants.DEFAULT_WORKER_GROUP);
262
        commandMapper.insert(command);
B
bao liang 已提交
263

264
        return command;
B
bao liang 已提交
265
    }
266

267
}