source: exampleparties/humangui/src/test/java/geniusweb/exampleparties/humangui/HumanGuiTest.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.8 KB
Line 
1package geniusweb.exampleparties.humangui;
2
3import static org.junit.Assert.assertEquals;
4import static org.junit.Assert.assertFalse;
5import static org.junit.Assert.assertNotNull;
6import static org.junit.Assert.assertTrue;
7import static org.mockito.Mockito.mock;
8import static org.mockito.Mockito.when;
9
10import java.io.IOException;
11import java.net.URI;
12import java.net.URISyntaxException;
13import java.nio.charset.StandardCharsets;
14import java.nio.file.Files;
15import java.nio.file.Paths;
16import java.util.Random;
17
18import org.junit.After;
19import org.junit.Before;
20import org.junit.Test;
21
22import com.fasterxml.jackson.core.JsonParseException;
23import com.fasterxml.jackson.databind.JsonMappingException;
24import com.fasterxml.jackson.databind.ObjectMapper;
25
26import geniusweb.actions.EndNegotiation;
27import geniusweb.actions.Offer;
28import geniusweb.actions.PartyId;
29import geniusweb.bidspace.AllBidsList;
30import geniusweb.inform.ActionDone;
31import geniusweb.inform.Agreements;
32import geniusweb.inform.Finished;
33import geniusweb.inform.Settings;
34import geniusweb.inform.YourTurn;
35import geniusweb.issuevalue.Bid;
36import geniusweb.party.Capabilities;
37import geniusweb.profile.Profile;
38import geniusweb.profile.utilityspace.LinearAdditive;
39import geniusweb.progress.ProgressRounds;
40import geniusweb.references.Parameters;
41import geniusweb.references.ProfileRef;
42import 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 */
49public 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}
Note: See TracBrowser for help on using the repository browser.