Search in sources :

Example 1 with NigoriDatastore

use of com.google.nigori.client.NigoriDatastore in project nigori by ucam-cl-dtg.

the class SetGetDeleteTest method getIndices.

@Test
public void getIndices() throws IOException, NigoriCryptographyException, UnauthorisedException {
    NigoriDatastore nigori = getStore();
    try {
        assertTrue("Not registered", nigori.register());
        final Index indexa = new Index("indexa");
        final Index indexb = new Index("indexb");
        final Revision revision = new Revision("a");
        assertTrue("Not put", nigori.put(indexa, revision, toBytes("aa")));
        assertTrue("Not put", nigori.put(indexb, revision, toBytes("bb")));
        try {
            List<Index> indices = nigori.getIndices();
            assertNotNull("No indices", indices);
            assertEquals("Not correct number of indices", 2, indices.size());
            assertThat(indices, hasItem(indexa));
            assertThat(indices, hasItem(indexb));
        } finally {
            nigori.delete(indexa, NULL_DELETE_TOKEN);
            nigori.delete(indexb, NULL_DELETE_TOKEN);
        }
    } finally {
        assertTrue("Not unregistered", nigori.unregister());
    }
}
Also used : Revision(com.google.nigori.common.Revision) NigoriDatastore(com.google.nigori.client.NigoriDatastore) Index(com.google.nigori.common.Index) Test(org.junit.Test)

Example 2 with NigoriDatastore

use of com.google.nigori.client.NigoriDatastore in project nigori by ucam-cl-dtg.

the class SetGetDeleteTest method immutableRevisions.

// @Test //TODO(drt24) Do we want to enforce revisions being immutable? Migori doesn't really need
// this due to the way revisions are generated.
public void immutableRevisions() throws IOException, NigoriCryptographyException, UnauthorisedException {
    NigoriDatastore nigori = getStore();
    try {
        assertTrue("Not registered", nigori.register());
        final Index index = new Index("index");
        final Revision revision = new Revision("rev");
        assertTrue("Not put", nigori.put(index, revision, Util.int2bin(0)));
        assertFalse("Could replace revision value", nigori.put(index, revision, Util.int2bin(1)));
        nigori.delete(index, NULL_DELETE_TOKEN);
    } finally {
        assertTrue("Not unregistered", nigori.unregister());
    }
}
Also used : Revision(com.google.nigori.common.Revision) NigoriDatastore(com.google.nigori.client.NigoriDatastore) Index(com.google.nigori.common.Index)

Example 3 with NigoriDatastore

use of com.google.nigori.client.NigoriDatastore in project nigori by ucam-cl-dtg.

the class SetGetDeleteTest method getRevisions.

@Test
public void getRevisions() throws IOException, NigoriCryptographyException, UnauthorisedException {
    NigoriDatastore nigori = getStore();
    try {
        assertTrue("Not registered", nigori.register());
        final Index index = new Index("index");
        final Revision a = new Revision("a");
        final Revision b = new Revision("b");
        assertTrue("Not put", nigori.put(index, a, toBytes("aa")));
        assertTrue("Not put", nigori.put(index, b, toBytes("bb")));
        try {
            List<Revision> revisions = nigori.getRevisions(index);
            assertNotNull("No revisions", revisions);
            assertEquals("Not correct number of revisions", 2, revisions.size());
            assertThat(revisions, hasItem(a));
            assertThat(revisions, hasItem(b));
        } finally {
            nigori.delete(index, NULL_DELETE_TOKEN);
        }
    } finally {
        assertTrue("Not unregistered", nigori.unregister());
    }
}
Also used : Revision(com.google.nigori.common.Revision) NigoriDatastore(com.google.nigori.client.NigoriDatastore) Index(com.google.nigori.common.Index) Test(org.junit.Test)

Example 4 with NigoriDatastore

use of com.google.nigori.client.NigoriDatastore in project nigori by ucam-cl-dtg.

the class ConcurrencyTest method singleUserConcurrency.

/**
 * Test that one user can do lots of things at the same time.
 *
 * @throws NigoriCryptographyException
 * @throws IOException
 * @throws UnauthorisedException
 */
