Skip to content

Commit

Permalink
Fix integration tests which were breaking in non-clean builds
Browse files Browse the repository at this point in the history
  • Loading branch information
ksclarke authored and Andrew Woods committed Apr 29, 2014
1 parent 7eb2a63 commit ba01f43
Show file tree
Hide file tree
Showing 7 changed files with 201 additions and 225 deletions.
Expand Up @@ -18,6 +18,7 @@

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.UUID;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
Expand Down Expand Up @@ -115,4 +116,11 @@ protected int getStatus(final HttpUriRequest method)
return result;
}

/**
* Gets a random (but valid) pid for use in testing. This pid is guaranteed
* to be unique within runs of this application.
*/
protected static String getRandomUniquePid() {
return UUID.randomUUID().toString();
}
}
Expand Up @@ -19,77 +19,62 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.io.IOException;
import java.util.UUID;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.util.EntityUtils;
import org.junit.Before;
import org.junit.Test;


public class FedoraResponseCodesIT extends AbstractResourceIT {

@Test
public void testAllowedAddDatastream() throws Exception {
final String pid = UUID.randomUUID().toString() + "Permit";
final HttpPut objMethod =
putObjMethod(pid);

final String pid = getRandomUniquePid() + "Permit";
final HttpPut objMethod = putObjMethod(pid);
assertEquals(201, getStatus(objMethod));
final HttpPost method =
postDSMethod(pid, "zxcpermit",
"foo");

final HttpPost method = postDSMethod(pid, "zxcpermit", "foo");
final HttpResponse response = client.execute(method);
final String location =
response.getFirstHeader("Location").getValue();
final String location = response.getFirstHeader("Location").getValue();
assertEquals(201, response.getStatusLine().getStatusCode());
assertEquals(
"Got wrong URI in Location header for datastream creation!",
serverAddress +
pid + "/zxcpermit/fcr:content",
location);
assertEquals("Got wrong URI in Location header for datastream creation!", serverAddress + pid +
"/zxcpermit/fcr:content", location);
}

@Test
public void testDeniedAddDatastream() throws Exception {
final String pid = UUID.randomUUID().toString() + "Permit";
final HttpPut objMethod =
putObjMethod(pid);
final String pid = getRandomUniquePid() + "Permit";
final HttpPut objMethod = putObjMethod(pid);

assertEquals(201, getStatus(objMethod));
final HttpPut obj2Method = putObjMethod(
pid + "/FedoraDatastreamsTest2Deny");

final HttpPut obj2Method = putObjMethod(pid + "/FedoraDatastreamsTest2Deny");
assertEquals(201, getStatus(obj2Method));
final HttpPost method = postDSMethod(pid + "/FedoraDatastreamsTest2Deny",
"zxc", "foo");

final HttpPost method = postDSMethod(pid + "/FedoraDatastreamsTest2Deny", "zxc", "foo");
final HttpResponse response = client.execute(method);
assertEquals(403, response.getStatusLine().getStatusCode());
}

@Test
public void testAllowedAddDeepDatastream() throws Exception {
final String pid = UUID.randomUUID().toString() + "Permit";
final String pid = getRandomUniquePid() + "Permit";
final HttpPut method =
putDSMethod(
pid + "/does_permit/not_permit/exist_permit/yet_permit",
"zxc_permit", "foo");
putDSMethod(pid + "/does_permit/not_permit/exist_permit/yet_permit", "zxc_permit", "foo");

final HttpResponse response = client.execute(method);
final String location =
response.getFirstHeader("Location").getValue();
assertEquals(201, response.getStatusLine().getStatusCode());
assertEquals(
"Got wrong URI in Location header for datastream creation!",
serverAddress +
pid + "/does_permit/not_permit/exist_permit/yet_permit/zxc_permit/fcr:content",
location);
assertEquals("Got wrong URI in Location header for datastream creation!", serverAddress + pid +
"/does_permit/not_permit/exist_permit/yet_permit/zxc_permit/fcr:content", location);
}

@Test
public void testDeniedAddDeepDatastream() throws Exception {
final String pid = UUID.randomUUID().toString() + "Permit";
final String pid = getRandomUniquePid() + "Permit";
final HttpPut method =
putDSMethod(
pid + "/does_permit/not_permit/exist_permit/yet_permit/allowed_child",
Expand All @@ -100,33 +85,29 @@ public void testDeniedAddDeepDatastream() throws Exception {

@Test
public void testAllowedPutDatastream() throws Exception {
final String pid = UUID.randomUUID().toString() + "Permit";
final HttpPut objMethod =
putObjMethod(pid);
final String pid = getRandomUniquePid() + "Permit";
final HttpPut objMethod = putObjMethod(pid);
assertEquals(201, getStatus(objMethod));
final HttpPut method =
putDSMethod(pid,
"zxc_permit", "foo");
final HttpPut method = putDSMethod(pid, "zxc_permit", "foo");
final HttpResponse response = client.execute(method);
assertEquals(201, response.getStatusLine().getStatusCode());
}

@Test
public void testDeniedPutDatastream() throws Exception {
final String pid = UUID.randomUUID().toString() + "Permit";
final HttpPut objMethod =
putObjMethod(pid + "/allowed_child");
final String pid = getRandomUniquePid() + "Permit";

final HttpPut objMethod = putObjMethod(pid + "/allowed_child");
assertEquals(201, getStatus(objMethod));
final HttpPut method =
putDSMethod(pid + "/allowed_child",
"zxc", "foo");

final HttpPut method = putDSMethod(pid + "/allowed_child", "zxc", "foo");
final HttpResponse response = client.execute(method);
assertEquals(403, response.getStatusLine().getStatusCode());
}

// @Test
public void testGetDatastreamContent() throws Exception {
final String pid = UUID.randomUUID().toString() + "Permit";
final String pid = getRandomUniquePid() + "Permit";
// TODO requires Grizzly client authN, see:
// https://java.net/projects/jersey/sources/svn/content/trunk/jersey/samples/https-clientserver-grizzly/src/main/java/com/sun/jersey/samples/https_grizzly/Server.java?rev=5853
// https://java.net/projects/jersey/sources/svn/content/trunk/jersey/samples/https-clientserver-grizzly/src/main/java/com/sun/jersey/samples/https_grizzly/auth/SecurityFilter.java?rev=5853
Expand All @@ -136,22 +117,20 @@ public void testGetDatastreamContent() throws Exception {

assertEquals(201, getStatus(objMethod));

final HttpPost createDSMethod =
postDSMethod(pid, "ds1",
"marbles for everyone");
final HttpPost createDSMethod = postDSMethod(pid, "ds1", "marbles for everyone");
assertEquals(201, getStatus(createDSMethod));
final HttpGet method_test_get =
new HttpGet(serverAddress +
pid + "/ds1/fcr:content");

final HttpGet method_test_get = new HttpGet(serverAddress + pid + "/ds1/fcr:content");
assertEquals(200, getStatus(method_test_get));

final HttpResponse response = client.execute(method_test_get);

logger.debug("Returned from HTTP GET, now checking content...");
assertTrue("Got the wrong content back!", "marbles for everyone"
.equals(EntityUtils.toString(response.getEntity())));

assertEquals("urn:sha1:ba6cb22191300aebcfcfb83de9635d6b224677df",
response.getFirstHeader("ETag").getValue().replace("\"",
""));
response.getFirstHeader("ETag").getValue().replace("\"", ""));

logger.debug("Content was correct.");
}
Expand Down

0 comments on commit ba01f43

Please sign in to comment.