Changed the simple voter registry tests (all passes) to the new strcutrue of the registry

Voter-Registry
Vladimir Eliezer Tokarev 2016-02-20 08:54:22 -08:00
parent 3347a6c42d
commit 6ec0d4a668
3 changed files with 50 additions and 52 deletions

View File

@ -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<BulletinBoardAPI.BulletinBoardMessage> 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<String, VoterRegistryMessage> map = GetLatestGroupsActions(messages);
callback.HandleResult(GetListOfGroupIds(map));
List<String> groupsOfUser = GetListOfGroupIds(map);
callback.HandleResult(groupsOfUser);
} catch (ParseException e) {
callback.HandleResult(null);
}

View File

@ -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<String> 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;

View File

@ -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<Boolean> 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<String> tags = new ArrayList<String>(){{ 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<Boolean> 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<String> tags = new ArrayList<String>(){{ 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<Boolean> 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<String> tags = new ArrayList<String>(){{ 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<List<BulletinBoardAPI.BulletinBoardMessage>> 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<String> tags = new ArrayList<String>(){{ 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<List<String>> groupsHandler = new DummyRegistryCallBackHandler<>();
registry.GetGroups(RegistryMessages.GroupID.newBuilder().setId(groupId).build(), groupsHandler);
jobSemaphore.acquire();
List<VoterRegistryMessage> voterMessages = ConvertToVoterRegistryMessages(handler.data);
assertTrue(voterMessages.get(0).GetWantedTagFromBasicMessage(RegistryTags.ID_TAG.toString()).contains(voterInfo.getVoterId().getId()));
List<String> 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<String> tags = new ArrayList<String>(){{ add(RegistryTags.VOTER_ENTRY_TAG.toString());}};
BulletinBoardAPI.MessageFilterList filters = registry.GetRelevantMessagesFilters(tags);
DummyBulletinBoardCallBackHandler bulletinHandler = new DummyBulletinBoardCallBackHandler();
communicator.readMessages(filters, bulletinHandler);
DummyRegistryCallBackHandler<VoterRegistryMessage> personalHandler = new DummyRegistryCallBackHandler<>();
registry.GetPersonIDDetails(RegistryMessages.VoterID.newBuilder().setId(id).build(), personalHandler);
jobSemaphore.acquire();
List<VoterRegistryMessage> voterMessages = ConvertToVoterRegistryMessages(handler.data);
int counter = countMessagesWithTags(voterMessages, new ArrayList<String>(){{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());
}
}