[48] | 1 | package geniusweb.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.Collections;
|
---|
| 10 | import java.util.HashSet;
|
---|
| 11 | import java.util.List;
|
---|
| 12 |
|
---|
| 13 | import org.junit.Test;
|
---|
| 14 |
|
---|
| 15 | import com.fasterxml.jackson.core.JsonProcessingException;
|
---|
| 16 | import com.fasterxml.jackson.databind.ObjectMapper;
|
---|
| 17 |
|
---|
| 18 | import geniusweb.issuevalue.Bid;
|
---|
| 19 | import geniusweb.issuevalue.DiscreteValue;
|
---|
| 20 | import tudelft.utilities.junit.GeneralTests;
|
---|
| 21 |
|
---|
| 22 | public class VotesTest extends GeneralTests<Votes> {
|
---|
| 23 | private final ObjectMapper jackson = new ObjectMapper();
|
---|
| 24 | private final String votestring = "{\"Votes\":{\"actor\":\"partyA\",\"votes\":[{\"Vote\":{\"actor\":\"partyA\",\"bid\":{\"issuevalues\":{\"is1\":\"val1\"}},\"minPower\":2,\"maxPower\":9}},{\"Vote\":{\"actor\":\"partyA\",\"bid\":{\"issuevalues\":{\"is1\":\"val2\"}},\"minPower\":2,\"maxPower\":9}}]}}";
|
---|
| 25 |
|
---|
| 26 | private PartyId partyA = new PartyId("partyA");
|
---|
| 27 | private PartyId partyB = new PartyId("partyB");
|
---|
| 28 | private Bid bid1 = new Bid("is1", new DiscreteValue("val1")),
|
---|
| 29 | bid2 = new Bid("is1", new DiscreteValue("val2"));
|
---|
| 30 | private Vote voteA1 = new Vote(partyA, bid1, 2, 9);
|
---|
| 31 | private Vote voteA2 = new Vote(partyA, bid2, 2, 9);
|
---|
| 32 | private Vote voteB1 = new Vote(partyB, bid1, 2, 9);
|
---|
| 33 | private Vote voteB2 = new Vote(partyB, bid2, 2, 9);
|
---|
| 34 |
|
---|
| 35 | private Votes votes1 = new Votes(partyA,
|
---|
| 36 | new HashSet<>(Arrays.asList(voteA1, voteA2)));
|
---|
| 37 | private Votes votes1a = new Votes(partyA,
|
---|
| 38 | new HashSet<>(Arrays.asList(voteA1, voteA2)));
|
---|
| 39 | private Votes votes2 = new Votes(partyB,
|
---|
| 40 | new HashSet<>(Arrays.asList(voteB1, voteB2)));
|
---|
| 41 |
|
---|
| 42 | @Override
|
---|
| 43 | public List<List<Votes>> getGeneralTestData() {
|
---|
| 44 | return Arrays.asList(Arrays.asList(votes1, votes1a),
|
---|
| 45 | Arrays.asList(votes2));
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | @Override
|
---|
| 49 | public List<String> getGeneralTestStrings() {
|
---|
| 50 | return Arrays.asList(
|
---|
| 51 | "Votes.*Vote.*partyA,Bid.*is1.*val1.*2.*Vote.*partyA,Bid.*is1.*val2.*2.*",
|
---|
| 52 | "Votes.*Vote.*partyB,Bid.*is1.*val2.*2.*Vote.*partyB,Bid.*is1.*val1.*2.*");
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | @SuppressWarnings("unused")
|
---|
| 56 | @Test(expected = IllegalArgumentException.class)
|
---|
| 57 | public void testRejectVoteWithIncorrectParty() {
|
---|
| 58 | new Votes(partyA, Collections.singleton(voteB1));
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | @Test
|
---|
| 62 | public void serializeTest() throws JsonProcessingException {
|
---|
| 63 | System.out.println(jackson.writeValueAsString(votes1));
|
---|
| 64 | assertEquals(votestring, jackson.writeValueAsString(votes1));
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | @Test
|
---|
| 68 | public void deserializeTest() throws IOException {
|
---|
| 69 | Action act = jackson.readValue(votestring, Action.class);
|
---|
| 70 | assertEquals(votes1, act);
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | @Test(expected = IllegalArgumentException.class)
|
---|
| 74 | public void testWrongPartyName() {
|
---|
| 75 | new Votes(partyA, new HashSet<>(Arrays.asList(voteB1, voteB2)));
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | @Test
|
---|
| 79 | public void isExtendingTestExtraVote() {
|
---|
| 80 | Votes otherVotes = new Votes(partyA, Collections.singleton(voteA1));
|
---|
| 81 | assertTrue(votes1.isExtending(otherVotes));
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | @Test
|
---|
| 85 | public void isExtendingTestNoVotes() {
|
---|
| 86 | Votes otherVotes = new Votes(partyA, Collections.emptySet());
|
---|
| 87 | assertTrue(votes1.isExtending(otherVotes));
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | @Test
|
---|
| 91 | public void isExtendingTestMissing() {
|
---|
| 92 | Votes otherVotes = new Votes(partyA, Collections.singleton(voteA1));
|
---|
| 93 | assertFalse(otherVotes.isExtending(votes1));
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | @Test
|
---|
| 97 | public void isExtendingIdenticalVotes() {
|
---|
| 98 | assertTrue(votes1.isExtending(votes1));
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | @Test
|
---|
| 102 | public void isReallyExtendingVotesMin() {
|
---|
| 103 | Vote powervoteA1 = new Vote(partyA, bid1, 1, 9);
|
---|
| 104 | Votes powerVotes = new Votes(partyA,
|
---|
| 105 | new HashSet<>(Arrays.asList(powervoteA1, voteA2)));
|
---|
| 106 | assertTrue(powerVotes.isExtending(votes1));
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | @Test
|
---|
| 110 | public void isReallyExtendingVotesMax() {
|
---|
| 111 | Vote powervoteA1 = new Vote(partyA, bid1, 2, 10);
|
---|
| 112 | Votes powerVotes = new Votes(partyA,
|
---|
| 113 | new HashSet<>(Arrays.asList(powervoteA1, voteA2)));
|
---|
| 114 | assertTrue(powerVotes.isExtending(votes1));
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | @Test
|
---|
| 118 | public void IsNarrowingOneVoteRemoved() {
|
---|
| 119 | Votes narrowVotes = new Votes(partyA, Collections.singleton(voteA2));
|
---|
| 120 | assertFalse(narrowVotes.isExtending(votes1));
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | @Test
|
---|
| 124 | public void isNarrowingTestMoreMinPower() {
|
---|
| 125 | Vote narrowVoteA1 = new Vote(partyA, bid1, 3, 9);
|
---|
| 126 | Votes narrowVotes = new Votes(partyA,
|
---|
| 127 | new HashSet<>(Arrays.asList(narrowVoteA1, voteA2)));
|
---|
| 128 | assertFalse(narrowVotes.isExtending(votes1));
|
---|
| 129 | assertTrue(votes1.isExtending(narrowVotes));
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | @Test
|
---|
| 133 | public void isNarrowingLessMaxPower() {
|
---|
| 134 | Vote powervoteA1 = new Vote(partyA, bid1, 2, 8);
|
---|
| 135 | Votes narrowVotes = new Votes(partyA,
|
---|
| 136 | new HashSet<>(Arrays.asList(powervoteA1, voteA2)));
|
---|
| 137 | assertFalse(narrowVotes.isExtending(votes1));
|
---|
| 138 | assertTrue(votes1.isExtending(narrowVotes));
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | @Test(expected = IllegalArgumentException.class)
|
---|
| 142 | public void testDuplicateBidsNotAllowed() {
|
---|
| 143 | Vote voteA1similar = new Vote(partyA, bid1, 3, 9);
|
---|
| 144 | Votes votes = new Votes(partyA,
|
---|
| 145 | new HashSet<>(Arrays.asList(voteA1, voteA1similar)));
|
---|
| 146 | }
|
---|
| 147 | }
|
---|