Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Adding examples for integration test cases (FCREPO-1584)

...

Integration tests use embedded Modeshape, servlet engine, etc. to run real requests against the code.  The web-based tests typically use HttpClient to make requests.  Non-web tests, use injection to have the repository and service objects made accessible to the tests (the embedded repository and injection are configured in the src/test/resources directory).  Some good examples are in the fcrepo-http-api module (e.g. FedoraNodesIT.java) and fcrepo-kernel module (e.g., FedoraResourceImplIT.java). By convention, integration test classes are named as: <FunctionalClass>IT.java

Examples

Code Block
languagejava
titlefcrepo-http-api/src/test/java/org/fcrepo/integration/http/api/FedoraNodesIT.java
firstline53
linenumberstrue
collapsetrue
 

...

   @Test
    public void testCopy() throws Exception {
        final HttpResponse response  = createObject("");
        final String pid = getRandomUniquePid();
        final String location = response.getFirstHeader("Location").getValue();
        final HttpCopy request = new HttpCopy(location);
        request.addHeader("Destination", serverAddress + pid);
        client.execute(request);

        final HttpGet httpGet = new HttpGet(serverAddress + pid);

        final HttpResponse copiedResult = client.execute(httpGet);
        assertEquals(OK.getStatusCode(), copiedResult.getStatusLine().getStatusCode());

        final HttpResponse originalResult = client.execute(new HttpGet(location));
        assertEquals(OK.getStatusCode(), originalResult.getStatusLine().getStatusCode());
    }

    @Test
    public void testCopyDestExists() throws Exception {
        final HttpResponse response1 = createObject("");
        final String location1 = response1.getFirstHeader("Location").getValue();
        final HttpResponse response2 = createObject("");
        final String location2 = response2.getFirstHeader("Location").getValue();

        final HttpCopy request = new HttpCopy(location1);
        request.addHeader("Destination", location2);
        final HttpResponse result = client.execute(request);
        assertEquals(PRECONDITION_FAILED.getStatusCode(), result.getStatusLine().getStatusCode());
    }
Code Block
languagejava
titlefcrepo-auth-common/src/test/java/org/fcrepo/auth/integration/FedoraResponseCodesIT.java
firstline36
linenumberstrue
collapsetrue
    @Test
    public void testAllowedAddDatastream() throws Exception {
        final String pid = getRandomUniquePid() + "Permit";
        final HttpPut objMethod = putObjMethod(pid);
        assertEquals(201, getStatus(objMethod));

        final HttpPost method = postDSMethod(pid, "zxcpermit", "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 +
            "/zxcpermit/jcr:content", location);
    }

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

        assertEquals(201, getStatus(objMethod));

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

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

...

Remember to follow the Code Style Guide when writing your test classes. This is especially true if writing a test class is your first foray into contributing to Fedora.

...