2016-03-04 08:36:48 -05:00
|
|
|
import com.google.common.util.concurrent.FutureCallback;
|
2016-03-11 09:02:09 -05:00
|
|
|
import meerkat.AsyncRegistry;
|
2016-03-04 08:36:48 -05:00
|
|
|
import meerkat.bulletinboard.AsyncBulletinBoardClient;
|
2016-02-20 10:48:10 -05:00
|
|
|
import meerkat.bulletinboard.ThreadedBulletinBoardClient;
|
2016-03-05 05:36:35 -05:00
|
|
|
import meerkat.crypto.DigitalSignature;
|
2016-02-19 11:41:02 -05:00
|
|
|
import meerkat.crypto.concrete.ECDSASignature;
|
2016-02-26 07:54:39 -05:00
|
|
|
import meerkat.protobuf.BulletinBoardAPI.BulletinBoardMessage;
|
|
|
|
import meerkat.protobuf.BulletinBoardAPI.MessageFilterList;
|
2016-03-04 09:05:06 -05:00
|
|
|
import meerkat.protobuf.VoterRegistry;
|
2016-02-26 08:52:03 -05:00
|
|
|
import meerkat.protobuf.VoterRegistry.GroupID;
|
|
|
|
import meerkat.protobuf.VoterRegistry.VoterID;
|
|
|
|
import meerkat.protobuf.VoterRegistry.VoterInfo;
|
2016-01-23 12:16:57 -05:00
|
|
|
import meerkat.protobuf.Voting;
|
2016-03-04 11:53:42 -05:00
|
|
|
import meerkat.registry.MessageCollectionUtils;
|
|
|
|
import meerkat.registry.RegistryTags;
|
2016-03-05 09:38:12 -05:00
|
|
|
import org.junit.Before;
|
2016-03-05 09:33:07 -05:00
|
|
|
import org.junit.Test;
|
2016-01-23 12:16:57 -05:00
|
|
|
|
2016-02-20 10:48:10 -05:00
|
|
|
import java.io.InputStream;
|
2016-01-23 12:16:57 -05:00
|
|
|
import java.math.BigInteger;
|
2016-02-20 10:48:10 -05:00
|
|
|
import java.security.KeyStore;
|
2016-01-30 05:30:12 -05:00
|
|
|
import java.security.SecureRandom;
|
2016-03-11 09:02:09 -05:00
|
|
|
import java.security.SignatureException;
|
2016-01-23 12:16:57 -05:00
|
|
|
import java.util.ArrayList;
|
2016-03-05 05:36:35 -05:00
|
|
|
import java.util.Collection;
|
2016-01-23 12:16:57 -05:00
|
|
|
import java.util.List;
|
2016-02-20 10:48:10 -05:00
|
|
|
import java.util.concurrent.Semaphore;
|
2016-01-30 05:30:12 -05:00
|
|
|
|
2016-03-05 09:38:12 -05:00
|
|
|
import static junit.framework.TestCase.assertEquals;
|
|
|
|
import static junit.framework.TestCase.assertTrue;
|
2016-03-04 11:53:42 -05:00
|
|
|
import static meerkat.util.BulletinBoardUtils.findTagWithPrefix;
|
|
|
|
|
2016-02-20 10:48:10 -05:00
|
|
|
/**
|
2016-02-26 00:45:52 -05:00
|
|
|
* TODO: add logs prints for the tests to be clear what they are
|
2016-02-20 10:48:10 -05:00
|
|
|
*/
|
|
|
|
|
2016-01-16 09:57:12 -05:00
|
|
|
/**
|
|
|
|
* Created by Vladimir Eliezer Tokarev on 1/16/2016.
|
2016-03-04 11:26:55 -05:00
|
|
|
* Tests the Simple registry contents
|
2016-01-23 12:16:57 -05:00
|
|
|
* NOTE: for most of this tests to pass there should run BulletinBoardServer
|
2016-01-30 05:30:12 -05:00
|
|
|
* that should be reachable on BULLETIN_BOARD_SERVER_ADDRESS
|
2016-01-16 09:57:12 -05:00
|
|
|
*/
|
2016-03-05 09:38:12 -05:00
|
|
|
public class SimpleRegistryTest /**extends TestCase**/ {
|
2016-01-16 09:57:12 -05:00
|
|
|
|
2016-03-05 05:36:35 -05:00
|
|
|
private Collection<DigitalSignature> signers;
|
2016-03-04 08:36:48 -05:00
|
|
|
private AsyncBulletinBoardClient bulletinBoardClient;
|
2016-02-26 07:54:39 -05:00
|
|
|
private InputStream certStream;
|
2016-01-30 05:30:12 -05:00
|
|
|
private SecureRandom random = new SecureRandom();
|
2016-02-26 07:54:39 -05:00
|
|
|
|
2016-02-20 10:48:10 -05:00
|
|
|
public static String KEYFILE_EXAMPLE = "/certs/enduser-certs/user1-key-with-password-secret.p12";
|
2016-02-26 07:54:39 -05:00
|
|
|
public static String CERT1_PEM_EXAMPLE = "/certs/enduser-certs/user1.crt";
|
2016-02-20 10:48:10 -05:00
|
|
|
public static String KEYFILE_PASSWORD = "secret";
|
|
|
|
Semaphore jobSemaphore;
|
|
|
|
|
2016-03-11 09:02:09 -05:00
|
|
|
class DummyRegistryCallBackHandler<T> implements FutureCallback<T>{
|
2016-02-20 10:48:10 -05:00
|
|
|
public int counter;
|
|
|
|
public T data;
|
|
|
|
|
|
|
|
public DummyRegistryCallBackHandler()
|
|
|
|
{
|
|
|
|
counter=0;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-03-11 09:02:09 -05:00
|
|
|
public void onSuccess(T result) {
|
2016-02-20 10:48:10 -05:00
|
|
|
counter++;
|
|
|
|
data = result;
|
|
|
|
jobSemaphore.release();
|
|
|
|
}
|
2016-02-27 06:46:36 -05:00
|
|
|
|
|
|
|
@Override
|
2016-03-11 09:02:09 -05:00
|
|
|
public void onFailure(Throwable throwable) {
|
2016-03-05 09:33:07 -05:00
|
|
|
System.out.print(throwable);
|
2016-02-27 06:46:36 -05:00
|
|
|
}
|
2016-02-20 10:48:10 -05:00
|
|
|
}
|
|
|
|
|
2016-03-04 08:36:48 -05:00
|
|
|
public class DummyBulletinBoardCallBackHandler implements FutureCallback<List<BulletinBoardMessage>> {
|
2016-02-26 07:54:39 -05:00
|
|
|
public List<BulletinBoardMessage> messages;
|
2016-02-20 10:48:10 -05:00
|
|
|
|
|
|
|
@Override
|
2016-03-04 08:36:48 -05:00
|
|
|
public void onSuccess(List<BulletinBoardMessage> msg)
|
2016-02-20 10:48:10 -05:00
|
|
|
{
|
|
|
|
messages = msg;
|
|
|
|
jobSemaphore.release();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-03-04 08:36:48 -05:00
|
|
|
public void onFailure(Throwable t){
|
2016-02-20 10:48:10 -05:00
|
|
|
messages = null;
|
|
|
|
jobSemaphore.release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-23 12:16:57 -05:00
|
|
|
|
2016-01-30 05:30:12 -05:00
|
|
|
private void CommunicatorSetup() {
|
2016-02-26 00:45:52 -05:00
|
|
|
bulletinBoardClient = new ThreadedBulletinBoardClient();
|
2016-02-27 06:00:14 -05:00
|
|
|
String BULLETIN_BOARD_SERVER_ADDRESS = "http://localhost:8081/";
|
2016-02-26 00:45:52 -05:00
|
|
|
bulletinBoardClient.init(Voting.BulletinBoardClientParams.newBuilder()
|
2016-01-23 12:16:57 -05:00
|
|
|
.addBulletinBoardAddress(BULLETIN_BOARD_SERVER_ADDRESS)
|
|
|
|
.setMinRedundancy((float) 1.0)
|
|
|
|
.build());
|
|
|
|
}
|
|
|
|
|
2016-02-20 10:48:10 -05:00
|
|
|
private void SetSigner(){
|
|
|
|
try {
|
2016-03-05 05:36:35 -05:00
|
|
|
signers = new ArrayList<>();
|
|
|
|
ECDSASignature signer = new ECDSASignature();
|
2016-02-20 10:48:10 -05:00
|
|
|
InputStream keyStream = getClass().getResourceAsStream(KEYFILE_EXAMPLE);
|
|
|
|
char[] password = KEYFILE_PASSWORD.toCharArray();
|
|
|
|
|
|
|
|
KeyStore.Builder keyStore = signer.getPKCS12KeyStoreBuilder(keyStream, password);
|
|
|
|
signer.loadSigningCertificate(keyStore);
|
2016-03-11 09:02:09 -05:00
|
|
|
signer.loadVerificationCertificates(getClass().getResourceAsStream(CERT1_PEM_EXAMPLE));
|
2016-02-26 07:54:39 -05:00
|
|
|
|
2016-02-20 10:48:10 -05:00
|
|
|
keyStream.close();
|
2016-03-05 05:36:35 -05:00
|
|
|
signers.add(signer);
|
2016-03-11 09:02:09 -05:00
|
|
|
|
2016-02-20 10:48:10 -05:00
|
|
|
}
|
|
|
|
catch (Exception e){
|
2016-03-05 05:36:35 -05:00
|
|
|
assert false : "The signers creation failed ";
|
2016-02-20 10:48:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-30 05:30:12 -05:00
|
|
|
/**
|
2016-03-04 11:26:55 -05:00
|
|
|
* Initialize registry object
|
2016-01-16 09:57:12 -05:00
|
|
|
*/
|
2016-03-05 09:38:12 -05:00
|
|
|
@Before
|
2016-01-30 05:30:12 -05:00
|
|
|
public void setUp() {
|
2016-02-20 10:48:10 -05:00
|
|
|
SetSigner();
|
2016-01-23 12:16:57 -05:00
|
|
|
CommunicatorSetup();
|
2016-02-20 10:48:10 -05:00
|
|
|
jobSemaphore = new Semaphore(0);
|
2016-01-23 12:16:57 -05:00
|
|
|
}
|
2016-01-16 09:57:12 -05:00
|
|
|
|
2016-03-11 09:02:09 -05:00
|
|
|
private AsyncRegistry GetRegistry()
|
2016-03-05 05:36:35 -05:00
|
|
|
{
|
2016-03-11 09:02:09 -05:00
|
|
|
AsyncRegistry registry = new AsyncRegistry();
|
2016-03-05 06:20:07 -05:00
|
|
|
registry.init(signers, bulletinBoardClient);
|
2016-03-05 05:36:35 -05:00
|
|
|
return registry;
|
|
|
|
}
|
|
|
|
|
2016-03-05 09:33:07 -05:00
|
|
|
private String generateString()
|
|
|
|
{
|
|
|
|
return new BigInteger(130, random).toString(32);
|
|
|
|
}
|
|
|
|
|
2016-01-23 12:16:57 -05:00
|
|
|
/**
|
|
|
|
* Checks if the creation of the registry have been successful
|
|
|
|
*/
|
2016-03-05 09:33:07 -05:00
|
|
|
@Test
|
|
|
|
public void simpleRegistryCreation() {
|
2016-01-23 12:16:57 -05:00
|
|
|
try {
|
2016-03-11 09:02:09 -05:00
|
|
|
AsyncRegistry registry = GetRegistry();
|
2016-01-30 05:30:12 -05:00
|
|
|
} catch (Exception e) {
|
2016-03-04 11:26:55 -05:00
|
|
|
assert false : "While creating the registry exception have been thrown " + e;
|
2016-01-23 12:16:57 -05:00
|
|
|
}
|
2016-01-16 09:57:12 -05:00
|
|
|
}
|
|
|
|
|
2016-02-20 10:48:10 -05:00
|
|
|
/**
|
|
|
|
* Counts the amount of messages from messages that have all the wanted tags inside
|
|
|
|
* @param messages List<VoterRegistryMessage>
|
|
|
|
* @param tags List<RegistryTags>
|
|
|
|
* @return integer that represent the amount of messages with wanted tags
|
|
|
|
*/
|
2016-02-27 06:46:36 -05:00
|
|
|
private int countMessagesWithTags(List<BulletinBoardMessage> messages, List<String> tags)
|
2016-02-20 10:48:10 -05:00
|
|
|
{
|
|
|
|
int counter = 0 ;
|
|
|
|
|
2016-03-04 11:53:42 -05:00
|
|
|
for (BulletinBoardMessage message : messages) {
|
2016-02-20 10:48:10 -05:00
|
|
|
int wantedTagsCounter = 0;
|
|
|
|
|
2016-03-04 11:53:42 -05:00
|
|
|
for (String tag : tags) {
|
|
|
|
if(findTagWithPrefix(message, tag)!=null){
|
2016-02-20 11:54:22 -05:00
|
|
|
wantedTagsCounter++;
|
|
|
|
}
|
2016-02-20 10:48:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if(wantedTagsCounter == tags.size())
|
|
|
|
{
|
|
|
|
counter++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return counter;
|
|
|
|
}
|
|
|
|
|
2016-03-05 09:33:07 -05:00
|
|
|
/**
|
|
|
|
* Reads messages from bulletinBoardClient by given tags and return the callback handler
|
|
|
|
* object
|
|
|
|
*
|
|
|
|
* @param tags list of strings that represents tags
|
|
|
|
* @return DummyBulletinBoardCallBackHandler which method will be called
|
|
|
|
*/
|
|
|
|
private DummyBulletinBoardCallBackHandler readMessagesByTags(List<String> tags)
|
|
|
|
{
|
|
|
|
MessageFilterList filters = MessageCollectionUtils.generateFiltersFromTags(tags);
|
|
|
|
DummyBulletinBoardCallBackHandler bulletinHandler = new DummyBulletinBoardCallBackHandler();
|
|
|
|
bulletinBoardClient.readMessages(filters, bulletinHandler);
|
|
|
|
return bulletinHandler;
|
|
|
|
}
|
|
|
|
|
2016-01-16 09:57:12 -05:00
|
|
|
/**
|
2016-02-19 11:41:02 -05:00
|
|
|
* Test that add voter creates new correct bulletin board message and adds the voter
|
2016-01-16 09:57:12 -05:00
|
|
|
*/
|
2016-03-05 09:33:07 -05:00
|
|
|
@Test
|
2016-03-11 09:02:09 -05:00
|
|
|
public void testAddVoter() throws InterruptedException, SignatureException {
|
2016-02-20 10:48:10 -05:00
|
|
|
DummyRegistryCallBackHandler<Boolean> handler = new DummyRegistryCallBackHandler<>();
|
2016-01-23 12:16:57 -05:00
|
|
|
|
2016-03-05 09:33:07 -05:00
|
|
|
String id = generateString();
|
|
|
|
String data = generateString();
|
2016-02-27 06:00:14 -05:00
|
|
|
VoterInfo voterInfo = VoterInfo.newBuilder().setId(VoterID.newBuilder().setId(id)).setInfo(data).build();
|
2016-01-30 05:30:12 -05:00
|
|
|
|
2016-03-11 09:02:09 -05:00
|
|
|
AsyncRegistry registry = GetRegistry();
|
2016-03-05 05:36:35 -05:00
|
|
|
registry.addVoter(voterInfo, handler);
|
2016-02-19 11:41:02 -05:00
|
|
|
|
2016-02-20 10:48:10 -05:00
|
|
|
jobSemaphore.acquire();
|
2016-02-20 11:54:22 -05:00
|
|
|
assertEquals(1, handler.counter );
|
2016-01-30 05:30:12 -05:00
|
|
|
|
2016-02-27 04:27:19 -05:00
|
|
|
List<String> tags = new ArrayList<String>(){{ add(RegistryTags.VOTER_ENTRY_TAG);}};
|
2016-03-05 09:33:07 -05:00
|
|
|
DummyBulletinBoardCallBackHandler bulletinHandler = readMessagesByTags(tags);
|
2016-01-16 09:57:12 -05:00
|
|
|
|
2016-02-20 10:48:10 -05:00
|
|
|
jobSemaphore.acquire();
|
2016-02-19 11:41:02 -05:00
|
|
|
|
2016-03-04 11:53:42 -05:00
|
|
|
tags.clear();
|
|
|
|
tags.add(RegistryTags.ID_TAG + id);
|
2016-02-20 11:54:22 -05:00
|
|
|
|
2016-02-27 06:46:36 -05:00
|
|
|
int counter = countMessagesWithTags(bulletinHandler.messages, tags);
|
2016-02-20 10:48:10 -05:00
|
|
|
assert counter == 1 : "The server don't have the new user data.";
|
2016-01-30 05:30:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-02-19 11:41:02 -05:00
|
|
|
* Test that set voted posts creates correct bulletin board message and sets that the user have been voted
|
2016-01-30 05:30:12 -05:00
|
|
|
*/
|
2016-03-05 09:33:07 -05:00
|
|
|
@Test
|
2016-03-11 09:02:09 -05:00
|
|
|
public void testSetVoted() throws InterruptedException, SignatureException {
|
2016-02-19 11:41:02 -05:00
|
|
|
DummyRegistryCallBackHandler<Boolean> handler = new DummyRegistryCallBackHandler<>();
|
|
|
|
|
2016-03-05 09:33:07 -05:00
|
|
|
String id = generateString();
|
2016-02-26 08:17:29 -05:00
|
|
|
VoterID voterInfo = VoterID.newBuilder().setId(id).build();
|
2016-01-30 05:30:12 -05:00
|
|
|
|
2016-03-11 09:02:09 -05:00
|
|
|
AsyncRegistry registry = GetRegistry();
|
2016-03-05 05:36:35 -05:00
|
|
|
registry.setVoted(voterInfo, handler);
|
2016-01-30 05:30:12 -05:00
|
|
|
|
2016-02-20 10:48:10 -05:00
|
|
|
jobSemaphore.acquire();
|
2016-02-20 11:54:22 -05:00
|
|
|
assertEquals(1, handler.counter );
|
2016-01-30 05:30:12 -05:00
|
|
|
|
2016-02-27 04:27:19 -05:00
|
|
|
List<String> tags = new ArrayList<String>(){{ add(RegistryTags.VOTE_ACTION_TAG);}};
|
2016-03-05 09:33:07 -05:00
|
|
|
DummyBulletinBoardCallBackHandler bulletinHandler = readMessagesByTags(tags);
|
2016-01-30 05:30:12 -05:00
|
|
|
|
2016-02-20 10:48:10 -05:00
|
|
|
jobSemaphore.acquire();
|
2016-01-23 12:16:57 -05:00
|
|
|
|
2016-03-04 11:53:42 -05:00
|
|
|
tags.clear();
|
|
|
|
tags.add(RegistryTags.ID_TAG + id);
|
|
|
|
|
2016-02-27 06:46:36 -05:00
|
|
|
int counter = countMessagesWithTags(bulletinHandler.messages, tags);
|
2016-02-20 10:48:10 -05:00
|
|
|
assert counter == 1 : "The server don't have the new user id.";
|
2016-01-16 09:57:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-01-30 12:19:25 -05:00
|
|
|
* Test that get groups retrieves the right groups the user are in
|
2016-01-16 09:57:12 -05:00
|
|
|
*/
|
2016-03-05 09:33:07 -05:00
|
|
|
@Test
|
2016-03-11 09:02:09 -05:00
|
|
|
public void testAddToGroup() throws InterruptedException, SignatureException {
|
2016-02-19 11:41:02 -05:00
|
|
|
DummyRegistryCallBackHandler<Boolean> handler = new DummyRegistryCallBackHandler<>();
|
|
|
|
|
2016-03-05 09:33:07 -05:00
|
|
|
String voterId = generateString();
|
|
|
|
String groupId = generateString();
|
2016-03-04 09:05:06 -05:00
|
|
|
VoterRegistry.VoterRegistryMessage voterInfo = VoterRegistry.VoterRegistryMessage.newBuilder()
|
|
|
|
.setVoterID(VoterID.newBuilder().setId(voterId))
|
|
|
|
.addGroupID(GroupID.newBuilder().setId(groupId)).build();
|
2016-02-19 11:41:02 -05:00
|
|
|
|
2016-03-11 09:02:09 -05:00
|
|
|
AsyncRegistry registry = GetRegistry();
|
2016-03-05 06:20:07 -05:00
|
|
|
registry.setVoterGroups(voterInfo, handler);
|
2016-02-19 11:41:02 -05:00
|
|
|
|
2016-02-20 10:48:10 -05:00
|
|
|
jobSemaphore.acquire();
|
2016-03-05 09:33:07 -05:00
|
|
|
assertEquals("The callback handler hasn't been called yet", 1, handler.counter);
|
2016-02-19 11:41:02 -05:00
|
|
|
|
2016-02-27 04:27:19 -05:00
|
|
|
List<String> tags = new ArrayList<String>(){{add(RegistryTags.ADD_TO_GROUP_TAG);}};
|
2016-03-05 09:33:07 -05:00
|
|
|
DummyBulletinBoardCallBackHandler bulletinHandler = readMessagesByTags(tags);
|
2016-02-19 11:41:02 -05:00
|
|
|
|
2016-02-20 10:48:10 -05:00
|
|
|
jobSemaphore.acquire();
|
2016-02-19 11:41:02 -05:00
|
|
|
|
2016-03-04 11:53:42 -05:00
|
|
|
tags.clear();
|
|
|
|
tags.add(RegistryTags.ID_TAG + voterId);
|
|
|
|
tags.add(RegistryTags.GROUP_ID_TAG + groupId);
|
2016-02-20 11:54:22 -05:00
|
|
|
|
2016-02-27 06:46:36 -05:00
|
|
|
int counter = countMessagesWithTags(bulletinHandler.messages, tags);
|
2016-02-20 10:48:10 -05:00
|
|
|
assert counter == 1 : "The server don't have the new user added to group.";
|
2016-01-16 09:57:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-01-30 12:19:25 -05:00
|
|
|
* Test that remove from group creates correct bulletin board message and removes the user from a group
|
2016-01-16 09:57:12 -05:00
|
|
|
*/
|
2016-03-05 09:33:07 -05:00
|
|
|
@Test
|
2016-03-11 09:02:09 -05:00
|
|
|
public void testGetGroups() throws InterruptedException, SignatureException {
|
|
|
|
DummyRegistryCallBackHandler<Boolean> handler =
|
2016-02-19 11:41:02 -05:00
|
|
|
new DummyRegistryCallBackHandler<>();
|
|
|
|
|
2016-03-05 09:33:07 -05:00
|
|
|
String voterId = generateString();
|
|
|
|
String groupId = generateString();
|
2016-03-04 09:05:06 -05:00
|
|
|
VoterRegistry.VoterRegistryMessage voterInfo = VoterRegistry.VoterRegistryMessage.newBuilder()
|
|
|
|
.setVoterID(VoterID.newBuilder().setId(voterId))
|
|
|
|
.addGroupID(GroupID.newBuilder().setId(groupId)).build();
|
2016-01-30 12:19:25 -05:00
|
|
|
|
2016-02-26 07:54:39 -05:00
|
|
|
this.certStream = getClass().getResourceAsStream(CERT1_PEM_EXAMPLE);
|
|
|
|
|
2016-03-11 09:02:09 -05:00
|
|
|
AsyncRegistry registry = GetRegistry();
|
2016-03-05 06:20:07 -05:00
|
|
|
registry.setVoterGroups(voterInfo, handler);
|
2016-01-30 12:19:25 -05:00
|
|
|
|
2016-02-20 10:48:10 -05:00
|
|
|
jobSemaphore.acquire();
|
2016-03-05 09:33:07 -05:00
|
|
|
assertEquals("The callback handler hasn't been called yet", 1, handler.counter );
|
2016-01-30 12:19:25 -05:00
|
|
|
|
2016-02-20 11:54:22 -05:00
|
|
|
DummyRegistryCallBackHandler<List<String>> groupsHandler = new DummyRegistryCallBackHandler<>();
|
2016-03-11 09:02:09 -05:00
|
|
|
registry.getGroups(VoterID.newBuilder().setId(groupId).build(), groupsHandler);
|
2016-02-19 11:41:02 -05:00
|
|
|
|
2016-02-26 07:54:39 -05:00
|
|
|
jobSemaphore.acquire(1);
|
2016-02-20 11:54:22 -05:00
|
|
|
List<String> userGroups = groupsHandler.data;
|
2016-03-04 11:53:42 -05:00
|
|
|
assert userGroups.contains(groupId) : "The simple voter registry object does not retrieved right user groups";
|
2016-01-16 09:57:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that the personal data outputted about the user is right
|
|
|
|
*/
|
2016-03-05 09:33:07 -05:00
|
|
|
@Test
|
2016-03-11 09:02:09 -05:00
|
|
|
public void testGetVoter() throws InterruptedException, SignatureException {
|
|
|
|
DummyRegistryCallBackHandler<Boolean> handler =
|
2016-02-20 10:48:10 -05:00
|
|
|
new DummyRegistryCallBackHandler<>();
|
|
|
|
|
2016-03-05 09:33:07 -05:00
|
|
|
String id = generateString();
|
|
|
|
String data = generateString();
|
2016-02-26 08:52:03 -05:00
|
|
|
VoterInfo voterInfo = VoterInfo.newBuilder().
|
|
|
|
setId(VoterID.newBuilder().setId(id)).setInfo(data).build();
|
2016-01-30 12:19:25 -05:00
|
|
|
|
2016-03-11 09:02:09 -05:00
|
|
|
AsyncRegistry registry = GetRegistry();
|
2016-03-05 05:36:35 -05:00
|
|
|
registry.addVoter(voterInfo, handler);
|
2016-01-30 12:19:25 -05:00
|
|
|
|
2016-02-20 10:48:10 -05:00
|
|
|
jobSemaphore.acquire();
|
2016-03-05 09:33:07 -05:00
|
|
|
assertEquals("The callback handler hasn't been called yet", 1, handler.counter );
|
2016-01-30 12:19:25 -05:00
|
|
|
|
2016-03-11 09:02:09 -05:00
|
|
|
DummyRegistryCallBackHandler<VoterInfo> personalHandler = new DummyRegistryCallBackHandler<>();
|
2016-03-05 09:38:12 -05:00
|
|
|
registry.getVoter(VoterID.newBuilder().setId(id).build(), personalHandler);
|
2016-02-19 11:41:02 -05:00
|
|
|
|
2016-02-26 08:52:03 -05:00
|
|
|
jobSemaphore.acquire(1);
|
2016-03-05 09:33:07 -05:00
|
|
|
assertEquals("The voter id doesn't match the created on ",
|
2016-03-11 09:02:09 -05:00
|
|
|
id, personalHandler.data.getId().getId());
|
|
|
|
String personalData = personalHandler.data.getInfo();
|
|
|
|
assertTrue("The voter personal data can't be found.", data.equals(personalData));
|
2016-01-16 09:57:12 -05:00
|
|
|
}
|
2016-03-05 09:33:07 -05:00
|
|
|
|
2016-03-05 09:38:12 -05:00
|
|
|
/**
|
|
|
|
* Tests that the hasVoted method of registry works
|
|
|
|
* @throws InterruptedException
|
|
|
|
*/
|
|
|
|
@Test
|
2016-03-11 09:02:09 -05:00
|
|
|
public void testHasVoted () throws InterruptedException, SignatureException {
|
2016-03-05 09:33:07 -05:00
|
|
|
DummyRegistryCallBackHandler<Boolean> handler = new DummyRegistryCallBackHandler<>();
|
|
|
|
String id = generateString();
|
|
|
|
VoterID voterInfo = VoterID.newBuilder().setId(id).build();
|
|
|
|
|
2016-03-11 09:02:09 -05:00
|
|
|
AsyncRegistry registry = GetRegistry();
|
2016-03-05 09:33:07 -05:00
|
|
|
registry.setVoted(voterInfo, handler);
|
|
|
|
jobSemaphore.acquire();
|
|
|
|
assertEquals("The callback handler hasn't been called yet", 1, handler.counter);
|
|
|
|
DummyRegistryCallBackHandler<Boolean> personalHandler = new DummyRegistryCallBackHandler<>();
|
|
|
|
registry.hasVoted(VoterID.newBuilder().setId(id).build(), personalHandler);
|
|
|
|
|
|
|
|
jobSemaphore.acquire(1);
|
|
|
|
assertTrue("The voter hasn't voted yet.", personalHandler.data);
|
|
|
|
}
|
2016-01-16 09:57:12 -05:00
|
|
|
}
|
2016-02-26 08:52:03 -05:00
|
|
|
|