|
| 1 | +package org.gitlab4j.api; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 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.Assert.fail; |
| 8 | +import static org.junit.Assume.assumeTrue; |
| 9 | + |
| 10 | +import java.util.Arrays; |
| 11 | +import java.util.List; |
| 12 | +import java.util.Map; |
| 13 | + |
| 14 | +import org.gitlab4j.api.GitLabApi.ApiVersion; |
| 15 | +import org.gitlab4j.api.models.Project; |
| 16 | +import org.gitlab4j.api.models.Visibility; |
| 17 | +import org.junit.AfterClass; |
| 18 | +import org.junit.Before; |
| 19 | +import org.junit.BeforeClass; |
| 20 | +import org.junit.Test; |
| 21 | + |
| 22 | +/** |
| 23 | + * In order for these tests to run you must set the following properties in ~/test-gitlab4j.properties |
| 24 | + * |
| 25 | + * TEST_NAMESPACE |
| 26 | + * TEST_HOST_URL |
| 27 | + * TEST_PRIVATE_TOKEN |
| 28 | + * |
| 29 | + * If any of the above are NULL, all tests in this class will be skipped. |
| 30 | + */ |
| 31 | +public class TestGitLabApiException { |
| 32 | + |
| 33 | + // The following needs to be set to your test repository |
| 34 | + private static final String TEST_NAMESPACE; |
| 35 | + private static final String TEST_HOST_URL; |
| 36 | + private static final String TEST_PRIVATE_TOKEN; |
| 37 | + |
| 38 | + |
| 39 | + static { |
| 40 | + TEST_NAMESPACE = TestUtils.getProperty("TEST_NAMESPACE"); |
| 41 | + TEST_HOST_URL = TestUtils.getProperty("TEST_HOST_URL"); |
| 42 | + TEST_PRIVATE_TOKEN = TestUtils.getProperty("TEST_PRIVATE_TOKEN"); |
| 43 | + } |
| 44 | + |
| 45 | + private static final String TEST_PROJECT_NAME_DUPLICATE = "test-gitlab4j-create-project-duplicate"; |
| 46 | + private static GitLabApi gitLabApi; |
| 47 | + |
| 48 | + public TestGitLabApiException() { |
| 49 | + super(); |
| 50 | + } |
| 51 | + |
| 52 | + @BeforeClass |
| 53 | + public static void setup() { |
| 54 | + |
| 55 | + String problems = ""; |
| 56 | + if (TEST_NAMESPACE == null || TEST_NAMESPACE.trim().isEmpty()) { |
| 57 | + problems += "TEST_NAMESPACE cannot be empty\n"; |
| 58 | + } |
| 59 | + |
| 60 | + if (TEST_HOST_URL == null || TEST_HOST_URL.trim().isEmpty()) { |
| 61 | + problems += "TEST_HOST_URL cannot be empty\n"; |
| 62 | + } |
| 63 | + |
| 64 | + if (TEST_PRIVATE_TOKEN == null || TEST_PRIVATE_TOKEN.trim().isEmpty()) { |
| 65 | + problems += "TEST_PRIVATE_TOKEN cannot be empty\n"; |
| 66 | + } |
| 67 | + |
| 68 | + if (problems.isEmpty()) { |
| 69 | + gitLabApi = new GitLabApi(ApiVersion.V4, TEST_HOST_URL, TEST_PRIVATE_TOKEN); |
| 70 | + } else { |
| 71 | + System.err.print(problems); |
| 72 | + } |
| 73 | + |
| 74 | + deleteAllTestProjects(); |
| 75 | + } |
| 76 | + |
| 77 | + @AfterClass |
| 78 | + public static void teardown() throws GitLabApiException { |
| 79 | + deleteAllTestProjects(); |
| 80 | + } |
| 81 | + |
| 82 | + private static void deleteAllTestProjects() { |
| 83 | + if (gitLabApi != null) { |
| 84 | + try { |
| 85 | + Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME_DUPLICATE); |
| 86 | + gitLabApi.getProjectApi().deleteProject(project); |
| 87 | + } catch (GitLabApiException ignore) {} |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + @Before |
| 92 | + public void beforeMethod() { |
| 93 | + assumeTrue(gitLabApi != null); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void testNotFoundError() throws GitLabApiException { |
| 98 | + |
| 99 | + try { |
| 100 | + gitLabApi.getProjectApi().getProject(123456789); |
| 101 | + fail("GitLabApiException not thrown"); |
| 102 | + } catch (GitLabApiException gae) { |
| 103 | + assertFalse(gae.hasValidationErrors()); |
| 104 | + assertEquals(404, gae.getHttpStatus()); |
| 105 | + assertTrue(gae.getMessage().contains("404")); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + public void testValidationErrors() throws GitLabApiException { |
| 111 | + |
| 112 | + Project project = new Project() |
| 113 | + .withName(TEST_PROJECT_NAME_DUPLICATE) |
| 114 | + .withDescription("GitLab4J test project.") |
| 115 | + .withIssuesEnabled(true) |
| 116 | + .withMergeRequestsEnabled(true) |
| 117 | + .withWikiEnabled(true) |
| 118 | + .withSnippetsEnabled(true) |
| 119 | + .withVisibility(Visibility.PUBLIC) |
| 120 | + .withTagList(Arrays.asList("tag1", "tag2")); |
| 121 | + |
| 122 | + Project newProject = gitLabApi.getProjectApi().createProject(project); |
| 123 | + assertNotNull(newProject); |
| 124 | + |
| 125 | + try { |
| 126 | + newProject = gitLabApi.getProjectApi().createProject(project); |
| 127 | + fail("GitLabApiException not thrown"); |
| 128 | + } catch (GitLabApiException gae) { |
| 129 | + assertTrue(gae.hasValidationErrors()); |
| 130 | + Map<String, List<String>> validationErrors = gae.getValidationErrors(); |
| 131 | + assertNotNull(validationErrors); |
| 132 | + assertFalse(validationErrors.isEmpty()); |
| 133 | + } |
| 134 | + } |
| 135 | +} |
0 commit comments