Commit 85137e73 authored by Greg Messner's avatar Greg Messner
Browse files

Fixed tests so they would reliably execute (#318).

parent 02c1995c
package org.gitlab4j.api; package org.gitlab4j.api;
import static java.util.stream.Collectors.toList;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeTrue;
import java.util.List;
import org.gitlab4j.api.GitLabApi.ApiVersion;
import org.gitlab4j.api.models.PipelineSchedule; import org.gitlab4j.api.models.PipelineSchedule;
import org.gitlab4j.api.models.Project;
import org.junit.AfterClass; import org.junit.AfterClass;
import org.junit.Before; import org.junit.Before;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test; import org.junit.Test;
import org.junit.runners.MethodSorters;
import java.util.List;
import static org.junit.Assert.*;
import static org.junit.Assume.assumeTrue;
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class TestPipelineApi { public class TestPipelineApi {
// The following needs to be set to your test repository // The following needs to be set to your test repository
private static final String TEST_NAMESPACE; private static final String TEST_NAMESPACE;
...@@ -28,13 +30,38 @@ public class TestPipelineApi { ...@@ -28,13 +30,38 @@ public class TestPipelineApi {
TEST_PRIVATE_TOKEN = TestUtils.getProperty("TEST_PRIVATE_TOKEN"); TEST_PRIVATE_TOKEN = TestUtils.getProperty("TEST_PRIVATE_TOKEN");
} }
private static final String SCHEDULE_DESCRIPTION = "Test pipeline schedule - DELETE AFTER TEST";
private static GitLabApi gitLabApi; private static GitLabApi gitLabApi;
private static Project testProject;
public TestPipelineApi() { public TestPipelineApi() {
super(); super();
} }
private static void deleteTestSchedules() {
if (testProject == null) {
return;
}
try {
List<PipelineSchedule> pipelineSchedules = gitLabApi.getPipelineApi().getPipelineSchedules(testProject);
if (pipelineSchedules == null || pipelineSchedules.isEmpty()) {
return;
}
for (PipelineSchedule schedule : pipelineSchedules) {
if (schedule.getDescription().startsWith(SCHEDULE_DESCRIPTION)) {
gitLabApi.getPipelineApi().deletePipelineSchedule(testProject, schedule.getId());
}
}
} catch (Exception ignore) {
}
}
@BeforeClass @BeforeClass
public static void setup() { public static void setup() {
...@@ -50,8 +77,17 @@ public class TestPipelineApi { ...@@ -50,8 +77,17 @@ public class TestPipelineApi {
if (TEST_PRIVATE_TOKEN == null || TEST_PRIVATE_TOKEN.trim().isEmpty()) { if (TEST_PRIVATE_TOKEN == null || TEST_PRIVATE_TOKEN.trim().isEmpty()) {
problems += "TEST_PRIVATE_TOKEN cannot be empty\n"; problems += "TEST_PRIVATE_TOKEN cannot be empty\n";
} }
if (problems.isEmpty()) { if (problems.isEmpty()) {
gitLabApi = new GitLabApi(GitLabApi.ApiVersion.V4, TEST_HOST_URL, TEST_PRIVATE_TOKEN); gitLabApi = new GitLabApi(ApiVersion.V4, TEST_HOST_URL, TEST_PRIVATE_TOKEN);
try {
testProject = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME);
} catch (GitLabApiException gle) {
}
deleteTestSchedules();
} else { } else {
System.err.print(problems); System.err.print(problems);
} }
...@@ -59,6 +95,7 @@ public class TestPipelineApi { ...@@ -59,6 +95,7 @@ public class TestPipelineApi {
@AfterClass @AfterClass
public static void teardown() { public static void teardown() {
deleteTestSchedules();
} }
...@@ -68,50 +105,56 @@ public class TestPipelineApi { ...@@ -68,50 +105,56 @@ public class TestPipelineApi {
} }
@Test @Test
public void testCreateProjectPipeLineSchedule() throws GitLabApiException { public void testCreateAndUpdateProjectPipeLineSchedule() throws GitLabApiException {
assumeTrue(TEST_NAMESPACE != null && TEST_PROJECT_NAME != null);
assumeTrue(TEST_NAMESPACE.trim().length() > 0 && TEST_PROJECT_NAME.trim().length() > 0);
Integer testProjectId = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME).getId(); assertNotNull(testProject);
String scheduleDescription = SCHEDULE_DESCRIPTION + " - test updatePipelineSchedule()";
PipelineSchedule newPipelineSchedule = new PipelineSchedule(); PipelineSchedule newPipelineSchedule = new PipelineSchedule();
newPipelineSchedule.setDescription("test pipeline schedule"); newPipelineSchedule.setDescription(scheduleDescription);
newPipelineSchedule.setCron("0 4 * * *"); newPipelineSchedule.setCron("2 4 * * *");
newPipelineSchedule.setRef("master"); newPipelineSchedule.setRef("master");
PipelineSchedule createdPipelineSchedule = gitLabApi.getPipelineApi().createPipelineSchedule(testProjectId,newPipelineSchedule); PipelineSchedule createdPipelineSchedule = gitLabApi.getPipelineApi().createPipelineSchedule(testProject, newPipelineSchedule);
assertNotNull(createdPipelineSchedule); assertNotNull(createdPipelineSchedule);
List<PipelineSchedule> pipelineSchedules = gitLabApi.getPipelineApi().getPipelineSchedules(testProjectId);
assertFalse(pipelineSchedules.isEmpty());
}
@Test // Make sure the created schedule is present before updating
public void testModifyProjectPipeLineSchedule() throws GitLabApiException { List<PipelineSchedule> pipelineSchedules = gitLabApi.getPipelineApi().getPipelineSchedules(testProject);
assumeTrue(TEST_NAMESPACE != null && TEST_PROJECT_NAME != null); assertNotNull(pipelineSchedules);
assumeTrue(TEST_NAMESPACE.trim().length() > 0 && TEST_PROJECT_NAME.trim().length() > 0); assertTrue(pipelineSchedules.stream().map(PipelineSchedule::getDescription).collect(toList()).contains(scheduleDescription));
Integer testProjectId = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME).getId();
List<PipelineSchedule> pipelineSchedules = gitLabApi.getPipelineApi().getPipelineSchedules(testProjectId);
assertTrue(pipelineSchedules.size()==1);
PipelineSchedule existingPipelineSchedule = pipelineSchedules.get(0);
assertTrue(existingPipelineSchedule.getDescription().equals("test pipeline schedule"));
existingPipelineSchedule.setDescription("new name");
gitLabApi.getPipelineApi().modifyPipelineSchedule(testProjectId,existingPipelineSchedule);
pipelineSchedules = gitLabApi.getPipelineApi().getPipelineSchedules(testProjectId);
PipelineSchedule newPipelineSchedule = pipelineSchedules.get(0);
assertTrue(pipelineSchedules.size()==1);
assertTrue(newPipelineSchedule.equals("new name"));
}
String newScheduleDescription = scheduleDescription + " - updated";
createdPipelineSchedule.setDescription(newScheduleDescription);
gitLabApi.getPipelineApi().updatePipelineSchedule(testProject, createdPipelineSchedule);
pipelineSchedules = gitLabApi.getPipelineApi().getPipelineSchedules(testProject);
assertNotNull(pipelineSchedules);
List<String> scheduleDecriptions = pipelineSchedules.stream().map(PipelineSchedule::getDescription).collect(toList());
assertFalse(scheduleDecriptions.contains(scheduleDescription));
assertTrue(scheduleDecriptions.contains(newScheduleDescription));
}
@Test @Test
public void testDeleteProjectPipeLineSchedule() throws GitLabApiException { public void testDeleteProjectPipeLineSchedule() throws GitLabApiException {
assumeTrue(TEST_NAMESPACE != null && TEST_PROJECT_NAME != null);
assumeTrue(TEST_NAMESPACE.trim().length() > 0 && TEST_PROJECT_NAME.trim().length() > 0); assertNotNull(testProject);
Integer testProjectId = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME).getId(); String scheduleDescription = SCHEDULE_DESCRIPTION + " - test deletePipelineSchedule()";
List<PipelineSchedule> pipelineSchedules = gitLabApi.getPipelineApi().getPipelineSchedules(testProjectId); PipelineSchedule newPipelineSchedule = new PipelineSchedule();
assertFalse(pipelineSchedules.isEmpty()); newPipelineSchedule.setDescription(scheduleDescription);
gitLabApi.getPipelineApi().deletePipelineSchedule(testProjectId,pipelineSchedules.get(0).getId()); newPipelineSchedule.setCron("1 4 * * *");
pipelineSchedules = gitLabApi.getPipelineApi().getPipelineSchedules(testProjectId); newPipelineSchedule.setRef("master");
assertTrue(pipelineSchedules.isEmpty()); PipelineSchedule createdPipelineSchedule = gitLabApi.getPipelineApi().createPipelineSchedule(testProject, newPipelineSchedule);
assertNotNull(createdPipelineSchedule);
// Make sure the created schedule is present before deleting
List<PipelineSchedule> pipelineSchedules = gitLabApi.getPipelineApi().getPipelineSchedules(testProject);
assertNotNull(pipelineSchedules);
assertTrue(pipelineSchedules.stream().map(PipelineSchedule::getDescription).collect(toList()).contains(scheduleDescription));
gitLabApi.getPipelineApi().deletePipelineSchedule(testProject, createdPipelineSchedule.getId());
pipelineSchedules = gitLabApi.getPipelineApi().getPipelineSchedules(testProject);
assertNotNull(pipelineSchedules);
assertFalse(pipelineSchedules.stream().map(PipelineSchedule::getDescription).collect(toList()).contains(scheduleDescription));
} }
} }
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment