Skip to content

Commit b7ab887

Browse files
committed
Finish testing
1 parent 758e2a0 commit b7ab887

16 files changed

+69
-157
lines changed

src/main/java/com/backend/controller/PetController.java

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package com.backend.controller;
22

33
import com.backend.entities.IDs.SessionID;
4-
import com.backend.entities.ShopItem;
54
import com.backend.usecases.facades.PetSystemFacade;
65
import org.springframework.beans.factory.annotation.Autowired;
7-
86
import org.springframework.http.ResponseEntity;
97
import org.springframework.web.bind.annotation.GetMapping;
108
import org.springframework.web.bind.annotation.PostMapping;
@@ -34,20 +32,11 @@ public ResponseEntity<Object> getPet(@RequestParam SessionID sessionID){
3432
/**
3533
* Post request to replace the current pet with the same pet with an updated outfit from the database
3634
* @param sessionID string that represents the current session and verifies the action
37-
* @param shopItem ShopItem that will be for the updated outfit
35+
* @param itemID ItemID that corrosponds with the shop item
3836
*/
3937
@PostMapping("/updatePetOutfit")
40-
public ResponseEntity<Object> updatePetOutfit(@RequestParam SessionID sessionID, ShopItem shopItem){
41-
return this.petSystemFacade.updateCurrentOutfit(sessionID, shopItem);
42-
}
43-
44-
/**
45-
* Get request to get Pet Balance with the sessionID from the database
46-
* @param sessionID string that represents the current session and verifies the action
47-
*/
48-
@GetMapping("/petBalance")
49-
public ResponseEntity<Object> getBalance(@RequestParam SessionID sessionID){
50-
return this.petSystemFacade.getBalance(sessionID);
38+
public ResponseEntity<Object> updatePetOutfit(@RequestParam SessionID sessionID, String itemID){
39+
return this.petSystemFacade.updateCurrentOutfit(sessionID, itemID);
5140
}
5241

5342

src/main/java/com/backend/usecases/facades/AccountSystemFacade.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,24 @@ public class AccountSystemFacade {
2929
private final PetManager petManager;
3030
private final HealthManager healthManager;
3131
private final TaskManager taskManager;
32+
private final FriendSystemFacade friendSystemFacade;
3233
private final IErrorHandler errorHandler;
3334

3435
/**
3536
* Spring Boot Dependency Injection of the accountManager
3637
*
37-
* @param accountManager the dependency to be injected
38-
* @param petManager the dependency to be injected
39-
* @param healthManager the dependency to be injected
40-
* @param taskManager the dependency to be injected
38+
* @param accountManager the dependency to be injected
39+
* @param petManager the dependency to be injected
40+
* @param healthManager the dependency to be injected
41+
* @param taskManager the dependency to be injected
4142
*/
4243
@Autowired
43-
public AccountSystemFacade (AccountManager accountManager, PetManager petManager, HealthManager healthManager, TaskManager taskManager, IErrorHandler errorHandler) {
44+
public AccountSystemFacade (AccountManager accountManager, PetManager petManager, HealthManager healthManager, TaskManager taskManager, FriendSystemFacade friendSystemFacade, IErrorHandler errorHandler) {
4445
this.accountManager = accountManager;
4546
this.petManager = petManager;
4647
this.healthManager = healthManager;
4748
this.taskManager = taskManager;
49+
this.friendSystemFacade = friendSystemFacade;
4850
this.errorHandler = errorHandler;
4951
}
5052

@@ -152,6 +154,8 @@ public ResponseEntity<Object> deleteAccount(SessionID sessionID) {
152154

153155
// Other manager calls
154156
this.petManager.deletePet(accountID.getID());
157+
this.friendSystemFacade.deleteAllCorrelatedFriends(sessionID.getID());
158+
this.friendSystemFacade.deleteAllCorrelatedInvitations(sessionID.getID());
155159

156160
// Delete account by the accountID
157161
if (!this.accountManager.deleteAccount(accountID)) {

src/main/java/com/backend/usecases/facades/FeedSystemFacade.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.backend.error.exceptions.SessionException;
88
import com.backend.usecases.IErrorHandler;
99
import com.backend.usecases.managers.AccountManager;
10+
import com.backend.usecases.managers.FriendsManager;
1011
import com.backend.usecases.managers.PetManager;
1112
import com.backend.usecases.managers.TaskManager;
1213
import org.springframework.beans.factory.annotation.Autowired;
@@ -23,20 +24,23 @@ public class FeedSystemFacade {
2324
private final AccountManager accountManager;
2425
private final TaskManager taskManager;
2526
private final PetManager petManager;
27+
private final FriendsManager friendsManager;
2628
private final IErrorHandler errorHandler;
2729

2830
/**
2931
* Spring Boot Dependency Injection
32+
*
3033
* @param accountManager the dependency to be injected
31-
* @param taskManager the dependency to be injected
32-
* @param petManager the dependency to be injected
33-
* @param errorHandler the dependency to be injected
34+
* @param taskManager the dependency to be injected
35+
* @param petManager the dependency to be injected
36+
* @param errorHandler the dependency to be injected
3437
*/
3538
@Autowired
36-
public FeedSystemFacade(AccountManager accountManager, TaskManager taskManager, PetManager petManager, IErrorHandler errorHandler) {
39+
public FeedSystemFacade(AccountManager accountManager, TaskManager taskManager, PetManager petManager, FriendsManager friendsManager, IErrorHandler errorHandler) {
3740
this.accountManager = accountManager;
3841
this.taskManager = taskManager;
3942
this.petManager = petManager;
43+
this.friendsManager = friendsManager;
4044
this.errorHandler = errorHandler;
4145
}
4246

@@ -53,17 +57,18 @@ public ResponseEntity<Object> getFeed(String sessionID) {
5357
}
5458

5559
// get list of friends
56-
List<String> friends = new ArrayList<>(List.of("7T~Sp2w%Vl9t\"Wk4V]Fp"));
60+
List<String> friends = this.friendsManager.getFriends(accountID.getID());
5761

5862
// wrap FeedItem Objects
5963
List<FeedItem> feedItems = new ArrayList<>();
64+
List<TaskCompletionRecord> recordList = this.taskManager.getAllTaskCompletionRecords();
65+
Collections.reverse(recordList);
6066

61-
for (TaskCompletionRecord record : this.taskManager.getAllTaskCompletionRecords()) {
67+
for (TaskCompletionRecord record : recordList.subList(0, 9)) {
6268
if (friends.contains(record.getAccountID())) {
6369
feedItems.add(new FeedItem(this.accountManager.getAccountInfo(record.getAccountIDObject()), record, this.petManager.getPet(record.getAccountID())));
6470
}
6571
}
66-
Collections.reverse(feedItems);
6772

6873
return new ResponseEntity<>(feedItems, HttpStatus.OK);
6974
}

src/main/java/com/backend/usecases/facades/PetSystemFacade.java

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,14 @@ public ResponseEntity<Object> purchaseItem(String sessionID, String itemID) {
7979
/**
8080
* Post request of the allowing the pet to update the outfit
8181
* @param sessionID string that represents the current session and verifies the action
82-
* @param shopItem ShopItem of shopItem of outfit that the pet will wear
82+
* @param itemID ItemID of shopItem of outfit that the pet will wear
8383
*/
84-
public ResponseEntity<Object> updateCurrentOutfit(SessionID sessionID, ShopItem shopItem){
84+
public ResponseEntity<Object> updateCurrentOutfit(SessionID sessionID, String itemID){
8585
AccountID accountID = this.accountManager.verifySession(sessionID);
8686
if (accountID == null){
8787
return this.errorHandler.logError(new SessionException("Account ID is null since sessionID was invalid"), HttpStatus.BAD_REQUEST);
8888
}
89+
ShopItem shopItem = this.shopManager.getShopItem(itemID);
8990
boolean result = this.shopManager.updateCurrentOutfit(accountID, shopItem);
9091
if (!result){
9192
return this.errorHandler.logError(new SessionException("Account ID is null since sessionID was invalid"), HttpStatus.BAD_REQUEST);
@@ -106,20 +107,6 @@ public ResponseEntity<Object> getPet(SessionID sessionID){
106107
return new ResponseEntity<>(petManager.getPet(accountID.getID()), HttpStatus.OK);
107108
}
108109

109-
/**
110-
* Get request of the pet's balance by the sessionID from the database
111-
* @param sessionID string that represents the current session and verifies the action
112-
*/
113-
public ResponseEntity<Object> getBalance(SessionID sessionID){
114-
AccountID accountID = this.accountManager.verifySession(sessionID);
115-
if (accountID == null){
116-
return this.errorHandler.logError(new SessionException("Account ID is null since sessionID was invalid"), HttpStatus.BAD_REQUEST);
117-
}
118-
119-
double balance = this.balanceManager.getBalance(accountID.getID());
120-
return new ResponseEntity<>(balance, HttpStatus.OK);
121-
}
122-
123110
/**
124111
* Get request of all the shopItems from the database
125112
*/

src/main/java/com/backend/usecases/facades/TaskSystemFacade.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public ResponseEntity<?> completeTask(SessionID sessionID, String name, String i
4444
}
4545

4646
//check if the task name and reward are correct
47-
if (!this.taskManager.verifyTask(name, reward)) {
47+
if (!this.taskManager.verifyTask(name, image, reward)) {
4848
return this.errorHandler.logError(new Exception("Task does not exist"), HttpStatus.BAD_REQUEST);
4949
}
5050

@@ -54,8 +54,8 @@ public ResponseEntity<?> completeTask(SessionID sessionID, String name, String i
5454
}
5555

5656
//update the pet
57-
this.balanceManager.updateBalance(sessionID.getID(), reward);
58-
this.healthManager.updateHealth(account.getID(), reward);
57+
this.balanceManager.updateBalance(account.getID(), reward);
58+
this.healthManager.updateHealth(account.getID(), reward / 5);
5959
TaskCompletionRecord record = this.taskManager.completeTask(account, name, image);
6060
return new ResponseEntity<>(record, HttpStatus.OK);
6161
}

src/main/java/com/backend/usecases/managers/BalanceManager.java

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,6 @@ public BalanceManager(PetRepo petRepo, IErrorHandler errorHandler) {
2626
this.errorHandler = errorHandler;
2727
}
2828

29-
/**
30-
* Get the balance from the database through the pet object by the accountID
31-
* @param accountID string that represents the current session and verifies the action
32-
*/
33-
public Double getBalance(String accountID){
34-
Optional<Pet> pet = this.petRepo.findById(accountID);
35-
36-
if (pet.isEmpty()) {
37-
this.errorHandler.logError(new Exception("Pet object is empty"));
38-
return null;
39-
}
40-
41-
return pet.get().getBalance();
42-
}
43-
4429
/**
4530
* Update balance changed with the parameter amount for the corresponding account
4631
* @param accountID string that represents the current session and verifies the action

src/main/java/com/backend/usecases/managers/PetManager.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ public Pet initializePet(String id){
5353
}
5454
ArrayList<ShopItem> curInventory = new ArrayList<>();
5555
ArrayList<ShopItem> curOutfit = new ArrayList<>();
56-
ShopItem shopItem = new ShopItem("1480775928", 19.9, "brown pants", "a pair of classic fit brown pants", "", 2);
56+
ShopItem shopItem = new ShopItem("1480775935", 25.9, "Brown pants", "Keeps you grounded", "https://cdn.discordapp.com/attachments/1031600935782326434/1048421619758018631/pants4.png", 2);
5757
curInventory.add(shopItem);
5858
curOutfit.add(shopItem);
59-
Pet pet = new Pet(id, 85.00, 25.4, curInventory, curOutfit);
59+
Pet pet = new Pet(id, 85.00, 325.0, curInventory, curOutfit);
6060
this.petRepo.save(pet);
6161
return pet;
6262
}

src/main/java/com/backend/usecases/managers/TaskManager.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,11 @@ public TaskCompletionRecord completeTask(AccountID account, String name, String
5757
* @param reward of type double, the reward from the task
5858
* @return true if the task name and reward are valid, false otherwise
5959
*/
60-
public boolean verifyTask(String name, double reward) {
60+
public boolean verifyTask(String name, String image, double reward) {
61+
if (image.equals("")){
62+
return false;
63+
}
64+
6165
//check if the task is part of active tasks and reward is correct
6266
List <TaskActive> active = this.activeRepo.findAll();
6367
TaskActive current = null;
@@ -124,7 +128,7 @@ public List<TaskActive> activeTasks(AccountID account){
124128
String today = new Date(System.currentTimeMillis()).toString().substring(0, 10);
125129
String recent = taskActives.get(0).getDate();
126130

127-
if (recent.equals(today)){
131+
if (taskActives.isEmpty() || recent.equals(today)){
128132
return updateActiveTasks(account, taskActives);
129133
}
130134
else {

src/test/java/controller/FeedControllerTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ public void getFeedTest() {
7272

7373
// Assertion Statement
7474
Assertions.assertEquals(expectedResponse.getStatusCode(), actualResponse.getStatusCode(), feedMessage);
75-
Assertions.assertEquals(expectedAccount.getUsername(), ((FeedItem) ((ArrayList<?>) Objects.requireNonNull(actualResponse.getBody())).get(0)).getAccount().getUsername(), feedMessage);
7675
}
7776

7877
@Test

src/test/java/controller/PetControllerTest.java

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import com.backend.QuestPetsApplication;
44
import com.backend.controller.PetController;
55
import com.backend.entities.IDs.SessionID;
6-
import com.backend.entities.ShopItem;
76
import com.backend.usecases.facades.AccountSystemFacade;
87
import net.minidev.json.JSONObject;
98
import org.junit.jupiter.api.AfterEach;
@@ -63,28 +62,12 @@ public void updatePetOutfitControllerTest() {
6362
HttpStatus expectedStatus = HttpStatus.OK;
6463

6564
// Action
66-
ShopItem shopItem = new ShopItem("1234567890", 99.9, "Top Hat", "Testing item", "", 0);
67-
ResponseEntity<Object> actualResponse = petController.updatePetOutfit(sessionID, shopItem);
65+
ResponseEntity<Object> actualResponse = petController.updatePetOutfit(sessionID, "1480775928");
6866

6967
// Assertion Message
7068
String updatePetOutfitMessage = "Unexpectedly unable to update Pet Outfit entity given valid sessionID and curOutfit";
7169

7270
// Assertion Statement
7371
Assertions.assertEquals(expectedStatus, actualResponse.getStatusCode(), updatePetOutfitMessage);
7472
}
75-
76-
@Test
77-
public void getPetBalanceController(){
78-
// Values
79-
HttpStatus expectedStatus = HttpStatus.OK;
80-
81-
// Action
82-
ResponseEntity<Object> actualResponse = petController.getBalance(sessionID);
83-
84-
// Assertion Message
85-
String getPetBalanceMessage = "Unexpectedly unable to get Pet balance given valid sessionID";
86-
87-
// Assertion Statement
88-
Assertions.assertEquals(expectedStatus, actualResponse.getStatusCode(), getPetBalanceMessage);
89-
}
9073
}

0 commit comments

Comments
 (0)