From c767d8af58899ac43118177a512b42a23dab1f01 Mon Sep 17 00:00:00 2001 From: Vladimir Eliezer Tokarev Date: Fri, 18 Mar 2016 10:42:07 -0700 Subject: [PATCH] Changed the writing of all the groups ids of one simple voter. --- .../src/main/java/meerkat/AsyncRegistry.java | 37 ++++++++------- .../src/main/java/meerkat/VoterRegistry.java | 15 +++--- .../GetGroupsCallback.java | 37 +++++++++++++-- .../HasVotedCallback.java | 10 ++-- .../getVoterCallback.java | 21 +++++---- .../src/test/java/SimpleRegistryTest.java | 47 ++++++++++++------- 6 files changed, 107 insertions(+), 60 deletions(-) diff --git a/voter-registry/src/main/java/meerkat/AsyncRegistry.java b/voter-registry/src/main/java/meerkat/AsyncRegistry.java index aebe79c..3dad76d 100644 --- a/voter-registry/src/main/java/meerkat/AsyncRegistry.java +++ b/voter-registry/src/main/java/meerkat/AsyncRegistry.java @@ -1,10 +1,10 @@ package meerkat; import com.google.common.util.concurrent.FutureCallback; +import com.google.protobuf.ByteString; import meerkat.bulletinboard.AsyncBulletinBoardClient; import meerkat.crypto.DigitalSignature; import meerkat.protobuf.BulletinBoardAPI.UnsignedBulletinBoardMessage; -import meerkat.protobuf.VoterRegistry.GroupID; import meerkat.protobuf.VoterRegistry.VoterID; import meerkat.protobuf.VoterRegistry.VoterInfo; import meerkat.protobuf.VoterRegistry.VoterRegistryMessage; @@ -15,6 +15,9 @@ import meerkat.registry.MessageCollectionUtils; import meerkat.registry.RegistryTags; import meerkat.util.BulletinBoardUtils; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.ObjectOutputStream; import java.security.SignatureException; import java.util.ArrayList; import java.util.Collection; @@ -42,12 +45,12 @@ public class AsyncRegistry implements VoterRegistry{ @Override - public void addVoter(VoterInfo voterInfo, FutureCallback callback) throws SignatureException { + public void addVoter(VoterInfo voterInfo, FutureCallback callback) throws SignatureException { UnsignedBulletinBoardMessage basicMessage = UnsignedBulletinBoardMessage.newBuilder() .addTag(RegistryTags.ID_TAG + voterInfo.getId().getId()) .addTag(RegistryTags.VOTER_ENTRY_TAG) - .setData(voterInfo.getInfoBytes()) + .setData(voterInfo.toByteString()) .setTimestamp(BulletinBoardUtils.getCurrentTimestampProto()) .build(); @@ -55,24 +58,23 @@ public class AsyncRegistry implements VoterRegistry{ } @Override - public void setVoterGroups(VoterRegistryMessage voterGroup, FutureCallback callback) throws SignatureException { + public void setVoterGroups(VoterRegistryMessage voterRegistryMessage, FutureCallback callback) throws SignatureException, IOException { + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + ObjectOutputStream oos = new ObjectOutputStream(bos); + oos.writeObject(voterRegistryMessage.getGroupIDList()); + UnsignedBulletinBoardMessage.Builder basicMessage = UnsignedBulletinBoardMessage.newBuilder() - .addTag(RegistryTags.ID_TAG + voterGroup.getVoterID().getId()) + .addTag(RegistryTags.ID_TAG + voterRegistryMessage.getVoterID().getId()) .addTag(RegistryTags.ADD_TO_GROUP_TAG) + .setData(ByteString.copyFrom(bos.toByteArray())) .setTimestamp(BulletinBoardUtils.getCurrentTimestampProto()); - for (GroupID groupId : voterGroup.getGroupIDList()) - { - basicMessage.addTag(RegistryTags.GROUP_ID_TAG + groupId.getId()); - } - bulletinBoardClient.postMessage(signBulletinBoardMessage(basicMessage.build(), signers), callback); - } @Override - public void setVoted(VoterID voterId, FutureCallback callback) throws SignatureException { + public void setVoted(VoterID voterId, FutureCallback callback) throws SignatureException { UnsignedBulletinBoardMessage basicMessage = UnsignedBulletinBoardMessage.newBuilder() .addTag(RegistryTags.ID_TAG + voterId.getId()) @@ -84,9 +86,10 @@ public class AsyncRegistry implements VoterRegistry{ } @Override - public void getGroups(VoterID voterID, FutureCallback> callback) { + public void getGroups(VoterID voterID, FutureCallback> callback) { List GroupsActionsTags = new ArrayList(2) {{ add(RegistryTags.ADD_TO_GROUP_TAG); + add(RegistryTags.ID_TAG + voterID.getId()); }}; bulletinBoardClient.readMessages(MessageCollectionUtils.generateFiltersFromTags(GroupsActionsTags), @@ -94,8 +97,8 @@ public class AsyncRegistry implements VoterRegistry{ } @Override - public void getVoter(VoterID voterID, FutureCallback callback) { - List addVoterTags = new ArrayList() {{ + public void getVoter(VoterID voterID, FutureCallback callback) { + List addVoterTags = new ArrayList(2) {{ add(RegistryTags.ID_TAG + voterID.getId()); add(RegistryTags.VOTER_ENTRY_TAG); }}; @@ -105,8 +108,8 @@ public class AsyncRegistry implements VoterRegistry{ } @Override - public void hasVoted(VoterID voterId, FutureCallback callBack) { - List setVotedTags = new ArrayList() {{ + public void hasVoted(VoterID voterId, FutureCallback callBack) { + List setVotedTags = new ArrayList(2) {{ add(RegistryTags.ID_TAG + voterId.getId()); add(RegistryTags.VOTE_ACTION_TAG); }}; diff --git a/voter-registry/src/main/java/meerkat/VoterRegistry.java b/voter-registry/src/main/java/meerkat/VoterRegistry.java index b63fc12..e5e1b3a 100644 --- a/voter-registry/src/main/java/meerkat/VoterRegistry.java +++ b/voter-registry/src/main/java/meerkat/VoterRegistry.java @@ -7,6 +7,7 @@ import meerkat.protobuf.VoterRegistry.VoterID; import meerkat.protobuf.VoterRegistry.VoterInfo; import meerkat.protobuf.VoterRegistry.VoterRegistryMessage; +import java.io.IOException; import java.security.SignatureException; import java.util.Collection; import java.util.List; @@ -40,11 +41,12 @@ public interface VoterRegistry { * Passes the group to callBack.handleResult if the actions succeeded else * call the onFailure method of the callback with the arisen error * - * @param voterGroup protobuff object that is coupling of voterId to groupId + * @param voterRegistryMessage protobuff object that is coupling of voterId to groupId * @param callBack when the adding voter done callBack.handleResult will be called * @throws SignatureException + * @throws IOException */ - void setVoterGroups(VoterRegistryMessage voterGroup, FutureCallback callBack) throws SignatureException; + void setVoterGroups(VoterRegistryMessage voterRegistryMessage, FutureCallback callBack) throws SignatureException, IOException; /** * Sets that the voter have voted @@ -64,9 +66,8 @@ public interface VoterRegistry { * * @param voterID protobuff object that represent the voter * @param callBack when the adding voter done callBack.handleResult will be called - * @throws SignatureException */ - void getGroups(VoterID voterID, FutureCallback> callBack) throws SignatureException; + void getGroups(VoterID voterID, FutureCallback> callBack); /** * Retrieves VoterInfo protobuff that represents voter @@ -75,9 +76,8 @@ public interface VoterRegistry { * * @param voterID protobuff object that represent the voter * @param callBack when the adding voter done callBack.handleResult will be called - * @throws SignatureException */ - void getVoter(VoterID voterID, FutureCallback callBack) throws SignatureException; + void getVoter(VoterID voterID, FutureCallback callBack); /** * Checks if the given voter (by his id) have already voted @@ -86,8 +86,7 @@ public interface VoterRegistry { * * @param voterID protobuff object that represent the voter * @param callBack method that will be called with the when the result will be found - * @throws SignatureException */ - void hasVoted(VoterID voterID, FutureCallback callBack) throws SignatureException; + void hasVoted(VoterID voterID, FutureCallback callBack); } diff --git a/voter-registry/src/main/java/meerkat/registry/AsyncRegistryCallbacks/GetGroupsCallback.java b/voter-registry/src/main/java/meerkat/registry/AsyncRegistryCallbacks/GetGroupsCallback.java index 9acd17b..1547d3e 100644 --- a/voter-registry/src/main/java/meerkat/registry/AsyncRegistryCallbacks/GetGroupsCallback.java +++ b/voter-registry/src/main/java/meerkat/registry/AsyncRegistryCallbacks/GetGroupsCallback.java @@ -1,12 +1,16 @@ package meerkat.registry.AsyncRegistryCallbacks; import com.google.common.util.concurrent.FutureCallback; +import javassist.NotFoundException; import meerkat.protobuf.BulletinBoardAPI.BulletinBoardMessage; -import meerkat.registry.RegistryTags; +import meerkat.protobuf.VoterRegistry; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.ObjectInputStream; import java.util.List; -import static meerkat.util.BulletinBoardUtils.GetListOfTags; +import static meerkat.util.BulletinBoardUtils.getLatestMessage; // TODO : add logging @@ -15,19 +19,42 @@ import static meerkat.util.BulletinBoardUtils.GetListOfTags; * AsyncRegistry getGroups creates this callback to list of groups the voter in */ public class GetGroupsCallback implements FutureCallback> { - protected FutureCallback> callback; + protected FutureCallback> callback; - public GetGroupsCallback(FutureCallback> callback){ + public GetGroupsCallback(FutureCallback> callback){ this.callback = callback; } + /** + * Gets List of GroupId objects from given message + * @param message BulletinBoardMessage which contains all groups ids of voter + * @return list of GroupId + * @throws IOException + * @throws ClassNotFoundException + */ + private List getVoterGroups(BulletinBoardMessage message) throws IOException, ClassNotFoundException { + ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(message.getMsg().getData().toByteArray())); + return (List)ois.readObject(); + } + + @Override public void onSuccess(List messages) { - callback.onSuccess(GetListOfTags(messages, RegistryTags.GROUP_ID_TAG)); + if (messages.isEmpty()){ + callback.onFailure(new NotFoundException("Voter permissions not found in database")); + } + + BulletinBoardMessage message = getLatestMessage(messages); + try { + callback.onSuccess(getVoterGroups(message)); + } catch (IOException | ClassNotFoundException e) { + callback.onFailure(e); + } } @Override public void onFailure(Throwable t) { + System.out.println(t); this.callback.onFailure(t); } } diff --git a/voter-registry/src/main/java/meerkat/registry/AsyncRegistryCallbacks/HasVotedCallback.java b/voter-registry/src/main/java/meerkat/registry/AsyncRegistryCallbacks/HasVotedCallback.java index b768a21..73570a5 100644 --- a/voter-registry/src/main/java/meerkat/registry/AsyncRegistryCallbacks/HasVotedCallback.java +++ b/voter-registry/src/main/java/meerkat/registry/AsyncRegistryCallbacks/HasVotedCallback.java @@ -2,13 +2,9 @@ package meerkat.registry.AsyncRegistryCallbacks; import com.google.common.util.concurrent.FutureCallback; import meerkat.protobuf.BulletinBoardAPI.BulletinBoardMessage; -import meerkat.registry.RegistryTags; import java.util.List; -import static meerkat.util.BulletinBoardUtils.findTagWithPrefix; -import static meerkat.util.BulletinBoardUtils.getLatestMessage; - // TODO : add logging /** @@ -24,8 +20,10 @@ public class HasVotedCallback implements FutureCallback messages) { - BulletinBoardMessage message = getLatestMessage(messages); - this.callback.onSuccess(findTagWithPrefix(message, RegistryTags.VOTE_ACTION_TAG) != null); + if (messages.isEmpty()){ + callback.onSuccess(false); + } + callback.onSuccess(true); } @Override diff --git a/voter-registry/src/main/java/meerkat/registry/AsyncRegistryCallbacks/getVoterCallback.java b/voter-registry/src/main/java/meerkat/registry/AsyncRegistryCallbacks/getVoterCallback.java index b1b8166..b7b503a 100644 --- a/voter-registry/src/main/java/meerkat/registry/AsyncRegistryCallbacks/getVoterCallback.java +++ b/voter-registry/src/main/java/meerkat/registry/AsyncRegistryCallbacks/getVoterCallback.java @@ -1,13 +1,13 @@ package meerkat.registry.AsyncRegistryCallbacks; import com.google.common.util.concurrent.FutureCallback; +import com.google.protobuf.InvalidProtocolBufferException; +import javassist.NotFoundException; import meerkat.protobuf.BulletinBoardAPI.BulletinBoardMessage; import meerkat.protobuf.VoterRegistry; -import meerkat.registry.RegistryTags; import java.util.List; -import static meerkat.util.BulletinBoardUtils.findTagWithPrefix; import static meerkat.util.BulletinBoardUtils.getLatestMessage; // TODO : add logging @@ -25,14 +25,19 @@ public class GetVoterCallback implements FutureCallback messages) { + if(messages.isEmpty()){ + callback.onFailure(new NotFoundException("Voter info not found in database.")); + } BulletinBoardMessage latestMessage = getLatestMessage(messages); - VoterRegistry.VoterInfo info = VoterRegistry.VoterInfo.newBuilder() - .setId(VoterRegistry.VoterID.newBuilder() - .setId(findTagWithPrefix(latestMessage, RegistryTags.ID_TAG))) - .setInfo(latestMessage.getMsg().getData().toStringUtf8()).build(); - - callback.onSuccess(info); + try { + VoterRegistry.VoterInfo info = VoterRegistry.VoterInfo.newBuilder() + .mergeFrom(latestMessage.getMsg().getData()).build(); + callback.onSuccess(info); + } catch (InvalidProtocolBufferException e) { + e.printStackTrace(); + callback.onFailure(e); + } } @Override diff --git a/voter-registry/src/test/java/SimpleRegistryTest.java b/voter-registry/src/test/java/SimpleRegistryTest.java index 00c7e6b..0a25aca 100644 --- a/voter-registry/src/test/java/SimpleRegistryTest.java +++ b/voter-registry/src/test/java/SimpleRegistryTest.java @@ -16,7 +16,10 @@ import meerkat.registry.RegistryTags; import org.junit.Before; import org.junit.Test; +import java.io.ByteArrayInputStream; +import java.io.IOException; import java.io.InputStream; +import java.io.ObjectInputStream; import java.math.BigInteger; import java.security.KeyStore; import java.security.SecureRandom; @@ -29,6 +32,7 @@ import java.util.concurrent.Semaphore; import static junit.framework.TestCase.assertEquals; import static junit.framework.TestCase.assertTrue; import static meerkat.util.BulletinBoardUtils.findTagWithPrefix; +import static meerkat.util.BulletinBoardUtils.getLatestMessage; /** * TODO: add logs prints for the tests to be clear what they are @@ -258,14 +262,14 @@ public class SimpleRegistryTest /**extends TestCase**/ { * Test that get groups retrieves the right groups the user are in */ @Test - public void testAddToGroup() throws InterruptedException, SignatureException { + public void testSetVoterGroups() throws InterruptedException, SignatureException, IOException, ClassNotFoundException { DummyRegistryCallBackHandler handler = new DummyRegistryCallBackHandler<>(); String voterId = generateString(); - String groupId = generateString(); + String groupId1 = generateString(); VoterRegistry.VoterRegistryMessage voterInfo = VoterRegistry.VoterRegistryMessage.newBuilder() .setVoterID(VoterID.newBuilder().setId(voterId)) - .addGroupID(GroupID.newBuilder().setId(groupId)).build(); + .addGroupID(GroupID.newBuilder().setId(groupId1)).build(); AsyncRegistry registry = GetRegistry(); registry.setVoterGroups(voterInfo, handler); @@ -273,32 +277,38 @@ public class SimpleRegistryTest /**extends TestCase**/ { jobSemaphore.acquire(); assertEquals("The callback handler hasn't been called yet", 1, handler.counter); - List tags = new ArrayList(){{add(RegistryTags.ADD_TO_GROUP_TAG);}}; + List tags = new ArrayList(){{add(RegistryTags.ADD_TO_GROUP_TAG); + add(RegistryTags.ID_TAG + voterId);}}; DummyBulletinBoardCallBackHandler bulletinHandler = readMessagesByTags(tags); jobSemaphore.acquire(); - tags.clear(); - tags.add(RegistryTags.ID_TAG + voterId); - tags.add(RegistryTags.GROUP_ID_TAG + groupId); + BulletinBoardMessage latestMessage = getLatestMessage(bulletinHandler.messages); - int counter = countMessagesWithTags(bulletinHandler.messages, tags); - assert counter == 1 : "The server don't have the new user added to group."; + assert findTagWithPrefix(latestMessage, RegistryTags.ID_TAG).equals(voterId) : + "The latest message recieved is not of our voter"; + + ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(latestMessage.getMsg().getData().toByteArray())); + List groupsIds = (List)ois.readObject(); + + assert groupsIds.get(0).getId().equals(groupId1) : "The latest message doesn't have the voter group"; } /** * Test that remove from group creates correct bulletin board message and removes the user from a group */ @Test - public void testGetGroups() throws InterruptedException, SignatureException { + public void testGetGroups() throws InterruptedException, SignatureException, IOException { DummyRegistryCallBackHandler handler = new DummyRegistryCallBackHandler<>(); String voterId = generateString(); - String groupId = generateString(); + String groupId1 = generateString(); + String groupId2 = generateString(); VoterRegistry.VoterRegistryMessage voterInfo = VoterRegistry.VoterRegistryMessage.newBuilder() .setVoterID(VoterID.newBuilder().setId(voterId)) - .addGroupID(GroupID.newBuilder().setId(groupId)).build(); + .addGroupID(GroupID.newBuilder().setId(groupId1)) + .addGroupID(GroupID.newBuilder().setId(groupId2)).build(); this.certStream = getClass().getResourceAsStream(CERT1_PEM_EXAMPLE); @@ -308,12 +318,17 @@ public class SimpleRegistryTest /**extends TestCase**/ { jobSemaphore.acquire(); assertEquals("The callback handler hasn't been called yet", 1, handler.counter ); - DummyRegistryCallBackHandler> groupsHandler = new DummyRegistryCallBackHandler<>(); - registry.getGroups(VoterID.newBuilder().setId(groupId).build(), groupsHandler); + DummyRegistryCallBackHandler> groupsHandler = new DummyRegistryCallBackHandler<>(); + registry.getGroups(VoterID.newBuilder().setId(voterId).build(), groupsHandler); jobSemaphore.acquire(1); - List userGroups = groupsHandler.data; - assert userGroups.contains(groupId) : "The simple voter registry object does not retrieved right user groups"; + List userGroups = groupsHandler.data; + + assert userGroups.contains(GroupID.newBuilder().setId(groupId1).build()) : + "The simple voter registry object does not retrieved the first user groups"; + + assert userGroups.contains(GroupID.newBuilder().setId(groupId2).build()) : + "The simple voter registry object does not retrieved the second user groups"; } /**