1
1
package org .gitlab4j .api ;
2
2
3
+ import static java .util .stream .Collectors .toList ;
4
+ import static org .junit .Assert .assertFalse ;
5
+ import static org .junit .Assert .assertNotNull ;
6
+ import static org .junit .Assert .assertTrue ;
7
+ import static org .junit .Assume .assumeTrue ;
8
+
9
+ import java .util .List ;
10
+
11
+ import org .gitlab4j .api .GitLabApi .ApiVersion ;
3
12
import org .gitlab4j .api .models .PipelineSchedule ;
13
+ import org .gitlab4j .api .models .Project ;
4
14
import org .junit .AfterClass ;
5
15
import org .junit .Before ;
6
16
import org .junit .BeforeClass ;
7
- import org .junit .FixMethodOrder ;
8
17
import org .junit .Test ;
9
- import org .junit .runners .MethodSorters ;
10
18
11
- import java .util .List ;
12
-
13
- import static org .junit .Assert .*;
14
- import static org .junit .Assume .assumeTrue ;
15
-
16
- @ FixMethodOrder (MethodSorters .NAME_ASCENDING )
17
19
public class TestPipelineApi {
18
20
// The following needs to be set to your test repository
19
21
private static final String TEST_NAMESPACE ;
@@ -28,13 +30,38 @@ public class TestPipelineApi {
28
30
TEST_PRIVATE_TOKEN = TestUtils .getProperty ("TEST_PRIVATE_TOKEN" );
29
31
}
30
32
33
+ private static final String SCHEDULE_DESCRIPTION = "Test pipeline schedule - DELETE AFTER TEST" ;
31
34
32
35
private static GitLabApi gitLabApi ;
36
+ private static Project testProject ;
33
37
34
38
public TestPipelineApi () {
35
39
super ();
36
40
}
37
41
42
+ private static void deleteTestSchedules () {
43
+
44
+ if (testProject == null ) {
45
+ return ;
46
+ }
47
+
48
+ try {
49
+
50
+ List <PipelineSchedule > pipelineSchedules = gitLabApi .getPipelineApi ().getPipelineSchedules (testProject );
51
+ if (pipelineSchedules == null || pipelineSchedules .isEmpty ()) {
52
+ return ;
53
+ }
54
+
55
+ for (PipelineSchedule schedule : pipelineSchedules ) {
56
+ if (schedule .getDescription ().startsWith (SCHEDULE_DESCRIPTION )) {
57
+ gitLabApi .getPipelineApi ().deletePipelineSchedule (testProject , schedule .getId ());
58
+ }
59
+ }
60
+
61
+ } catch (Exception ignore ) {
62
+ }
63
+ }
64
+
38
65
@ BeforeClass
39
66
public static void setup () {
40
67
@@ -50,15 +77,25 @@ public static void setup() {
50
77
if (TEST_PRIVATE_TOKEN == null || TEST_PRIVATE_TOKEN .trim ().isEmpty ()) {
51
78
problems += "TEST_PRIVATE_TOKEN cannot be empty\n " ;
52
79
}
80
+
53
81
if (problems .isEmpty ()) {
54
- gitLabApi = new GitLabApi (GitLabApi .ApiVersion .V4 , TEST_HOST_URL , TEST_PRIVATE_TOKEN );
82
+ gitLabApi = new GitLabApi (ApiVersion .V4 , TEST_HOST_URL , TEST_PRIVATE_TOKEN );
83
+
84
+ try {
85
+ testProject = gitLabApi .getProjectApi ().getProject (TEST_NAMESPACE , TEST_PROJECT_NAME );
86
+ } catch (GitLabApiException gle ) {
87
+ }
88
+
89
+ deleteTestSchedules ();
90
+
55
91
} else {
56
92
System .err .print (problems );
57
93
}
58
94
}
59
95
60
96
@ AfterClass
61
97
public static void teardown () {
98
+ deleteTestSchedules ();
62
99
}
63
100
64
101
@@ -68,50 +105,56 @@ public void beforeMethod() {
68
105
}
69
106
70
107
@ Test
71
- public void testCreateProjectPipeLineSchedule () throws GitLabApiException {
72
- assumeTrue (TEST_NAMESPACE != null && TEST_PROJECT_NAME != null );
73
- assumeTrue (TEST_NAMESPACE .trim ().length () > 0 && TEST_PROJECT_NAME .trim ().length () > 0 );
108
+ public void testCreateAndUpdateProjectPipeLineSchedule () throws GitLabApiException {
74
109
75
- Integer testProjectId = gitLabApi .getProjectApi ().getProject (TEST_NAMESPACE , TEST_PROJECT_NAME ).getId ();
110
+ assertNotNull (testProject );
111
+
112
+ String scheduleDescription = SCHEDULE_DESCRIPTION + " - test updatePipelineSchedule()" ;
76
113
PipelineSchedule newPipelineSchedule = new PipelineSchedule ();
77
- newPipelineSchedule .setDescription ("test pipeline schedule" );
78
- newPipelineSchedule .setCron ("0 4 * * *" );
114
+ newPipelineSchedule .setDescription (scheduleDescription );
115
+ newPipelineSchedule .setCron ("2 4 * * *" );
79
116
newPipelineSchedule .setRef ("master" );
80
- PipelineSchedule createdPipelineSchedule = gitLabApi .getPipelineApi ().createPipelineSchedule (testProjectId , newPipelineSchedule );
117
+ PipelineSchedule createdPipelineSchedule = gitLabApi .getPipelineApi ().createPipelineSchedule (testProject , newPipelineSchedule );
81
118
assertNotNull (createdPipelineSchedule );
82
- List <PipelineSchedule > pipelineSchedules = gitLabApi .getPipelineApi ().getPipelineSchedules (testProjectId );
83
- assertFalse (pipelineSchedules .isEmpty ());
84
- }
85
119
86
- @ Test
87
- public void testModifyProjectPipeLineSchedule () throws GitLabApiException {
88
- assumeTrue (TEST_NAMESPACE != null && TEST_PROJECT_NAME != null );
89
- assumeTrue (TEST_NAMESPACE .trim ().length () > 0 && TEST_PROJECT_NAME .trim ().length () > 0 );
90
-
91
- Integer testProjectId = gitLabApi .getProjectApi ().getProject (TEST_NAMESPACE , TEST_PROJECT_NAME ).getId ();
92
- List <PipelineSchedule > pipelineSchedules = gitLabApi .getPipelineApi ().getPipelineSchedules (testProjectId );
93
- assertTrue (pipelineSchedules .size ()==1 );
94
- PipelineSchedule existingPipelineSchedule = pipelineSchedules .get (0 );
95
- assertTrue (existingPipelineSchedule .getDescription ().equals ("test pipeline schedule" ));
96
- existingPipelineSchedule .setDescription ("new name" );
97
- gitLabApi .getPipelineApi ().modifyPipelineSchedule (testProjectId ,existingPipelineSchedule );
98
- pipelineSchedules = gitLabApi .getPipelineApi ().getPipelineSchedules (testProjectId );
99
- PipelineSchedule newPipelineSchedule = pipelineSchedules .get (0 );
100
- assertTrue (pipelineSchedules .size ()==1 );
101
- assertTrue (newPipelineSchedule .equals ("new name" ));
102
- }
120
+ // Make sure the created schedule is present before updating
121
+ List <PipelineSchedule > pipelineSchedules = gitLabApi .getPipelineApi ().getPipelineSchedules (testProject );
122
+ assertNotNull (pipelineSchedules );
123
+ assertTrue (pipelineSchedules .stream ().map (PipelineSchedule ::getDescription ).collect (toList ()).contains (scheduleDescription ));
103
124
125
+ String newScheduleDescription = scheduleDescription + " - updated" ;
126
+ createdPipelineSchedule .setDescription (newScheduleDescription );
127
+ gitLabApi .getPipelineApi ().updatePipelineSchedule (testProject , createdPipelineSchedule );
128
+
129
+ pipelineSchedules = gitLabApi .getPipelineApi ().getPipelineSchedules (testProject );
130
+ assertNotNull (pipelineSchedules );
131
+
132
+ List <String > scheduleDecriptions = pipelineSchedules .stream ().map (PipelineSchedule ::getDescription ).collect (toList ());
133
+ assertFalse (scheduleDecriptions .contains (scheduleDescription ));
134
+ assertTrue (scheduleDecriptions .contains (newScheduleDescription ));
135
+ }
104
136
105
137
@ Test
106
138
public void testDeleteProjectPipeLineSchedule () throws GitLabApiException {
107
- assumeTrue (TEST_NAMESPACE != null && TEST_PROJECT_NAME != null );
108
- assumeTrue (TEST_NAMESPACE .trim ().length () > 0 && TEST_PROJECT_NAME .trim ().length () > 0 );
109
-
110
- Integer testProjectId = gitLabApi .getProjectApi ().getProject (TEST_NAMESPACE , TEST_PROJECT_NAME ).getId ();
111
- List <PipelineSchedule > pipelineSchedules = gitLabApi .getPipelineApi ().getPipelineSchedules (testProjectId );
112
- assertFalse (pipelineSchedules .isEmpty ());
113
- gitLabApi .getPipelineApi ().deletePipelineSchedule (testProjectId ,pipelineSchedules .get (0 ).getId ());
114
- pipelineSchedules = gitLabApi .getPipelineApi ().getPipelineSchedules (testProjectId );
115
- assertTrue (pipelineSchedules .isEmpty ());
139
+
140
+ assertNotNull (testProject );
141
+
142
+ String scheduleDescription = SCHEDULE_DESCRIPTION + " - test deletePipelineSchedule()" ;
143
+ PipelineSchedule newPipelineSchedule = new PipelineSchedule ();
144
+ newPipelineSchedule .setDescription (scheduleDescription );
145
+ newPipelineSchedule .setCron ("1 4 * * *" );
146
+ newPipelineSchedule .setRef ("master" );
147
+ PipelineSchedule createdPipelineSchedule = gitLabApi .getPipelineApi ().createPipelineSchedule (testProject , newPipelineSchedule );
148
+ assertNotNull (createdPipelineSchedule );
149
+
150
+ // Make sure the created schedule is present before deleting
151
+ List <PipelineSchedule > pipelineSchedules = gitLabApi .getPipelineApi ().getPipelineSchedules (testProject );
152
+ assertNotNull (pipelineSchedules );
153
+ assertTrue (pipelineSchedules .stream ().map (PipelineSchedule ::getDescription ).collect (toList ()).contains (scheduleDescription ));
154
+
155
+ gitLabApi .getPipelineApi ().deletePipelineSchedule (testProject , createdPipelineSchedule .getId ());
156
+ pipelineSchedules = gitLabApi .getPipelineApi ().getPipelineSchedules (testProject );
157
+ assertNotNull (pipelineSchedules );
158
+ assertFalse (pipelineSchedules .stream ().map (PipelineSchedule ::getDescription ).collect (toList ()).contains (scheduleDescription ));
116
159
}
117
160
}
0 commit comments