@Test
public void singleUserConcurrency() throws NigoriCryptographyException, IOException, UnauthorisedException {
    failed = false;
    Thread[] threads = new Thread[THREADS];
    final NigoriDatastore nigori = getStore();
    assertTrue("Not registered", nigori.register());
    try {
        final List<Throwable> exceptionList = Collections.synchronizedList(new LinkedList<Throwable>());
        for (int j = 0; j < THREADS; ++j) {
            threads[j] = new Thread() {

                @Override
                public void run() {
                    boolean succeeded = false;
                    try {
                        Random r = new Random();
                        for (int i = 0; i < AcceptanceTests.REPEAT * REPEAT_FACTOR; ++i) {
                            // check we can do this more than once
                            for (IndexValue iv : SetGetDeleteTest.testCases) {
                                // once round for each
                                final Index index = new Index(new byte[16]);
                                // need to generate some different indices
                                r.nextBytes(index.getBytes());
                                final byte[] value = iv.revvalue.getValue();
                                final Revision revision = iv.revvalue.getRevision();
                                assertTrue("Not put" + i, nigori.put(index, revision, value));
                                try {
                                    assertArrayEquals("Got different" + i, value, nigori.getRevision(index, revision));
                                } finally {
                                    assertTrue("Not deleted" + i, nigori.delete(index, NULL_DELETE_TOKEN));
                                }
                                assertNull("Not deleted" + i, nigori.get(index));
                            }
                        }
                        succeeded = true;
                    } catch (Throwable e) {
                        exceptionList.add(e);
                    } finally {
                        if (!succeeded && !failed) {
                            failed = true;
                        }
                    }
                }
            };
        }
        startThenJoinThreads(threads);
        ifFailedPrintFailures(failed, exceptionList);
    } finally {
        assertTrue(nigori.unregister());
    }
}
Also used : Random(java.util.Random) Revision(com.google.nigori.common.Revision) NigoriDatastore(com.google.nigori.client.NigoriDatastore) Index(com.google.nigori.common.Index) IndexValue(com.google.nigori.client.accept.n.SetGetDeleteTest.IndexValue) Test(org.junit.Test)

Example 5 with NigoriDatastore

use of com.google.nigori.client.NigoriDatastore in project nigori by ucam-cl-dtg.

the class SetGetDeleteTest method setGetDelete.

@Test
public void setGetDelete() throws NigoriCryptographyException, IOException, UnauthorisedException {
    NigoriDatastore nigori = getStore();
    for (int i = 0; i < AcceptanceTests.REPEAT; ++i) {
        // check we can do this more than once
        try {
            assertTrue("Not registered" + i, nigori.register());
            assertTrue("Not a clean store " + i, nigori.getIndices().isEmpty());
            for (IndexValue iv : testCases) {
                // once round for each
                final Index index = iv.index;
                final Revision revision = iv.revvalue.getRevision();
                final byte[] value = iv.revvalue.getValue();
                assertTrue("Not put " + i, nigori.put(index, revision, value));
                List<RevValue> revs = nigori.get(index);
                assertFalse("Revisions must exist" + i, revs.isEmpty());
                assertThat(revs, hasItem(iv.revvalue));
                assertEquals("Not one revision " + i, 1, revs.size());
                assertTrue("Not deleted" + i, nigori.delete(index, NULL_DELETE_TOKEN));
                assertNull("Not deleted" + i, nigori.get(index));
                assertFalse("Could redelete", nigori.delete(index, NULL_DELETE_TOKEN));
            }
            // allow them to accumulate
            for (IndexValue iv : testCases) {
                final Index index = iv.index;
                final byte[] value = iv.revvalue.getValue();
                final Revision revision = iv.revvalue.getRevision();
                assertTrue("Not put" + i, nigori.put(index, revision, value));
            }
            try {
                for (IndexValue iv : testCases) {
                    final Index index = iv.index;
                    final byte[] value = iv.revvalue.getValue();
                    final Revision revision = iv.revvalue.getRevision();
                    assertArrayEquals("Got different" + i, value, nigori.getRevision(index, revision));
                }
            } finally {
                for (IndexValue iv : testCases) {
                    final Index index = iv.index;
                    if (!iv.later) {
                        assertTrue("Not deleted" + i, nigori.delete(index, NULL_DELETE_TOKEN));
                    } else {
                        // should have already been deleted
                        assertFalse("Not deleted" + i, nigori.delete(index, NULL_DELETE_TOKEN));
                    }
                }
            }
            for (IndexValue iv : testCases) {
                final Index index = iv.index;
                assertNull("Still there after deletion " + i, nigori.get(index));
            }
        } finally {
            assertTrue("Not unregistered", nigori.unregister());
        }
    }
}
Also used : Revision(com.google.nigori.common.Revision) RevValue(com.google.nigori.common.RevValue) NigoriDatastore(com.google.nigori.client.NigoriDatastore) Index(com.google.nigori.common.Index) Test(org.junit.Test)

Aggregations

NigoriDatastore (com.google.nigori.client.NigoriDatastore)5 Index (com.google.nigori.common.Index)5 Revision (com.google.nigori.common.Revision)5 Test (org.junit.Test)4 IndexValue (com.google.nigori.client.accept.n.SetGetDeleteTest.IndexValue)1 RevValue (com.google.nigori.common.RevValue)1 Random (java.util.Random)1