mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-13 00:45:44 +01:00
improve repository integration tests
This commit is contained in:
@@ -38,6 +38,9 @@ package sonia.scm.it;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
import org.junit.runners.Parameterized.Parameters;
|
||||
|
||||
import sonia.scm.repository.Permission;
|
||||
import sonia.scm.repository.PermissionType;
|
||||
@@ -56,23 +59,45 @@ import static sonia.scm.it.IntegrationTestUtil.*;
|
||||
|
||||
import com.sun.jersey.api.client.Client;
|
||||
import com.sun.jersey.api.client.ClientResponse;
|
||||
import com.sun.jersey.api.client.GenericType;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
public class RepositoryExtendedPermisionITCase
|
||||
@RunWith(Parameterized.class)
|
||||
public class RepositoryExtendedITCase
|
||||
{
|
||||
|
||||
/** Field description */
|
||||
private static Repository REPOSITORY = null;
|
||||
/**
|
||||
* Constructs ...
|
||||
*
|
||||
*
|
||||
*
|
||||
* @param repository
|
||||
* @param username
|
||||
* @param password
|
||||
*
|
||||
* @throws IOException
|
||||
* @throws RepositoryClientException
|
||||
*/
|
||||
public RepositoryExtendedITCase(Repository repository, String username,
|
||||
String password)
|
||||
throws RepositoryClientException, IOException
|
||||
{
|
||||
this.repository = repository;
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
//~--- methods --------------------------------------------------------------
|
||||
|
||||
@@ -86,10 +111,40 @@ public class RepositoryExtendedPermisionITCase
|
||||
Client client = createAdminClient();
|
||||
|
||||
createResource(client, "users/trillian").delete();
|
||||
createResource(client, "repositories/" + REPOSITORY.getId()).delete();
|
||||
|
||||
Collection<Repository> repositories =
|
||||
createResource(client, "repositories").get(
|
||||
new GenericType<Collection<Repository>>() {}
|
||||
);
|
||||
|
||||
if (repositories != null)
|
||||
{
|
||||
for (Repository r : repositories)
|
||||
{
|
||||
createResource(client, "repositories/" + r.getId()).delete();
|
||||
}
|
||||
}
|
||||
|
||||
client.destroy();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Parameters
|
||||
public static Collection<Object[]> createParameters()
|
||||
{
|
||||
Collection<Object[]> params = new ArrayList<Object[]>();
|
||||
Repository repository = createRepository("git", "trillian");
|
||||
|
||||
params.add(new Object[] { repository, "trillian", "secret" });
|
||||
|
||||
return params;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
@@ -102,19 +157,6 @@ public class RepositoryExtendedPermisionITCase
|
||||
|
||||
trillian.setPassword("secret");
|
||||
createResource(client, "users").post(trillian);
|
||||
|
||||
Repository repository = RepositoryTestData.createHeartOfGold("git");
|
||||
|
||||
repository.setPermissions(Arrays.asList(new Permission("trillian",
|
||||
PermissionType.WRITE)));
|
||||
|
||||
ClientResponse response = createResource(client,
|
||||
"repositories").post(ClientResponse.class,
|
||||
repository);
|
||||
String url = response.getHeaders().get("Location").get(0) + EXTENSION;
|
||||
|
||||
response.close();
|
||||
REPOSITORY = client.resource(url).get(Repository.class);
|
||||
client.destroy();
|
||||
}
|
||||
|
||||
@@ -123,26 +165,30 @@ public class RepositoryExtendedPermisionITCase
|
||||
*
|
||||
*
|
||||
*
|
||||
* @param repository
|
||||
* @param username
|
||||
* @param password
|
||||
*
|
||||
* @throws IOException
|
||||
* @throws RepositoryClientException
|
||||
*/
|
||||
@Test
|
||||
public void write() throws RepositoryClientException, IOException
|
||||
private static void addTestFiles(Repository repository, String username,
|
||||
String password)
|
||||
throws RepositoryClientException, IOException
|
||||
{
|
||||
File directory = new File(System.getProperty("java.io.tmpdir"),
|
||||
UUID.randomUUID().toString());
|
||||
File directory = createTempDirectory();
|
||||
|
||||
try
|
||||
{
|
||||
RepositoryClient rc = RepositoryClientFactory.createClient("git",
|
||||
directory, REPOSITORY.getUrl(), "trillian",
|
||||
"secret");
|
||||
RepositoryClient rc =
|
||||
RepositoryClientFactory.createClient(repository.getType(), directory,
|
||||
repository.getUrl(), username, password);
|
||||
|
||||
rc.init();
|
||||
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
createRandomFile(rc, directory, i);
|
||||
createRandomFile(rc, directory);
|
||||
}
|
||||
|
||||
rc.commit("added some test files");
|
||||
@@ -160,21 +206,21 @@ public class RepositoryExtendedPermisionITCase
|
||||
*
|
||||
* @param client
|
||||
* @param directory
|
||||
* @param i
|
||||
*
|
||||
* @throws IOException
|
||||
* @throws RepositoryClientException
|
||||
*/
|
||||
private void createRandomFile(RepositoryClient client, File directory, int i)
|
||||
private static void createRandomFile(RepositoryClient client, File directory)
|
||||
throws IOException, RepositoryClientException
|
||||
{
|
||||
String name = "file-" + i + ".uuid";
|
||||
String uuid = UUID.randomUUID().toString();
|
||||
String name = "file-" + uuid + ".uuid";
|
||||
FileOutputStream out = null;
|
||||
|
||||
try
|
||||
{
|
||||
out = new FileOutputStream(new File(directory, name));
|
||||
out.write(UUID.randomUUID().toString().getBytes());
|
||||
out.write(uuid.getBytes());
|
||||
}
|
||||
finally
|
||||
{
|
||||
@@ -183,4 +229,101 @@ public class RepositoryExtendedPermisionITCase
|
||||
|
||||
client.add(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param type
|
||||
* @param username
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
private static Repository createRepository(String type, String username)
|
||||
{
|
||||
Client client = createAdminClient();
|
||||
Repository repository = RepositoryTestData.createHeartOfGold(type);
|
||||
|
||||
repository.setPermissions(Arrays.asList(new Permission(username,
|
||||
PermissionType.WRITE)));
|
||||
|
||||
ClientResponse response = createResource(client,
|
||||
"repositories").post(ClientResponse.class,
|
||||
repository);
|
||||
String url = response.getHeaders().get("Location").get(0) + EXTENSION;
|
||||
|
||||
response.close();
|
||||
repository = client.resource(url).get(Repository.class);
|
||||
client.destroy();
|
||||
|
||||
return repository;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
private static File createTempDirectory()
|
||||
{
|
||||
File directory = new File(System.getProperty("java.io.tmpdir"),
|
||||
UUID.randomUUID().toString());
|
||||
|
||||
IOUtil.mkdirs(directory);
|
||||
|
||||
return directory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @throws IOException
|
||||
* @throws RepositoryClientException
|
||||
*/
|
||||
@Test
|
||||
public void read() throws RepositoryClientException, IOException
|
||||
{
|
||||
File directory = createTempDirectory();
|
||||
|
||||
try
|
||||
{
|
||||
RepositoryClient rc =
|
||||
RepositoryClientFactory.createClient(repository.getType(), directory,
|
||||
repository.getUrl(), username, password);
|
||||
|
||||
rc.init();
|
||||
rc.checkout();
|
||||
}
|
||||
finally
|
||||
{
|
||||
IOUtil.delete(directory);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
*
|
||||
* @throws IOException
|
||||
* @throws RepositoryClientException
|
||||
*/
|
||||
@Test
|
||||
public void write() throws RepositoryClientException, IOException
|
||||
{
|
||||
addTestFiles(repository, username, password);
|
||||
}
|
||||
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
/** Field description */
|
||||
private String password;
|
||||
|
||||
/** Field description */
|
||||
private Repository repository;
|
||||
|
||||
/** Field description */
|
||||
private String username;
|
||||
}
|
||||
Reference in New Issue
Block a user