forked from gitlab4j/gitlab4j-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestGitLabApiException.java
138 lines (115 loc) · 5.26 KB
/
TestGitLabApiException.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package org.gitlab4j.api;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
import java.util.List;
import java.util.Map;
import javax.ws.rs.core.Response.Status;
import org.gitlab4j.api.models.Project;
import org.gitlab4j.api.models.Visibility;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
/**
* In order for these tests to run you must set the following properties in ~/test-gitlab4j.properties
*
* TEST_NAMESPACE
* TEST_HOST_URL
* TEST_PRIVATE_TOKEN
*
* If any of the above are NULL, all tests in this class will be skipped.
*/
@Tag("integration")
@ExtendWith(SetupIntegrationTestExtension.class)
@org.junit.jupiter.api.Disabled("Integration tests are disabled, see https://github.com/gitlab4j/gitlab4j-api/issues/1165")
public class TestGitLabApiException extends AbstractIntegrationTest {
private static final String TEST_PROJECT_NAME_DUPLICATE = "test-gitlab4j-create-project-duplicate";
private static final String TEST_ERROR_MESSAGE = "Another open merge request already exists for this source branch: !6";
private static final String TEST_RESPONSE_JSON_STRING = "{\"message\": \"" + TEST_ERROR_MESSAGE + "\"}";
private static final String TEST_RESPONSE_JSON_ARRAY = "{\"message\": [\"" + TEST_ERROR_MESSAGE + "\"]}";
private static final String TEST_RESPONSE_ERROR_JSON_STRING = "{\"error\": \"" + TEST_ERROR_MESSAGE + "\"}";
private static GitLabApi gitLabApi;
public TestGitLabApiException() {
super();
}
@BeforeAll
public static void setup() {
// Must setup the connection to the GitLab test server
gitLabApi = baseTestSetup();
deleteAllTestProjects();
}
@AfterAll
public static void teardown() throws GitLabApiException {
deleteAllTestProjects();
}
private static void deleteAllTestProjects() {
if (gitLabApi != null) {
try {
Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME_DUPLICATE);
gitLabApi.getProjectApi().deleteProject(project);
} catch (GitLabApiException ignore) {}
}
}
@BeforeEach
public void beforeMethod() {
assumeTrue(gitLabApi != null);
}
@Test
public void testNotFoundError() throws GitLabApiException {
try {
gitLabApi.getProjectApi().getProject(123456789L);
fail("GitLabApiException not thrown");
} catch (GitLabApiException gae) {
assertFalse(gae.hasValidationErrors());
assertEquals(404, gae.getHttpStatus());
assertTrue(gae.getMessage().contains("404"));
assertFalse(gae.getHeaders().isEmpty());
assertTrue(gae.getHeaders().containsKey("X-Request-Id"), () -> "headers contains key 'X-Request-Id'. Available keys: " + String.join(", ", gae.getHeaders().keySet()));
}
}
@Test
public void testValidationErrors() throws GitLabApiException {
Project project = new Project()
.withName(TEST_PROJECT_NAME_DUPLICATE)
.withDescription("GitLab4J test project.")
.withVisibility(Visibility.PUBLIC);
Project newProject = gitLabApi.getProjectApi().createProject(project);
assertNotNull(newProject);
try {
newProject = gitLabApi.getProjectApi().createProject(project);
fail("GitLabApiException not thrown");
} catch (GitLabApiException gae) {
assertTrue(gae.hasValidationErrors());
Map<String, List<String>> validationErrors = gae.getValidationErrors();
assertNotNull(validationErrors);
assertFalse(validationErrors.isEmpty());
}
}
@Test
public void testStringMessage() throws GitLabApiException {
final MockResponse response = new MockResponse(Status.BAD_REQUEST, TEST_RESPONSE_JSON_STRING);
GitLabApiException glae = new GitLabApiException(response);
assertEquals(Status.BAD_REQUEST.getStatusCode(), glae.getHttpStatus());
assertEquals(TEST_ERROR_MESSAGE, glae.getMessage());
}
@Test
public void testArrayMessage() throws GitLabApiException {
final MockResponse response = new MockResponse(Status.BAD_REQUEST, TEST_RESPONSE_JSON_ARRAY);
GitLabApiException glae = new GitLabApiException(response);
assertEquals(Status.BAD_REQUEST.getStatusCode(), glae.getHttpStatus());
assertEquals(TEST_ERROR_MESSAGE, glae.getMessage());
}
@Test
public void testError() throws GitLabApiException {
final MockResponse response = new MockResponse(Status.BAD_REQUEST, TEST_RESPONSE_ERROR_JSON_STRING);
GitLabApiException glae = new GitLabApiException(response);
assertEquals(Status.BAD_REQUEST.getStatusCode(), glae.getHttpStatus());
assertEquals(TEST_ERROR_MESSAGE, glae.getMessage());
}
}