diff --git a/meerkat-common/src/main/java/meerkat/util/BulletinBoardUtils.java b/meerkat-common/src/main/java/meerkat/util/BulletinBoardUtils.java index 68867ff..9bc97ff 100644 --- a/meerkat-common/src/main/java/meerkat/util/BulletinBoardUtils.java +++ b/meerkat-common/src/main/java/meerkat/util/BulletinBoardUtils.java @@ -18,7 +18,6 @@ public class BulletinBoardUtils { * @return the tag without the prefix, if found, or null if not found */ public static String findTagWithPrefix(BulletinBoardMessage message, String prefix) { - for (String tag : message.getMsg().getTagList()){ if (tag.startsWith(prefix)) { return tag.substring(prefix.length()); diff --git a/voter-registry/src/main/java/meerkat/Registry.java b/voter-registry/src/main/java/meerkat/Registry.java index 9920167..ee00759 100644 --- a/voter-registry/src/main/java/meerkat/Registry.java +++ b/voter-registry/src/main/java/meerkat/Registry.java @@ -80,9 +80,9 @@ public class Registry implements VoterRegistry{ .addTag(RegistryTags.ADD_TO_GROUP_TAG) .setTimestamp(Timestamp.newBuilder().setNanos((int) System.nanoTime()).build()); - for ( int i = 0 ; i < voterGroup.getGroupIDCount() ; i++) + for (GroupID groupId : voterGroup.getGroupIDList()) { - basicMessage.addTag(RegistryTags.GROUP_ID_TAG + voterGroup.getGroupID(i).getId()); + basicMessage.addTag(RegistryTags.GROUP_ID_TAG + groupId.getId()); } bulletinBoardClient.postMessage(CreateBulletinBoardMessage(basicMessage.build()), new MessagesCallBack(callback)); diff --git a/voter-registry/src/main/java/meerkat/registry/LatestMessagesCallBack.java b/voter-registry/src/main/java/meerkat/registry/LatestMessagesCallBack.java index 5a6e044..0b9ad7e 100644 --- a/voter-registry/src/main/java/meerkat/registry/LatestMessagesCallBack.java +++ b/voter-registry/src/main/java/meerkat/registry/LatestMessagesCallBack.java @@ -56,8 +56,7 @@ public class LatestMessagesCallBack implements FutureCallback msg) { List tags = msg.get(0).getMsg().getTagList(); - for (int i = 0 ;i < tags.size(); i++) { - String tag = tags.get(i); + for (String tag : tags) { if(tag.contains(RegistryTags.GROUP_ID_TAG)) { return true; @@ -123,9 +122,8 @@ public class LatestMessagesCallBack implements FutureCallback validate(List object) throws ValidationError { List verifiedMessages = new ArrayList<>(object.size()); - for (int i = 0 ; i < object.size() ; i++) + for (BulletinBoardMessage message : object) { - BulletinBoardMessage message = object.get(i); if(VerifyMessage(message.getMsg())) { verifiedMessages.add(message); diff --git a/voter-registry/src/main/java/meerkat/registry/MessageCollectionUtils.java b/voter-registry/src/main/java/meerkat/registry/MessageCollectionUtils.java index c011e38..98c02c7 100644 --- a/voter-registry/src/main/java/meerkat/registry/MessageCollectionUtils.java +++ b/voter-registry/src/main/java/meerkat/registry/MessageCollectionUtils.java @@ -28,9 +28,8 @@ public abstract class MessageCollectionUtils { return filters.build(); } - for (int i = 0 ;i < tags.size() ; i++) { - String tag = tags.get(i); - MessageFilter.Builder filter = MessageFilter.newBuilder().setTag(tag).setType(FilterType.TAG); + for (String tag : tags) { + MessageFilter filter = MessageFilter.newBuilder().setTag(tag).setType(FilterType.TAG).build(); filters.addFilter(filter); } return filters.build(); diff --git a/voter-registry/src/main/java/meerkat/registry/RegistryTags.java b/voter-registry/src/main/java/meerkat/registry/RegistryTags.java index dc09ad4..df1910a 100644 --- a/voter-registry/src/main/java/meerkat/registry/RegistryTags.java +++ b/voter-registry/src/main/java/meerkat/registry/RegistryTags.java @@ -5,13 +5,13 @@ package meerkat.registry; * Created by Vladimir Eliezer Tokarev on 1/9/2016. * Have the tags for the registry messages */ -public abstract class RegistryTags { - public final static String ID_TAG = "ID: "; - public final static String VOTER_ENTRY_TAG = "VoterEntry: "; - public final static String VOTER_DATA_TAG = "Data: "; - public final static String GROUP_ID_TAG = "GroupID: "; - public final static String ADD_TO_GROUP_TAG = "AddToGroups: "; - public final static String VOTE_ACTION_TAG = "VoteAction: "; +public interface RegistryTags { + String ID_TAG = "ID: "; + String VOTER_ENTRY_TAG = "VoterEntry: "; + String VOTER_DATA_TAG = "Data: "; + String GROUP_ID_TAG = "GroupID: "; + String ADD_TO_GROUP_TAG = "AddToGroups: "; + String VOTE_ACTION_TAG = "VoteAction: "; } diff --git a/voter-registry/src/test/java/SimpleRegistryTest.java b/voter-registry/src/test/java/SimpleRegistryTest.java index 352b75c..6bafb5d 100644 --- a/voter-registry/src/test/java/SimpleRegistryTest.java +++ b/voter-registry/src/test/java/SimpleRegistryTest.java @@ -2,8 +2,6 @@ import com.google.common.util.concurrent.FutureCallback; import com.google.protobuf.InvalidProtocolBufferException; import junit.framework.TestCase; import meerkat.Registry; -import meerkat.registry.MessageCollectionUtils; -import meerkat.registry.RegistryTags; import meerkat.bulletinboard.AsyncBulletinBoardClient; import meerkat.bulletinboard.ThreadedBulletinBoardClient; import meerkat.crypto.concrete.ECDSASignature; @@ -14,6 +12,8 @@ import meerkat.protobuf.VoterRegistry.GroupID; import meerkat.protobuf.VoterRegistry.VoterID; import meerkat.protobuf.VoterRegistry.VoterInfo; import meerkat.protobuf.Voting; +import meerkat.registry.MessageCollectionUtils; +import meerkat.registry.RegistryTags; import java.io.InputStream; import java.math.BigInteger; @@ -23,6 +23,8 @@ import java.util.ArrayList; import java.util.List; import java.util.concurrent.Semaphore; +import static meerkat.util.BulletinBoardUtils.findTagWithPrefix; + /** * TODO: add logs prints for the tests to be clear what they are */ @@ -144,13 +146,11 @@ public class SimpleRegistryTest extends TestCase { { int counter = 0 ; - for (int i = 0 ; i < messages.size() ; i++) { - BulletinBoardMessage message = messages.get(i); + for (BulletinBoardMessage message : messages) { int wantedTagsCounter = 0; - for (int j = 0 ;j < tags.size() ; j++) { - String tag = tags.get(j); - if(MessageCollectionUtils.GetTagByName(message.getMsg().getTagList(), tag)!=null){ + for (String tag : tags) { + if(findTagWithPrefix(message, tag)!=null){ wantedTagsCounter++; } } @@ -186,7 +186,8 @@ public class SimpleRegistryTest extends TestCase { jobSemaphore.acquire(); - tags.add(id); + tags.clear(); + tags.add(RegistryTags.ID_TAG + id); int counter = countMessagesWithTags(bulletinHandler.messages, tags); assert counter == 1 : "The server don't have the new user data."; @@ -214,7 +215,9 @@ public class SimpleRegistryTest extends TestCase { jobSemaphore.acquire(); - tags.add(id); + tags.clear(); + tags.add(RegistryTags.ID_TAG + id); + int counter = countMessagesWithTags(bulletinHandler.messages, tags); assert counter == 1 : "The server don't have the new user id."; } @@ -244,8 +247,9 @@ public class SimpleRegistryTest extends TestCase { jobSemaphore.acquire(); - tags.add(voterId); - tags.add(groupId); + tags.clear(); + tags.add(RegistryTags.ID_TAG + voterId); + tags.add(RegistryTags.GROUP_ID_TAG + groupId); int counter = countMessagesWithTags(bulletinHandler.messages, tags); assert counter == 1 : "The server don't have the new user added to group."; @@ -277,8 +281,7 @@ public class SimpleRegistryTest extends TestCase { jobSemaphore.acquire(1); List userGroups = groupsHandler.data; - assert userGroups.contains(RegistryTags.GROUP_ID_TAG + groupId) : - "The simple voter registry object does not retrieved right user groups"; + assert userGroups.contains(groupId) : "The simple voter registry object does not retrieved right user groups"; } /** @@ -303,10 +306,8 @@ public class SimpleRegistryTest extends TestCase { registry.GetPersonIDDetails(VoterID.newBuilder().setId(id).build(), personalHandler); jobSemaphore.acquire(1); - assertEquals(RegistryTags.ID_TAG + id, MessageCollectionUtils.GetTagByName(personalHandler - .data.getMsg().getTagList(), RegistryTags.ID_TAG)); - assertTrue(MessageCollectionUtils.GetTagByName(personalHandler.data.getMsg().getTagList(), - RegistryTags.VOTER_DATA_TAG).contains(data)); + assertEquals(id, findTagWithPrefix(personalHandler.data, RegistryTags.ID_TAG)); + assertTrue(findTagWithPrefix(personalHandler.data, RegistryTags.VOTER_DATA_TAG).contains(data)); } }