diff --git a/README.md b/README.md index 7065e497a6aec68ad85a07bc45d086e502081f68..2d2c0a77149354b2affec673210ec616783f1113 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ To utilize the GitLab API for Java in your project, simply add the following dep ```java dependencies { ... - compile group: 'org.gitlab4j', name: 'gitlab4j-api', version: '4.4.7' + compile group: 'org.gitlab4j', name: 'gitlab4j-api', version: '4.4.8' } ``` @@ -20,11 +20,11 @@ dependencies { org.gitlab4j gitlab4j-api - 4.4.7 + 4.4.8 ``` -If you are not using Gradle or Maven you can download the latest gitlab4j-api JAR file here: [gitlab4j-api-4.4.7.jar](https://oss.sonatype.org/service/local/repositories/releases/content/org/gitlab4j/gitlab4j-api/4.4.7/gitlab4j-api-4.4.7.jar "Download JAR") +If you are not using Gradle or Maven you can download the latest gitlab4j-api JAR file here: [gitlab4j-api-4.4.8.jar](https://oss.sonatype.org/service/local/repositories/releases/content/org/gitlab4j/gitlab4j-api/4.4.8/gitlab4j-api-4.4.8.jar "Download JAR") Javadocs are available here: Javadocs diff --git a/src/main/java/org/gitlab4j/api/ProjectApi.java b/src/main/java/org/gitlab4j/api/ProjectApi.java index 1dcfda4e06bd26c86a78283e922333eec5fc79dc..0355ba479df80f8668c00adee1a2a1afba08a974 100644 --- a/src/main/java/org/gitlab4j/api/ProjectApi.java +++ b/src/main/java/org/gitlab4j/api/ProjectApi.java @@ -1,3 +1,26 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2017 Greg Messner + * + * 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 org.gitlab4j.api; import java.io.UnsupportedEncodingException; @@ -14,6 +37,7 @@ import org.gitlab4j.api.models.Issue; import org.gitlab4j.api.models.Member; import org.gitlab4j.api.models.Project; import org.gitlab4j.api.models.ProjectHook; +import org.gitlab4j.api.models.Snippet; import org.gitlab4j.api.models.Visibility; /** @@ -1265,4 +1289,147 @@ public class ProjectApi extends AbstractApi implements Constants { Response.Status expectedStatus = (isApiVersion(ApiVersion.V3) ? Response.Status.OK : Response.Status.NO_CONTENT); delete(expectedStatus, getDefaultPerPageParam(), "projects", projectId, "issues", issueId); } + + /** + * Get a list of project snippets. This only returns the first page of snippets. + * + * GET /projects/:id/snippets + * + * @param projectId the project ID to get the snippets for + * @return a list of project's snippets + * @throws GitLabApiException if any exception occurs + */ + public List getSnippets(Integer projectId) throws GitLabApiException { + return (getSnippets(projectId, 1, this.getDefaultPerPage())); + } + + /** + * Get a list of project snippets. This only returns the first page of snippets. + * + * GET /projects/:id/snippets + * + * @param projectId the project ID to get the snippets for + * @param page the page to get + * @param perPage the number of snippets per page + * @return a list of project's snippets for the specified range + * @throws GitLabApiException if any exception occurs + */ + public List getSnippets(Integer projectId, int page, int perPage) throws GitLabApiException { + Response response = get(Response.Status.OK, getPageQueryParams(page, perPage), "projects", projectId, "snippets"); + return (response.readEntity(new GenericType>() {})); + } + + /** + * Get a Pager of project's snippets. + * + * GET /projects/:id/snippets + * + * @param projectId the project ID to get the issues for + * @param itemsPerPage the number of snippets per page + * @return the Pager of snippets + * @throws GitLabApiException if any exception occurs + */ + public Pager getSnippets(Integer projectId, int itemsPerPage) throws GitLabApiException { + return (new Pager(this, Snippet.class, itemsPerPage, null, "projects", projectId, "snippets")); + } + + /** + * Get a single of project snippet. + * + * GET /projects/:id/snippets/:snippet_id + * + * @param projectId the project ID to get the snippet for + * @param snippetId the ID of the project's snippet + * @return the specified project Snippet + * @throws GitLabApiException if any exception occurs + */ + public Snippet getSnippet(Integer projectId, Integer snippetId) throws GitLabApiException { + Response response = get(Response.Status.OK, null, "projects", projectId, "snippets", snippetId); + return (response.readEntity(Snippet.class)); + } + + /** + * Creates a new project snippet. The user must have permission to create new snippets. + * + * POST /projects/:id/snippets + * + * @param id the ID of the project owned by the authenticated user, required + * @param title the title of a snippet, required + * @param fileName the name of a snippet file, required + * @param description the description of a snippet, optional + * @param code the content of a snippet, required + * @param visibility the snippet's visibility, required + * @return a Snippet instance with info on the created snippet + * @throws GitLabApiException if any exception occurs + */ + public Snippet createSnippet(Integer projectId, String title, String filename, String description, + String code, Visibility visibility) throws GitLabApiException { + + GitLabApiForm formData = new GitLabApiForm() + .withParam("title", title, true) + .withParam("file_name", filename, true) + .withParam("description", description) + .withParam("code", code, true) + .withParam("visibility", visibility, true); + + Response response = post(Response.Status.CREATED, formData, "projects", projectId, "snippets"); + return (response.readEntity(Snippet.class)); + } + + /** + * Updates an existing project snippet. The user must have permission to change an existing snippet. + * + * PUT /projects/:id/snippets/:snippet_id + * + * @param id the ID of the project owned by the authenticated user, required + * @param snippetId the ID of a project's snippet, required + * @param title the title of a snippet, optional + * @param fileName the name of a snippet file, optional + * @param description the description of a snippet, optioptionalonal + * @param code the content of a snippet, optional + * @param visibility the snippet's visibility, reqoptionaluired + * @return a Snippet instance with info on the updated snippet + * @throws GitLabApiException if any exception occurs + */ + public Snippet updateSnippet(Integer projectId, Integer snippetId, String title, String filename, String description, + String code, Visibility visibility) throws GitLabApiException { + + GitLabApiForm formData = new GitLabApiForm() + .withParam("title", title) + .withParam("file_name", filename) + .withParam("description", description) + .withParam("code", code) + .withParam("visibility", visibility); + + Response response = put(Response.Status.OK, formData.asMap(), "projects", projectId, "snippets", snippetId); + return (response.readEntity(Snippet.class)); + } + + /* + * Deletes an existing project snippet. This is an idempotent function and deleting a + * non-existent snippet does not cause an error. + * + * DELETE /projects/:id/snippets/:snippet_id + * + * @param projectId the project ID of the snippet + * @param snippetId the ID of the project's snippet + * @throws GitLabApiException if any exception occurs + */ + public void deleteSnippet(Integer projectId, Integer snippetId) throws GitLabApiException { + delete(Response.Status.NO_CONTENT, null, "projects", projectId, "snippets", snippetId); + } + + /* + * Get the raw project snippet as plain text. + * + * GET /projects/:id/snippets/:snippet_id/raw + * + * @param projectId the project ID of the snippet + * @param snippetId the ID of the project's snippet + * @throws GitLabApiException if any exception occurs + */ + public String getRawSnippetContent(Integer projectId, Integer snippetId) throws GitLabApiException { + Response response = get(Response.Status.OK, null, "projects", projectId, "snippets", snippetId, "raw"); + return (response.readEntity(String.class)); + } } diff --git a/src/main/java/org/gitlab4j/api/models/JobStatus.java b/src/main/java/org/gitlab4j/api/models/JobStatus.java index 9f625f0db3c20775408e5fb515294306b3828954..16e86c7ce055b8254f8c95b41e8f92bdfd8a238a 100644 --- a/src/main/java/org/gitlab4j/api/models/JobStatus.java +++ b/src/main/java/org/gitlab4j/api/models/JobStatus.java @@ -1,7 +1,29 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2017 Greg Messner + * + * 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 org.gitlab4j.api.models; -import java.util.HashMap; -import java.util.Map; +import org.gitlab4j.api.utils.JacksonJsonEnumHelper; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonValue; @@ -13,24 +35,20 @@ public enum JobStatus { RUNNING, PENDING, SUCCESS, FAILED, CANCELED, SKIPPED; - private static Map valuesMap = new HashMap<>(6); - static { - for (JobStatus status : JobStatus.values()) - valuesMap.put(status.toValue(), status); - } + private static JacksonJsonEnumHelper enumHelper = new JacksonJsonEnumHelper<>(JobStatus.class); @JsonCreator public static JobStatus forValue(String value) { - return valuesMap.get(value); - } + return enumHelper.forValue(value); + } @JsonValue public String toValue() { - return (name().toLowerCase()); - } + return (enumHelper.toString(this)); + } @Override public String toString() { - return (name().toLowerCase()); - } + return (enumHelper.toString(this)); + } } \ No newline at end of file diff --git a/src/main/java/org/gitlab4j/api/models/ProjectSnippet.java b/src/main/java/org/gitlab4j/api/models/Snippet.java similarity index 58% rename from src/main/java/org/gitlab4j/api/models/ProjectSnippet.java rename to src/main/java/org/gitlab4j/api/models/Snippet.java index e5de3e9e0538351e45adfd4b0d8fd72623b71c6f..739d21740e485941995f25b7df40b0d87b53a8cd 100644 --- a/src/main/java/org/gitlab4j/api/models/ProjectSnippet.java +++ b/src/main/java/org/gitlab4j/api/models/Snippet.java @@ -1,3 +1,26 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2017 Greg Messner + * + * 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 org.gitlab4j.api.models; import java.util.Date; @@ -8,7 +31,7 @@ import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement @XmlAccessorType(XmlAccessType.FIELD) -public class ProjectSnippet { +public class Snippet { private Author author; private Date createdAt; diff --git a/src/main/java/org/gitlab4j/api/models/Visibility.java b/src/main/java/org/gitlab4j/api/models/Visibility.java index 714e99c97c5706ef226991a7cd18fc97204a217b..68f3d74ad18f0b39f865c72abcf217e4a85dcaed 100644 --- a/src/main/java/org/gitlab4j/api/models/Visibility.java +++ b/src/main/java/org/gitlab4j/api/models/Visibility.java @@ -1,7 +1,29 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2017 Greg Messner + * + * 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 org.gitlab4j.api.models; -import java.util.HashMap; -import java.util.Map; +import org.gitlab4j.api.utils.JacksonJsonEnumHelper; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonValue; @@ -10,24 +32,20 @@ public enum Visibility { PUBLIC, PRIVATE, INTERNAL; - private static Map valuesMap = new HashMap<>(3); - static { - for (Visibility visibility : Visibility.values()) - valuesMap.put(visibility.toValue(), visibility); - } + private static JacksonJsonEnumHelper enumHelper = new JacksonJsonEnumHelper<>(Visibility.class); @JsonCreator public static Visibility forValue(String value) { - return valuesMap.get(value); + return enumHelper.forValue(value); } @JsonValue public String toValue() { - return (name().toLowerCase()); + return (enumHelper.toString(this)); } @Override public String toString() { - return (name().toLowerCase()); + return (enumHelper.toString(this)); } } diff --git a/src/test/java/org/gitlab4j/api/TestGitLabApiBeans.java b/src/test/java/org/gitlab4j/api/TestGitLabApiBeans.java index b0d57b051ead71f0bb011c74e3b5e1ee48b1b671..67c43d4be272f79418b1d8de857b8054b909373f 100644 --- a/src/test/java/org/gitlab4j/api/TestGitLabApiBeans.java +++ b/src/test/java/org/gitlab4j/api/TestGitLabApiBeans.java @@ -1,3 +1,26 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2017 Greg Messner + * + * 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 org.gitlab4j.api; import static org.junit.Assert.assertTrue; @@ -25,8 +48,8 @@ import org.gitlab4j.api.models.Note; import org.gitlab4j.api.models.Pipeline; import org.gitlab4j.api.models.Project; import org.gitlab4j.api.models.ProjectHook; -import org.gitlab4j.api.models.ProjectSnippet; import org.gitlab4j.api.models.Session; +import org.gitlab4j.api.models.Snippet; import org.gitlab4j.api.models.SshKey; import org.gitlab4j.api.models.SystemHook; import org.gitlab4j.api.models.Tag; @@ -274,8 +297,8 @@ public class TestGitLabApiBeans { public void testProjectSnippet() { try { - ProjectSnippet projectSnippet = makeFakeApiCall(ProjectSnippet.class, "project-snippet"); - assertTrue(compareJson(projectSnippet, "project-snippet")); + Snippet snippet = makeFakeApiCall(Snippet.class, "snippet"); + assertTrue(compareJson(snippet, "snippet")); } catch (Exception e) { e.printStackTrace(); } diff --git a/src/test/java/org/gitlab4j/api/TestProjectApi.java b/src/test/java/org/gitlab4j/api/TestProjectApi.java index c550811d9873b163122d824b0609a155be8aaf58..7879e0c915367df18cdb6b1f78982b0e89672095 100644 --- a/src/test/java/org/gitlab4j/api/TestProjectApi.java +++ b/src/test/java/org/gitlab4j/api/TestProjectApi.java @@ -1,3 +1,26 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2017 Greg Messner + * + * 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 org.gitlab4j.api; import static org.junit.Assert.assertEquals; @@ -18,9 +41,10 @@ import org.junit.Test; import org.junit.runners.MethodSorters; /** - * In order for these tests to run you must set the following properties in test-gitlab4j.properties + * In order for these tests to run you must set the following properties in ~/test-gitlab4j.properties * * TEST_NAMESPACE + * TEST_PROJECT_NAME * TEST_HOST_URL * TEST_PRIVATE_TOKEN * @@ -32,7 +56,6 @@ import org.junit.runners.MethodSorters; public class TestProjectApi { // The following needs to be set to your test repository - private static final String TEST_NAMESPACE; private static final String TEST_PROJECT_NAME; private static final String TEST_HOST_URL; diff --git a/src/test/java/org/gitlab4j/api/TestProjectApiSnippets.java b/src/test/java/org/gitlab4j/api/TestProjectApiSnippets.java new file mode 100644 index 0000000000000000000000000000000000000000..763015c52dc4b25b0cbcab6f197bf1c895f84e26 --- /dev/null +++ b/src/test/java/org/gitlab4j/api/TestProjectApiSnippets.java @@ -0,0 +1,217 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2017 Greg Messner + * + * 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 org.gitlab4j.api; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; +import static org.junit.Assume.assumeTrue; + +import java.util.List; + +import org.gitlab4j.api.GitLabApi.ApiVersion; +import org.gitlab4j.api.models.Project; +import org.gitlab4j.api.models.Snippet; +import org.gitlab4j.api.models.Visibility; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +/** + * In order for these tests to run you must set the following properties in ~/test-gitlab4j.properties + * + * TEST_NAMESPACE + * TEST_PROJECT_NAME + * TEST_HOST_URL + * TEST_PRIVATE_TOKEN + * + * If any of the above are NULL, all tests in this class will be skipped. + */ +public class TestProjectApiSnippets { + + // The following needs to be set to your test repository + private static final String TEST_NAMESPACE; + private static final String TEST_PROJECT_NAME; + private static final String TEST_HOST_URL; + private static final String TEST_PRIVATE_TOKEN; + + static { + TEST_NAMESPACE = TestUtils.getProperty("TEST_NAMESPACE"); + TEST_PROJECT_NAME = TestUtils.getProperty("TEST_PROJECT_NAME"); + TEST_HOST_URL = TestUtils.getProperty("TEST_HOST_URL"); + TEST_PRIVATE_TOKEN = TestUtils.getProperty("TEST_PRIVATE_TOKEN"); + } + + private static final String TEST_SNIPPET_TITLE_PREFIX = "Test Snippet: "; + private static GitLabApi gitLabApi; + private static Integer testProjectId; + + public TestProjectApiSnippets() { + super(); + } + + @BeforeClass + public static void setup() { + + String problems = ""; + if (TEST_NAMESPACE == null || TEST_NAMESPACE.trim().length() == 0) { + problems += "TEST_NAMESPACE cannot be empty\n"; + } + + if (TEST_HOST_URL == null || TEST_HOST_URL.trim().length() == 0) { + problems += "TEST_HOST_URL cannot be empty\n"; + } + + if (TEST_PRIVATE_TOKEN == null || TEST_PRIVATE_TOKEN.trim().length() == 0) { + problems += "TEST_PRIVATE_TOKEN cannot be empty\n"; + } + + if (problems.isEmpty()) { + gitLabApi = new GitLabApi(ApiVersion.V4, TEST_HOST_URL, TEST_PRIVATE_TOKEN); + } else { + System.err.print(problems); + } + + if (gitLabApi != null) { + try { + Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME); + testProjectId = project.getId(); + } catch (Exception e) { + System.err.print(e.getMessage()); + gitLabApi = null; + } + } + + deleteAllTestSnippets(); + } + + @AfterClass + public static void teardown() throws GitLabApiException { + deleteAllTestSnippets(); + } + + private static void deleteAllTestSnippets() { + if (gitLabApi != null) { + try { + List snippets = gitLabApi.getProjectApi().getSnippets(testProjectId); + if (snippets != null) { + for (Snippet snippet : snippets) { + if (snippet.getTitle().startsWith(TEST_SNIPPET_TITLE_PREFIX)) { + gitLabApi.getProjectApi().deleteSnippet(testProjectId, snippet.getId()); + } + } + } + } catch (GitLabApiException ignore) { + } + } + } + + @Before + public void beforeMethod() { + assumeTrue(gitLabApi != null); + } + + private Snippet createSnippet(String title, String filename, String description, + String code, Visibility visibility) throws GitLabApiException { + return (gitLabApi.getProjectApi().createSnippet(testProjectId, title, filename, description, code, visibility)); + } + + @Test + public void testCreate() throws GitLabApiException { + + String title = TEST_SNIPPET_TITLE_PREFIX + "Test createSnippet()"; + String filename = "test-create-snippet.js"; + String description = null; + String code = "window.open();"; + Visibility visibility = Visibility.PRIVATE; + Snippet snippet = createSnippet(title, filename, description, code, visibility); + assertNotNull(snippet); + assertEquals(title, snippet.getTitle()); + assertEquals(filename, snippet.getFileName()); + } + + @Test + public void testUpdate() throws GitLabApiException { + + String title = TEST_SNIPPET_TITLE_PREFIX + "Test createSnippet()"; + String filename = "test-update-snippet.js"; + String description = null; + String code = "window.open();"; + Visibility visibility = Visibility.INTERNAL; + Snippet snippet = createSnippet(title, filename, description, code, visibility); + assertNotNull(snippet); + + title = TEST_SNIPPET_TITLE_PREFIX + "Test updateSnippet()"; + snippet = gitLabApi.getProjectApi().updateSnippet(testProjectId, snippet.getId(), title, null, null, null, null); + assertEquals(title, snippet.getTitle()); + assertEquals(filename, snippet.getFileName()); + } + + @Test + public void testListSnippets() throws GitLabApiException { + + String title = TEST_SNIPPET_TITLE_PREFIX + "Test listSnippets()"; + String filename = "test-update-snippet.js"; + String description = null; + String code = "window.open();"; + Visibility visibility = Visibility.INTERNAL; + Snippet newSnippet = createSnippet(title, filename, description, code, visibility); + assertNotNull(newSnippet); + + int snippetId = newSnippet.getId(); + List snippets = gitLabApi.getProjectApi().getSnippets(testProjectId); + assertNotNull(snippets); + for (Snippet snippet : snippets) { + if (snippet.getId() == snippetId) { + assertEquals(title, snippet.getTitle()); + assertEquals(filename, snippet.getFileName()); + break; + } + } + } + + @Test + public void testDeleteSnippet() throws GitLabApiException { + + String title = TEST_SNIPPET_TITLE_PREFIX + "Test listSnippets()"; + String filename = "test-update-snippet.js"; + String description = null; + String code = "window.open();"; + Visibility visibility = Visibility.INTERNAL; + Snippet createdSnippet = createSnippet(title, filename, description, code, visibility); + assertNotNull(createdSnippet); + + int snippetId = createdSnippet.getId(); + gitLabApi.getProjectApi().deleteSnippet(testProjectId, snippetId); + List snippets = gitLabApi.getProjectApi().getSnippets(testProjectId); + if (snippets != null) { + for (Snippet snippet : snippets) { + if (snippet.getId() == snippetId) { + fail("Snippet was not deleted."); + } + } + } + } +} diff --git a/src/test/resources/org/gitlab4j/api/project-snippet.json b/src/test/resources/org/gitlab4j/api/snippet.json similarity index 100% rename from src/test/resources/org/gitlab4j/api/project-snippet.json rename to src/test/resources/org/gitlab4j/api/snippet.json