31
31
import org .mockito .ArgumentCaptor ;
32
32
import org .mockito .Mockito ;
33
33
34
+ import org .mockito .stubbing .Answer ;
34
35
import org .springframework .beans .factory .annotation .Autowired ;
35
36
import org .springframework .boot .autoconfigure .context .PropertyPlaceholderAutoConfiguration ;
36
37
import org .springframework .boot .autoconfigure .jdbc .DataSourceProperties ;
60
61
import org .springframework .cloud .dataflow .server .service .SchedulerServiceProperties ;
61
62
import org .springframework .cloud .dataflow .server .service .TaskExecutionInfoService ;
62
63
import org .springframework .cloud .deployer .resource .docker .DockerResource ;
63
- import org .springframework .cloud .deployer .spi .core .AppDefinition ;
64
64
import org .springframework .cloud .deployer .spi .scheduler .CreateScheduleException ;
65
65
import org .springframework .cloud .deployer .spi .scheduler .ScheduleInfo ;
66
66
import org .springframework .cloud .deployer .spi .scheduler .ScheduleRequest ;
67
67
import org .springframework .cloud .deployer .spi .scheduler .Scheduler ;
68
68
import org .springframework .cloud .deployer .spi .task .TaskLauncher ;
69
69
import org .springframework .cloud .task .listener .TaskException ;
70
70
import org .springframework .core .env .PropertyResolver ;
71
+ import org .springframework .core .io .FileSystemResource ;
71
72
import org .springframework .core .io .Resource ;
72
73
import org .springframework .core .io .ResourceLoader ;
73
74
import org .springframework .data .domain .Page ;
74
75
import org .springframework .data .domain .PageRequest ;
75
76
import org .springframework .test .annotation .DirtiesContext ;
76
77
77
78
import static org .assertj .core .api .Assertions .assertThat ;
79
+ import static org .assertj .core .api .Assertions .assertThatIllegalArgumentException ;
78
80
import static org .junit .jupiter .api .Assertions .assertEquals ;
79
81
import static org .junit .jupiter .api .Assertions .assertThrows ;
80
82
import static org .mockito .ArgumentMatchers .any ;
81
83
import static org .mockito .ArgumentMatchers .anyString ;
84
+ import static org .mockito .Mockito .doAnswer ;
82
85
import static org .mockito .Mockito .mock ;
83
86
import static org .mockito .Mockito .verify ;
84
87
import static org .mockito .Mockito .when ;
@@ -105,7 +108,9 @@ public class DefaultSchedulerServiceTests {
105
108
106
109
private static final String BASE_DEFINITION_NAME = "myTaskDefinition" ;
107
110
108
- private static final String CTR_DEFINITION_NAME = "myCtrDefinition" ;
111
+ private static final String CTR_DEFINITION_NAME = "myCtrDefinition" ;
112
+
113
+ private static final String DEMO_APP_NAME = "demoAppName" ;
109
114
110
115
@ Autowired
111
116
private Scheduler simpleTestScheduler ;
@@ -431,18 +436,51 @@ public void testScheduleWithoutCommandLineArguments() {
431
436
@ Test
432
437
public void testGetDefaultCTR () {
433
438
ScheduleRequest request = getScheduleRequest (new ArrayList <>(), "springcloudtask/composed-task-runner:latest" , "1: timestamp && 2: timestamp" );
434
- AppDefinition definition = request .getDefinition ();
435
439
assertEquals ("Docker Resource [docker:springcloudtask/composed-task-runner:latest]" , request .getResource ().toString ());
436
440
}
441
+ @ Test
442
+ public void testVersionWithResource () {
443
+ String validVersionNumber = "3.0.0" ;
444
+ ScheduleRequest request = scheduleRequest (validVersionNumber );
445
+ assertThat (request .getResource ().toString ()).contains ("file:src/test/resources/apps/foo-task" );
446
+ }
447
+
448
+ @ Test
449
+ public void testVersionWithResourceInvalidVersion () {
450
+ String invalidVersionNumber = "2.0.0" ;
451
+ assertThatIllegalArgumentException ()
452
+ .isThrownBy (() -> {
453
+ scheduleRequest (invalidVersionNumber );
454
+ }).withMessage ("Unknown task app: demo" );
455
+ }
456
+
457
+ private ScheduleRequest scheduleRequest (String appVersionToTest ) {
458
+ String definition = "demo" ;
459
+ Map <String , String > resourceTestProps = new HashMap <>(testProperties );
460
+ resourceTestProps .put ("version.demo" , appVersionToTest );
461
+ AppRegistryService mockAppRegistryService = mock (AppRegistryService .class );
462
+ TaskDefinition taskDefinition = new TaskDefinition (BASE_DEFINITION_NAME , definition );
463
+ AppRegistration demoRegistration = new AppRegistration ();
464
+ demoRegistration .setName (DEMO_APP_NAME );
465
+
466
+ when (mockAppRegistryService .find (taskDefinition .getRegisteredAppName (), ApplicationType .task , "3.0.0" ))
467
+ .thenReturn (demoRegistration );
468
+ return getScheduleRequest (new ArrayList <>(),
469
+ "springcloudtask/composed-task-runner:latest" ,
470
+ definition , resourceTestProps , mockAppRegistryService );
471
+ }
437
472
438
473
private List <String > getCommandLineArguments (List <String > commandLineArguments ) {
439
474
return getScheduleRequest (commandLineArguments ,"springcloudtask/timestamp-task:latest" , "timestamp" ).getCommandlineArguments ();
440
475
}
441
476
442
477
private ScheduleRequest getScheduleRequest (List <String > commandLineArguments , String resourceToReturn , String definition ) {
478
+ AppRegistryService mockAppRegistryService = mock (AppRegistryService .class );
479
+ return getScheduleRequest (commandLineArguments , resourceToReturn , definition , this .testProperties , mockAppRegistryService );
480
+ }
481
+ private ScheduleRequest getScheduleRequest (List <String > commandLineArguments , String resourceToReturn , String definition , Map <String , String > testProperties , AppRegistryService appRegistryService ) {
443
482
Scheduler mockScheduler = mock (SimpleTestScheduler .class );
444
483
TaskDefinitionRepository mockTaskDefinitionRepository = mock (TaskDefinitionRepository .class );
445
- AppRegistryService mockAppRegistryService = mock (AppRegistryService .class );
446
484
447
485
Launcher launcher = new Launcher ("default" , "defaultType" , null , mockScheduler );
448
486
List <Launcher > launchers = new ArrayList <>();
@@ -452,7 +490,7 @@ private ScheduleRequest getScheduleRequest(List<String> commandLineArguments, St
452
490
mock (CommonApplicationProperties .class ),
453
491
taskPlatform ,
454
492
mockTaskDefinitionRepository ,
455
- mockAppRegistryService ,
493
+ appRegistryService ,
456
494
mock (ResourceLoader .class ),
457
495
this .taskConfigurationProperties ,
458
496
mock (DataSourceProperties .class ),
@@ -470,10 +508,20 @@ private ScheduleRequest getScheduleRequest(List<String> commandLineArguments, St
470
508
TaskDefinition taskDefinition = new TaskDefinition (BASE_DEFINITION_NAME , definition );
471
509
472
510
when (mockTaskDefinitionRepository .findById (BASE_DEFINITION_NAME )).thenReturn (Optional .of (taskDefinition ));
473
- when (mockAppRegistryService .getAppResource (any ())).thenReturn (new DockerResource (resourceToReturn ));
474
- when (mockAppRegistryService .find (taskDefinition .getRegisteredAppName (), ApplicationType .task ))
511
+ doAnswer ((Answer <Resource >) invocation -> {
512
+ AppRegistration appRegistration = invocation .getArgument (0 , AppRegistration .class );
513
+ String name = appRegistration .getName ();
514
+ Resource resource = new DockerResource (resourceToReturn );
515
+ if (name != null && name .equals (DEMO_APP_NAME )) {
516
+ resource = new FileSystemResource ("file:src/test/resources/apps/foo-task" );
517
+ }
518
+ return resource ;
519
+ }).when (appRegistryService ).getAppResource (any ());
520
+ when (appRegistryService .find (taskDefinition .getRegisteredAppName (), ApplicationType .task ))
475
521
.thenReturn (new AppRegistration ());
476
- mockSchedulerService .schedule (BASE_SCHEDULE_NAME , BASE_DEFINITION_NAME , this .testProperties ,
522
+
523
+
524
+ mockSchedulerService .schedule (BASE_SCHEDULE_NAME , BASE_DEFINITION_NAME , testProperties ,
477
525
commandLineArguments , null );
478
526
479
527
ArgumentCaptor <ScheduleRequest > scheduleRequestArgumentCaptor = ArgumentCaptor .forClass (ScheduleRequest .class );
0 commit comments