1 | package geniusweb.exampleparties.humangui;
|
---|
2 |
|
---|
3 | import static org.junit.Assert.assertEquals;
|
---|
4 | import static org.junit.Assert.assertFalse;
|
---|
5 | import static org.junit.Assert.assertNotNull;
|
---|
6 | import static org.junit.Assert.assertTrue;
|
---|
7 | import static org.mockito.Mockito.mock;
|
---|
8 | import static org.mockito.Mockito.when;
|
---|
9 |
|
---|
10 | import java.io.IOException;
|
---|
11 | import java.net.URI;
|
---|
12 | import java.net.URISyntaxException;
|
---|
13 | import java.nio.charset.StandardCharsets;
|
---|
14 | import java.nio.file.Files;
|
---|
15 | import java.nio.file.Paths;
|
---|
16 | import java.util.Random;
|
---|
17 |
|
---|
18 | import org.junit.After;
|
---|
19 | import org.junit.Before;
|
---|
20 | import org.junit.Test;
|
---|
21 |
|
---|
22 | import com.fasterxml.jackson.core.JsonParseException;
|
---|
23 | import com.fasterxml.jackson.databind.JsonMappingException;
|
---|
24 | import com.fasterxml.jackson.databind.ObjectMapper;
|
---|
25 |
|
---|
26 | import geniusweb.actions.EndNegotiation;
|
---|
27 | import geniusweb.actions.Offer;
|
---|
28 | import geniusweb.actions.PartyId;
|
---|
29 | import geniusweb.bidspace.AllBidsList;
|
---|
30 | import geniusweb.inform.ActionDone;
|
---|
31 | import geniusweb.inform.Agreements;
|
---|
32 | import geniusweb.inform.Finished;
|
---|
33 | import geniusweb.inform.Settings;
|
---|
34 | import geniusweb.inform.YourTurn;
|
---|
35 | import geniusweb.issuevalue.Bid;
|
---|
36 | import geniusweb.party.Capabilities;
|
---|
37 | import geniusweb.profile.Profile;
|
---|
38 | import geniusweb.profile.utilityspace.LinearAdditive;
|
---|
39 | import geniusweb.progress.ProgressRounds;
|
---|
40 | import geniusweb.references.Parameters;
|
---|
41 | import geniusweb.references.ProfileRef;
|
---|
42 | import geniusweb.references.ProtocolRef;
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Should test the GUI stuff. At this moment it's just a simple quick test of
|
---|
46 | * the GUI without much testing
|
---|
47 | *
|
---|
48 | */
|
---|
49 | public class HumanGuiTest {
|
---|
50 |
|
---|
51 | private static final String SAOP = "SAOP";
|
---|
52 | private static final PartyId otherparty = new PartyId("other");
|
---|
53 | private static final String PROFILE = "src/test/resources/testprofile.json";
|
---|
54 | private final static ObjectMapper jackson = new ObjectMapper();
|
---|
55 |
|
---|
56 | private HumanGui party;
|
---|
57 | private final TestConnection connection = new TestConnection();
|
---|
58 | private final ProtocolRef protocol = mock(ProtocolRef.class);
|
---|
59 | private final ProgressRounds progress = mock(ProgressRounds.class);
|
---|
60 | private Settings settings;
|
---|
61 | private LinearAdditive profile;
|
---|
62 | private final Parameters parameters = new Parameters();
|
---|
63 |
|
---|
64 | @Before
|
---|
65 | public void before() throws JsonParseException, JsonMappingException,
|
---|
66 | IOException, URISyntaxException {
|
---|
67 | party = new HumanGui();
|
---|
68 | settings = new Settings(new PartyId("party1"),
|
---|
69 | new ProfileRef(new URI("file:" + PROFILE)), protocol, progress,
|
---|
70 | parameters);
|
---|
71 |
|
---|
72 | String serialized = new String(Files.readAllBytes(Paths.get(PROFILE)),
|
---|
73 | StandardCharsets.UTF_8);
|
---|
74 | profile = (LinearAdditive) jackson.readValue(serialized, Profile.class);
|
---|
75 |
|
---|
76 | }
|
---|
77 |
|
---|
78 | @After
|
---|
79 | public void after() {
|
---|
80 | Agreements agreements = mock(Agreements.class);
|
---|
81 | when(agreements.toString()).thenReturn("agree");
|
---|
82 | party.notifyChange(new Finished(agreements));
|
---|
83 | }
|
---|
84 |
|
---|
85 | @Test
|
---|
86 | public void smokeTest() {
|
---|
87 | }
|
---|
88 |
|
---|
89 | @Test
|
---|
90 | public void getDescriptionTest() {
|
---|
91 | assertNotNull(party.getDescription());
|
---|
92 | }
|
---|
93 |
|
---|
94 | @Test
|
---|
95 | public void getCapabilitiesTest() {
|
---|
96 | Capabilities capabilities = party.getCapabilities();
|
---|
97 | assertFalse("party does not define protocols",
|
---|
98 | capabilities.getBehaviours().isEmpty());
|
---|
99 | }
|
---|
100 |
|
---|
101 | @Test
|
---|
102 | public void testGetCapabilities() {
|
---|
103 | assertTrue(party.getCapabilities().getBehaviours().contains(SAOP));
|
---|
104 | }
|
---|
105 |
|
---|
106 | @Test
|
---|
107 | public void testInformConnection() {
|
---|
108 | party.connect(connection);
|
---|
109 | // agent should not start acting just after an inform
|
---|
110 | assertEquals(0, connection.getActions().size());
|
---|
111 | }
|
---|
112 |
|
---|
113 | @Test
|
---|
114 | public void testInformSettings() throws InterruptedException {
|
---|
115 | party.connect(connection);
|
---|
116 | connection.notifyListeners(settings);
|
---|
117 | assertEquals(0, connection.getActions().size());
|
---|
118 | Thread.sleep(1000);
|
---|
119 | }
|
---|
120 |
|
---|
121 | @Test
|
---|
122 | public void testOtherWalksAway() throws InterruptedException {
|
---|
123 | party.connect(connection);
|
---|
124 | party.notifyChange(settings);
|
---|
125 |
|
---|
126 | party.notifyChange(new ActionDone(new EndNegotiation(otherparty)));
|
---|
127 |
|
---|
128 | // party should not act at this point
|
---|
129 | assertEquals(0, connection.getActions().size());
|
---|
130 |
|
---|
131 | Thread.sleep(1000);
|
---|
132 |
|
---|
133 | }
|
---|
134 |
|
---|
135 | @Test
|
---|
136 | public void testReceiveOtherBid() throws InterruptedException {
|
---|
137 | party.connect(connection);
|
---|
138 | party.notifyChange(settings);
|
---|
139 | Thread.sleep(1000);
|
---|
140 |
|
---|
141 | AllBidsList allbids = new AllBidsList(profile.getDomain());
|
---|
142 | Random r = new Random();
|
---|
143 | party.notifyChange(new ActionDone(new Offer(otherparty,
|
---|
144 | allbids.get(r.nextInt(allbids.size().intValue())))));
|
---|
145 |
|
---|
146 | // party should not act at this point
|
---|
147 | assertEquals(0, connection.getActions().size());
|
---|
148 |
|
---|
149 | Thread.sleep(1000);
|
---|
150 |
|
---|
151 | }
|
---|
152 |
|
---|
153 | @Test
|
---|
154 | public void testRecvAndYourTurn() throws InterruptedException {
|
---|
155 | party.connect(connection);
|
---|
156 | party.notifyChange(settings);
|
---|
157 |
|
---|
158 | party.notifyChange(new ActionDone(new Offer(otherparty, randomBid())));
|
---|
159 | party.notifyChange(new YourTurn());
|
---|
160 |
|
---|
161 | Thread.sleep(1000);
|
---|
162 |
|
---|
163 | }
|
---|
164 |
|
---|
165 | private Bid randomBid() {
|
---|
166 | AllBidsList allbids = new AllBidsList(profile.getDomain());
|
---|
167 | Random r = new Random();
|
---|
168 | return allbids.get(r.nextInt(allbids.size().intValue()));
|
---|
169 |
|
---|
170 | }
|
---|
171 |
|
---|
172 | }
|
---|