1 | package geniusweb.exampleparties.humangui;
|
---|
2 |
|
---|
3 | import static org.junit.Assert.assertEquals;
|
---|
4 | import static org.mockito.Matchers.any;
|
---|
5 | import static org.mockito.Matchers.eq;
|
---|
6 | import static org.mockito.Mockito.mock;
|
---|
7 | import static org.mockito.Mockito.times;
|
---|
8 | import static org.mockito.Mockito.verify;
|
---|
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.Arrays;
|
---|
17 | import java.util.Collections;
|
---|
18 | import java.util.logging.Level;
|
---|
19 |
|
---|
20 | import javax.websocket.DeploymentException;
|
---|
21 |
|
---|
22 | import org.junit.Before;
|
---|
23 | import org.junit.Test;
|
---|
24 |
|
---|
25 | import com.fasterxml.jackson.core.JsonParseException;
|
---|
26 | import com.fasterxml.jackson.databind.JsonMappingException;
|
---|
27 | import com.fasterxml.jackson.databind.ObjectMapper;
|
---|
28 |
|
---|
29 | import geniusweb.actions.Accept;
|
---|
30 | import geniusweb.actions.PartyId;
|
---|
31 | import geniusweb.inform.Settings;
|
---|
32 | import geniusweb.issuevalue.Bid;
|
---|
33 | import geniusweb.profile.Profile;
|
---|
34 | import geniusweb.profile.utilityspace.LinearAdditive;
|
---|
35 | import geniusweb.profileconnection.ProfileConnectionFactory;
|
---|
36 | import geniusweb.profileconnection.ProfileInterface;
|
---|
37 | import geniusweb.progress.ProgressRounds;
|
---|
38 | import geniusweb.references.Parameters;
|
---|
39 | import geniusweb.references.ProfileRef;
|
---|
40 | import geniusweb.references.ProtocolRef;
|
---|
41 | import tudelft.utilities.listener.DefaultListenable;
|
---|
42 | import tudelft.utilities.listener.Listener;
|
---|
43 | import tudelft.utilities.logging.Reporter;
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Should test the GUI stuff. At this moment it's just a simple quick test of
|
---|
47 | * the GUI without much testing
|
---|
48 | *
|
---|
49 | */
|
---|
50 | public class BiddingInfoTest {
|
---|
51 |
|
---|
52 | private static final PartyId PARTY_ID = new PartyId("party1");
|
---|
53 | private static final ProtocolRef SAOP = new ProtocolRef("SAOP");
|
---|
54 | private static final PartyId otherparty = new PartyId("other");
|
---|
55 | private static final String PROFILE = "src/test/resources/testprofile.json";
|
---|
56 | private static final String PROFILE2 = "src/test/resources/testprofile2.json";
|
---|
57 | private final static ObjectMapper jackson = new ObjectMapper();
|
---|
58 |
|
---|
59 | private final TestConnection connection = new TestConnection();
|
---|
60 | private final ProtocolRef protocol = mock(ProtocolRef.class);
|
---|
61 | private final ProgressRounds progress = mock(ProgressRounds.class);
|
---|
62 | private Settings settings;
|
---|
63 | private LinearAdditive profile, profile2;
|
---|
64 | private final Parameters parameters = new Parameters();
|
---|
65 | private Reporter reporter = mock(Reporter.class);
|
---|
66 | private ProfileInterface profileint;
|
---|
67 | private BiddingInfo info;
|
---|
68 |
|
---|
69 | @Before
|
---|
70 | public void before() throws JsonParseException, JsonMappingException,
|
---|
71 | IOException, URISyntaxException, DeploymentException {
|
---|
72 | settings = new Settings(PARTY_ID,
|
---|
73 | new ProfileRef(new URI("file:" + PROFILE)), protocol, progress,
|
---|
74 | parameters);
|
---|
75 |
|
---|
76 | String serialized = new String(Files.readAllBytes(Paths.get(PROFILE)),
|
---|
77 | StandardCharsets.UTF_8);
|
---|
78 | profile = (LinearAdditive) jackson.readValue(serialized, Profile.class);
|
---|
79 | String serialized2 = new String(Files.readAllBytes(Paths.get(PROFILE2)),
|
---|
80 | StandardCharsets.UTF_8);
|
---|
81 | profile2 = (LinearAdditive) jackson.readValue(serialized2,
|
---|
82 | Profile.class);
|
---|
83 |
|
---|
84 | profileint = ProfileConnectionFactory
|
---|
85 | .create(settings.getProfile().getURI(), reporter);
|
---|
86 |
|
---|
87 | info = new BiddingInfo(settings, connection, reporter, profileint);
|
---|
88 | }
|
---|
89 |
|
---|
90 | @Test
|
---|
91 | public void smokeTest() {
|
---|
92 | assertEquals(profile, info.getProfile());
|
---|
93 | assertEquals(Arrays.asList("issue2", "issue1"), info.getIssues());
|
---|
94 | verify(reporter, times(0)).log(any(), any());
|
---|
95 |
|
---|
96 | }
|
---|
97 |
|
---|
98 | @Test
|
---|
99 | public void testUpdateProfile() throws IOException, DeploymentException {
|
---|
100 | TestProfileInterface profileInterface = new TestProfileInterface(
|
---|
101 | profile);
|
---|
102 | info = new BiddingInfo(settings, connection, reporter,
|
---|
103 | profileInterface);
|
---|
104 | @SuppressWarnings("unchecked")
|
---|
105 | Listener<Object> testlistener = mock(Listener.class);
|
---|
106 | info.addListener(testlistener);
|
---|
107 | assertEquals(profile, info.getProfile());
|
---|
108 |
|
---|
109 | // push new profile into the interface and check it's handled
|
---|
110 | profileInterface.updateProfile(profile2);
|
---|
111 | assertEquals(profile2, info.getProfile());
|
---|
112 | verify(testlistener, times(1)).notifyChange(profile2);
|
---|
113 | assertEquals(Arrays.asList("issue3", "issue2"), info.getIssues());
|
---|
114 | verify(reporter, times(0)).log(any(), any());
|
---|
115 |
|
---|
116 | }
|
---|
117 |
|
---|
118 | @Test
|
---|
119 | public void testActNotMyTurn() {
|
---|
120 | info.doAction(new Accept(PARTY_ID, new Bid(Collections.emptyMap())));
|
---|
121 | verify(reporter, times(1)).log(eq(Level.SEVERE), any());
|
---|
122 |
|
---|
123 | }
|
---|
124 |
|
---|
125 | @Test
|
---|
126 | public void testChangeTurn() {
|
---|
127 | Listener<Object> testlistener = mock(Listener.class);
|
---|
128 | info.addListener(testlistener);
|
---|
129 |
|
---|
130 | info.setMyTurn(true);
|
---|
131 | verify(testlistener, times(1)).notifyChange(eq(true));
|
---|
132 | }
|
---|
133 |
|
---|
134 | @Test
|
---|
135 | public void testActMyTurn() {
|
---|
136 | info.setMyTurn(true);
|
---|
137 | info.doAction(new Accept(PARTY_ID, new Bid(Collections.emptyMap())));
|
---|
138 | verify(reporter, times(0)).log(any(), any());
|
---|
139 |
|
---|
140 | }
|
---|
141 |
|
---|
142 | @Test
|
---|
143 | public void testSetCurrentBid() {
|
---|
144 | Listener<Object> testlistener = mock(Listener.class);
|
---|
145 | info.addListener(testlistener);
|
---|
146 |
|
---|
147 | Bid bid = mock(Bid.class);
|
---|
148 | info.setCurrentBid(bid);
|
---|
149 | assertEquals(bid, info.getCurrentBid());
|
---|
150 | verify(testlistener, times(1)).notifyChange(bid);
|
---|
151 |
|
---|
152 | }
|
---|
153 |
|
---|
154 | }
|
---|
155 |
|
---|
156 | class TestProfileInterface extends DefaultListenable<Profile>
|
---|
157 | implements ProfileInterface {
|
---|
158 |
|
---|
159 | private Profile profile;
|
---|
160 |
|
---|
161 | public TestProfileInterface(Profile profile) {
|
---|
162 | this.profile = profile;
|
---|
163 | }
|
---|
164 |
|
---|
165 | public void updateProfile(Profile profile) {
|
---|
166 | this.profile = profile;
|
---|
167 | notifyListeners(profile);
|
---|
168 | }
|
---|
169 |
|
---|
170 | @Override
|
---|
171 | public Profile getProfile() {
|
---|
172 | return profile;
|
---|
173 | }
|
---|
174 |
|
---|
175 | @Override
|
---|
176 | public void close() {
|
---|
177 | }
|
---|
178 |
|
---|
179 | }
|
---|