11package org .gitlab4j .api ;
22
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 ;
312import org .gitlab4j .api .models .PipelineSchedule ;
13+ import org .gitlab4j .api .models .Project ;
414import org .junit .AfterClass ;
515import org .junit .Before ;
616import org .junit .BeforeClass ;
7- import org .junit .FixMethodOrder ;
817import org .junit .Test ;
9- import org .junit .runners .MethodSorters ;
1018
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 )
1719public class TestPipelineApi {
1820 // The following needs to be set to your test repository
1921 private static final String TEST_NAMESPACE ;
@@ -28,13 +30,38 @@ public class TestPipelineApi {
2830 TEST_PRIVATE_TOKEN = TestUtils .getProperty ("TEST_PRIVATE_TOKEN" );
2931 }
3032
33+ private static final String SCHEDULE_DESCRIPTION = "Test pipeline schedule - DELETE AFTER TEST" ;
3134
3235 private static GitLabApi gitLabApi ;
36+ private static Project testProject ;
3337
3438 public TestPipelineApi () {
3539 super ();
3640 }
3741
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+
3865 @ BeforeClass
3966 public static void setup () {
4067
@@ -50,15 +77,25 @@ public static void setup() {
5077 if (TEST_PRIVATE_TOKEN == null || TEST_PRIVATE_TOKEN .trim ().isEmpty ()) {
5178 problems += "TEST_PRIVATE_TOKEN cannot be empty\n " ;
5279 }
80+
5381 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+
5591 } else {
5692 System .err .print (problems );
5793 }
5894 }
5995
6096 @ AfterClass
6197 public static void teardown () {
98+ deleteTestSchedules ();
6299 }
63100
64101
@@ -68,50 +105,56 @@ public void beforeMethod() {
68105 }
69106
70107 @ 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 {
74109
75- Integer testProjectId = gitLabApi .getProjectApi ().getProject (TEST_NAMESPACE , TEST_PROJECT_NAME ).getId ();
110+ assertNotNull (testProject );
111+
112+ String scheduleDescription = SCHEDULE_DESCRIPTION + " - test updatePipelineSchedule()" ;
76113 PipelineSchedule newPipelineSchedule = new PipelineSchedule ();
77- newPipelineSchedule .setDescription ("test pipeline schedule" );
78- newPipelineSchedule .setCron ("0 4 * * *" );
114+ newPipelineSchedule .setDescription (scheduleDescription );
115+ newPipelineSchedule .setCron ("2 4 * * *" );
79116 newPipelineSchedule .setRef ("master" );
80- PipelineSchedule createdPipelineSchedule = gitLabApi .getPipelineApi ().createPipelineSchedule (testProjectId , newPipelineSchedule );
117+ PipelineSchedule createdPipelineSchedule = gitLabApi .getPipelineApi ().createPipelineSchedule (testProject , newPipelineSchedule );
81118 assertNotNull (createdPipelineSchedule );
82- List <PipelineSchedule > pipelineSchedules = gitLabApi .getPipelineApi ().getPipelineSchedules (testProjectId );
83- assertFalse (pipelineSchedules .isEmpty ());
84- }
85119
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 ));
103124
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+ }
104136
105137 @ Test
106138 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 ));
116159 }
117160}
0 commit comments