forked from gitlab4j/gitlab4j-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestEnvironmentsApi.java
135 lines (108 loc) · 4.26 KB
/
TestEnvironmentsApi.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
package org.gitlab4j.api;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
import java.util.List;
import java.util.Optional;
import java.util.Random;
import java.util.stream.Stream;
import org.gitlab4j.api.models.Environment;
import org.gitlab4j.api.models.Project;
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
* <p>
* TEST_HOST_URL
* TEST_PRIVATE_TOKEN
* <p>
* If any of the above are NULL, all tests in this class will be skipped.
*/
@Tag("integration")
@ExtendWith(SetupIntegrationTestExtension.class)
public class TestEnvironmentsApi extends AbstractIntegrationTest {
private static GitLabApi gitLabApi;
private static Project testProject;
private static final String ENVIRONMENT_NAME = "gitlab4j-testing";
private static final String EXTERNAL_URL = "https:/testing.example.com/";
private static final String TIER = "testing";
private static Random randomNumberGenerator = new Random();
public TestEnvironmentsApi() {
super();
}
@BeforeAll
public static void setup() {
// Must setup the connection to the GitLab test server and get the test Project
// instance
gitLabApi = baseTestSetup();
testProject = getTestProject();
deleteAllTestAssets();
}
@AfterAll
public static void teardown() throws GitLabApiException {
deleteAllTestAssets();
}
private static void deleteAllTestAssets() {
if (gitLabApi != null) {
try {
List<Environment> envs = gitLabApi.getEnvironmentsApi().getEnvironments(testProject);
if (envs != null) {
for (Environment env : envs) {
if (env.getName().startsWith(ENVIRONMENT_NAME)) {
gitLabApi.getEnvironmentsApi().stopEnvironment(testProject, env.getId());
gitLabApi.getEnvironmentsApi().deleteEnvironment(testProject, env.getId());
}
}
}
} catch (GitLabApiException ignore) {
System.out.println("ERROR");
}
}
}
@BeforeEach
public void beforeMethod() {
assumeTrue(gitLabApi != null);
}
private static String getUniqueName() {
return (ENVIRONMENT_NAME + " - " + (randomNumberGenerator.nextInt() + 1));
}
@Test
public void testGetEnvironments() throws GitLabApiException {
final Environment env = gitLabApi.getEnvironmentsApi().createEnvironment(
testProject, getUniqueName(), EXTERNAL_URL, TIER);
List<Environment> envs = gitLabApi.getEnvironmentsApi().getEnvironments(testProject);
assertTrue(envs.size() > 0);
Environment foundEnv = envs.stream().filter(
e -> e.getName().equals(env.getName())).findFirst().orElse(null);
assertNotNull(foundEnv);
assertEquals(env.getName(), foundEnv.getName());
}
@Test
public void testStopAndDeleteEnvironment() throws GitLabApiException {
final Environment env = gitLabApi.getEnvironmentsApi().createEnvironment(
testProject, getUniqueName(), EXTERNAL_URL, TIER);
gitLabApi.getEnvironmentsApi().stopEnvironment(testProject, env.getId());
gitLabApi.getEnvironmentsApi().deleteEnvironment(testProject, env.getId());
Stream<Environment> envs = gitLabApi.getEnvironmentsApi().getEnvironmentsStream(testProject);
Environment foundEnv = envs.filter(e -> e.getName().equals(env.getName())).findFirst().orElse(null);
assertNull(foundEnv);
}
@Test
public void testOptionalEnvironment() throws GitLabApiException {
final Environment env = gitLabApi.getEnvironmentsApi().createEnvironment(
testProject, getUniqueName(), EXTERNAL_URL, TIER);
Optional<Environment> optionalEnv =
gitLabApi.getEnvironmentsApi().getOptionalEnvironment(testProject, env.getId());
assertTrue(optionalEnv.isPresent());
assertEquals(env.getName(), optionalEnv.get().getName());
gitLabApi.getEnvironmentsApi().stopEnvironment(testProject, env.getId());
gitLabApi.getEnvironmentsApi().deleteEnvironment(testProject, env.getId());
}
}