source: exampleparties/humangui/src/test/java/geniusweb/exampleparties/humangui/BiddingInfoTest.java@ 9

Last change on this file since 9 was 9, checked in by bart, 5 years ago

Release 1.1.0

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.issuevalue.Bid;
32import geniusweb.party.inform.Settings;
33import geniusweb.profile.Profile;
34import geniusweb.profile.utilityspace.LinearAdditiveUtilitySpace;
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 LinearAdditiveUtilitySpace 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 = (LinearAdditiveUtilitySpace) jackson.readValue(serialized,
79 Profile.class);
80 String serialized2 = new String(Files.readAllBytes(Paths.get(PROFILE2)),
81 StandardCharsets.UTF_8);
82 profile2 = (LinearAdditiveUtilitySpace) jackson.readValue(serialized2,
83 Profile.class);
84
85 profileint = ProfileConnectionFactory
86 .create(settings.getProfile().getURI(), reporter);
87
88 info = new BiddingInfo(settings, connection, reporter, profileint);
89 }
90
91 @Test
92 public void smokeTest() {
93 assertEquals(profile, info.getProfile());
94 assertEquals(Arrays.asList("issue2", "issue1"), info.getIssues());
95 verify(reporter, times(0)).log(any(), any());
96
97 }
98
99 @Test
100 public void testUpdateProfile() throws IOException, DeploymentException {
101 TestProfileInterface profileInterface = new TestProfileInterface(
102 profile);
103 info = new BiddingInfo(settings, connection, reporter,
104 profileInterface);
105 @SuppressWarnings("unchecked")
106 Listener<Object> testlistener = mock(Listener.class);
107 info.addListener(testlistener);
108 assertEquals(profile, info.getProfile());
109
110 // push new profile into the interface and check it's handled
111 profileInterface.updateProfile(profile2);
112 assertEquals(profile2, info.getProfile());
113 verify(testlistener, times(1)).notifyChange(profile2);
114 assertEquals(Arrays.asList("issue3", "issue2"), info.getIssues());
115 verify(reporter, times(0)).log(any(), any());
116
117 }
118
119 @Test
120 public void testActNotMyTurn() {
121 info.doAction(new Accept(PARTY_ID, new Bid(Collections.emptyMap())));
122 verify(reporter, times(1)).log(eq(Level.SEVERE), any());
123
124 }
125
126 @Test
127 public void testChangeTurn() {
128 Listener<Object> testlistener = mock(Listener.class);
129 info.addListener(testlistener);
130
131 info.setMyTurn(true);
132 verify(testlistener, times(1)).notifyChange(eq(true));
133 }
134
135 @Test
136 public void testActMyTurn() {
137 info.setMyTurn(true);
138 info.doAction(new Accept(PARTY_ID, new Bid(Collections.emptyMap())));
139 verify(reporter, times(0)).log(any(), any());
140
141 }
142
143 @Test
144 public void testSetCurrentBid() {
145 Listener<Object> testlistener = mock(Listener.class);
146 info.addListener(testlistener);
147
148 Bid bid = mock(Bid.class);
149 info.setCurrentBid(bid);
150 assertEquals(bid, info.getCurrentBid());
151 verify(testlistener, times(1)).notifyChange(bid);
152
153 }
154
155}
156
157class TestProfileInterface extends DefaultListenable<Profile>
158 implements ProfileInterface {
159
160 private Profile profile;
161
162 public TestProfileInterface(Profile profile) {
163 this.profile = profile;
164 }
165
166 public void updateProfile(Profile profile) {
167 this.profile = profile;
168 notifyListeners(profile);
169 }
170
171 @Override
172 public Profile getProfile() {
173 return profile;
174 }
175
176}
Note: See TracBrowser for help on using the repository browser.