@ -22,16 +22,15 @@ import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt ;
import org.apache.dolphinscheduler.api.dto.CommandStateCount ;
import org.apache.dolphinscheduler.api.dto.TaskStateCount ;
import org.apache.dolphinscheduler.api.enums.Status ;
import org.apache.dolphinscheduler.api.service.impl.DataAnalysisServiceImpl ;
import org.apache.dolphinscheduler.api.service.impl.ProjectServiceImpl ;
import org.apache.dolphinscheduler.common.Constants ;
import org.apache.dolphinscheduler.common.enums.CommandType ;
import org.apache.dolphinscheduler.common.enums.ExecutionStatus ;
import org.apache.dolphinscheduler.common.enums.UserType ;
import org.apache.dolphinscheduler.common.utils.DateUtils ;
import org.apache.dolphinscheduler.dao.entity.CommandCount ;
import org.apache.dolphinscheduler.dao.entity.DefinitionGroupByUser ;
import org.apache.dolphinscheduler.dao.entity.ExecuteStatusCount ;
import org.apache.dolphinscheduler.dao.entity.Project ;
import org.apache.dolphinscheduler.dao.entity.User ;
@ -43,6 +42,7 @@ import org.apache.dolphinscheduler.dao.mapper.ProjectMapper;
import org.apache.dolphinscheduler.dao.mapper.TaskInstanceMapper ;
import org.apache.dolphinscheduler.service.process.ProcessService ;
import java.text.MessageFormat ;
import java.util.ArrayList ;
import java.util.Collections ;
import java.util.HashMap ;
@ -57,6 +57,7 @@ import org.junit.runner.RunWith;
import org.mockito.InjectMocks ;
import org.mockito.Mock ;
import org.mockito.Mockito ;
import org.powermock.api.mockito.PowerMockito ;
import org.powermock.modules.junit4.PowerMockRunner ;
/ * *
@ -66,13 +67,13 @@ import org.powermock.modules.junit4.PowerMockRunner;
public class DataAnalysisServiceTest {
@InjectMocks
private DataAnalysisServiceImpl dataAnalysisService ;
private DataAnalysisServiceImpl dataAnalysisServiceImpl ;
@Mock
ProjectMapper projectMapper ;
@Mock
ProjectServiceImpl projectService ;
ProjectService projectService ;
@Mock
ProcessInstanceMapper processInstanceMapper ;
@ -102,134 +103,144 @@ public class DataAnalysisServiceTest {
user = new User ( ) ;
Project project = new Project ( ) ;
project . setId ( 1 ) ;
project . setName ( "test" ) ;
resultMap = new HashMap < > ( ) ;
Mockito . when ( projectMapper . selectById ( 1 ) ) . thenReturn ( project ) ;
Mockito . when ( projectService . hasProjectAndPerm ( user , project , resultMap ) ) . thenReturn ( true ) ;
Mockito . when ( projectMapper . queryByCode ( 1L ) ) . thenReturn ( project ) ;
}
@After
public void after ( ) {
user = null ;
projectMapper = null ;
resultMap = null ;
}
@Test
public void testCountTaskStateByProject ( ) {
public void testCountTaskStateByProject_success ( ) {
String startDate = "2020-02-11 16:02:18" ;
String endDate = "2020-02-11 16:03:18" ;
//checkProject false
Map < String , Object > result = dataAnalysisService . countTaskStateByProject ( user , 2 , startDate , endDate ) ;
Assert . assertTrue ( result . isEmpty ( ) ) ;
Map < String , Object > result = new HashMap < > ( ) ;
putMsg ( result , Status . SUCCESS , null ) ;
Mockito . when ( projectService . checkProjectAndAuth ( any ( ) , any ( ) , any ( ) ) ) . thenReturn ( result ) ;
Mockito . when ( projectMapper . queryByCode ( Mockito . any ( ) ) ) . thenReturn ( getProject ( "test" ) ) ;
//SUCCESS
// SUCCESS
Mockito . when ( taskInstanceMapper . countTaskInstanceStateByUser ( DateUtils . getScheduleDate ( startDate ) ,
DateUtils . getScheduleDate ( endDate ) , new Long [ ] { 1L } ) ) . thenReturn ( getTaskInstanceStateCounts ( ) ) ;
Mockito . when ( projectMapper . selectById ( Mockito . any ( ) ) ) . thenReturn ( getProject ( "test" ) ) ;
Mockito . when ( projectService . hasProjectAndPerm ( Mockito . any ( ) , Mockito . any ( ) , Mockito . any ( ) ) ) . thenReturn ( true ) ;
result = dataAnalysisService . countTaskStateByProject ( user , 1 , startDate , endDate ) ;
DateUtils . getScheduleDate ( endDate ) , new Long [ ] { 1L } ) ) . thenReturn ( getTaskInstanceStateCounts ( ) ) ;
Mockito . when ( projectMapper . queryByCode ( Mockito . any ( ) ) ) . thenReturn ( getProject ( "test" ) ) ;
result = dataAnalysisServiceImpl . countTaskStateByProject ( user , 1 , startDate , endDate ) ;
Assert . assertEquals ( Status . SUCCESS , result . get ( Constants . STATUS ) ) ;
}
@Test
public void testCountTaskStateByProject_projectNotFound ( ) {
String startDate = "2020-02-11 16:02:18" ;
String endDate = "2020-02-11 16:03:18" ;
// checkProject false
Map < String , Object > failResult = new HashMap < > ( ) ;
putMsg ( failResult , Status . PROJECT_NOT_FOUNT , 1 ) ;
Mockito . when ( projectService . checkProjectAndAuth ( any ( ) , any ( ) , any ( ) ) ) . thenReturn ( failResult ) ;
failResult = dataAnalysisServiceImpl . countTaskStateByProject ( user , 1 , startDate , endDate ) ;
Assert . assertEquals ( Status . PROJECT_NOT_FOUNT , failResult . get ( Constants . STATUS ) ) ;
}
@Test
public void testCountTaskStateByProject_paramValid ( ) {
Map < String , Object > result = new HashMap < > ( ) ;
putMsg ( result , Status . SUCCESS , null ) ;
Mockito . when ( projectService . checkProjectAndAuth ( any ( ) , any ( ) , any ( ) ) ) . thenReturn ( result ) ;
Mockito . when ( projectMapper . queryByCode ( Mockito . any ( ) ) ) . thenReturn ( getProject ( "test" ) ) ;
// when date in illegal format then return error message
String startDate2 = "illegalDateString" ;
String endDate2 = "illegalDateString" ;
result = dataAnalysisService . countTaskStateByProject ( user , 0 , startDate2 , endDate2 ) ;
result = dataAnalysisServiceImpl . countTaskStateByProject ( user , 1 , startDate2 , endDate2 ) ;
Assert . assertEquals ( Status . REQUEST_PARAMS_NOT_VALID_ERROR , result . get ( Constants . STATUS ) ) ;
// when one of date in illegal format then return error message
String startDate3 = "2020-08-28 14:13:40" ;
String endDate3 = "illegalDateString" ;
result = dataAnalysisService . countTaskStateByProject ( user , 0 , startDate3 , endDate3 ) ;
result = dataAnalysisServiceImpl . countTaskStateByProject ( user , 1 , startDate3 , endDate3 ) ;
Assert . assertEquals ( Status . REQUEST_PARAMS_NOT_VALID_ERROR , result . get ( Constants . STATUS ) ) ;
// when one of date in illegal format then return error message
String startDate4 = "illegalDateString" ;
String endDate4 = "2020-08-28 14:13:40" ;
result = dataAnalysisService . countTaskStateByProject ( user , 0 , startDate4 , endDate4 ) ;
result = dataAnalysisServiceImpl . countTaskStateByProject ( user , 1 , startDate4 , endDate4 ) ;
Assert . assertEquals ( Status . REQUEST_PARAMS_NOT_VALID_ERROR , result . get ( Constants . STATUS ) ) ;
}
// when counting general user's task status then return user's task status count
user . setUserType ( UserType . GENERAL_USER ) ;
Mockito . when ( processService . getProjectIdListHavePerm ( anyInt ( ) ) )
. thenReturn ( Collections . singletonList ( 123L ) ) ;
ExecuteStatusCount executeStatusCount = new ExecuteStatusCount ( ) ;
executeStatusCount . setExecutionStatus ( ExecutionStatus . RUNNING_EXECUTION ) ;
executeStatusCount . setCount ( 10 ) ;
Mockito . when ( taskInstanceMapper . countTaskInstanceStateByUser ( any ( ) , any ( ) , any ( ) ) )
. thenReturn ( Collections . singletonList ( executeStatusCount ) ) ;
result = dataAnalysisService . countTaskStateByProject ( user , 0 , startDate , null ) ;
assertThat ( result . get ( Constants . DATA_LIST ) ) . extracting ( "taskCountDtos" ) . first ( ) . asList ( )
. hasSameSizeAs ( ExecutionStatus . values ( ) ) ;
assertThat ( result . get ( Constants . DATA_LIST ) ) . extracting ( "totalCount" ) . first ( ) . isEqualTo ( 10 ) ;
TaskStateCount taskStateCount = new TaskStateCount ( ExecutionStatus . RUNNING_EXECUTION , 10 ) ;
assertThat ( result . get ( Constants . DATA_LIST ) ) . extracting ( "taskCountDtos" ) . first ( ) . asList ( ) . containsOnlyOnce ( taskStateCount ) ;
@Test
public void testCountTaskStateByProject_allCountZero ( ) {
Map < String , Object > result = new HashMap < > ( ) ;
putMsg ( result , Status . SUCCESS , null ) ;
Mockito . when ( projectService . checkProjectAndAuth ( any ( ) , any ( ) , any ( ) ) ) . thenReturn ( result ) ;
Mockito . when ( projectMapper . queryByCode ( Mockito . any ( ) ) ) . thenReturn ( getProject ( "test" ) ) ;
// when general user doesn't have any task then return all count are 0
user . setUserType ( UserType . GENERAL_USER ) ;
Mockito . when ( processService . getProjectIdListHavePerm ( anyInt ( ) ) )
. thenReturn ( new ArrayList < > ( ) ) ;
Mockito . when ( taskInstanceMapper . countTaskInstanceStateByUser ( any ( ) , any ( ) , any ( ) ) )
. thenReturn ( Collections . emptyList ( ) ) ;
result = dataAnalysisService . countTaskStateByProject ( user , 0 , null , null ) ;
Mockito . when ( taskInstanceMapper . countTaskInstanceStateByUser ( any ( ) , any ( ) , any ( ) ) ) . thenReturn (
Collections . emptyList ( ) ) ;
result = dataAnalysisServiceImpl . countTaskStateByProject ( user , 1 , null , null ) ;
assertThat ( result . get ( Constants . DATA_LIST ) ) . extracting ( "totalCount" ) . first ( ) . isEqualTo ( 0 ) ;
assertThat ( result . get ( Constants . DATA_LIST ) ) . extracting ( "taskCountDtos" ) . first ( ) . asList ( )
. hasSameSizeAs ( ExecutionStatus . values ( ) ) ;
assertThat ( result . get ( Constants . DATA_LIST ) ) . extracting ( "taskCountDtos" ) . first ( ) . asList ( )
. extracting ( "count" ) . allMatch ( count - > count . equals ( 0 ) ) ;
assertThat ( result . get ( Constants . DATA_LIST ) ) . extracting ( "taskCountDtos" ) . first ( ) . asList ( ) . hasSameSizeAs (
ExecutionStatus . values ( ) ) ;
assertThat ( result . get ( Constants . DATA_LIST ) ) . extracting ( "taskCountDtos" ) . first ( ) . asList ( ) . extracting (
"count" ) . allMatch ( count - > count . equals ( 0 ) ) ;
}
// when general user doesn't have any task then return all count are 0
user . setUserType ( UserType . GENERAL_USER ) ;
Mockito . when ( processService . getProjectIdListHavePerm ( anyInt ( ) ) )
. thenReturn ( new ArrayList < > ( ) ) ;
Mockito . when ( taskInstanceMapper . countTaskInstanceStateByUser ( any ( ) , any ( ) , any ( ) ) )
. thenReturn ( Collections . emptyList ( ) ) ;
result = dataAnalysisService . countTaskStateByProject ( user , 0 , null , null ) ;
assertThat ( result . get ( Constants . DATA_LIST ) ) . extracting ( "totalCount" ) . first ( ) . isEqualTo ( 0 ) ;
assertThat ( result . get ( Constants . DATA_LIST ) ) . extracting ( "taskCountDtos" ) . first ( ) . asList ( )
. hasSameSizeAs ( ExecutionStatus . values ( ) ) ;
assertThat ( result . get ( Constants . DATA_LIST ) ) . extracting ( "taskCountDtos" ) . first ( ) . asList ( )
. extracting ( "count" ) . allMatch ( count - > count . equals ( 0 ) ) ;
@Test
public void testCountTaskStateByProject_noData ( ) {
Map < String , Object > result = new HashMap < > ( ) ;
putMsg ( result , Status . SUCCESS , null ) ;
Mockito . when ( projectService . checkProjectAndAuth ( any ( ) , any ( ) , any ( ) ) ) . thenReturn ( result ) ;
Mockito . when ( projectMapper . queryByCode ( Mockito . any ( ) ) ) . thenReturn ( getProject ( "test" ) ) ;
// when instanceStateCounter return null, then return nothing
user . setUserType ( UserType . GENERAL_USER ) ;
Mockito . when ( processService . getProjectIdListHavePerm ( anyInt ( ) ) )
. thenReturn ( new ArrayList < > ( ) ) ;
Mockito . when ( taskInstanceMapper . countTaskInstanceStateByUser ( any ( ) , any ( ) , any ( ) ) )
. thenReturn ( null ) ;
result = dataAnalysisService . countTaskStateByProject ( user , 0 , null , null ) ;
assertThat ( result ) . isEmpty ( ) ;
PowerMockito . when ( taskInstanceMapper . countTaskInstanceStateByUser ( any ( ) , any ( ) , any ( ) ) ) . thenReturn ( null ) ;
result = dataAnalysisServiceImpl . countTaskStateByProject ( user , 1 , null , null ) ;
Assert . assertNull ( result . get ( Constants . DATA_LIST ) ) ;
}
@Test
public void testCountProcessInstanceStateByProject ( ) {
String startDate = "2020-02-11 16:02:18" ;
String endDate = "2020-02-11 16:03:18" ;
Mockito . when ( projectMapper . queryByCode ( Mockito . any ( ) ) ) . thenReturn ( getProject ( "test" ) ) ;
//checkProject false
Map < String , Object > result = dataAnalysisService . countProcessInstanceStateByProject ( user , 2 , startDate , endDate ) ;
Assert . assertTrue ( result . isEmpty ( ) ) ;
Map < String , Object > failResult = new HashMap < > ( ) ;
putMsg ( failResult , Status . PROJECT_NOT_FOUNT , 1 ) ;
Mockito . when ( projectService . checkProjectAndAuth ( any ( ) , any ( ) , any ( ) ) ) . thenReturn ( failResult ) ;
failResult = dataAnalysisServiceImpl . countProcessInstanceStateByProject ( user , 2 , startDate , endDate ) ;
Assert . assertEquals ( Status . PROJECT_NOT_FOUNT , failResult . get ( Constants . STATUS ) ) ;
Map < String , Object > result = new HashMap < > ( ) ;
putMsg ( result , Status . SUCCESS , null ) ;
Mockito . when ( projectService . checkProjectAndAuth ( any ( ) , any ( ) , any ( ) ) ) . thenReturn ( result ) ;
//SUCCESS
Mockito . when ( projectMapper . selectById ( Mockito . any ( ) ) ) . thenReturn ( getProject ( "test" ) ) ;
Mockito . when ( processInstanceMapper . countInstanceStateByUser ( DateUtils . getScheduleDate ( startDate ) ,
DateUtils . getScheduleDate ( endDate ) , new Long [ ] { 1L } ) ) . thenReturn ( getTaskInstanceStateCounts ( ) ) ;
Mockito . when ( projectService . hasProjectAndPerm ( Mockito . any ( ) , Mockito . any ( ) , Mockito . any ( ) ) ) . thenReturn ( true ) ;
result = dataAnalysisService . countProcessInstanceStateByProject ( user , 1 , startDate , endDate ) ;
result = dataAnalysisServiceImpl . countProcessInstanceStateByProject ( user , 1 , startDate , endDate ) ;
Assert . assertEquals ( Status . SUCCESS , result . get ( Constants . STATUS ) ) ;
}
@Test
public void testCountDefinitionByUser ( ) {
Mockito . when ( projectMapper . selectById ( Mockito . any ( ) ) ) . thenReturn ( getProject ( "test" ) ) ;
Map < String , Object > result = dataAnalysisService . countDefinitionByUser ( user , 0 ) ;
Mockito . when ( processDefinitionMapper . countDefinitionGroupByUser ( Mockito . anyInt ( ) , Mockito . any ( Long [ ] . class ) ,
Mockito . anyBoolean ( ) ) ) . thenReturn ( new ArrayList < DefinitionGroupByUser > ( ) ) ;
Map < String , Object > result = dataAnalysisServiceImpl . countDefinitionByUser ( user , 0 ) ;
Assert . assertEquals ( Status . SUCCESS , result . get ( Constants . STATUS ) ) ;
}
@ -239,7 +250,7 @@ public class DataAnalysisServiceTest {
String startDate = "2020-02-11 16:02:18" ;
String endDate = "2020-02-11 16:03:18" ;
//checkProject false
Map < String , Object > result = dataAnalysisService . countCommandState ( user , 2 , startDate , endDate ) ;
Map < String , Object > result = dataAnalysisServiceImpl . countCommandState ( user , 2 , startDate , endDate ) ;
Assert . assertTrue ( result . isEmpty ( ) ) ;
List < CommandCount > commandCounts = new ArrayList < > ( 1 ) ;
CommandCount commandCount = new CommandCount ( ) ;
@ -253,31 +264,31 @@ public class DataAnalysisServiceTest {
Mockito . when ( projectMapper . selectById ( Mockito . any ( ) ) ) . thenReturn ( getProject ( "test" ) ) ;
Mockito . when ( projectService . hasProjectAndPerm ( Mockito . any ( ) , Mockito . any ( ) , Mockito . any ( ) ) ) . thenReturn ( true ) ;
result = dataAnalysisService . countCommandState ( user , 1 , startDate , endDate ) ;
result = dataAnalysisServiceImpl . countCommandState ( user , 1 , startDate , endDate ) ;
Assert . assertEquals ( Status . SUCCESS , result . get ( Constants . STATUS ) ) ;
// when all date in illegal format then return error message
String startDate2 = "illegalDateString" ;
String endDate2 = "illegalDateString" ;
Map < String , Object > result2 = dataAnalysisService . countCommandState ( user , 0 , startDate2 , endDate2 ) ;
Map < String , Object > result2 = dataAnalysisServiceImpl . countCommandState ( user , 0 , startDate2 , endDate2 ) ;
Assert . assertEquals ( Status . REQUEST_PARAMS_NOT_VALID_ERROR , result2 . get ( Constants . STATUS ) ) ;
// when one of date in illegal format then return error message
String startDate3 = "2020-08-22 09:23:10" ;
String endDate3 = "illegalDateString" ;
Map < String , Object > result3 = dataAnalysisService . countCommandState ( user , 0 , startDate3 , endDate3 ) ;
Map < String , Object > result3 = dataAnalysisServiceImpl . countCommandState ( user , 0 , startDate3 , endDate3 ) ;
Assert . assertEquals ( Status . REQUEST_PARAMS_NOT_VALID_ERROR , result3 . get ( Constants . STATUS ) ) ;
// when one of date in illegal format then return error message
String startDate4 = "illegalDateString" ;
String endDate4 = "2020-08-22 09:23:10" ;
Map < String , Object > result4 = dataAnalysisService . countCommandState ( user , 0 , startDate4 , endDate4 ) ;
Map < String , Object > result4 = dataAnalysisServiceImpl . countCommandState ( user , 0 , startDate4 , endDate4 ) ;
Assert . assertEquals ( Status . REQUEST_PARAMS_NOT_VALID_ERROR , result4 . get ( Constants . STATUS ) ) ;
// when no command found then return all count are 0
Mockito . when ( commandMapper . countCommandState ( anyInt ( ) , any ( ) , any ( ) , any ( ) ) ) . thenReturn ( Collections . emptyList ( ) ) ;
Mockito . when ( errorCommandMapper . countCommandState ( any ( ) , any ( ) , any ( ) ) ) . thenReturn ( Collections . emptyList ( ) ) ;
Map < String , Object > result5 = dataAnalysisService . countCommandState ( user , 0 , startDate , null ) ;
Map < String , Object > result5 = dataAnalysisServiceImpl . countCommandState ( user , 0 , startDate , null ) ;
assertThat ( result5 ) . containsEntry ( Constants . STATUS , Status . SUCCESS ) ;
assertThat ( result5 . get ( Constants . DATA_LIST ) ) . asList ( ) . extracting ( "errorCount" ) . allMatch ( count - > count . equals ( 0 ) ) ;
assertThat ( result5 . get ( Constants . DATA_LIST ) ) . asList ( ) . extracting ( "normalCount" ) . allMatch ( count - > count . equals ( 0 ) ) ;
@ -292,7 +303,7 @@ public class DataAnalysisServiceTest {
Mockito . when ( commandMapper . countCommandState ( anyInt ( ) , any ( ) , any ( ) , any ( ) ) ) . thenReturn ( Collections . singletonList ( normalCommandCount ) ) ;
Mockito . when ( errorCommandMapper . countCommandState ( any ( ) , any ( ) , any ( ) ) ) . thenReturn ( Collections . singletonList ( errorCommandCount ) ) ;
Map < String , Object > result6 = dataAnalysisService . countCommandState ( user , 0 , null , null ) ;
Map < String , Object > result6 = dataAnalysisServiceImpl . countCommandState ( user , 0 , null , null ) ;
assertThat ( result6 ) . containsEntry ( Constants . STATUS , Status . SUCCESS ) ;
CommandStateCount commandStateCount = new CommandStateCount ( ) ;
@ -305,11 +316,11 @@ public class DataAnalysisServiceTest {
@Test
public void testCountQueueState ( ) {
// when project check fail then return nothing
Map < String , Object > result1 = dataAnalysisService . countQueueState ( user , 2 ) ;
Map < String , Object > result1 = dataAnalysisServiceImpl . countQueueState ( user , 2 ) ;
Assert . assertTrue ( result1 . isEmpty ( ) ) ;
// when project check success when return all count are 0
Map < String , Object > result2 = dataAnalysisService . countQueueState ( user , 1 ) ;
Map < String , Object > result2 = dataAnalysisServiceImpl . countQueueState ( user , 1 ) ;
assertThat ( result2 . get ( Constants . DATA_LIST ) ) . extracting ( "taskQueue" , "taskKill" )
. isNotEmpty ( )
. allMatch ( count - > count . equals ( 0 ) ) ;
@ -342,4 +353,13 @@ public class DataAnalysisServiceTest {
project . setUserId ( 1 ) ;
return project ;
}
}
private void putMsg ( Map < String , Object > result , Status status , Object . . . statusParams ) {
result . put ( Constants . STATUS , status ) ;
if ( statusParams ! = null & & statusParams . length > 0 ) {
result . put ( Constants . MSG , MessageFormat . format ( status . getMsg ( ) , statusParams ) ) ;
} else {
result . put ( Constants . MSG , status . getMsg ( ) ) ;
}
}
}