Commit 63ee9bf8 authored by Greg Messner's avatar Greg Messner
Browse files

Added support for project snippets.

parent f336a43e
......@@ -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 {
<dependency>
<groupId>org.gitlab4j</groupId>
<artifactId>gitlab4j-api</artifactId>
<version>4.4.7</version>
<version>4.4.8</version>
</dependency>
```
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: <a href="http://www.messners.com/gitlab4j-api/javadocs/index.html?org/gitlab4j/api/package-summary.html" target="_top">Javadocs</a>
......
/*
* The MIT License (MIT)
*
* Copyright (c) 2017 Greg Messner <greg@messners.com>
*
* 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<Snippet> 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<Snippet> 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<List<Snippet>>() {}));
}
/**
* 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<Snippet> getSnippets(Integer projectId, int itemsPerPage) throws GitLabApiException {
return (new Pager<Snippet>(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));
}
}
/*
* The MIT License (MIT)
*
* Copyright (c) 2017 Greg Messner <greg@messners.com>
*
* 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<String, JobStatus> valuesMap = new HashMap<>(6);
static {
for (JobStatus status : JobStatus.values())
valuesMap.put(status.toValue(), status);
}
private static JacksonJsonEnumHelper<JobStatus> 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
/*
* The MIT License (MIT)
*
* Copyright (c) 2017 Greg Messner <greg@messners.com>
*
* 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;
......
/*
* The MIT License (MIT)
*
* Copyright (c) 2017 Greg Messner <greg@messners.com>
*
* 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<String, Visibility> valuesMap = new HashMap<>(3);
static {
for (Visibility visibility : Visibility.values())
valuesMap.put(visibility.toValue(), visibility);
}
private static JacksonJsonEnumHelper<Visibility> 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));
}
}
/*
* The MIT License (MIT)
*
* Copyright (c) 2017 Greg Messner <greg@messners.com>
*
* 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();
}
......
/*
* The MIT License (MIT)
*
* Copyright (c) 2017 Greg Messner <greg@messners.com>
*
* 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;
......
/*
* The MIT License (MIT)
*
* Copyright (c) 2017 Greg Messner <greg@messners.com>
*
* 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<Snippet> 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<Snippet> 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<Snippet> snippets = gitLabApi.getProjectApi().getSnippets(testProjectId);
if (snippets != null) {
for (Snippet snippet : snippets) {
if (snippet.getId() == snippetId) {
fail("Snippet was not deleted.");
}
}
}
}
}
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