Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#11878] Add methods to get an account request by ID #12953

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall we also remove the old methods (and update places of usage) as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is only the first PR. There's more that needs to be done. I'm doing this first, because after this, then I think the other usages can then probably be updated in parallel.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, let's merge this first

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