1 | package geniusweb.protocol.tournament.allpermutations;
|
---|
2 |
|
---|
3 | import static org.junit.Assert.assertEquals;
|
---|
4 | import static org.junit.Assert.assertFalse;
|
---|
5 | import static org.junit.Assert.assertTrue;
|
---|
6 | import static org.mockito.Mockito.mock;
|
---|
7 | import static org.mockito.Mockito.when;
|
---|
8 |
|
---|
9 | import java.math.BigInteger;
|
---|
10 | import java.util.ArrayList;
|
---|
11 | import java.util.Arrays;
|
---|
12 | import java.util.List;
|
---|
13 | import java.util.Map;
|
---|
14 |
|
---|
15 | import org.junit.Test;
|
---|
16 |
|
---|
17 | import geniusweb.actions.PartyId;
|
---|
18 | import geniusweb.protocol.session.SessionResult;
|
---|
19 | import geniusweb.protocol.session.SessionSettings;
|
---|
20 | import geniusweb.references.PartyWithProfile;
|
---|
21 | import tudelft.utilities.immutablelist.ImmutableList;
|
---|
22 |
|
---|
23 | public class AllPermutationsStateTest {
|
---|
24 |
|
---|
25 | private final AllPermutationsSettings toursettings = mock(
|
---|
26 | AllPermutationsSettings.class);;
|
---|
27 | @SuppressWarnings("unchecked")
|
---|
28 | private final List<SessionResult> results = mock(List.class);
|
---|
29 | @SuppressWarnings("unchecked")
|
---|
30 | private final ImmutableList<SessionSettings> settings = mock(
|
---|
31 | ImmutableList.class);
|
---|
32 | private final AllPermutationsState state = new AllPermutationsState(
|
---|
33 | toursettings, settings, results);
|
---|
34 | private final SessionSettings session7settings = mock(
|
---|
35 | SessionSettings.class);
|
---|
36 | private final long NOW = 1000;
|
---|
37 | private PartyWithProfile pp1 = mock(PartyWithProfile.class);
|
---|
38 | private PartyWithProfile pp2 = mock(PartyWithProfile.class);
|
---|
39 |
|
---|
40 | @SuppressWarnings("unchecked")
|
---|
41 | private final List<PartyWithProfile> participants = Arrays.asList(pp1, pp2);
|
---|
42 |
|
---|
43 | @Test
|
---|
44 | public void getResultsTest() {
|
---|
45 | assertEquals(results, state.getResults());
|
---|
46 | }
|
---|
47 |
|
---|
48 | @Test
|
---|
49 | public void isFinalTestFalse() {
|
---|
50 | when(settings.size()).thenReturn(BigInteger.TEN);
|
---|
51 | when(results.size()).thenReturn(11);
|
---|
52 | assertFalse(state.isFinal(NOW));
|
---|
53 | }
|
---|
54 |
|
---|
55 | @Test
|
---|
56 | public void isFinalTestTrue() {
|
---|
57 | when(settings.size()).thenReturn(BigInteger.TEN);
|
---|
58 | when(results.size()).thenReturn(10);
|
---|
59 | assertTrue(state.isFinal(NOW));
|
---|
60 | }
|
---|
61 |
|
---|
62 | @Test
|
---|
63 | public void getNextSetting() {
|
---|
64 | prepareNextSettings();
|
---|
65 | assertEquals(session7settings, state.getNextSettings());
|
---|
66 | }
|
---|
67 |
|
---|
68 | private void prepareNextSettings() {
|
---|
69 | when(results.size()).thenReturn(7); // result 0..6
|
---|
70 | when(settings.size()).thenReturn(BigInteger.TEN);
|
---|
71 | // when(session7settings.getTeams()).thenReturn(participants);
|
---|
72 | when(session7settings.getAllParties()).thenReturn(participants);
|
---|
73 | when(settings.get(BigInteger.valueOf(7))).thenReturn(session7settings);
|
---|
74 |
|
---|
75 | }
|
---|
76 |
|
---|
77 | @Test
|
---|
78 | public void withTest() {
|
---|
79 | prepareNextSettings();
|
---|
80 | ArrayList<SessionResult> results1 = new ArrayList<>();
|
---|
81 | for (int n = 0; n < 7; n++) {
|
---|
82 | results1.add(mock(SessionResult.class));
|
---|
83 | }
|
---|
84 | // use state with "real" results... to allow array copy in with
|
---|
85 | AllPermutationsState state = new AllPermutationsState(toursettings,
|
---|
86 | settings, results1);
|
---|
87 |
|
---|
88 | when(settings.size()).thenReturn(BigInteger.TEN);
|
---|
89 | when(settings.get(BigInteger.valueOf(7))).thenReturn(session7settings);
|
---|
90 |
|
---|
91 | SessionResult result = mock(SessionResult.class);
|
---|
92 |
|
---|
93 | Map<PartyId, PartyWithProfile> participants3 = mock(Map.class);
|
---|
94 | when(participants3.values()).thenReturn(participants);
|
---|
95 | when(result.getParticipants()).thenReturn(participants3);
|
---|
96 | AllPermutationsState state1 = state.with(result);
|
---|
97 |
|
---|
98 | assertEquals(8, state1.getResults().size());
|
---|
99 | assertEquals(toursettings, state1.getSettings());
|
---|
100 |
|
---|
101 | }
|
---|
102 | }
|
---|