Skip to content

Commit 76c8b8c

Browse files
committed
Merge branch 'Tests!!'
2 parents 8a55864 + 3426dbb commit 76c8b8c

File tree

29 files changed

+1602
-161
lines changed

29 files changed

+1602
-161
lines changed

Client/pom.xml

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
<modelVersion>4.0.0</modelVersion>
66
<properties>
77
<javafx.version>23.0.2</javafx.version>
8-
<junit.version>5.11.4</junit.version>
98
<maven.compiler.source>24</maven.compiler.source>
109
<maven.compiler.target>24</maven.compiler.target>
1110
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
@@ -21,12 +20,6 @@
2120

2221
<dependencies>
2322
<!-- https://mvnrepository.com/artifact/org.openjfx/javafx -->
24-
<dependency>
25-
<groupId>edu.sdccd.cisc191.template</groupId>
26-
<artifactId>Common</artifactId>
27-
<version>${project.version}</version>
28-
<scope>compile</scope>
29-
</dependency>
3023
<dependency>
3124
<groupId>org.openjfx</groupId>
3225
<artifactId>javafx-controls</artifactId>
@@ -43,10 +36,16 @@
4336
<groupId>org.springframework.boot</groupId>
4437
<artifactId>spring-boot-starter-web</artifactId>
4538
</dependency>
39+
<dependency>
40+
<groupId>org.openjfx</groupId>
41+
<artifactId>javafx-swing</artifactId>
42+
<version>19</version>
43+
</dependency>
44+
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
4645
<dependency>
4746
<groupId>org.junit.jupiter</groupId>
4847
<artifactId>junit-jupiter-api</artifactId>
49-
<version>${junit.version}</version>
48+
<version>5.11.4</version>
5049
<scope>test</scope>
5150
</dependency>
5251
<dependency>
@@ -55,7 +54,25 @@
5554
<version>1.0.0</version>
5655
<scope>compile</scope>
5756
</dependency>
58-
</dependencies>
57+
<dependency>
58+
<groupId>org.mockito</groupId>
59+
<artifactId>mockito-junit-jupiter</artifactId>
60+
<version>2.22.0</version>
61+
<scope>test</scope>
62+
</dependency>
63+
<dependency>
64+
<groupId>org.testfx</groupId>
65+
<artifactId>testfx-core</artifactId>
66+
<version>4.0.18</version>
67+
<scope>test</scope>
68+
</dependency>
69+
<dependency>
70+
<groupId>org.testfx</groupId>
71+
<artifactId>testfx-junit5</artifactId>
72+
<version>4.0.18</version>
73+
<scope>test</scope>
74+
</dependency>
75+
</dependencies>
5976

6077
<build>
6178
<plugins>

Client/src/.DS_Store

0 Bytes
Binary file not shown.

