From f9695aca4ffff2f65beb9e3ff736c469c8d7fd2b Mon Sep 17 00:00:00 2001 From: Sean Minami Date: Sun, 15 Dec 2024 23:07:04 -0800 Subject: [PATCH] Unit tests for HttpService class --- fightme_webapp/lib/Models/friend_request.dart | 18 +- fightme_webapp/lib/Models/httpservice.dart | 58 +++-- fightme_webapp/lib/Models/message.dart | 3 +- fightme_webapp/test/httpservice_test.dart | 228 +++++++++++++++++- 4 files changed, 273 insertions(+), 34 deletions(-) diff --git a/fightme_webapp/lib/Models/friend_request.dart b/fightme_webapp/lib/Models/friend_request.dart index aab3674..db67c0b 100644 --- a/fightme_webapp/lib/Models/friend_request.dart +++ b/fightme_webapp/lib/Models/friend_request.dart @@ -1,7 +1,6 @@ +enum Status { pending, accepted, rejected } -enum Status {pending, accepted, rejected } - -class FriendRequest{ +class FriendRequest { int id = 0; int fromUserID = 0; int toUserID = 0; @@ -15,14 +14,21 @@ class FriendRequest{ } bool isEmpty() { - return id == 0 && fromUserID == 0 && toUserID == 0 && status == Status.pending; + return id == 0 && + fromUserID == 0 && + toUserID == 0 && + status == Status.pending; } FriendRequest.fromJson(Map json) { id = json['id']; fromUserID = json['fromUserID']; toUserID = json['toUserID']; - status = (json['status'] == 'PENDING') ? Status.pending : (json['status'] == 'ACCEPTED') ? Status.accepted : Status.rejected; + status = (json['status'] == 'STATUS.PENDING') + ? Status.pending + : (json['status'] == 'STATUS.ACCEPTED') + ? Status.accepted + : Status.rejected; } Map toJson() => { @@ -31,4 +37,4 @@ class FriendRequest{ 'toUserID': toUserID, 'status': status.toString().toUpperCase() }; -} \ No newline at end of file +} diff --git a/fightme_webapp/lib/Models/httpservice.dart b/fightme_webapp/lib/Models/httpservice.dart index 3db09f8..c21fa99 100644 --- a/fightme_webapp/lib/Models/httpservice.dart +++ b/fightme_webapp/lib/Models/httpservice.dart @@ -66,11 +66,12 @@ class HttpService { } Future> getDoneGames(int id) async { - Response res = await get(Uri.parse("$springbootUserURL$id/games")); + http.Response res = + await _httpClient.get(Uri.parse("$springbootUserURL$id/games")); if (res.statusCode == 200) { List body = jsonDecode(res.body); List doneGames = - body.map((dynamic item) => FightGameSession.fromJson(item)).toList(); + body.map((dynamic item) => FightGameSession.fromJson(item)).toList(); return doneGames; } else { throw "Unable to retrieve user data."; @@ -78,11 +79,12 @@ class HttpService { } Future> getActiveGames(int id) async { - Response res = await get(Uri.parse("$springbootUserURL$id/vs")); + http.Response res = + await _httpClient.get(Uri.parse("$springbootUserURL$id/vs")); if (res.statusCode == 200) { List body = jsonDecode(res.body); List activeGames = - body.map((dynamic item) => FightGameSession.fromJson(item)).toList(); + body.map((dynamic item) => FightGameSession.fromJson(item)).toList(); return activeGames; } else { throw "Unable to retrieve user data."; @@ -222,11 +224,12 @@ class HttpService { } Future> getAllSentRequests(int userID) async { - Response res = await get(Uri.parse("$springbootFriendRequestURL/$userID/sent")); + http.Response res = await _httpClient + .get(Uri.parse("$springbootFriendRequestURL/$userID/sent")); if (res.statusCode == 200) { List body = jsonDecode(res.body); List friendRequests = - body.map((dynamic item) => FriendRequest.fromJson(item)).toList(); + body.map((dynamic item) => FriendRequest.fromJson(item)).toList(); return friendRequests; } else { print("Unable to retrieve friend request data for user $userID"); @@ -235,7 +238,8 @@ class HttpService { } Future getFriendRequest(int fromUserID, int toUserID) async { - Response res = await get(Uri.parse("$springbootFriendRequestURL/$fromUserID/$toUserID")); + http.Response res = await _httpClient + .get(Uri.parse("$springbootFriendRequestURL/$fromUserID/$toUserID")); if (res.statusCode == 200) { dynamic body = jsonDecode(res.body); FriendRequest friendRequest = FriendRequest.fromJson(body); @@ -312,7 +316,8 @@ class HttpService { } Future updateUserProfilePicture(int userID, int profilePicture) async { - Response res = await put(Uri.parse("$springbootUserURL$userID/profilePicture"), + http.Response res = await _httpClient.put( + Uri.parse("$springbootUserURL$userID/profilePicture"), headers: {"Content-Type": "application/json"}, body: jsonEncode({"profilePicture": profilePicture})); if (res.statusCode == 200) { @@ -323,7 +328,8 @@ class HttpService { } Future addUserProfilePicture(int userID, int profilePicture) async { - Response res = await put(Uri.parse("$springbootUserURL$userID/addProfilePicture"), + http.Response res = await _httpClient.put( + Uri.parse("$springbootUserURL$userID/addProfilePicture"), headers: {"Content-Type": "application/json"}, body: jsonEncode({"profilePicture": profilePicture})); if (res.statusCode == 200) { @@ -334,7 +340,8 @@ class HttpService { } Future updateUserTheme(int userID, int theme) async { - Response res = await put(Uri.parse("$springbootUserURL$userID/theme"), + http.Response res = await _httpClient.put( + Uri.parse("$springbootUserURL$userID/theme"), headers: {"Content-Type": "application/json"}, body: jsonEncode({"theme": theme})); if (res.statusCode == 200) { @@ -345,7 +352,8 @@ class HttpService { } Future addUserTheme(int userID, int theme) async { - Response res = await put(Uri.parse("$springbootUserURL$userID/addTheme"), + http.Response res = await _httpClient.put( + Uri.parse("$springbootUserURL$userID/addTheme"), headers: {"Content-Type": "application/json"}, body: jsonEncode({"theme": theme})); if (res.statusCode == 200) { @@ -355,10 +363,16 @@ class HttpService { } } - Future postFightGame(User user1, User user2, int requesterID) async { - Response res = await post(Uri.parse(springbootFightGamesURL), + Future postFightGame( + User user1, User user2, int requesterID) async { + http.Response res = await _httpClient.post( + Uri.parse(springbootFightGamesURL), headers: {"Content-Type": "application/json"}, - body: jsonEncode({"user1": user1.toJson(), "user2": user2.toJson(), "requesterID": requesterID})); + body: jsonEncode({ + "user1": user1.toJson(), + "user2": user2.toJson(), + "requesterID": requesterID + })); print(res.body); if (res.statusCode == 201) { print("Game created successfully."); @@ -372,7 +386,8 @@ class HttpService { } Future getFightGame(int user1ID, int user2ID) async { - Response res = await get(Uri.parse("$springbootFightGamesURL/$user1ID/$user2ID")); + http.Response res = await _httpClient + .get(Uri.parse("$springbootFightGamesURL/$user1ID/$user2ID")); if (res.statusCode == 200) { print(res.body); dynamic body = jsonDecode(res.body); @@ -386,9 +401,11 @@ class HttpService { } Future setMove(int gameID, int userID, Move move) async { - Response res = await put(Uri.parse("$springbootFightGamesURL/$gameID/setMove"), + http.Response res = await _httpClient.put( + Uri.parse("$springbootFightGamesURL/$gameID/setMove"), headers: {"Content-Type": "application/json"}, - body: jsonEncode({"userID": userID, "move": move.name.toString().toUpperCase()})); + body: jsonEncode( + {"userID": userID, "move": move.name.toString().toUpperCase()})); if (res.statusCode == 200) { print("Move set successfully."); } else { @@ -399,7 +416,8 @@ class HttpService { } Future setNewTurn(FightGameSession game) async { - Response res = await put(Uri.parse("$springbootFightGamesURL/newTurn"), + http.Response res = await _httpClient.put( + Uri.parse("$springbootFightGamesURL/newTurn"), headers: {"Content-Type": "application/json"}, body: jsonEncode(game.toJson())); if (res.statusCode == 200) { @@ -410,7 +428,8 @@ class HttpService { } Future declareWinner(int gameID) async { - Response res = await put(Uri.parse("$springbootFightGamesURL/$gameID/winner"), + http.Response res = await _httpClient.put( + Uri.parse("$springbootFightGamesURL/$gameID/winner"), headers: {"Content-Type": "application/json"}); if (res.statusCode == 200) { print("Winner set successfully."); @@ -419,4 +438,3 @@ class HttpService { } } } - diff --git a/fightme_webapp/lib/Models/message.dart b/fightme_webapp/lib/Models/message.dart index 4815415..a5ada32 100644 --- a/fightme_webapp/lib/Models/message.dart +++ b/fightme_webapp/lib/Models/message.dart @@ -20,7 +20,8 @@ class Message { 'fromId': fromId, 'content': content, 'isRead': false, - 'chatroomId': chatroomId + 'chatroomId': chatroomId, + 'timeStamp': timeStamp }; Message(int to, int from, String text, int chatId) { diff --git a/fightme_webapp/test/httpservice_test.dart b/fightme_webapp/test/httpservice_test.dart index 600edfb..c04050a 100644 --- a/fightme_webapp/test/httpservice_test.dart +++ b/fightme_webapp/test/httpservice_test.dart @@ -1,10 +1,16 @@ import 'dart:convert'; +import 'package:fightme_webapp/Models/fight_game_session.dart'; import 'package:test/test.dart'; import 'package:http/http.dart'; import 'package:mocktail/mocktail.dart'; import 'package:fightme_webapp/Models/httpservice.dart'; +import 'package:fightme_webapp/Models/user.dart'; +import 'package:fightme_webapp/Models/message.dart'; +import 'package:fightme_webapp/Models/chatroom.dart'; +import 'package:fightme_webapp/Models/friend_request.dart'; +import 'package:fightme_webapp/Models/fight_game_session.dart'; class MockHttpClient extends Mock implements Client {} @@ -34,7 +40,11 @@ void main() { "gamerScore": 0, "attackScore": 0, "defenseScore": 0, - "magicScore": 0 + "magicScore": 0, + "profilePicture": 0, + "unlockedProfilePictures": [0], + "theme": 0, + "unlockedThemes": [0] }, { "id": 2, @@ -45,7 +55,11 @@ void main() { "gamerScore": 0, "attackScore": 0, "defenseScore": 0, - "magicScore": 0 + "magicScore": 0, + "profilePicture": 0, + "unlockedProfilePictures": [0], + "theme": 0, + "unlockedThemes": [0] }, ]), ); @@ -67,7 +81,11 @@ void main() { "gamerScore": 0, "attackScore": 0, "defenseScore": 0, - "magicScore": 0 + "magicScore": 0, + "profilePicture": 0, + "unlockedProfilePictures": [0], + "theme": 0, + "unlockedThemes": [0] }, ), ); @@ -98,7 +116,11 @@ void main() { "gamerScore": 0, "attackScore": 0, "defenseScore": 0, - "magicScore": 0 + "magicScore": 0, + "profilePicture": 0, + "unlockedProfilePictures": [0], + "theme": 0, + "unlockedThemes": [0] }, ), ); @@ -106,8 +128,34 @@ void main() { expect(users.length, 1); }); - test("Signup User", () async { + test('Get done games', () async { + User user1 = User('John'); + User user2 = User('Sara'); + user2.id = 1; + when(() => httpClient.get(any())).thenAnswer((_) async => response); + when(() => response.statusCode).thenReturn(200); + when(() => response.body) + .thenReturn(jsonEncode([FightGameSession(user1, user2).toJson()])); + final games = await httpService.getDoneGames(0); + expect(games.length, 1); + expect(games[0].user1.name, user1.name); + }); + + test('Get active games', () async { + User user1 = User('John'); + User user2 = User('Sara'); + user2.id = 1; + when(() => httpClient.get(any())).thenAnswer((_) async => response); when(() => response.statusCode).thenReturn(200); + when(() => response.body) + .thenReturn(jsonEncode([FightGameSession(user1, user2).toJson()])); + final games = await httpService.getActiveGames(0); + expect(games.length, 1); + expect(games[0].user1.name, user1.name); + }); + + test("Signup User", () async { + when(() => response.statusCode).thenReturn(201); when(() => response.body).thenReturn(jsonEncode(7)); when(() => response.headers) .thenReturn({"Content-Type": "application/json"}); @@ -122,6 +170,172 @@ void main() { }); test("Login User", () async { - - }) + when(() => response.statusCode).thenReturn(200); + when(() => response.body).thenReturn(jsonEncode(7)); + when(() => response.headers) + .thenReturn({"Content-Type": "application/json"}); + //print(jsonEncode(7)); + when(() => httpClient.post(any(), + body: any(named: 'body'), + headers: any(named: 'headers'))).thenAnswer((_) async => response); + + final id = await httpService.loginUser("john@mail.com", "testpass123"); + expect(id, 7); + }); + + test("get Chatroom Messages", () async { + Message testMessage = Message(1, 2, "Hello World!", 1); + testMessage.timeStamp = 10; + when(() => httpClient.get(any())).thenAnswer((_) async => response); + when(() => response.statusCode).thenReturn(200); + when(() => response.body).thenReturn(jsonEncode([testMessage.toJson()])); + + final messages = await httpService.getChatroomMessages(0); + expect(messages.length, 1); + expect(messages[0].content, "Hello World!"); + }); + + test("get Chatrooms by User ID", () async { + Chatroom testRoom = Chatroom("Chatroom1"); + User user1 = User('John'); + User user2 = User('Sara'); + user2.id = 1; + Message testMessage = Message(1, 2, "Hello World!", 1); + testMessage.timeStamp = 10; + testRoom.users.add(user1); + testRoom.users.add(user2); + testRoom.messages.add(testMessage); + + when(() => httpClient.get(any())).thenAnswer((_) async => response); + when(() => response.statusCode).thenReturn(200); + when(() => response.body).thenReturn(jsonEncode([testRoom.toJson()])); + + final rooms = await httpService.getChatroomsByUserId(0); + expect(rooms.length, 1); + expect(rooms[0].users.length, 2); + expect(rooms[0].messages.length, 1); + expect(rooms[0].messages[0].content, "Hello World!"); + }); + + test("get Friend Requests for User ID", () async { + FriendRequest testRequest = FriendRequest.empty(); + testRequest.fromUserID = 1; + testRequest.toUserID = 2; + when(() => httpClient.get(any())).thenAnswer((_) async => response); + when(() => response.statusCode).thenReturn(200); + when(() => response.body).thenReturn(jsonEncode([testRequest.toJson()])); + + final requests = await httpService.getAllFriendRequests(0); + expect(requests.length, 1); + expect(requests[0].fromUserID, 1); + expect(requests[0].toUserID, 2); + expect(requests[0].status, Status.pending); + }); + + test("get sent Friend Requests for User ID", () async { + FriendRequest testRequest = FriendRequest.empty(); + testRequest.fromUserID = 1; + testRequest.toUserID = 2; + + when(() => httpClient.get(any())).thenAnswer((_) async => response); + when(() => response.statusCode).thenReturn(200); + when(() => response.body).thenReturn(jsonEncode([testRequest.toJson()])); + + final requests = await httpService.getAllSentRequests(1); + expect(requests.length, 1); + expect(requests[0].fromUserID, 1); + expect(requests[0].toUserID, 2); + expect(requests[0].status, Status.pending); + }); + + test("get Friend Request", () async { + FriendRequest testRequest = FriendRequest.empty(); + testRequest.fromUserID = 1; + testRequest.toUserID = 2; + + when(() => httpClient.get(any())).thenAnswer((_) async => response); + when(() => response.statusCode).thenReturn(200); + when(() => response.body).thenReturn(jsonEncode(testRequest.toJson())); + + final request = await httpService.getFriendRequest(1, 2); + expect(request.fromUserID, 1); + expect(request.toUserID, 2); + expect(request.status, Status.pending); + }); + + test('Get all users', () async { + when(() => httpClient.get(any())).thenAnswer((_) async => response); + when(() => response.statusCode).thenReturn(200); + when(() => response.body).thenReturn( + jsonEncode([ + { + "id": 1, + "name": "Josh", + "email": "Josh@mail.com", + "password": "testpass123", + "dateCreated": 0, + "gamerScore": 0, + "attackScore": 0, + "defenseScore": 0, + "magicScore": 0, + "profilePicture": 0, + "unlockedProfilePictures": [0], + "theme": 0, + "unlockedThemes": [0] + }, + { + "id": 2, + "name": "Sara", + "email": "Sara@mail.com", + "password": "testpass123", + "dateCreated": 0, + "gamerScore": 0, + "attackScore": 0, + "defenseScore": 0, + "magicScore": 0, + "profilePicture": 0, + "unlockedProfilePictures": [0], + "theme": 0, + "unlockedThemes": [0] + }, + ]), + ); + final users = await httpService.getSuggestedFriends(1); + expect(users.length, 2); + expect(users[0].name, "Josh"); + expect(users[1].name, "Sara"); + }); + + test("post Fight Game", () async { + User user1 = User('John'); + User user2 = User('Sara'); + user2.id = 1; + FightGameSession testFight = FightGameSession(user1, user2); + testFight.requesterID = user1.id; + when(() => response.statusCode).thenReturn(201); + when(() => response.body).thenReturn(jsonEncode(testFight.toJson())); + when(() => response.headers) + .thenReturn({"Content-Type": "application/json"}); + when(() => httpClient.post(any(), + body: any(named: 'body'), + headers: any(named: 'headers'))).thenAnswer((_) async => response); + + final game = await httpService.postFightGame(user1, user2, user1.id); + expect(game.user1.name, user1.name); + expect(game.user2.name, user2.name); + }); + + test('Get fight game', () async { + User user1 = User('John'); + User user2 = User('Sara'); + user2.id = 1; + FightGameSession testFight = FightGameSession(user1, user2); + testFight.requesterID = user1.id; + when(() => httpClient.get(any())).thenAnswer((_) async => response); + when(() => response.statusCode).thenReturn(200); + when(() => response.body).thenReturn(jsonEncode(testFight.toJson())); + final game = await httpService.getFightGame(user1.id, user2.id); + expect(game.user1.name, user1.name); + expect(game.user2.name, user2.name); + }); }