<commit_before>package org.fcrepo.integration.api;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.codehaus.jackson.map.ObjectMapper;
import org.fcrepo.Transaction;
import org.fcrepo.Transaction.State;
import org.junit.Test;


public class FedoraTransactionsIT extends AbstractResourceIT {


	@Test
	public void testCreateAndGetTransaction() throws Exception {
		/* create a tx */
		HttpPost createTx = new HttpPost(serverAddress + "fcr:tx");
		HttpResponse resp = execute(createTx);
		assertTrue(resp.getStatusLine().getStatusCode() == 200);
		ObjectMapper mapper = new ObjectMapper();
		Transaction tx = mapper.readValue(resp.getEntity().getContent(), Transaction.class);
		createTx.releaseConnection();
		assertNotNull(tx);
		assertNotNull(tx.getId());
		assertNotNull(tx.getState());
		assertNotNull(tx.getCreated());
		assertTrue(tx.getState() == State.NEW);

		/* fetch the created tx from the endpoint */
		HttpGet getTx = new HttpGet(serverAddress + "fcr:tx/" + tx.getId());
		resp = execute(getTx);
		Transaction fetched = mapper.readValue(resp.getEntity().getContent(), Transaction.class);
		getTx.releaseConnection();
		assertEquals(tx.getId(), fetched.getId());
		assertEquals(tx.getState(), fetched.getState());
		assertEquals(tx.getCreated(), fetched.getCreated());
	}


	@Test
	public void testCreateAndTimeoutTransaction() throws Exception {
	    System.setProperty("fcrepo4.tx.timeout", "10");
        /* create a tx */
        HttpPost createTx = new HttpPost(serverAddress + "fcr:tx");
        HttpResponse resp = execute(createTx);
        assertTrue(resp.getStatusLine().getStatusCode() == 200);
        ObjectMapper mapper = new ObjectMapper();
        Transaction tx = mapper.readValue(resp.getEntity().getContent(), Transaction.class);
        assertNotNull(tx);
        assertNotNull(tx.getId());
        assertNotNull(tx.getState());
        assertNotNull(tx.getCreated());
        assertTrue(tx.getState() == State.NEW);
        createTx.releaseConnection();

        Thread.sleep(200); // wait for the tx to expire

        /* check that the tx is removed */
        HttpGet getTx = new HttpGet(serverAddress + "fcr:tx/" + tx.getId());
        resp = execute(getTx);
        getTx.releaseConnection();
        assertEquals(404, resp.getStatusLine().getStatusCode());
        System.clearProperty("fcrepo4.tx.timeout");
	}

	@Test
	public void testCreateAndCommitTransaction() throws Exception {
		/* create a tx */
		HttpPost createTx = new HttpPost(serverAddress + "fcr:tx");
		HttpResponse resp = execute(createTx);
		assertTrue(resp.getStatusLine().getStatusCode() == 200);
		ObjectMapper mapper = new ObjectMapper();
		Transaction tx = mapper.readValue(resp.getEntity().getContent(), Transaction.class);
		createTx.releaseConnection();
		assertNotNull(tx);
		assertNotNull(tx.getId());
		assertNotNull(tx.getState());
		assertNotNull(tx.getCreated());
		assertTrue(tx.getState() == State.NEW);

		/* create a new object inside the tx */
		HttpPost postNew = new HttpPost(serverAddress + "fcr:tx/" + tx.getId() + "/objects/testobj1/fcr:newhack");
		resp = execute(postNew);
		postNew.releaseConnection();
		assertTrue(resp.getStatusLine().getStatusCode() == 200);

		/* check if the object is already there, before the commit */
		HttpGet getObj = new HttpGet(serverAddress + "/objects/testobj1");
		resp = execute(getObj);
		getObj.releaseConnection();
		assertTrue(resp.getStatusLine().toString(),resp.getStatusLine().getStatusCode() == 404);

		/* commit the tx */
		HttpPost commitTx = new HttpPost(serverAddress + "fcr:tx/" + tx.getId() + "/fcr:commit");
		resp = execute(commitTx);
		assertTrue(resp.getStatusLine().getStatusCode() == 200);
		Transaction committed = mapper.readValue(resp.getEntity().getContent(), Transaction.class);
		commitTx.releaseConnection();
		assertEquals(committed.getState(), State.COMMITED);

		/* check if the object is there, after the commit */
		resp = execute(getObj);
		assertTrue(resp.getStatusLine().toString(),resp.getStatusLine().getStatusCode() == 200);
		getObj.releaseConnection();
	}

