Skip to content

Commit f8de66a

Browse files
committed
Mods to support integration testing using default gitlab-ce docker image.
1 parent 31984c6 commit f8de66a

File tree

6 files changed

+152
-42
lines changed

6 files changed

+152
-42
lines changed

pom.xml

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,24 @@
4242
<java.source.version>1.8</java.source.version>
4343
<java.target.version>1.8</java.target.version>
4444

45+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
46+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
47+
4548
<jersey.version>2.28</jersey.version>
4649
<jackson.version>2.9.8</jackson.version>
47-
4850
<javaServlet.version>4.0.1</javaServlet.version>
4951

5052
<junit.version>4.12</junit.version>
51-
<mockito.version>2.19.0</mockito.version>
53+
<mockito.version>2.27.0</mockito.version>
5254
<hamcrest.version>1.3</hamcrest.version>
53-
<systemRules.version>1.18.0</systemRules.version>
54-
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
55-
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
55+
<systemRules.version>1.19.0</systemRules.version>
5656

5757
<changelog-lib.version>1.59</changelog-lib.version>
58+
59+
<gitlab.version>11.10.4-ce.0</gitlab.version>
60+
<gitlab.autoremove-container>true</gitlab.autoremove-container>
61+
<gitlab.skip-docker-start>true</gitlab.skip-docker-start>
62+
<gitlab.port>8090</gitlab.port>
5863
</properties>
5964

6065
<scm>
@@ -276,6 +281,51 @@
276281
</signature>
277282
</configuration>
278283
</plugin>
284+
285+
<plugin>
286+
<groupId>io.fabric8</groupId>
287+
<artifactId>docker-maven-plugin</artifactId>
288+
<version>0.30.0</version>
289+
<configuration>
290+
<images>
291+
<image>
292+
<name>gitlab/gitlab-ce:${gitlab.version}</name>
293+
<alias>gitlab</alias>
294+
<run>
295+
<skip>${gitlab.skip-docker-start}</skip>
296+
<env>
297+
<GITLAB_OMNIBUS_CONFIG>gitlab_rails['initial_root_password']="password"; gitlab_rails['lfs_enabled']=false;</GITLAB_OMNIBUS_CONFIG>
298+
</env>
299+
<ports>
300+
<port>${gitlab.port}:80</port>
301+
</ports>
302+
<wait>
303+
<healthy>true</healthy>
304+
<time>300000</time>
305+
</wait>
306+
</run>
307+
</image>
308+
</images>
309+
<removeVolumes>true</removeVolumes>
310+
</configuration>
311+
<executions>
312+
<execution>
313+
<id>start</id>
314+
<phase>pre-integration-test</phase>
315+
<goals>
316+
<goal>start</goal>
317+
</goals>
318+
</execution>
319+
<execution>
320+
<id>stop</id>
321+
<phase>post-integration-test</phase>
322+
<goals>
323+
<goal>stop</goal>
324+
</goals>
325+
</execution>
326+
</executions>
327+
</plugin>
328+
279329
</plugins>
280330

281331
<pluginManagement>
@@ -348,6 +398,7 @@
348398
<version>${junit.version}</version>
349399
<scope>test</scope>
350400
</dependency>
401+
351402
<dependency>
352403
<groupId>org.mockito</groupId>
353404
<artifactId>mockito-core</artifactId>

src/test/java/org/gitlab4j/api/HelperUtils.java

Lines changed: 49 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,35 +8,46 @@
88

