Skip to content

Commit 0aca947

Browse files
committed
INitial commit (#175, #176).
1 parent 54a3670 commit 0aca947

File tree

2 files changed

+209
-0
lines changed

2 files changed

+209
-0
lines changed
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
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.Assume.assumeTrue;
8+
9+
import org.gitlab4j.api.GitLabApi.ApiVersion;
10+
import org.gitlab4j.api.models.Project;
11+
import org.gitlab4j.api.services.JiraService;
12+
import org.gitlab4j.api.services.SlackService;
13+
import org.junit.Before;
14+
import org.junit.BeforeClass;
15+
import org.junit.FixMethodOrder;
16+
import org.junit.Test;
17+
import org.junit.runners.MethodSorters;
18+
19+
/**
20+
* In order for these tests to run you must set the following properties in test-gitlab4j.properties
21+
*
22+
* TEST_NAMESPACE
23+
* TEST_PROJECT_NAME
24+
* TEST_HOST_URL
25+
* TEST_PRIVATE_TOKEN
26+
*
27+
* If any of the above are NULL, all tests in this class will be skipped.
28+
*/
29+
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
30+
public class TestServicesApi {
31+
32+
// The following needs to be set to your test repository
33+
private static final String TEST_PROJECT_NAME;
34+
private static final String TEST_NAMESPACE;
35+
private static final String TEST_HOST_URL;
36+
private static final String TEST_PRIVATE_TOKEN;
37+
static {
38+
TEST_NAMESPACE = TestUtils.getProperty("TEST_NAMESPACE");
39+
TEST_PROJECT_NAME = TestUtils.getProperty("TEST_PROJECT_NAME");
40+
TEST_HOST_URL = TestUtils.getProperty("TEST_HOST_URL");
41+
TEST_PRIVATE_TOKEN = TestUtils.getProperty("TEST_PRIVATE_TOKEN");
42+
}
43+
44+
private static GitLabApi gitLabApi;
45+
private static Project testProject;
46+
47+
public TestServicesApi() {
48+
super();
49+
}
50+
51+
@BeforeClass
52+
public static void setup() {
53+
54+
String problems = "";
55+
if (TEST_NAMESPACE == null || TEST_NAMESPACE.trim().isEmpty()) {
56+
problems += "TEST_NAMESPACE cannot be empty\n";
57+
}
58+
59+
if (TEST_PROJECT_NAME == null || TEST_PROJECT_NAME.trim().isEmpty()) {
60+
problems += "TEST_PROJECT_NAME cannot be empty\n";
61+
}
62+
63+
if (TEST_HOST_URL == null || TEST_HOST_URL.trim().isEmpty()) {
64+
problems += "TEST_HOST_URL cannot be empty\n";
65+
}
66+
67+
if (TEST_PRIVATE_TOKEN == null || TEST_PRIVATE_TOKEN.trim().isEmpty()) {
68+
problems += "TEST_PRIVATE_TOKEN cannot be empty\n";
69+
}
70+
71+
if (problems.isEmpty()) {
72+
gitLabApi = new GitLabApi(ApiVersion.V4, TEST_HOST_URL, TEST_PRIVATE_TOKEN);
73+
74+
try {
75+
testProject = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME);
76+
try { gitLabApi.getServicesApi().deleteJiraService(testProject); } catch (Exception ignore) {}
77+
try { gitLabApi.getServicesApi().deleteSlackService(testProject); } catch (Exception ignore) {}
78+
} catch (GitLabApiException gle) {
79+
System.err.print(gle.getMessage());
80+
}
81+
82+
} else {
83+
System.err.print(problems);
84+
}
85+
}
86+
87+
@Before
88+
public void beforeMethod() {
89+
assumeTrue(gitLabApi != null && testProject != null);
90+
}
91+
92+
@Test
93+
public void testProjectIdOrPath() throws GitLabApiException {
94+
Integer projectId = testProject.getId();
95+
JiraService jiraServiceById = gitLabApi.getServicesApi().getJiraService(projectId);
96+
assertNotNull(jiraServiceById);
97+
JiraService jiraServiceByPath = gitLabApi.getServicesApi().getJiraService(testProject.getPathWithNamespace());
98+
assertNotNull(jiraServiceByPath);
99+
JiraService jiraServiceByProject = gitLabApi.getServicesApi().getJiraService(testProject);
100+
assertNotNull(jiraServiceByProject);
101+
102+
assertEquals(jiraServiceById.getTitle(), jiraServiceByPath.getTitle());
103+
assertEquals(jiraServiceById.getTitle(), jiraServiceByProject.getTitle());
104+
}
105+
106+
@Test
107+
public void testGetJiraService() throws GitLabApiException {
108+
JiraService jiraService = gitLabApi.getServicesApi().getJiraService(testProject);
109+
assertNotNull(jiraService);
110+
}
111+
112+
@Test
113+
public void testUpdateJiraService() throws GitLabApiException {
114+
115+
try {
116+
JiraService jiraService = new JiraService()
117+
.withCommitEvents(true)
118+
.withMergeRequestsEvents(true)
119+
.withUrl("http://jira.example.com")
120+
.withUsername("GitLab4J")
121+
.withPassword("test")
122+
.withProjectKey("GL4J");
123+
JiraService updatedJiraService = gitLabApi.getServicesApi().updateJiraService(testProject, jiraService);
124+
assertNotNull(updatedJiraService);
125+
} finally {
126+
try { gitLabApi.getServicesApi().deleteJiraService(testProject); } catch (Exception ignore) {}
127+
}
128+
}
129+
130+
@Test
131+
public void testDeleteJiraService() throws GitLabApiException {
132+
133+
JiraService jiraService = new JiraService()
134+
.withCommitEvents(true)
135+
.withMergeRequestsEvents(true)
136+
.withUrl("http://jira.example.com")
137+
.withUsername("GitLab4J")
138+
.withPassword("test")
139+
.withProjectKey("GL4J");
140+
JiraService updatedJiraService = gitLabApi.getServicesApi().updateJiraService(testProject, jiraService);
141+
assertNotNull(updatedJiraService);
142+
assertTrue(updatedJiraService.getActive());
143+
144+
gitLabApi.getServicesApi().deleteJiraService(testProject);
145+
JiraService deleteJiraService = gitLabApi.getServicesApi().getJiraService(testProject);
146+
assertNotNull(deleteJiraService);
147+
assertFalse(deleteJiraService.getActive());
148+
}
149+
150+
@Test
151+
public void testGetSlackService() throws GitLabApiException {
152+
SlackService slackService = gitLabApi.getServicesApi().getSlackService(testProject);
153+
assertNotNull(slackService);
154+
}
155+
156+
@Test
157+
public void testUpdateSlackService() throws GitLabApiException {
158+
159+
try {
160+
SlackService slackService = new SlackService()
161+
.withMergeRequestsEvents(true)
162+
.withWebhook("https://hooks.slack.com/services/ABCDEFGHI/KJLMNOPQR/wetrewq7897HKLH8998wfjjj")
163+
.withUsername("GitLab4J");
164+
SlackService updatedSlackService = gitLabApi.getServicesApi().updateSlackService(testProject, slackService);
165+
assertNotNull(updatedSlackService);
166+
} finally {
167+
try { gitLabApi.getServicesApi().deleteSlackService(testProject); } catch (Exception ignore) {}
168+
}
169+
}
170+
171+
@Test
172+
public void testDeleteSlackService() throws GitLabApiException {
173+
SlackService slackService = new SlackService()
174+
.withMergeRequestsEvents(true)
175+
.withWebhook("https://hooks.slack.com/services/ABCDEFGHI/KJLMNOPQR/wetrewq7897HKLH8998wfjjj")
176+
.withUsername("GitLab4J");
177+
SlackService updatedSlackService = gitLabApi.getServicesApi().updateSlackService(testProject, slackService);
178+
assertNotNull(updatedSlackService);
179+
assertTrue(updatedSlackService.getActive());
180+
181+
gitLabApi.getServicesApi().deleteSlackService(testProject);
182+
SlackService deleteSlackService = gitLabApi.getServicesApi().getSlackService(testProject);
183+
assertNotNull(deleteSlackService);
184+
assertFalse(deleteSlackService.getActive());
185+
}
186+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"id": 30,
3+
"title": "JIRA",
4+
"created_at": "2018-04-21T19:09:12.551Z",
5+
"updated_at": "2018-04-23T06:19:53.535Z",
6+
"active": true,
7+
"push_events": true,
8+
"issues_events": true,
9+
"confidential_issues_events": true,
10+
"merge_requests_events": false,
11+
"tag_push_events": true,
12+
"note_events": true,
13+
"confidential_note_events": true,
14+
"pipeline_events": true,
15+
"wiki_page_events": true,
16+
"job_events": true,
17+
"properties": {
18+
"url": "https://jira.example.com",
19+
"api_url": "",
20+
"username": "test",
21+
"jira_issue_transition_id": ""
22+
}
23+
}

0 commit comments

Comments
 (0)