Skip to content

Commit

Permalink
[#11878] Add methods to get an account request by ID (#12953)
Browse files Browse the repository at this point in the history
* Add facade logic method to get an account request by ID

* Add storage method to get an account request by ID

* Add logic method to get an account request by ID
  • Loading branch information
jayasting98 authored Mar 28, 2024
1 parent 5618370 commit 0aa7dfb
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/it/java/teammates/it/sqllogic/core/AccountRequestsLogicIT.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package teammates.it.sqllogic.core;

import java.time.Instant;
import java.util.UUID;

import org.testng.annotations.Test;

Expand All @@ -20,6 +21,23 @@ public class AccountRequestsLogicIT extends BaseTestCaseWithSqlDatabaseAccess {

private AccountRequestsLogic accountRequestsLogic = AccountRequestsLogic.inst();

@Test
public void testGetAccountRequest_nonExistentAccountRequest_returnsNull() {
UUID id = UUID.randomUUID();
AccountRequest actualAccountRequest = accountRequestsLogic.getAccountRequest(id);
assertNull(actualAccountRequest);
}

@Test
public void testGetAccountRequest_existingAccountRequest_getsSuccessfully() throws InvalidParametersException {
AccountRequest expectedAccountRequest =
new AccountRequest("test@gmail.com", "name", "institute", AccountRequestStatus.PENDING, "comments");
UUID id = expectedAccountRequest.getId();
accountRequestsLogic.createAccountRequest(expectedAccountRequest);
AccountRequest actualAccountRequest = accountRequestsLogic.getAccountRequest(id);
assertEquals(expectedAccountRequest, actualAccountRequest);
}

@Test
public void testResetAccountRequest()
throws EntityAlreadyExistsException, InvalidParametersException, EntityDoesNotExistException {
Expand Down
19 changes: 19 additions & 0 deletions src/it/java/teammates/it/storage/sqlapi/AccountRequestsDbIT.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package teammates.it.storage.sqlapi;

import java.util.List;
import java.util.UUID;

import org.testng.annotations.Test;

import teammates.common.datatransfer.AccountRequestStatus;
import teammates.common.exception.EntityDoesNotExistException;
import teammates.common.exception.InvalidParametersException;
import teammates.it.test.BaseTestCaseWithSqlDatabaseAccess;
import teammates.storage.sqlapi.AccountRequestsDb;
import teammates.storage.sqlentity.AccountRequest;
Expand Down Expand Up @@ -72,6 +74,23 @@ public void testCreateReadDeleteAccountRequest() throws Exception {
assertNull(actualAccountRequest);
}

@Test
public void testGetAccountRequest_nonExistentAccountRequest_returnsNull() {
UUID id = UUID.randomUUID();
AccountRequest actualAccountRequest = accountRequestDb.getAccountRequest(id);
assertNull(actualAccountRequest);
}

@Test
public void testGetAccountRequest_existingAccountRequest_getsSuccessfully() throws InvalidParametersException {
AccountRequest expectedAccountRequest =
new AccountRequest("test@gmail.com", "name", "institute", AccountRequestStatus.PENDING, "comments");
UUID id = expectedAccountRequest.getId();
accountRequestDb.createAccountRequest(expectedAccountRequest);
AccountRequest actualAccountRequest = accountRequestDb.getAccountRequest(id);
assertEquals(expectedAccountRequest, actualAccountRequest);
}

@Test
public void testUpdateAccountRequest() throws Exception {
______TS("Update account request, does not exists, exception thrown");
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/teammates/sqllogic/api/Logic.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,15 @@ public AccountRequest createAccountRequest(String name, String email, String ins
return accountRequestLogic.createAccountRequest(name, email, institute, status, comments);
}

/**
* Gets the account request with the given {@code id}.
*
* @return account request with the given {@code id}.
*/
public AccountRequest getAccountRequest(UUID id) {
return accountRequestLogic.getAccountRequest(id);
}

/**
* Gets the account request with the given email and institute.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package teammates.sqllogic.core;

import java.util.List;
import java.util.UUID;

import teammates.common.datatransfer.AccountRequestStatus;
import teammates.common.exception.EntityDoesNotExistException;
Expand Down Expand Up @@ -65,6 +66,13 @@ public AccountRequest createAccountRequest(String name, String email, String ins
return accountRequestDb.createAccountRequest(toCreate);
}

/**
* Gets the account request associated with the {@code id}.
*/
public AccountRequest getAccountRequest(UUID id) {
return accountRequestDb.getAccountRequest(id);
}

/**
* Gets account request associated with the {@code email} and {@code institute}.
*/
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/teammates/storage/sqlapi/AccountRequestsDb.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ public AccountRequest createAccountRequest(AccountRequest accountRequest) throws
return accountRequest;
}

/**
* Get AccountRequest by {@code id} from the database.
*/
public AccountRequest getAccountRequest(UUID id) {
assert id != null;
return HibernateUtil.get(AccountRequest.class, id);
}

/**
* Get AccountRequest by {@code email} and {@code institute} from database.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package teammates.sqllogic.core;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.UUID;

import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import teammates.common.datatransfer.AccountRequestStatus;
import teammates.storage.sqlapi.AccountRequestsDb;
import teammates.storage.sqlentity.AccountRequest;
import teammates.test.BaseTestCase;

/**
* SUT: {@link AccountRequestsLogic}.
*/
public class AccountRequestsLogicTest extends BaseTestCase {

private AccountRequestsLogic accountRequestsLogic = AccountRequestsLogic.inst();

private AccountRequestsDb accountRequestsDb;

@BeforeMethod
public void setUpMethod() {
accountRequestsDb = mock(AccountRequestsDb.class);
accountRequestsLogic.initLogicDependencies(accountRequestsDb);
}

@Test
public void testGetAccountRequest_nonExistentAccountRequest_returnsNull() {
UUID id = UUID.randomUUID();
when(accountRequestsDb.getAccountRequest(id)).thenReturn(null);
AccountRequest actualAccountRequest = accountRequestsLogic.getAccountRequest(id);
verify(accountRequestsDb).getAccountRequest(id);
assertNull(actualAccountRequest);
}

@Test
public void testGetAccountRequest_existingAccountRequest_getsSuccessfully() {
AccountRequest expectedAccountRequest =
new AccountRequest("test@gmail.com", "name", "institute", AccountRequestStatus.PENDING, "comments");
UUID id = expectedAccountRequest.getId();
when(accountRequestsDb.getAccountRequest(id)).thenReturn(expectedAccountRequest);
AccountRequest actualAccountRequest = accountRequestsLogic.getAccountRequest(id);
verify(accountRequestsDb).getAccountRequest(id);
assertEquals(expectedAccountRequest, actualAccountRequest);
}
}
21 changes: 21 additions & 0 deletions src/test/java/teammates/storage/sqlapi/AccountRequestsDbTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import static org.mockito.Mockito.verify;

import java.util.List;
import java.util.UUID;

import org.mockito.MockedStatic;
import org.testng.annotations.AfterMethod;
Expand Down Expand Up @@ -70,6 +71,26 @@ public void testCreateAccountRequest_accountRequestAlreadyExists_createsSuccessf
mockHibernateUtil.verify(() -> HibernateUtil.persist(accountRequest));
}

@Test
public void testGetAccountRequest_nonExistentAccountRequest_returnsNull() {
UUID id = UUID.randomUUID();
mockHibernateUtil.when(() -> HibernateUtil.get(AccountRequest.class, id)).thenReturn(null);
AccountRequest actualAccountRequest = accountRequestDb.getAccountRequest(id);
mockHibernateUtil.verify(() -> HibernateUtil.get(AccountRequest.class, id));
assertNull(actualAccountRequest);
}

@Test
public void testGetAccountRequest_existingAccountRequest_getsSuccessfully() {
AccountRequest expectedAccountRequest =
new AccountRequest("test@gmail.com", "name", "institute", AccountRequestStatus.PENDING, "comments");
UUID id = expectedAccountRequest.getId();
mockHibernateUtil.when(() -> HibernateUtil.get(AccountRequest.class, id)).thenReturn(expectedAccountRequest);
AccountRequest actualAccountRequest = accountRequestDb.getAccountRequest(id);
mockHibernateUtil.verify(() -> HibernateUtil.get(AccountRequest.class, id));
assertEquals(expectedAccountRequest, actualAccountRequest);
}

@Test
public void testUpdateAccountRequest_invalidEmail_throwsInvalidParametersException() {
AccountRequest accountRequestWithInvalidEmail =
Expand Down

0 comments on commit 0aa7dfb

Please sign in to comment.