Skip to content

Commit

Permalink
Administrator model has 100% coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
umbernhard committed Dec 3, 2024
1 parent d95fd9f commit 351e3e3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,7 @@
import java.time.Clock;
import java.time.Instant;

import javax.persistence.Cacheable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Index;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import javax.persistence.Version;
import javax.persistence.*;

import us.freeandfair.corla.persistence.PersistentEntity;

Expand Down Expand Up @@ -111,6 +97,7 @@ public class Administrator implements PersistentEntity, Serializable {
/**
* A clock that is only used for testing
*/
@Transient
private Clock my_clock;

/**
Expand Down Expand Up @@ -153,7 +140,7 @@ public Administrator(final String the_username,
final AdministratorType the_type,
final String the_full_name,
final County the_county,
Clock clock) {
final Clock clock) {
this(the_username, the_type, the_full_name, the_county);
my_clock = clock;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package us.freeandfair.corla.model;

import com.github.tomakehurst.wiremock.core.Admin;
import org.testng.AssertJUnit;
import org.testng.annotations.Test;
import us.freeandfair.corla.persistence.Persistence;
import us.freeandfair.corla.query.AdministratorQueries;
Expand All @@ -8,7 +10,9 @@
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 {
Expand All @@ -27,7 +31,9 @@ public static void testGettersAndSetters() {
null,
testClock);


assertNull(admin.id());
assertNull(admin.version());
Persistence.saveOrUpdate(admin);

// this is a database constraint
Expand All @@ -40,7 +46,6 @@ public static void testGettersAndSetters() {
assertEquals(expectedUsername, admin.username());
assertEquals(expectedFullname, admin.fullName());
assertEquals(expectedType, admin.type());
assertNull(admin.version());
assertNull(admin.county());

// Because we're using a stopped clock, this time will be the same for both
Expand All @@ -58,5 +63,38 @@ public static void testGettersAndSetters() {
null + ", last_login_time=" + expectedTime +
", last_logout_time=" + expectedTime + "]";

assertEquals(expected_string, admin.toString());
}

@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));

}
}

0 comments on commit 351e3e3

Please sign in to comment.