source: exampleparties/comparebids/src/test/java/geniusweb/exampleparties/comparebids/CompareBidsTest.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.7 KB
Line 
1package geniusweb.exampleparties.comparebids;
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;
8
9import java.io.IOException;
10import java.math.BigDecimal;
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.LinkedList;
17import java.util.List;
18
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.Action;
27import geniusweb.actions.EndNegotiation;
28import geniusweb.actions.PartyId;
29import geniusweb.bidspace.AllBidsList;
30import geniusweb.connection.ConnectionEnd;
31import geniusweb.inform.ActionDone;
32import geniusweb.inform.Inform;
33import geniusweb.inform.Settings;
34import geniusweb.issuevalue.Bid;
35import geniusweb.party.Capabilities;
36import geniusweb.profile.Profile;
37import geniusweb.profile.utilityspace.LinearAdditive;
38import geniusweb.progress.ProgressRounds;
39import geniusweb.references.Parameters;
40import geniusweb.references.ProfileRef;
41import geniusweb.references.ProtocolRef;
42import geniusweb.references.Reference;
43import tudelft.utilities.listener.DefaultListenable;
44
45public class CompareBidsTest {
46
47 private static final String COB = "COB";
48 private static final PartyId otherparty = new PartyId("other");
49 private static final String PROFILE = "src/test/resources/testprofile.json";
50 private final static ObjectMapper jackson = new ObjectMapper();
51
52 private CompareBids party;
53 private final TestConnection connection = new TestConnection();
54 private final ProtocolRef protocol = mock(ProtocolRef.class);
55 private final ProgressRounds progress = mock(ProgressRounds.class);
56 private Settings settings;
57 private LinearAdditive profile;
58 private final Parameters parameters = new Parameters();
59
60 @Before
61 public void before() throws JsonParseException, JsonMappingException,
62 IOException, URISyntaxException {
63 party = new CompareBids();
64 settings = new Settings(new PartyId("party1"),
65 new ProfileRef(new URI("file:" + PROFILE)), protocol, progress,
66 parameters);
67
68 String serialized = new String(Files.readAllBytes(Paths.get(PROFILE)),
69 StandardCharsets.UTF_8);
70 profile = (LinearAdditive) jackson.readValue(serialized, Profile.class);
71
72 }
73
74 @Test
75 public void smokeTest() {
76 }
77
78 @Test
79 public void getDescriptionTest() {
80 assertNotNull(party.getDescription());
81 }
82
83 @Test
84 public void getCapabilitiesTest() {
85 Capabilities capabilities = party.getCapabilities();
86 assertFalse("party does not define protocols",
87 capabilities.getBehaviours().isEmpty());
88 }
89
90 @Test
91 public void testInformConnection() {
92 party.connect(connection);
93 // agent should not start acting just after an inform
94 assertEquals(0, connection.getActions().size());
95 }
96
97 @Test
98 public void testInformSettings() {
99 party.connect(connection);
100 connection.notifyListeners(settings);
101 assertEquals(0, connection.getActions().size());
102 }
103
104 @Test
105 public void testInformAndConnection() {
106 party.connect(connection);
107 party.notifyChange(settings);
108 assertEquals(0, connection.getActions().size());
109 }
110
111 @Test
112 public void testOtherWalksAway() {
113 party.connect(connection);
114 party.notifyChange(settings);
115
116 party.notifyChange(new ActionDone(new EndNegotiation(otherparty)));
117
118 // party should not act at this point
119 assertEquals(0, connection.getActions().size());
120 }
121
122 @Test
123 public void testGetCapabilities() {
124 assertTrue(party.getCapabilities().getBehaviours().contains(COB));
125 }
126
127 private Bid findGoodBid() {
128 for (Bid bid : new AllBidsList(profile.getDomain())) {
129 if (profile.getUtility(bid)
130 .compareTo(BigDecimal.valueOf(0.7)) > 0) {
131 return bid;
132 }
133 }
134 throw new IllegalStateException(
135 "Test can not be done: there is no good bid with utility>0.7");
136 }
137
138}
139
140/**
141 * A "real" connection object, because the party is going to subscribe etc, and
142 * without a real connection we would have to do a lot of mocks that would make
143 * the test very hard to read.
144 *
145 */
146class TestConnection extends DefaultListenable<Inform>
147 implements ConnectionEnd<Inform, Action> {
148 private List<Action> actions = new LinkedList<>();
149
150 @Override
151 public void send(Action action) throws IOException {
152 actions.add(action);
153 }
154
155 @Override
156 public Reference getReference() {
157 return null;
158 }
159
160 @Override
161 public URI getRemoteURI() {
162 return null;
163 }
164
165 @Override
166 public void close() {
167
168 }
169
170 @Override
171 public Error getError() {
172 return null;
173 }
174
175 public List<Action> getActions() {
176 return actions;
177 }
178
179}
Note: See TracBrowser for help on using the repository browser.