Unverified Commit cc37d216 authored by Gautier de Saint Martin Lacaze's avatar Gautier de Saint Martin Lacaze
Browse files

Add DeploymentsApi tests

parent bfadd897
...@@ -805,7 +805,9 @@ public interface Constants { ...@@ -805,7 +805,9 @@ public interface Constants {
/** Enum to use for specifying the status of a deployment. */ /** Enum to use for specifying the status of a deployment. */
public enum DeploymentStatus { public enum DeploymentStatus {
/**
* After some tests, {@link #CREATED} value is not a valid value.
*/
CREATED, RUNNING, SUCCESS, FAILED, CANCELED; CREATED, RUNNING, SUCCESS, FAILED, CANCELED;
private static JacksonJsonEnumHelper<DeploymentStatus> enumHelper = new JacksonJsonEnumHelper<>(DeploymentStatus.class); private static JacksonJsonEnumHelper<DeploymentStatus> enumHelper = new JacksonJsonEnumHelper<>(DeploymentStatus.class);
......
...@@ -148,8 +148,8 @@ public class DeploymentsApi extends AbstractApi { ...@@ -148,8 +148,8 @@ public class DeploymentsApi extends AbstractApi {
* @param environment The name of the environment to create the deployment for, required * @param environment The name of the environment to create the deployment for, required
* @param sha The SHA of the commit that is deployed, required * @param sha The SHA of the commit that is deployed, required
* @param ref The name of the branch or tag that is deployed, required * @param ref The name of the branch or tag that is deployed, required
* @param tag A boolean that indicates if the deployed ref is a tag (true) or not (false). , required * @param tag A boolean that indicates if the deployed ref is a tag (true) or not (false), required
* @param status The status to filter deployments by, optional * @param status The status to filter deployments by, required
* @return a Deployment instance with info on the added deployment * @return a Deployment instance with info on the added deployment
* @throws GitLabApiException if any exception occurs * @throws GitLabApiException if any exception occurs
*/ */
...@@ -160,7 +160,8 @@ public class DeploymentsApi extends AbstractApi { ...@@ -160,7 +160,8 @@ public class DeploymentsApi extends AbstractApi {
.withParam("sha", sha, true) .withParam("sha", sha, true)
.withParam("ref", ref, true) .withParam("ref", ref, true)
.withParam("tag", tag, true) .withParam("tag", tag, true)
.withParam("status", status); .withParam("status", status, true);
Response response = post(Response.Status.CREATED, formData, Response response = post(Response.Status.CREATED, formData,
"projects", getProjectIdOrPath(projectIdOrPath), "deployments"); "projects", getProjectIdOrPath(projectIdOrPath), "deployments");
return (response.readEntity(Deployment.class)); return (response.readEntity(Deployment.class));
...@@ -173,7 +174,7 @@ public class DeploymentsApi extends AbstractApi { ...@@ -173,7 +174,7 @@ public class DeploymentsApi extends AbstractApi {
* *
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance * @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
* @param deploymentId The ID of the deployment to update, required * @param deploymentId The ID of the deployment to update, required
* @param status The new status of the deployment, optional * @param status The new status of the deployment, required
* @return an updated Deployment instance * @return an updated Deployment instance
* @throws GitLabApiException if any exception occurs * @throws GitLabApiException if any exception occurs
*/ */
...@@ -183,9 +184,9 @@ public class DeploymentsApi extends AbstractApi { ...@@ -183,9 +184,9 @@ public class DeploymentsApi extends AbstractApi {
throw new RuntimeException("deploymentId cannot be null"); throw new RuntimeException("deploymentId cannot be null");
} }
final Deployment key = new Deployment(); final Deployment deployment = new Deployment();
key.setStatus(status); deployment.setStatus(status);
final Response response = put(Response.Status.OK, key, final Response response = put(Response.Status.OK, deployment,
"projects", getProjectIdOrPath(projectIdOrPath), "deployments", deploymentId); "projects", getProjectIdOrPath(projectIdOrPath), "deployments", deploymentId);
return (response.readEntity(Deployment.class)); return (response.readEntity(Deployment.class));
......
...@@ -58,6 +58,7 @@ public class GitLabApi implements AutoCloseable { ...@@ -58,6 +58,7 @@ public class GitLabApi implements AutoCloseable {
private ContainerRegistryApi containerRegistryApi; private ContainerRegistryApi containerRegistryApi;
private DiscussionsApi discussionsApi; private DiscussionsApi discussionsApi;
private DeployKeysApi deployKeysApi; private DeployKeysApi deployKeysApi;
private DeploymentsApi deploymentsApi;
private DeployTokensApi deployTokensApi; private DeployTokensApi deployTokensApi;
private EnvironmentsApi environmentsApi; private EnvironmentsApi environmentsApi;
private EpicsApi epicsApi; private EpicsApi epicsApi;
...@@ -961,6 +962,25 @@ public class GitLabApi implements AutoCloseable { ...@@ -961,6 +962,25 @@ public class GitLabApi implements AutoCloseable {
return (deployKeysApi); return (deployKeysApi);
} }
/**
* Gets the DeployKeysApi instance owned by this GitLabApi instance. The DeploymentsApi is used
* to perform all deployment related API calls.
*
* @return the DeploymentsApi instance owned by this GitLabApi instance
*/
public DeploymentsApi getDeploymentsApi() {
if (deploymentsApi == null) {
synchronized (this) {
if (deploymentsApi == null) {
deploymentsApi = new DeploymentsApi(this);
}
}
}
return (deploymentsApi);
}
/** /**
* Gets the DeployTokensApi instance owned by this GitLabApi instance. The DeployTokensApi is used * Gets the DeployTokensApi instance owned by this GitLabApi instance. The DeployTokensApi is used
* to perform all deploy token related API calls. * to perform all deploy token related API calls.
......
package org.gitlab4j.api;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeTrue;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;
import org.gitlab4j.api.Constants.DeploymentStatus;
import org.gitlab4j.api.models.Commit;
import org.gitlab4j.api.models.Deployment;
import org.gitlab4j.api.models.DeploymentFilter;
import org.gitlab4j.api.models.Project;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;
/**
* In order for these tests to run you must set the following properties in
* test-gitlab4j.properties
*
* TEST_HOST_URL TEST_PRIVATE_TOKEN TEST_USERNAME
*
* If any of the above are NULL, all tests in this class will be skipped.
*
*/
@Category(IntegrationTest.class)
public class TestDeploymentsApi extends AbstractIntegrationTest {
// The following needs to be set to your test repository
private static final String TEST_USERNAME = HelperUtils.getProperty(USERNAME_KEY);
private static GitLabApi gitLabApi;
private static Project testProject;
public TestDeploymentsApi() {
super();
}
@BeforeClass
public static void setup() {
// Must setup the connection to the GitLab test server
gitLabApi = baseTestSetup();
testProject = getTestProject();
if (TEST_USERNAME == null || TEST_USERNAME.trim().isEmpty()) {
System.err.println("TEST_USER_NAME cannot be empty");
}
}
@Before
public void beforeMethod() {
assumeTrue(gitLabApi != null);
assumeTrue(testProject != null);
}
@Test
public void testCreateDeployment() throws GitLabApiException {
assertNotNull(testProject);
String environment = "environment-" + HelperUtils.getRandomInt(1000);
List<Commit> commits = gitLabApi.getCommitsApi().getCommits(testProject);
assertTrue("Commits list should not be empty.", commits.size() > 0);
Deployment deployment = gitLabApi.getDeploymentsApi().addDeployment(testProject,
environment,
commits.get(0).getId(),
testProject.getDefaultBranch(),
false,
DeploymentStatus.RUNNING);
assertNotNull(deployment);
assertEquals(environment, deployment.getEnvironment().getName());
assertEquals(commits.get(0).getId(), deployment.getSha());
assertEquals(testProject.getDefaultBranch(), deployment.getRef());
assertEquals(DeploymentStatus.RUNNING, deployment.getStatus());
}
@Test
public void testUpdateDeployment() throws GitLabApiException {
assertNotNull(testProject);
String environment = "environment-" + HelperUtils.getRandomInt(1000);
List<Commit> commits = gitLabApi.getCommitsApi().getCommits(testProject);
assertTrue("Commits list should not be empty.", commits.size() > 0);
Deployment deployment = gitLabApi.getDeploymentsApi().addDeployment(testProject,
environment,
commits.get(0).getId(),
testProject.getDefaultBranch(),
false,
DeploymentStatus.RUNNING);
assertNotNull(deployment);
assertEquals(environment, deployment.getEnvironment().getName());
assertEquals(commits.get(0).getId(), deployment.getSha());
assertEquals(testProject.getDefaultBranch(), deployment.getRef());
assertEquals(DeploymentStatus.RUNNING, deployment.getStatus());
Deployment updatedDeployment = gitLabApi.getDeploymentsApi().updateDeployment(testProject, deployment.getId(),
DeploymentStatus.SUCCESS);
assertNotNull(updatedDeployment);
assertEquals(environment, updatedDeployment.getEnvironment().getName());
assertEquals(commits.get(0).getId(), updatedDeployment.getSha());
assertEquals(testProject.getDefaultBranch(), updatedDeployment.getRef());
assertEquals(DeploymentStatus.SUCCESS, updatedDeployment.getStatus());
}
@Test
public void testGetDeployment() throws GitLabApiException {
assertNotNull(testProject);
String environment = "environment-" + HelperUtils.getRandomInt(1000);
List<Commit> commits = gitLabApi.getCommitsApi().getCommits(testProject);
assertTrue("Commits list should not be empty.", commits.size() > 0);
Deployment deployment = gitLabApi.getDeploymentsApi().addDeployment(testProject,
environment,
commits.get(0).getId(),
testProject.getDefaultBranch(),
false,
DeploymentStatus.SUCCESS);
assertNotNull(deployment);
assertEquals(environment, deployment.getEnvironment().getName());
assertEquals(commits.get(0).getId(), deployment.getSha());
assertEquals(testProject.getDefaultBranch(), deployment.getRef());
assertEquals(DeploymentStatus.SUCCESS, deployment.getStatus());
Deployment getDeployment = gitLabApi.getDeploymentsApi().getDeployment(testProject, deployment.getId());
assertNotNull(getDeployment);
assertEquals(environment, getDeployment.getEnvironment().getName());
assertEquals(commits.get(0).getId(), getDeployment.getSha());
assertEquals(testProject.getDefaultBranch(), getDeployment.getRef());
assertEquals(DeploymentStatus.SUCCESS, getDeployment.getStatus());
assertEquals(deployment.getCreatedAt(), getDeployment.getCreatedAt());
Optional<Deployment> optionalDeployment = gitLabApi.getDeploymentsApi().getOptionalDeployment(testProject,
getDeployment.getId());
optionalDeployment.ifPresent(d -> {
assertEquals(environment, d.getEnvironment().getName());
assertEquals(commits.get(0).getId(), d.getSha());
assertEquals(testProject.getDefaultBranch(), d.getRef());
assertEquals(DeploymentStatus.SUCCESS, d.getStatus());
assertEquals(deployment.getCreatedAt(), d.getCreatedAt());
});
if (!optionalDeployment.isPresent()) {
fail("A deployment should be present.");
}
}
@Test
public void testGetDeployments() throws GitLabApiException {
assertNotNull(testProject);
String environment = "environment-" + HelperUtils.getRandomInt(1000);
List<Commit> commits = gitLabApi.getCommitsApi().getCommits(testProject);
assertTrue("Commits list should not be empty.", commits.size() > 0);
for (int i = 0; i < 20; i++) {
gitLabApi.getDeploymentsApi().addDeployment(testProject,
environment,
commits.get(0).getId(),
testProject.getDefaultBranch(),
false,
DeploymentStatus.SUCCESS);
}
Pager<Deployment> pager = gitLabApi.getDeploymentsApi().getProjectDeployments(testProject, 2);
while (pager.hasNext()) {
pager.next();
assertTrue(pager.current().size() == 1 || pager.current().size() == 2);
}
List<Deployment> deployments = gitLabApi.getDeploymentsApi().getProjectDeployments(testProject);
int unfilteredeploymentNb = deployments.size();
assertTrue(unfilteredeploymentNb >= 10);
DeploymentFilter deploymentFilter = new DeploymentFilter();
deploymentFilter.setEnvironment(environment);
Pager<Deployment> filteredPager = gitLabApi.getDeploymentsApi().getProjectDeployments(testProject, deploymentFilter);
while (filteredPager.hasNext()) {
filteredPager.next();
assertTrue(filteredPager.current().size() > 1 && filteredPager.current().size() < unfilteredeploymentNb);
}
deploymentFilter.setEnvironment("none");
filteredPager = gitLabApi.getDeploymentsApi().getProjectDeployments(testProject, deploymentFilter);
if (filteredPager.hasNext()) {
filteredPager.next();
assertTrue("Should be no deployments for environment `none`", filteredPager.current().size() == 0);
}
Stream<Deployment> projectDeploymentsStream = gitLabApi.getDeploymentsApi().getProjectDeploymentsStream(testProject);
assertTrue(projectDeploymentsStream.count() >= 10);
projectDeploymentsStream = gitLabApi.getDeploymentsApi().getProjectDeploymentsStream(testProject, deploymentFilter);
assertEquals(0L, projectDeploymentsStream.count());
}
}
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