-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
f077dac
commit 256e3cf
Showing
9 changed files
with
340 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package teammates.it.storage.sqlapi; | ||
|
||
import org.testng.annotations.Test; | ||
|
||
import teammates.common.exception.EntityAlreadyExistsException; | ||
import teammates.common.exception.InvalidParametersException; | ||
import teammates.common.util.HibernateUtil; | ||
import teammates.it.test.BaseTestCaseWithSqlDatabaseAccess; | ||
import teammates.storage.sqlapi.AccountsDb; | ||
import teammates.storage.sqlentity.Account; | ||
|
||
/** | ||
* SUT: {@link AccountsDb}. | ||
*/ | ||
public class AccountsDbIT extends BaseTestCaseWithSqlDatabaseAccess { | ||
|
||
private final AccountsDb accountsDb = AccountsDb.inst(); | ||
|
||
@Test | ||
public void testCreateAccount() throws Exception { | ||
______TS("Create account, does not exists, succeeds"); | ||
|
||
Account account = new Account("google-id", "name", "[email protected]"); | ||
|
||
accountsDb.createAccount(account); | ||
HibernateUtil.flushSession(); | ||
|
||
Account actualAccount = accountsDb.getAccount(account.getId()); | ||
verifyEquals(account, actualAccount); | ||
} | ||
|
||
@Test | ||
public void testUpdateAccount() throws Exception { | ||
Account account = new Account("google-id", "name", "[email protected]"); | ||
accountsDb.createAccount(account); | ||
HibernateUtil.flushSession(); | ||
|
||
______TS("Update existing account, success"); | ||
|
||
account.setName("new account name"); | ||
accountsDb.updateAccount(account); | ||
|
||
Account actual = accountsDb.getAccount(account.getId()); | ||
verifyEquals(account, actual); | ||
} | ||
|
||
@Test | ||
public void testDeleteAccount() throws InvalidParametersException, EntityAlreadyExistsException { | ||
Account account = new Account("google-id", "name", "[email protected]"); | ||
accountsDb.createAccount(account); | ||
HibernateUtil.flushSession(); | ||
|
||
______TS("Delete existing account, success"); | ||
|
||
accountsDb.deleteAccount(account); | ||
|
||
Account actual = accountsDb.getAccount(account.getId()); | ||
assertNull(actual); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package teammates.storage.sqlapi; | ||
|
||
import teammates.common.exception.EntityAlreadyExistsException; | ||
import teammates.common.exception.EntityDoesNotExistException; | ||
import teammates.common.exception.InvalidParametersException; | ||
import teammates.common.util.HibernateUtil; | ||
import teammates.storage.sqlentity.Account; | ||
|
||
/** | ||
* Handles CRUD operations for accounts. | ||
* | ||
* @see Account | ||
*/ | ||
public final class AccountsDb extends EntitiesDb<Account> { | ||
|
||
private static final AccountsDb instance = new AccountsDb(); | ||
|
||
private AccountsDb() { | ||
// prevent initialization | ||
} | ||
|
||
public static AccountsDb inst() { | ||
return instance; | ||
} | ||
|
||
/** | ||
* Returns an Account with the {@code id} or null if it does not exist. | ||
*/ | ||
public Account getAccount(Integer id) { | ||
assert id != null; | ||
|
||
return HibernateUtil.get(Account.class, id); | ||
} | ||
|
||
/** | ||
* Returns an Account with the {@code googleId} or null if it does not exist. | ||
*/ | ||
public Account getAccountByGoogleId(String googleId) { | ||
assert googleId != null; | ||
|
||
return HibernateUtil.getBySimpleNaturalId(Account.class, googleId); | ||
} | ||
|
||
/** | ||
* Creates an Account. | ||
*/ | ||
public Account createAccount(Account account) throws InvalidParametersException, EntityAlreadyExistsException { | ||
assert account != null; | ||
|
||
if (!account.isValid()) { | ||
throw new InvalidParametersException(account.getInvalidityInfo()); | ||
} | ||
|
||
if (getAccountByGoogleId(account.getGoogleId()) != null) { | ||
throw new EntityAlreadyExistsException(String.format(ERROR_CREATE_ENTITY_ALREADY_EXISTS, account.toString())); | ||
} | ||
|
||
persist(account); | ||
return account; | ||
} | ||
|
||
/** | ||
* Saves an updated {@code Account} to the db. | ||
*/ | ||
public Account updateAccount(Account account) throws InvalidParametersException, EntityDoesNotExistException { | ||
assert account != null; | ||
|
||
if (!account.isValid()) { | ||
throw new InvalidParametersException(account.getInvalidityInfo()); | ||
} | ||
|
||
if (getAccount(account.getId()) == null) { | ||
throw new EntityDoesNotExistException(ERROR_UPDATE_NON_EXISTENT + account.toString()); | ||
} | ||
|
||
return merge(account); | ||
} | ||
|
||
/** | ||
* Deletes an Account. | ||
*/ | ||
public void deleteAccount(Account account) { | ||
if (account != null) { | ||
delete(account); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.