From a865be09b6ac38a0d0bfe3cfc4fb897e6038471b Mon Sep 17 00:00:00 2001 From: Eric Gao Date: Sun, 9 Oct 2022 12:38:03 +0800 Subject: [PATCH] Remove powermock from DatasyncTaskTest (#12270) --- .../plugin/task/datasync/DatasyncTask.java | 33 +++++---- .../task/datasync/DatasyncTaskTest.java | 71 +++++++++---------- 2 files changed, 52 insertions(+), 52 deletions(-) diff --git a/dolphinscheduler-task-plugin/dolphinscheduler-task-datasync/src/main/java/org/apache/dolphinscheduler/plugin/task/datasync/DatasyncTask.java b/dolphinscheduler-task-plugin/dolphinscheduler-task-datasync/src/main/java/org/apache/dolphinscheduler/plugin/task/datasync/DatasyncTask.java index 6fba880279..37a82b173d 100644 --- a/dolphinscheduler-task-plugin/dolphinscheduler-task-datasync/src/main/java/org/apache/dolphinscheduler/plugin/task/datasync/DatasyncTask.java +++ b/dolphinscheduler-task-plugin/dolphinscheduler-task-datasync/src/main/java/org/apache/dolphinscheduler/plugin/task/datasync/DatasyncTask.java @@ -17,26 +17,31 @@ package org.apache.dolphinscheduler.plugin.task.datasync; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.PropertyNamingStrategies; -import com.fasterxml.jackson.databind.json.JsonMapper; +import static com.fasterxml.jackson.databind.DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT; +import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES; +import static com.fasterxml.jackson.databind.DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL; +import static com.fasterxml.jackson.databind.MapperFeature.REQUIRE_SETTERS_FOR_GETTERS; + import org.apache.dolphinscheduler.plugin.task.api.AbstractRemoteTask; import org.apache.dolphinscheduler.plugin.task.api.TaskConstants; import org.apache.dolphinscheduler.plugin.task.api.TaskException; import org.apache.dolphinscheduler.plugin.task.api.TaskExecutionContext; import org.apache.dolphinscheduler.spi.utils.JSONUtils; import org.apache.dolphinscheduler.spi.utils.StringUtils; + import software.amazon.awssdk.services.datasync.model.TaskExecutionStatus; import java.util.Collections; import java.util.List; -import static com.fasterxml.jackson.databind.DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT; -import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES; -import static com.fasterxml.jackson.databind.DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL; -import static com.fasterxml.jackson.databind.MapperFeature.REQUIRE_SETTERS_FOR_GETTERS; +import lombok.Setter; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.PropertyNamingStrategies; +import com.fasterxml.jackson.databind.json.JsonMapper; +@Setter public class DatasyncTask extends AbstractRemoteTask { private static final ObjectMapper objectMapper = @@ -80,8 +85,8 @@ public class DatasyncTask extends AbstractRemoteTask { } catch (JsonProcessingException e) { throw new RuntimeException(e); } - //parameters = JSONUtils.parseObject(parameters.getJson(), DatasyncParameters.class); - logger.info("Datasync convert task params {}", parameters); + // parameters = JSONUtils.parseObject(parameters.getJson(), DatasyncParameters.class); + logger.info("Datasync convert task params {}", parameters); } } @@ -90,18 +95,18 @@ public class DatasyncTask extends AbstractRemoteTask { try { int exitStatusCode = checkCreateTask(); if (exitStatusCode == TaskConstants.EXIT_CODE_FAILURE) { - //if create task failure go end + // if create task failure go end setExitStatusCode(exitStatusCode); return; } - //start task + // start task exitStatusCode = startDatasyncTask(); setExitStatusCode(exitStatusCode); } catch (Exception e) { setExitStatusCode(TaskConstants.EXIT_CODE_FAILURE); throw new TaskException("datasync task error", e); } - //set taskExecArn to the appIds if start success + // set taskExecArn to the appIds if start success setAppIds(hook.getTaskExecArn()); } @@ -159,4 +164,4 @@ public class DatasyncTask extends AbstractRemoteTask { return parameters; } -} \ No newline at end of file +} diff --git a/dolphinscheduler-task-plugin/dolphinscheduler-task-datasync/src/test/java/org/apache/dolphinscheduler/plugin/task/datasync/DatasyncTaskTest.java b/dolphinscheduler-task-plugin/dolphinscheduler-task-datasync/src/test/java/org/apache/dolphinscheduler/plugin/task/datasync/DatasyncTaskTest.java index 7b66abd969..0a7ca878a1 100644 --- a/dolphinscheduler-task-plugin/dolphinscheduler-task-datasync/src/test/java/org/apache/dolphinscheduler/plugin/task/datasync/DatasyncTaskTest.java +++ b/dolphinscheduler-task-plugin/dolphinscheduler-task-datasync/src/test/java/org/apache/dolphinscheduler/plugin/task/datasync/DatasyncTaskTest.java @@ -17,30 +17,15 @@ package org.apache.dolphinscheduler.plugin.task.datasync; +import static org.mockito.Mockito.any; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mockStatic; -import static org.powermock.api.mockito.PowerMockito.doReturn; -import static org.powermock.api.mockito.PowerMockito.mock; -import static org.powermock.api.mockito.PowerMockito.spy; -import static org.powermock.api.mockito.PowerMockito.when; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.when; import org.apache.dolphinscheduler.plugin.task.api.TaskExecutionContext; import org.apache.dolphinscheduler.spi.utils.JSONUtils; -import org.apache.dolphinscheduler.spi.utils.PropertyUtils; - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.MockedStatic; -import org.mockito.Mockito; - -import static org.mockito.Mockito.any; - -import org.powermock.api.support.membermodification.MemberModifier; -import org.powermock.core.classloader.annotations.PowerMockIgnore; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; import software.amazon.awssdk.http.SdkHttpResponse; import software.amazon.awssdk.services.datasync.DataSyncClient; @@ -53,18 +38,23 @@ import software.amazon.awssdk.services.datasync.model.StartTaskExecutionResponse import software.amazon.awssdk.services.datasync.model.TaskExecutionStatus; import software.amazon.awssdk.services.datasync.model.TaskStatus; -@RunWith(PowerMockRunner.class) -@PrepareForTest({ - JSONUtils.class, - PropertyUtils.class, - DatasyncHook.class -}) -@PowerMockIgnore({"javax.*"}) +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.MockedStatic; +import org.mockito.Mockito; +import org.mockito.junit.MockitoJUnitRunner; + +@RunWith(MockitoJUnitRunner.class) public class DatasyncTaskTest { - private static final String mockExeArn = "arn:aws:datasync:ap-northeast-3:523202806641:task/task-017642db08fdf6a55/execution/exec-0ac3607778dfc31f5"; + private static final String mockExeArn = + "arn:aws:datasync:ap-northeast-3:523202806641:task/task-017642db08fdf6a55/execution/exec-0ac3607778dfc31f5"; - private static final String mockTaskArn = "arn:aws:datasync:ap-northeast-3:523202806641:task/task-071ca64ff4c2f0d4a"; + private static final String mockTaskArn = + "arn:aws:datasync:ap-northeast-3:523202806641:task/task-071ca64ff4c2f0d4a"; DatasyncHook datasyncHook; @@ -81,14 +71,16 @@ public class DatasyncTaskTest { DatasyncParameters DatasyncParameters = new DatasyncParameters(); datasyncTask = initTask(DatasyncParameters); - MemberModifier.field(DatasyncTask.class, "hook").set(datasyncTask, datasyncHook); + datasyncTask.setHook(datasyncHook); } @Test public void testCreateTaskJson() { String jsonData = "{\n" + - " \"CloudWatchLogGroupArn\": \"arn:aws:logs:ap-northeast-3:523202806641:log-group:/aws/datasync:*\",\n" + - " \"DestinationLocationArn\": \"arn:aws:datasync:ap-northeast-3:523202806641:location/loc-01cf61e102e58e365\",\n" + + " \"CloudWatchLogGroupArn\": \"arn:aws:logs:ap-northeast-3:523202806641:log-group:/aws/datasync:*\",\n" + + + " \"DestinationLocationArn\": \"arn:aws:datasync:ap-northeast-3:523202806641:location/loc-01cf61e102e58e365\",\n" + + " \"Excludes\": [ \n" + " { \n" + " \"FilterType\": \"exType1\",\n" + @@ -122,7 +114,8 @@ public class DatasyncTaskTest { " \"Schedule\": { \n" + " \"ScheduleExpression\": \"* * * * * ?\"\n" + " },\n" + - " \"SourceLocationArn\": \"arn:aws:datasync:ap-northeast-3:523202806641:location/loc-04ceafb4aaf7a1a0d\",\n" + + " \"SourceLocationArn\": \"arn:aws:datasync:ap-northeast-3:523202806641:location/loc-04ceafb4aaf7a1a0d\",\n" + + " \"Tags\": [ \n" + " { \n" + " \"Key\": \"tagKey1\",\n" + @@ -136,10 +129,13 @@ public class DatasyncTaskTest { DatasyncTask DatasyncTask = initTask(DatasyncParameters); DatasyncParameters datasyncParameters = DatasyncTask.getParameters(); - Assert.assertEquals("arn:aws:logs:ap-northeast-3:523202806641:log-group:/aws/datasync:*", datasyncParameters.getCloudWatchLogGroupArn()); + Assert.assertEquals("arn:aws:logs:ap-northeast-3:523202806641:log-group:/aws/datasync:*", + datasyncParameters.getCloudWatchLogGroupArn()); Assert.assertEquals("task001", datasyncParameters.getName()); - Assert.assertEquals("arn:aws:datasync:ap-northeast-3:523202806641:location/loc-04ceafb4aaf7a1a0d", datasyncParameters.getSourceLocationArn()); - Assert.assertEquals("arn:aws:datasync:ap-northeast-3:523202806641:location/loc-01cf61e102e58e365", datasyncParameters.getDestinationLocationArn()); + Assert.assertEquals("arn:aws:datasync:ap-northeast-3:523202806641:location/loc-04ceafb4aaf7a1a0d", + datasyncParameters.getSourceLocationArn()); + Assert.assertEquals("arn:aws:datasync:ap-northeast-3:523202806641:location/loc-01cf61e102e58e365", + datasyncParameters.getDestinationLocationArn()); Assert.assertEquals("inType1", datasyncParameters.getIncludes().get(0).getFilterType()); Assert.assertEquals("inValue1", datasyncParameters.getIncludes().get(0).getValue()); Assert.assertEquals("exType1", datasyncParameters.getExcludes().get(0).getFilterType()); @@ -228,7 +224,6 @@ public class DatasyncTaskTest { String parameters = JSONUtils.toJsonString(DatasyncParameters); TaskExecutionContext taskExecutionContext = Mockito.mock(TaskExecutionContext.class); Mockito.when(taskExecutionContext.getTaskParams()).thenReturn(parameters); - Mockito.when(taskExecutionContext.getExecutePath()).thenReturn("/tmp/dolphinscheduler_datasync_%s"); return taskExecutionContext; } -} \ No newline at end of file +}