Skip to content

Commit 057e70c

Browse files
kurtislawShaanPurewal
authored andcommitted
Javadoc and Testing for Feed System
1 parent b9b873e commit 057e70c

File tree

4 files changed

+207
-1
lines changed

4 files changed

+207
-1
lines changed

src/main/java/com/backend/entities/FeedItem.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,26 @@ public FeedItem(ProtectedAccount account, TaskCompletionRecord record, Pet pet){
1515
this.pet = pet;
1616
}
1717

18+
/**
19+
* Get the ProtectedAccount information associated with this feedItem
20+
* @return the ProtectedAccount for this instance
21+
*/
1822
public ProtectedAccount getAccount() {
1923
return this.account;
2024
}
2125

26+
/**
27+
* Get the Pet information associated with this feedItem
28+
* @return the Pet for this instance
29+
*/
2230
public Pet getPet() {
2331
return this.pet;
2432
}
2533

34+
/**
35+
* Get the TaskCompletionRecord information associated with this feedItem
36+
* @return the TaskCompletionRecord for this instance
37+
*/
2638
@SuppressWarnings("unused")
2739
public TaskCompletionRecord getRecord() {
2840
return this.record;

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ public class FeedSystemFacade {
2525
private final PetManager petManager;
2626
private final IErrorHandler errorHandler;
2727

28+
/**
29+
* Spring Boot Dependency Injection
30+
* @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+
*/
2835
@Autowired
2936
public FeedSystemFacade(AccountManager accountManager, TaskManager taskManager, PetManager petManager, IErrorHandler errorHandler) {
3037
this.accountManager = accountManager;
@@ -33,6 +40,11 @@ public FeedSystemFacade(AccountManager accountManager, TaskManager taskManager,
3340
this.errorHandler = errorHandler;
3441
}
3542

43+
/**
44+
* Get a list of feed data representing information regarding recent posts made by friends
45+
* @param sessionID of type String, password to reference associated account
46+
* @return a response entity detailing successful completion (with a newly generated SessionID) or any associated error
47+
*/
3648
public ResponseEntity<Object> getFeed(String sessionID) {
3749
// verify session
3850
AccountID accountID = this.accountManager.verifySession(new SessionID(sessionID));
@@ -41,7 +53,7 @@ public ResponseEntity<Object> getFeed(String sessionID) {
4153
}
4254

4355
// get list of friends
44-
List<String> friends = new ArrayList<>(List.of("3q'Xc3z!If9W`Yf3G%Ms", "9j:Op4y<Ga6R+Cn9R>Lc", "1Q.Wm4V`Dm2Q$Fz8m]Ec"));
56+
List<String> friends = new ArrayList<>(List.of("7T~Sp2w%Vl9t\"Wk4V]Fp"));
4557

4658
// wrap FeedItem Objects
4759
List<FeedItem> feedItems = new ArrayList<>();
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package controller;
2+
3+
import com.backend.QuestPetsApplication;
4+
import com.backend.controller.FeedController;
5+
import com.backend.entities.FeedItem;
6+
import com.backend.entities.IDs.AccountID;
7+
import com.backend.entities.IDs.SessionID;
8+
import com.backend.entities.Pet;
9+
import com.backend.entities.TaskCompletionRecord;
10+
import com.backend.entities.users.ProtectedAccount;
11+
import com.backend.usecases.facades.AccountSystemFacade;
12+
import net.minidev.json.JSONObject;
13+
import org.junit.jupiter.api.AfterEach;
14+
import org.junit.jupiter.api.Assertions;
15+
import org.junit.jupiter.api.BeforeEach;
16+
import org.junit.jupiter.api.Test;
17+
import org.springframework.beans.factory.annotation.Autowired;
18+
import org.springframework.boot.test.context.SpringBootTest;
19+
import org.springframework.http.HttpStatus;
20+
import org.springframework.http.ResponseEntity;
21+
22+
import java.util.ArrayList;
23+
import java.util.List;
24+
import java.util.Objects;
25+
26+
@SpringBootTest(classes = QuestPetsApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
27+
public class FeedControllerTest {
28+
private final AccountSystemFacade accountSystemFacade;
29+
private final FeedController feedController;
30+
private SessionID sessionID;
31+
32+
@Autowired
33+
public FeedControllerTest(AccountSystemFacade accountSystemFacade, FeedController feedController) {
34+
this.accountSystemFacade = accountSystemFacade;
35+
this.feedController = feedController;
36+
}
37+
38+
@BeforeEach
39+
public void setup() {
40+
String username = "username";
41+
String password = "abc123!";
42+
ResponseEntity<Object> register = this.accountSystemFacade.registerAccount(username, password);
43+
44+
if (!(register.getStatusCode() == HttpStatus.OK)){
45+
sessionID = new SessionID((String) ((JSONObject) Objects.requireNonNull(this.accountSystemFacade.loginAccount(username, password).getBody())).get("sessionID"));
46+
} else {
47+
sessionID = new SessionID((String) ((JSONObject) Objects.requireNonNull(register.getBody())).get("sessionID"));
48+
}
49+
50+
}
51+
52+
@AfterEach
53+
public void tearDown() {
54+
if (sessionID != null) {
55+
this.accountSystemFacade.logoutAccount(this.sessionID);
56+
}
57+
}
58+
59+
@Test
60+
public void getFeedTest() {
61+
// Values
62+
ProtectedAccount expectedAccount = new ProtectedAccount("usernameFriend");
63+
TaskCompletionRecord expectedTaskCompletionRecord = new TaskCompletionRecord(new AccountID(null), null, null, null);
64+
Pet expectedPet = new Pet(null, null, null, null, null);
65+
ResponseEntity<Object> expectedResponse = new ResponseEntity<>(new ArrayList<>(List.of(new FeedItem(expectedAccount, expectedTaskCompletionRecord, expectedPet))), HttpStatus.OK);
66+
67+
// Action
68+
ResponseEntity<Object> actualResponse = this.feedController.getFeed(sessionID.getID());
69+
70+
// Assertion Message
71+
String feedMessage = "The given sessionID didn't yield the corresponding feed information";
72+
73+
// Assertion Statement
74+
Assertions.assertEquals(expectedResponse.getStatusCode(), actualResponse.getStatusCode(), feedMessage);
75+
Assertions.assertEquals(expectedAccount.getUsername(), ((FeedItem) ((ArrayList<?>) Objects.requireNonNull(actualResponse.getBody())).get(0)).getAccount().getUsername(), feedMessage);
76+
}
77+
78+
@Test
79+
public void getFeedInvalidSessionTest() {
80+
// Values (Not Required)
81+
82+
// Action
83+
ResponseEntity<Object> actualResponse = this.feedController.getFeed("InvalidSession");
84+
85+
// Assertion Message
86+
String feedMessage = "The given sessionID unexpectedly yielded the corresponding feed information";
87+
88+
// Assertion Statement
89+
Assertions.assertEquals(HttpStatus.BAD_REQUEST, actualResponse.getStatusCode(), feedMessage);
90+
}
91+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package usecases.facades;
2+
3+
import com.backend.QuestPetsApplication;
4+
import com.backend.entities.FeedItem;
5+
import com.backend.entities.IDs.AccountID;
6+
import com.backend.entities.IDs.SessionID;
7+
import com.backend.entities.Pet;
8+
import com.backend.entities.TaskCompletionRecord;
9+
import com.backend.entities.users.ProtectedAccount;
10+
import com.backend.usecases.facades.AccountSystemFacade;
11+
import com.backend.usecases.facades.FeedSystemFacade;
12+
import net.minidev.json.JSONObject;
13+
import org.junit.jupiter.api.AfterEach;
14+
import org.junit.jupiter.api.Assertions;
15+
import org.junit.jupiter.api.BeforeEach;
16+
import org.junit.jupiter.api.Test;
17+
import org.springframework.beans.factory.annotation.Autowired;
18+
import org.springframework.boot.test.context.SpringBootTest;
19+
import org.springframework.http.HttpStatus;
20+
import org.springframework.http.ResponseEntity;
21+
22+
import java.util.ArrayList;
23+
import java.util.List;
24+
import java.util.Objects;
25+
26+
@SpringBootTest(classes = QuestPetsApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
27+
public class FeedSystemFacadeTest {
28+
private final AccountSystemFacade accountSystemFacade;
29+
private final FeedSystemFacade feedSystemFacade;
30+
private SessionID sessionID;
31+
32+
@Autowired
33+
public FeedSystemFacadeTest(AccountSystemFacade accountSystemFacade, FeedSystemFacade feedSystemFacade) {
34+
this.accountSystemFacade = accountSystemFacade;
35+
this.feedSystemFacade = feedSystemFacade;
36+
}
37+
38+
@BeforeEach
39+
public void setup() {
40+
String username = "username";
41+
String password = "abc123!";
42+
ResponseEntity<Object> register = this.accountSystemFacade.registerAccount(username, password);
43+
44+
if (!(register.getStatusCode() == HttpStatus.OK)){
45+
sessionID = new SessionID((String) ((JSONObject) Objects.requireNonNull(this.accountSystemFacade.loginAccount(username, password).getBody())).get("sessionID"));
46+
} else {
47+
sessionID = new SessionID((String) ((JSONObject) Objects.requireNonNull(register.getBody())).get("sessionID"));
48+
}
49+
50+
}
51+
52+
@AfterEach
53+
public void tearDown() {
54+
if (sessionID != null) {
55+
this.accountSystemFacade.logoutAccount(this.sessionID);
56+
}
57+
}
58+
59+
@Test
60+
public void getFeedTest() {
61+
// Values
62+
ProtectedAccount expectedAccount = new ProtectedAccount("usernameFriend");
63+
TaskCompletionRecord expectedTaskCompletionRecord = new TaskCompletionRecord(new AccountID(null), null, null, null);
64+
Pet expectedPet = new Pet(null, null, null, null, null);
65+
ResponseEntity<Object> expectedResponse = new ResponseEntity<>(new ArrayList<>(List.of(new FeedItem(expectedAccount, expectedTaskCompletionRecord, expectedPet))), HttpStatus.OK);
66+
67+
// Action
68+
ResponseEntity<Object> actualResponse = this.feedSystemFacade.getFeed(sessionID.getID());
69+
70+
// Assertion Message
71+
String feedMessage = "The given sessionID didn't yield the corresponding feed information";
72+
73+
// Assertion Statement
74+
Assertions.assertEquals(expectedResponse.getStatusCode(), actualResponse.getStatusCode(), feedMessage);
75+
Assertions.assertEquals(expectedAccount.getUsername(), ((FeedItem) ((ArrayList<?>) Objects.requireNonNull(actualResponse.getBody())).get(0)).getAccount().getUsername(), feedMessage);
76+
}
77+
78+
@Test
79+
public void getFeedInvalidSessionTest() {
80+
// Values (Not Required)
81+
82+
// Action
83+
ResponseEntity<Object> actualResponse = this.feedSystemFacade.getFeed("InvalidSession");
84+
85+
// Assertion Message
86+
String feedMessage = "The given sessionID unexpectedly yielded the corresponding feed information";
87+
88+
// Assertion Statement
89+
Assertions.assertEquals(HttpStatus.BAD_REQUEST, actualResponse.getStatusCode(), feedMessage);
90+
}
91+
}

0 commit comments

Comments
 (0)