Skip to content

Commit

Permalink
Add test cases for lite lifecycle.
Browse files Browse the repository at this point in the history
  • Loading branch information
haocao committed Apr 6, 2017
1 parent 402046f commit d938c4e
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ public final class JobBriefInfo implements Serializable, Comparable<JobBriefInfo

private String jobName;

private String jobType;

private JobStatus status;

private String description;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ public JobBriefInfo getJobBriefInfo(final String jobName) {
return null;
}
LiteJobConfiguration liteJobConfig = LiteJobConfigurationGsonFactory.fromJson(liteJobConfigJson);
result.setJobType(liteJobConfig.getTypeConfig().getJobType().name());
result.setDescription(liteJobConfig.getTypeConfig().getCoreConfig().getDescription());
result.setCron(liteJobConfig.getTypeConfig().getCoreConfig().getCron());
result.setInstanceCount(getJobInstanceCount(jobName));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ public final class ShardingStatisticsAPIImpl implements ShardingStatisticsAPI {
@Override
public Collection<ShardingInfo> getShardingInfo(final String jobName) {
String shardingRootPath = new JobNodePath(jobName).getShardingNodePath();
if (!regCenter.isExisted(shardingRootPath)) {
return Collections.emptyList();
}
List<String> items = regCenter.getChildrenKeys(shardingRootPath);
List<ShardingInfo> result = new ArrayList<>(items.size());
for (String each : items) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.dangdang.ddframe.job.lite.lifecycle.api.JobAPIFactoryTest;
import com.dangdang.ddframe.job.lite.lifecycle.domain.ShardingStatusTest;
import com.dangdang.ddframe.job.lite.lifecycle.internal.operate.JobOperateAPIImplTest;
import com.dangdang.ddframe.job.lite.lifecycle.internal.operate.ShardingOperateAPIImplTest;
import com.dangdang.ddframe.job.lite.lifecycle.internal.reg.RegistryCenterFactoryTest;
import com.dangdang.ddframe.job.lite.lifecycle.internal.settings.JobSettingsAPIImplTest;
import com.dangdang.ddframe.job.lite.lifecycle.internal.statistics.JobStatisticsAPIImplTest;
Expand All @@ -38,6 +39,7 @@
ShardingStatusTest.class,
RegistryCenterFactoryTest.class,
JobOperateAPIImplTest.class,
ShardingOperateAPIImplTest.class,
JobStatisticsAPIImplTest.class,
ServerStatisticsAPIImplTest.class,
ShardingStatisticsAPIImplTest.class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ public void assertCreateJobOperateAPI() {
assertThat(JobAPIFactory.createJobOperateAPI(getConnectionString(), "namespace", Optional.<String>absent()), instanceOf(JobOperateAPI.class));
}

@Test
public void assertCreateServerOperateAPI() {
assertThat(JobAPIFactory.createShardingOperateAPI(getConnectionString(), "namespace", Optional.<String>absent()), instanceOf(ShardingOperateAPI.class));
}

@Test
public void assertCreateJobStatisticsAPI() {
assertThat(JobAPIFactory.createJobStatisticsAPI(getConnectionString(), "namespace", Optional.<String>absent()), instanceOf(JobStatisticsAPI.class));
Expand All @@ -45,4 +50,9 @@ public void assertCreateJobStatisticsAPI() {
public void assertCreateServerStatisticsAPI() {
assertThat(JobAPIFactory.createServerStatisticsAPI(getConnectionString(), "namespace", Optional.<String>absent()), instanceOf(ServerStatisticsAPI.class));
}

@Test
public void assertCreateShardingStatisticsAPI() {
assertThat(JobAPIFactory.createShardingStatisticsAPI(getConnectionString(), "namespace", Optional.<String>absent()), instanceOf(ShardingStatisticsAPI.class));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright 1999-2015 dangdang.com.
* <p>
* Licensed 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:https://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.
* </p>
*/

package com.dangdang.ddframe.job.lite.lifecycle.internal.operate;

import com.dangdang.ddframe.job.lite.lifecycle.api.ShardingOperateAPI;
import com.dangdang.ddframe.job.reg.base.CoordinatorRegistryCenter;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.util.Arrays;

import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public final class ShardingOperateAPIImplTest {

private ShardingOperateAPI shardingOperateAPI;

@Mock
private CoordinatorRegistryCenter regCenter;

@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
shardingOperateAPI = new ShardingOperateAPIImpl(regCenter);
}

@Test
public void assertDisableSharding() {
when(regCenter.getChildrenKeys("/test_job/servers")).thenReturn(Arrays.asList("ip1", "ip2"));
when(regCenter.getChildrenKeys("/test_job/sharding")).thenReturn(Arrays.asList("0", "1"));
shardingOperateAPI.disable("test_job", "0");
verify(regCenter).persist("/test_job/sharding/0/disabled", "");
}

@Test
public void assertEnableSharding() {
when(regCenter.getChildrenKeys("/test_job/servers")).thenReturn(Arrays.asList("ip1", "ip2"));
when(regCenter.getChildrenKeys("/test_job/sharding")).thenReturn(Arrays.asList("0", "1"));
shardingOperateAPI.enable("test_job", "0");
verify(regCenter).remove("/test_job/sharding/0/disabled");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import com.dangdang.ddframe.job.lite.lifecycle.api.JobStatisticsAPI;
import com.dangdang.ddframe.job.lite.lifecycle.domain.JobBriefInfo;
import com.dangdang.ddframe.job.lite.lifecycle.domain.JobBriefInfo.JobStatus;
import com.dangdang.ddframe.job.lite.lifecycle.fixture.LifecycleJsonConstants;
import com.dangdang.ddframe.job.reg.base.CoordinatorRegistryCenter;
import com.google.common.collect.Lists;
Expand Down Expand Up @@ -52,6 +53,12 @@ public void assertGetAllJobsBriefInfoWithoutNamespace() {
assertThat(jobStatisticsAPI.getAllJobsBriefInfo().size(), is(0));
}

@Test
public void assertGetJobsTotalCount() {
when(regCenter.getChildrenKeys("/")).thenReturn(Arrays.asList("test_job_1", "test_job_2"));
assertThat(jobStatisticsAPI.getJobsTotalCount(), is(2));
}

@Test
public void assertGetJobBriefInfo() {
when(regCenter.getChildrenKeys("/")).thenReturn(Lists.newArrayList("test_job"));
Expand All @@ -68,7 +75,6 @@ public void assertGetJobBriefInfo() {
assertThat(jobBrief.getJobName(), is("test_job"));
assertThat(jobBrief.getDescription(), is("desc"));
assertThat(jobBrief.getCron(), is("0/1 * * * * ?"));
assertThat(jobBrief.getJobType(), is("SIMPLE"));
assertThat(jobBrief.getInstanceCount(), is(2));
assertThat(jobBrief.getShardingTotalCount(), is(3));
}
Expand All @@ -80,8 +86,6 @@ public void assertGetAllJobsBriefInfo() {
when(regCenter.get("/test_job_2/config")).thenReturn(LifecycleJsonConstants.getSimpleJobJson("test_job_2", "desc2"));
when(regCenter.getChildrenKeys("/test_job_1/servers")).thenReturn(Arrays.asList("ip1", "ip2"));
when(regCenter.getChildrenKeys("/test_job_2/servers")).thenReturn(Arrays.asList("ip3", "ip4"));
when(regCenter.getChildrenKeys("/test_job_1/sharding")).thenReturn(Arrays.asList("0"));
when(regCenter.getChildrenKeys("/test_job_2/sharding")).thenReturn(Arrays.asList("0", "1"));
when(regCenter.getChildrenKeys("/test_job_1/instances")).thenReturn(Arrays.asList("ip1@-@defaultInstance"));
when(regCenter.getChildrenKeys("/test_job_2/instances")).thenReturn(Arrays.asList("ip1@-@defaultInstance", "ip2@-@defaultInstance"));
int i = 0;
Expand All @@ -90,10 +94,30 @@ public void assertGetAllJobsBriefInfo() {
assertThat(each.getJobName(), is("test_job_" + i));
assertThat(each.getDescription(), is("desc" + i));
assertThat(each.getCron(), is("0/1 * * * * ?"));
assertThat(each.getJobType(), is("SIMPLE"));
assertThat(each.getInstanceCount(), is(i));
assertThat(each.getShardingTotalCount(), is(3));
assertThat(each.getStatus(), is(JobStatus.OK));
}
}

@Test
public void getJobsBriefInfoByIp() {
when(regCenter.getChildrenKeys("/")).thenReturn(Arrays.asList("test_job_1", "test_job_2"));
when(regCenter.get("/test_job_1/config")).thenReturn(LifecycleJsonConstants.getSimpleJobJson("test_job_1", "desc1"));
when(regCenter.get("/test_job_2/config")).thenReturn(LifecycleJsonConstants.getSimpleJobJson("test_job_2", "desc2"));
when(regCenter.getChildrenKeys("/test_job_1/servers")).thenReturn(Arrays.asList("ip1"));
when(regCenter.isExisted("/test_job_1/servers/ip1")).thenReturn(true);
when(regCenter.getChildrenKeys("/test_job_1/instances")).thenReturn(Arrays.asList("ip1@-@defaultInstance"));
int i = 0;
for (JobBriefInfo each : jobStatisticsAPI.getJobsBriefInfo("ip1")) {
assertThat(each.getJobName(), is("test_job_" + ++i));
if (i == 1) {
assertThat(each.getStatus(), is(JobStatus.OK));
assertThat(each.getInstanceCount(), is(1));
} else {
assertThat(each.getInstanceCount(), is(0));
assertThat(each.getStatus(), is(JobStatus.CRASHED));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ public void setUp() {
serverStatisticsAPI = new ServerStatisticsAPIImpl(regCenter);
}

@Test
public void assertGetJobsTotalCount() {
when(regCenter.getChildrenKeys("/")).thenReturn(Arrays.asList("test_job_1", "test_job_2"));
when(regCenter.getChildrenKeys("/test_job_1/servers")).thenReturn(Arrays.asList("ip1", "ip2"));
when(regCenter.getChildrenKeys("/test_job_2/servers")).thenReturn(Arrays.asList("ip2", "ip3"));
assertThat(serverStatisticsAPI.getServersTotalCount(), is(3));
}

@Test
public void assertGetAllServersBriefInfo() {
when(regCenter.getChildrenKeys("/")).thenReturn(Arrays.asList("test_job1", "test_job2"));
Expand Down

0 comments on commit d938c4e

Please sign in to comment.