Client/src/main/java/edu/sdccd/cisc191/Client/BetView.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public void start(Stage stage) throws Exception {
122122
try {
123123
Client.patchAddBetToMainUser(1L, game.getDbId(), team, amount, winAmt);
124124
} catch (Exception e) {
125-
throw new RuntimeException(e);
125+
System.out.println("Error adding bet to main user" + e.getMessage());
126126
}
127127
try {
128128
new UI().start(stage);

Client/src/main/java/edu/sdccd/cisc191/Client/Client.java

Lines changed: 47 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import edu.sdccd.cisc191.Common.Request;
1111
import javafx.application.Application;
1212
import javafx.stage.Stage;
13+
import lombok.Getter;
1314
import org.json.simple.JSONArray;
1415
import org.json.simple.JSONObject;
1516
import org.json.simple.parser.JSONParser;
@@ -28,126 +29,20 @@
2829
* It handles game, user, and size requests, as well as modifications to user data.
2930
*/
3031
public class Client {
31-
/**
32-
* A static user representing the client user. Initialized with name "Chase" and money 1000000.
33-
*/
34-
35-
/**
36-
* The socket used to connect to the server.
37-
*/
38-
private static Socket clientSocket;
39-
40-
private static ObjectOutputStream out; // The output stream for sending requests to the server.
41-
private static ObjectInputStream in; // The input stream for receiving responses from the server.
42-
43-
// --- Socket and Request Methods ---
44-
/**
45-
* Establishes a connection to the server using the provided IP address and port.
46-
*
47-
* @param ip the IP address of the server.
48-
* @param port the port number on the server.
49-
* @throws IOException if an I/O error occurs when opening the connection.
50-
*/
51-
public static void startConnection(String ip, int port) throws IOException {
52-
clientSocket = new Socket(ip, port);
5332

54-
out = new ObjectOutputStream(clientSocket.getOutputStream());
55-
in = new ObjectInputStream(clientSocket.getInputStream());
33+
protected HttpClient httpClient() {
34+
return HttpClient.newHttpClient();
5635
}
5736

58-
5937
/**
60-
* Sends a request to the server and returns a response of the expected type.
38+
* Grabs odds from the Sports API via the Database Server
6139
*
62-
* Never call this method directly, call one of its wrappers for safe usage.
63-
*
64-
* @param <T> the type parameter corresponding to the expected response type.
65-
* @return the response from the server cast to the specified type.
66-
* @throws Exception if an error occurs during the request.
67-
*/
68-
static <T> T sendRequest(Request request, Class<T> responseType) throws Exception {
69-
// Write request
70-
out.writeObject(request);
71-
out.flush();
72-
73-
// read back whatever the server sent
74-
Object raw = in. readObject();
75-
System.out.println("Raw: " + raw);
76-
System.out.println("Raw type: " + raw.getClass());
77-
System.out.println("Response Type: " + responseType);
78-
79-
// cast into the expected type
80-
81-
try {
82-
return responseType.cast(raw);
83-
}
84-
catch (ClassCastException e) {
85-
System.out.println("ClassCastException, could not cast " + raw.getClass() + " to " + responseType);
86-
}
87-
88-
return null;
89-
90-
}
91-
92-
// Update stopConnection to check for null before closing resources:
93-
public void stopConnection() throws IOException {
94-
if (in != null) {
95-
in.close();
96-
}
97-
if (out != null) {
98-
out.close();
99-
}
100-
if (clientSocket != null) {
101-
clientSocket.close();
102-
}
103-
}
104-
105-
106-
public User userGetRequest(int id) throws IOException {
107-
Client client = new Client();
108-
109-
try {
110-
client.startConnection("localhost", 4445);
111-
112-
// build a request object
113-
Request req = new Request("User", id);
114-
115-
return client.sendRequest(req, User.class);
116-
} catch (Exception e) {
117-
throw new RuntimeException(e);
118-
} finally {
119-
try {
120-
client.stopConnection();
121-
} catch (Exception e) {
122-
e.printStackTrace();
123-
}
124-
}
125-
126-
}
127-
/**
128-
* Modifies a user on the server with the provided attributes and returns the updated user.
129-
*
130-
* @param id the identifier of the user to modify.
131-
* @param modifiedAttributes a map containing the fields and their new values.
132-
* Valid Fields: ("Name", "Money", "addBet", "removeBet")
133-
* @return the updated User object if modification is successful; null otherwise.
134-
* @throws IOException if an I/O error occurs during the request.
135-
*/
136-
public User userModifyRequest(int id, Map<String, Object> modifiedAttributes) throws IOException {
137-
Client client = new Client();
138-
try {
139-
client.startConnection("localhost", 4444);
140-
System.out.println("Sending userModifyRequest with ID: " + id);
141-
return client.sendRequest(new Request("ModifyUser", id, modifiedAttributes), User.class);
142-
} catch (Exception e) {
143-
e.printStackTrace();
144-
}
145-
stopConnection();
146-
return null;
147-
}
148-
40+
* @param gameId the game ID (from the API) to grab the odds for.
41+
* @param sport the sport to grab the odds for.
42+
* @param homeOrAway 0 for home, 1 for away.
43+
* */
14944
public static double getOdds(int gameId, String sport, int homeOrAway) throws IOException, InterruptedException {
150-
HttpClient client = HttpClient.newHttpClient();
45+
HttpClient client = new Client().httpClient();
15146

15247
HttpRequest request = HttpRequest.newBuilder()
15348
.uri(URI.create("http://localhost:9090/games/odds/" + sport + "/" + gameId))
@@ -199,7 +94,7 @@ public static double getOdds(int gameId, String sport, int homeOrAway) throws IO
19994
* @throws IOException if an I/O error occurs during retrieval.
20095
*/
20196
public static ArrayList<Game> getGames() throws Exception {
202-
HttpClient client = HttpClient.newHttpClient();
97+
HttpClient client = new Client().httpClient();
20398

20499
HttpRequest request = HttpRequest.newBuilder()
205100
.uri(URI.create("http://localhost:9090/games"))
@@ -228,8 +123,11 @@ public static ArrayList<Game> getGames() throws Exception {
228123
return allGames;
229124
}
230125

126+
/**
127+
* Retrieves the primary user, user of ID 1, from the server and initializes it to a User object
128+
* */
231129
public static User getMainUser() throws Exception {
232-
HttpClient client = HttpClient.newHttpClient();
130+
HttpClient client = new Client().httpClient();
233131

234132
HttpRequest request = HttpRequest.newBuilder()
235133
.uri(URI.create("http://localhost:9090/users/1"))
@@ -262,8 +160,12 @@ public static User getMainUser() throws Exception {
262160

263161
}
264162

163+
/**
164+
* Uses a PUT request to update the main user's money amount.
165+
* @param money the new amount of money to set the main user's money to.
166+
* */
265167
private static void updateMainUserMoney(long money) throws Exception {
266-
HttpClient client = HttpClient.newHttpClient();
168+
HttpClient client = new Client().httpClient();
267169

268170
String jsonBody = "{\"id\": 1, \"name\": \"Chase\", \"money\": " + money + "}";
269171

@@ -280,8 +182,11 @@ private static void updateMainUserMoney(long money) throws Exception {
280182

281183
}
282184

185+
/**
186+
* Calls a GET method on the Database server to fulfill expired user bets
187+
* */
283188
public static void updateBets() throws Exception {
284-
HttpClient client = HttpClient.newHttpClient();
189+
HttpClient client = new Client().httpClient();
285190

286191
HttpRequest request = HttpRequest.newBuilder()
287192
.uri(URI.create("http://localhost:9090/updateAllBets"))
@@ -293,6 +198,14 @@ public static void updateBets() throws Exception {
293198
System.out.println("Status Code (update bets on app open): " + response.statusCode());
294199
}
295200

201+
/**
202+
* Calls a PATCH method on the Database server to add a bet to a user's bets.
203+
* @param userId the ID of the user to add the bet to.
204+
* @param gameId the ID (from the API) of the game to add the bet for.
205+
* @param betTeam the team the user bets on.
206+
* @param betAmt the amount the user bets.
207+
* @param winAmt the amount the user wins.
208+
* */
296209
public static void patchAddBetToMainUser(Long userId, Long gameId, String betTeam, int betAmt, int winAmt) throws Exception {
297210
ObjectMapper mapper = new ObjectMapper();
298211
// build only the DTO fields
@@ -306,7 +219,7 @@ public static void patchAddBetToMainUser(Long userId, Long gameId, String betTea
306219

307220
System.out.println("PATCH body: " + jsonBody);
308221

309-
HttpClient client = HttpClient.newHttpClient();
222+
HttpClient client = new Client().httpClient();
310223
HttpRequest request = HttpRequest.newBuilder()
311224
.uri(URI.create("http://localhost:9090/" + userId + "/bets"))
312225
.header("Content-Type", "application/json")
@@ -320,14 +233,15 @@ public static void patchAddBetToMainUser(Long userId, Long gameId, String betTea
320233
System.out.println("Response Body: " + response.body());
321234
}
322235

236+
@Getter
323237
private static ArrayList<BotBase> bots = new ArrayList<>();
324238

325-
public static ArrayList<BotBase> getBots() {
326-
return bots;
327-
}
328-
239+
/**
240+
* Creates an array of BotBase objects from the server.
241+
* @throws Exception if an error occurs during retrieval.
242+
* */
329243
public static void createBotArray() throws Exception {
330-
HttpClient client = HttpClient.newHttpClient();
244+
HttpClient client = new Client().httpClient();
331245

332246
HttpRequest request = HttpRequest.newBuilder()
333247
.uri(URI.create("http://localhost:9090/users"))
@@ -353,15 +267,22 @@ public static void createBotArray() throws Exception {
353267
}
354268
}
355269

270+
/**
271+
* Starts all the bots in the array.
272+
* @throws Exception if an error occurs during retrieval.
273+
*/
356274
public static void launchBots() throws Exception {
357275
createBotArray();
358276
for (BotBase bot : bots) {
359277
bot.startBot();
360278
}
361279
}
362280

363-
364-
281+
/**
282+
* Launches bots and the JavaFX UI
283+
* @param args the command line arguments.
284+
* @throws Exception if an error occurs during retrieval.
285+
* */
365286
public static void main(String[] args) throws Exception {
366287
launchBots();
367288
Application.launch(UI.class, args);

0 commit comments

Comments
 (0)