Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added unit tests for PipelineJobCenter.java #28887

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions kernel/data-pipeline/core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,12 @@
<version>${project.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.7.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package org.apache.shardingsphere.data.pipeline.core.job;

import org.apache.shardingsphere.data.pipeline.api.context.PipelineJobItemContext;
import org.apache.shardingsphere.data.pipeline.api.job.PipelineJob;
import org.apache.shardingsphere.data.pipeline.api.task.PipelineTasksRunner;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import java.util.Collection;
import java.util.Collections;
import java.util.Optional;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

class PipelineJobCenterTest {

private static final String JOB_ID = "testJobId";
private static final String NOT_JOB_ID = "nontestJobId";
private static final int SHARDING_ITEM = 0;
private static final int NOT_SHARDING_ITEM = 1;

private PipelineJob mockJob;
private PipelineTasksRunner mockTasksRunner;
private PipelineJobItemContext mockJobItemContext;

@BeforeEach
void setUp() {
mockJob = mock(PipelineJob.class);
mockTasksRunner = mock(PipelineTasksRunner.class);
mockJobItemContext = mock(PipelineJobItemContext.class);

when(mockJob.getTasksRunner(SHARDING_ITEM)).thenReturn(Optional.of(mockTasksRunner));
when(mockTasksRunner.getJobItemContext()).thenReturn(mockJobItemContext);
when(mockJob.getShardingItems()).thenReturn(Collections.singleton(SHARDING_ITEM));

PipelineJobCenter.addJob(JOB_ID, mockJob);
}

@AfterEach
void tearDown() {
PipelineJobCenter.stop(JOB_ID);
}

@Test
void testAddJobAndIsJobExisting() {
assertTrue(PipelineJobCenter.isJobExisting(JOB_ID));
}

@Test
void testIsJobNonExisting() {
assertFalse(PipelineJobCenter.isJobExisting(NOT_JOB_ID));
}

@Test
void testStop() {
// Already stopped by AfterEach
// PipelineJobCenter.stop(JOB_ID);
assertFalse(PipelineJobCenter.isJobExisting(JOB_ID));
verify(mockJob).stop();
}

@Test
void testStopWithNonExistingJob() {
PipelineJobCenter.stop(NOT_JOB_ID);
assertFalse(PipelineJobCenter.isJobExisting(NOT_JOB_ID));
}

@Test
void testGetJobItemContext() {
Optional<PipelineJobItemContext> jobItemContext = PipelineJobCenter.getJobItemContext(JOB_ID, SHARDING_ITEM);
assertTrue(jobItemContext.isPresent());
assertEquals(mockJobItemContext, jobItemContext.get());
}

@Test
void testGetJobItemContextWithNonExistingJob() {
Optional<PipelineJobItemContext> jobItemContext = PipelineJobCenter.getJobItemContext(NOT_JOB_ID, SHARDING_ITEM);
assertFalse(jobItemContext.isPresent());
}

@Test
void testGetJobItemContextWithNonExistingShardingItem() {
Optional<PipelineJobItemContext> jobItemContext = PipelineJobCenter.getJobItemContext(JOB_ID, NOT_SHARDING_ITEM);
assertFalse(jobItemContext.isPresent());
}

@Test
void testGetShardingItems() {
Collection<Integer> shardingItems = PipelineJobCenter.getShardingItems(JOB_ID);
assertNotNull(shardingItems);
assertEquals(NOT_SHARDING_ITEM, shardingItems.size());
assertTrue(shardingItems.contains(SHARDING_ITEM));
}

@Test
void testGetShardingItemsWithNonExistingJob() {
Collection<Integer> shardingItems = PipelineJobCenter.getShardingItems(NOT_JOB_ID);
assertNotNull(shardingItems);
assertTrue(shardingItems.isEmpty());
}
}
Loading