source: protocol/src/test/java/geniusweb/protocol/tournament/allpermutations/AllPermutationsStateTest.java@ 52

Last change on this file since 52 was 52, checked in by ruud, 14 months ago

Fixed small issues in domaineditor.

File size: 4.3 KB
Line 
1package geniusweb.protocol.tournament.allpermutations;
2
3import static org.junit.Assert.assertEquals;
4import static org.junit.Assert.assertFalse;
5import static org.junit.Assert.assertTrue;
6import static org.mockito.Mockito.mock;
7import static org.mockito.Mockito.when;
8
9import java.io.File;
10import java.io.IOException;
11import java.math.BigInteger;
12import java.net.URISyntaxException;
13import java.util.ArrayList;
14import java.util.Arrays;
15import java.util.List;
16import java.util.Map;
17
18import org.junit.Before;
19import org.junit.Test;
20
21import com.fasterxml.jackson.databind.ObjectMapper;
22
23import geniusweb.actions.PartyId;
24import geniusweb.protocol.NegoState;
25import geniusweb.protocol.session.SessionResult;
26import geniusweb.protocol.session.SessionSettings;
27import geniusweb.references.PartyWithProfile;
28import tudelft.utilities.immutablelist.ImmutableList;
29
30public 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}
Note: See TracBrowser for help on using the repository browser.