|
| 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