forked from cdos-rla/colorado-rla
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #225 from DemocracyDevelopers/models-tests
More data model tests
- Loading branch information
Showing
5 changed files
with
217 additions
and
23 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
115 changes: 115 additions & 0 deletions
115
server/eclipse-project/src/test/java/us/freeandfair/corla/model/AdministratorTest.java
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,115 @@ | ||
package us.freeandfair.corla.model; | ||
|
||
import org.testng.annotations.Test; | ||
import us.freeandfair.corla.persistence.Persistence; | ||
import us.freeandfair.corla.util.TestClassWithDatabase; | ||
|
||
import java.time.Clock; | ||
import java.time.Instant; | ||
|
||
import static org.testng.Assert.assertNotEquals; | ||
import static org.testng.AssertJUnit.*; | ||
import static us.freeandfair.corla.util.EqualsHashcodeHelper.nullableHashCode; | ||
|
||
@Test | ||
public class AdministratorTest extends TestClassWithDatabase { | ||
|
||
@Test | ||
public static void testGettersAndSetters() { | ||
String expectedUsername = "testname"; | ||
Administrator.AdministratorType expectedType = Administrator.AdministratorType.STATE; | ||
String expectedFullname = "fulltestname"; | ||
|
||
// Make the clock stuck | ||
Clock testClock = Clock.fixed(Instant.now(), Clock.systemDefaultZone().getZone()); | ||
Administrator admin = new Administrator(expectedUsername, | ||
expectedType, | ||
expectedFullname, | ||
null, | ||
testClock); | ||
|
||
|
||
assertNull(admin.id()); | ||
assertNull(admin.version()); | ||
Persistence.saveOrUpdate(admin); | ||
|
||
// this is a database constraint | ||
assertNotNull(admin.id()); | ||
|
||
Long expectedID = 42L; | ||
admin.setID(expectedID); | ||
assertEquals(expectedID, admin.id()); | ||
|
||
assertEquals(expectedUsername, admin.username()); | ||
assertEquals(expectedFullname, admin.fullName()); | ||
assertEquals(expectedType, admin.type()); | ||
assertNull(admin.county()); | ||
|
||
// Because we're using a stopped clock, this time will be the same for both | ||
Instant expectedTime = testClock.instant(); | ||
assertNull(admin.lastLoginTime()); | ||
admin.updateLastLoginTime(); | ||
assertEquals(expectedTime, admin.lastLoginTime()); | ||
|
||
assertNull(admin.lastLogoutTime()); | ||
admin.updateLastLogoutTime(); | ||
assertEquals(expectedTime, admin.lastLogoutTime()); | ||
|
||
String expected_string = "Administrator [username=" + expectedUsername + ", type=" + | ||
expectedType+ ", full_name=" + expectedFullname+ ", county=" + | ||
null + ", last_login_time=" + expectedTime + | ||
", last_logout_time=" + expectedTime + "]"; | ||
|
||
assertEquals(expected_string, admin.toString()); | ||
|
||
/** | ||
* Now test an admin without a clock set. This means we can't test timing-specific things, | ||
* but we can at least test that the code paths behave the way we expect. | ||
*/ | ||
admin = new Administrator(expectedUsername, | ||
expectedType, | ||
expectedFullname, | ||
null); | ||
|
||
assertNull(admin.lastLoginTime()); | ||
assertNull(admin.lastLogoutTime()); | ||
|
||
admin.updateLastLoginTime(); | ||
assertNotNull(admin.lastLoginTime()); | ||
admin.updateLastLogoutTime(); | ||
assertNotNull(admin.lastLogoutTime()); | ||
assert(admin.lastLoginTime() != admin.lastLogoutTime()); | ||
} | ||
|
||
@Test | ||
public static void testEquality() { | ||
String firstAdminUsername = "first"; | ||
String secondAdminUsername = "second"; | ||
Administrator.AdministratorType expectedType = Administrator.AdministratorType.STATE; | ||
String expectedFullname = "fulltestname"; | ||
|
||
Administrator firstAdmin = new Administrator(firstAdminUsername, expectedType, expectedFullname, null); | ||
Administrator secondAdmin = new Administrator(secondAdminUsername, expectedType, expectedFullname, null); | ||
|
||
assertTrue(firstAdmin.equals(firstAdmin)); | ||
assertFalse(firstAdmin.equals(secondAdmin)); | ||
|
||
// Now test that non-admin objects result in false | ||
assertFalse(firstAdmin.equals(firstAdminUsername)); | ||
} | ||
|
||
@Test | ||
public static void testHash() { | ||
String expectedUsername = "testname"; | ||
Administrator.AdministratorType expectedType = Administrator.AdministratorType.STATE; | ||
String expectedFullname = "fulltestname"; | ||
|
||
Administrator admin = new Administrator(expectedUsername, | ||
expectedType, | ||
expectedFullname, | ||
null); | ||
|
||
assertEquals(admin.hashCode(), nullableHashCode(expectedUsername)); | ||
|
||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
server/eclipse-project/src/test/java/us/freeandfair/corla/model/AuditBoardTest.java
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,59 @@ | ||
package us.freeandfair.corla.model; | ||
|
||
import org.testng.annotations.Test; | ||
import us.freeandfair.corla.util.TestClassWithDatabase; | ||
|
||
import java.lang.reflect.Array; | ||
import java.time.Instant; | ||
|
||
import java.util.ArrayList; | ||
|
||
import static org.testng.AssertJUnit.*; | ||
import static us.freeandfair.corla.util.EqualsHashcodeHelper.nullableHashCode; | ||
|
||
public class AuditBoardTest extends TestClassWithDatabase { | ||
|
||
@Test | ||
public static void testGettersAndSetters () { | ||
|
||
AuditBoard defaultAB = new AuditBoard(); | ||
assertEquals(new ArrayList<Elector>(), defaultAB.members()); | ||
assertNull(defaultAB.signInTime()); | ||
assertNull(defaultAB.signOutTime()); | ||
|
||
ArrayList<Elector> electors = new ArrayList<>(); | ||
|
||
electors.add(new Elector("firstname", "lastname", "party")); | ||
|
||
// TODO: do we want a stopped clock here too? | ||
Instant sign_in_time = Instant.now(); | ||
|
||
AuditBoard ab = new AuditBoard(electors, sign_in_time); | ||
|
||
assertEquals(electors, ab.members()); | ||
|
||
assertEquals(sign_in_time, ab.signInTime()); | ||
|
||
Instant sign_out_time = Instant.now(); | ||
|
||
ab.setSignOutTime(sign_out_time); | ||
assertEquals(sign_out_time, ab.signOutTime()); | ||
|
||
String expectedToString = "AuditBoard [members=" + electors + ", sign_in_time=" + | ||
sign_in_time + ", sign_out_time=" + sign_out_time + "]"; | ||
|
||
assertEquals(expectedToString, ab.toString()); | ||
|
||
assertEquals(nullableHashCode(sign_in_time), ab.hashCode()); | ||
|
||
assertFalse(ab.equals("Not an auditboard")); | ||
ArrayList<Elector> otherElectors = new ArrayList<>(); | ||
otherElectors.add(new Elector("otherfirstname", "otherlastname", "otherparty")); | ||
|
||
AuditBoard otherAB = new AuditBoard(otherElectors, sign_in_time); | ||
|
||
assertFalse(ab.equals(otherAB)); | ||
assertTrue(ab.equals(ab)); | ||
|
||
} | ||
} |
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