99
public class HelperUtils {
1010

11-
private static Properties testProperties;
12-
static {
11+
private static final Properties testProperties = new Properties();
1312

14-
File propertiesFile = new File((String) System.getProperties().get("user.home"), "test-gitlab4j.properties");
15-
if (!propertiesFile.exists()) {
13+
static {
1614

17-
// Get the maven basedir, we use it to locate the properties for the unit tests
18-
String basedir = (String) System.getProperties().get("basedir");
15+
boolean propertiesLoaded = false;
1916

20-
// If we are performing a release in target/checkout, trim off the target/checkout directory from basedir
21-
if (basedir != null && (basedir.endsWith("target/checkout") || basedir.endsWith("target\\checkout"))) {
22-
basedir = basedir.substring(0, basedir.length() - 15);
23-
}
17+
// Get the maven basedir, we use it to locate the default properties for the unit tests
18+
String basedir = (String) System.getProperties().get("basedir");
2419

25-
propertiesFile = new File(basedir, "src/test/gitlab/test-gitlab4j.properties");
20+
// If we are performing a release in target/checkout, trim off the target/checkout directory from basedir
21+
if (basedir != null && (basedir.endsWith("target/checkout") || basedir.endsWith("target\\checkout"))) {
22+
basedir = basedir.substring(0, basedir.length() - 15);
2623
}
2724

25+
// Load the base test properties from "src/test/resources/test-gitlab4j.properties"
26+
File propertiesFile = new File(basedir, "src/test/resources/test-gitlab4j.properties");
2827
if (propertiesFile.exists()) {
28+
try (InputStream input = new FileInputStream(propertiesFile)) {
29+
testProperties.load(input);
30+
System.out.println("Loaded base test properties from: " + propertiesFile.getAbsolutePath());
31+
propertiesLoaded = true;
32+
} catch (IOException ioe) {
33+
System.err.println("Error loading base test properties, error=" + ioe.getMessage());
34+
}
35+
}
2936

30-
System.out.println("test-gitlab4j.properties location: " + propertiesFile.getAbsolutePath());
31-
32-
testProperties = new Properties();
37+
// Now load the overriding test properties if found in the user's home directory
38+
propertiesFile = new File((String) System.getProperties().get("user.home"), "test-gitlab4j.properties");
39+
if (propertiesFile.exists()) {
3340
try (InputStream input = new FileInputStream(propertiesFile)) {
3441
testProperties.load(input);
42+
System.out.println("Loaded overriding test properties from: " + propertiesFile.getAbsolutePath());
43+
propertiesLoaded = true;
3544
} catch (IOException ioe) {
45+
System.err.println("Error loading overriding test properties, error=" + ioe.getMessage());
3646
}
47+
}
3748

38-
} else {
39-
System.out.println("No test-gitlab4j.properties file found");
49+
if (!propertiesLoaded) {
50+
System.out.println("No test properties have been loaded!");
4051
}
4152
}
4253

@@ -50,6 +61,28 @@ public static final String getProperty(String key) {
5061
return (testProperties.getProperty(key));
5162
}
5263

64+
/**
65+
* Get a named property from the test-gitlab4j.properties file,
66+
* will return the defaultValue if null or empty.
67+
*
68+
* @param key the key of the property to get
69+
* @param defaultValue the value to return if property is null or empty
70+
* @return the named property from the test-gitlab4j.properties file
71+
*/
72+
public static final String getProperty(String key, String defaultValue) {
73+
74+
String value = getProperty(key);
75+
if (value != null && value.trim().length() > 0) {
76+
return (value);
77+
}
78+
79+
if (defaultValue != null) {
80+
testProperties.setProperty(key, defaultValue);
81+
}
82+
83+
return (defaultValue);
84+
}
85+
5386
/**
5487
* Set a named property, this will amend and overwrite properties read from the test-gitlab4j.properties file.
5588
*

src/test/java/org/gitlab4j/api/PropertyConstants.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22

33
public interface PropertyConstants {
44

5+
public static final String TEST_PROJECT_SUBDIRECTORY_PATH = "src/main/docs/test-project.txt";
56
public static final String TEST_PROPERTIES_FILENAME = "test-gitlab4j.properties";
67

8+
// The following are keys used to look up values in the test propertiues file
9+
public static final String ADMIN_PASSWORD_KEY = "TEST_ADMIN_PASSWORD";
10+
public static final String ADMIN_USERNAME_KEY = "TEST_ADMIN_USERNAME";
711
public static final String ACCESS_TOKEN_KEY = "TEST_ACCESS_TOKEN";
8-
public static final String BLOCK_USERNAME_KEY = "TEST_BLOCK_USERNAM";
12+
public static final String BLOCK_USERNAME_KEY = "TEST_BLOCK_USERNAME";
913
public static final String GROUP_KEY = "TEST_GROUP";
1014
public static final String GROUP_MEMBER_USERNAME_KEY = "TEST_GROUP_MEMBER_USERNAME";
1115
public static final String GROUP_PROJECT_KEY = "TEST_GROUP_PROJECT";

src/test/java/org/gitlab4j/api/TestCommitsApi.java

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
import org.gitlab4j.api.models.CommitRef;
1717
import org.gitlab4j.api.models.Diff;
1818
import org.gitlab4j.api.models.Project;
19+
import org.gitlab4j.api.models.RepositoryFile;
1920
import org.gitlab4j.api.utils.ISO8601;
21+
import org.junit.AfterClass;
2022
import org.junit.Before;
2123
import org.junit.BeforeClass;
2224
import org.junit.FixMethodOrder;
@@ -38,10 +40,9 @@
3840
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
3941
public class TestCommitsApi extends AbstractIntegrationTest {
4042

41-
private static final String TEST_PROJECT_SUBDIRECTORY_PATH = "src/main/docs/test-project.txt";
42-
4343
private static GitLabApi gitLabApi;
4444
private static Project testProject;
45+
private static boolean createdSubDirectoryPathFile = false;
4546

4647
public TestCommitsApi() {
4748
super();
@@ -52,6 +53,25 @@ public static void setup() {
5253
// Must setup the connection to the GitLab test server and get the test Project instance
5354
gitLabApi = baseTestSetup();
5455
testProject = getTestProject();
56+
57+
if (!gitLabApi.getRepositoryFileApi().getOptionalFile(testProject, TEST_PROJECT_SUBDIRECTORY_PATH, "master").isPresent()) {
58+
try {
59+
RepositoryFile repoFile = new RepositoryFile();
60+
repoFile.setFilePath(TEST_PROJECT_SUBDIRECTORY_PATH);
61+
repoFile.setContent("This is a test project used to test GitLab4J-API.");
62+
gitLabApi.getRepositoryFileApi().createFile(testProject, repoFile, "master", "Initial commit.");
63+
createdSubDirectoryPathFile = true;
64+
} catch (GitLabApiException ignore) {}
65+
}
66+
}
67+
68+
@AfterClass
69+
public static void teardown() {
70+
if (createdSubDirectoryPathFile) {
71+
try {
72+
gitLabApi.getRepositoryFileApi().deleteFile(testProject, TEST_PROJECT_SUBDIRECTORY_PATH, "master", "No longer needed.");
73+
} catch (Exception ignore) {}
74+
}
5575
}
5676

5777
@Before
@@ -155,16 +175,12 @@ public void testCommitsByPath() throws GitLabApiException {
155175
CommitsApi commitsApi = gitLabApi.getCommitsApi();
156176
List<Commit> commits = commitsApi.getCommits(testProject, "master", null);
157177
assertNotNull(commits);
158-
assertTrue(commits.size() > 0);
159-
160-
commits = commitsApi.getCommits(testProject, "master", "README");
161-
assertNotNull(commits);
162-
assertTrue(commits.size() > 0);
178+
assertTrue(commits.size() > 1);
163179

164180
commitsApi = gitLabApi.getCommitsApi();
165181
commits = commitsApi.getCommits(testProject, "master", "README.md");
166182
assertNotNull(commits);
167-
assertTrue(commits.size() > 0);
183+
assertTrue(commits.size() > 1);
168184

169185
commits = commitsApi.getCommits(testProject, "master", TEST_PROJECT_SUBDIRECTORY_PATH);
170186
assertNotNull(commits);

src/test/java/org/gitlab4j/api/TestHealthCheckApi.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import static org.junit.Assume.assumeTrue;
55

66
import org.gitlab4j.api.models.HealthCheckInfo;
7+
import org.gitlab4j.api.utils.AccessTokenUtils;
78
import org.junit.Before;
89
import org.junit.BeforeClass;
910
import org.junit.Test;
@@ -23,22 +24,30 @@ public class TestHealthCheckApi implements PropertyConstants {
2324

2425
// The following needs to be set to your test repository
2526
private static final String TEST_HOST_URL = HelperUtils.getProperty(HOST_URL_KEY);
26-
private static final String TEST_HEALTH_CHECK_TOKEN = HelperUtils.getProperty(HEALTH_CHECK_TOKEN_KEY);
27-
27+
private static final String TEST_LOGIN_USERNAME = HelperUtils.getProperty(LOGIN_USERNAME_KEY);
28+
private static final String TEST_LOGIN_PASSWORD = HelperUtils.getProperty(LOGIN_PASSWORD_KEY);
29+
private static String TEST_HEALTH_CHECK_TOKEN = HelperUtils.getProperty(HEALTH_CHECK_TOKEN_KEY);
2830
private static GitLabApi gitLabApi;
2931

3032
public TestHealthCheckApi() {
3133
super();
3234
}
3335

3436
@BeforeClass
35-
public static void setup() {
37+
public static void setup() throws GitLabApiException {
3638

3739
String problems = "";
3840
if (TEST_HOST_URL == null || TEST_HOST_URL.trim().isEmpty()) {
3941
problems += "TEST_HOST_URL cannot be empty\n";
4042
}
4143

44+
// Fetch the Health Check Token if not set already
45+
if (TEST_HEALTH_CHECK_TOKEN == null || TEST_HEALTH_CHECK_TOKEN.trim().isEmpty()) {
46+
TEST_HEALTH_CHECK_TOKEN = AccessTokenUtils.getHealthCheckAccessToken(
47+
TEST_HOST_URL, TEST_LOGIN_USERNAME, TEST_LOGIN_PASSWORD);
48+
HelperUtils.setProperty(HEALTH_CHECK_TOKEN_KEY, TEST_HEALTH_CHECK_TOKEN);
49+
}
50+
4251
if (TEST_HEALTH_CHECK_TOKEN == null || TEST_HEALTH_CHECK_TOKEN.trim().isEmpty()) {
4352
problems += "TEST_HEALTH_CHECK_TOKEN cannot be empty\n";
4453
}

src/test/java/org/gitlab4j/api/TestRepositoryFileApi.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,8 @@ public void testRawFileViaFile() throws GitLabApiException, IOException {
9494
Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME);
9595
assertNotNull(project);
9696

97-
File file = gitLabApi.getRepositoryFileApi().getRawFile(project.getId(), "master", "README", null);
98-
assertTrue(file.length() > 0);
99-
file.delete();
100-
101-
file = gitLabApi.getRepositoryFileApi().getRawFile(project.getId(), "master", "README", new File("."));
97+
File tempDir = new File(System.getProperty("java.io.tmpdir"));
98+
File file = gitLabApi.getRepositoryFileApi().getRawFile(project.getId(), "master", "README.md", tempDir);
10299
assertTrue(file.length() > 0);
103100
file.delete();
104101
}
@@ -109,9 +106,9 @@ public void testRepositoryFileViaInputStream() throws GitLabApiException, IOExce
109106
Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME);
110107
assertNotNull(project);
111108

112-
InputStream in = gitLabApi.getRepositoryFileApi().getRawFile(project.getId(), "master", "README");
109+
InputStream in = gitLabApi.getRepositoryFileApi().getRawFile(project.getId(), "master", "README.md");
113110

114-
Path target = Files.createTempFile(TEST_PROJECT_NAME + "-README", "");
111+
Path target = Files.createTempFile(TEST_PROJECT_NAME + "-README", "md");
115112
Files.copy(in, target, StandardCopyOption.REPLACE_EXISTING);
116113

117114
assertTrue(target.toFile().length() > 0);
@@ -124,7 +121,7 @@ public void testRepositoryFileGetFile() throws GitLabApiException, IOException {
124121
Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME);
125122
assertNotNull(project);
126123

127-
RepositoryFile fileInfo = gitLabApi.getRepositoryFileApi().getFileInfo(project.getId(), "README", "master");
124+
RepositoryFile fileInfo = gitLabApi.getRepositoryFileApi().getFileInfo(project.getId(), "README.md", "master");
128125
assertNotNull(fileInfo);
129126
}
130127

@@ -134,7 +131,7 @@ public void testRepositoryFileGetOptionalFile() throws GitLabApiException, IOExc
134131
Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME);
135132
assertNotNull(project);
136133

137-
Optional<RepositoryFile> fileInfo = gitLabApi.getRepositoryFileApi().getOptionalFileInfo(project.getId(), "README", "master");
134+
Optional<RepositoryFile> fileInfo = gitLabApi.getRepositoryFileApi().getOptionalFileInfo(project.getId(), "README.md", "master");
138135
assertNotNull(fileInfo.get());
139136

140137
fileInfo = gitLabApi.getRepositoryFileApi().getOptionalFileInfo(project.getId(), "I-DONT-EXIST", "master");

0 commit comments

Comments
 (0)