From 49286c5ac881324f3ecef55bd18658b30ac4a69c Mon Sep 17 00:00:00 2001 From: HarshSawarkar GitHub Date: Fri, 27 Oct 2023 13:29:12 +0530 Subject: [PATCH 1/6] 28542-Add-unit-test-for-PipelineJobCenter --- .../core/job/PipelineJobCenterTest.java | 132 ++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/job/PipelineJobCenterTest.java diff --git a/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/job/PipelineJobCenterTest.java b/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/job/PipelineJobCenterTest.java new file mode 100644 index 0000000000000..53b3509cb0010 --- /dev/null +++ b/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/job/PipelineJobCenterTest.java @@ -0,0 +1,132 @@ +package org.apache.shardingsphere.data.pipeline.core.job; + +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; + +import org.apache.shardingsphere.data.pipeline.common.context.PipelineJobItemContext; +import org.apache.shardingsphere.data.pipeline.common.job.PipelineJob; +import org.apache.shardingsphere.data.pipeline.core.task.runner.PipelineTasksRunner; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +class PipelineJobCenterTest { + + @Test + void addJob(){ + PipelineJob pipelineJob=new PipelineJob() { + @Override + public Optional getTasksRunner(int shardingItem) { + return Optional.empty(); + } + + @Override + public Collection getShardingItems() { + return null; + } + + @Override + public void stop() { + + } + }; + PipelineJobCenter.addJob("12",pipelineJob); + Assertions.assertEquals(pipelineJob,PipelineJobCenter.getJob("12")); + Assertions.assertTrue(PipelineJobCenter.isJobExisting("12")); + } + + @Test + void isJobExisting() { + PipelineJob pipelineJob=new PipelineJob() { + @Override + public Optional getTasksRunner(int shardingItem) { + return Optional.empty(); + } + + @Override + public Collection getShardingItems() { + return null; + } + + @Override + public void stop() { + + } + }; + PipelineJobCenter.addJob("12",pipelineJob); + Assertions.assertTrue(PipelineJobCenter.isJobExisting("12")); + + } + + @Test + void getJob() { + PipelineJob pipelineJob=new PipelineJob() { + @Override + public Optional getTasksRunner(int shardingItem) { + return Optional.empty(); + } + + @Override + public Collection getShardingItems() { + return null; + } + + @Override + public void stop() { + + } + }; + PipelineJobCenter.addJob("13",pipelineJob); + Assertions.assertEquals(pipelineJob,PipelineJobCenter.getJob("13")); + } + + @Test + void getJobItemContext() { + PipelineJob pipelineJob=mock(PipelineJob.class); + PipelineTasksRunner pipelineTasksRunner=mock(PipelineTasksRunner.class); + PipelineJobItemContext pipelineJobItemContext=mock(PipelineJobItemContext.class); + when(pipelineJob.getTasksRunner(anyInt())).thenReturn(Optional.of(pipelineTasksRunner)); + when(pipelineTasksRunner.getJobItemContext()).thenReturn(pipelineJobItemContext); + PipelineJobCenter.addJob("111",pipelineJob); + Optional result=PipelineJobCenter.getJobItemContext("111",1); + assertTrue(result.isPresent()); + assertEquals(pipelineJobItemContext,result); + } + + @Test + void getShardingItems() { + PipelineJob pipelineJob=new PipelineJob() { + @Override + public Optional getTasksRunner(int shardingItem) { + return Optional.empty(); + } + + @Override + public Collection getShardingItems() { + Collection collection=new ArrayList<>(); + collection.add(2); + collection.add(3); + return collection; + } + + @Override + public void stop() { + + } + }; + PipelineJobCenter.addJob("11",pipelineJob); + Collection testCollection=new ArrayList<>(); + testCollection.add(2); + testCollection.add(3); + Assertions.assertFalse(pipelineJob.getShardingItems().isEmpty()); + Assertions.assertEquals(testCollection,PipelineJobCenter.getShardingItems("11")); + } +} \ No newline at end of file From caa1a42f5af2dbf6a9f1a85b3a1206af8acbc6f8 Mon Sep 17 00:00:00 2001 From: HarshSawarkar GitHub Date: Fri, 27 Oct 2023 15:15:32 +0530 Subject: [PATCH 2/6] 28542-Add-unit-test-for-PipelineJobCenter --- .../core/job/PipelineJobCenterTest.java | 54 ++----------------- 1 file changed, 5 insertions(+), 49 deletions(-) diff --git a/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/job/PipelineJobCenterTest.java b/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/job/PipelineJobCenterTest.java index 53b3509cb0010..4e122f7379e1e 100644 --- a/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/job/PipelineJobCenterTest.java +++ b/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/job/PipelineJobCenterTest.java @@ -22,22 +22,7 @@ class PipelineJobCenterTest { @Test void addJob(){ - PipelineJob pipelineJob=new PipelineJob() { - @Override - public Optional getTasksRunner(int shardingItem) { - return Optional.empty(); - } - - @Override - public Collection getShardingItems() { - return null; - } - - @Override - public void stop() { - - } - }; + PipelineJob pipelineJob=mock(PipelineJob.class); PipelineJobCenter.addJob("12",pipelineJob); Assertions.assertEquals(pipelineJob,PipelineJobCenter.getJob("12")); Assertions.assertTrue(PipelineJobCenter.isJobExisting("12")); @@ -45,46 +30,17 @@ public void stop() { @Test void isJobExisting() { - PipelineJob pipelineJob=new PipelineJob() { - @Override - public Optional getTasksRunner(int shardingItem) { - return Optional.empty(); - } - - @Override - public Collection getShardingItems() { - return null; - } - - @Override - public void stop() { - - } - }; + PipelineJob pipelineJob=mock(PipelineJob.class); PipelineJobCenter.addJob("12",pipelineJob); Assertions.assertTrue(PipelineJobCenter.isJobExisting("12")); - + assertNotNull(PipelineJobCenter.getJob("12")); } @Test void getJob() { - PipelineJob pipelineJob=new PipelineJob() { - @Override - public Optional getTasksRunner(int shardingItem) { - return Optional.empty(); - } - - @Override - public Collection getShardingItems() { - return null; - } - - @Override - public void stop() { - - } - }; + PipelineJob pipelineJob=mock(PipelineJob.class); PipelineJobCenter.addJob("13",pipelineJob); + assertTrue(PipelineJobCenter.isJobExisting("13")); Assertions.assertEquals(pipelineJob,PipelineJobCenter.getJob("13")); } From a2877a740fcf516acf39f91701f9367d31f69082 Mon Sep 17 00:00:00 2001 From: HarshSawarkar GitHub Date: Mon, 30 Oct 2023 17:14:17 +0530 Subject: [PATCH 3/6] Made all the changes as mentioned in review comments. --- .../core/job/PipelineJobCenterTest.java | 117 +++++++++--------- 1 file changed, 61 insertions(+), 56 deletions(-) diff --git a/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/job/PipelineJobCenterTest.java b/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/job/PipelineJobCenterTest.java index 4e122f7379e1e..5c4494571fefd 100644 --- a/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/job/PipelineJobCenterTest.java +++ b/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/job/PipelineJobCenterTest.java @@ -1,17 +1,39 @@ +/* + * 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. + */ + package org.apache.shardingsphere.data.pipeline.core.job; import java.sql.SQLException; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; +import java.util.Collections; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.Optional; import org.apache.shardingsphere.data.pipeline.common.context.PipelineJobItemContext; import org.apache.shardingsphere.data.pipeline.common.job.PipelineJob; import org.apache.shardingsphere.data.pipeline.core.task.runner.PipelineTasksRunner; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import org.locationtech.jts.util.Assert; import static org.junit.jupiter.api.Assertions.*; import static org.mockito.ArgumentMatchers.anyInt; @@ -19,70 +41,53 @@ import static org.mockito.Mockito.when; class PipelineJobCenterTest { - + @Test - void addJob(){ - PipelineJob pipelineJob=mock(PipelineJob.class); - PipelineJobCenter.addJob("12",pipelineJob); - Assertions.assertEquals(pipelineJob,PipelineJobCenter.getJob("12")); - Assertions.assertTrue(PipelineJobCenter.isJobExisting("12")); + void testPipelineJobCenter() { + PipelineJob pipelineJob = mock(PipelineJob.class); + PipelineJobCenter.addJob("Job1", pipelineJob); + + assertTrue(PipelineJobCenter.isJobExisting("Job1")); + assertFalse(PipelineJobCenter.isJobExisting("Job2")); + assertNotNull(PipelineJobCenter.getJob("Job1")); + assertEquals(pipelineJob, PipelineJobCenter.getJob("Job1")); + assertNull(PipelineJobCenter.getJob("Job2")); + + PipelineJob pipelineJob1 = mock(PipelineJob.class); + PipelineJobCenter.addJob("Job3", pipelineJob1); + assertNotNull(PipelineJobCenter.getJob("Job3")); + assertEquals(pipelineJob1, PipelineJobCenter.getJob("Job3")); + PipelineJobCenter.stop("Job3"); } - - @Test - void isJobExisting() { - PipelineJob pipelineJob=mock(PipelineJob.class); - PipelineJobCenter.addJob("12",pipelineJob); - Assertions.assertTrue(PipelineJobCenter.isJobExisting("12")); - assertNotNull(PipelineJobCenter.getJob("12")); - } - - @Test - void getJob() { - PipelineJob pipelineJob=mock(PipelineJob.class); - PipelineJobCenter.addJob("13",pipelineJob); - assertTrue(PipelineJobCenter.isJobExisting("13")); - Assertions.assertEquals(pipelineJob,PipelineJobCenter.getJob("13")); - } - + @Test void getJobItemContext() { - PipelineJob pipelineJob=mock(PipelineJob.class); - PipelineTasksRunner pipelineTasksRunner=mock(PipelineTasksRunner.class); - PipelineJobItemContext pipelineJobItemContext=mock(PipelineJobItemContext.class); + PipelineJob pipelineJob = mock(PipelineJob.class); + PipelineTasksRunner pipelineTasksRunner = mock(PipelineTasksRunner.class); + PipelineJobItemContext pipelineJobItemContext = mock(PipelineJobItemContext.class); + Optional pipelineJobItemContext1 = Optional.ofNullable(pipelineJobItemContext); when(pipelineJob.getTasksRunner(anyInt())).thenReturn(Optional.of(pipelineTasksRunner)); when(pipelineTasksRunner.getJobItemContext()).thenReturn(pipelineJobItemContext); - PipelineJobCenter.addJob("111",pipelineJob); - Optional result=PipelineJobCenter.getJobItemContext("111",1); + PipelineJobCenter.addJob("Job1", pipelineJob); + Optional result = PipelineJobCenter.getJobItemContext("Job1", 1); + Optional result1 = PipelineJobCenter.getJobItemContext("Job2", 1); assertTrue(result.isPresent()); - assertEquals(pipelineJobItemContext,result); + assertEquals(pipelineJobItemContext1, result); + assertFalse(result1.isPresent()); + assertNotNull(result1); + PipelineJobCenter.stop("Job1"); } - + @Test void getShardingItems() { - PipelineJob pipelineJob=new PipelineJob() { - @Override - public Optional getTasksRunner(int shardingItem) { - return Optional.empty(); - } - - @Override - public Collection getShardingItems() { - Collection collection=new ArrayList<>(); - collection.add(2); - collection.add(3); - return collection; - } - - @Override - public void stop() { - - } - }; - PipelineJobCenter.addJob("11",pipelineJob); - Collection testCollection=new ArrayList<>(); - testCollection.add(2); - testCollection.add(3); - Assertions.assertFalse(pipelineJob.getShardingItems().isEmpty()); - Assertions.assertEquals(testCollection,PipelineJobCenter.getShardingItems("11")); + PipelineJob pipelineJob = mock(PipelineJob.class); + PipelineJobCenter.addJob("Job1", pipelineJob); + when(pipelineJob.getShardingItems()).thenReturn(Arrays.asList(1, 2, 3)); + Collection shardingItems = pipelineJob.getShardingItems(); + + Assertions.assertFalse(shardingItems.isEmpty()); + Assertions.assertEquals(Arrays.asList(1, 2, 3), PipelineJobCenter.getShardingItems("Job1")); + assertEquals(Collections.EMPTY_LIST, PipelineJobCenter.getShardingItems("Job2")); + PipelineJobCenter.stop("Job1"); } -} \ No newline at end of file +} From b084c1620ab25dadfa1f542d8b6f6828d7d7be83 Mon Sep 17 00:00:00 2001 From: HarshSawarkar GitHub Date: Tue, 31 Oct 2023 17:54:47 +0530 Subject: [PATCH 4/6] Formatted the code as per code of conduct and made the changes as mentioned in the review comments. --- .../core/job/PipelineJobCenterTest.java | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/job/PipelineJobCenterTest.java b/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/job/PipelineJobCenterTest.java index 5c4494571fefd..898b735f74c08 100644 --- a/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/job/PipelineJobCenterTest.java +++ b/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/job/PipelineJobCenterTest.java @@ -46,18 +46,12 @@ class PipelineJobCenterTest { void testPipelineJobCenter() { PipelineJob pipelineJob = mock(PipelineJob.class); PipelineJobCenter.addJob("Job1", pipelineJob); - assertTrue(PipelineJobCenter.isJobExisting("Job1")); assertFalse(PipelineJobCenter.isJobExisting("Job2")); assertNotNull(PipelineJobCenter.getJob("Job1")); assertEquals(pipelineJob, PipelineJobCenter.getJob("Job1")); assertNull(PipelineJobCenter.getJob("Job2")); - - PipelineJob pipelineJob1 = mock(PipelineJob.class); - PipelineJobCenter.addJob("Job3", pipelineJob1); - assertNotNull(PipelineJobCenter.getJob("Job3")); - assertEquals(pipelineJob1, PipelineJobCenter.getJob("Job3")); - PipelineJobCenter.stop("Job3"); + PipelineJobCenter.stop("Job1"); } @Test @@ -65,16 +59,14 @@ void getJobItemContext() { PipelineJob pipelineJob = mock(PipelineJob.class); PipelineTasksRunner pipelineTasksRunner = mock(PipelineTasksRunner.class); PipelineJobItemContext pipelineJobItemContext = mock(PipelineJobItemContext.class); - Optional pipelineJobItemContext1 = Optional.ofNullable(pipelineJobItemContext); + Optional optionalPipelineJobItemContext = Optional.ofNullable(pipelineJobItemContext); when(pipelineJob.getTasksRunner(anyInt())).thenReturn(Optional.of(pipelineTasksRunner)); when(pipelineTasksRunner.getJobItemContext()).thenReturn(pipelineJobItemContext); PipelineJobCenter.addJob("Job1", pipelineJob); Optional result = PipelineJobCenter.getJobItemContext("Job1", 1); - Optional result1 = PipelineJobCenter.getJobItemContext("Job2", 1); assertTrue(result.isPresent()); - assertEquals(pipelineJobItemContext1, result); - assertFalse(result1.isPresent()); - assertNotNull(result1); + assertEquals(Optional.empty(), PipelineJobCenter.getJobItemContext("Job2", 1)); + assertEquals(optionalPipelineJobItemContext, result); PipelineJobCenter.stop("Job1"); } @@ -84,7 +76,6 @@ void getShardingItems() { PipelineJobCenter.addJob("Job1", pipelineJob); when(pipelineJob.getShardingItems()).thenReturn(Arrays.asList(1, 2, 3)); Collection shardingItems = pipelineJob.getShardingItems(); - Assertions.assertFalse(shardingItems.isEmpty()); Assertions.assertEquals(Arrays.asList(1, 2, 3), PipelineJobCenter.getShardingItems("Job1")); assertEquals(Collections.EMPTY_LIST, PipelineJobCenter.getShardingItems("Job2")); From dccc6b5c00809c55e2683e506f630d2f0b0f077b Mon Sep 17 00:00:00 2001 From: HarshSawarkar GitHub Date: Wed, 1 Nov 2023 12:28:20 +0530 Subject: [PATCH 5/6] Corrected the checkstyle errors --- .../core/job/PipelineJobCenterTest.java | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/job/PipelineJobCenterTest.java b/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/job/PipelineJobCenterTest.java index 898b735f74c08..521f89c390bf8 100644 --- a/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/job/PipelineJobCenterTest.java +++ b/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/job/PipelineJobCenterTest.java @@ -17,25 +17,20 @@ package org.apache.shardingsphere.data.pipeline.core.job; -import java.sql.SQLException; -import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; import java.util.Optional; - import org.apache.shardingsphere.data.pipeline.common.context.PipelineJobItemContext; import org.apache.shardingsphere.data.pipeline.common.job.PipelineJob; import org.apache.shardingsphere.data.pipeline.core.task.runner.PipelineTasksRunner; -import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; -import org.locationtech.jts.util.Assert; - -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -59,11 +54,11 @@ void getJobItemContext() { PipelineJob pipelineJob = mock(PipelineJob.class); PipelineTasksRunner pipelineTasksRunner = mock(PipelineTasksRunner.class); PipelineJobItemContext pipelineJobItemContext = mock(PipelineJobItemContext.class); - Optional optionalPipelineJobItemContext = Optional.ofNullable(pipelineJobItemContext); when(pipelineJob.getTasksRunner(anyInt())).thenReturn(Optional.of(pipelineTasksRunner)); when(pipelineTasksRunner.getJobItemContext()).thenReturn(pipelineJobItemContext); PipelineJobCenter.addJob("Job1", pipelineJob); Optional result = PipelineJobCenter.getJobItemContext("Job1", 1); + Optional optionalPipelineJobItemContext = Optional.ofNullable(pipelineJobItemContext); assertTrue(result.isPresent()); assertEquals(Optional.empty(), PipelineJobCenter.getJobItemContext("Job2", 1)); assertEquals(optionalPipelineJobItemContext, result); From d1ee562a0828f7a44dbace556b2b5abe23afb4d8 Mon Sep 17 00:00:00 2001 From: HarshSawarkar GitHub Date: Wed, 1 Nov 2023 18:18:48 +0530 Subject: [PATCH 6/6] Updated method name --- .../pipeline/core/job/PipelineJobCenterTest.java | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/job/PipelineJobCenterTest.java b/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/job/PipelineJobCenterTest.java index 521f89c390bf8..ab6e08941b328 100644 --- a/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/job/PipelineJobCenterTest.java +++ b/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/job/PipelineJobCenterTest.java @@ -17,15 +17,17 @@ package org.apache.shardingsphere.data.pipeline.core.job; -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.Optional; import org.apache.shardingsphere.data.pipeline.common.context.PipelineJobItemContext; import org.apache.shardingsphere.data.pipeline.common.job.PipelineJob; import org.apache.shardingsphere.data.pipeline.core.task.runner.PipelineTasksRunner; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; + +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.Optional; + import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -38,7 +40,7 @@ class PipelineJobCenterTest { @Test - void testPipelineJobCenter() { + void assertPipelineJobCenter() { PipelineJob pipelineJob = mock(PipelineJob.class); PipelineJobCenter.addJob("Job1", pipelineJob); assertTrue(PipelineJobCenter.isJobExisting("Job1")); @@ -50,7 +52,7 @@ void testPipelineJobCenter() { } @Test - void getJobItemContext() { + void assertGetJobItemContext() { PipelineJob pipelineJob = mock(PipelineJob.class); PipelineTasksRunner pipelineTasksRunner = mock(PipelineTasksRunner.class); PipelineJobItemContext pipelineJobItemContext = mock(PipelineJobItemContext.class); @@ -66,7 +68,7 @@ void getJobItemContext() { } @Test - void getShardingItems() { + void assertGetShardingItems() { PipelineJob pipelineJob = mock(PipelineJob.class); PipelineJobCenter.addJob("Job1", pipelineJob); when(pipelineJob.getShardingItems()).thenReturn(Arrays.asList(1, 2, 3));