diff --git a/voter-registry/src/main/java/meerkat/MessageValidator.java b/voter-registry/src/main/java/meerkat/MessageValidator.java deleted file mode 100644 index 1f5c38c..0000000 --- a/voter-registry/src/main/java/meerkat/MessageValidator.java +++ /dev/null @@ -1,23 +0,0 @@ -package meerkat; - -import java.util.List; - -/** - * Provides the validation option for objects - * @param - */ -public interface MessageValidator > -{ - /** - * Throws ValidationError when no part of the object is valid else return the valid part - * @param object object which will be checked - * @return T object - */ - T validate(T object) throws ValidationError; - - class ValidationError extends Exception { - public ValidationError(String message) { - super(message); - } - } -} diff --git a/voter-registry/src/main/java/meerkat/Registry.java b/voter-registry/src/main/java/meerkat/Registry.java index 9f363a0..e631675 100644 --- a/voter-registry/src/main/java/meerkat/Registry.java +++ b/voter-registry/src/main/java/meerkat/Registry.java @@ -121,22 +121,28 @@ public class Registry implements VoterRegistry{ }}; bulletinBoardClient.readMessages(MessageCollectionUtils.generateFiltersFromTags(GroupsActionsTags), - new LatestMessagesCallBack(callback, signers)); + new LatestMessagesCallBack(callback, signers, "getGroups")); } @Override public void getPersonIDDetails(VoterID voterID, RegistryCallBack callback) { - List GroupsActionsTags = new ArrayList() {{ + List addVoterTags = new ArrayList() {{ add(RegistryTags.ID_TAG + voterID.getId()); add(RegistryTags.VOTER_ENTRY_TAG); }}; - bulletinBoardClient.readMessages(MessageCollectionUtils.generateFiltersFromTags(GroupsActionsTags), - new LatestMessagesCallBack(callback, signers)); + bulletinBoardClient.readMessages(MessageCollectionUtils.generateFiltersFromTags(addVoterTags), + new LatestMessagesCallBack(callback, signers, "getPersonIDDetails")); } @Override - public boolean hasVoted(VoterID voterId, RegistryCallBack callBack) { - return false; + public void hasVoted(VoterID voterId, RegistryCallBack callBack) { + List setVotedTags = new ArrayList() {{ + add(RegistryTags.ID_TAG + voterId.getId()); + add(RegistryTags.VOTE_ACTION_TAG); + }}; + + bulletinBoardClient.readMessages(MessageCollectionUtils.generateFiltersFromTags(setVotedTags), + new LatestMessagesCallBack(callBack, signers, "hasVoted")); } } diff --git a/voter-registry/src/main/java/meerkat/VoterRegistry.java b/voter-registry/src/main/java/meerkat/VoterRegistry.java index c0f1f50..8e85db7 100644 --- a/voter-registry/src/main/java/meerkat/VoterRegistry.java +++ b/voter-registry/src/main/java/meerkat/VoterRegistry.java @@ -84,6 +84,6 @@ public interface VoterRegistry { * @param callBack method that will be called with the when the result will be found * @return true if voter had voted */ - boolean hasVoted(VoterID voterId, RegistryCallBack callBack); + void hasVoted(VoterID voterId, RegistryCallBack callBack); } diff --git a/voter-registry/src/main/java/meerkat/registry/LatestMessagesCallBack.java b/voter-registry/src/main/java/meerkat/registry/LatestMessagesCallBack.java index 597ef1f..dddc821 100644 --- a/voter-registry/src/main/java/meerkat/registry/LatestMessagesCallBack.java +++ b/voter-registry/src/main/java/meerkat/registry/LatestMessagesCallBack.java @@ -1,58 +1,42 @@ package meerkat.registry; import com.google.common.util.concurrent.FutureCallback; -import meerkat.MessageValidator; import meerkat.VoterRegistry.RegistryCallBack; import meerkat.crypto.DigitalSignature; import meerkat.protobuf.BulletinBoardAPI.BulletinBoardMessage; -import meerkat.protobuf.Crypto; import meerkat.util.TimestampComparator; -import java.security.InvalidKeyException; -import java.security.cert.CertificateException; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.List; import static meerkat.util.BulletinBoardUtils.GetListOfTags; +import static meerkat.util.BulletinBoardUtils.findTagWithPrefix; /** - * TODO : add logging and verification of the messages + * TODO : add logging and verification of the messages by Arbel Peled */ /** * Created by Vladimir Eliezer Tokarev on 2/19/2016. * Gets latest data from given List */ -public class LatestMessagesCallBack implements FutureCallback>, MessageValidator> { +public class LatestMessagesCallBack implements FutureCallback> { public RegistryCallBack callback; protected Collection validators; + protected String type; /** * init MessagesCallBack * @param callback voter registry callback object * @param validators DigitalSignature object */ - public LatestMessagesCallBack(RegistryCallBack callback, Collection validators) { + public LatestMessagesCallBack(RegistryCallBack callback, + Collection validators, String type) { this.callback = callback; this.validators = validators; - } - - /** - * Checks if the given list have tags of type GROUP_ID_TAG or else - * @param msg List - * @return true if the messages are with GROUP_ID_TAG tags - */ - private boolean isAddToGroupsList(List msg) { - List tags = msg.get(0).getMsg().getTagList(); - for (String tag : tags) { - if(tag.contains(RegistryTags.GROUP_ID_TAG)) - { - return true; - } - } - return false; + this.type = type; } /** @@ -70,12 +54,16 @@ public class LatestMessagesCallBack implements FutureCallback messages = new ArrayList(){{add(lastAddedMessage);}}; - - if (isAddToGroupsList(msg)) { - callback.handleResult(GetListOfTags(messages, RegistryTags.GROUP_ID_TAG)); - } - else { - callback.handleResult(lastAddedMessage); + switch (type){ + case "getGroups" : + callback.handleResult(GetListOfTags(messages, RegistryTags.GROUP_ID_TAG)); + break; + case "getPersonIDDetails" : callback.handleResult(lastAddedMessage); + break; + case "hasVoted" : + callback.handleResult(findTagWithPrefix(lastAddedMessage, + RegistryTags.VOTE_ACTION_TAG) != null); + break; } } @@ -87,51 +75,4 @@ public class LatestMessagesCallBack implements FutureCallback - * @throws ValidationError - */ - @Override - public List validate(List object) throws ValidationError { - List verifiedMessages = new ArrayList<>(object.size()); - for (BulletinBoardMessage message : object) - { - if(verifyMessage(message)) - { - verifiedMessages.add(message); - } - } - return verifiedMessages; - } } diff --git a/voter-registry/src/test/java/SimpleRegistryTest.java b/voter-registry/src/test/java/SimpleRegistryTest.java index 59631e4..dff3380 100644 --- a/voter-registry/src/test/java/SimpleRegistryTest.java +++ b/voter-registry/src/test/java/SimpleRegistryTest.java @@ -1,5 +1,4 @@ import com.google.common.util.concurrent.FutureCallback; -import com.google.protobuf.InvalidProtocolBufferException; import junit.framework.TestCase; import meerkat.Registry; import meerkat.bulletinboard.AsyncBulletinBoardClient; @@ -15,6 +14,7 @@ import meerkat.protobuf.VoterRegistry.VoterInfo; import meerkat.protobuf.Voting; import meerkat.registry.MessageCollectionUtils; import meerkat.registry.RegistryTags; +import org.junit.Test; import java.io.InputStream; import java.math.BigInteger; @@ -67,6 +67,7 @@ public class SimpleRegistryTest extends TestCase { @Override public void handleFailure(Throwable throwable) { + System.out.print(throwable); } } @@ -137,10 +138,16 @@ public class SimpleRegistryTest extends TestCase { return registry; } + private String generateString() + { + return new BigInteger(130, random).toString(32); + } + /** * Checks if the creation of the registry have been successful */ - public void testSimpleRegistryCreation() { + @Test + public void simpleRegistryCreation() { try { Registry registry = GetRegistry(); } catch (Exception e) { @@ -175,14 +182,30 @@ public class SimpleRegistryTest extends TestCase { return counter; } + /** + * Reads messages from bulletinBoardClient by given tags and return the callback handler + * object + * + * @param tags list of strings that represents tags + * @return DummyBulletinBoardCallBackHandler which method will be called + */ + private DummyBulletinBoardCallBackHandler readMessagesByTags(List tags) + { + MessageFilterList filters = MessageCollectionUtils.generateFiltersFromTags(tags); + DummyBulletinBoardCallBackHandler bulletinHandler = new DummyBulletinBoardCallBackHandler(); + bulletinBoardClient.readMessages(filters, bulletinHandler); + return bulletinHandler; + } + /** * Test that add voter creates new correct bulletin board message and adds the voter */ - public void testAddVoter() throws InvalidProtocolBufferException, InterruptedException { + @Test + public void testAddVoter() throws InterruptedException { DummyRegistryCallBackHandler handler = new DummyRegistryCallBackHandler<>(); - String id = new BigInteger(130, random).toString(32); - String data = new BigInteger(130, random).toString(32); + String id = generateString(); + String data = generateString(); VoterInfo voterInfo = VoterInfo.newBuilder().setId(VoterID.newBuilder().setId(id)).setInfo(data).build(); Registry registry = GetRegistry(); @@ -192,9 +215,7 @@ public class SimpleRegistryTest extends TestCase { assertEquals(1, handler.counter ); List tags = new ArrayList(){{ add(RegistryTags.VOTER_ENTRY_TAG);}}; - MessageFilterList filters = MessageCollectionUtils.generateFiltersFromTags(tags); - DummyBulletinBoardCallBackHandler bulletinHandler = new DummyBulletinBoardCallBackHandler(); - bulletinBoardClient.readMessages(filters, bulletinHandler); + DummyBulletinBoardCallBackHandler bulletinHandler = readMessagesByTags(tags); jobSemaphore.acquire(); @@ -208,10 +229,11 @@ public class SimpleRegistryTest extends TestCase { /** * Test that set voted posts creates correct bulletin board message and sets that the user have been voted */ - public void testSetVoted() throws InvalidProtocolBufferException, InterruptedException { + @Test + public void testSetVoted() throws InterruptedException { DummyRegistryCallBackHandler handler = new DummyRegistryCallBackHandler<>(); - String id = new BigInteger(130, random).toString(32); + String id = generateString(); VoterID voterInfo = VoterID.newBuilder().setId(id).build(); Registry registry = GetRegistry(); @@ -221,9 +243,7 @@ public class SimpleRegistryTest extends TestCase { assertEquals(1, handler.counter ); List tags = new ArrayList(){{ add(RegistryTags.VOTE_ACTION_TAG);}}; - MessageFilterList filters = MessageCollectionUtils.generateFiltersFromTags(tags); - DummyBulletinBoardCallBackHandler bulletinHandler = new DummyBulletinBoardCallBackHandler(); - bulletinBoardClient.readMessages(filters, bulletinHandler); + DummyBulletinBoardCallBackHandler bulletinHandler = readMessagesByTags(tags); jobSemaphore.acquire(); @@ -237,11 +257,12 @@ public class SimpleRegistryTest extends TestCase { /** * Test that get groups retrieves the right groups the user are in */ - public void testAddToGroup() throws InvalidProtocolBufferException, InterruptedException { + @Test + public void testAddToGroup() throws InterruptedException { DummyRegistryCallBackHandler handler = new DummyRegistryCallBackHandler<>(); - String voterId = new BigInteger(130, random).toString(32); - String groupId = new BigInteger(130, random).toString(32); + String voterId = generateString(); + String groupId = generateString(); VoterRegistry.VoterRegistryMessage voterInfo = VoterRegistry.VoterRegistryMessage.newBuilder() .setVoterID(VoterID.newBuilder().setId(voterId)) .addGroupID(GroupID.newBuilder().setId(groupId)).build(); @@ -250,12 +271,10 @@ public class SimpleRegistryTest extends TestCase { registry.setVoterGroups(voterInfo, handler); jobSemaphore.acquire(); - assertEquals(1, handler.counter); + assertEquals("The callback handler hasn't been called yet", 1, handler.counter); List tags = new ArrayList(){{add(RegistryTags.ADD_TO_GROUP_TAG);}}; - MessageFilterList filters = MessageCollectionUtils.generateFiltersFromTags(tags); - DummyBulletinBoardCallBackHandler bulletinHandler = new DummyBulletinBoardCallBackHandler(); - bulletinBoardClient.readMessages(filters, bulletinHandler); + DummyBulletinBoardCallBackHandler bulletinHandler = readMessagesByTags(tags); jobSemaphore.acquire(); @@ -270,12 +289,13 @@ public class SimpleRegistryTest extends TestCase { /** * Test that remove from group creates correct bulletin board message and removes the user from a group */ - public void testGetGroups() throws InvalidProtocolBufferException, InterruptedException { + @Test + public void testGetGroups() throws InterruptedException { DummyRegistryCallBackHandler> handler = new DummyRegistryCallBackHandler<>(); - String voterId = new BigInteger(130, random).toString(32); - String groupId = new BigInteger(130, random).toString(32); + String voterId = generateString(); + String groupId = generateString(); VoterRegistry.VoterRegistryMessage voterInfo = VoterRegistry.VoterRegistryMessage.newBuilder() .setVoterID(VoterID.newBuilder().setId(voterId)) .addGroupID(GroupID.newBuilder().setId(groupId)).build(); @@ -286,7 +306,7 @@ public class SimpleRegistryTest extends TestCase { registry.setVoterGroups(voterInfo, handler); jobSemaphore.acquire(); - assertEquals(1, handler.counter ); + assertEquals("The callback handler hasn't been called yet", 1, handler.counter ); DummyRegistryCallBackHandler> groupsHandler = new DummyRegistryCallBackHandler<>(); registry.getGroups(GroupID.newBuilder().setId(groupId).build(), groupsHandler); @@ -299,12 +319,13 @@ public class SimpleRegistryTest extends TestCase { /** * Test that the personal data outputted about the user is right */ - public void testGetPersonalIDDetails() throws InvalidProtocolBufferException, InterruptedException { + @Test + public void testGetPersonalIDDetails() throws InterruptedException { DummyRegistryCallBackHandler> handler = new DummyRegistryCallBackHandler<>(); - String id = new BigInteger(130, random).toString(32); - String data = new BigInteger(130, random).toString(32); + String id = generateString(); + String data = generateString(); VoterInfo voterInfo = VoterInfo.newBuilder(). setId(VoterID.newBuilder().setId(id)).setInfo(data).build(); @@ -312,14 +333,34 @@ public class SimpleRegistryTest extends TestCase { registry.addVoter(voterInfo, handler); jobSemaphore.acquire(); - assertEquals(1, handler.counter ); + assertEquals("The callback handler hasn't been called yet", 1, handler.counter ); DummyRegistryCallBackHandler personalHandler = new DummyRegistryCallBackHandler<>(); registry.getPersonIDDetails(VoterID.newBuilder().setId(id).build(), personalHandler); jobSemaphore.acquire(1); - assertEquals(id, findTagWithPrefix(personalHandler.data, RegistryTags.ID_TAG)); - assertTrue(findTagWithPrefix(personalHandler.data, data) != null); + assertEquals("The voter id doesn't match the created on ", + id, findTagWithPrefix(personalHandler.data, RegistryTags.ID_TAG)); + assertTrue("The voter personal data can't be found.", + findTagWithPrefix(personalHandler.data, data) != null); } + + + public void testHasVoted () throws InterruptedException { + DummyRegistryCallBackHandler handler = new DummyRegistryCallBackHandler<>(); + String id = generateString(); + VoterID voterInfo = VoterID.newBuilder().setId(id).build(); + + Registry registry = GetRegistry(); + registry.setVoted(voterInfo, handler); + jobSemaphore.acquire(); + assertEquals("The callback handler hasn't been called yet", 1, handler.counter); + DummyRegistryCallBackHandler personalHandler = new DummyRegistryCallBackHandler<>(); + registry.hasVoted(VoterID.newBuilder().setId(id).build(), personalHandler); + + jobSemaphore.acquire(1); + assertTrue("The voter hasn't voted yet.", personalHandler.data); + } + }