From fcbf26a03b85818e313fc0ffb00bb337bb900a71 Mon Sep 17 00:00:00 2001 From: Tom Hacohen Date: Thu, 6 Apr 2017 19:05:48 +0100 Subject: [PATCH] Tests: Add journalentrymanager tests. --- .../journalmanager/ServiceTest.java | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/app/src/test/java/com/etesync/syncadapter/journalmanager/ServiceTest.java b/app/src/test/java/com/etesync/syncadapter/journalmanager/ServiceTest.java index ffbfaaea..626bbf9a 100644 --- a/app/src/test/java/com/etesync/syncadapter/journalmanager/ServiceTest.java +++ b/app/src/test/java/com/etesync/syncadapter/journalmanager/ServiceTest.java @@ -17,6 +17,7 @@ import org.junit.Before; import org.junit.Test; import java.io.IOException; +import java.util.LinkedList; import java.util.List; import okhttp3.HttpUrl; @@ -27,6 +28,7 @@ import okhttp3.RequestBody; import okhttp3.Response; import okio.BufferedSink; +import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; @@ -126,4 +128,72 @@ public class ServiceTest { } assertNotNull(caught); } + + + @Test + public void testSyncEntry() throws IOException, Exceptions.HttpException, Exceptions.GenericCryptoException, Exceptions.IntegrityException { + Exception caught; + JournalManager journalManager = new JournalManager(httpClient, remote); + CollectionInfo info = CollectionInfo.defaultForServiceType(CollectionInfo.Type.ADDRESS_BOOK); + info.uid = JournalManager.Journal.genUid(); + info.displayName = "Test"; + Crypto.CryptoManager crypto = new Crypto.CryptoManager(info.version, Helpers.keyBase64, info.uid); + JournalManager.Journal journal = new JournalManager.Journal(crypto, info.toJson(), info.uid); + journalManager.putJournal(journal); + + JournalEntryManager journalEntryManager = new JournalEntryManager(httpClient, remote, info.uid); + JournalEntryManager.Entry previousEntry = null; + JournalEntryManager.Entry entry = new JournalEntryManager.Entry(); + entry.update(crypto, "Content", previousEntry); + + List entries = new LinkedList<>(); + + entries.add(entry); + journalEntryManager.putEntries(entries, null); + previousEntry = entry; + + entries.clear(); + JournalEntryManager.Entry entry2 = new JournalEntryManager.Entry(); + entry2.update(crypto, "Content", previousEntry); + entries.add(entry2); + + // Pushing a correct entries without the last parameter + try { + caught = null; + journalEntryManager.putEntries(entries, null); + } catch (Exceptions.HttpException e) { + caught = e; + } + assertNotNull(caught); + + // Adding a second entry + journalEntryManager.putEntries(entries, previousEntry.getUid()); + previousEntry = entry2; + + entries.clear(); + entries.add(entry); + entries.add(entry2); + + // Check last works: + entries = journalEntryManager.getEntries(crypto, entry.getUid()); + assertEquals(entries.size(), 1); + entries = journalEntryManager.getEntries(crypto, entry2.getUid()); + assertEquals(entries.size(), 0); + + // Corrupt the journal and verify we catch it + entries.clear(); + entry2 = new JournalEntryManager.Entry(); + entry2.update(crypto, "Content", null); + entries.add(entry2); + + journalEntryManager.putEntries(entries, previousEntry.getUid()); + + try { + caught = null; + journalEntryManager.getEntries(crypto, null); + } catch (Exceptions.IntegrityException e) { + caught = e; + } + assertNotNull(caught); + } }