Skip to content

Commit eac251a

Browse files
committed
Initial commit for support of the Applications API (#338).
1 parent 15e3818 commit eac251a

File tree

4 files changed

+265
-0
lines changed

4 files changed

+265
-0
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package org.gitlab4j.api;
2+
3+
import java.util.List;
4+
import java.util.stream.Collectors;
5+
import java.util.stream.Stream;
6+
7+
import javax.ws.rs.core.GenericType;
8+
import javax.ws.rs.core.Response;
9+
10+
import org.gitlab4j.api.models.Application;
11+
12+
/**
13+
* This class implements the client side API for the GitLab Applications API.
14+
* See <a href="https://docs.gitlab.com/ce/api/applications.html">Applications API at GitLab</a> for more information.
15+
*/
16+
public class ApplicationsApi extends AbstractApi {
17+
18+
public ApplicationsApi(GitLabApi gitLabApi) {
19+
super(gitLabApi);
20+
}
21+
22+
/**
23+
* Get all OATH applications.
24+
*
25+
* @return a List of OAUTH Application instances
26+
* @throws GitLabApiException if any exception occurs
27+
*/
28+
public List<Application> getApplications() throws GitLabApiException {
29+
return (getApplications(getDefaultPerPage()).all());
30+
}
31+
32+
/**
33+
* Get all OAUTH applications using the specified page and per page setting
34+
*
35+
* @param page the page to get
36+
* @param perPage the number of items per page
37+
* @return a list of OAUTH Applications in the specified range
38+
* @throws GitLabApiException if any exception occurs
39+
*/
40+
public List<Application> getApplications(int page, int perPage) throws GitLabApiException {
41+
Response response = get(javax.ws.rs.core.Response.Status.OK, getPageQueryParams(page, perPage), "applications");
42+
return (response.readEntity(new GenericType<List<Application>>() {}));
43+
}
44+
45+
/**
46+
* Get a Pager of all OAUTH applications.
47+
*
48+
* @param itemsPerPage the number of items per page
49+
* @return a Pager of Application instances in the specified range
50+
* @throws GitLabApiException if any exception occurs
51+
*/
52+
public Pager<Application> getApplications(int itemsPerPage) throws GitLabApiException {
53+
return (new Pager<Application>(this, Application.class, itemsPerPage, null, "applications"));
54+
}
55+
56+
/**
57+
* Get a Stream of all OAUTH Application instances.
58+
*
59+
* @return a Stream of OAUTH Application instances
60+
* @throws GitLabApiException if any exception occurs
61+
*/
62+
public Stream<Application> getApplicationsStream() throws GitLabApiException {
63+
return (getApplications(getDefaultPerPage()).stream());
64+
}
65+
66+
/**
67+
* Create an OAUTH Application.
68+
*
69+
* @param name the name for the OAUTH Application
70+
* @param redirectUri the redirect URI for the OAUTH Application
71+
* @param scopes the scopes of the application (api, read_user, sudo, read_repository, openid, profile, email)
72+
* @return the created Application instance
73+
* @throws GitLabApiException if any exception occurs
74+
*/
75+
public Application createApplication(String name, String redirectUri, List<ApplicationScope> scopes) throws GitLabApiException {
76+
77+
if (scopes == null || scopes.isEmpty()) {
78+
throw new GitLabApiException("scopes cannot be null or empty");
79+
}
80+
81+
String scopesString = scopes.stream().map(ApplicationScope::toString).collect(Collectors.joining(","));
82+
GitLabApiForm formData = new GitLabApiForm()
83+
.withParam("name", name, true)
84+
.withParam("redirect_uri", redirectUri, true)
85+
.withParam("scopes", scopesString, true);
86+
Response response = post(Response.Status.CREATED, formData, "applications");
87+
return (response.readEntity(Application.class));
88+
}
89+
90+
/**
91+
* Delete the specified OAUTH Application.
92+
*
93+
* @param applicationId the ID of the OUAUTH Application to delete
94+
* @throws GitLabApiException if any exception occurs
95+
*/
96+
public void deleteApplication(Integer applicationId) throws GitLabApiException {
97+
delete(Response.Status.NO_CONTENT, null, "applications", applicationId);
98+
}
99+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package org.gitlab4j.api.models;
2+
3+
public class Application {
4+
5+
private Integer id;
6+
private String applicationId;
7+
private String applicationName;
8+
private String callbackUrl;
9+
10+
public Integer getId() {
11+
return id;
12+
}
13+
14+
public void setId(Integer id) {
15+
this.id = id;
16+
}
17+
18+
public String getApplicationId() {
19+
return applicationId;
20+
}
21+
22+
public void setApplicationId(String applicationId) {
23+
this.applicationId = applicationId;
24+
}
25+
26+
public String getApplicationName() {
27+
return applicationName;
28+
}
29+
30+
public void setApplicationName(String applicationName) {
31+
this.applicationName = applicationName;
32+
}
33+
34+
public String getCallbackUrl() {
35+
return callbackUrl;
36+
}
37+
38+
public void setCallbackUrl(String callbackUrl) {
39+
this.callbackUrl = callbackUrl;
40+
}
41+
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2017 Greg Messner <greg@messners.com>
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy of
7+
* this software and associated documentation files (the "Software"), to deal in
8+
* the Software without restriction, including without limitation the rights to
9+
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10+
* the Software, and to permit persons to whom the Software is furnished to do so,
11+
* subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18+
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20+
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22+
*/
23+
24+
package org.gitlab4j.api;
25+
26+
import static org.junit.Assert.assertNotNull;
27+
import static org.junit.Assert.assertNull;
28+
import static org.junit.Assert.assertTrue;
29+
import static org.junit.Assume.assumeNotNull;
30+
31+
import java.util.Arrays;
32+
import java.util.List;
33+
34+
import org.gitlab4j.api.Constants.ApplicationScope;
35+
import org.gitlab4j.api.models.Application;
36+
import org.junit.AfterClass;
37+
import org.junit.Before;
38+
import org.junit.BeforeClass;
39+
import org.junit.Test;
40+
import org.junit.experimental.categories.Category;
41+
42+
/**
43+
* In order for these tests to run you must set the following properties in test-gitlab4j.properties
44+
* <p>
45+
* TEST_HOST_URL
46+
* TEST_PRIVATE_TOKEN
47+
* <p>
48+
* If any of the above are NULL, all tests in this class will be skipped.
49+
*/
50+
@Category(IntegrationTest.class)
51+
public class TestApplications extends AbstractIntegrationTest {
52+
53+
private static final String TEST_APPLICATION_NAME = "Test Application for GitLab4J-API";
54+
private static final String TEST_APPLICATION_REDIRECT = "http://example.com/application";
55+
private static final List<ApplicationScope> TEST_APPLICATION_SCOPES =
56+
Arrays.asList(ApplicationScope.SUDO, ApplicationScope.EMAIL);
57+
58+
private static GitLabApi gitLabApi;
59+
60+
public TestApplications() {
61+
super();
62+
}
63+
64+
@BeforeClass
65+
public static void setup() {
66+
// Must setup the connection to the GitLab test server
67+
gitLabApi = baseTestSetup();
68+
69+
if (gitLabApi != null) {
70+
try {
71+
List<Application> apps = gitLabApi.getApplicationsApi().getApplications();
72+
for (Application app : apps) {
73+
if (TEST_APPLICATION_NAME.equals(app.getApplicationName())) {
74+
gitLabApi.getApplicationsApi().deleteApplication(app.getId());
75+
}
76+
}
77+
} catch (Exception ignore) {}
78+
}
79+
}
80+
81+
@AfterClass
82+
public static void teardown() throws GitLabApiException {
83+
}
84+
85+
@Before
86+
public void beforeMethod() {
87+
assumeNotNull(gitLabApi);
88+
}
89+
90+
@Test
91+
public void testGetApplications() throws GitLabApiException {
92+
List<Application> apps = gitLabApi.getApplicationsApi().getApplications();
93+
assertNotNull(apps);
94+
}
95+
96+
@Test
97+
public void testAddAndDeleteApplication() throws GitLabApiException {
98+
99+
List<Application> apps = gitLabApi.getApplicationsApi().getApplications();
100+
int appCount = apps.size();
101+
102+
Application app = gitLabApi.getApplicationsApi().createApplication(TEST_APPLICATION_NAME, TEST_APPLICATION_REDIRECT, TEST_APPLICATION_SCOPES);
103+
assertNotNull(app);
104+
105+
apps = gitLabApi.getApplicationsApi().getApplications();
106+
assertTrue(apps.size() == appCount + 1);
107+
108+
Application found = apps.stream().filter(a -> TEST_APPLICATION_NAME.equals(a.getApplicationName())).findAny().orElse(null);
109+
assertNotNull(found);
110+
111+
gitLabApi.getApplicationsApi().deleteApplication(app.getId());
112+
apps = gitLabApi.getApplicationsApi().getApplications();
113+
assertTrue(apps.size() == appCount);
114+
found = apps.stream().filter(a -> TEST_APPLICATION_NAME.equals(a.getApplicationName())).findAny().orElse(null);
115+
assertNull(found);
116+
}
117+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[
2+
{
3+
"id": 1,
4+
"application_id": "5832fc6e14300a0d962240a8144466eef4ee93ef0d218477e55f11cf12fc3737",
5+
"application_name": "MyApplication",
6+
"callback_url": "http://redirect.uri"
7+
}
8+
]

0 commit comments

Comments
 (0)