[21] | 1 | package actions;
|
---|
| 2 |
|
---|
| 3 | import static org.junit.Assert.assertEquals;
|
---|
| 4 | import static org.junit.Assert.assertFalse;
|
---|
| 5 | import static org.junit.Assert.assertTrue;
|
---|
| 6 |
|
---|
| 7 | import java.io.IOException;
|
---|
| 8 | import java.util.Arrays;
|
---|
| 9 | import java.util.List;
|
---|
| 10 |
|
---|
| 11 | import org.junit.Test;
|
---|
| 12 |
|
---|
| 13 | import com.fasterxml.jackson.core.JsonProcessingException;
|
---|
| 14 | import com.fasterxml.jackson.databind.ObjectMapper;
|
---|
| 15 |
|
---|
| 16 | import geniusweb.actions.Action;
|
---|
| 17 | import geniusweb.actions.PartyId;
|
---|
| 18 | import geniusweb.actions.Vote;
|
---|
| 19 | import geniusweb.actions.Votes;
|
---|
| 20 | import geniusweb.issuevalue.Bid;
|
---|
| 21 | import geniusweb.issuevalue.DiscreteValue;
|
---|
| 22 | import tudelft.utilities.junit.GeneralTests;
|
---|
| 23 |
|
---|
| 24 | public class VotesTest extends GeneralTests<Votes> {
|
---|
| 25 | private final ObjectMapper jackson = new ObjectMapper();
|
---|
| 26 | private final String votestring = "{\"Votes\":{\"actor\":\"partyA\",\"votes\":[{\"vote\":{\"actor\":\"partyA\",\"bid\":{\"issuevalues\":{\"is1\":\"val1\"}},\"minPower\":2}},{\"vote\":{\"actor\":\"partyA\",\"bid\":{\"issuevalues\":{\"is1\":\"val2\"}},\"minPower\":2}}]}}";
|
---|
| 27 |
|
---|
| 28 | private PartyId partyA = new PartyId("partyA");
|
---|
| 29 | private PartyId partyB = new PartyId("partyB");
|
---|
| 30 | private Bid bid1 = new Bid("is1", new DiscreteValue("val1")),
|
---|
| 31 | bid2 = new Bid("is1", new DiscreteValue("val2"));
|
---|
| 32 | private Vote voteA1 = new Vote(partyA, bid1, 2);
|
---|
| 33 | private Vote voteA2 = new Vote(partyA, bid2, 2);
|
---|
| 34 | private Vote voteB1 = new Vote(partyB, bid1, 2);
|
---|
| 35 | private Vote voteB2 = new Vote(partyB, bid2, 2);
|
---|
| 36 |
|
---|
| 37 | private Votes votes1 = new Votes(partyA, Arrays.asList(voteA1, voteA2));
|
---|
| 38 | private Votes votes1a = new Votes(partyA, Arrays.asList(voteA1, voteA2));
|
---|
| 39 | private Votes votes2 = new Votes(partyB, Arrays.asList(voteB1, voteB2));
|
---|
| 40 |
|
---|
| 41 | @Override
|
---|
| 42 | public List<List<Votes>> getGeneralTestData() {
|
---|
| 43 | return Arrays.asList(Arrays.asList(votes1, votes1a),
|
---|
| 44 | Arrays.asList(votes2));
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | @Override
|
---|
| 48 | public List<String> getGeneralTestStrings() {
|
---|
| 49 | return Arrays.asList(
|
---|
| 50 | "Votes.*Vote.*partyA,Bid.*is1.*val1.*2.*Vote.*partyA,Bid.*is1.*val2.*2.*",
|
---|
| 51 | "Votes.*Vote.*partyB,Bid.*is1.*val1.*2.*Vote.*partyB,Bid.*is1.*val2.*2.*");
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | @SuppressWarnings("unused")
|
---|
| 55 | @Test(expected = IllegalArgumentException.class)
|
---|
| 56 | public void testRejectVoteWithIncorrectParty() {
|
---|
| 57 | new Votes(partyA, Arrays.asList(voteB1));
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | @Test
|
---|
| 61 | public void serializeTest() throws JsonProcessingException {
|
---|
| 62 | System.out.println(jackson.writeValueAsString(votes1));
|
---|
| 63 | assertEquals(votestring, jackson.writeValueAsString(votes1));
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | @Test
|
---|
| 67 | public void deserializeTest() throws IOException {
|
---|
| 68 | Action act = jackson.readValue(votestring, Action.class);
|
---|
| 69 | assertEquals(votes1, act);
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | @Test(expected = IllegalArgumentException.class)
|
---|
| 73 | public void testWrongPartyName() {
|
---|
| 74 | new Votes(partyA, Arrays.asList(voteB1, voteB2));
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | @Test
|
---|
| 78 | public void isExtendingTestExtraVote() {
|
---|
| 79 | Votes otherVotes = new Votes(partyA, Arrays.asList(voteA1));
|
---|
| 80 | assertTrue(votes1.isExtending(otherVotes));
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | @Test
|
---|
| 84 | public void isExtendingTestNoVotes() {
|
---|
| 85 | Votes otherVotes = new Votes(partyA, Arrays.asList());
|
---|
| 86 | assertTrue(votes1.isExtending(otherVotes));
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | @Test
|
---|
| 90 | public void isExtendingTestMissing() {
|
---|
| 91 | Votes otherVotes = new Votes(partyA, Arrays.asList(voteA1));
|
---|
| 92 | assertFalse(otherVotes.isExtending(votes1));
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | @Test
|
---|
| 96 | public void isExtendingTestLessPower() {
|
---|
| 97 | Vote powervoteA1 = new Vote(partyA, bid1, 3);
|
---|
| 98 | Votes powerVotes = new Votes(partyA, Arrays.asList(powervoteA1));
|
---|
| 99 | assertFalse(powerVotes.isExtending(votes1));
|
---|
| 100 | assertTrue(votes1.isExtending(powerVotes));
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | @Test(expected = IllegalArgumentException.class)
|
---|
| 104 | public void testDuplicateBidsNotAllowed() {
|
---|
| 105 | Vote voteA1similar = new Vote(partyA, bid1, 3);
|
---|
| 106 | Votes votes = new Votes(partyA, Arrays.asList(voteA1, voteA1similar));
|
---|
| 107 | }
|
---|
| 108 | }
|
---|