You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2020/07/21 11:16:12 UTC

[shardingsphere-elasticjob] branch master updated: Add test cases for JobExecutor (#1210) (#1225)

This is an automated email from the ASF dual-hosted git repository.

zhangliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere-elasticjob.git


The following commit(s) were added to refs/heads/master by this push:
     new a6601c6  Add test cases for JobExecutor (#1210) (#1225)
a6601c6 is described below

commit a6601c6815c58b619b127c146c9caa43e5b6266c
Author: Tboy <gu...@immomo.com>
AuthorDate: Tue Jul 21 19:16:02 2020 +0800

    Add test cases for JobExecutor (#1210) (#1225)
---
 .../dataflow/executor/DataflowJobExecutorTest.java | 90 ++++++++++++++++++++++
 .../elasticjob/script/ScriptJobExecutorTest.java   | 88 +++++++++++++++++++++
 .../simple/executor/SimpleJobExecutorTest.java     | 64 +++++++++++++++
 .../elasticjob-executor-type/pom.xml               | 19 +++++
 4 files changed, 261 insertions(+)

diff --git a/elasticjob-executor/elasticjob-executor-type/elasticjob-dataflow-executor/src/test/java/org/apache/shardingsphere/elasticjob/dataflow/executor/DataflowJobExecutorTest.java b/elasticjob-executor/elasticjob-executor-type/elasticjob-dataflow-executor/src/test/java/org/apache/shardingsphere/elasticjob/dataflow/executor/DataflowJobExecutorTest.java
new file mode 100644
index 0000000..64c9a14
--- /dev/null
+++ b/elasticjob-executor/elasticjob-executor-type/elasticjob-dataflow-executor/src/test/java/org/apache/shardingsphere/elasticjob/dataflow/executor/DataflowJobExecutorTest.java
@@ -0,0 +1,90 @@
+/*
+ * 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.elasticjob.dataflow.executor;
+
+import org.apache.shardingsphere.elasticjob.api.JobConfiguration;
+import org.apache.shardingsphere.elasticjob.api.ShardingContext;
+import org.apache.shardingsphere.elasticjob.dataflow.job.DataflowJob;
+import org.apache.shardingsphere.elasticjob.dataflow.props.DataflowJobProperties;
+import org.apache.shardingsphere.elasticjob.executor.JobFacade;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.Properties;
+
+import static org.junit.Assert.assertEquals;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+@RunWith(MockitoJUnitRunner.class)
+public final class DataflowJobExecutorTest {
+    
+    private DataflowJobExecutor jobExecutor;
+    
+    @Mock
+    private DataflowJob elasticJob;
+    
+    @Mock
+    private JobConfiguration jobConfig;
+    
+    @Mock
+    private JobFacade jobFacade;
+    
+    @Mock
+    private ShardingContext shardingContext;
+    
+    @Mock
+    private Properties properties;
+    
+    @Before
+    public void createJobExecutor() {
+        jobExecutor = new DataflowJobExecutor();
+    }
+    
+    @Test
+    public void testProcessWithStreamingExecute() {
+        List<String> data = Arrays.asList("DataflowJob1", "DataflowJob2");
+        when(jobConfig.getProps()).thenReturn(properties);
+        when(properties.getOrDefault(DataflowJobProperties.STREAM_PROCESS_KEY, false)).thenReturn("true");
+        when(elasticJob.fetchData(shardingContext)).thenReturn(data);
+        when(jobFacade.isNeedSharding()).thenReturn(true);
+        jobExecutor.process(elasticJob, jobConfig, jobFacade, shardingContext);
+        verify(elasticJob, times(1)).processData(shardingContext, data);
+    }
+    
+    @Test
+    public void testProcessWithOneOffExecute() {
+        List<String> data = Arrays.asList("DataflowJob1", "DataflowJob2");
+        when(jobConfig.getProps()).thenReturn(properties);
+        when(properties.getOrDefault(DataflowJobProperties.STREAM_PROCESS_KEY, false)).thenReturn("false");
+        when(elasticJob.fetchData(shardingContext)).thenReturn(data);
+        jobExecutor.process(elasticJob, jobConfig, jobFacade, shardingContext);
+        verify(elasticJob, times(1)).processData(shardingContext, data);
+    }
+    
+    @Test
+    public void testGetElasticJobClass() {
+        assertEquals(DataflowJob.class, jobExecutor.getElasticJobClass());
+    }
+}
diff --git a/elasticjob-executor/elasticjob-executor-type/elasticjob-script-executor/src/test/java/org/apache/shardingsphere/elasticjob/script/ScriptJobExecutorTest.java b/elasticjob-executor/elasticjob-executor-type/elasticjob-script-executor/src/test/java/org/apache/shardingsphere/elasticjob/script/ScriptJobExecutorTest.java
new file mode 100644
index 0000000..eeb791f
--- /dev/null
+++ b/elasticjob-executor/elasticjob-executor-type/elasticjob-script-executor/src/test/java/org/apache/shardingsphere/elasticjob/script/ScriptJobExecutorTest.java
@@ -0,0 +1,88 @@
+/*
+ * 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.elasticjob.script;
+
+import org.apache.shardingsphere.elasticjob.api.ElasticJob;
+import org.apache.shardingsphere.elasticjob.api.JobConfiguration;
+import org.apache.shardingsphere.elasticjob.api.ShardingContext;
+import org.apache.shardingsphere.elasticjob.executor.JobFacade;
+import org.apache.shardingsphere.elasticjob.infra.exception.JobConfigurationException;
+import org.apache.shardingsphere.elasticjob.infra.exception.JobSystemException;
+import org.apache.shardingsphere.elasticjob.script.executor.ScriptJobExecutor;
+import org.apache.shardingsphere.elasticjob.script.props.ScriptJobProperties;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+
+import java.util.Properties;
+
+import static org.junit.Assert.assertEquals;
+import static org.mockito.Mockito.when;
+
+@RunWith(MockitoJUnitRunner.class)
+public final class ScriptJobExecutorTest {
+    
+    @Mock
+    private ElasticJob elasticJob;
+    
+    @Mock
+    private JobConfiguration jobConfig;
+    
+    @Mock
+    private JobFacade jobFacade;
+    
+    @Mock
+    private Properties properties;
+    
+    @Mock
+    private ShardingContext shardingContext;
+    
+    private ScriptJobExecutor jobExecutor;
+    
+    @Before
+    public void setUp() {
+        jobExecutor = new ScriptJobExecutor();
+    }
+    
+    @Test(expected = JobConfigurationException.class)
+    public void testProcessWithJobConfigurationException() {
+        when(jobConfig.getProps()).thenReturn(properties);
+        jobExecutor.process(elasticJob, jobConfig, jobFacade, shardingContext);
+    }
+    
+    @Test(expected = JobSystemException.class)
+    public void testProcessWithJobSystemException() {
+        when(jobConfig.getProps()).thenReturn(properties);
+        when(properties.getProperty(ScriptJobProperties.SCRIPT_KEY)).thenReturn("demo.sh");
+        jobExecutor.process(elasticJob, jobConfig, jobFacade, shardingContext);
+    }
+    
+    @Test
+    public void testProcess() {
+        when(jobConfig.getProps()).thenReturn(properties);
+        when(properties.getProperty(ScriptJobProperties.SCRIPT_KEY)).thenReturn("echo script-job");
+        jobExecutor.process(elasticJob, jobConfig, jobFacade, shardingContext);
+    }
+    
+    @Test
+    public void testGetType() {
+        assertEquals("SCRIPT", jobExecutor.getType());
+    }
+}
diff --git a/elasticjob-executor/elasticjob-executor-type/elasticjob-simple-executor/src/test/java/org/apache/shardingsphere/elasticjob/simple/executor/SimpleJobExecutorTest.java b/elasticjob-executor/elasticjob-executor-type/elasticjob-simple-executor/src/test/java/org/apache/shardingsphere/elasticjob/simple/executor/SimpleJobExecutorTest.java
new file mode 100644
index 0000000..ef952cd
--- /dev/null
+++ b/elasticjob-executor/elasticjob-executor-type/elasticjob-simple-executor/src/test/java/org/apache/shardingsphere/elasticjob/simple/executor/SimpleJobExecutorTest.java
@@ -0,0 +1,64 @@
+/*
+ * 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.elasticjob.simple.executor;
+
+import org.apache.shardingsphere.elasticjob.api.JobConfiguration;
+import org.apache.shardingsphere.elasticjob.executor.JobFacade;
+import org.apache.shardingsphere.elasticjob.simple.job.FooSimpleJob;
+import org.apache.shardingsphere.elasticjob.simple.job.SimpleJob;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+
+import static org.junit.Assert.assertEquals;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+
+@RunWith(MockitoJUnitRunner.class)
+public class SimpleJobExecutorTest {
+    
+    @Mock
+    private FooSimpleJob fooSimpleJob;
+    
+    @Mock
+    private JobConfiguration jobConfig;
+    
+    @Mock
+    private JobFacade jobFacade;
+    
+    private SimpleJobExecutor jobExecutor;
+    
+    @Before
+    public void setUp() {
+        jobExecutor = new SimpleJobExecutor();
+    }
+    
+    @Test
+    public void testProcess() {
+        jobExecutor.process(fooSimpleJob, jobConfig, jobFacade, any());
+        verify(fooSimpleJob, times(1)).execute(any());
+    }
+    
+    @Test
+    public void testGetElasticJobClass() {
+        assertEquals(jobExecutor.getElasticJobClass(), SimpleJob.class);
+    }
+}
diff --git a/elasticjob-executor/elasticjob-executor-type/pom.xml b/elasticjob-executor/elasticjob-executor-type/pom.xml
index 074ac69..58b981a 100644
--- a/elasticjob-executor/elasticjob-executor-type/pom.xml
+++ b/elasticjob-executor/elasticjob-executor-type/pom.xml
@@ -34,4 +34,23 @@
         <module>elasticjob-dataflow-executor</module>
         <module>elasticjob-script-executor</module>
     </modules>
+    
+    <dependencies>
+        <dependency>
+            <groupId>org.projectlombok</groupId>
+            <artifactId>lombok</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.mockito</groupId>
+            <artifactId>mockito-core</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.mockito</groupId>
+            <artifactId>mockito-inline</artifactId>
+        </dependency>
+    </dependencies>
 </project>