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.io.File;
|
---|
10 | import java.io.IOException;
|
---|
11 | import java.math.BigInteger;
|
---|
12 | import java.net.URISyntaxException;
|
---|
13 | import java.util.ArrayList;
|
---|
14 | import java.util.Arrays;
|
---|
15 | import java.util.List;
|
---|
16 | import java.util.Map;
|
---|
17 |
|
---|
18 | import org.junit.Before;
|
---|
19 | import org.junit.Test;
|
---|
20 |
|
---|
21 | import com.fasterxml.jackson.databind.ObjectMapper;
|
---|
22 |
|
---|
23 | import geniusweb.actions.PartyId;
|
---|
24 | import geniusweb.protocol.NegoState;
|
---|
25 | import geniusweb.protocol.session.SessionResult;
|
---|
26 | import geniusweb.protocol.session.SessionSettings;
|
---|
27 | import geniusweb.references.PartyWithProfile;
|
---|
28 | import tudelft.utilities.immutablelist.ImmutableList;
|
---|
29 |
|
---|
30 | public class AllPermutationsStateTest {
|
---|
31 | private final static ObjectMapper jackson = new ObjectMapper();
|
---|
32 | private final AllPermutationsSettings toursettings = mock(
|
---|
33 | AllPermutationsSettings.class);;
|
---|
34 | @SuppressWarnings("unchecked")
|
---|
35 | private final List<SessionResult> results = mock(List.class);
|
---|
36 | @SuppressWarnings("unchecked")
|
---|
37 | private final ImmutableList<SessionSettings> settings = mock(
|
---|
38 | ImmutableList.class);
|
---|
39 | private final AllPermutationsState state = new AllPermutationsState(
|
---|
40 | toursettings, results);
|
---|
41 | private final SessionSettings session7settings = mock(
|
---|
42 | SessionSettings.class);
|
---|
43 | private final long NOW = 1000;
|
---|
44 | private PartyWithProfile pp1 = mock(PartyWithProfile.class);
|
---|
45 | private PartyWithProfile pp2 = mock(PartyWithProfile.class);
|
---|
46 |
|
---|
47 | @Before
|
---|
48 | public void before() {
|
---|
49 | when(toursettings.permutations()).thenReturn(settings);
|
---|
50 | }
|
---|
51 |
|
---|
52 | @SuppressWarnings("unchecked")
|
---|
53 | private final List<PartyWithProfile> participants = Arrays.asList(pp1, pp2);
|
---|
54 |
|
---|
55 | @Test
|
---|
56 | public void getResultsTest() {
|
---|
57 | assertEquals(results, state.getResults());
|
---|
58 | }
|
---|
59 |
|
---|
60 | @Test
|
---|
61 | public void isFinalTestFalse() {
|
---|
62 | when(settings.size()).thenReturn(BigInteger.TEN);
|
---|
63 | when(results.size()).thenReturn(11);
|
---|
64 | assertFalse(state.isFinal(NOW));
|
---|
65 | }
|
---|
66 |
|
---|
67 | @Test
|
---|
68 | public void isFinalTestTrue() {
|
---|
69 | when(settings.size()).thenReturn(BigInteger.TEN);
|
---|
70 | when(results.size()).thenReturn(10);
|
---|
71 | assertTrue(state.isFinal(NOW));
|
---|
72 | }
|
---|
73 |
|
---|
74 | @Test
|
---|
75 | public void getNextSetting() {
|
---|
76 | prepareNextSettings();
|
---|
77 | assertEquals(session7settings, state.getNextSettings());
|
---|
78 | }
|
---|
79 |
|
---|
80 | private void prepareNextSettings() {
|
---|
81 | when(results.size()).thenReturn(7); // result 0..6
|
---|
82 | when(settings.size()).thenReturn(BigInteger.TEN);
|
---|
83 | // when(session7settings.getTeams()).thenReturn(participants);
|
---|
84 | when(session7settings.getAllParties()).thenReturn(participants);
|
---|
85 | when(settings.get(BigInteger.valueOf(7))).thenReturn(session7settings);
|
---|
86 |
|
---|
87 | }
|
---|
88 |
|
---|
89 | @Test
|
---|
90 | public void withTest() {
|
---|
91 | prepareNextSettings();
|
---|
92 | ArrayList<SessionResult> results1 = new ArrayList<>();
|
---|
93 | for (int n = 0; n < 7; n++) {
|
---|
94 | results1.add(mock(SessionResult.class));
|
---|
95 | }
|
---|
96 | // use state with "real" results... to allow array copy in with
|
---|
97 | AllPermutationsState state = new AllPermutationsState(toursettings,
|
---|
98 | results1);
|
---|
99 |
|
---|
100 | when(settings.size()).thenReturn(BigInteger.TEN);
|
---|
101 | when(settings.get(BigInteger.valueOf(7))).thenReturn(session7settings);
|
---|
102 |
|
---|
103 | SessionResult result = mock(SessionResult.class);
|
---|
104 |
|
---|
105 | Map<PartyId, PartyWithProfile> participants3 = mock(Map.class);
|
---|
106 | when(participants3.values()).thenReturn(participants);
|
---|
107 | when(result.getParticipants()).thenReturn(participants3);
|
---|
108 | AllPermutationsState state1 = state.with(Arrays.asList(result));
|
---|
109 |
|
---|
110 | assertEquals(8, state1.getResults().size());
|
---|
111 | assertEquals(toursettings, state1.getSettings());
|
---|
112 |
|
---|
113 | }
|
---|
114 |
|
---|
115 | @Test
|
---|
116 | public void testDeserialize() throws IOException {
|
---|
117 | NegoState state = jackson.readValue(
|
---|
118 | new File("src/test/resources/APP1646925947841.json"),
|
---|
119 | NegoState.class);
|
---|
120 | assertTrue(state instanceof AllPermutationsState);
|
---|
121 | AllPermutationsState negostate = (AllPermutationsState) state;
|
---|
122 |
|
---|
123 | System.out.println(negostate);
|
---|
124 | // all sessions should be done so time irrelevant
|
---|
125 | assertTrue(negostate.isFinal(0));
|
---|
126 | assertEquals(1, negostate.getSettings().getNrTournaments());
|
---|
127 | }
|
---|
128 |
|
---|
129 | @Test
|
---|
130 | public void testSerialize() throws URISyntaxException, IOException {
|
---|
131 | NegoState obj = jackson.readValue(
|
---|
132 | new File("src/test/resources/APP1646925947841.json"),
|
---|
133 | NegoState.class);
|
---|
134 | String string = jackson.writeValueAsString(obj);
|
---|
135 | // just smoke test, FIXME really check the output.
|
---|
136 | }
|
---|
137 | }
|
---|