-
Notifications
You must be signed in to change notification settings - Fork 273
/
Setup.java
818 lines (759 loc) · 32 KB
/
Setup.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
/*
* The MIT License
*
* Copyright (c) 2010, 2014 Sony Mobile Communications Inc. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.sonyericsson.hudson.plugins.gerrit.trigger.mock;
import com.sonymobile.tools.gerrit.gerritevents.dto.attr.Account;
import com.sonymobile.tools.gerrit.gerritevents.dto.attr.Approval;
import com.sonymobile.tools.gerrit.gerritevents.dto.attr.Change;
import com.sonymobile.tools.gerrit.gerritevents.dto.attr.PatchSet;
import com.sonymobile.tools.gerrit.gerritevents.dto.attr.Provider;
import com.sonymobile.tools.gerrit.gerritevents.dto.attr.RefUpdate;
import com.sonymobile.tools.gerrit.gerritevents.dto.events.ChangeAbandoned;
import com.sonymobile.tools.gerrit.gerritevents.dto.events.ChangeMerged;
import com.sonymobile.tools.gerrit.gerritevents.dto.events.ChangeRestored;
import com.sonymobile.tools.gerrit.gerritevents.dto.events.CommentAdded;
import com.sonymobile.tools.gerrit.gerritevents.dto.events.DraftPublished;
import com.sonymobile.tools.gerrit.gerritevents.dto.events.PatchsetCreated;
import com.sonymobile.tools.gerrit.gerritevents.dto.events.RefReplicated;
import com.sonymobile.tools.gerrit.gerritevents.dto.events.RefUpdated;
import com.sonyericsson.hudson.plugins.gerrit.trigger.VerdictCategory;
import com.sonyericsson.hudson.plugins.gerrit.trigger.PluginImpl;
import com.sonyericsson.hudson.plugins.gerrit.trigger.events.ManualPatchsetCreated;
import com.sonyericsson.hudson.plugins.gerrit.trigger.gerritnotifier.ToGerritRunListener;
import com.sonyericsson.hudson.plugins.gerrit.trigger.gerritnotifier.model.BuildMemory.MemoryImprint;
import com.sonyericsson.hudson.plugins.gerrit.trigger.gerritnotifier.model.BuildsStartedStats;
import com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.GerritCause;
import com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.GerritTrigger;
import com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.data.SkipVote;
import com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.events.PluginGerritEvent;
import com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.events.PluginPatchsetCreatedEvent;
import com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.events.PluginRefUpdatedEvent;
import com.sonyericsson.hudson.plugins.gerrit.trigger.utils.StringUtil;
import hudson.EnvVars;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.Cause;
import hudson.model.CauseAction;
import hudson.model.Hudson;
import hudson.model.Job;
import hudson.model.Result;
import hudson.model.TaskListener;
import hudson.security.GlobalMatrixAuthorizationStrategy;
import hudson.security.SecurityRealm;
import hudson.triggers.Trigger;
import hudson.triggers.TriggerDescriptor;
import net.sf.json.JSONObject;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.junit.Assert;
import org.jvnet.hudson.test.JenkinsRule;
import org.powermock.api.mockito.PowerMockito;
import java.io.IOException;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import static org.mockito.Mockito.when;
import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.spy;
import static com.sonymobile.tools.gerrit.gerritevents.dto.GerritEventKeys.BRANCH;
import static com.sonymobile.tools.gerrit.gerritevents.dto.GerritEventKeys.CHANGE;
import static com.sonymobile.tools.gerrit.gerritevents.dto.GerritEventKeys.EMAIL;
import static com.sonymobile.tools.gerrit.gerritevents.dto.GerritEventKeys.ID;
import static com.sonymobile.tools.gerrit.gerritevents.dto.GerritEventKeys.NAME;
import static com.sonymobile.tools.gerrit.gerritevents.dto.GerritEventKeys.NUMBER;
import static com.sonymobile.tools.gerrit.gerritevents.dto.GerritEventKeys.OWNER;
import static com.sonymobile.tools.gerrit.gerritevents.dto.GerritEventKeys.PATCH_SET;
import static com.sonymobile.tools.gerrit.gerritevents.dto.GerritEventKeys.PROJECT;
import static com.sonymobile.tools.gerrit.gerritevents.dto.GerritEventKeys.REF;
import static com.sonymobile.tools.gerrit.gerritevents.dto.GerritEventKeys.REVISION;
import static com.sonymobile.tools.gerrit.gerritevents.dto.GerritEventKeys.SUBJECT;
import static com.sonymobile.tools.gerrit.gerritevents.dto.GerritEventKeys.UPLOADER;
import static com.sonymobile.tools.gerrit.gerritevents.dto.GerritEventKeys.URL;
/**
* Utility class for standard data during testing.
* @author Robert Sandell <robert.sandell@sonyericsson.com>
*/
public final class Setup {
/**
* Utility class.
*/
private Setup() {
}
/**
* Gives you a Config mock.
* @return MockGerritHudsonTriggerConfig mock.
*/
public static MockGerritHudsonTriggerConfig createConfig() {
return new MockGerritHudsonTriggerConfig();
}
/**
* Gives you a Config mock with nulled code review values.
* @return MockGerritHudsonTriggerConfig mock.
*/
public static MockGerritHudsonTriggerConfig createConfigWithCodeReviewsNull() {
MockGerritHudsonTriggerConfig config = new MockGerritHudsonTriggerConfig();
config.setGerritBuildFailedCodeReviewValue(null);
config.setGerritBuildSuccessfulCodeReviewValue(null);
config.setGerritBuildNotBuiltCodeReviewValue(null);
config.setGerritBuildStartedCodeReviewValue(null);
config.setGerritBuildUnstableCodeReviewValue(null);
return config;
}
/**
* Gives you a PatchsetCreated mock.
* @return PatchsetCreated mock.
*/
public static PatchsetCreated createPatchsetCreated() {
return createPatchsetCreated(PluginImpl.DEFAULT_SERVER_NAME);
}
/**
* Gives you a PatchsetCreated mock for a specific server name.
* @param serverName name of the server.
* @return PatchsetCreated mock.
*/
public static PatchsetCreated createPatchsetCreated(String serverName) {
return createPatchsetCreated(serverName, "project", "ref");
}
/**
* Create a new RefUpdated event with given data.
* @param serverName The server name.
* @param project The project.
* @param ref The ref.
* @return a RefUpdated event
*/
public static RefUpdated createRefUpdated(String serverName, String project, String ref) {
RefUpdated event = new RefUpdated();
Account account = new Account();
account.setEmail("email@domain.com");
account.setName("Name");
event.setAccount(account);
event.setProvider(new Provider(serverName, "gerrit", "29418", "ssh", "http://gerrit/", "1"));
RefUpdate refUpdate = new RefUpdate();
refUpdate.setNewRev("2");
refUpdate.setOldRev("1");
refUpdate.setProject(project);
refUpdate.setRefName(ref);
event.setRefUpdate(refUpdate);
event.setEventCreatedOn("1418133772");
return event;
}
/**
* Create a new patchset created event with the given data.
* @param serverName The server name
* @param project The project
* @param ref The ref
* @return a pactchsetCreated event
*/
public static PatchsetCreated createPatchsetCreated(String serverName, String project, String ref) {
PatchsetCreated event = new PatchsetCreated();
Change change = new Change();
change.setBranch("branch");
change.setId("Iddaaddaa123456789");
change.setNumber("1000");
Account account = new Account();
account.setEmail("email@domain.com");
account.setName("Name");
change.setOwner(account);
change.setProject(project);
change.setSubject("subject");
change.setUrl("http://gerrit/1000");
event.setChange(change);
PatchSet patch = new PatchSet();
patch.setNumber("1");
patch.setRevision("9999");
patch.setRef(ref);
event.setPatchset(patch);
event.setProvider(new Provider(serverName, "gerrit", "29418", "ssh", "http://gerrit/", "1"));
event.setEventCreatedOn("1418133772");
return event;
}
/**
* Create a new patchset created event with the given data.
*
* @param owner owner of the change
* @param uploader uploader of the patchset
* @param account account that caused event
* @return a new PatchsetCreated event.
*/
public static PatchsetCreated createPatchsetCreatedWithAccounts(Account owner, Account uploader, Account account)
{
PatchsetCreated event = createPatchsetCreated();
event.getChange().setOwner(owner);
event.getPatchSet().setUploader(uploader);
event.setAccount(account);
event.setEventCreatedOn("1418133772");
return event;
}
/**
* Gives you a DraftPublished mock.
* @return DraftPublished mock.
*/
public static DraftPublished createDraftPublished() {
DraftPublished event = new DraftPublished();
Change change = new Change();
change.setBranch("branch");
change.setId("Iddaaddaa123456789");
change.setNumber("1000");
Account account = new Account();
account.setEmail("email@domain.com");
account.setName("Name");
change.setOwner(account);
change.setProject("project");
change.setSubject("subject");
change.setUrl("http://gerrit/1000");
event.setChange(change);
PatchSet patch = new PatchSet();
patch.setNumber("1");
patch.setRevision("9999");
event.setPatchset(patch);
patch.setRef("ref");
event.setProvider(new Provider(PluginImpl.DEFAULT_SERVER_NAME, "gerrit", "29418", "ssh", "http://gerrit/", "1"));
event.setEventCreatedOn("1418133772");
return event;
}
/**
* Gives you a ChangeAbandoned mock.
* @return ChangeAbandoned mock.
*/
public static ChangeAbandoned createChangeAbandoned() {
ChangeAbandoned event = new ChangeAbandoned();
Change change = new Change();
change.setBranch("branch");
change.setId("Iddaaddaa123456789");
change.setNumber("1000");
Account account = new Account("Name", "email@domain.com");
change.setOwner(account);
change.setProject("project");
change.setSubject("subject");
change.setUrl("http://gerrit/1000");
event.setChange(change);
PatchSet patch = new PatchSet();
patch.setNumber("1");
patch.setRevision("9999");
event.setPatchset(patch);
Account abandoner = new Account("Name1", "email@domain1.com");
event.setAbandoner(abandoner);
event.setEventCreatedOn("1418133772");
return event;
}
/**
* Gives you a ChangeMerged mock.
* @param serverName The server name
* @param project The project
* @param ref The ref
* @return ChangeMerged mock.
*/
public static ChangeMerged createChangeMerged(String serverName, String project, String ref) {
return createChangeMergedWithPatchSetDate(serverName, project, ref, new Date());
}
/**
* Gives you a DraftPublished mock.
* @param serverName The server name
* @param project The project
* @param ref The ref
* @return DraftPublished mock.
*/
public static DraftPublished createDraftPublished(String serverName, String project, String ref) {
return createDraftPublishedWithPatchSetDate(serverName, project, ref, new Date());
}
/**
* Gives you a ChangeMerged mock.
* @return ChangeMerged mock.
*/
public static ChangeMerged createChangeMerged() {
ChangeMerged event = new ChangeMerged();
Change change = new Change();
change.setBranch("branch");
change.setId("Iddaaddaa123456789");
change.setNumber("1000");
Account account = new Account();
account.setEmail("email@domain.com");
account.setName("Name");
change.setOwner(account);
change.setProject("project");
change.setSubject("subject");
change.setUrl("http://gerrit/1000");
event.setChange(change);
PatchSet patch = new PatchSet();
patch.setNumber("1");
patch.setRevision("9999");
event.setPatchset(patch);
event.setEventCreatedOn("1418133772");
return event;
}
/**
* Gives you a ChangeMerged mock.
* @param serverName The server name
* @param project The project
* @param ref The ref
* @param date The patchset's createdOn date
* @return ChangeMerged mock.
*/
public static ChangeMerged createChangeMergedWithPatchSetDate(String serverName, String project,
String ref, Date date) {
ChangeMerged event = new ChangeMerged();
Change change = new Change();
change.setBranch("branch");
change.setId("Iddaaddaa123456789");
change.setNumber("1000");
Account account = new Account();
account.setEmail("email@domain.com");
account.setName("Name");
change.setOwner(account);
change.setProject(project);
change.setSubject("subject");
change.setUrl("http://gerrit/1000");
event.setChange(change);
PatchSet patch = new PatchSet();
patch.setNumber("1");
patch.setRevision("9999");
event.setPatchset(patch);
patch.setRef(ref);
patch.setCreatedOn(date);
event.setProvider(new Provider(serverName, "gerrit", "29418", "ssh", "http://gerrit/", "1"));
event.setEventCreatedOn("1418133772");
return event;
}
/**
* Gives you a DraftPublished mock.
* @param serverName The server name
* @param project The project
* @param ref The ref
* @param date The patchset's createdOn date
* @return DraftPublished mock.
*/
public static DraftPublished createDraftPublishedWithPatchSetDate(String serverName, String project,
String ref, Date date) {
DraftPublished event = new DraftPublished();
Change change = new Change();
change.setBranch("branch");
change.setId("Iddaaddaa123456789");
change.setNumber("1000");
Account account = new Account();
account.setEmail("email@domain.com");
account.setName("Name");
change.setOwner(account);
change.setProject(project);
change.setSubject("subject");
change.setUrl("http://gerrit/1000");
event.setChange(change);
PatchSet patch = new PatchSet();
patch.setNumber("1");
patch.setRevision("9999");
event.setPatchset(patch);
patch.setRef(ref);
patch.setCreatedOn(date);
event.setProvider(new Provider(serverName, "gerrit", "29418", "ssh", "http://gerrit/", "1"));
event.setEventCreatedOn("1418133772");
return event;
}
/**
* Gives you a ChangeRestored mock.
* @return ChangeRestored mock.
*/
public static ChangeRestored createChangeRestored() {
ChangeRestored event = new ChangeRestored();
Change change = new Change();
change.setBranch("branch");
change.setId("Iddaaddaa123456789");
change.setNumber("1000");
Account account = new Account("Name", "email@domain.com");
change.setOwner(account);
change.setProject("project");
change.setSubject("subject");
change.setUrl("http://gerrit/1000");
event.setChange(change);
PatchSet patch = new PatchSet();
patch.setNumber("1");
patch.setRevision("9999");
event.setPatchset(patch);
Account restorer = new Account("Name1", "email@domain1.com");
event.setRestorer(restorer);
event.setEventCreatedOn("1418133772");
return event;
}
/**
* Gives you a CommentAdded mock.
* @return CommentAdded mock.
*/
public static CommentAdded createCommentAdded() {
CommentAdded event = new CommentAdded();
Change change = new Change();
change.setBranch("branch");
change.setId("Iddaaddaa123456789");
change.setNumber("1000");
Account account = new Account();
account.setEmail("email@domain.com");
account.setName("Name");
change.setOwner(account);
change.setProject("project");
change.setSubject("subject");
change.setUrl("http://gerrit/1000");
event.setChange(change);
event.setProvider(new Provider(PluginImpl.DEFAULT_SERVER_NAME, "gerrit", "29418", "ssh", "http://gerrit/", "1"));
PatchSet patch = new PatchSet();
patch.setNumber("1");
patch.setRevision("9999");
event.setPatchset(patch);
List<Approval> approvals = new LinkedList<Approval>();
Approval approval = new Approval();
approval.setType("CRVW");
approval.setValue("1");
approvals.add(approval);
event.setApprovals(approvals);
event.setEventCreatedOn("1418133772");
return event;
}
/**
* Gives you a PatchsetCreated mock as a JSON object.
* @return PatchsetCreated mock.
* @see #createPatchsetCreated()
*/
public static JSONObject createPatchsetCreatedJson() {
JSONObject event = new JSONObject();
JSONObject change = new JSONObject();
JSONObject account = new JSONObject();
account.put(NAME, "Name");
account.put(EMAIL, "email@domain.com");
change.put(PROJECT, "project");
change.put(BRANCH, "branch");
change.put(ID, "Iddaaddaa123456789");
change.put(NUMBER, "1000");
change.put(SUBJECT, "subject");
change.put(URL, "http://gerrit/1000");
change.put(OWNER, account);
event.put(CHANGE, change);
JSONObject patchSet = new JSONObject();
patchSet.put(NUMBER, "1");
patchSet.put(REVISION, "9999");
patchSet.put(REF, "refs/changes/1000/1");
patchSet.put(UPLOADER, account);
event.put(PATCH_SET, patchSet);
event.put(UPLOADER, account);
return event;
}
/**
* Gives you a ManualPatchsetCreated mock.
* @return PatchsetCreated mock.
*/
public static ManualPatchsetCreated createManualPatchsetCreated() {
ManualPatchsetCreated event = new ManualPatchsetCreated();
Change change = new Change();
change.setBranch("branch");
change.setId("Iddaaddaa123456789");
change.setNumber("1000");
Account account = new Account();
account.setEmail("email@domain.com");
account.setName("Name");
change.setOwner(account);
change.setProject("project");
change.setSubject("subject");
change.setUrl("http://gerrit/1000");
event.setChange(change);
PatchSet patch = new PatchSet();
patch.setNumber("1");
patch.setRevision("9999");
event.setPatchset(patch);
event.setUserName("Bobby");
event.setProvider(new Provider(PluginImpl.DEFAULT_SERVER_NAME, "gerrit", "29418", "ssh", "http://gerrit/", "1"));
event.setEventCreatedOn("1418133772");
return event;
}
/**
* Gives you a BuildsStartedStats mock object for the given event.
* @param event the event.
* @return BuildsStartedStats mock.
*/
public static BuildsStartedStats createBuildStartedStats(PatchsetCreated event) {
//CS IGNORE MagicNumber FOR NEXT 2 LINES. REASON: mock.
return new BuildsStartedStats(event, 3, 1);
}
/**
* EnvVars mock.
* @return EnvVars mock
*/
public static EnvVars createEnvVars() {
EnvVars env = new EnvVars();
env.put("BRANCH", "branch");
env.put("CHANGE", "1000");
env.put("PATCHSET", "1");
env.put("REFSPEC", StringUtil.REFSPEC_PREFIX + "00/1000/1");
env.put("CHANGE_URL", "http://gerrit/1000");
return env;
}
/**
* GerritTrigger mock for build.
* @param build The build
* @return GerritTrigger mock
*/
public static GerritTrigger createGerritTrigger(AbstractBuild build) {
GerritTrigger trigger = mock(GerritTrigger.class);
AbstractProject project = build.getProject();
setTrigger(trigger, project);
return trigger;
}
/**
* Create a new default trigger object.
*
* @param job if not null, start the trigger with the given project.
* @return a new GerritTrigger object.
*/
public static GerritTrigger createDefaultTrigger(Job job) {
PluginPatchsetCreatedEvent pluginEvent = new PluginPatchsetCreatedEvent();
List<PluginGerritEvent> triggerOnEvents = new LinkedList<PluginGerritEvent>();
triggerOnEvents.add(pluginEvent);
boolean silentMode = true;
boolean silentStart = false;
GerritTrigger trigger = new GerritTrigger(null, null, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
silentMode, silentStart, true, false, false, "", "", "", "", "", "", "", null,
PluginImpl.DEFAULT_SERVER_NAME, null, triggerOnEvents, false, "", null);
if (job != null) {
trigger.start(job, true);
try {
if (job instanceof AbstractProject) {
((AbstractProject)job).addTrigger(trigger);
} else if (job instanceof WorkflowJob) {
((WorkflowJob)job).addTrigger(trigger);
} else {
Assert.fail("Unsupported Job type: " + job.getClass().getName());
}
} catch (IOException e) {
// for the sake of testing this should be ok
throw new RuntimeException(e);
}
}
return trigger;
}
/**
* Create a new default trigger object.
*
* @param job if not null, start the trigger with the given project.
* @return a new GerritTrigger object.
*/
public static GerritTrigger createRefUpdatedTrigger(Job job) {
PluginRefUpdatedEvent pluginEvent = new PluginRefUpdatedEvent();
List<PluginGerritEvent> triggerOnEvents = new LinkedList<PluginGerritEvent>();
triggerOnEvents.add(pluginEvent);
boolean silentMode = true;
boolean silentStart = false;
GerritTrigger trigger = new GerritTrigger(null, null, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
silentMode, silentStart, true, false, false, "", "", "", "", "", "", "", null,
PluginImpl.DEFAULT_SERVER_NAME, null, triggerOnEvents, false, "", null);
if (job != null) {
trigger.start(job, true);
try {
if (job instanceof AbstractProject) {
((AbstractProject)job).addTrigger(trigger);
} else if (job instanceof WorkflowJob) {
((WorkflowJob)job).addTrigger(trigger);
} else {
Assert.fail("Unsupported Job type: " + job.getClass().getName());
}
} catch (IOException e) {
// for the sake of testing this should be ok
throw new RuntimeException(e);
}
}
return trigger;
}
/**
* ToGerritRunListener mock for failure-message tests.
*
* @param build The build
* @param event The event
* @param filepath The filepath glob string
* @return ToGerritRunListener mock (spy)
*/
public static ToGerritRunListener createFailureMessageRunListener(final AbstractBuild build,
final PatchsetCreated event, final String filepath) {
GerritCause cause = new GerritCause(event, false);
when(build.getCause(GerritCause.class)).thenReturn(cause);
CauseAction causeAction = mock(CauseAction.class);
when(causeAction.getCauses()).thenReturn(Collections.<Cause>singletonList(cause));
when(build.getAction(CauseAction.class)).thenReturn(causeAction);
when(build.getResult()).thenReturn(Result.FAILURE);
GerritTrigger trigger = Setup.createGerritTrigger(build);
when(trigger.getBuildUnsuccessfulFilepath()).thenReturn(filepath);
ToGerritRunListener toGerritRunListener = spy(new ToGerritRunListener());
return toGerritRunListener;
}
/**
* Gives a List of VerdictCategories containing Code Review.
* @return the List.
*/
public static List<VerdictCategory> createCodeReviewVerdictCategoryList() {
VerdictCategory cat = new VerdictCategory("CRVW", "Code review");
List<VerdictCategory> list = new LinkedList<VerdictCategory>();
list.add(cat);
return list;
}
/**
* Create an MemoryImprint.Entry for the specific build and project.
*
* @param project the project
* @param build the build
* @return an entry.
*/
public static MemoryImprint.Entry createImprintEntry(AbstractProject project, AbstractBuild build) {
MemoryImprint.Entry entry = mock(MemoryImprint.Entry.class);
when(entry.getBuild()).thenReturn(build);
when(entry.getProject()).thenReturn(project);
return entry;
}
/**
* Create an MemoryImprint.Entry with a given trigger and a build that
* returns the given result.
*
* @param trigger the trigger
* @param result the result
* @return an entry with the parameters.
*/
public static MemoryImprint.Entry createAndSetupMemoryImprintEntry(GerritTrigger trigger, Result result) {
AbstractProject project = mock(AbstractProject.class);
setTrigger(trigger, project);
AbstractBuild build = mock(AbstractBuild.class);
when(build.getResult()).thenReturn(result);
return createImprintEntry(project, build);
}
/**
* Set/mock the supplied trigger onto the supplied {@link AbstractProject} instance.
* @param trigger The trigger.
* @param project The project.
*/
public static void setTrigger(GerritTrigger trigger, AbstractProject project) {
when(project.getTrigger(GerritTrigger.class)).thenReturn(trigger);
HashMap<TriggerDescriptor, Trigger<?>> triggers = new HashMap<TriggerDescriptor, Trigger<?>>();
triggers.put(new GerritTrigger.DescriptorImpl(), trigger);
PowerMockito.when(project.getTriggers()).thenReturn(triggers);
}
/**
* Create an MemoryImprint.Entry with a default trigger and a build that
* returns the given result. The trigger will be configured to "skip" the result.
*
* @param result the build result
* @param resultsCodeReviewVote what vote the job should cast for the build result.
* @param resultsVerifiedVote what vote the job should cast for the build result.
* @param shouldSkip if the result should be configured to be skipped or not.
* @return an entry with the parameters.
* @see #createAndSetupMemoryImprintEntry(GerritTrigger, hudson.model.Result)
* @see com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.GerritTrigger#getSkipVote()
*/
public static MemoryImprint.Entry createAndSetupMemoryImprintEntry(Result result,
int resultsCodeReviewVote,
int resultsVerifiedVote,
boolean shouldSkip) {
GerritTrigger trigger = mock(GerritTrigger.class);
SkipVote skipVote = null;
if (result == Result.SUCCESS) {
when(trigger.getGerritBuildSuccessfulCodeReviewValue()).thenReturn(resultsCodeReviewVote);
when(trigger.getGerritBuildSuccessfulVerifiedValue()).thenReturn(resultsVerifiedVote);
if (shouldSkip) {
skipVote = new SkipVote(true, false, false, false);
}
} else if (result == Result.FAILURE) {
when(trigger.getGerritBuildFailedCodeReviewValue()).thenReturn(resultsCodeReviewVote);
when(trigger.getGerritBuildFailedVerifiedValue()).thenReturn(resultsVerifiedVote);
if (shouldSkip) {
skipVote = new SkipVote(false, true, false, false);
}
} else if (result == Result.UNSTABLE) {
when(trigger.getGerritBuildUnstableCodeReviewValue()).thenReturn(resultsCodeReviewVote);
when(trigger.getGerritBuildUnstableVerifiedValue()).thenReturn(resultsVerifiedVote);
if (shouldSkip) {
skipVote = new SkipVote(false, false, true, false);
}
} else if (result == Result.NOT_BUILT) {
when(trigger.getGerritBuildSuccessfulCodeReviewValue()).thenReturn(1);
when(trigger.getGerritBuildSuccessfulCodeReviewValue()).thenReturn(1);
if (shouldSkip) {
skipVote = new SkipVote(false, false, false, true);
}
} else {
throw new IllegalArgumentException("Unsupported build result setup: " + result);
}
if (!shouldSkip) {
skipVote = new SkipVote(false, false, false, false);
}
when(trigger.getSkipVote()).thenReturn(skipVote);
return Setup.createAndSetupMemoryImprintEntry(trigger, result);
}
/**
* Create a mocked build that has the url 'test/' and the given project and
* env-vars. TaskListener is needed to stub out AbstractBuild.getEnvironment.
*
* @param project the project.
* @param taskListener the taskListener to stub out AbstractBuild.getEnvironment.
* @param env the env vars.
* @return a mocked build.
* @throws java.io.IOException if so.
* @throws InterruptedException if so.
*/
public static AbstractBuild createBuild(AbstractProject project, TaskListener taskListener, EnvVars env)
throws IOException, InterruptedException {
AbstractBuild build = mock(AbstractBuild.class);
when(build.getUrl()).thenReturn("test/");
when(build.getProject()).thenReturn(project);
when(build.getEnvironment(taskListener)).thenReturn(env);
return build;
}
/**
* Create a RefReplicated event with the specified info.
* @param project the project
* @param ref the ref
* @param server the server
* @param slave the slave
* @param status hte status
* @return a RefReplicated event
*/
public static RefReplicated createRefReplicatedEvent(String project, String ref, String server, String slave,
String status) {
RefReplicated refReplicated = new RefReplicated();
refReplicated.setProject(project);
refReplicated.setProvider(new Provider(server, null, null, null, null, null));
refReplicated.setRef(ref);
refReplicated.setTargetNode(slave);
refReplicated.setStatus(status);
return refReplicated;
}
/**
* Lock down the instance.
* @param j JenkinsRule.
* @throws Exception throw if so.
*/
public static void lockDown(JenkinsRule j) throws Exception {
SecurityRealm securityRealm = j.createDummySecurityRealm();
j.getInstance().setSecurityRealm(securityRealm);
GlobalMatrixAuthorizationStrategy authorizationStrategy = new GlobalMatrixAuthorizationStrategy();
authorizationStrategy.add(Hudson.READ, "authenticated");
j.getInstance().setAuthorizationStrategy(authorizationStrategy);
}
/**
* Unlock the instance.
* @param j JenkinsRule.
* @throws Exception throw if so.
*/
public static void unLock(JenkinsRule j) throws Exception {
j.getInstance().setSecurityRealm(SecurityRealm.NO_AUTHENTICATION);
}
}