39
39
import org .springframework .cloud .dataflow .acceptance .test .util .TestConfigurationProperties ;
40
40
import org .springframework .cloud .dataflow .rest .client .AppRegistryOperations ;
41
41
import org .springframework .cloud .dataflow .rest .client .DataFlowTemplate ;
42
+ import org .springframework .cloud .dataflow .rest .client .SchedulerOperations ;
42
43
import org .springframework .cloud .dataflow .rest .client .TaskOperations ;
44
+ import org .springframework .cloud .dataflow .rest .resource .ScheduleInfoResource ;
43
45
import org .springframework .cloud .dataflow .rest .resource .TaskDefinitionResource ;
44
46
import org .springframework .cloud .dataflow .rest .resource .TaskExecutionResource ;
45
47
import org .springframework .hateoas .PagedResources ;
46
48
import org .springframework .test .context .junit4 .SpringRunner ;
47
49
import org .springframework .web .client .RestTemplate ;
48
50
51
+ import static org .assertj .core .api .Assertions .assertThat ;
52
+
49
53
/**
50
54
* Abstract base class that is used by task acceptance tests. This class contains commonly
51
55
* used utility methods for task acceptance tests as well as the ability to retrieve
58
62
@ EnableConfigurationProperties (TestConfigurationProperties .class )
59
63
public abstract class AbstractTaskTests implements InitializingBean {
60
64
65
+ protected static final String DEFAULT_CRON_EXPRESSION_KEY = "spring.cloud.scheduler.cron.expression" ;
66
+
61
67
private static final Logger logger = LoggerFactory .getLogger (AbstractTaskTests .class );
62
68
63
69
private static boolean tasksRegistered = false ;
@@ -69,6 +75,8 @@ public abstract class AbstractTaskTests implements InitializingBean {
69
75
70
76
protected TaskOperations taskOperations ;
71
77
78
+ protected SchedulerOperations schedulerOperations ;
79
+
72
80
protected AppRegistryOperations appRegistryOperations ;
73
81
74
82
@ Autowired
@@ -123,6 +131,18 @@ protected String taskLaunch(String definition,
123
131
return taskDefinitionName ;
124
132
}
125
133
134
+ /**
135
+ * Creates a unique task definition name from a UUID.
136
+ *
137
+ * @param definition The definition to test.
138
+ * @return The name of the task associated with this launch.
139
+ */
140
+ protected String taskCreate (String definition ) {
141
+ String taskDefinitionName = "task-" + UUID .randomUUID ().toString ();
142
+ taskOperations .create (taskDefinitionName , definition );
143
+ return taskDefinitionName ;
144
+ }
145
+
126
146
/**
127
147
* Launch an existing task definition.
128
148
*/
@@ -162,6 +182,7 @@ public void afterPropertiesSet() {
162
182
DataFlowTemplate dataFlowOperationsTemplate = new DataFlowTemplate (
163
183
new URI (configurationProperties .getServerUri ()));
164
184
taskOperations = dataFlowOperationsTemplate .taskOperations ();
185
+ schedulerOperations = dataFlowOperationsTemplate .schedulerOperations ();
165
186
appRegistryOperations = dataFlowOperationsTemplate .appRegistryOperations ();
166
187
}
167
188
catch (URISyntaxException uriException ) {
@@ -226,6 +247,75 @@ protected List<TaskExecutionResource> getTaskExecutionResource(
226
247
return result ;
227
248
}
228
249
250
+ /**
251
+ * Creates a unique schedule name from a UUID from an existing task definition.
252
+ *
253
+ * @param taskDefinitionName The definition to test.
254
+ * @param properties Map containing deployment properties for the task.
255
+ * @param arguments List containing the arguments used to execute the task.
256
+ * @return The name of the schedule.
257
+ */
258
+ protected String schedule (String taskDefinitionName ,
259
+ Map <String , String > properties , List <String > arguments ) {
260
+ String scheduleName = "schedule-" + UUID .randomUUID ().toString ();
261
+ this .schedulerOperations .schedule (scheduleName , taskDefinitionName , properties , arguments );
262
+ return scheduleName ;
263
+ }
264
+
265
+ /**
266
+ * Verifies that the scheduleName specified exists in the schedule list results.
267
+ * @param scheduleName the name of the schedule to search.
268
+ * @return true if found else false;
269
+ */
270
+
271
+ protected boolean verifyScheduleExists (String scheduleName ) {
272
+ boolean result = false ;
273
+ for (ScheduleInfoResource resource : this .schedulerOperations .list ()) {
274
+ if (resource .getScheduleName ().equals (scheduleName )){
275
+ result = true ;
276
+ break ;
277
+ }
278
+ }
279
+ return result ;
280
+ }
281
+
282
+ /**
283
+ * Asserts that the {@link ScheduleInfoResource} contains the expected data.
284
+ * @param scheduleInfoResource the {@link ScheduleInfoResource} that needs to be interrogated.
285
+ * @param scheduleName The expected name of the schedule.
286
+ * @param cronExpression The expected expression.
287
+ */
288
+ protected void verifyScheduleIsValid (ScheduleInfoResource scheduleInfoResource , String scheduleName , String cronExpression ) {
289
+ assertThat (scheduleInfoResource .getScheduleName ()).isEqualTo (scheduleName );
290
+ assertThat (scheduleInfoResource .getScheduleProperties ().containsKey (DEFAULT_CRON_EXPRESSION_KEY )).isTrue ();
291
+ assertThat (scheduleInfoResource .getScheduleProperties ().get (DEFAULT_CRON_EXPRESSION_KEY )).isEqualTo (cronExpression );
292
+ }
293
+
294
+ /**
295
+ * Retrieves a {@link PagedResources} of the existing schedules.
296
+ */
297
+ protected PagedResources <ScheduleInfoResource > listSchedules () {
298
+ return this .schedulerOperations .list ();
299
+ }
300
+
301
+ /**
302
+ * Retrieves a {@link PagedResources} of the existing {@link ScheduleInfoResource}s that are
303
+ * associated with the task definition name.
304
+ * @param taskDefinitionName The name of the task definition that the schedules should be associated.
305
+ */
306
+ protected PagedResources <ScheduleInfoResource > listSchedules (String taskDefinitionName ) {
307
+ return this .schedulerOperations .list (taskDefinitionName );
308
+ }
309
+
310
+ /**
311
+ * Deletes an existing schedule based on the schedule name.
312
+ *
313
+ * @param scheduleName the name of the schedule instance to be unscheduled.
314
+ */
315
+ protected void unschedule (String scheduleName ) {
316
+ this .schedulerOperations .unschedule (scheduleName );
317
+ }
318
+
229
319
public enum TaskTestTypes {
230
320
TIMESTAMP ,
231
321
CORE
0 commit comments