Skip to content

Commit

Permalink
#452 修改根据条件获取作业个数的sql,增加作业总览相关的方法ut
Browse files Browse the repository at this point in the history
  • Loading branch information
leewcc committed Jul 20, 2018
1 parent c6b1fdd commit 52cf152
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,20 +98,20 @@
from job_config
<where>
namespace = #{namespace,jdbcType=VARCHAR}
<choose>
<when test="condition.type == 1">
and job_name like CONCAT('%',#{condition.key},'%')
</when>
<when test="condition.type == 2">
and description like CONCAT('%',#{condition.key},'%')
</when>
</choose>
<if test="condition.groups != null">
and groups = #{condition.groups}
</if>
<if test="condition.isEnabled != null">
and is_enabled = #{condition.isEnabled}
</if>
<choose>
<when test="condition.jobName != null">
and job_name like CONCAT('%',#{condition.jobName},'%')
</when>
<when test="condition.description != null">
and description like CONCAT('%',#{condition.description},'%')
</when>
</choose>
</where>
</select>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,30 @@

import com.vip.saturn.job.console.domain.ExecutionInfo;
import com.vip.saturn.job.console.domain.ExecutionInfo.ExecutionStatus;
import com.vip.saturn.job.console.domain.JobStatus;
import com.vip.saturn.job.console.domain.JobType;
import com.vip.saturn.job.console.exception.SaturnJobConsoleException;
import com.vip.saturn.job.console.mybatis.entity.JobConfig4DB;
import com.vip.saturn.job.console.mybatis.service.CurrentJobConfigService;
import com.vip.saturn.job.console.repository.zookeeper.CuratorRepository.CuratorFrameworkOp;
import com.vip.saturn.job.console.service.RegistryCenterService;
import com.vip.saturn.job.console.utils.JobNodePath;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.assertj.core.util.Lists;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Matchers;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import java.util.List;
import org.springframework.data.domain.Pageable;

import static org.junit.Assert.*;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
Expand All @@ -35,6 +43,55 @@ public class JobServiceImplTest {
@InjectMocks
private JobServiceImpl jobService;

@Test
public void testGetUnSystemJobsWithCondition() throws SaturnJobConsoleException {
String namespace = "ns1";
String jobName = "testJob";
int count = 4;
Map<String, Object> condition = buildCondition(null);
when(currentJobConfigService.findConfigsByNamespaceWithCondition(eq(namespace), eq(condition),
Matchers.<Pageable> anyObject())).thenReturn(buildJobConfig4DBList(namespace, jobName, count));
assertTrue(jobService.getUnSystemJobsWithCondition(namespace, condition, 1, 25).size() == count);
}

@Test
public void testGetUnSystemJobWithConditionAndStatus() throws SaturnJobConsoleException {
String namespace = "ns1";
String jobName = "testJob";
int count = 4;
List<JobConfig4DB> jobConfig4DBList = buildJobConfig4DBList(namespace, jobName, count);
Map<String, Object> condition = buildCondition(JobStatus.READY);
when(currentJobConfigService.findConfigsByNamespaceWithCondition(eq(namespace), eq(condition),
Matchers.<Pageable> anyObject())).thenReturn(jobConfig4DBList);
when(registryCenterService.getCuratorFrameworkOp(namespace)).thenReturn(curatorFrameworkOp);
for (int i = 0; i < count; i++) {
JobConfig4DB jobConfig4DB = jobConfig4DBList.get(i);
// 设置 index 为单数的job enabled 为 true
jobConfig4DB.setEnabled(i % 2 == 1);
when(currentJobConfigService.findConfigByNamespaceAndJobName(eq(namespace), eq(jobConfig4DB.getJobName())))
.thenReturn(jobConfig4DB);
when(curatorFrameworkOp.getChildren(JobNodePath.getExecutionNodePath(jobConfig4DB.getJobName())))
.thenReturn(null);
}
assertTrue(jobService.getUnSystemJobsWithCondition(namespace, condition, 1, 25).size() == (count / 2));
}




@Test
public void testIsJobShardingAllocatedExecutor() throws SaturnJobConsoleException {
String namespace = "ns1";
String jobName = "testJob";
String executor = "executor1";
when(registryCenterService.getCuratorFrameworkOp(namespace)).thenReturn(curatorFrameworkOp);
when(curatorFrameworkOp.getChildren(JobNodePath.getServerNodePath(jobName)))
.thenReturn(Lists.newArrayList(executor));
when(curatorFrameworkOp.getData(JobNodePath.getServerNodePath(jobName, executor, "sharding")))
.thenReturn("true");
assertTrue(jobService.isJobShardingAllocatedExecutor(namespace, jobName));
}

@Test
public void testGetExecutionStatusSuccessfully() throws Exception {
String namespace = "ns1";
Expand Down Expand Up @@ -214,4 +271,25 @@ private JobConfig4DB buildJobConfig4DB(String namespace, String jobName) {
config.setJobType(JobType.JAVA_JOB.toString());
return config;
}

private List<JobConfig4DB> buildJobConfig4DBList(String namespace, String jobName, int count) {
List<JobConfig4DB> config4DBList = new ArrayList<>();
for (int i = 0; i < count; i++) {
JobConfig4DB config = new JobConfig4DB();
config.setNamespace(namespace);
config.setJobName(jobName + i);
config.setEnabled(true);
config.setEnabledReport(true);
config.setJobType(JobType.JAVA_JOB.toString());
config4DBList.add(config);
}
return config4DBList;
}

private Map<String, Object> buildCondition(JobStatus jobStatus) {
Map<String, Object> condition = new HashMap<>();
condition.put("jobStatus", jobStatus);
return condition;
}

}

0 comments on commit 52cf152

Please sign in to comment.