Skip to content

Commit cc37d21

Browse files
committed
Add DeploymentsApi tests
1 parent bfadd89 commit cc37d21

File tree

4 files changed

+256
-8
lines changed

4 files changed

+256
-8
lines changed

src/main/java/org/gitlab4j/api/Constants.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -805,7 +805,9 @@ public String toString() {
805805

806806
/** Enum to use for specifying the status of a deployment. */
807807
public enum DeploymentStatus {
808-
808+
/**
809+
* After some tests, {@link #CREATED} value is not a valid value.
810+
*/
809811
CREATED, RUNNING, SUCCESS, FAILED, CANCELED;
810812

811813
private static JacksonJsonEnumHelper<DeploymentStatus> enumHelper = new JacksonJsonEnumHelper<>(DeploymentStatus.class);

src/main/java/org/gitlab4j/api/DeploymentsApi.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,8 @@ public Optional<Deployment> getOptionalDeployment(Object projectIdOrPath, Intege
148148
* @param environment The name of the environment to create the deployment for, required
149149
* @param sha The SHA of the commit that is deployed, required
150150
* @param ref The name of the branch or tag that is deployed, required
151-
* @param tag A boolean that indicates if the deployed ref is a tag (true) or not (false). , required
152-
* @param status The status to filter deployments by, optional
151+
* @param tag A boolean that indicates if the deployed ref is a tag (true) or not (false), required
152+
* @param status The status to filter deployments by, required
153153
* @return a Deployment instance with info on the added deployment
154154
* @throws GitLabApiException if any exception occurs
155155
*/
@@ -160,7 +160,8 @@ public Deployment addDeployment(Object projectIdOrPath, String environment, Stri
160160
.withParam("sha", sha, true)
161161
.withParam("ref", ref, true)
162162
.withParam("tag", tag, true)
163-
.withParam("status", status);
163+
.withParam("status", status, true);
164+
164165
Response response = post(Response.Status.CREATED, formData,
165166
"projects", getProjectIdOrPath(projectIdOrPath), "deployments");
166167
return (response.readEntity(Deployment.class));
@@ -173,7 +174,7 @@ public Deployment addDeployment(Object projectIdOrPath, String environment, Stri
173174
*
174175
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
175176
* @param deploymentId The ID of the deployment to update, required
176-
* @param status The new status of the deployment, optional
177+
* @param status The new status of the deployment, required
177178
* @return an updated Deployment instance
178179
* @throws GitLabApiException if any exception occurs
179180
*/
@@ -183,9 +184,9 @@ public Deployment updateDeployment(Object projectIdOrPath, Integer deploymentId,
183184
throw new RuntimeException("deploymentId cannot be null");
184185
}
185186

186-
final Deployment key = new Deployment();
187-
key.setStatus(status);
188-
final Response response = put(Response.Status.OK, key,
187+
final Deployment deployment = new Deployment();
188+
deployment.setStatus(status);
189+
final Response response = put(Response.Status.OK, deployment,
189190
"projects", getProjectIdOrPath(projectIdOrPath), "deployments", deploymentId);
190191

191192
return (response.readEntity(Deployment.class));

src/main/java/org/gitlab4j/api/GitLabApi.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public String getApiNamespace() {
5858
private ContainerRegistryApi containerRegistryApi;
5959
private DiscussionsApi discussionsApi;
6060
private DeployKeysApi deployKeysApi;
61+
private DeploymentsApi deploymentsApi;
6162
private DeployTokensApi deployTokensApi;
6263
private EnvironmentsApi environmentsApi;
6364
private EpicsApi epicsApi;
@@ -961,6 +962,25 @@ public DeployKeysApi getDeployKeysApi() {
961962
return (deployKeysApi);
962963
}
963964

965+
/**
966+
* Gets the DeployKeysApi instance owned by this GitLabApi instance. The DeploymentsApi is used
967+
* to perform all deployment related API calls.
968+
*
969+
* @return the DeploymentsApi instance owned by this GitLabApi instance
970+
*/
971+
public DeploymentsApi getDeploymentsApi() {
972+
973+
if (deploymentsApi == null) {
974+
synchronized (this) {
975+
if (deploymentsApi == null) {
976+
deploymentsApi = new DeploymentsApi(this);
977+
}
978+
}
979+
}
980+
981+
return (deploymentsApi);
982+
}
983+
964984
/**
965985
* Gets the DeployTokensApi instance owned by this GitLabApi instance. The DeployTokensApi is used
966986
* to perform all deploy token related API calls.
Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
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.Assert.fail;
8+
import static org.junit.Assume.assumeTrue;
9+
10+
import java.util.List;
11+
import java.util.Optional;
12+
import java.util.stream.Stream;
13+
14+
import org.gitlab4j.api.Constants.DeploymentStatus;
15+
import org.gitlab4j.api.models.Commit;
16+
import org.gitlab4j.api.models.Deployment;
17+
import org.gitlab4j.api.models.DeploymentFilter;
18+
import org.gitlab4j.api.models.Project;
19+
import org.junit.Before;
20+
import org.junit.BeforeClass;
21+
import org.junit.Test;
22+
import org.junit.experimental.categories.Category;
23+
24+
/**
25+
* In order for these tests to run you must set the following properties in
26+
* test-gitlab4j.properties
27+
*
28+
* TEST_HOST_URL TEST_PRIVATE_TOKEN TEST_USERNAME
29+
*
30+
* If any of the above are NULL, all tests in this class will be skipped.
31+
*
32+
*/
33+
@Category(IntegrationTest.class)
34+
public class TestDeploymentsApi extends AbstractIntegrationTest {
35+
36+
// The following needs to be set to your test repository
37+
private static final String TEST_USERNAME = HelperUtils.getProperty(USERNAME_KEY);
38+
39+
private static GitLabApi gitLabApi;
40+
private static Project testProject;
41+
42+
public TestDeploymentsApi() {
43+
super();
44+
}
45+
46+
@BeforeClass
47+
public static void setup() {
48+
49+
// Must setup the connection to the GitLab test server
50+
gitLabApi = baseTestSetup();
51+
testProject = getTestProject();
52+
53+
if (TEST_USERNAME == null || TEST_USERNAME.trim().isEmpty()) {
54+
System.err.println("TEST_USER_NAME cannot be empty");
55+
}
56+
}
57+
58+
@Before
59+
public void beforeMethod() {
60+
assumeTrue(gitLabApi != null);
61+
assumeTrue(testProject != null);
62+
}
63+
64+
@Test
65+
public void testCreateDeployment() throws GitLabApiException {
66+
67+
assertNotNull(testProject);
68+
69+
String environment = "environment-" + HelperUtils.getRandomInt(1000);
70+
List<Commit> commits = gitLabApi.getCommitsApi().getCommits(testProject);
71+
72+
assertTrue("Commits list should not be empty.", commits.size() > 0);
73+
74+
Deployment deployment = gitLabApi.getDeploymentsApi().addDeployment(testProject,
75+
environment,
76+
commits.get(0).getId(),
77+
testProject.getDefaultBranch(),
78+
false,
79+
DeploymentStatus.RUNNING);
80+
81+
assertNotNull(deployment);
82+
assertEquals(environment, deployment.getEnvironment().getName());
83+
assertEquals(commits.get(0).getId(), deployment.getSha());
84+
assertEquals(testProject.getDefaultBranch(), deployment.getRef());
85+
assertEquals(DeploymentStatus.RUNNING, deployment.getStatus());
86+
}
87+
88+
@Test
89+
public void testUpdateDeployment() throws GitLabApiException {
90+
91+
assertNotNull(testProject);
92+
93+
String environment = "environment-" + HelperUtils.getRandomInt(1000);
94+
List<Commit> commits = gitLabApi.getCommitsApi().getCommits(testProject);
95+
96+
assertTrue("Commits list should not be empty.", commits.size() > 0);
97+
98+
Deployment deployment = gitLabApi.getDeploymentsApi().addDeployment(testProject,
99+
environment,
100+
commits.get(0).getId(),
101+
testProject.getDefaultBranch(),
102+
false,
103+
DeploymentStatus.RUNNING);
104+
105+
assertNotNull(deployment);
106+
assertEquals(environment, deployment.getEnvironment().getName());
107+
assertEquals(commits.get(0).getId(), deployment.getSha());
108+
assertEquals(testProject.getDefaultBranch(), deployment.getRef());
109+
assertEquals(DeploymentStatus.RUNNING, deployment.getStatus());
110+
111+
Deployment updatedDeployment = gitLabApi.getDeploymentsApi().updateDeployment(testProject, deployment.getId(),
112+
DeploymentStatus.SUCCESS);
113+
114+
assertNotNull(updatedDeployment);
115+
assertEquals(environment, updatedDeployment.getEnvironment().getName());
116+
assertEquals(commits.get(0).getId(), updatedDeployment.getSha());
117+
assertEquals(testProject.getDefaultBranch(), updatedDeployment.getRef());
118+
assertEquals(DeploymentStatus.SUCCESS, updatedDeployment.getStatus());
119+
}
120+
121+
@Test
122+
public void testGetDeployment() throws GitLabApiException {
123+
124+
assertNotNull(testProject);
125+
126+
String environment = "environment-" + HelperUtils.getRandomInt(1000);
127+
128+
List<Commit> commits = gitLabApi.getCommitsApi().getCommits(testProject);
129+
130+
assertTrue("Commits list should not be empty.", commits.size() > 0);
131+
132+
Deployment deployment = gitLabApi.getDeploymentsApi().addDeployment(testProject,
133+
environment,
134+
commits.get(0).getId(),
135+
testProject.getDefaultBranch(),
136+
false,
137+
DeploymentStatus.SUCCESS);
138+
139+
assertNotNull(deployment);
140+
assertEquals(environment, deployment.getEnvironment().getName());
141+
assertEquals(commits.get(0).getId(), deployment.getSha());
142+
assertEquals(testProject.getDefaultBranch(), deployment.getRef());
143+
assertEquals(DeploymentStatus.SUCCESS, deployment.getStatus());
144+
145+
Deployment getDeployment = gitLabApi.getDeploymentsApi().getDeployment(testProject, deployment.getId());
146+
147+
assertNotNull(getDeployment);
148+
assertEquals(environment, getDeployment.getEnvironment().getName());
149+
assertEquals(commits.get(0).getId(), getDeployment.getSha());
150+
assertEquals(testProject.getDefaultBranch(), getDeployment.getRef());
151+
assertEquals(DeploymentStatus.SUCCESS, getDeployment.getStatus());
152+
assertEquals(deployment.getCreatedAt(), getDeployment.getCreatedAt());
153+
154+
Optional<Deployment> optionalDeployment = gitLabApi.getDeploymentsApi().getOptionalDeployment(testProject,
155+
getDeployment.getId());
156+
157+
optionalDeployment.ifPresent(d -> {
158+
assertEquals(environment, d.getEnvironment().getName());
159+
assertEquals(commits.get(0).getId(), d.getSha());
160+
assertEquals(testProject.getDefaultBranch(), d.getRef());
161+
assertEquals(DeploymentStatus.SUCCESS, d.getStatus());
162+
assertEquals(deployment.getCreatedAt(), d.getCreatedAt());
163+
});
164+
165+
if (!optionalDeployment.isPresent()) {
166+
fail("A deployment should be present.");
167+
}
168+
}
169+
170+
@Test
171+
public void testGetDeployments() throws GitLabApiException {
172+
173+
assertNotNull(testProject);
174+
175+
String environment = "environment-" + HelperUtils.getRandomInt(1000);
176+
177+
List<Commit> commits = gitLabApi.getCommitsApi().getCommits(testProject);
178+
179+
assertTrue("Commits list should not be empty.", commits.size() > 0);
180+
181+
for (int i = 0; i < 20; i++) {
182+
gitLabApi.getDeploymentsApi().addDeployment(testProject,
183+
environment,
184+
commits.get(0).getId(),
185+
testProject.getDefaultBranch(),
186+
false,
187+
DeploymentStatus.SUCCESS);
188+
}
189+
190+
Pager<Deployment> pager = gitLabApi.getDeploymentsApi().getProjectDeployments(testProject, 2);
191+
while (pager.hasNext()) {
192+
pager.next();
193+
assertTrue(pager.current().size() == 1 || pager.current().size() == 2);
194+
}
195+
196+
List<Deployment> deployments = gitLabApi.getDeploymentsApi().getProjectDeployments(testProject);
197+
int unfilteredeploymentNb = deployments.size();
198+
assertTrue(unfilteredeploymentNb >= 10);
199+
200+
DeploymentFilter deploymentFilter = new DeploymentFilter();
201+
deploymentFilter.setEnvironment(environment);
202+
203+
Pager<Deployment> filteredPager = gitLabApi.getDeploymentsApi().getProjectDeployments(testProject, deploymentFilter);
204+
while (filteredPager.hasNext()) {
205+
filteredPager.next();
206+
assertTrue(filteredPager.current().size() > 1 && filteredPager.current().size() < unfilteredeploymentNb);
207+
}
208+
209+
deploymentFilter.setEnvironment("none");
210+
211+
filteredPager = gitLabApi.getDeploymentsApi().getProjectDeployments(testProject, deploymentFilter);
212+
if (filteredPager.hasNext()) {
213+
filteredPager.next();
214+
assertTrue("Should be no deployments for environment `none`", filteredPager.current().size() == 0);
215+
}
216+
217+
Stream<Deployment> projectDeploymentsStream = gitLabApi.getDeploymentsApi().getProjectDeploymentsStream(testProject);
218+
assertTrue(projectDeploymentsStream.count() >= 10);
219+
220+
projectDeploymentsStream = gitLabApi.getDeploymentsApi().getProjectDeploymentsStream(testProject, deploymentFilter);
221+
assertEquals(0L, projectDeploymentsStream.count());
222+
223+
}
224+
225+
}

0 commit comments

Comments
 (0)