-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestProxyController.java
More file actions
228 lines (198 loc) · 7.87 KB
/
TestProxyController.java
File metadata and controls
228 lines (198 loc) · 7.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package cs3500.pa04;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import com.fasterxml.jackson.databind.JsonNode;
import cs3500.pa03.BattleSalvoController;
import cs3500.pa04.JSON.EndgameJson;
import cs3500.pa04.JSON.JsonUtils;
import cs3500.pa04.JSON.MessageJson;
import cs3500.pa04.JSON.JoinJson;
import cs3500.pa04.JSON.ReportDamageJson;
import cs3500.pa04.JSON.SetupJson;
import cs3500.pa04.JSON.TakeShotsJson;
import cs3500.pa03.Model.AiPlayer;
import cs3500.pa03.Model.Coord;
import cs3500.pa03.Model.DirectionType;
import cs3500.pa03.Model.GameResult;
import cs3500.pa03.Model.GameType;
import cs3500.pa03.Model.Player;
import cs3500.pa04.JSON.SucessfulHitsJson;
import cs3500.pa03.Model.Ship;
import cs3500.pa03.Model.ShipType;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public class TestProxyController {
private ProxyController pc;
private Player ai;
private BattleSalvoController bsc;
private ByteArrayOutputStream tester;
@BeforeEach
public void setup() {
bsc = new BattleSalvoController();
ai = new AiPlayer(bsc);
this.tester = new ByteArrayOutputStream(2048);
assertEquals("", logToString());
}
@Test
public void testHandleJoin() {
JoinJson joinJson = new JoinJson("riasingh125", GameType.SINGLE.toString());
JsonNode sampleMessage = createSampleMessage("join", joinJson);
Mocket mockSocket = new Mocket(this.tester, List.of(sampleMessage.toString()));
try {
this.pc = new ProxyController(mockSocket, ai);
} catch (IOException e) {
fail("proxy controller couldn't be created");
}
this.pc.run();
String expectedOutcome =
"{\"method-name\":\"join\",\"arguments\":{\"name\":\"A-goofyGoober\",\"game-type\":\"SINGLE\"}}";
assertEquals(expectedOutcome.stripTrailing(), logToString().stripTrailing());
}
@Test
public void testHandleTakeShots() {
Coord coord1 = new Coord(1, 2);
Coord coord2 = new Coord(2, 1);
TakeShotsJson shotsJson = new TakeShotsJson(List.of(coord1, coord2));
JsonNode sampleMessage = createSampleMessage("take-shots", shotsJson);
Mocket mockSocket = new Mocket(this.tester, List.of());
try {
this.pc = new ProxyController(mockSocket, ai);
} catch (IOException e) {
fail("proxy controller couldn't be created");
}
pc.run();
String expectedOutcome =
"{\"method-name\":\"take-shots\",\"arguments\":{\"coordinates\":[{\"x\":1,\"y\":2},{\"x\":2,\"y\":1}]}}";
assertEquals(expectedOutcome.stripLeading().stripLeading(), sampleMessage.toString());
}
@Test
public void handleEndGameTestTie() {
EndgameJson endJson = new EndgameJson(GameResult.TIE, "test");
JsonNode sampleMessage = createSampleMessage("end-game", endJson);
Mocket mockSocket = new Mocket(this.tester, List.of(sampleMessage.toString()));
try {
this.pc = new ProxyController(mockSocket, ai);
} catch (IOException e) {
fail("couldn't make proxycontroller");
}
this.pc.run();
String expected = "";
assertEquals(expected, logToString());
}
@Test
public void handleEndGameTestWin() {
EndgameJson endJson = new EndgameJson(GameResult.WIN, "test");
JsonNode sampleMessage = createSampleMessage("end-game", endJson);
Mocket mockSocket = new Mocket(this.tester, List.of(sampleMessage.toString()));
try {
this.pc = new ProxyController(mockSocket, ai);
} catch (IOException e) {
fail("couldn't make proxycontroller");
}
this.pc.run();
String expected = "";
assertEquals(expected, logToString());
}
@Test
public void handleEndGameTestLose() {
EndgameJson endJson = new EndgameJson(GameResult.LOSE, "test");
JsonNode sampleMessage = createSampleMessage("end-game", endJson);
Mocket mockSocket = new Mocket(this.tester, List.of(sampleMessage.toString()));
try {
this.pc = new ProxyController(mockSocket, ai);
} catch (IOException e) {
fail("couldn't make proxycontroller");
}
this.pc.run();
String expected = "";
assertEquals(expected, logToString());
}
@Test
public void testReportDamage() {
Coord coord1 = new Coord(1, 2);
Coord coord2 = new Coord(2, 1);
ReportDamageJson report = new ReportDamageJson(List.of(coord2, coord1));
JsonNode sampleMessage = createSampleMessage("report-damage", report);
Mocket mocket = new Mocket(tester, List.of(sampleMessage.toString()));
try {
this.pc = new ProxyController(mocket, ai);
} catch (IOException e) {
fail("couldn't make proxycontroller");
}
Coord coord3 = new Coord(1, 2);
Coord coord4 = new Coord(2, 1);
Coord coord5 = new Coord(1, 3);
Coord coord6 = new Coord(3, 1);
Ship ship1 =
new Ship(ShipType.BATTLESHIP, List.of(coord3, coord4), DirectionType.HORIZONTAL, coord3);
Ship ship2 =
new Ship(ShipType.SUBMARINE, List.of(coord5, coord6), DirectionType.VERTICAL, coord5);
pc.fleet = List.of(ship1, ship2);
this.pc.run();
String expected =
"{\"method-name\":\"report-damage\",\"arguments\":{\"coordinates\":[{\"x\":1,\"y\":2}]}}";
assertEquals(expected.stripTrailing(), logToString().stripTrailing());
}
@Test
public void handleSuccessfulHits() {
Coord coord1 = new Coord(1, 2);
Coord coord2 = new Coord(2, 1);
SucessfulHitsJson sucess = new SucessfulHitsJson("");
JsonNode sampleMessage = createSampleMessage("successful-hits", sucess);
Mocket mocket = new Mocket(tester, List.of(coord1.toString(), coord2.toString()));
try {
this.pc = new ProxyController(mocket, ai);
} catch (IOException e) {
fail("couldn't make proxycontroller");
}
this.pc.run();
String expected = "{\"method-name\":\"successful-hits\",\"arguments\":{\"arguments: \":\"\"}}";
assertEquals(expected.stripLeading().stripTrailing(), sampleMessage.toString());
}
@Test
public void handleSetUp() {
BattleSalvoController bsc2 = new BattleSalvoController();
AiPlayer aip2 = new AiPlayer(bsc2);
Map<ShipType, Integer> sampleFleetSpec = new LinkedHashMap<>();
sampleFleetSpec.put(ShipType.SUBMARINE, 1);
sampleFleetSpec.put(ShipType.CARRIER, 1);
sampleFleetSpec.put(ShipType.BATTLESHIP, 1);
sampleFleetSpec.put(ShipType.DESTROYER, 1);
SetupJson setupJson = new SetupJson(10, 10, sampleFleetSpec);
JsonNode sampleMessage = createSampleMessage("setup", setupJson);
Mocket mocket = new Mocket(tester, List.of(sampleMessage.toString()));
try {
this.pc = new ProxyController(mocket, aip2);
} catch (IOException e) {
fail("couldn't make proxycontroller");
}
this.pc.run();
String expected =
"{\"method-name\":\"setup\",\"arguments\":{\"width\":10,\"height\":10,\"fleet-spec\":{\"SUBMARINE\":1,\"CARRIER\":1,\"BATTLESHIP\":1,\"DESTROYER\":1}}}";
assertEquals(expected.stripLeading().stripTrailing(), sampleMessage.toString().stripTrailing());
}
/**
* Converts the ByteArrayOutputStream log to a string in UTF_8 format
*
* @return String representing the current log buffer
*/
private String logToString() {
return tester.toString();
}
/**
* Create a MessageJson for some name and arguments.
*
* @param messageName name of the type of message; "hint" or "win"
* @param messageObject object to embed in a message json
* @return a MessageJson for the object
*/
private JsonNode createSampleMessage(String messageName, Record messageObject) {
MessageJson messageJson =
new MessageJson(messageName, JsonUtils.serializeRecord(messageObject));
return JsonUtils.serializeRecord(messageJson);
}
}