|
| 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