source: exampleparties/humangui/src/test/java/geniusweb/exampleparties/humangui/BiddingInfoTest.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: 5.4 KB
Line 
1package geniusweb.exampleparties.humangui;
2
3import static org.junit.Assert.assertEquals;
4import static org.mockito.Matchers.any;
5import static org.mockito.Matchers.eq;
6import static org.mockito.Mockito.mock;
7import static org.mockito.Mockito.times;
8import static org.mockito.Mockito.verify;
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.Arrays;
17import java.util.Collections;
18import java.util.logging.Level;
19
20import javax.websocket.DeploymentException;
21
22import org.junit.Before;
23import org.junit.Test;
24
25import com.fasterxml.jackson.core.JsonParseException;
26import com.fasterxml.jackson.databind.JsonMappingException;
27import com.fasterxml.jackson.databind.ObjectMapper;
28
29import geniusweb.actions.Accept;
30import geniusweb.actions.PartyId;
31import geniusweb.inform.Settings;
32import geniusweb.issuevalue.Bid;
33import geniusweb.profile.Profile;
34import geniusweb.profile.utilityspace.LinearAdditive;
35import geniusweb.profileconnection.ProfileConnectionFactory;
36import geniusweb.profileconnection.ProfileInterface;
37import geniusweb.progress.ProgressRounds;
38import geniusweb.references.Parameters;
39import geniusweb.references.ProfileRef;
40import geniusweb.references.ProtocolRef;
41import tudelft.utilities.listener.DefaultListenable;
42import tudelft.utilities.listener.Listener;
43import 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 */
50public 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
156class 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}
Note: See TracBrowser for help on using the repository browser.