Skip to content

Commit

Permalink
Add test cases on PipelineJobIdUtils (#33318)
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu authored Oct 19, 2024
1 parent f2ebcab commit 4dbbd23
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,6 @@ public static PipelineJobType parseJobType(final String jobId) {
return JobCodeRegistry.getJobType(jobId.substring(1, 3));
}

private static void verifyJobId(final String jobId) {
Preconditions.checkArgument(jobId.length() > 10, "Invalid job id length, job id: `%s`", jobId);
Preconditions.checkArgument('j' == jobId.charAt(0), "Invalid job id, first char: `%s`", jobId.charAt(0));
}

/**
* Parse context key.
*
Expand All @@ -92,6 +87,11 @@ public static PipelineContextKey parseContextKey(final String jobId) {
return new PipelineContextKey(databaseName, InstanceType.valueOf(instanceTypeCode));
}

private static void verifyJobId(final String jobId) {
Preconditions.checkArgument(jobId.length() > 10, "Invalid job id length, job id: `%s`", jobId);
Preconditions.checkArgument('j' == jobId.charAt(0), "Invalid job id, first char: `%s`", jobId.charAt(0));
}

/**
* Get ElasticJob configuration POJO.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,34 @@
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

class PipelineJobIdUtilsTest {

@Test
void assertParse() {
for (InstanceType each : InstanceType.values()) {
assertParse0(each);
}
void assertParseJobTypeWithInvalidJobId() {
assertThrows(IllegalArgumentException.class, () -> PipelineJobIdUtils.parseJobType("123"));
assertThrows(IllegalArgumentException.class, () -> PipelineJobIdUtils.parseJobType("12345678901234567890"));
}

private void assertParse0(final InstanceType instanceType) {
PipelineContextKey contextKey = new PipelineContextKey("sharding_db", instanceType);
String jobId = PipelineJobIdUtils.marshal(new MigrationJobId(contextKey, Collections.singletonList("t_order:ds_0.t_order_0,ds_0.t_order_1")));
@Test
void assertParseJobType() {
PipelineContextKey contextKey = new PipelineContextKey("foo_db", InstanceType.JDBC);
String jobId = PipelineJobIdUtils.marshal(new MigrationJobId(contextKey, Collections.singletonList("foo_tbl:foo_ds.foo_tbl_0,foo_ds.foo_tbl_1")));
assertThat(PipelineJobIdUtils.parseJobType(jobId), instanceOf(MigrationJobType.class));
PipelineContextKey actualContextKey = PipelineJobIdUtils.parseContextKey(jobId);
assertThat(actualContextKey.getInstanceType(), is(instanceType));
assertThat(actualContextKey.getDatabaseName(), is(instanceType == InstanceType.PROXY ? "" : "sharding_db"));
}

@Test
void assertParseContextKeyWithJDBCInstanceType() {
PipelineContextKey contextKey = new PipelineContextKey("foo_db", InstanceType.JDBC);
String jobId = PipelineJobIdUtils.marshal(new MigrationJobId(contextKey, Collections.singletonList("foo_tbl:foo_ds.foo_tbl_0,foo_ds.foo_tbl_1")));
assertThat(PipelineJobIdUtils.parseContextKey(jobId).getDatabaseName(), is("foo_db"));
}

@Test
void assertParseContextKeyWithProxyInstanceType() {
PipelineContextKey contextKey = new PipelineContextKey("foo_db", InstanceType.PROXY);
String jobId = PipelineJobIdUtils.marshal(new MigrationJobId(contextKey, Collections.singletonList("foo_tbl:foo_ds.foo_tbl_0,foo_ds.foo_tbl_1")));
assertThat(PipelineJobIdUtils.parseContextKey(jobId).getDatabaseName(), is(""));
}
}

0 comments on commit 4dbbd23

Please sign in to comment.