<commit_before><commit_msg>Add a transaction store test<commit_after>package peergos.server.tests;

import org.junit.*;
import org.junit.runner.*;
import org.junit.runners.*;
import peergos.server.sql.*;
import peergos.server.storage.*;
import peergos.server.util.*;
import peergos.shared.crypto.hash.*;
import peergos.shared.io.ipfs.cid.*;
import peergos.shared.io.ipfs.multihash.*;
import peergos.shared.storage.*;

import java.security.*;
import java.util.*;

@RunWith(Parameterized.class)
public class TransactionsStoreTests {

    private final TransactionStore store;

    public TransactionsStoreTests(TransactionStore store) {
        this.store = store;
    }

    @Parameterized.Parameters()
    public static Collection<Object[]> parameters() throws Exception {
        TransactionStore ram = JdbcTransactionStore.build(Sqlite.build(":memory:"), new SqliteCommands());
        return Arrays.asList(new Object[][] {
                {ram}
        });
    }

    public static byte[] hash(byte[] input) {
        try {
            MessageDigest md = MessageDigest.getInstance("SHA-256");
            md.update(input);
            return md.digest();
        } catch (NoSuchAlgorithmException e) {
            throw new IllegalStateException("couldn't find hash algorithm");
        }
    }

    public static Cid hashToCid(byte[] input, boolean isRaw) {
        byte[] hash = hash(input);
        return new Cid(1, isRaw ? Cid.Codec.Raw : Cid.Codec.DagCbor, Multihash.Type.sha2_256, hash);
    }

    @Test
    public void singleTransaction() {
        Cid multihash = hashToCid(new byte[2], true);
        PublicKeyHash owner = new PublicKeyHash(multihash);
        TransactionId tid = store.startTransaction(owner);
        List<Multihash> pending = new ArrayList<>();
        for (int i=0; i < 20; i++) {
            Cid block = hashToCid(new byte[]{(byte) i}, false);
            pending.add(block);
            store.addBlock(block, tid, owner);
        }
        List<Multihash> uncommitted = store.getOpenTransactionBlocks();
        Assert.assertTrue("All blocks present", uncommitted.containsAll(pending));

        store.closeTransaction(owner, tid);
        List<Multihash> empty = store.getOpenTransactionBlocks();
        Assert.assertTrue("All blocks removed", empty.isEmpty());
    }
}
{'fraction_non_alphanumeric': 0.12391681109185441, 'fraction_numerical': 0.005199306759098787, 'mean_word_length': 3.0367132867132867, 'pattern_counts': {'":': 1, '<': 9, '<?xml version=': 0, '>': 8, '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': '8636', 'n_tokens_mistral': 668, 'n_tokens_neox': 656, 'n_words': 150}