From 20d2b3e68cbae24e05e2deca8dcbe870ce77850a Mon Sep 17 00:00:00 2001 From: Vladimir Eliezer Tokarev Date: Thu, 25 Feb 2016 21:45:52 -0800 Subject: [PATCH] Ended the refactoring of the code acording to Arbel Peled first CR commit --- .../src/main/java/meerkat/SimpleRegistry.java | 31 ++------------ .../java/util/CollectionMessagesUtils.java | 41 ++++++++++++++++++- .../src/test/java/SimpleRegistryTest.java | 33 +++++++-------- 3 files changed, 59 insertions(+), 46 deletions(-) diff --git a/voter-registry/src/main/java/meerkat/SimpleRegistry.java b/voter-registry/src/main/java/meerkat/SimpleRegistry.java index 40959c8..c9fd287 100644 --- a/voter-registry/src/main/java/meerkat/SimpleRegistry.java +++ b/voter-registry/src/main/java/meerkat/SimpleRegistry.java @@ -2,13 +2,11 @@ package meerkat; import meerkat.bulletinboard.BulletinBoardClient; import meerkat.crypto.DigitalSignature; -import meerkat.protobuf.BulletinBoardAPI; import meerkat.protobuf.BulletinBoardAPI.BulletinBoardMessage; -import meerkat.protobuf.BulletinBoardAPI.MessageFilter; -import meerkat.protobuf.BulletinBoardAPI.MessageFilterList; import meerkat.protobuf.BulletinBoardAPI.UnsignedBulletinBoardMessage; import meerkat.protobuf.Crypto; import util.AccurateTimestamp; +import util.CollectionMessagesUtils; import util.RegistryTags; import java.security.SignatureException; @@ -55,29 +53,6 @@ public class SimpleRegistry implements VoterRegistry{ } } - /** - * Gets messages that have the wanted id tag and have or the ADD_TO_GROUP_TAG or REMOVE_FROM_GROUP_TAG - * - * @param tags the tags based on which the messages will be filtered - * @return MessageFilterList. - */ - public MessageFilterList GetRelevantMessagesFilters(List tags) { - MessageFilterList.Builder filters = MessageFilterList.newBuilder(); - - if (tags == null){ - return filters.build(); - } - - for (String tag : tags) { - MessageFilter.Builder filter = - MessageFilter.newBuilder().setTag(tag) - .setType(BulletinBoardAPI.FilterType.TAG); - - filters.addFilter(filter); - } - return filters.build(); - } - /** * Tries to post basicMessage and return true if its been successfully posted * @@ -125,7 +100,7 @@ public class SimpleRegistry implements VoterRegistry{ List GroupsActionsTags = new ArrayList() {{ add(RegistryTags.GROUP_ID_TAG + groupID.getId()); }}; - bulletinBoardClient.readMessages(GetRelevantMessagesFilters(GroupsActionsTags), + bulletinBoardClient.readMessages(CollectionMessagesUtils.GetRelevantMessagesFilters(GroupsActionsTags), new RelevantDataCallBack(callback)); } @@ -134,7 +109,7 @@ public class SimpleRegistry implements VoterRegistry{ add(RegistryTags.ID_TAG + voterID.getId()); add(RegistryTags.VOTER_ENTRY_TAG.toString()); }}; - bulletinBoardClient.readMessages(GetRelevantMessagesFilters(GroupsActionsTags), + bulletinBoardClient.readMessages(CollectionMessagesUtils.GetRelevantMessagesFilters(GroupsActionsTags), new RelevantDataCallBack(callback)); } } diff --git a/voter-registry/src/main/java/util/CollectionMessagesUtils.java b/voter-registry/src/main/java/util/CollectionMessagesUtils.java index fc71bac..24c1c87 100644 --- a/voter-registry/src/main/java/util/CollectionMessagesUtils.java +++ b/voter-registry/src/main/java/util/CollectionMessagesUtils.java @@ -10,12 +10,20 @@ import java.util.List; import java.util.Map; import java.util.stream.Collectors; + + /** * Created by Vladimir Eliezer Tokarev on 1/15/2016. * adds extra functionality to Messages collections */ public abstract class CollectionMessagesUtils { + static class EmptyListException extends Exception { + public EmptyListException(String message) { + super(message); + } + } + /** * Converts lost of UnsignedBulletinBoardMessage to VoterRegistryMessages * @param messages list @@ -65,9 +73,16 @@ public abstract class CollectionMessagesUtils { return groupsIds; } - public static VoterRegistryMessage GetLatestMessage(List messages) throws ParseException { + /** + * Gets the message with the latest timestamp from messages + * @param messages List + * @return VoterRegistryMessage + * @throws ParseException + * @throws EmptyListException + */ + public static VoterRegistryMessage GetLatestMessage(List messages) throws ParseException, EmptyListException { if (messages.size() == 0 ){ - return null; + throw new EmptyListException("The list of messages passed to GetLatestMessage is empty."); } VoterRegistryMessage LatestMessage = messages.get(0); @@ -79,4 +94,26 @@ public abstract class CollectionMessagesUtils { return LatestMessage; } + /** + * Gets messages that have the wanted id tag and have or the ADD_TO_GROUP_TAG or REMOVE_FROM_GROUP_TAG + * + * @param tags the tags based on which the messages will be filtered + * @return MessageFilterList. + */ + public static BulletinBoardAPI.MessageFilterList GetRelevantMessagesFilters(List tags) { + BulletinBoardAPI.MessageFilterList.Builder filters = BulletinBoardAPI.MessageFilterList.newBuilder(); + + if (tags.isEmpty()){ + return filters.build(); + } + + for (String tag : tags) { + BulletinBoardAPI.MessageFilter.Builder filter = + BulletinBoardAPI.MessageFilter.newBuilder().setTag(tag) + .setType(BulletinBoardAPI.FilterType.TAG); + + filters.addFilter(filter); + } + return filters.build(); + } } diff --git a/voter-registry/src/test/java/SimpleRegistryTest.java b/voter-registry/src/test/java/SimpleRegistryTest.java index c90e56a..e2fa751 100644 --- a/voter-registry/src/test/java/SimpleRegistryTest.java +++ b/voter-registry/src/test/java/SimpleRegistryTest.java @@ -9,6 +9,7 @@ import meerkat.bulletinboard.ThreadedBulletinBoardClient; import meerkat.crypto.concrete.ECDSASignature; import meerkat.protobuf.BulletinBoardAPI; import meerkat.protobuf.Voting; +import util.CollectionMessagesUtils; import util.RegistryTags; import java.io.InputStream; @@ -22,7 +23,7 @@ import java.util.concurrent.Semaphore; import static util.CollectionMessagesUtils.ConvertToVoterRegistryMessages; /** - * TODO: add logs prints for the tests to be clear what they are + * TODO: add logs prints for the tests to be clear what they are */ @@ -35,7 +36,7 @@ import static util.CollectionMessagesUtils.ConvertToVoterRegistryMessages; public class SimpleRegistryTest extends TestCase { private ECDSASignature signer; - private BulletinBoardClient communicator; + private BulletinBoardClient bulletinBoardClient; private SecureRandom random = new SecureRandom(); public static String KEYFILE_EXAMPLE = "/certs/enduser-certs/user1-key-with-password-secret.p12"; public static String KEYFILE_PASSWORD = "secret"; @@ -80,9 +81,9 @@ public class SimpleRegistryTest extends TestCase { * Creates the communication object (the bulletinBoardClient) */ private void CommunicatorSetup() { - communicator = new ThreadedBulletinBoardClient(); + bulletinBoardClient = new ThreadedBulletinBoardClient(); String BULLETIN_BOARD_SERVER_ADDRESS = "http://localhost:8081"; - communicator.init(Voting.BulletinBoardClientParams.newBuilder() + bulletinBoardClient.init(Voting.BulletinBoardClientParams.newBuilder() .addBulletinBoardAddress(BULLETIN_BOARD_SERVER_ADDRESS) .setMinRedundancy((float) 1.0) .build()); @@ -120,7 +121,7 @@ public class SimpleRegistryTest extends TestCase { */ public void testSimpleRegistryCreation() { try { - new SimpleRegistry(signer, communicator); + new SimpleRegistry(signer, bulletinBoardClient); } catch (Exception e) { assert false : "While creating the SimpleRegistry exception have been thrown " + e; } @@ -165,16 +166,16 @@ public class SimpleRegistryTest extends TestCase { RegistryMessages.VoterInfo voterInfo = RegistryMessages.VoterInfo.newBuilder(). setId(RegistryMessages.VoterID.newBuilder().setId(id)).setInfo(data).build(); - SimpleRegistry registry = new SimpleRegistry(signer, communicator); + SimpleRegistry registry = new SimpleRegistry(signer, bulletinBoardClient); registry.AddVoter(voterInfo, handler); jobSemaphore.acquire(); assertEquals(1, handler.counter ); List tags = new ArrayList(){{ add(RegistryTags.VOTER_ENTRY_TAG.toString());}}; - BulletinBoardAPI.MessageFilterList filters = registry.GetRelevantMessagesFilters(tags); + BulletinBoardAPI.MessageFilterList filters = CollectionMessagesUtils.GetRelevantMessagesFilters(tags); DummyBulletinBoardCallBackHandler bulletinHandler = new DummyBulletinBoardCallBackHandler(); - communicator.readMessages(filters, bulletinHandler); + bulletinBoardClient.readMessages(filters, bulletinHandler); jobSemaphore.acquire(); @@ -193,16 +194,16 @@ public class SimpleRegistryTest extends TestCase { String id = new BigInteger(130, random).toString(32); RegistryMessages.VoterID voterInfo = RegistryMessages.VoterID.newBuilder().setId(id).build(); - SimpleRegistry registry = new SimpleRegistry(signer, communicator); + SimpleRegistry registry = new SimpleRegistry(signer, bulletinBoardClient); registry.SetVoted(voterInfo, handler); jobSemaphore.acquire(); assertEquals(1, handler.counter ); List tags = new ArrayList(){{ add(RegistryTags.VOTE_ACTION_TAG.toString());}}; - BulletinBoardAPI.MessageFilterList filters = registry.GetRelevantMessagesFilters(tags); + BulletinBoardAPI.MessageFilterList filters = CollectionMessagesUtils.GetRelevantMessagesFilters(tags); DummyBulletinBoardCallBackHandler bulletinHandler = new DummyBulletinBoardCallBackHandler(); - communicator.readMessages(filters, bulletinHandler); + bulletinBoardClient.readMessages(filters, bulletinHandler); jobSemaphore.acquire(); @@ -223,7 +224,7 @@ public class SimpleRegistryTest extends TestCase { .setVoterId(RegistryMessages.VoterID.newBuilder().setId(voterId)) .setGroupId(RegistryMessages.GroupID.newBuilder().setId(groupId)).build(); - SimpleRegistry registry = new SimpleRegistry(signer, communicator); + SimpleRegistry registry = new SimpleRegistry(signer, bulletinBoardClient); registry.AddToGroup(voterInfo, handler); jobSemaphore.acquire(); @@ -231,9 +232,9 @@ public class SimpleRegistryTest extends TestCase { List tags = new ArrayList(){{ add(RegistryTags.GROUP_ACTION_TAG .toString() + RegistryTags.ADD_TO_GROUP_TAG.toString());}}; - BulletinBoardAPI.MessageFilterList filters = registry.GetRelevantMessagesFilters(tags); + BulletinBoardAPI.MessageFilterList filters = CollectionMessagesUtils.GetRelevantMessagesFilters(tags); DummyBulletinBoardCallBackHandler bulletinHandler = new DummyBulletinBoardCallBackHandler(); - communicator.readMessages(filters, bulletinHandler); + bulletinBoardClient.readMessages(filters, bulletinHandler); jobSemaphore.acquire(); @@ -257,7 +258,7 @@ public class SimpleRegistryTest extends TestCase { .setVoterId(RegistryMessages.VoterID.newBuilder().setId(voterId)) .setGroupId(RegistryMessages.GroupID.newBuilder().setId(groupId)).build(); - SimpleRegistry registry = new SimpleRegistry(signer, communicator); + SimpleRegistry registry = new SimpleRegistry(signer, bulletinBoardClient); registry.AddToGroup(voterInfo, handler); jobSemaphore.acquire(); @@ -284,7 +285,7 @@ public class SimpleRegistryTest extends TestCase { RegistryMessages.VoterInfo voterInfo = RegistryMessages.VoterInfo.newBuilder(). setId(RegistryMessages.VoterID.newBuilder().setId(id)).setInfo(data).build(); - SimpleRegistry registry = new SimpleRegistry(signer, communicator); + SimpleRegistry registry = new SimpleRegistry(signer, bulletinBoardClient); registry.AddVoter(voterInfo, handler); jobSemaphore.acquire();