1 | package geniusweb.exampleparties.comparebids;
|
---|
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 |
|
---|
9 | import java.io.IOException;
|
---|
10 | import java.math.BigDecimal;
|
---|
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.LinkedList;
|
---|
17 | import java.util.List;
|
---|
18 |
|
---|
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.Action;
|
---|
27 | import geniusweb.actions.EndNegotiation;
|
---|
28 | import geniusweb.actions.PartyId;
|
---|
29 | import geniusweb.bidspace.AllBidsList;
|
---|
30 | import geniusweb.connection.ConnectionEnd;
|
---|
31 | import geniusweb.inform.ActionDone;
|
---|
32 | import geniusweb.inform.Inform;
|
---|
33 | import geniusweb.inform.Settings;
|
---|
34 | import geniusweb.issuevalue.Bid;
|
---|
35 | import geniusweb.party.Capabilities;
|
---|
36 | import geniusweb.profile.Profile;
|
---|
37 | import geniusweb.profile.utilityspace.LinearAdditive;
|
---|
38 | import geniusweb.progress.ProgressRounds;
|
---|
39 | import geniusweb.references.Parameters;
|
---|
40 | import geniusweb.references.ProfileRef;
|
---|
41 | import geniusweb.references.ProtocolRef;
|
---|
42 | import geniusweb.references.Reference;
|
---|
43 | import tudelft.utilities.listener.DefaultListenable;
|
---|
44 |
|
---|
45 | public 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 | */
|
---|
146 | class 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 | }
|
---|