	@Test
	public void testCreateAndRollbackTransaction() throws Exception {
		/* create a tx */
		HttpPost createTx = new HttpPost(serverAddress + "fcr:tx");
		HttpResponse resp = execute(createTx);
		assertTrue(resp.getStatusLine().getStatusCode() == 200);
		ObjectMapper mapper = new ObjectMapper();
		Transaction tx = mapper.readValue(resp.getEntity().getContent(), Transaction.class);
		createTx.releaseConnection();
		assertNotNull(tx);
		assertNotNull(tx.getId());
		assertNotNull(tx.getState());
		assertNotNull(tx.getCreated());
		assertTrue(tx.getState() == State.NEW);

		/* create a new object inside the tx */
		HttpPost postNew = new HttpPost(serverAddress + "fcr:tx/" + tx.getId() + "/objects/testobj2/fcr:newhack");
		resp = execute(postNew);
		assertTrue(resp.getStatusLine().getStatusCode() == 200);
		postNew.releaseConnection();

		/* check if the object is already there, before the commit */
		HttpGet getObj = new HttpGet(serverAddress + "/objects/testobj2");
		resp = execute(getObj);
		getObj.releaseConnection();
		assertTrue(resp.getStatusLine().toString(),resp.getStatusLine().getStatusCode() == 404);

		/* and rollback */
		HttpPost rollbackTx = new HttpPost(serverAddress + "fcr:tx/" + tx.getId() + "/fcr:rollback");
		resp = execute(rollbackTx);
		Transaction rolledBack = mapper.readValue(resp.getEntity().getContent(), Transaction.class);
		rollbackTx.releaseConnection();
		assertEquals(rolledBack.getId(),tx.getId());
		assertTrue(rolledBack.getState() == State.ROLLED_BACK);

		/* check if the object is there, after the rollback */
		resp = execute(getObj);
		getObj.releaseConnection();
		assertTrue(resp.getStatusLine().toString(),resp.getStatusLine().getStatusCode() == 404);
	}
}<commit_msg>added retry mechanism for timeouts<commit_after>
package org.fcrepo.integration.api;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.codehaus.jackson.map.ObjectMapper;
import org.fcrepo.Transaction;
import org.fcrepo.Transaction.State;
import org.junit.Test;

public class FedoraTransactionsIT extends AbstractResourceIT {

    @Test
    public void testCreateAndGetTransaction() throws Exception {
        /* create a tx */
        HttpPost createTx = new HttpPost(serverAddress + "fcr:tx");
        HttpResponse resp = execute(createTx);
        assertTrue(resp.getStatusLine().getStatusCode() == 200);
        ObjectMapper mapper = new ObjectMapper();
        Transaction tx =
                mapper.readValue(resp.getEntity().getContent(),
                        Transaction.class);
        createTx.releaseConnection();
        assertNotNull(tx);
        assertNotNull(tx.getId());
        assertNotNull(tx.getState());
        assertNotNull(tx.getCreated());
        assertTrue(tx.getState() == State.NEW);

        /* fetch the created tx from the endpoint */
        HttpGet getTx = new HttpGet(serverAddress + "fcr:tx/" + tx.getId());
        resp = execute(getTx);
        Transaction fetched =
                mapper.readValue(resp.getEntity().getContent(),
                        Transaction.class);
        getTx.releaseConnection();
        assertEquals(tx.getId(), fetched.getId());
        assertEquals(tx.getState(), fetched.getState());
        assertEquals(tx.getCreated(), fetched.getCreated());
    }

    @Test
    public void testCreateAndTimeoutTransaction() throws Exception {
        System.setProperty("fcrepo4.tx.timeout", "10");
        /* create a tx */
        HttpPost createTx = new HttpPost(serverAddress + "fcr:tx");
        HttpResponse resp = execute(createTx);
        assertTrue(resp.getStatusLine().getStatusCode() == 200);
        ObjectMapper mapper = new ObjectMapper();
        Transaction tx =
                mapper.readValue(resp.getEntity().getContent(),
                        Transaction.class);
        assertNotNull(tx);
        assertNotNull(tx.getId());
        assertNotNull(tx.getState());
        assertNotNull(tx.getCreated());
        assertTrue(tx.getState() == State.NEW);
        createTx.releaseConnection();

        boolean expired = false;
        long start = System.currentTimeMillis();
        while (!expired && System.currentTimeMillis() - start < 300) {
            /* check that the tx is removed */
            HttpGet getTx = new HttpGet(serverAddress + "fcr:tx/" + tx.getId());
            resp = execute(getTx);
            getTx.releaseConnection();
            expired = (404 == resp.getStatusLine().getStatusCode());
        }

        assertTrue("Transaction did not expire", expired);
        System.clearProperty("fcrepo4.tx.timeout");
    }

