3131import org .mockito .ArgumentCaptor ;
3232import org .mockito .Mockito ;
3333
34+ import org .mockito .stubbing .Answer ;
3435import org .springframework .beans .factory .annotation .Autowired ;
3536import org .springframework .boot .autoconfigure .context .PropertyPlaceholderAutoConfiguration ;
3637import org .springframework .boot .autoconfigure .jdbc .DataSourceProperties ;
6061import org .springframework .cloud .dataflow .server .service .SchedulerServiceProperties ;
6162import org .springframework .cloud .dataflow .server .service .TaskExecutionInfoService ;
6263import org .springframework .cloud .deployer .resource .docker .DockerResource ;
63- import org .springframework .cloud .deployer .spi .core .AppDefinition ;
6464import org .springframework .cloud .deployer .spi .scheduler .CreateScheduleException ;
6565import org .springframework .cloud .deployer .spi .scheduler .ScheduleInfo ;
6666import org .springframework .cloud .deployer .spi .scheduler .ScheduleRequest ;
6767import org .springframework .cloud .deployer .spi .scheduler .Scheduler ;
6868import org .springframework .cloud .deployer .spi .task .TaskLauncher ;
6969import org .springframework .cloud .task .listener .TaskException ;
7070import org .springframework .core .env .PropertyResolver ;
71+ import org .springframework .core .io .FileSystemResource ;
7172import org .springframework .core .io .Resource ;
7273import org .springframework .core .io .ResourceLoader ;
7374import org .springframework .data .domain .Page ;
7475import org .springframework .data .domain .PageRequest ;
7576import org .springframework .test .annotation .DirtiesContext ;
7677
7778import static org .assertj .core .api .Assertions .assertThat ;
79+ import static org .assertj .core .api .Assertions .assertThatIllegalArgumentException ;
7880import static org .junit .jupiter .api .Assertions .assertEquals ;
7981import static org .junit .jupiter .api .Assertions .assertThrows ;
8082import static org .mockito .ArgumentMatchers .any ;
8183import static org .mockito .ArgumentMatchers .anyString ;
84+ import static org .mockito .Mockito .doAnswer ;
8285import static org .mockito .Mockito .mock ;
8386import static org .mockito .Mockito .verify ;
8487import static org .mockito .Mockito .when ;
@@ -105,7 +108,9 @@ public class DefaultSchedulerServiceTests {
105108
106109 private static final String BASE_DEFINITION_NAME = "myTaskDefinition" ;
107110
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" ;
109114
110115 @ Autowired
111116 private Scheduler simpleTestScheduler ;
@@ -431,18 +436,51 @@ public void testScheduleWithoutCommandLineArguments() {
431436 @ Test
432437 public void testGetDefaultCTR () {
433438 ScheduleRequest request = getScheduleRequest (new ArrayList <>(), "springcloudtask/composed-task-runner:latest" , "1: timestamp && 2: timestamp" );
434- AppDefinition definition = request .getDefinition ();
435439 assertEquals ("Docker Resource [docker:springcloudtask/composed-task-runner:latest]" , request .getResource ().toString ());
436440 }
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+ }
437472
438473 private List <String > getCommandLineArguments (List <String > commandLineArguments ) {
439474 return getScheduleRequest (commandLineArguments ,"springcloudtask/timestamp-task:latest" , "timestamp" ).getCommandlineArguments ();
440475 }
441476
442477 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 ) {
443482 Scheduler mockScheduler = mock (SimpleTestScheduler .class );
444483 TaskDefinitionRepository mockTaskDefinitionRepository = mock (TaskDefinitionRepository .class );
445- AppRegistryService mockAppRegistryService = mock (AppRegistryService .class );
446484
447485 Launcher launcher = new Launcher ("default" , "defaultType" , null , mockScheduler );
448486 List <Launcher > launchers = new ArrayList <>();
@@ -452,7 +490,7 @@ private ScheduleRequest getScheduleRequest(List<String> commandLineArguments, St
452490 mock (CommonApplicationProperties .class ),
453491 taskPlatform ,
454492 mockTaskDefinitionRepository ,
455- mockAppRegistryService ,
493+ appRegistryService ,
456494 mock (ResourceLoader .class ),
457495 this .taskConfigurationProperties ,
458496 mock (DataSourceProperties .class ),
@@ -470,10 +508,20 @@ private ScheduleRequest getScheduleRequest(List<String> commandLineArguments, St
470508 TaskDefinition taskDefinition = new TaskDefinition (BASE_DEFINITION_NAME , definition );
471509
472510 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 ))
475521 .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 ,
477525 commandLineArguments , null );
478526
479527 ArgumentCaptor <ScheduleRequest > scheduleRequestArgumentCaptor = ArgumentCaptor .forClass (ScheduleRequest .class );
0 commit comments