Improved tests for repositories
This commit is contained in:
parent
9c375d6608
commit
60adf73616
@ -30,11 +30,12 @@ import org.junit.Test;
|
|||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
import static ch.dissem.bitmessage.entity.Plaintext.Type.MSG;
|
import static ch.dissem.bitmessage.entity.Plaintext.Type.MSG;
|
||||||
import static ch.dissem.bitmessage.utils.Singleton.security;
|
import static ch.dissem.bitmessage.utils.Singleton.security;
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.hamcrest.CoreMatchers.is;
|
||||||
import static org.junit.Assert.assertNotNull;
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
public class JdbcMessageRepositoryTest extends TestBase {
|
public class JdbcMessageRepositoryTest extends TestBase {
|
||||||
private BitmessageAddress contactA;
|
private BitmessageAddress contactA;
|
||||||
@ -47,6 +48,7 @@ public class JdbcMessageRepositoryTest extends TestBase {
|
|||||||
|
|
||||||
private Label inbox;
|
private Label inbox;
|
||||||
private Label drafts;
|
private Label drafts;
|
||||||
|
private Label unread;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
@ -72,27 +74,28 @@ public class JdbcMessageRepositoryTest extends TestBase {
|
|||||||
|
|
||||||
inbox = repo.getLabels(Label.Type.INBOX).get(0);
|
inbox = repo.getLabels(Label.Type.INBOX).get(0);
|
||||||
drafts = repo.getLabels(Label.Type.DRAFT).get(0);
|
drafts = repo.getLabels(Label.Type.DRAFT).get(0);
|
||||||
|
unread = repo.getLabels(Label.Type.UNREAD).get(0);
|
||||||
|
|
||||||
addMessage(contactA, identity, Plaintext.Status.RECEIVED, inbox);
|
addMessage(contactA, identity, Plaintext.Status.RECEIVED, inbox, unread);
|
||||||
addMessage(identity, contactA, Plaintext.Status.DRAFT, drafts);
|
addMessage(identity, contactA, Plaintext.Status.DRAFT, drafts);
|
||||||
addMessage(identity, contactB, Plaintext.Status.DRAFT);
|
addMessage(identity, contactB, Plaintext.Status.DRAFT, unread);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetLabels() throws Exception {
|
public void ensureLabelsAreRetrieved() throws Exception {
|
||||||
List<Label> labels = repo.getLabels();
|
List<Label> labels = repo.getLabels();
|
||||||
assertEquals(5, labels.size());
|
assertEquals(5, labels.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetLabelsByType() throws Exception {
|
public void ensureLabelsCanBeRetrievedByType() throws Exception {
|
||||||
List<Label> labels = repo.getLabels(Label.Type.INBOX);
|
List<Label> labels = repo.getLabels(Label.Type.INBOX);
|
||||||
assertEquals(1, labels.size());
|
assertEquals(1, labels.size());
|
||||||
assertEquals("Inbox", labels.get(0).toString());
|
assertEquals("Inbox", labels.get(0).toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testFindMessagesByLabel() throws Exception {
|
public void ensureMessagesCanBeFoundByLabel() throws Exception {
|
||||||
List<Plaintext> messages = repo.findMessages(inbox);
|
List<Plaintext> messages = repo.findMessages(inbox);
|
||||||
assertEquals(1, messages.size());
|
assertEquals(1, messages.size());
|
||||||
Plaintext m = messages.get(0);
|
Plaintext m = messages.get(0);
|
||||||
@ -101,6 +104,28 @@ public class JdbcMessageRepositoryTest extends TestBase {
|
|||||||
assertEquals(Plaintext.Status.RECEIVED, m.getStatus());
|
assertEquals(Plaintext.Status.RECEIVED, m.getStatus());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void ensureUnreadMessagesCanBeFoundForAllLabels() {
|
||||||
|
int unread = repo.countUnread(null);
|
||||||
|
assertThat(unread, is(2));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void ensureUnreadMessagesCanBeFoundByLabel() {
|
||||||
|
int unread = repo.countUnread(inbox);
|
||||||
|
assertThat(unread, is(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void ensureMessageCanBeRetrievedByInitialHash() {
|
||||||
|
byte[] initialHash = new byte[64];
|
||||||
|
Plaintext message = repo.findMessages(contactA).get(0);
|
||||||
|
message.setInitialHash(initialHash);
|
||||||
|
repo.save(message);
|
||||||
|
Plaintext other = repo.getMessage(initialHash);
|
||||||
|
assertThat(other, is(message));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testFindMessagesByStatus() throws Exception {
|
public void testFindMessagesByStatus() throws Exception {
|
||||||
List<Plaintext> messages = repo.findMessages(Plaintext.Status.RECEIVED);
|
List<Plaintext> messages = repo.findMessages(Plaintext.Status.RECEIVED);
|
||||||
|
@ -0,0 +1,84 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2016 Christian Basler
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package ch.dissem.bitmessage.repository;
|
||||||
|
|
||||||
|
import ch.dissem.bitmessage.entity.BitmessageAddress;
|
||||||
|
import ch.dissem.bitmessage.entity.ObjectMessage;
|
||||||
|
import ch.dissem.bitmessage.entity.payload.GetPubkey;
|
||||||
|
import ch.dissem.bitmessage.ports.ProofOfWorkRepository;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.hamcrest.CoreMatchers.*;
|
||||||
|
import static org.junit.Assert.assertThat;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Christian Basler
|
||||||
|
*/
|
||||||
|
public class JdbcProofOfWorkRepositoryTest extends TestBase {
|
||||||
|
private TestJdbcConfig config;
|
||||||
|
private JdbcProofOfWorkRepository repo;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() throws Exception {
|
||||||
|
config = new TestJdbcConfig();
|
||||||
|
config.reset();
|
||||||
|
|
||||||
|
repo = new JdbcProofOfWorkRepository(config);
|
||||||
|
|
||||||
|
repo.putObject(new ObjectMessage.Builder()
|
||||||
|
.payload(new GetPubkey(new BitmessageAddress("BM-2DAjcCFrqFrp88FUxExhJ9kPqHdunQmiyn"))).build(),
|
||||||
|
1000, 1000);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void ensureObjectIsStored() throws Exception {
|
||||||
|
int sizeBefore = repo.getItems().size();
|
||||||
|
repo.putObject(new ObjectMessage.Builder()
|
||||||
|
.payload(new GetPubkey(new BitmessageAddress("BM-2D9U2hv3YBMHM1zERP32anKfVKohyPN9x2"))).build(),
|
||||||
|
1000, 1000);
|
||||||
|
assertThat(repo.getItems().size(), is(sizeBefore + 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void ensureItemCanBeRetrieved() {
|
||||||
|
byte[] initialHash = repo.getItems().get(0);
|
||||||
|
ProofOfWorkRepository.Item item = repo.getItem(initialHash);
|
||||||
|
assertThat(item, notNullValue());
|
||||||
|
assertThat(item.object.getPayload(), instanceOf(GetPubkey.class));
|
||||||
|
assertThat(item.nonceTrialsPerByte, is(1000L));
|
||||||
|
assertThat(item.extraBytes, is(1000L));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = RuntimeException.class)
|
||||||
|
public void ensureRetrievingNonexistingItemThrowsException() {
|
||||||
|
repo.getItem(new byte[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void ensureItemCanBeDeleted() {
|
||||||
|
byte[] initialHash = repo.getItems().get(0);
|
||||||
|
repo.removeObject(initialHash);
|
||||||
|
assertTrue(repo.getItems().isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void ensureDeletionOfNonexistingItemIsHandledSilently() {
|
||||||
|
repo.removeObject(new byte[0]);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user