    @Test
    public void testCreateAndCommitTransaction() throws Exception {
        /* create a tx */
        HttpPost createTx = new HttpPost(serverAddress + "fcr:tx");
        HttpResponse resp = execute(createTx);
        assertTrue(resp.getStatusLine().getStatusCode() == 200);
        ObjectMapper mapper = new ObjectMapper();
        Transaction tx =
                mapper.readValue(resp.getEntity().getContent(),
                        Transaction.class);
        createTx.releaseConnection();
        assertNotNull(tx);
        assertNotNull(tx.getId());
        assertNotNull(tx.getState());
        assertNotNull(tx.getCreated());
        assertTrue(tx.getState() == State.NEW);

        /* create a new object inside the tx */
        HttpPost postNew =
                new HttpPost(serverAddress + "fcr:tx/" + tx.getId() +
                        "/objects/testobj1/fcr:newhack");
        resp = execute(postNew);
        postNew.releaseConnection();
        assertTrue(resp.getStatusLine().getStatusCode() == 200);

        /* check if the object is already there, before the commit */
        HttpGet getObj = new HttpGet(serverAddress + "/objects/testobj1");
        resp = execute(getObj);
        getObj.releaseConnection();
        assertTrue(resp.getStatusLine().toString(), resp.getStatusLine()
                .getStatusCode() == 404);

        /* commit the tx */
        HttpPost commitTx =
                new HttpPost(serverAddress + "fcr:tx/" + tx.getId() +
                        "/fcr:commit");
        resp = execute(commitTx);
        assertTrue(resp.getStatusLine().getStatusCode() == 200);
        Transaction committed =
                mapper.readValue(resp.getEntity().getContent(),
                        Transaction.class);
        commitTx.releaseConnection();
        assertEquals(committed.getState(), State.COMMITED);

        /* check if the object is there, after the commit */
        resp = execute(getObj);
        assertTrue(resp.getStatusLine().toString(), resp.getStatusLine()
                .getStatusCode() == 200);
        getObj.releaseConnection();
    }

    @Test
    public void testCreateAndRollbackTransaction() throws Exception {
        /* create a tx */
        HttpPost createTx = new HttpPost(serverAddress + "fcr:tx");
        HttpResponse resp = execute(createTx);
        assertTrue(resp.getStatusLine().getStatusCode() == 200);
        ObjectMapper mapper = new ObjectMapper();
        Transaction tx =
                mapper.readValue(resp.getEntity().getContent(),
                        Transaction.class);
        createTx.releaseConnection();
        assertNotNull(tx);
        assertNotNull(tx.getId());
        assertNotNull(tx.getState());
        assertNotNull(tx.getCreated());
        assertTrue(tx.getState() == State.NEW);

        /* create a new object inside the tx */
        HttpPost postNew =
                new HttpPost(serverAddress + "fcr:tx/" + tx.getId() +
                        "/objects/testobj2/fcr:newhack");
        resp = execute(postNew);
        assertTrue(resp.getStatusLine().getStatusCode() == 200);
        postNew.releaseConnection();

        /* check if the object is already there, before the commit */
        HttpGet getObj = new HttpGet(serverAddress + "/objects/testobj2");
        resp = execute(getObj);
        getObj.releaseConnection();
        assertTrue(resp.getStatusLine().toString(), resp.getStatusLine()
                .getStatusCode() == 404);

        /* and rollback */
        HttpPost rollbackTx =
                new HttpPost(serverAddress + "fcr:tx/" + tx.getId() +
                        "/fcr:rollback");
        resp = execute(rollbackTx);
        Transaction rolledBack =
                mapper.readValue(resp.getEntity().getContent(),
                        Transaction.class);
        rollbackTx.releaseConnection();
        assertEquals(rolledBack.getId(), tx.getId());
        assertTrue(rolledBack.getState() == State.ROLLED_BACK);

        /* check if the object is there, after the rollback */
        resp = execute(getObj);
        getObj.releaseConnection();
        assertTrue(resp.getStatusLine().toString(), resp.getStatusLine()
                .getStatusCode() == 404);
    }
}
{'fraction_non_alphanumeric': 0.13125703564727956, 'fraction_numerical': 0.007054409005628518, 'mean_word_length': 3.129532073132941, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7663810', 'n_tokens_mistral': 3766, 'n_tokens_neox': 3624, 'n_words': 754}