This repository has been archived by the owner on Jul 1, 2022. It is now read-only.
forked from eclipse/kapua
/
JobServiceSteps.java
2006 lines (1653 loc) · 78.9 KB
/
JobServiceSteps.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
* Copyright (c) 2017, 2020 Eurotech and/or its affiliates and others
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Eurotech - initial API and implementation
* Red Hat Inc
*******************************************************************************/
package org.eclipse.kapua.service.job.steps;
import cucumber.api.Scenario;
import cucumber.api.java.After;
import cucumber.api.java.Before;
import cucumber.api.java.en.And;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import cucumber.runtime.java.guice.ScenarioScoped;
import org.apache.shiro.SecurityUtils;
import org.eclipse.kapua.KapuaException;
import org.eclipse.kapua.commons.security.KapuaSecurityUtils;
import org.eclipse.kapua.commons.security.KapuaSession;
import org.eclipse.kapua.commons.util.xml.XmlUtil;
import org.eclipse.kapua.job.engine.JobEngineFactory;
import org.eclipse.kapua.job.engine.JobEngineService;
import org.eclipse.kapua.job.engine.JobStartOptions;
import org.eclipse.kapua.locator.KapuaLocator;
import org.eclipse.kapua.model.id.KapuaId;
import org.eclipse.kapua.model.query.predicate.AttributePredicate;
import org.eclipse.kapua.model.query.predicate.AttributePredicate.Operator;
import org.eclipse.kapua.qa.common.DBHelper;
import org.eclipse.kapua.qa.common.StepData;
import org.eclipse.kapua.qa.common.TestBase;
import org.eclipse.kapua.qa.common.TestJAXBContextProvider;
import org.eclipse.kapua.qa.common.cucumber.CucConfig;
import org.eclipse.kapua.qa.common.cucumber.CucJobStepProperty;
import org.eclipse.kapua.service.device.registry.Device;
import org.eclipse.kapua.service.job.Job;
import org.eclipse.kapua.service.job.JobAttributes;
import org.eclipse.kapua.service.job.JobCreator;
import org.eclipse.kapua.service.job.JobFactory;
import org.eclipse.kapua.service.job.JobListResult;
import org.eclipse.kapua.service.job.JobQuery;
import org.eclipse.kapua.service.job.JobService;
import org.eclipse.kapua.service.job.execution.JobExecution;
import org.eclipse.kapua.service.job.execution.JobExecutionAttributes;
import org.eclipse.kapua.service.job.execution.JobExecutionCreator;
import org.eclipse.kapua.service.job.execution.JobExecutionFactory;
import org.eclipse.kapua.service.job.execution.JobExecutionListResult;
import org.eclipse.kapua.service.job.execution.JobExecutionQuery;
import org.eclipse.kapua.service.job.execution.JobExecutionService;
import org.eclipse.kapua.service.job.step.JobStep;
import org.eclipse.kapua.service.job.step.JobStepAttributes;
import org.eclipse.kapua.service.job.step.JobStepCreator;
import org.eclipse.kapua.service.job.step.JobStepFactory;
import org.eclipse.kapua.service.job.step.JobStepListResult;
import org.eclipse.kapua.service.job.step.JobStepQuery;
import org.eclipse.kapua.service.job.step.JobStepService;
import org.eclipse.kapua.service.job.step.definition.JobStepDefinition;
import org.eclipse.kapua.service.job.step.definition.JobStepDefinitionCreator;
import org.eclipse.kapua.service.job.step.definition.JobStepDefinitionFactory;
import org.eclipse.kapua.service.job.step.definition.JobStepDefinitionQuery;
import org.eclipse.kapua.service.job.step.definition.JobStepDefinitionService;
import org.eclipse.kapua.service.job.step.definition.JobStepProperty;
import org.eclipse.kapua.service.job.step.definition.JobStepType;
import org.eclipse.kapua.service.job.targets.JobTarget;
import org.eclipse.kapua.service.job.targets.JobTargetAttributes;
import org.eclipse.kapua.service.job.targets.JobTargetCreator;
import org.eclipse.kapua.service.job.targets.JobTargetFactory;
import org.eclipse.kapua.service.job.targets.JobTargetListResult;
import org.eclipse.kapua.service.job.targets.JobTargetQuery;
import org.eclipse.kapua.service.job.targets.JobTargetService;
import org.eclipse.kapua.service.job.targets.JobTargetStatus;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.inject.Inject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
// ****************************************************************************************
// * Implementation of Gherkin steps used in JobService.feature scenarios. *
// * *
// * MockedLocator is used for Location Service. Mockito is used to mock other *
// * services that the Account services dependent on. Dependent services are: *
// * - Authorization Service *
// ****************************************************************************************
@ScenarioScoped
public class JobServiceSteps extends TestBase {
private static final Logger LOGGER = LoggerFactory.getLogger(JobServiceSteps.class);
private static final String COUNT = "Count";
private static final String CURRENT_JOB_ID = "CurrentJobId";
private static final String CURRENT_JOB_STEP_DEFINITION_ID = "CurrentJobStepDefinitionId";
private static final String CURRENT_STEP_ID = "CurrentStepId";
private static final String JOB_CREATOR = "JobCreator";
private static final String JOB_EXECUTION = "JobExecution";
private static final String JOB_EXECUTION_LIST = "JobExecutionList";
private static final String JOB_NAME = "jobName";
private static final String JOB_STEP = "JobStep";
private static final String JOB_STEP_CREATOR = "JobStepCreator";
private static final String JOB_STEP_DEFINITION = "JobStepDefinition";
private static final String JOB_STEP_DEFINITIONS = "JobSetpDefinitions";
private static final String JOB_STEP_DEFINITION_CREATOR = "JobStepDefinitionCreator";
private static final String JOB_TARGET = "JobTarget";
private static final String JOB_TARGET_CREATOR = "JobTargetCreator";
private static final String JOB_TARGET_LIST = "JobTargetList";
private static final String TEST_JOB = "Test job";
// Job service objects
private JobFactory jobFactory;
private JobService jobService;
// Job Step definition service objects
private JobStepDefinitionService jobStepDefinitionService;
private JobStepDefinitionFactory jobStepDefinitionFactory;
// Job Step service objects
private JobStepService jobStepService;
private JobStepFactory jobStepFactory;
// Job Target service objects
private JobTargetService jobTargetService;
private JobTargetFactory jobTargetFactory;
// Job Execution service objects
private JobExecutionService jobExecutionService;
private JobExecutionFactory jobExecutionFactory;
//Job Engine Service objects
private JobEngineService jobEngineService;
private JobEngineFactory jobEngineFactory;
private static final int SECONDS_TO_WAIT = 100;
private static final int SECONDS_TO_TRY = 2;
// Default constructor
@Inject
public JobServiceSteps(StepData stepData, DBHelper dbHelper) {
this.stepData = stepData;
this.database = dbHelper;
}
// ************************************************************************************
// ************************************************************************************
// * Definition of Cucumber scenario steps *
// ************************************************************************************
// ************************************************************************************
// ************************************************************************************
// * Setup and tear-down steps *
// ************************************************************************************
@Before
public void beforeScenario(Scenario scenario) {
this.scenario = scenario;
database.setup();
stepData.clear();
locator = KapuaLocator.getInstance();
jobService = locator.getService(JobService.class);
jobFactory = locator.getFactory(JobFactory.class);
jobStepDefinitionService = locator.getService(JobStepDefinitionService.class);
jobStepDefinitionFactory = locator.getFactory(JobStepDefinitionFactory.class);
jobStepService = locator.getService(JobStepService.class);
jobStepFactory = locator.getFactory(JobStepFactory.class);
jobTargetService = locator.getService(JobTargetService.class);
jobTargetFactory = locator.getFactory(JobTargetFactory.class);
jobExecutionService = locator.getService(JobExecutionService.class);
jobExecutionFactory = locator.getFactory(JobExecutionFactory.class);
jobEngineService = locator.getService(JobEngineService.class);
jobEngineFactory = locator.getFactory(JobEngineFactory.class);
if (isUnitTest()) {
// Create KapuaSession using KapuaSecurtiyUtils and kapua-sys user as logged in user.
// All operations on database are performed using system user.
// Only for unit tests. Integration tests assume that a real logon is performed.
KapuaSession kapuaSession = new KapuaSession(null, SYS_SCOPE_ID, SYS_USER_ID);
KapuaSecurityUtils.setSession(kapuaSession);
}
// Setup JAXB context
XmlUtil.setContextProvider(new TestJAXBContextProvider());
}
@After
public void afterScenario() {
// Clean up the database
try {
LOGGER.info("Logging out in cleanup");
if (isIntegrationTest()) {
database.deleteAll();
SecurityUtils.getSubject().logout();
} else {
database.dropAll();
database.close();
}
KapuaSecurityUtils.clearSession();
} catch (Exception e) {
LOGGER.error("Failed to log out in @After", e);
}
}
// ************************************************************************************
// ************************************************************************************
// * Cucumber Test steps *
// ************************************************************************************
// ************************************************************************************
// ************************************************************************************
// * Job service steps *
// ************************************************************************************
@When("^I configure the job service$")
public void setJobServiceConfigurationValue(List<CucConfig> cucConfigs) throws Exception {
Map<String, Object> valueMap = new HashMap<>();
KapuaId accId = getCurrentScopeId();
KapuaId scopeId = getCurrentParentId();
for (CucConfig config : cucConfigs) {
config.addConfigToMap(valueMap);
if (config.getParentId() != null) {
scopeId = getKapuaId(config.getParentId());
}
if (config.getScopeId() != null) {
accId = getKapuaId(config.getScopeId());
}
}
primeException();
try {
jobService.setConfigValues(accId, scopeId, valueMap);
} catch (KapuaException ke) {
verifyException(ke);
}
}
@Given("^A regular job creator with the name \"(.+)\"$")
public void prepareARegularJobCreator(String name) {
JobCreator jobCreator = jobFactory.newCreator(getCurrentScopeId());
jobCreator.setName(name);
jobCreator.setDescription(TEST_JOB);
stepData.put(JOB_CREATOR, jobCreator);
}
@Given("^A job creator with a null name$")
public void prepareAJobCreatorWithNullName() {
JobCreator jobCreator = jobFactory.newCreator(getCurrentScopeId());
jobCreator.setName(null);
jobCreator.setDescription(TEST_JOB);
stepData.put(JOB_CREATOR, jobCreator);
}
@Given("^A job creator with an empty name$")
public void prepareAJobCreatorWithAnEmptyName() {
JobCreator jobCreator = jobFactory.newCreator(getCurrentScopeId());
jobCreator.setName("");
jobCreator.setDescription(TEST_JOB);
stepData.put(JOB_CREATOR, jobCreator);
}
@Given("^I create a job with the name \"(.+)\"$")
public void createANamedJob(String name)
throws Exception {
prepareARegularJobCreator(name);
JobCreator jobCreator = (JobCreator) stepData.get(JOB_CREATOR);
primeException();
try {
stepData.remove("Job");
stepData.remove(CURRENT_JOB_ID);
Job job = jobService.create(jobCreator);
stepData.put("Job", job);
stepData.put(CURRENT_JOB_ID, job.getId());
} catch (KapuaException ex) {
verifyException(ex);
}
}
@Given("^I create (\\d+) job items$")
public void createANumberOfJobs(int num)
throws Exception {
primeException();
try {
for (int i = 0; i < num; i++) {
JobCreator tmpCreator = jobFactory.newCreator(getCurrentScopeId());
tmpCreator.setName(String.format("TestJobNum%d", i));
tmpCreator.setDescription("TestJobDescription");
jobService.create(tmpCreator);
}
} catch (KapuaException ex) {
verifyException(ex);
}
}
@Given("^I create (\\d+) job items with the name \"(.+)\"$")
public void createANumberOfJobsWithName(int num, String name)
throws Exception {
JobCreator tmpCreator = jobFactory.newCreator(getCurrentScopeId());
tmpCreator.setDescription("TestJobDescription");
primeException();
try {
for (int i = 0; i < num; i++) {
tmpCreator.setName(name + "_" + i);
jobService.create(tmpCreator);
}
} catch (KapuaException ex) {
verifyException(ex);
}
}
@When("^I create a new job entity from the existing creator$")
public void createJobFromCreator()
throws Exception {
JobCreator jobCreator = (JobCreator) stepData.get(JOB_CREATOR);
primeException();
try {
stepData.remove("Job");
stepData.remove(CURRENT_JOB_ID);
Job job = jobService.create(jobCreator);
stepData.put("Job", job);
stepData.put(CURRENT_JOB_ID, job.getId());
} catch (KapuaException ex) {
verifyException(ex);
}
}
@When("^I change the job name to \"(.+)\"$")
public void updateExistingJobName(String newName)
throws Exception {
Job oldJob = (Job) stepData.get("Job");
oldJob.setName(newName);
primeException();
try {
stepData.remove("Job");
Job newJob = jobService.update(oldJob);
stepData.put("Job", newJob);
} catch (KapuaException ex) {
verifyException(ex);
}
}
@When("^I change the job description to \"(.+)\"$")
public void updateExistingJobDescription(String newDescription)
throws Exception {
Job oldJob = (Job) stepData.get("Job");
oldJob.setDescription(newDescription);
primeException();
try {
stepData.remove("Job");
Job newJob = jobService.update(oldJob);
stepData.put("Job", newJob);
} catch (KapuaException ex) {
verifyException(ex);
}
}
@When("^I change the job XML definition to \"(.+)\"$")
public void updateExistingJobXMLDefinition(String newDefinition)
throws Exception {
Job oldJob = (Job) stepData.get("Job");
oldJob.setJobXmlDefinition(newDefinition);
primeException();
try {
stepData.remove("Job");
Job newJob = jobService.update(oldJob);
stepData.put("Job", newJob);
} catch (KapuaException ex) {
verifyException(ex);
}
}
@When("^I add the current step to the last job$")
public void updateJobWithSteps()
throws Exception {
Job oldJob = (Job) stepData.get("Job");
List<JobStep> tmpStepList = oldJob.getJobSteps();
JobStep step = (JobStep) stepData.get("Step");
tmpStepList.add(step);
oldJob.setJobSteps(tmpStepList);
primeException();
try {
stepData.remove("Job");
Job newJob = jobService.update(oldJob);
stepData.put("Job", newJob);
} catch (KapuaException ex) {
verifyException(ex);
}
}
@When("^I delete the job$")
public void deleteJobFromDatabase()
throws Exception {
Job job = (Job) stepData.get("Job");
primeException();
try {
jobService.delete(job.getScopeId(), job.getId());
} catch (KapuaException ex) {
verifyException(ex);
}
}
@When("^I search for the job in the database$")
public void findJobInDatabase()
throws Exception {
KapuaId currentJobId = (KapuaId) stepData.get(CURRENT_JOB_ID);
primeException();
try {
stepData.remove("Job");
Job job = jobService.find(getCurrentScopeId(), currentJobId);
stepData.put("Job", job);
} catch (KapuaException ex) {
verifyException(ex);
}
}
@When("^I count the jobs in the database$")
public void countJobsInDatabase()
throws Exception {
JobQuery tmpQuery = jobFactory.newQuery(getCurrentScopeId());
primeException();
try {
stepData.remove(COUNT);
Long count = jobService.count(tmpQuery);
stepData.put(COUNT, count);
} catch (KapuaException ex) {
verifyException(ex);
}
}
@When("^I query for jobs in scope (\\d+)$")
public void countJobsInScope(int id)
throws Exception {
JobQuery tmpQuery = jobFactory.newQuery(getKapuaId(id));
primeException();
try {
stepData.remove(COUNT);
JobListResult jobList = jobService.query(tmpQuery);
Long count = (long) jobList.getSize();
stepData.put(COUNT, count);
} catch (KapuaException ex) {
verifyException(ex);
}
}
@When("^I count the jobs with the name starting with \"(.+)\"$")
public void countJobsWithName(String name)
throws Exception {
JobQuery tmpQuery = jobFactory.newQuery(getCurrentScopeId());
tmpQuery.setPredicate(tmpQuery.attributePredicate(JobAttributes.NAME, name, Operator.STARTS_WITH));
primeException();
try {
stepData.remove(COUNT);
JobListResult jobList = jobService.query(tmpQuery);
Long count = (long) jobList.getSize();
stepData.put(COUNT, count);
} catch (KapuaException ex) {
verifyException(ex);
}
}
@When("^I query for the job with the name \"(.+)\"$")
public void queryForJobWithName(String name)
throws Exception {
JobQuery tmpQuery = jobFactory.newQuery(getCurrentScopeId());
tmpQuery.setPredicate(tmpQuery.attributePredicate(JobAttributes.NAME, name));
primeException();
try {
stepData.remove("Job");
Job job = jobService.query(tmpQuery).getFirstItem();
stepData.put("Job", job);
assertEquals(name, job.getName());
} catch (KapuaException ex) {
verifyException(ex);
}
}
@Then("^The job entity matches the creator$")
public void checkJobAgainstCreator() {
Job job = (Job) stepData.get("Job");
JobCreator jobCreator = (JobCreator) stepData.get(JOB_CREATOR);
assertEquals("The job scope does not match the creator.", jobCreator.getScopeId(), job.getScopeId());
assertEquals("The job name does not match the creator.", jobCreator.getName(), job.getName());
assertEquals("The job description does not match the creator.", jobCreator.getDescription(), job.getDescription());
}
@Then("^The job has (\\d+) steps$")
public void checkNumberOfJobSteps(int num) {
Job job = (Job) stepData.get("Job");
assertEquals("The job item has the wrong number of steps", num, job.getJobSteps().size());
}
@Then("^I find a job item in the database$")
public void checkThatAJobWasFound() {
assertNotNull("Unexpected null value for the job.", stepData.get("Job"));
}
@Then("^There is no such job item in the database$")
public void checkThatNoJobWasFound() {
assertNull("Unexpected job item was found!", stepData.get("Job"));
}
@Then("The job name is \"(.+)\"")
public void checkJobItemName(String name) {
Job job = (Job) stepData.get("Job");
assertEquals("The job name does not match!", name, job.getName());
}
@Then("The job description is \"(.+)\"")
public void checkJobItemDescription(String description) {
Job job = (Job) stepData.get("Job");
assertEquals("The job description does not match!", description, job.getDescription());
}
@Then("The job XML definition is \"(.+)\"")
public void checkJobItemXMLDefinition(String definition) {
Job job = (Job) stepData.get("Job");
assertEquals("The job XML definition does not match!", definition, job.getJobXmlDefinition());
}
@When("^I test the sanity of the job factory$")
public void testJobFactorySanity() {
primeException();
assertNotNull("The job factory returned a null creator!", jobFactory.newCreator(SYS_SCOPE_ID));
assertNotNull("The job factory returned a null job object!", jobFactory.newEntity(SYS_SCOPE_ID));
assertNotNull("The job factory returned a null job query!", jobFactory.newQuery(SYS_SCOPE_ID));
assertNotNull("The job factory returned a null job list result!", jobFactory.newListResult());
}
// ************************************************************************************
// * Job Step Definition Test steps *
// ************************************************************************************
@Given("^A regular step definition creator with the name \"(.*)\"$")
public void prepareARegularStepDefinitionCreator(String name) {
JobStepDefinitionCreator stepDefinitionCreator = prepareDefaultJobStepDefinitionCreator();
stepDefinitionCreator.setName(name);
stepData.put(JOB_STEP_DEFINITION_CREATOR, stepDefinitionCreator);
}
@Given("^A regular definition creator with the name \"(.*)\" and (\\d+) properties$")
public void prepareARegularStepDefinitionCreatorWithProperties(String name, Integer cnt) {
JobStepDefinitionCreator stepDefinitionCreator = prepareDefaultJobStepDefinitionCreator();
stepDefinitionCreator.setName(name);
List<JobStepProperty> tmpPropLst = new ArrayList<>();
tmpPropLst.add(jobStepDefinitionFactory.newStepProperty("Property1", "Type1", null));
tmpPropLst.add(jobStepDefinitionFactory.newStepProperty("Property2", "Type2", null));
tmpPropLst.add(jobStepDefinitionFactory.newStepProperty("Property3", "Type3", null));
stepDefinitionCreator.setStepProperties(tmpPropLst);
stepData.put(JOB_STEP_DEFINITION_CREATOR, stepDefinitionCreator);
}
@Given("^A regular step definition creator with the name \"(.*)\" and the following properties$")
public void prepareARegularStepDefinitionCreatorWithPropertyList(String name, List<CucJobStepProperty> list) {
JobStepDefinitionCreator stepDefinitionCreator = prepareDefaultJobStepDefinitionCreator();
stepDefinitionCreator.setName(name);
List<JobStepProperty> tmpPropLst = new ArrayList<>();
for (CucJobStepProperty prop : list) {
tmpPropLst.add(jobStepDefinitionFactory.newStepProperty(prop.getName(), prop.getType(), null));
}
stepDefinitionCreator.setStepProperties(tmpPropLst);
stepData.put(JOB_STEP_DEFINITION_CREATOR, stepDefinitionCreator);
}
@Given("^A regular step definition with the name \"(.*)\" and the following properties$")
public void createARegularStepDefinitionWithProperties(String name, List<CucJobStepProperty> list)
throws Exception {
prepareARegularStepDefinitionCreatorWithPropertyList(name, list);
JobStepDefinitionCreator stepDefinitionCreator = (JobStepDefinitionCreator) stepData.get(JOB_STEP_DEFINITION_CREATOR);
primeException();
try {
stepData.remove(JOB_STEP_DEFINITION);
stepData.remove(CURRENT_JOB_STEP_DEFINITION_ID);
JobStepDefinition stepDefinition = jobStepDefinitionService.create(stepDefinitionCreator);
stepData.put(JOB_STEP_DEFINITION, stepDefinition);
stepData.put(CURRENT_JOB_STEP_DEFINITION_ID, stepDefinition.getId());
} catch (KapuaException ex) {
verifyException(ex);
}
}
@Given("^Search for step definition with the name \"(.*)\"$")
public void searchARegularStepDefinitionWithProperties(String name)
throws Exception {
primeException();
try {
stepData.remove(JOB_STEP_DEFINITION);
stepData.remove(CURRENT_JOB_STEP_DEFINITION_ID);
JobStepDefinition stepDefinition = jobStepDefinitionService.findByName(name);
stepData.put(JOB_STEP_DEFINITION, stepDefinition);
stepData.put(CURRENT_JOB_STEP_DEFINITION_ID, stepDefinition.getId());
} catch (KapuaException ex) {
verifyException(ex);
}
}
@Given("^I set the step definition creator name to null$")
public void setDefinitionCreatorNameToNull() {
JobStepDefinitionCreator stepDefinitionCreator = (JobStepDefinitionCreator) stepData.get(JOB_STEP_DEFINITION_CREATOR);
stepDefinitionCreator.setName(null);
stepData.put(JOB_STEP_DEFINITION_CREATOR, stepDefinitionCreator);
}
@Given("^I set the step definition creator processor name to \"(.+)\"$")
public void setDefinitionCreatorProcessorNameTo(String name) {
JobStepDefinitionCreator stepDefinitionCreator = (JobStepDefinitionCreator) stepData.get(JOB_STEP_DEFINITION_CREATOR);
stepDefinitionCreator.setProcessorName(name);
stepData.put(JOB_STEP_DEFINITION_CREATOR, stepDefinitionCreator);
}
@Given("^I set the step definition creator processor name to null$")
public void setDefinitionCreatorProcessorNameToNull() {
JobStepDefinitionCreator stepDefinitionCreator = (JobStepDefinitionCreator) stepData.get(JOB_STEP_DEFINITION_CREATOR);
stepDefinitionCreator.setProcessorName(null);
stepData.put(JOB_STEP_DEFINITION_CREATOR, stepDefinitionCreator);
}
@Given("^I create (\\d+) step definition items$")
public void createANumberOfStepDefinitions(Integer num)
throws Exception {
JobStepDefinitionCreator tmpCreator;
primeException();
try {
for (int i = 0; i < num; i++) {
tmpCreator = jobStepDefinitionFactory.newCreator(getCurrentScopeId());
tmpCreator.setName(String.format("TestStepDefinitionNum%d", random.nextLong()));
tmpCreator.setProcessorName("TestStepProcessor");
tmpCreator.setStepType(JobStepType.TARGET);
jobStepDefinitionService.create(tmpCreator);
}
} catch (KapuaException ex) {
verifyException(ex);
}
}
@When("^I create a new step definition entity from the existing creator$")
public void createAStepDefinitionFromTheCreator()
throws Exception {
JobStepDefinitionCreator stepDefinitionCreator = (JobStepDefinitionCreator) stepData.get(JOB_STEP_DEFINITION_CREATOR);
primeException();
try {
stepData.remove(JOB_STEP_DEFINITION);
stepData.remove(CURRENT_JOB_STEP_DEFINITION_ID);
JobStepDefinition stepDefinition = jobStepDefinitionService.create(stepDefinitionCreator);
stepData.put(JOB_STEP_DEFINITION, stepDefinition);
stepData.put(CURRENT_JOB_STEP_DEFINITION_ID, stepDefinition.getId());
} catch (KapuaException ex) {
verifyException(ex);
}
}
@When("^I search for the step definition in the database$")
public void findTheExistingStepDefinitionInTheDatabase()
throws Exception {
KapuaId currentStepDefId = (KapuaId) stepData.get(CURRENT_JOB_STEP_DEFINITION_ID);
primeException();
try {
stepData.remove(JOB_STEP_DEFINITION);
JobStepDefinition stepDefinition = jobStepDefinitionService.find(getCurrentScopeId(), currentStepDefId);
stepData.put(JOB_STEP_DEFINITION, stepDefinition);
} catch (KapuaException ex) {
verifyException(ex);
}
}
@When("^I count the step definition in the database$")
public void countStepDefinitionInDatabase()
throws Exception {
JobStepDefinitionQuery tmpQuery = jobStepDefinitionFactory.newQuery(getCurrentScopeId());
primeException();
try {
stepData.remove(COUNT);
Long itemCount = jobStepDefinitionService.count(tmpQuery);
stepData.put(COUNT, itemCount);
} catch (KapuaException ex) {
verifyException(ex);
}
}
@When("^I query for step definitions in scope (\\d+)$")
public void countStepDefinitijonsInScope(Integer id)
throws Exception {
JobStepDefinitionQuery tmpQuery = jobStepDefinitionFactory.newQuery(getKapuaId(id));
primeException();
try {
stepData.remove(COUNT);
Long itemCount = (long) jobStepDefinitionService.query(tmpQuery).getSize();
stepData.put(COUNT, itemCount);
} catch (KapuaException ex) {
verifyException(ex);
}
}
@When("^I delete the step definition$")
public void deleteExistingStepDefinition()
throws Exception {
KapuaId currentStepDefId = (KapuaId) stepData.get(CURRENT_JOB_STEP_DEFINITION_ID);
primeException();
try {
jobStepDefinitionService.delete(getCurrentScopeId(), currentStepDefId);
} catch (KapuaException ex) {
verifyException(ex);
}
}
@When("^I change the step definition name to \"(.+)\"$")
public void changeExistingStepDefinitionName(String name)
throws Exception {
JobStepDefinition stepDefinition = (JobStepDefinition) stepData.get(JOB_STEP_DEFINITION);
primeException();
try {
stepDefinition.setName(name);
stepDefinition = jobStepDefinitionService.update(stepDefinition);
stepData.put(JOB_STEP_DEFINITION, stepDefinition);
} catch (KapuaException ex) {
verifyException(ex);
}
}
@When("^I change the step definition processor name to \"(.+)\"$")
public void changeExistingStepDefinitionProcessor(String name)
throws Exception {
JobStepDefinition stepDefinition = (JobStepDefinition) stepData.get(JOB_STEP_DEFINITION);
primeException();
try {
stepDefinition.setProcessorName(name);
stepDefinition = jobStepDefinitionService.update(stepDefinition);
stepData.put(JOB_STEP_DEFINITION, stepDefinition);
} catch (KapuaException ex) {
verifyException(ex);
}
}
@When("^I change the step definition type to \"(.+)\"$")
public void changeExistingStepDefinitionType(String type)
throws Exception {
JobStepDefinition stepDefinition = (JobStepDefinition) stepData.get(JOB_STEP_DEFINITION);
primeException();
try {
stepDefinition.setStepType(getTypeFromString(type));
stepDefinition = jobStepDefinitionService.update(stepDefinition);
stepData.put(JOB_STEP_DEFINITION, stepDefinition);
} catch (KapuaException ex) {
verifyException(ex);
}
}
@Then("^The step definition entity matches the creator$")
public void checkTheStepDefinitionAgainstTheCreator() {
JobStepDefinitionCreator stepDefinitionCreator = (JobStepDefinitionCreator) stepData.get(JOB_STEP_DEFINITION_CREATOR);
JobStepDefinition stepDefinition = (JobStepDefinition) stepData.get(JOB_STEP_DEFINITION);
assertEquals("The step definition has the wrong name!", stepDefinitionCreator.getName(), stepDefinition.getName());
assertEquals("The step definition has the wrong description!", stepDefinitionCreator.getDescription(), stepDefinition.getDescription());
assertEquals("The step definition has the wrong reader name!", stepDefinitionCreator.getReaderName(), stepDefinition.getReaderName());
assertEquals("The step definition has the wrong processor name!", stepDefinitionCreator.getProcessorName(), stepDefinition.getProcessorName());
assertEquals("The step definition has the wrong writer name!", stepDefinitionCreator.getWriterName(), stepDefinition.getWriterName());
assertEquals("The step definition has a wrong step type!", stepDefinitionCreator.getStepType(), stepDefinition.getStepType());
assertNotNull("The step definition has no properties!", stepDefinition.getStepProperties());
assertEquals("The step definition has a wrong number of properties!", stepDefinitionCreator.getStepProperties().size(), stepDefinition.getStepProperties().size());
for (int i = 0; i < stepDefinitionCreator.getStepProperties().size(); i++) {
assertEquals(stepDefinitionCreator.getStepProperties().get(i).getName(), stepDefinition.getStepProperties().get(i).getName());
assertEquals(stepDefinitionCreator.getStepProperties().get(i).getPropertyType(), stepDefinition.getStepProperties().get(i).getPropertyType());
assertEquals(stepDefinitionCreator.getStepProperties().get(i).getPropertyValue(), stepDefinition.getStepProperties().get(i).getPropertyValue());
}
}
@Then("^There is no such step definition item in the database$")
public void checkThatNoStepDefinitionWasFound() {
JobStepDefinition stepDefinition = (JobStepDefinition) stepData.get(JOB_STEP_DEFINITION);
assertNull("Unexpected step definition item was found!", stepDefinition);
}
@Then("^The step definition name is \"(.+)\"$")
public void checkStepDefinitionName(String name) {
JobStepDefinition stepDefinition = (JobStepDefinition) stepData.get(JOB_STEP_DEFINITION);
assertEquals("The step definition name does not match!", name, stepDefinition.getName());
}
@Then("^The step definition type is \"(.+)\"$")
public void checkStepDefinitionType(String type) {
JobStepDefinition stepDefinition = (JobStepDefinition) stepData.get(JOB_STEP_DEFINITION);
assertEquals("The step definition type does not match!", getTypeFromString(type), stepDefinition.getStepType());
}
@Then("^The step definition processor name is \"(.+)\"$")
public void checkStepDefinitionProcessorName(String name) {
JobStepDefinition stepDefinition = (JobStepDefinition) stepData.get(JOB_STEP_DEFINITION);
assertEquals("The step definition processor name does not match!", name, stepDefinition.getProcessorName());
}
@When("^I test the sanity of the step definition factory$")
public void testTheStepDefinitionFactory() {
assertNotNull(jobStepDefinitionFactory.newCreator(SYS_SCOPE_ID));
assertNotNull(jobStepDefinitionFactory.newEntity(SYS_SCOPE_ID));
assertNotNull(jobStepDefinitionFactory.newListResult());
assertNotNull(jobStepDefinitionFactory.newQuery(SYS_SCOPE_ID));
assertNotNull(jobStepDefinitionFactory.newStepProperty("TestName", "TestType", "TestValue", "TestExampleValue"));
}
// ************************************************************************************
// * Job Step Service Test steps *
// ************************************************************************************
@Given("^A regular step creator with the name \"(.*)\" and the following properties$")
public void prepareARegularStepCreatorWithPropertyList(String name, List<CucJobStepProperty> list) {
JobStepCreator stepCreator;
KapuaId currentStepDefId = (KapuaId) stepData.get(CURRENT_JOB_STEP_DEFINITION_ID);
stepCreator = prepareDefaultJobStepCreator();
stepCreator.setName(name);
stepCreator.setJobStepDefinitionId(currentStepDefId);
List<JobStepProperty> tmpPropLst = new ArrayList<>();
for (CucJobStepProperty prop : list) {
tmpPropLst.add(jobStepFactory.newStepProperty(prop.getName(), prop.getType(), prop.getValue()));
}
stepCreator.setJobStepProperties(tmpPropLst);
stepData.put(JOB_STEP_CREATOR, stepCreator);
}
@When("^I create a new step entity from the existing creator$")
public void createAStepFromTheCreator()
throws Exception {
KapuaId currentJobId = (KapuaId) stepData.get(CURRENT_JOB_ID);
JobStepCreator stepCreator = (JobStepCreator) stepData.get(JOB_STEP_CREATOR);
stepCreator.setJobId(currentJobId);
primeException();
try {
stepData.remove(JOB_STEP);
stepData.remove(CURRENT_STEP_ID);
JobStep step = jobStepService.create(stepCreator);
stepData.put(JOB_STEP, step);
stepData.put(CURRENT_STEP_ID, step.getId());
} catch (KapuaException ex) {
verifyException(ex);
}
}
@When("I search the database for created job steps and I find (\\d+)$")
public void searchJobSteps(int count) throws Exception {
KapuaId currentJobId = (KapuaId) stepData.get(CURRENT_JOB_ID);
primeException();
try {
JobStepQuery tmpQuery = jobStepFactory.newQuery(getCurrentScopeId());
tmpQuery.setPredicate(tmpQuery.attributePredicate(JobStepAttributes.JOB_ID, currentJobId, AttributePredicate.Operator.EQUAL));
JobStepListResult jobStepListResult = jobStepService.query(tmpQuery);
assertEquals(count, jobStepListResult.getSize());
} catch (KapuaException ke) {
verifyException(ke);
}
}
@When("^I change the step name to \"(.+)\"$")
public void updateStepName(String name)
throws Exception {
JobStep step = (JobStep) stepData.get(JOB_STEP);
step.setName(name);
primeException();
try {
step = jobStepService.update(step);
stepData.put(JOB_STEP, step);
} catch (KapuaException ex) {
verifyException(ex);
}
}
@When("^I update the step with a new definition$")
public void updateStepDefinition() throws Exception {
JobStep step = (JobStep) stepData.get(JOB_STEP);
KapuaId currentStepDefId = (KapuaId) stepData.get(CURRENT_JOB_STEP_DEFINITION_ID);
step.setJobStepDefinitionId(currentStepDefId);
primeException();
try {
step = jobStepService.update(step);
stepData.put(JOB_STEP, step);
} catch (KapuaException ex) {
verifyException(ex);
}
}
@When("^I search for the last step in the database$")
public void findLastStep()
throws Exception {
JobStep step = (JobStep) stepData.get(JOB_STEP);
primeException();
try {
JobStep newStep = jobStepService.find(step.getScopeId(), step.getId());
stepData.put(JOB_STEP, newStep);
} catch (KapuaException ex) {
verifyException(ex);
}
}