Commit f8de66af authored by Greg Messner's avatar Greg Messner
Browse files

Mods to support integration testing using default gitlab-ce docker image.

parent 31984c64
...@@ -42,19 +42,24 @@ ...@@ -42,19 +42,24 @@
<java.source.version>1.8</java.source.version> <java.source.version>1.8</java.source.version>
<java.target.version>1.8</java.target.version> <java.target.version>1.8</java.target.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<jersey.version>2.28</jersey.version> <jersey.version>2.28</jersey.version>
<jackson.version>2.9.8</jackson.version> <jackson.version>2.9.8</jackson.version>
<javaServlet.version>4.0.1</javaServlet.version> <javaServlet.version>4.0.1</javaServlet.version>
<junit.version>4.12</junit.version> <junit.version>4.12</junit.version>
<mockito.version>2.19.0</mockito.version> <mockito.version>2.27.0</mockito.version>
<hamcrest.version>1.3</hamcrest.version> <hamcrest.version>1.3</hamcrest.version>
<systemRules.version>1.18.0</systemRules.version> <systemRules.version>1.19.0</systemRules.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<changelog-lib.version>1.59</changelog-lib.version> <changelog-lib.version>1.59</changelog-lib.version>
<gitlab.version>11.10.4-ce.0</gitlab.version>
<gitlab.autoremove-container>true</gitlab.autoremove-container>
<gitlab.skip-docker-start>true</gitlab.skip-docker-start>
<gitlab.port>8090</gitlab.port>
</properties> </properties>
<scm> <scm>
...@@ -276,6 +281,51 @@ ...@@ -276,6 +281,51 @@
</signature> </signature>
</configuration> </configuration>
</plugin> </plugin>
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.30.0</version>
<configuration>
<images>
<image>
<name>gitlab/gitlab-ce:${gitlab.version}</name>
<alias>gitlab</alias>
<run>
<skip>${gitlab.skip-docker-start}</skip>
<env>
<GITLAB_OMNIBUS_CONFIG>gitlab_rails['initial_root_password']="password"; gitlab_rails['lfs_enabled']=false;</GITLAB_OMNIBUS_CONFIG>
</env>
<ports>
<port>${gitlab.port}:80</port>
</ports>
<wait>
<healthy>true</healthy>
<time>300000</time>
</wait>
</run>
</image>
</images>
<removeVolumes>true</removeVolumes>
</configuration>
<executions>
<execution>
<id>start</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>stop</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins> </plugins>
<pluginManagement> <pluginManagement>
...@@ -348,6 +398,7 @@ ...@@ -348,6 +398,7 @@
<version>${junit.version}</version> <version>${junit.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.mockito</groupId> <groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId> <artifactId>mockito-core</artifactId>
......
...@@ -8,35 +8,46 @@ import java.util.Properties; ...@@ -8,35 +8,46 @@ import java.util.Properties;
public class HelperUtils { public class HelperUtils {
private static Properties testProperties; private static final Properties testProperties = new Properties();
static {
File propertiesFile = new File((String) System.getProperties().get("user.home"), "test-gitlab4j.properties"); static {
if (!propertiesFile.exists()) {
// Get the maven basedir, we use it to locate the properties for the unit tests boolean propertiesLoaded = false;
String basedir = (String) System.getProperties().get("basedir");
// If we are performing a release in target/checkout, trim off the target/checkout directory from basedir // Get the maven basedir, we use it to locate the default properties for the unit tests
if (basedir != null && (basedir.endsWith("target/checkout") || basedir.endsWith("target\\checkout"))) { String basedir = (String) System.getProperties().get("basedir");
basedir = basedir.substring(0, basedir.length() - 15);
}
propertiesFile = new File(basedir, "src/test/gitlab/test-gitlab4j.properties"); // If we are performing a release in target/checkout, trim off the target/checkout directory from basedir
if (basedir != null && (basedir.endsWith("target/checkout") || basedir.endsWith("target\\checkout"))) {
basedir = basedir.substring(0, basedir.length() - 15);
} }
// Load the base test properties from "src/test/resources/test-gitlab4j.properties"
File propertiesFile = new File(basedir, "src/test/resources/test-gitlab4j.properties");
if (propertiesFile.exists()) { if (propertiesFile.exists()) {
try (InputStream input = new FileInputStream(propertiesFile)) {
testProperties.load(input);
System.out.println("Loaded base test properties from: " + propertiesFile.getAbsolutePath());
propertiesLoaded = true;
} catch (IOException ioe) {
System.err.println("Error loading base test properties, error=" + ioe.getMessage());
}
}
System.out.println("test-gitlab4j.properties location: " + propertiesFile.getAbsolutePath()); // Now load the overriding test properties if found in the user's home directory
propertiesFile = new File((String) System.getProperties().get("user.home"), "test-gitlab4j.properties");
testProperties = new Properties(); if (propertiesFile.exists()) {
try (InputStream input = new FileInputStream(propertiesFile)) { try (InputStream input = new FileInputStream(propertiesFile)) {
testProperties.load(input); testProperties.load(input);
System.out.println("Loaded overriding test properties from: " + propertiesFile.getAbsolutePath());
propertiesLoaded = true;
} catch (IOException ioe) { } catch (IOException ioe) {
System.err.println("Error loading overriding test properties, error=" + ioe.getMessage());
} }
}
} else { if (!propertiesLoaded) {
System.out.println("No test-gitlab4j.properties file found"); System.out.println("No test properties have been loaded!");
} }
} }
...@@ -50,6 +61,28 @@ public class HelperUtils { ...@@ -50,6 +61,28 @@ public class HelperUtils {
return (testProperties.getProperty(key)); return (testProperties.getProperty(key));
} }
/**
* Get a named property from the test-gitlab4j.properties file,
* will return the defaultValue if null or empty.
*
* @param key the key of the property to get
* @param defaultValue the value to return if property is null or empty
* @return the named property from the test-gitlab4j.properties file
*/
public static final String getProperty(String key, String defaultValue) {
String value = getProperty(key);
if (value != null && value.trim().length() > 0) {
return (value);
}
if (defaultValue != null) {
testProperties.setProperty(key, defaultValue);
}
return (defaultValue);
}
/** /**
* Set a named property, this will amend and overwrite properties read from the test-gitlab4j.properties file. * Set a named property, this will amend and overwrite properties read from the test-gitlab4j.properties file.
* *
......
...@@ -2,10 +2,14 @@ package org.gitlab4j.api; ...@@ -2,10 +2,14 @@ package org.gitlab4j.api;
public interface PropertyConstants { public interface PropertyConstants {
public static final String TEST_PROJECT_SUBDIRECTORY_PATH = "src/main/docs/test-project.txt";
public static final String TEST_PROPERTIES_FILENAME = "test-gitlab4j.properties"; public static final String TEST_PROPERTIES_FILENAME = "test-gitlab4j.properties";
// The following are keys used to look up values in the test propertiues file
public static final String ADMIN_PASSWORD_KEY = "TEST_ADMIN_PASSWORD";
public static final String ADMIN_USERNAME_KEY = "TEST_ADMIN_USERNAME";
public static final String ACCESS_TOKEN_KEY = "TEST_ACCESS_TOKEN"; public static final String ACCESS_TOKEN_KEY = "TEST_ACCESS_TOKEN";
public static final String BLOCK_USERNAME_KEY = "TEST_BLOCK_USERNAM"; public static final String BLOCK_USERNAME_KEY = "TEST_BLOCK_USERNAME";
public static final String GROUP_KEY = "TEST_GROUP"; public static final String GROUP_KEY = "TEST_GROUP";
public static final String GROUP_MEMBER_USERNAME_KEY = "TEST_GROUP_MEMBER_USERNAME"; public static final String GROUP_MEMBER_USERNAME_KEY = "TEST_GROUP_MEMBER_USERNAME";
public static final String GROUP_PROJECT_KEY = "TEST_GROUP_PROJECT"; public static final String GROUP_PROJECT_KEY = "TEST_GROUP_PROJECT";
......
...@@ -16,7 +16,9 @@ import org.gitlab4j.api.models.Commit; ...@@ -16,7 +16,9 @@ import org.gitlab4j.api.models.Commit;
import org.gitlab4j.api.models.CommitRef; import org.gitlab4j.api.models.CommitRef;
import org.gitlab4j.api.models.Diff; import org.gitlab4j.api.models.Diff;
import org.gitlab4j.api.models.Project; import org.gitlab4j.api.models.Project;
import org.gitlab4j.api.models.RepositoryFile;
import org.gitlab4j.api.utils.ISO8601; import org.gitlab4j.api.utils.ISO8601;
import org.junit.AfterClass;
import org.junit.Before; import org.junit.Before;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.FixMethodOrder; import org.junit.FixMethodOrder;
...@@ -38,10 +40,9 @@ import org.junit.runners.MethodSorters; ...@@ -38,10 +40,9 @@ import org.junit.runners.MethodSorters;
@FixMethodOrder(MethodSorters.NAME_ASCENDING) @FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class TestCommitsApi extends AbstractIntegrationTest { public class TestCommitsApi extends AbstractIntegrationTest {
private static final String TEST_PROJECT_SUBDIRECTORY_PATH = "src/main/docs/test-project.txt";
private static GitLabApi gitLabApi; private static GitLabApi gitLabApi;
private static Project testProject; private static Project testProject;
private static boolean createdSubDirectoryPathFile = false;
public TestCommitsApi() { public TestCommitsApi() {
super(); super();
...@@ -52,6 +53,25 @@ public class TestCommitsApi extends AbstractIntegrationTest { ...@@ -52,6 +53,25 @@ public class TestCommitsApi extends AbstractIntegrationTest {
// Must setup the connection to the GitLab test server and get the test Project instance // Must setup the connection to the GitLab test server and get the test Project instance
gitLabApi = baseTestSetup(); gitLabApi = baseTestSetup();
testProject = getTestProject(); testProject = getTestProject();
if (!gitLabApi.getRepositoryFileApi().getOptionalFile(testProject, TEST_PROJECT_SUBDIRECTORY_PATH, "master").isPresent()) {
try {
RepositoryFile repoFile = new RepositoryFile();
repoFile.setFilePath(TEST_PROJECT_SUBDIRECTORY_PATH);
repoFile.setContent("This is a test project used to test GitLab4J-API.");
gitLabApi.getRepositoryFileApi().createFile(testProject, repoFile, "master", "Initial commit.");
createdSubDirectoryPathFile = true;
} catch (GitLabApiException ignore) {}
}
}
@AfterClass
public static void teardown() {
if (createdSubDirectoryPathFile) {
try {
gitLabApi.getRepositoryFileApi().deleteFile(testProject, TEST_PROJECT_SUBDIRECTORY_PATH, "master", "No longer needed.");
} catch (Exception ignore) {}
}
} }
@Before @Before
...@@ -155,16 +175,12 @@ public class TestCommitsApi extends AbstractIntegrationTest { ...@@ -155,16 +175,12 @@ public class TestCommitsApi extends AbstractIntegrationTest {
CommitsApi commitsApi = gitLabApi.getCommitsApi(); CommitsApi commitsApi = gitLabApi.getCommitsApi();
List<Commit> commits = commitsApi.getCommits(testProject, "master", null); List<Commit> commits = commitsApi.getCommits(testProject, "master", null);
assertNotNull(commits); assertNotNull(commits);
assertTrue(commits.size() > 0); assertTrue(commits.size() > 1);
commits = commitsApi.getCommits(testProject, "master", "README");
assertNotNull(commits);
assertTrue(commits.size() > 0);
commitsApi = gitLabApi.getCommitsApi(); commitsApi = gitLabApi.getCommitsApi();
commits = commitsApi.getCommits(testProject, "master", "README.md"); commits = commitsApi.getCommits(testProject, "master", "README.md");
assertNotNull(commits); assertNotNull(commits);
assertTrue(commits.size() > 0); assertTrue(commits.size() > 1);
commits = commitsApi.getCommits(testProject, "master", TEST_PROJECT_SUBDIRECTORY_PATH); commits = commitsApi.getCommits(testProject, "master", TEST_PROJECT_SUBDIRECTORY_PATH);
assertNotNull(commits); assertNotNull(commits);
......
...@@ -4,6 +4,7 @@ import static org.junit.Assert.assertNotNull; ...@@ -4,6 +4,7 @@ import static org.junit.Assert.assertNotNull;
import static org.junit.Assume.assumeTrue; import static org.junit.Assume.assumeTrue;
import org.gitlab4j.api.models.HealthCheckInfo; import org.gitlab4j.api.models.HealthCheckInfo;
import org.gitlab4j.api.utils.AccessTokenUtils;
import org.junit.Before; import org.junit.Before;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
...@@ -23,8 +24,9 @@ public class TestHealthCheckApi implements PropertyConstants { ...@@ -23,8 +24,9 @@ public class TestHealthCheckApi implements PropertyConstants {
// The following needs to be set to your test repository // The following needs to be set to your test repository
private static final String TEST_HOST_URL = HelperUtils.getProperty(HOST_URL_KEY); private static final String TEST_HOST_URL = HelperUtils.getProperty(HOST_URL_KEY);
private static final String TEST_HEALTH_CHECK_TOKEN = HelperUtils.getProperty(HEALTH_CHECK_TOKEN_KEY); private static final String TEST_LOGIN_USERNAME = HelperUtils.getProperty(LOGIN_USERNAME_KEY);
private static final String TEST_LOGIN_PASSWORD = HelperUtils.getProperty(LOGIN_PASSWORD_KEY);
private static String TEST_HEALTH_CHECK_TOKEN = HelperUtils.getProperty(HEALTH_CHECK_TOKEN_KEY);
private static GitLabApi gitLabApi; private static GitLabApi gitLabApi;
public TestHealthCheckApi() { public TestHealthCheckApi() {
...@@ -32,13 +34,20 @@ public class TestHealthCheckApi implements PropertyConstants { ...@@ -32,13 +34,20 @@ public class TestHealthCheckApi implements PropertyConstants {
} }
@BeforeClass @BeforeClass
public static void setup() { public static void setup() throws GitLabApiException {
String problems = ""; String problems = "";
if (TEST_HOST_URL == null || TEST_HOST_URL.trim().isEmpty()) { if (TEST_HOST_URL == null || TEST_HOST_URL.trim().isEmpty()) {
problems += "TEST_HOST_URL cannot be empty\n"; problems += "TEST_HOST_URL cannot be empty\n";
} }
// Fetch the Health Check Token if not set already
if (TEST_HEALTH_CHECK_TOKEN == null || TEST_HEALTH_CHECK_TOKEN.trim().isEmpty()) {
TEST_HEALTH_CHECK_TOKEN = AccessTokenUtils.getHealthCheckAccessToken(
TEST_HOST_URL, TEST_LOGIN_USERNAME, TEST_LOGIN_PASSWORD);
HelperUtils.setProperty(HEALTH_CHECK_TOKEN_KEY, TEST_HEALTH_CHECK_TOKEN);
}
if (TEST_HEALTH_CHECK_TOKEN == null || TEST_HEALTH_CHECK_TOKEN.trim().isEmpty()) { if (TEST_HEALTH_CHECK_TOKEN == null || TEST_HEALTH_CHECK_TOKEN.trim().isEmpty()) {
problems += "TEST_HEALTH_CHECK_TOKEN cannot be empty\n"; problems += "TEST_HEALTH_CHECK_TOKEN cannot be empty\n";
} }
......
...@@ -94,11 +94,8 @@ public class TestRepositoryFileApi extends AbstractIntegrationTest { ...@@ -94,11 +94,8 @@ public class TestRepositoryFileApi extends AbstractIntegrationTest {
Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME); Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME);
assertNotNull(project); assertNotNull(project);
File file = gitLabApi.getRepositoryFileApi().getRawFile(project.getId(), "master", "README", null); File tempDir = new File(System.getProperty("java.io.tmpdir"));
assertTrue(file.length() > 0); File file = gitLabApi.getRepositoryFileApi().getRawFile(project.getId(), "master", "README.md", tempDir);
file.delete();
file = gitLabApi.getRepositoryFileApi().getRawFile(project.getId(), "master", "README", new File("."));
assertTrue(file.length() > 0); assertTrue(file.length() > 0);
file.delete(); file.delete();
} }
...@@ -109,9 +106,9 @@ public class TestRepositoryFileApi extends AbstractIntegrationTest { ...@@ -109,9 +106,9 @@ public class TestRepositoryFileApi extends AbstractIntegrationTest {
Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME); Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME);
assertNotNull(project); assertNotNull(project);
InputStream in = gitLabApi.getRepositoryFileApi().getRawFile(project.getId(), "master", "README"); InputStream in = gitLabApi.getRepositoryFileApi().getRawFile(project.getId(), "master", "README.md");
Path target = Files.createTempFile(TEST_PROJECT_NAME + "-README", ""); Path target = Files.createTempFile(TEST_PROJECT_NAME + "-README", "md");
Files.copy(in, target, StandardCopyOption.REPLACE_EXISTING); Files.copy(in, target, StandardCopyOption.REPLACE_EXISTING);
assertTrue(target.toFile().length() > 0); assertTrue(target.toFile().length() > 0);
...@@ -124,7 +121,7 @@ public class TestRepositoryFileApi extends AbstractIntegrationTest { ...@@ -124,7 +121,7 @@ public class TestRepositoryFileApi extends AbstractIntegrationTest {
Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME); Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME);
assertNotNull(project); assertNotNull(project);
RepositoryFile fileInfo = gitLabApi.getRepositoryFileApi().getFileInfo(project.getId(), "README", "master"); RepositoryFile fileInfo = gitLabApi.getRepositoryFileApi().getFileInfo(project.getId(), "README.md", "master");
assertNotNull(fileInfo); assertNotNull(fileInfo);
} }
...@@ -134,7 +131,7 @@ public class TestRepositoryFileApi extends AbstractIntegrationTest { ...@@ -134,7 +131,7 @@ public class TestRepositoryFileApi extends AbstractIntegrationTest {
Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME); Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME);
assertNotNull(project); assertNotNull(project);
Optional<RepositoryFile> fileInfo = gitLabApi.getRepositoryFileApi().getOptionalFileInfo(project.getId(), "README", "master"); Optional<RepositoryFile> fileInfo = gitLabApi.getRepositoryFileApi().getOptionalFileInfo(project.getId(), "README.md", "master");
assertNotNull(fileInfo.get()); assertNotNull(fileInfo.get());
fileInfo = gitLabApi.getRepositoryFileApi().getOptionalFileInfo(project.getId(), "I-DONT-EXIST", "master"); fileInfo = gitLabApi.getRepositoryFileApi().getOptionalFileInfo(project.getId(), "I-DONT-EXIST", "master");
......
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