diff --git a/voter-registry/src/main/java/meerkat/RelevantDataCallBack.java b/voter-registry/src/main/java/meerkat/RelevantDataCallBack.java index 9cfa276..b91428b 100644 --- a/voter-registry/src/main/java/meerkat/RelevantDataCallBack.java +++ b/voter-registry/src/main/java/meerkat/RelevantDataCallBack.java @@ -31,7 +31,13 @@ public class RelevantDataCallBack implements BulletinBoardClient.ClientCallback< * @return true if the messages are with GROUP_ID_TAG tags */ private boolean isAddToGroupsList(List msg) { - return msg.get(0).getMsg().getTagList().get(0).contains(RegistryTags.GROUP_ID_TAG.toString()); + for (String tag : msg.get(0).getMsg().getTagList()) { + if(tag.contains(RegistryTags.GROUP_ID_TAG.toString())) + { + return true; + } + } + return false; } /** @@ -48,7 +54,8 @@ public class RelevantDataCallBack implements BulletinBoardClient.ClientCallback< if(isAddToGroupsList(msg)){ try { Map map = GetLatestGroupsActions(messages); - callback.HandleResult(GetListOfGroupIds(map)); + List groupsOfUser = GetListOfGroupIds(map); + callback.HandleResult(groupsOfUser); } catch (ParseException e) { callback.HandleResult(null); } diff --git a/voter-registry/src/main/java/util/CollectionMessagesUtils.java b/voter-registry/src/main/java/util/CollectionMessagesUtils.java index 57b9d65..fc71bac 100644 --- a/voter-registry/src/main/java/util/CollectionMessagesUtils.java +++ b/voter-registry/src/main/java/util/CollectionMessagesUtils.java @@ -37,7 +37,7 @@ public abstract class CollectionMessagesUtils { // iterate trough all the messages and put into the map the last updated groups actions for (VoterRegistryMessage message : messages) { - String groupId = message.GetWantedTagFromBasicMessage(RegistryTags.GROUP_ID_TAG); + String groupId = message.GetWantedTagFromBasicMessage(RegistryTags.GROUP_ID_TAG.toString()); VoterRegistryMessage temp = groupIdToMessage.get(groupId); if (temp != null && temp != message) { @@ -59,7 +59,7 @@ public abstract class CollectionMessagesUtils { List groupsIds = new ArrayList<>(); groupIdToMessage.values().stream().filter(VoterRegistryMessage::IsGroupAdding).forEach(message -> { - String groupId = message.GetWantedTagFromBasicMessage(RegistryTags.GROUP_ID_TAG); + String groupId = message.GetWantedTagFromBasicMessage(RegistryTags.GROUP_ID_TAG.toString()); groupsIds.add(groupId); }); return groupsIds; diff --git a/voter-registry/src/test/java/SimpleRegistryTest.java b/voter-registry/src/test/java/SimpleRegistryTest.java index 2150c82..c90e56a 100644 --- a/voter-registry/src/test/java/SimpleRegistryTest.java +++ b/voter-registry/src/test/java/SimpleRegistryTest.java @@ -22,8 +22,7 @@ import java.util.concurrent.Semaphore; import static util.CollectionMessagesUtils.ConvertToVoterRegistryMessages; /** - * TODO: Have to do more relevant prints (print what happens) - * TODO: repair all the other methods + * TODO: add logs prints for the tests to be clear what they are */ @@ -70,10 +69,9 @@ public class SimpleRegistryTest extends TestCase { } @Override - public void handleFailure(Throwable t) throws Throwable { + public void handleFailure(Throwable t){ messages = null; jobSemaphore.release(); - throw t; } } @@ -142,9 +140,9 @@ public class SimpleRegistryTest extends TestCase { int wantedTagsCounter = 0; for (String tag : tags) { - if(message.GetWantedTagFromBasicMessage(tag)!=null){ - wantedTagsCounter++; - } + if(message.GetWantedTagFromBasicMessage(tag)!=null){ + wantedTagsCounter++; + } } if(wantedTagsCounter == tags.size()) @@ -162,16 +160,16 @@ public class SimpleRegistryTest extends TestCase { public void testAddVoter() throws InvalidProtocolBufferException, InterruptedException { DummyRegistryCallBackHandler handler = new DummyRegistryCallBackHandler<>(); + String id = new BigInteger(130, random).toString(32); + String data = new BigInteger(130, random).toString(32); RegistryMessages.VoterInfo voterInfo = RegistryMessages.VoterInfo.newBuilder(). - setId(RegistryMessages.VoterID.newBuilder() - .setId(new BigInteger(130, random).toString(32))) - .setInfo(new BigInteger(130, random).toString(32)).build(); + setId(RegistryMessages.VoterID.newBuilder().setId(id)).setInfo(data).build(); SimpleRegistry registry = new SimpleRegistry(signer, communicator); registry.AddVoter(voterInfo, handler); jobSemaphore.acquire(); - assertEquals(handler.counter , 1); + assertEquals(1, handler.counter ); List tags = new ArrayList(){{ add(RegistryTags.VOTER_ENTRY_TAG.toString());}}; BulletinBoardAPI.MessageFilterList filters = registry.GetRelevantMessagesFilters(tags); @@ -180,6 +178,8 @@ public class SimpleRegistryTest extends TestCase { jobSemaphore.acquire(); + tags.add(id); + int counter = countMessagesWithTags(ConvertToVoterRegistryMessages(bulletinHandler.messages), tags); assert counter == 1 : "The server don't have the new user data."; } @@ -190,15 +190,14 @@ public class SimpleRegistryTest extends TestCase { public void testSetVoted() throws InvalidProtocolBufferException, InterruptedException { DummyRegistryCallBackHandler handler = new DummyRegistryCallBackHandler<>(); - RegistryMessages.VoterID voterInfo = RegistryMessages.VoterID.newBuilder() - .setId(new BigInteger(130, random).toString(32)).build(); + String id = new BigInteger(130, random).toString(32); + RegistryMessages.VoterID voterInfo = RegistryMessages.VoterID.newBuilder().setId(id).build(); SimpleRegistry registry = new SimpleRegistry(signer, communicator); registry.SetVoted(voterInfo, handler); jobSemaphore.acquire(); - - assertEquals(handler.counter , 0); + assertEquals(1, handler.counter ); List tags = new ArrayList(){{ add(RegistryTags.VOTE_ACTION_TAG.toString());}}; BulletinBoardAPI.MessageFilterList filters = registry.GetRelevantMessagesFilters(tags); @@ -207,6 +206,7 @@ public class SimpleRegistryTest extends TestCase { jobSemaphore.acquire(); + tags.add(id); int counter = countMessagesWithTags(ConvertToVoterRegistryMessages(bulletinHandler.messages), tags); assert counter == 1 : "The server don't have the new user id."; } @@ -217,18 +217,17 @@ public class SimpleRegistryTest extends TestCase { public void testAddToGroup() throws InvalidProtocolBufferException, InterruptedException { DummyRegistryCallBackHandler handler = new DummyRegistryCallBackHandler<>(); + String voterId = new BigInteger(130, random).toString(32); + String groupId = new BigInteger(130, random).toString(32); RegistryMessages.VoterGroup voterInfo = RegistryMessages.VoterGroup.newBuilder() - .setVoterId(RegistryMessages.VoterID.newBuilder() - .setId((new BigInteger(130, random).toString(32)))) - .setGroupId(RegistryMessages.GroupID.newBuilder() - .setId((new BigInteger(130, random).toString(32)))).build(); + .setVoterId(RegistryMessages.VoterID.newBuilder().setId(voterId)) + .setGroupId(RegistryMessages.GroupID.newBuilder().setId(groupId)).build(); SimpleRegistry registry = new SimpleRegistry(signer, communicator); registry.AddToGroup(voterInfo, handler); jobSemaphore.acquire(); - - assertEquals(handler.counter , 0); + assertEquals(1, handler.counter); List tags = new ArrayList(){{ add(RegistryTags.GROUP_ACTION_TAG .toString() + RegistryTags.ADD_TO_GROUP_TAG.toString());}}; @@ -238,6 +237,9 @@ public class SimpleRegistryTest extends TestCase { jobSemaphore.acquire(); + tags.add(voterId); + tags.add(groupId); + int counter = countMessagesWithTags(ConvertToVoterRegistryMessages(bulletinHandler.messages), tags); assert counter == 1 : "The server don't have the new user added to group."; } @@ -249,33 +251,27 @@ public class SimpleRegistryTest extends TestCase { DummyRegistryCallBackHandler> handler = new DummyRegistryCallBackHandler<>(); + String voterId = new BigInteger(130, random).toString(32); + String groupId = new BigInteger(130, random).toString(32); RegistryMessages.VoterGroup voterInfo = RegistryMessages.VoterGroup.newBuilder() - .setVoterId(RegistryMessages.VoterID.newBuilder() - .setId((new BigInteger(130, random).toString(32)))) - .setGroupId(RegistryMessages.GroupID.newBuilder() - .setId((new BigInteger(130, random).toString(32)))).build(); + .setVoterId(RegistryMessages.VoterID.newBuilder().setId(voterId)) + .setGroupId(RegistryMessages.GroupID.newBuilder().setId(groupId)).build(); SimpleRegistry registry = new SimpleRegistry(signer, communicator); registry.AddToGroup(voterInfo, handler); jobSemaphore.acquire(); + assertEquals(1, handler.counter ); - assertEquals(handler.counter , 0); - - List tags = new ArrayList(){{ add(RegistryTags.GROUP_ACTION_TAG .toString() - + RegistryTags.ADD_TO_GROUP_TAG.toString());}}; - BulletinBoardAPI.MessageFilterList filters = registry.GetRelevantMessagesFilters(tags); - DummyBulletinBoardCallBackHandler bulletinHandler = new DummyBulletinBoardCallBackHandler(); - communicator.readMessages(filters, bulletinHandler); + DummyRegistryCallBackHandler> groupsHandler = new DummyRegistryCallBackHandler<>(); + registry.GetGroups(RegistryMessages.GroupID.newBuilder().setId(groupId).build(), groupsHandler); jobSemaphore.acquire(); - - List voterMessages = ConvertToVoterRegistryMessages(handler.data); - assertTrue(voterMessages.get(0).GetWantedTagFromBasicMessage(RegistryTags.ID_TAG.toString()).contains(voterInfo.getVoterId().getId())); + List userGroups = groupsHandler.data; + assert userGroups.contains(RegistryTags.GROUP_ID_TAG + groupId) : + "The simple voter registry object does not retrieved right user groups"; } - - /** * Test that the personal data outputted about the user is right */ @@ -285,7 +281,6 @@ public class SimpleRegistryTest extends TestCase { String id = new BigInteger(130, random).toString(32); String data = new BigInteger(130, random).toString(32); - RegistryMessages.VoterInfo voterInfo = RegistryMessages.VoterInfo.newBuilder(). setId(RegistryMessages.VoterID.newBuilder().setId(id)).setInfo(data).build(); @@ -293,18 +288,14 @@ public class SimpleRegistryTest extends TestCase { registry.AddVoter(voterInfo, handler); jobSemaphore.acquire(); + assertEquals(1, handler.counter ); - assertEquals(handler.counter , 0); - - List tags = new ArrayList(){{ add(RegistryTags.VOTER_ENTRY_TAG.toString());}}; - BulletinBoardAPI.MessageFilterList filters = registry.GetRelevantMessagesFilters(tags); - DummyBulletinBoardCallBackHandler bulletinHandler = new DummyBulletinBoardCallBackHandler(); - communicator.readMessages(filters, bulletinHandler); + DummyRegistryCallBackHandler personalHandler = new DummyRegistryCallBackHandler<>(); + registry.GetPersonIDDetails(RegistryMessages.VoterID.newBuilder().setId(id).build(), personalHandler); jobSemaphore.acquire(); - - List voterMessages = ConvertToVoterRegistryMessages(handler.data); - int counter = countMessagesWithTags(voterMessages, new ArrayList(){{add(id);add(data);}}); - assert counter == 1 : "The method doesnt retreived the wanted information."; + assertEquals(RegistryTags.ID_TAG + id, + personalHandler.data.GetWantedTagFromBasicMessage(RegistryTags.ID_TAG.toString())); + assertEquals(data,personalHandler.data.base.getData().toStringUtf8()); } }