diff --git a/elections-ejb/src/main/java/net/lacnic/elections/dao/AuditorDao.java b/elections-ejb/src/main/java/net/lacnic/elections/dao/AuditorDao.java index 8676802..6b24585 100644 --- a/elections-ejb/src/main/java/net/lacnic/elections/dao/AuditorDao.java +++ b/elections-ejb/src/main/java/net/lacnic/elections/dao/AuditorDao.java @@ -17,34 +17,17 @@ public AuditorDao(EntityManager em) { this.em = em; } - /** - * Gets Auditor by id. - * - * @param auditorId Auditor id. - * @return Returns the Auditor entity corresponding to the id - */ public Auditor getAuditor(long auditorId) { TypedQuery q = em.createQuery("SELECT a FROM Auditor a WHERE a.auditorId = :auditorId", Auditor.class); q.setParameter("auditorId", auditorId); return q.getSingleResult(); } - /** - * Gets all the auditor from the system. - * - * @return returns all the auditors present on the system. - */ public List getAuditorsAll() { TypedQuery q = em.createQuery("SELECT a FROM Auditor a", Auditor.class); return q.getResultList(); } - /** - * Gets a list of Auditors related to a particular election. - * - * @param electionId Election identifier. - * @return Returns a list of auditor related to a particular election. - */ public List getElectionAuditors(long electionId) { TypedQuery q = em.createQuery("SELECT a FROM Auditor a WHERE a.election.electionId = :electionId", Auditor.class); q.setParameter(ELECTION_ID, electionId); @@ -74,12 +57,6 @@ public Auditor getAuditorByResultToken(String resultToken) { return q.getSingleResult(); } - /** - * Gets a list of result tokens from the Auditors - * - * - * @return returns a list of result tokens from the Auditors - */ public List getAllAuditorsUUIDs() { TypedQuery q = em.createQuery("SELECT a.resultToken FROM Auditor a", String.class); return q.getResultList(); diff --git a/elections-ejb/src/main/java/net/lacnic/elections/dao/ElectionEmailTemplateDao.java b/elections-ejb/src/main/java/net/lacnic/elections/dao/ElectionEmailTemplateDao.java index fb37704..22ca512 100644 --- a/elections-ejb/src/main/java/net/lacnic/elections/dao/ElectionEmailTemplateDao.java +++ b/elections-ejb/src/main/java/net/lacnic/elections/dao/ElectionEmailTemplateDao.java @@ -11,13 +11,14 @@ import net.lacnic.elections.domain.ElectionEmailTemplate; +import static net.lacnic.elections.utils.Constants.ELECTION_ID; +import static net.lacnic.elections.utils.Constants.TEMPLATE_TYPE; + public class ElectionEmailTemplateDao { private static final Logger appLogger = LogManager.getLogger("ejbAppLogger"); private EntityManager em; - private static final String ELECTION_ID = "electionId"; - private static final String TEMPLATE_TYPE = "templateType"; public ElectionEmailTemplateDao(EntityManager em) { this.em = em; diff --git a/elections-ejb/src/main/java/net/lacnic/elections/dao/IpAccessDao.java b/elections-ejb/src/main/java/net/lacnic/elections/dao/IpAccessDao.java index 1eded13..5254b44 100644 --- a/elections-ejb/src/main/java/net/lacnic/elections/dao/IpAccessDao.java +++ b/elections-ejb/src/main/java/net/lacnic/elections/dao/IpAccessDao.java @@ -11,7 +11,6 @@ import net.lacnic.elections.domain.IpAccess; - public class IpAccessDao { private static final Logger appLogger = LogManager.getLogger("ejbAppLogger"); diff --git a/elections-ejb/src/main/java/net/lacnic/elections/dao/ReportDao.java b/elections-ejb/src/main/java/net/lacnic/elections/dao/ReportDao.java index 769a1f0..0dbf1fb 100644 --- a/elections-ejb/src/main/java/net/lacnic/elections/dao/ReportDao.java +++ b/elections-ejb/src/main/java/net/lacnic/elections/dao/ReportDao.java @@ -1,9 +1,8 @@ package net.lacnic.elections.dao; -import java.util.List; - import javax.persistence.EntityManager; import javax.persistence.Query; +import java.util.List; public class ReportDao { @@ -14,9 +13,6 @@ public ReportDao(EntityManager em) { this.em = em; } - public ReportDao() { - } - /** * Gets the amount of emails in the system * diff --git a/elections-ejb/src/main/java/net/lacnic/elections/domain/Election.java b/elections-ejb/src/main/java/net/lacnic/elections/domain/Election.java index 8994008..c2cb1a9 100644 --- a/elections-ejb/src/main/java/net/lacnic/elections/domain/Election.java +++ b/elections-ejb/src/main/java/net/lacnic/elections/domain/Election.java @@ -1,30 +1,20 @@ package net.lacnic.elections.domain; -import java.io.Serializable; -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.List; - -import javax.persistence.CascadeType; -import javax.persistence.Column; -import javax.persistence.Entity; -import javax.persistence.EnumType; -import javax.persistence.Enumerated; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; -import javax.persistence.OneToMany; -import javax.persistence.SequenceGenerator; -import javax.persistence.Transient; - +import net.lacnic.elections.utils.DateTimeUtils; +import net.lacnic.elections.utils.LinksUtils; +import net.lacnic.elections.utils.StringUtils; import org.apache.log4j.LogManager; import org.apache.log4j.Logger; import org.joda.time.DateTime; -import net.lacnic.elections.utils.DateTimeUtils; -import net.lacnic.elections.utils.LinksUtils; -import net.lacnic.elections.utils.StringUtils; +import javax.persistence.*; +import java.io.Serializable; +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; +import java.util.Date; +import java.util.List; @Entity public class Election implements Serializable { @@ -233,14 +223,19 @@ public void initDatesStartEndDates() { if (!getAuxEndDate().isEmpty() && !getAuxStartDate().isEmpty() && !getAuxStartHour().isEmpty() && !getAuxEndHour().isEmpty()) { String startDatetime = getAuxStartDate() + " " + getAuxStartHour(); String endDatetime = getAuxEndDate() + " " + getAuxEndHour(); - SimpleDateFormat sdf = new SimpleDateFormat(DateTimeUtils.ELECTION_DATE_TIME_FORMAT); + + DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DateTimeUtils.ELECTION_DATE_TIME_FORMAT); try { - Date beforeSubtractStart = sdf.parse(startDatetime); - Date beforeSubtractEnd = sdf.parse(endDatetime); + LocalDateTime startLocalDateTime = LocalDateTime.parse(startDatetime, formatter); + LocalDateTime endLocalDateTime = LocalDateTime.parse(endDatetime, formatter); + + Date beforeSubtractStart = Date.from(startLocalDateTime.atZone(ZoneId.systemDefault()).toInstant()); + Date beforeSubtractEnd = Date.from(endLocalDateTime.atZone(ZoneId.systemDefault()).toInstant()); + setStartDate(new DateTime(beforeSubtractStart).minusHours(getDiffUTC()).toDate()); setEndDate(new DateTime(beforeSubtractEnd).minusHours(getDiffUTC()).toDate()); - } catch (ParseException e) { + } catch (Exception e) { appLogger.error(e); } } @@ -252,14 +247,18 @@ public void initDatesStartEndDates() { public void initStringsStartEndDates() { Date unchangedStartDate = getStartDate(); Date unchangedEndDate = getEndDate(); + if (unchangedEndDate != null && unchangedStartDate != null) { - SimpleDateFormat day = new SimpleDateFormat(DateTimeUtils.ELECTION_DATE_FORMAT); - SimpleDateFormat hour = new SimpleDateFormat(DateTimeUtils.ELECTION_TIME_FORMAT); + DateTimeFormatter dayFormatter = DateTimeFormatter.ofPattern(DateTimeUtils.ELECTION_DATE_FORMAT); + DateTimeFormatter hourFormatter = DateTimeFormatter.ofPattern(DateTimeUtils.ELECTION_TIME_FORMAT); + + ZonedDateTime startZonedDateTime = unchangedStartDate.toInstant().atZone(ZoneId.systemDefault()).plusHours(getDiffUTC()); + ZonedDateTime endZonedDateTime = unchangedEndDate.toInstant().atZone(ZoneId.systemDefault()).plusHours(getDiffUTC()); - setAuxStartDate(day.format(new DateTime(getStartDate()).plusHours(getDiffUTC()).toDate())); - setAuxStartHour(hour.format(new DateTime(getStartDate()).plusHours(getDiffUTC()).toDate())); - setAuxEndDate(day.format(new DateTime(getEndDate()).plusHours(getDiffUTC()).toDate())); - setAuxEndHour(hour.format(new DateTime(getEndDate()).plusHours(getDiffUTC()).toDate())); + setAuxStartDate(startZonedDateTime.format(dayFormatter)); + setAuxStartHour(startZonedDateTime.format(hourFormatter)); + setAuxEndDate(endZonedDateTime.format(dayFormatter)); + setAuxEndHour(endZonedDateTime.format(hourFormatter)); } } diff --git a/elections-ejb/src/main/java/net/lacnic/elections/domain/ElectionLight.java b/elections-ejb/src/main/java/net/lacnic/elections/domain/ElectionLight.java index 695f5ae..9f3ac57 100644 --- a/elections-ejb/src/main/java/net/lacnic/elections/domain/ElectionLight.java +++ b/elections-ejb/src/main/java/net/lacnic/elections/domain/ElectionLight.java @@ -1,7 +1,9 @@ package net.lacnic.elections.domain; import java.io.Serializable; -import java.text.SimpleDateFormat; +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.time.format.DateTimeFormatter; import java.util.Date; import javax.persistence.Column; @@ -192,22 +194,39 @@ public void copyLanguageURLs(String language) { } public String getStartDateString() { - SimpleDateFormat simpleDateFormat = new SimpleDateFormat(SIMPLE_DATE_FORMAT); - return simpleDateFormat.format(new DateTime(getStartDate()).plusHours(getDiffUTC()).toDate()) + UTC; + DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(SIMPLE_DATE_FORMAT); + LocalDateTime adjustedDateTime = new DateTime(getStartDate()) + .plusHours(getDiffUTC()) + .toDate() + .toInstant() + .atZone(ZoneId.systemDefault()) + .toLocalDateTime(); + return adjustedDateTime.format(dateTimeFormatter) + UTC; } public String getEndDateString() { - SimpleDateFormat simpleDateFormat = new SimpleDateFormat(SIMPLE_DATE_FORMAT); - return simpleDateFormat.format(new DateTime(getEndDate()).plusHours(getDiffUTC()).toDate()) + UTC; + DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(SIMPLE_DATE_FORMAT); + LocalDateTime adjustedDateTime = new DateTime(getEndDate()) + .plusHours(getDiffUTC()) + .toDate() + .toInstant() + .atZone(ZoneId.systemDefault()) + .toLocalDateTime(); + return adjustedDateTime.format(dateTimeFormatter) + UTC; } public String getClosedDateString() { - SimpleDateFormat simpleDateFormat = new SimpleDateFormat(SIMPLE_DATE_FORMAT); if (this.getClosedDate() == null) { return ""; - } else { - return simpleDateFormat.format(new DateTime(getClosedDate()).plusHours(getDiffUTC()).toDate()) + UTC; } + DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(SIMPLE_DATE_FORMAT); + LocalDateTime adjustedDateTime = new DateTime(getClosedDate()) + .plusHours(getDiffUTC()) + .toDate() + .toInstant() + .atZone(ZoneId.systemDefault()) + .toLocalDateTime(); + return adjustedDateTime.format(dateTimeFormatter) + UTC; } public String getResultLink() { diff --git a/elections-ejb/src/main/java/net/lacnic/elections/domain/services/dbtables/ElectionTableReport.java b/elections-ejb/src/main/java/net/lacnic/elections/domain/services/dbtables/ElectionTableReport.java index aa23d2d..dba0fb2 100644 --- a/elections-ejb/src/main/java/net/lacnic/elections/domain/services/dbtables/ElectionTableReport.java +++ b/elections-ejb/src/main/java/net/lacnic/elections/domain/services/dbtables/ElectionTableReport.java @@ -315,21 +315,4 @@ public void setClosedDate(String closedDate) { this.closedDate = closedDate; } - @Override - public boolean equals(Object election) { - if (this == election) { - return true; - } - if (election == null || getClass() != election.getClass()) { - return false; - } - ElectionTableReport other = (ElectionTableReport) election; - return this.electionId.equals(other.electionId); - } - - @Override - public int hashCode() { - return electionId.hashCode(); - } - } diff --git a/elections-ejb/src/main/java/net/lacnic/elections/utils/Constants.java b/elections-ejb/src/main/java/net/lacnic/elections/utils/Constants.java index 8148c52..515e764 100644 --- a/elections-ejb/src/main/java/net/lacnic/elections/utils/Constants.java +++ b/elections-ejb/src/main/java/net/lacnic/elections/utils/Constants.java @@ -4,15 +4,14 @@ import java.util.Map; /** - * This class is used to store system constants like template paths, services - * URLs, etc. + * This class is used to store system constants like template paths, services URLs, etc. * * @author LACNIC * */ public class Constants { - private static HashMap parameters = new HashMap<>(); + private static Map parameters = new HashMap<>(); // Parameter names for 'parameter' database table public static final String APP = "APP"; @@ -62,6 +61,20 @@ public class Constants { public static final String API_ELECTIONS = "api-Elections"; public static final String ELECTIONS_MANAGER = "elections-manager"; + public static final String ELECTION_ID = "electionId"; + public static final String TEMPLATE_TYPE = "templateType"; + public static final String VOTE_ID = "voteId"; + public static final String ORG_ID = "orgID"; + public static final String USER_VOTER_ID = "userVoterId"; + public static final String VOTE_AMOUNT = "voteAmount"; + public static final String ACTIVITY_ID = "activityId"; + public static final String CANDIDATE_ID = "candidateId"; + public static final String AUDITOR_ID = "auditorId"; + public static final String CANDIDATE_ORDER = "candidateOrder"; + public static final String COMMISSIONER_ID = "commissionerId"; + public static final String ELECTION_EMAIL_TEMPLATE_ID = "electionEmailTemplateId"; + public static final String EMAIL_HISTORY_ID = "emailHistoryId"; + private Constants() { throw new IllegalStateException("Utility class"); } @@ -90,7 +103,7 @@ public static Map getParameters() { return parameters; } - public static void setParameters(HashMap parameters) { + public static void setParameters(Map parameters) { Constants.parameters = parameters; } diff --git a/elections-ejb/src/main/java/net/lacnic/elections/utils/DateTimeUtils.java b/elections-ejb/src/main/java/net/lacnic/elections/utils/DateTimeUtils.java index cf03a08..55815ea 100644 --- a/elections-ejb/src/main/java/net/lacnic/elections/utils/DateTimeUtils.java +++ b/elections-ejb/src/main/java/net/lacnic/elections/utils/DateTimeUtils.java @@ -1,6 +1,8 @@ package net.lacnic.elections.utils; -import java.text.SimpleDateFormat; +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.time.format.DateTimeFormatter; import java.util.Date; public class DateTimeUtils { @@ -15,16 +17,22 @@ private DateTimeUtils() { public static String getTableServicesDateTimeString(Date dateTime) { if (dateTime != null) { - SimpleDateFormat dateFormat = new SimpleDateFormat(TABLE_SERVICES_DATE_TIME_FORMAT); - return dateFormat.format(dateTime); + DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern(TABLE_SERVICES_DATE_TIME_FORMAT); + LocalDateTime localDateTime = dateTime.toInstant() + .atZone(ZoneId.systemDefault()) + .toLocalDateTime(); + return localDateTime.format(dateFormatter); } return null; } public static String getElectionDateTimeString(Date dateTime) { if (dateTime != null) { - SimpleDateFormat dateFormat = new SimpleDateFormat(ELECTION_DATE_TIME_FORMAT); - return dateFormat.format(dateTime); + DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern(ELECTION_DATE_TIME_FORMAT); + LocalDateTime localDateTime = dateTime.toInstant() + .atZone(ZoneId.systemDefault()) + .toLocalDateTime(); + return localDateTime.format(dateFormatter); } return null; } diff --git a/elections-ejb/src/test/java/net/lacnic/elections/dao/ActivityDaoTest.java b/elections-ejb/src/test/java/net/lacnic/elections/dao/ActivityDaoTest.java new file mode 100644 index 0000000..5efd8d6 --- /dev/null +++ b/elections-ejb/src/test/java/net/lacnic/elections/dao/ActivityDaoTest.java @@ -0,0 +1,105 @@ +package net.lacnic.elections.dao; + +import static net.lacnic.elections.utils.Constants.ACTIVITY_ID; +import static net.lacnic.elections.utils.Constants.ELECTION_ID; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.util.Arrays; +import java.util.List; + +import javax.persistence.EntityManager; +import javax.persistence.Query; +import javax.persistence.TypedQuery; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; +import net.lacnic.elections.domain.Activity; + +public class ActivityDaoTest extends TestCase { + + private EntityManager em; + private ActivityDao activityDao; + + public static Test suite() { + return new TestSuite(ActivityDaoTest.class); + } + + @Override + public void setUp() { + // Initialize EntityManager using Mockito + em = mock(EntityManager.class); + activityDao = new ActivityDao(em); + } + + public void testGetActivitiesAll() { + // Mock the behavior of a TypedQuery + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT a FROM Activity a ORDER BY a.timestamp DESC", Activity.class)).thenReturn(query); + + Activity activity1 = new Activity(); + Activity activity2 = new Activity(); + List activities = Arrays.asList(activity1, activity2); + when(query.getResultList()).thenReturn(activities); + + // Call the method and verify the results + List result = activityDao.getActivitiesAll(); + assertEquals(2, result.size()); + verify(em).createQuery("SELECT a FROM Activity a ORDER BY a.timestamp DESC", Activity.class); + } + + public void testGetActivity() { + // Mock the behavior of a TypedQuery + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT a FROM Activity a WHERE a.activityId = :activityId", Activity.class)).thenReturn(query); + + Activity activity = new Activity(); + when(query.setParameter(ACTIVITY_ID, 1L)).thenReturn(query); + when(query.getSingleResult()).thenReturn(activity); + + // Call the method and verify the result + Activity result = activityDao.getActivity(1L); + assertNotNull(result); + verify(query).setParameter(ACTIVITY_ID, 1L); + verify(query).getSingleResult(); + } + + public void testGetElectionActivities() { + // Mock the behavior of a TypedQuery + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT a FROM Activity a WHERE a.electionId = :electionId ORDER BY a.timestamp DESC", Activity.class)).thenReturn(query); + + Activity activity1 = new Activity(); + Activity activity2 = new Activity(); + List activities = Arrays.asList(activity1, activity2); + when(query.setParameter(ELECTION_ID, 100L)).thenReturn(query); + when(query.getResultList()).thenReturn(activities); + + // Call the method and verify the results + List result = activityDao.getElectionActivities(100L); + assertEquals(2, result.size()); + verify(query).setParameter(ELECTION_ID, 100L); + } + + public void testGetActivitiesAllIdAndDescription() { + // Mock the behavior of a Query + Query query = mock(Query.class); + when(em.createQuery("SELECT a.activityId, a.description FROM Activity a ORDER BY a.activityId")).thenReturn(query); + + Object[] result1 = new Object[] { 1L, "Description 1" }; + Object[] result2 = new Object[] { 2L, "Description 2" }; + List results = Arrays.asList(result1, result2); + when(query.setMaxResults(10)).thenReturn(query); + when(query.setFirstResult(0)).thenReturn(query); + when(query.getResultList()).thenReturn(results); + + // Call the method and verify the results + List result = activityDao.getActivitiesAllIdAndDescription(10, 0); + assertEquals(2, result.size()); + verify(query).setMaxResults(10); + verify(query).setFirstResult(0); + } + +} diff --git a/elections-ejb/src/test/java/net/lacnic/elections/dao/AuditorDaoTest.java b/elections-ejb/src/test/java/net/lacnic/elections/dao/AuditorDaoTest.java new file mode 100644 index 0000000..b511087 --- /dev/null +++ b/elections-ejb/src/test/java/net/lacnic/elections/dao/AuditorDaoTest.java @@ -0,0 +1,230 @@ +package net.lacnic.elections.dao; + +import static net.lacnic.elections.utils.Constants.AUDITOR_ID; +import static net.lacnic.elections.utils.Constants.ELECTION_ID; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import javax.persistence.EntityManager; +import javax.persistence.Query; +import javax.persistence.TypedQuery; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; +import net.lacnic.elections.domain.Auditor; + +public class AuditorDaoTest extends TestCase { + + private EntityManager em; + private AuditorDao auditorDao; + + public static Test suite() { + return new TestSuite(AuditorDaoTest.class); + } + + @Override + public void setUp() { + // Initialize EntityManager using Mockito + em = mock(EntityManager.class); + auditorDao = new AuditorDao(em); + } + + public void testGetAuditor() { + // Mock the behavior of TypedQuery + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT a FROM Auditor a WHERE a.auditorId = :auditorId", Auditor.class)).thenReturn(query); + + Auditor auditor = new Auditor(); + when(query.setParameter(AUDITOR_ID, 1L)).thenReturn(query); + when(query.getSingleResult()).thenReturn(auditor); + + // Call the method and verify the result + Auditor result = auditorDao.getAuditor(1L); + assertNotNull(result); + verify(query).setParameter(AUDITOR_ID, 1L); + verify(query).getSingleResult(); + } + + public void testGetAuditorsAll() { + // Mock the behavior of TypedQuery + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT a FROM Auditor a", Auditor.class)).thenReturn(query); + + Auditor auditor1 = new Auditor(); + Auditor auditor2 = new Auditor(); + List auditors = Arrays.asList(auditor1, auditor2); + when(query.getResultList()).thenReturn(auditors); + + // Call the method and verify the result + List result = auditorDao.getAuditorsAll(); + assertEquals(2, result.size()); + verify(em).createQuery("SELECT a FROM Auditor a", Auditor.class); + } + + public void testGetElectionAuditors() { + // Mock the behavior of TypedQuery + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT a FROM Auditor a WHERE a.election.electionId = :electionId", Auditor.class)).thenReturn(query); + + Auditor auditor1 = new Auditor(); + Auditor auditor2 = new Auditor(); + List auditors = Arrays.asList(auditor1, auditor2); + when(query.setParameter(ELECTION_ID, 100L)).thenReturn(query); + when(query.getResultList()).thenReturn(auditors); + + // Call the method and verify the result + List result = auditorDao.getElectionAuditors(100L); + assertEquals(2, result.size()); + verify(query).setParameter(ELECTION_ID, 100L); + } + + public void testGetAuditorsByEmail() { + // Mock the behavior of TypedQuery + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT a FROM Auditor a WHERE a.mail = :email ORDER BY a.auditorId", Auditor.class)).thenReturn(query); + + Auditor auditor1 = new Auditor(); + Auditor auditor2 = new Auditor(); + List auditors = Arrays.asList(auditor1, auditor2); + when(query.setParameter("email", "example@example.com")).thenReturn(query); + when(query.setMaxResults(10)).thenReturn(query); + when(query.setFirstResult(0)).thenReturn(query); + when(query.getResultList()).thenReturn(auditors); + + // Call the method and verify the result + List result = auditorDao.getAuditorsByEmail("example@example.com", 10, 0); + assertEquals(2, result.size()); + verify(query).setParameter("email", "example@example.com"); + verify(query).setMaxResults(10); + verify(query).setFirstResult(0); + } + + public void testAuditorExists() { + // Mock the behavior of Query + Query query = mock(Query.class); + when(em.createQuery("SELECT a.auditorId FROM Auditor a WHERE a.election.electionId = :electionId AND a.name = :name AND a.mail = :mail")).thenReturn(query); + + when(query.setParameter(ELECTION_ID, 100L)).thenReturn(query); + when(query.setParameter("mail", "auditor@example.com")).thenReturn(query); + when(query.setParameter("name", "Auditor Name")).thenReturn(query); + when(query.setMaxResults(1)).thenReturn(query); + when(query.getResultList()).thenReturn(Collections.singletonList(1L)); + + // Call the method and verify the result + boolean exists = auditorDao.auditorExists(100L, "Auditor Name", "auditor@example.com"); + assertTrue(exists); + verify(query).setParameter(ELECTION_ID, 100L); + verify(query).setParameter("mail", "auditor@example.com"); + verify(query).setParameter("name", "Auditor Name"); + verify(query).setMaxResults(1); + } + + public void testAuditorDoesNotExist() { + // Mock the behavior of Query + Query query = mock(Query.class); + when(em.createQuery("SELECT a.auditorId FROM Auditor a WHERE a.election.electionId = :electionId AND a.name = :name AND a.mail = :mail")).thenReturn(query); + + when(query.setParameter(ELECTION_ID, 100L)).thenReturn(query); + when(query.setParameter("mail", "auditor@example.com")).thenReturn(query); + when(query.setParameter("name", "Auditor Name")).thenReturn(query); + when(query.setMaxResults(1)).thenReturn(query); + when(query.getResultList()).thenReturn(Collections.emptyList()); // Simulate an empty result list + + // Call the method and verify the result + boolean exists = auditorDao.auditorExists(100L, "Auditor Name", "auditor@example.com"); + assertFalse(exists); // The auditor does not exist, so it should return false + verify(query).setParameter(ELECTION_ID, 100L); + verify(query).setParameter("mail", "auditor@example.com"); + verify(query).setParameter("name", "Auditor Name"); + verify(query).setMaxResults(1); + } + + public void testGetAuditorByResultToken() { + // Mock the behavior of TypedQuery + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT a FROM Auditor a WHERE a.resultToken = :resultToken", Auditor.class)).thenReturn(query); + + Auditor auditor = new Auditor(); + when(query.setParameter("resultToken", "resultToken123")).thenReturn(query); + when(query.getSingleResult()).thenReturn(auditor); + + // Call the method and verify the result + Auditor result = auditorDao.getAuditorByResultToken("resultToken123"); + assertNotNull(result); + verify(query).setParameter("resultToken", "resultToken123"); + verify(query).getSingleResult(); + } + + public void testGetAllAuditorsUUIDs() { + // Mock the behavior of TypedQuery + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT a.resultToken FROM Auditor a", String.class)).thenReturn(query); + + List resultTokens = Arrays.asList("resultToken1", "resultToken2"); + when(query.getResultList()).thenReturn(resultTokens); + + // Call the method and verify the result + List result = auditorDao.getAllAuditorsUUIDs(); + assertEquals(2, result.size()); + verify(query).getResultList(); + } + + public void testGetElectionAuditorsNotAgreedConformity() { + // Mock the behavior of TypedQuery + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT a FROM Auditor a WHERE a.commissioner = TRUE AND a.agreedConformity = FALSE AND a.election.electionId = :electionId", Auditor.class)).thenReturn(query); + + Auditor auditor1 = new Auditor(); + Auditor auditor2 = new Auditor(); + List auditors = Arrays.asList(auditor1, auditor2); + when(query.setParameter(ELECTION_ID, 100L)).thenReturn(query); + when(query.getResultList()).thenReturn(auditors); + + // Call the method and verify the result + List result = auditorDao.getElectionAuditorsNotAgreedConformity(100L); + assertEquals(2, result.size()); + verify(query).setParameter(ELECTION_ID, 100L); + } + + public void testGetElectionAuditorsAgreedConformity() { + // Mock the behavior of TypedQuery + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT a FROM Auditor a WHERE a.commissioner = TRUE AND a.agreedConformity = TRUE AND a.election.electionId = :electionId", Auditor.class)).thenReturn(query); + + Auditor auditor1 = new Auditor(); + Auditor auditor2 = new Auditor(); + List auditors = Arrays.asList(auditor1, auditor2); + when(query.setParameter(ELECTION_ID, 100L)).thenReturn(query); + when(query.getResultList()).thenReturn(auditors); + + // Call the method and verify the result + List result = auditorDao.getElectionAuditorsAgreedConformity(100L); + assertEquals(2, result.size()); + verify(query).setParameter(ELECTION_ID, 100L); + } + + public void testGetAuditorsAllIdAndDescription() { + // Mock the behavior of Query + Query query = mock(Query.class); + when(em.createQuery("SELECT a.auditorId, a.name FROM Auditor a ORDER BY a.auditorId")).thenReturn(query); + + Object[] result1 = new Object[] { 1L, "Auditor 1" }; + Object[] result2 = new Object[] { 2L, "Auditor 2" }; + List results = Arrays.asList(result1, result2); + when(query.setMaxResults(10)).thenReturn(query); + when(query.setFirstResult(0)).thenReturn(query); + when(query.getResultList()).thenReturn(results); + + // Call the method and verify the result + List result = auditorDao.getAuditorsAllIdAndDescription(10, 0); + assertEquals(2, result.size()); + verify(query).setMaxResults(10); + verify(query).setFirstResult(0); + } +} \ No newline at end of file diff --git a/elections-ejb/src/test/java/net/lacnic/elections/dao/CandidateDaoTest.java b/elections-ejb/src/test/java/net/lacnic/elections/dao/CandidateDaoTest.java new file mode 100644 index 0000000..ae23875 --- /dev/null +++ b/elections-ejb/src/test/java/net/lacnic/elections/dao/CandidateDaoTest.java @@ -0,0 +1,279 @@ +package net.lacnic.elections.dao; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; +import net.lacnic.elections.domain.Candidate; +import net.lacnic.elections.utils.Constants; + +import javax.persistence.EntityManager; +import javax.persistence.Query; +import javax.persistence.TypedQuery; +import java.util.Arrays; +import java.util.List; + +import static net.lacnic.elections.utils.Constants.*; +import static org.mockito.Mockito.*; + +public class CandidateDaoTest extends TestCase { + + private EntityManager em; + private CandidateDao candidateDao; + + /** + * @return the suite of tests being tested + */ + public static Test suite() { + return new TestSuite(CandidateDaoTest.class); + } + + @Override + public void setUp() { + // Initialize EntityManager using Mockito + em = mock(EntityManager.class); + candidateDao = new CandidateDao(em); + } + + public void testGetCandidate() { + // Mock the behavior of TypedQuery + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT c FROM Candidate c WHERE c.candidateId = :candidateId", Candidate.class)).thenReturn(query); + + Candidate candidate = new Candidate(); + when(query.setParameter(CANDIDATE_ID, 1L)).thenReturn(query); + when(query.getSingleResult()).thenReturn(candidate); + + // Call the method and verify the result + Candidate result = candidateDao.getCandidate(1L); + assertNotNull(result); + verify(query).setParameter(CANDIDATE_ID, 1L); + verify(query).getSingleResult(); + } + + public void testGetCandidatesByEmail() { + // Mock the behavior of TypedQuery + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT c FROM Candidate c WHERE c.mail = :email ORDER BY c.candidateId", Candidate.class)).thenReturn(query); + + Candidate candidate1 = new Candidate(); + Candidate candidate2 = new Candidate(); + List candidates = Arrays.asList(candidate1, candidate2); + when(query.setParameter("email", "example@example.com")).thenReturn(query); + when(query.setMaxResults(10)).thenReturn(query); + when(query.setFirstResult(0)).thenReturn(query); + when(query.getResultList()).thenReturn(candidates); + + // Call the method and verify the result + List result = candidateDao.getCandidatesByEmail("example@example.com", 10, 0); + assertEquals(2, result.size()); + verify(query).setParameter("email", "example@example.com"); + verify(query).setMaxResults(10); + verify(query).setFirstResult(0); + } + + public void testGetElectionFirstCandidate() { + // Mock the behavior of TypedQuery + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT c FROM Candidate c WHERE c.election.electionId = :electionId ORDER BY c.candidateOrder DESC", Candidate.class)).thenReturn(query); + + Candidate candidate = new Candidate(); + when(query.setParameter(ELECTION_ID, 200L)).thenReturn(query); + when(query.setMaxResults(1)).thenReturn(query); + when(query.getSingleResult()).thenReturn(candidate); + + // Call the method and verify the result + Candidate result = candidateDao.getElectionFirstCandidate(200L); + assertNotNull(result); + verify(query).setParameter(ELECTION_ID, 200L); + verify(query).setMaxResults(1); + verify(query).getSingleResult(); + } + + public void testGetElectionLastCandidate() { + // Mock the behavior of TypedQuery + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT c FROM Candidate c WHERE c.election.electionId = :electionId ORDER BY c.candidateOrder", Candidate.class)).thenReturn(query); + + Candidate candidate = new Candidate(); + when(query.setParameter(ELECTION_ID, 200L)).thenReturn(query); + when(query.setMaxResults(1)).thenReturn(query); + when(query.getSingleResult()).thenReturn(candidate); + + // Call the method and verify the result + Candidate result = candidateDao.getElectionLastCandidate(200L); + assertNotNull(result); + verify(query).setParameter(ELECTION_ID, 200L); + verify(query).setMaxResults(1); + verify(query).getSingleResult(); + } + + public void testGetElectionCandidates() { + // Mock the behavior of TypedQuery + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT c FROM Candidate c WHERE c.election.electionId = :electionId ORDER BY c.candidateOrder DESC", Candidate.class)).thenReturn(query); + + Candidate candidate1 = new Candidate(); + Candidate candidate2 = new Candidate(); + List candidates = Arrays.asList(candidate1, candidate2); + when(query.setParameter(ELECTION_ID, 200L)).thenReturn(query); + when(query.getResultList()).thenReturn(candidates); + + // Call the method and verify the result + List result = candidateDao.getElectionCandidates(200L); + assertEquals(2, result.size()); + verify(query).setParameter(ELECTION_ID, 200L); + } + + public void testGetCandidateVotesAmount() { + // Mock the behavior of Query + Query query = mock(Query.class); + when(em.createQuery("SELECT COUNT(v.voteId) FROM Vote v WHERE v.candidate.candidateId = :candidateId")).thenReturn(query); + + when(query.setParameter(CANDIDATE_ID, 1L)).thenReturn(query); + when(query.getSingleResult()).thenReturn(10L); + + // Call the method and verify the result + long result = candidateDao.getCandidateVotesAmount(1L); + assertEquals(10L, result); + verify(query).setParameter(CANDIDATE_ID, 1L); + verify(query).getSingleResult(); + } + + public void testGetLastNonFixedCandidateOrder() { + // Mock the behavior of Query + Query query = mock(Query.class); + when(em.createQuery("SELECT c.candidateOrder FROM Candidate c WHERE c.election.electionId = :electionId AND c.candidateOrder != :maxorder AND c.candidateOrder != :minorder ORDER BY c.candidateOrder DESC")).thenReturn(query); + + when(query.setParameter(ELECTION_ID, 200L)).thenReturn(query); + when(query.setParameter("maxorder", Constants.MAX_ORDER)).thenReturn(query); + when(query.setParameter("minorder", Constants.MIN_ORDER)).thenReturn(query); + when(query.setMaxResults(1)).thenReturn(query); + when(query.getSingleResult()).thenReturn(5); + + // Call the method and verify the result + int result = candidateDao.getLastNonFixedCandidateOrder(200L); + assertEquals(5, result); + verify(query).setParameter(ELECTION_ID, 200L); + verify(query).setMaxResults(1); + verify(query).getSingleResult(); + } + + public void testGetLastNonFixedCandidateOrderException() { + // Mock the behavior of Query + Query query = mock(Query.class); + when(em.createQuery("SELECT c.candidateOrder FROM Candidate c WHERE c.election.electionId = :electionId AND c.candidateOrder != :maxorder AND c.candidateOrder != :minorder ORDER BY c.candidateOrder DESC")) + .thenReturn(query); + + // Simulate an exception when calling getSingleResult + when(query.setParameter(ELECTION_ID, 200L)).thenReturn(query); + when(query.setParameter("maxorder", Constants.MAX_ORDER)).thenReturn(query); + when(query.setParameter("minorder", Constants.MIN_ORDER)).thenReturn(query); + when(query.setMaxResults(1)).thenReturn(query); + when(query.getSingleResult()).thenThrow(new RuntimeException("Database error")); + + // Call the method and verify that it returns 1 due to the exception + int result = candidateDao.getLastNonFixedCandidateOrder(200L); + assertEquals(1, result); // Default return value in case of an exception + verify(query).setParameter(ELECTION_ID, 200L); + verify(query).setParameter("maxorder", Constants.MAX_ORDER); + verify(query).setParameter("minorder", Constants.MIN_ORDER); + verify(query).setMaxResults(1); + } + + public void testGetNextAboveCandidate() { + // Mock the behavior of TypedQuery + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT c FROM Candidate c WHERE c.election.electionId = :electionId AND c.candidateOrder > :candidateOrder ORDER BY c.candidateOrder", Candidate.class)).thenReturn(query); + + Candidate candidate = new Candidate(); + when(query.setParameter(ELECTION_ID, 200L)).thenReturn(query); + when(query.setParameter(CANDIDATE_ORDER, 5)).thenReturn(query); + when(query.setMaxResults(1)).thenReturn(query); + when(query.getSingleResult()).thenReturn(candidate); + + // Call the method and verify the result + Candidate result = candidateDao.getNextAboveCandidate(200L, 5); + assertNotNull(result); + verify(query).setParameter(ELECTION_ID, 200L); + verify(query).setParameter(CANDIDATE_ORDER, 5); + verify(query).setMaxResults(1); + } + + public void testGetNextAboveCandidateException() { + // Mock the behavior of TypedQuery + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT c FROM Candidate c WHERE c.election.electionId = :electionId AND c.candidateOrder > :candidateOrder ORDER BY c.candidateOrder", Candidate.class)) + .thenReturn(query); + + // Simulate an exception when calling getSingleResult + when(query.setParameter(ELECTION_ID, 200L)).thenReturn(query); + when(query.setParameter(CANDIDATE_ORDER, 5)).thenReturn(query); + when(query.setMaxResults(1)).thenReturn(query); + when(query.getSingleResult()).thenThrow(new RuntimeException("Database error")); + + // Call the method and verify that it returns null due to the exception + Candidate result = candidateDao.getNextAboveCandidate(200L, 5); + assertNull(result); // Default return value in case of an exception + verify(query).setParameter(ELECTION_ID, 200L); + verify(query).setParameter(CANDIDATE_ORDER, 5); + verify(query).setMaxResults(1); + } + + public void testGetNextBelowCandidate() { + // Mock the behavior of TypedQuery + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT c FROM Candidate c WHERE c.election.electionId = :electionId AND c.candidateOrder < :candidateOrder ORDER BY c.candidateOrder DESC", Candidate.class)).thenReturn(query); + + Candidate candidate = new Candidate(); + when(query.setParameter(ELECTION_ID, 200L)).thenReturn(query); + when(query.setParameter(CANDIDATE_ORDER, 5)).thenReturn(query); + when(query.setMaxResults(1)).thenReturn(query); + when(query.getSingleResult()).thenReturn(candidate); + + // Call the method and verify the result + Candidate result = candidateDao.getNextBelowCandidate(200L, 5); + assertNotNull(result); + verify(query).setParameter(ELECTION_ID, 200L); + verify(query).setParameter(CANDIDATE_ORDER, 5); + verify(query).setMaxResults(1); + } + + public void testGetNextBelowCandidateException() { + // Mock the behavior of TypedQuery + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT c FROM Candidate c WHERE c.election.electionId = :electionId AND c.candidateOrder < :candidateOrder ORDER BY c.candidateOrder DESC", Candidate.class)) + .thenReturn(query); + + // Simulate an exception when calling getSingleResult + when(query.setParameter(ELECTION_ID, 200L)).thenReturn(query); + when(query.setParameter(CANDIDATE_ORDER, 5)).thenReturn(query); + when(query.setMaxResults(1)).thenReturn(query); + when(query.getSingleResult()).thenThrow(new RuntimeException("Database error")); + + // Call the method and verify that it returns null due to the exception + Candidate result = candidateDao.getNextBelowCandidate(200L, 5); + assertNull(result); // Default return value in case of an exception + verify(query).setParameter(ELECTION_ID, 200L); + verify(query).setParameter(CANDIDATE_ORDER, 5); + verify(query).setMaxResults(1); + } + + public void testGetCandidatesAllIdAndDescription() { + // Mock the behavior of Query + Query query = mock(Query.class); + when(em.createQuery("SELECT c.candidateId, c.name FROM Candidate c ORDER BY c.candidateId")).thenReturn(query); + + Object[] result1 = new Object[] { 1L, "Candidate 1" }; + Object[] result2 = new Object[] { 2L, "Candidate 2" }; + List results = Arrays.asList(result1, result2); + when(query.setMaxResults(10)).thenReturn(query); + when(query.setFirstResult(0)).thenReturn(query); + when(query.getResultList()).thenReturn(results); + + // Call the method and verify the result + List result = candidateDao.getCandidatesAllIdAndDescription(10, 0); + assertEquals(2, result.size()); + verify(query).setMaxResults(10); + verify(query).setFirstResult(0); + } +} diff --git a/elections-ejb/src/test/java/net/lacnic/elections/dao/CommissionerDaoTest.java b/elections-ejb/src/test/java/net/lacnic/elections/dao/CommissionerDaoTest.java new file mode 100644 index 0000000..cc4dedb --- /dev/null +++ b/elections-ejb/src/test/java/net/lacnic/elections/dao/CommissionerDaoTest.java @@ -0,0 +1,181 @@ +package net.lacnic.elections.dao; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; +import net.lacnic.elections.domain.Commissioner; + +import javax.persistence.EntityManager; +import javax.persistence.Query; +import javax.persistence.TypedQuery; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import static net.lacnic.elections.utils.Constants.COMMISSIONER_ID; +import static org.mockito.Mockito.*; + +public class CommissionerDaoTest extends TestCase { + + private EntityManager em; + private CommissionerDao commissionerDao; + + /** + * @return the suite of tests being tested + */ + public static Test suite() { + return new TestSuite(CommissionerDaoTest.class); + } + + @Override + public void setUp() { + // Initialize EntityManager using Mockito + em = mock(EntityManager.class); + commissionerDao = new CommissionerDao(em); + } + + public void testGetCommissioner() { + // Mock the behavior of TypedQuery + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT c FROM Commissioner c WHERE c.commissionerId =:commissionerId", Commissioner.class)).thenReturn(query); + + Commissioner commissioner = new Commissioner(); + when(query.setParameter(COMMISSIONER_ID, 1L)).thenReturn(query); + when(query.getSingleResult()).thenReturn(commissioner); + + // Call the method and verify the result + Commissioner result = commissionerDao.getCommissioner(1L); + assertNotNull(result); + verify(query).setParameter(COMMISSIONER_ID, 1L); + verify(query).getSingleResult(); + } + + public void testGetCommissionersAll() { + // Mock the behavior of TypedQuery + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT c FROM Commissioner c", Commissioner.class)).thenReturn(query); + + Commissioner commissioner1 = new Commissioner(); + Commissioner commissioner2 = new Commissioner(); + List commissioners = Arrays.asList(commissioner1, commissioner2); + when(query.getResultList()).thenReturn(commissioners); + + // Call the method and verify the result + List result = commissionerDao.getCommissionersAll(); + assertEquals(2, result.size()); + verify(em).createQuery("SELECT c FROM Commissioner c", Commissioner.class); + } + + public void testGetCommissionerByMail() { + // Mock the behavior of TypedQuery + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT c FROM Commissioner c WHERE UPPER(c.mail) = :mail", Commissioner.class)).thenReturn(query); + + Commissioner commissioner = new Commissioner(); + when(query.setParameter("mail", "test@example.com")).thenReturn(query); + when(query.getSingleResult()).thenReturn(commissioner); + + // Call the method and verify the result + Commissioner result = commissionerDao.getCommissionerByMail("test@example.com"); + assertNotNull(result); + verify(query).setParameter("mail", "TEST@EXAMPLE.COM"); + verify(query).getSingleResult(); + } + + public void testGetCommissionerByMailException() { + // Mock the behavior of TypedQuery + TypedQuery query = mock(TypedQuery.class); + + // Simulate an exception when trying to get the result + when(em.createQuery("SELECT c FROM Commissioner c WHERE UPPER(c.mail) = :mail", Commissioner.class)).thenReturn(query); + when(query.setParameter("mail", "test@example.com")).thenReturn(query); + when(query.getSingleResult()).thenThrow(new RuntimeException("Database error")); + + // Call the method and verify that it returns null due to the exception + Commissioner result = commissionerDao.getCommissionerByMail("test@example.com"); + + // Verify that the result is null because of the exception handling + assertNull(result); + } + + public void testCommissionerExists() { + // Mock the behavior of Query + Query query = mock(Query.class); + when(em.createQuery("SELECT c.commissionerId FROM Commissioner c WHERE UPPER(c.name) = :name AND UPPER(c.mail) = :mail")).thenReturn(query); + + when(query.setParameter("mail", "test@example.com")).thenReturn(query); + when(query.setParameter("name", "John Doe")).thenReturn(query); + when(query.setMaxResults(1)).thenReturn(query); + when(query.getResultList()).thenReturn(Collections.singletonList(1L)); + + // Call the method and verify the result + boolean exists = commissionerDao.commissionerExists("John Doe", "test@example.com"); + assertTrue(exists); + + verify(query).setParameter("mail", "TEST@EXAMPLE.COM"); + verify(query).setParameter("name", "JOHN DOE"); + verify(query).setMaxResults(1); + } + + public void testGetCommissionersAllIdAndDescription() { + // Mock the behavior of Query + Query query = mock(Query.class); + when(em.createQuery("SELECT c.commissionerId, c.name FROM Commissioner c ORDER BY c.commissionerId")).thenReturn(query); + + Object[] result1 = new Object[] { 1L, "Commissioner 1" }; + Object[] result2 = new Object[] { 2L, "Commissioner 2" }; + List results = Arrays.asList(result1, result2); + when(query.setMaxResults(10)).thenReturn(query); + when(query.setFirstResult(0)).thenReturn(query); + when(query.getResultList()).thenReturn(results); + + // Call the method and verify the result + List result = commissionerDao.getCommissionersAllIdAndDescription(10, 0); + assertEquals(2, result.size()); + verify(query).setMaxResults(10); + verify(query).setFirstResult(0); + } + + public void testCommissionerExistsWhenCommissionerExists() { + // Mock the behavior of Query + Query query = mock(Query.class); + when(em.createQuery("SELECT c.commissionerId FROM Commissioner c WHERE UPPER(c.name) = :name AND UPPER(c.mail) = :mail")) + .thenReturn(query); + + // Simulate that the result list is not empty (commissioner exists) + when(query.setParameter("name", "JOHN DOE")).thenReturn(query); + when(query.setParameter("mail", "JOHN.DOE@EXAMPLE.COM")).thenReturn(query); + when(query.setMaxResults(1)).thenReturn(query); + when(query.getResultList()).thenReturn(Collections.singletonList(1L)); // Simulating that we have a result + + // Call the method and verify the result + boolean result = commissionerDao.commissionerExists("John Doe", "john.doe@example.com"); + assertTrue(result); // Commissioner exists, so the result should be true + verify(query).setParameter("name", "JOHN DOE"); + verify(query).setParameter("mail", "JOHN.DOE@EXAMPLE.COM"); + verify(query).setMaxResults(1); + verify(query).getResultList(); + } + + public void testCommissionerExistsWhenCommissionerDoesNotExist() { + // Mock the behavior of Query + Query query = mock(Query.class); + when(em.createQuery("SELECT c.commissionerId FROM Commissioner c WHERE UPPER(c.name) = :name AND UPPER(c.mail) = :mail")) + .thenReturn(query); + + // Simulate that the result list is empty (commissioner does not exist) + when(query.setParameter("name", "NON EXISTING NAME")).thenReturn(query); + when(query.setParameter("mail", "NON.EXISTING@EXAMPLE.COM")).thenReturn(query); + when(query.setMaxResults(1)).thenReturn(query); + when(query.getResultList()).thenReturn(Collections.emptyList()); // Simulating an empty result list + + // Call the method and verify the result + boolean result = commissionerDao.commissionerExists("Non Existing Name", "non.existing@example.com"); + assertFalse(result); // No commissioner found, so the result should be false + verify(query).setParameter("name", "NON EXISTING NAME"); + verify(query).setParameter("mail", "NON.EXISTING@EXAMPLE.COM"); + verify(query).setMaxResults(1); + verify(query).getResultList(); + } + +} diff --git a/elections-ejb/src/test/java/net/lacnic/elections/dao/CustomizationDaoTest.java b/elections-ejb/src/test/java/net/lacnic/elections/dao/CustomizationDaoTest.java new file mode 100644 index 0000000..1d27458 --- /dev/null +++ b/elections-ejb/src/test/java/net/lacnic/elections/dao/CustomizationDaoTest.java @@ -0,0 +1,83 @@ +package net.lacnic.elections.dao; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.util.Arrays; +import java.util.List; + +import javax.persistence.EntityManager; +import javax.persistence.Query; +import javax.persistence.TypedQuery; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; +import net.lacnic.elections.domain.Customization; + +public class CustomizationDaoTest extends TestCase { + + private EntityManager em; + private CustomizationDao customizationDao; + + public static Test suite() { + return new TestSuite(CustomizationDaoTest.class); + } + + @Override + public void setUp() { + em = mock(EntityManager.class); + customizationDao = new CustomizationDao(em); + } + + public void testGetCustomization() { + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT c FROM Customization c WHERE c.customizationId = 1", Customization.class)).thenReturn(query); + + Customization customization = new Customization(); + when(query.getSingleResult()).thenReturn(customization); + + Customization result = customizationDao.getCustomization(); + assertNotNull(result); + verify(query).getSingleResult(); + } + + public void testGetCustomizationException() { + TypedQuery query = mock(TypedQuery.class); + + when(em.createQuery("SELECT c FROM Customization c WHERE c.customizationId = 1", Customization.class)).thenReturn(query); + when(query.getSingleResult()).thenThrow(new RuntimeException("Database error")); + + Customization result = customizationDao.getCustomization(); + + assertNull(result); + } + + public void testGetCustomizationById() { + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT c FROM Customization c WHERE c.customizationId = :customizationId", Customization.class)).thenReturn(query); + + Customization customization = new Customization(); + when(query.setParameter("customizationId", 1L)).thenReturn(query); + when(query.getSingleResult()).thenReturn(customization); + + Customization result = customizationDao.getCustomizationById(1L); + assertNotNull(result); + verify(query).setParameter("customizationId", 1L); + verify(query).getSingleResult(); + } + + public void testGetCustomizationsAllIdAndDescription() { + Query query = mock(Query.class); + when(em.createQuery("SELECT c.customizationId, c.siteTitle FROM Customization c ORDER BY c.customizationId")).thenReturn(query); + + Object[] result1 = new Object[] { 1L, "Title 1" }; + Object[] result2 = new Object[] { 2L, "Title 2" }; + when(query.getResultList()).thenReturn(Arrays.asList(result1, result2)); + + List results = customizationDao.getCustomizationsAllIdAndDescription(); + assertEquals(2, results.size()); + verify(query).getResultList(); + } +} diff --git a/elections-ejb/src/test/java/net/lacnic/elections/dao/ElectionDaoTest.java b/elections-ejb/src/test/java/net/lacnic/elections/dao/ElectionDaoTest.java new file mode 100644 index 0000000..60ba9b4 --- /dev/null +++ b/elections-ejb/src/test/java/net/lacnic/elections/dao/ElectionDaoTest.java @@ -0,0 +1,287 @@ +package net.lacnic.elections.dao; + +import static net.lacnic.elections.utils.Constants.ELECTION_ID; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.util.Arrays; +import java.util.Collections; +import java.util.Date; +import java.util.List; + +import javax.persistence.EntityManager; +import javax.persistence.Query; +import javax.persistence.TypedQuery; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; +import net.lacnic.elections.domain.Election; +import net.lacnic.elections.domain.ElectionLight; +import net.lacnic.elections.domain.JointElection; + +public class ElectionDaoTest extends TestCase { + + private EntityManager em; + private ElectionDao electionDao; + + public static Test suite() { + return new TestSuite(ElectionDaoTest.class); + } + + @Override + public void setUp() { + em = mock(EntityManager.class); + electionDao = new ElectionDao(em); + } + + public void testGetElection() { + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT e FROM Election e WHERE e.electionId = :electionId", Election.class)).thenReturn(query); + + Election election = new Election(); + when(query.setParameter(ELECTION_ID, 1L)).thenReturn(query); + when(query.getSingleResult()).thenReturn(election); + + Election result = electionDao.getElection(1L); + assertNotNull(result); + verify(query).setParameter(ELECTION_ID, 1L); + verify(query).getSingleResult(); + } + + public void testGetElectionByResultToken() { + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT e FROM Election e WHERE e.resultToken = :resultToken", Election.class)).thenReturn(query); + + Election election = new Election(); + when(query.setParameter("resultToken", "result-token")).thenReturn(query); + when(query.getSingleResult()).thenReturn(election); + + Election result = electionDao.getElectionByResultToken("result-token"); + assertNotNull(result); + verify(query).setParameter("resultToken", "result-token"); + verify(query).getSingleResult(); + } + + public void testGetElections() { + TypedQuery query = mock(TypedQuery.class); + + when(em.createQuery("SELECT e FROM Election e", Election.class)).thenReturn(query); + + Election election1 = new Election(); + Election election2 = new Election(); + when(query.getResultList()).thenReturn(Arrays.asList(election1, election2)); + + List result = electionDao.getElections(); + + assertEquals(2, result.size()); + verify(em).createQuery("SELECT e FROM Election e", Election.class); + verify(query).getResultList(); + } + + public void testGetElectionsWithPagination() { + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT e FROM Election e ORDER BY e.electionId", Election.class)).thenReturn(query); + + Election election1 = new Election(); + Election election2 = new Election(); + when(query.setMaxResults(10)).thenReturn(query); + when(query.setFirstResult(0)).thenReturn(query); + when(query.getResultList()).thenReturn(Arrays.asList(election1, election2)); + + List result = electionDao.getElections(10, 0); + assertEquals(2, result.size()); + verify(query).setMaxResults(10); + verify(query).setFirstResult(0); + verify(query).getResultList(); + } + + public void testGetElectionsAllOrderCreationDate() { + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT e FROM Election e ORDER BY e.creationDate", Election.class)).thenReturn(query); + + Election election1 = new Election(); + Election election2 = new Election(); + when(query.getResultList()).thenReturn(Arrays.asList(election1, election2)); + + List result = electionDao.getElectionsAllOrderCreationDate(); + assertEquals(2, result.size()); + verify(query).getResultList(); + } + + public void testGetElectionsAllOrderStartDateDesc() { + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT e FROM Election e ORDER BY e.startDate DESC", Election.class)).thenReturn(query); + + Election election1 = new Election(); + Election election2 = new Election(); + when(query.getResultList()).thenReturn(Arrays.asList(election1, election2)); + + List result = electionDao.getElectionsAllOrderStartDateDesc(); + assertEquals(2, result.size()); + verify(query).getResultList(); + } + + public void testGetElectionsLightAllOrderStartDateDesc() { + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT e FROM ElectionLight e ORDER BY e.startDate DESC", ElectionLight.class)).thenReturn(query); + + ElectionLight electionLight1 = new ElectionLight(); + ElectionLight electionLight2 = new ElectionLight(); + when(query.getResultList()).thenReturn(Arrays.asList(electionLight1, electionLight2)); + + List result = electionDao.getElectionsLightAllOrderStartDateDesc(); + assertEquals(2, result.size()); + verify(query).getResultList(); + } + + public void testGetElectionsLightThisYear() { + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT e FROM Election e WHERE e.creationDate > :startDate ORDER BY e.creationDate DESC", Election.class)).thenReturn(query); + + when(query.setParameter(eq("startDate"), any(Date.class))).thenReturn(query); + + Election election1 = new Election(); + Election election2 = new Election(); + when(query.getResultList()).thenReturn(Arrays.asList(election1, election2)); + + List result = electionDao.getElectionsLightThisYear(); + assertEquals(2, result.size()); + verify(query).setParameter(eq("startDate"), any(Date.class)); + verify(query).getResultList(); + } + + public void testOneElectionExists() { + Query query = mock(Query.class); + when(em.createQuery("SELECT e.electionId FROM Election e")).thenReturn(query); + + when(query.setMaxResults(1)).thenReturn(query); + when(query.getResultList()).thenReturn(Collections.singletonList(1L)); + + boolean result = electionDao.oneElectionExists(); + assertTrue(result); + verify(query).setMaxResults(1); + verify(query).getResultList(); + } + + public void testOneElectionExists_NoElection() { + Query query = mock(Query.class); + + when(em.createQuery("SELECT e.electionId FROM Election e")).thenReturn(query); + when(query.setMaxResults(1)).thenReturn(query); + when(query.getResultList()).thenReturn(Collections.emptyList()); // Simulate no elections + + boolean result = electionDao.oneElectionExists(); + assertFalse(result); + verify(query).setMaxResults(1); + verify(query).getResultList(); + } + + public void testElectionIsSimple() { + Query query = mock(Query.class); + + when(em.createQuery("SELECT e FROM JointElection e WHERE e.idElectionA = :electionId OR e.idElectionB = :electionId")).thenReturn(query); + when(query.setParameter(ELECTION_ID, 200L)).thenReturn(query); + when(query.getResultList()).thenReturn(Collections.emptyList()); + + boolean result = electionDao.electionIsSimple(200L); + + assertTrue(result); + verify(query).setParameter(ELECTION_ID, 200L); + } + + public void testElectionIsSimple_NoJointElections() { + Query query = mock(Query.class); + + when(em.createQuery("SELECT e FROM JointElection e WHERE e.idElectionA = :electionId OR e.idElectionB = :electionId")).thenReturn(query); + when(query.setParameter(ELECTION_ID, 200L)).thenReturn(query); + when(query.getResultList()).thenReturn(Collections.emptyList()); // No joint elections found + + boolean result = electionDao.electionIsSimple(200L); + assertTrue(result); + } + + public void testElectionIsSimple_ResultListNull() { + Query query = mock(Query.class); + when(em.createQuery("SELECT e FROM JointElection e WHERE e.idElectionA = :electionId OR e.idElectionB = :electionId")).thenReturn(query); + when(query.setParameter(ELECTION_ID, 200L)).thenReturn(query); + when(query.getResultList()).thenReturn(null); // Simulating a null result list + + boolean result = electionDao.electionIsSimple(200L); + + assertTrue(result); // Election should be simple because resultList is null + verify(query).setParameter(ELECTION_ID, 200L); + verify(query).getResultList(); + } + + public void testElectionIsSimple_HasJointElections() { + Query query = mock(Query.class); + + when(em.createQuery("SELECT e FROM JointElection e WHERE e.idElectionA = :electionId OR e.idElectionB = :electionId")).thenReturn(query); + when(query.setParameter(ELECTION_ID, 200L)).thenReturn(query); + when(query.getResultList()).thenReturn(Collections.singletonList(new JointElection())); + + boolean result = electionDao.electionIsSimple(200L); + assertFalse(result); + } + + public void testGetJointElectionForElection() { + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT e FROM JointElection e WHERE e.idElectionA = :electionId OR e.idElectionB = :electionId", JointElection.class)).thenReturn(query); + + JointElection jointElection = new JointElection(); + when(query.setParameter(ELECTION_ID, 200L)).thenReturn(query); + when(query.getSingleResult()).thenReturn(jointElection); + + JointElection result = electionDao.getJointElectionForElection(200L); + assertNotNull(result); + verify(query).setParameter(ELECTION_ID, 200L); + verify(query).getSingleResult(); + } + + public void testGetJointElectionsAll() { + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT e FROM JointElection e", JointElection.class)).thenReturn(query); + + JointElection jointElection1 = new JointElection(); + JointElection jointElection2 = new JointElection(); + when(query.getResultList()).thenReturn(Arrays.asList(jointElection1, jointElection2)); + + List result = electionDao.getJointElectionsAll(); + assertEquals(2, result.size()); + verify(query).getResultList(); + } + + public void testGetElectionsAllIdAndTitle() { + Query query = mock(Query.class); + when(em.createQuery("SELECT e.electionId, e.titleSpanish FROM Election e ORDER BY e.electionId")).thenReturn(query); + + Object[] result1 = new Object[] { 1L, "Title 1" }; + Object[] result2 = new Object[] { 2L, "Title 2" }; + when(query.getResultList()).thenReturn(Arrays.asList(result1, result2)); + + List results = electionDao.getElectionsAllIdAndTitle(); + assertEquals(2, results.size()); + verify(query).getResultList(); + } + + public void testGetElectionsAllIdAndDescription() { + Query query = mock(Query.class); + when(em.createQuery("SELECT e.electionId, e.titleSpanish FROM Election e ORDER BY e.electionId")).thenReturn(query); + + Object[] result1 = new Object[] { 1L, "Title 1" }; + Object[] result2 = new Object[] { 2L, "Title 2" }; + when(query.setMaxResults(10)).thenReturn(query); + when(query.setFirstResult(0)).thenReturn(query); + when(query.getResultList()).thenReturn(Arrays.asList(result1, result2)); + + List results = electionDao.getElectionsAllIdAndDescription(10, 0); + assertEquals(2, results.size()); + verify(query).setMaxResults(10); + verify(query).setFirstResult(0); + verify(query).getResultList(); + } +} diff --git a/elections-ejb/src/test/java/net/lacnic/elections/dao/ElectionEmailTemplateDaoTest.java b/elections-ejb/src/test/java/net/lacnic/elections/dao/ElectionEmailTemplateDaoTest.java new file mode 100644 index 0000000..c6a8043 --- /dev/null +++ b/elections-ejb/src/test/java/net/lacnic/elections/dao/ElectionEmailTemplateDaoTest.java @@ -0,0 +1,250 @@ +package net.lacnic.elections.dao; + +import static net.lacnic.elections.utils.Constants.ELECTION_EMAIL_TEMPLATE_ID; +import static net.lacnic.elections.utils.Constants.ELECTION_ID; +import static net.lacnic.elections.utils.Constants.TEMPLATE_TYPE; +import static org.junit.Assert.assertArrayEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import javax.persistence.EntityManager; +import javax.persistence.Query; +import javax.persistence.TypedQuery; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; +import net.lacnic.elections.domain.ElectionEmailTemplate; + +public class ElectionEmailTemplateDaoTest extends TestCase { + + private EntityManager em; + private ElectionEmailTemplateDao electionEmailTemplateDao; + + public static Test suite() { + return new TestSuite(ElectionEmailTemplateDaoTest.class); + } + + @Override + public void setUp() { + em = mock(EntityManager.class); + electionEmailTemplateDao = new ElectionEmailTemplateDao(em); + } + + public void testGetElectionEmailTemplate() { + // Mock the behavior of TypedQuery + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT e FROM ElectionEmailTemplate e WHERE e.electionEmailTemplateId = :electionEmailTemplateId", ElectionEmailTemplate.class)).thenReturn(query); + + ElectionEmailTemplate template = new ElectionEmailTemplate(); + when(query.setParameter(ELECTION_EMAIL_TEMPLATE_ID, 1L)).thenReturn(query); + when(query.getSingleResult()).thenReturn(template); + + // Call the method and verify the result + ElectionEmailTemplate result = electionEmailTemplateDao.getElectionEmailTemplate(1L); + assertNotNull(result); + verify(query).setParameter(ELECTION_EMAIL_TEMPLATE_ID, 1L); + verify(query).getSingleResult(); + } + + public void testGetSubjectByElectionTypeLanguageSP() { + // Mock the behavior of TypedQuery + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT t.subjectSP FROM ElectionEmailTemplate t WHERE t.election.electionId = :electionId AND t.templateType = :templateType", String.class)).thenReturn(query); + + when(query.setParameter(ELECTION_ID, 1L)).thenReturn(query); + when(query.setParameter(TEMPLATE_TYPE, "TYPE")).thenReturn(query); + when(query.getSingleResult()).thenReturn("Subject SP"); + + // Call the method and verify the result + String result = electionEmailTemplateDao.getSubjectByElectionTypeLanguageSP(1L, "type"); + assertEquals("Subject SP", result); + verify(query).setParameter(ELECTION_ID, 1L); + verify(query).setParameter(TEMPLATE_TYPE, "TYPE"); + } + + public void testGetSubjectByElectionTypeLanguageEN() { + // Similar to previous test, but for English subject + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT t.subjectEN FROM ElectionEmailTemplate t WHERE t.election.electionId = :electionId AND t.templateType = :templateType", String.class)).thenReturn(query); + + when(query.setParameter(ELECTION_ID, 1L)).thenReturn(query); + when(query.setParameter(TEMPLATE_TYPE, "TYPE")).thenReturn(query); + when(query.getSingleResult()).thenReturn("Subject EN"); + + // Call the method and verify the result + String result = electionEmailTemplateDao.getSubjectByElectionTypeLanguageEN(1L, "type"); + assertEquals("Subject EN", result); + verify(query).setParameter(ELECTION_ID, 1L); + verify(query).setParameter(TEMPLATE_TYPE, "TYPE"); + } + + public void testGetSubjectByElectionTypeLanguagePT() { + // Similar to previous test, but for Portuguese subject + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT t.subjectPT FROM ElectionEmailTemplate t WHERE t.election.electionId = :electionId AND t.templateType = :templateType", String.class)).thenReturn(query); + + when(query.setParameter(ELECTION_ID, 1L)).thenReturn(query); + when(query.setParameter(TEMPLATE_TYPE, "TYPE")).thenReturn(query); + when(query.getSingleResult()).thenReturn("Subject PT"); + + // Call the method and verify the result + String result = electionEmailTemplateDao.getSubjectByElectionTypeLanguagePT(1L, "type"); + assertEquals("Subject PT", result); + verify(query).setParameter(ELECTION_ID, 1L); + verify(query).setParameter(TEMPLATE_TYPE, "TYPE"); + } + + public void testGetBodyByElectionTypeLanguageSP() { + // Similar to previous test, but for body in Spanish + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT t.bodySP FROM ElectionEmailTemplate t WHERE t.election.electionId = :electionId AND t.templateType = :templateType", String.class)).thenReturn(query); + + when(query.setParameter(ELECTION_ID, 1L)).thenReturn(query); + when(query.setParameter(TEMPLATE_TYPE, "TYPE")).thenReturn(query); + when(query.getSingleResult()).thenReturn("Body SP"); + + // Call the method and verify the result + String result = electionEmailTemplateDao.getBodyByElectionTypeLanguageSP(1L, "type"); + assertEquals("Body SP", result); + verify(query).setParameter(ELECTION_ID, 1L); + verify(query).setParameter(TEMPLATE_TYPE, "TYPE"); + } + + public void testGetBodyByElectionTypeLanguageEN() { + // Similar to previous test, but for body in English + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT t.bodyEN FROM ElectionEmailTemplate t WHERE t.election.electionId = :electionId AND t.templateType = :templateType", String.class)).thenReturn(query); + + when(query.setParameter(ELECTION_ID, 1L)).thenReturn(query); + when(query.setParameter(TEMPLATE_TYPE, "TYPE")).thenReturn(query); + when(query.getSingleResult()).thenReturn("Body EN"); + + // Call the method and verify the result + String result = electionEmailTemplateDao.getBodyByElectionTypeLanguageEN(1L, "type"); + assertEquals("Body EN", result); + verify(query).setParameter(ELECTION_ID, 1L); + verify(query).setParameter(TEMPLATE_TYPE, "TYPE"); + } + + public void testGetBodyByElectionTypeLanguagePT() { + // Similar to previous test, but for body in Portuguese + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT t.bodyPT FROM ElectionEmailTemplate t WHERE t.election.electionId = :electionId AND t.templateType = :templateType", String.class)).thenReturn(query); + + when(query.setParameter(ELECTION_ID, 1L)).thenReturn(query); + when(query.setParameter(TEMPLATE_TYPE, "TYPE")).thenReturn(query); + when(query.getSingleResult()).thenReturn("Body PT"); + + // Call the method and verify the result + String result = electionEmailTemplateDao.getBodyByElectionTypeLanguagePT(1L, "type"); + assertEquals("Body PT", result); + verify(query).setParameter(ELECTION_ID, 1L); + verify(query).setParameter(TEMPLATE_TYPE, "TYPE"); + } + + public void testGetElectionTemplates() { + // Mock the behavior of TypedQuery + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT t FROM ElectionEmailTemplate t WHERE t.election.electionId = :electionId", ElectionEmailTemplate.class)).thenReturn(query); + + ElectionEmailTemplate template1 = new ElectionEmailTemplate(); + ElectionEmailTemplate template2 = new ElectionEmailTemplate(); + List templates = Arrays.asList(template1, template2); + when(query.setParameter(ELECTION_ID, 1L)).thenReturn(query); + when(query.getResultList()).thenReturn(templates); + + // Call the method and verify the result + List result = electionEmailTemplateDao.getElectionTemplates(1L); + assertEquals(2, result.size()); + verify(query).setParameter(ELECTION_ID, 1L); + } + + public void testGetBaseTemplates() { + // Mock the behavior of TypedQuery + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT t FROM ElectionEmailTemplate t WHERE t.election = NULL", ElectionEmailTemplate.class)).thenReturn(query); + + ElectionEmailTemplate template1 = new ElectionEmailTemplate(); + ElectionEmailTemplate template2 = new ElectionEmailTemplate(); + List templates = Arrays.asList(template1, template2); + when(query.getResultList()).thenReturn(templates); + + // Call the method and verify the result + List result = electionEmailTemplateDao.getBaseTemplates(); + assertEquals(2, result.size()); + } + + public void testGetBaseTemplate() { + // Test for existing template + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT t FROM ElectionEmailTemplate t WHERE t.templateType = :templateType and t.election = NULL", ElectionEmailTemplate.class)).thenReturn(query); + + ElectionEmailTemplate template = new ElectionEmailTemplate(); + when(query.setParameter(TEMPLATE_TYPE, "TYPE")).thenReturn(query); + when(query.getSingleResult()).thenReturn(template); + + // Call the method and verify the result + ElectionEmailTemplate result = electionEmailTemplateDao.getBaseTemplate("type"); + assertNotNull(result); + verify(query).setParameter(TEMPLATE_TYPE, "TYPE"); + verify(query).getSingleResult(); + + // Test for non-existing template + when(query.getSingleResult()).thenThrow(new javax.persistence.NoResultException("No result")); + result = electionEmailTemplateDao.getBaseTemplate("nonexistent"); + assertNull(result); + } + + public void testGetElectionTemplateByType() { + // Mock the behavior of TypedQuery + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT t FROM ElectionEmailTemplate t WHERE t.templateType = :templateType and t.election.electionId = :electionId", ElectionEmailTemplate.class)).thenReturn(query); + + // Test case where the result list is not empty (template exists) + ElectionEmailTemplate template = new ElectionEmailTemplate(); + List templates = Collections.singletonList(template); + when(query.setParameter(TEMPLATE_TYPE, "TYPE")).thenReturn(query); + when(query.setParameter(ELECTION_ID, 1L)).thenReturn(query); + when(query.getResultList()).thenReturn(templates); + + // Call the method and verify the result + ElectionEmailTemplate result = electionEmailTemplateDao.getElectionTemplateByType("type", 1L); + assertNotNull(result); // Verify that a result is returned + verify(query).setParameter(TEMPLATE_TYPE, "TYPE"); + verify(query).setParameter(ELECTION_ID, 1L); + + // Test case where the result list is empty (no template found) + when(query.getResultList()).thenReturn(Collections.emptyList()); + + // Call the method and verify the result + result = electionEmailTemplateDao.getElectionTemplateByType("type", 1L); + assertNull(result); // Verify that null is returned when the list is empty + verify(query, times(2)).setParameter(TEMPLATE_TYPE, "TYPE"); + verify(query, times(2)).setParameter(ELECTION_ID, 1L); + } + + public void testGetElectionEmailTemplatesAllIdAndDescription() { + // Mock the behavior of Query + Query query = mock(Query.class); + when(em.createQuery("SELECT e.electionEmailTemplateId, e.templateType FROM ElectionEmailTemplate e ORDER BY e.electionEmailTemplateId")).thenReturn(query); + + Object[] resultArray = { 1L, "type" }; + List results = new ArrayList<>(Collections.singleton(resultArray)); + when(query.setMaxResults(10)).thenReturn(query); + when(query.setFirstResult(0)).thenReturn(query); + when(query.getResultList()).thenReturn(results); + + // Call the method and verify the result + List result = electionEmailTemplateDao.getElectionEmailTemplatesAllIdAndDescription(10, 0); + assertEquals(1, result.size()); + assertArrayEquals(resultArray, result.get(0)); + } +} diff --git a/elections-ejb/src/test/java/net/lacnic/elections/dao/ElectionsDaoFactoryTest.java b/elections-ejb/src/test/java/net/lacnic/elections/dao/ElectionsDaoFactoryTest.java new file mode 100644 index 0000000..856b738 --- /dev/null +++ b/elections-ejb/src/test/java/net/lacnic/elections/dao/ElectionsDaoFactoryTest.java @@ -0,0 +1,99 @@ +package net.lacnic.elections.dao; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +import javax.persistence.EntityManager; + +import static org.mockito.Mockito.mock; + +public class ElectionsDaoFactoryTest extends TestCase { + + private EntityManager em; + + @Override + public void setUp() { + // Initialize EntityManager mock + em = mock(EntityManager.class); + } + + public static Test suite() { + return new TestSuite(ElectionsDaoFactoryTest.class); + } + + public void testCreateUserVoterDao() { + UserVoterDao userVoterDao = ElectionsDaoFactory.createUserVoterDao(em); + assertNotNull(userVoterDao); + } + + public void testCreateVoteDao() { + VoteDao voteDao = ElectionsDaoFactory.createVoteDao(em); + assertNotNull(voteDao); + } + + public void testCreateElectionDao() { + ElectionDao electionDao = ElectionsDaoFactory.createElectionDao(em); + assertNotNull(electionDao); + } + + public void testCreateIpAccessDao() { + IpAccessDao ipAccessDao = ElectionsDaoFactory.createIpAccessDao(em); + assertNotNull(ipAccessDao); + } + + public void testCreateCandidateDao() { + CandidateDao candidateDao = ElectionsDaoFactory.createCandidateDao(em); + assertNotNull(candidateDao); + } + + public void testCreateActivityDao() { + ActivityDao activityDao = ElectionsDaoFactory.createActivityDao(em); + assertNotNull(activityDao); + } + + public void testCreateElectionEmailTemplateDao() { + ElectionEmailTemplateDao electionEmailTemplateDao = ElectionsDaoFactory.createElectionEmailTemplateDao(em); + assertNotNull(electionEmailTemplateDao); + } + + public void testCreateEmailDao() { + EmailDao emailDao = ElectionsDaoFactory.createEmailDao(em); + assertNotNull(emailDao); + } + + public void testCreateCommissionerDao() { + CommissionerDao commissionerDao = ElectionsDaoFactory.createCommissionerDao(em); + assertNotNull(commissionerDao); + } + + public void testCreateAuditorDao() { + AuditorDao auditorDao = ElectionsDaoFactory.createAuditorDao(em); + assertNotNull(auditorDao); + } + + public void testCreateReportDao() { + ReportDao reportDao = ElectionsDaoFactory.createReportDao(em); + assertNotNull(reportDao); + } + + public void testCreateUserAdminDao() { + UserAdminDao userAdminDao = ElectionsDaoFactory.createUserAdminDao(em); + assertNotNull(userAdminDao); + } + + public void testCreateParameterDao() { + ParameterDao parameterDao = ElectionsDaoFactory.createParameterDao(em); + assertNotNull(parameterDao); + } + + public void testCreateCustomizationDao() { + CustomizationDao customizationDao = ElectionsDaoFactory.createCustomizationDao(em); + assertNotNull(customizationDao); + } + + public void testCreateJointElectionDao() { + JointElectionDao jointElectionDao = ElectionsDaoFactory.createJointElectionDao(em); + assertNotNull(jointElectionDao); + } +} diff --git a/elections-ejb/src/test/java/net/lacnic/elections/dao/EmailDaoTest.java b/elections-ejb/src/test/java/net/lacnic/elections/dao/EmailDaoTest.java new file mode 100644 index 0000000..9baecc8 --- /dev/null +++ b/elections-ejb/src/test/java/net/lacnic/elections/dao/EmailDaoTest.java @@ -0,0 +1,187 @@ +package net.lacnic.elections.dao; + +import static net.lacnic.elections.utils.Constants.ELECTION_ID; +import static net.lacnic.elections.utils.Constants.EMAIL_HISTORY_ID; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.sql.Timestamp; +import java.util.Arrays; +import java.util.List; + +import javax.persistence.EntityManager; +import javax.persistence.Query; +import javax.persistence.TypedQuery; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; +import net.lacnic.elections.domain.Email; +import net.lacnic.elections.domain.EmailHistory; + +public class EmailDaoTest extends TestCase { + + private EntityManager em; + private EmailDao emailDao; + + public static Test suite() { + return new TestSuite(EmailDaoTest.class); + } + + @Override + public void setUp() { + em = mock(EntityManager.class); + emailDao = new EmailDao(em); + } + + public void testGetEmailsAll() { + @SuppressWarnings("unchecked") + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT e FROM Email e", Email.class)).thenReturn(query); + + Email email1 = new Email(); + Email email2 = new Email(); + when(query.getResultList()).thenReturn(Arrays.asList(email1, email2)); + + List result = emailDao.getEmailsAll(); + assertEquals(2, result.size()); + verify(query).getResultList(); + } + + public void testGetEmail() { + @SuppressWarnings("unchecked") + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT e FROM Email e WHERE e.emailId = :emailId", Email.class)).thenReturn(query); + + Email email = new Email(); + when(query.setParameter("emailId", 1L)).thenReturn(query); + when(query.getSingleResult()).thenReturn(email); + + Email result = emailDao.getEmail(1L); + assertNotNull(result); + verify(query).setParameter("emailId", 1L); + verify(query).getSingleResult(); + } + + public void testGetEmailHistory() { + @SuppressWarnings("unchecked") + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT eh FROM EmailHistory eh WHERE eh.emailHistoryId = :emailHistoryId", EmailHistory.class)).thenReturn(query); + + EmailHistory emailHistory = new EmailHistory(); + when(query.setParameter(EMAIL_HISTORY_ID, 1L)).thenReturn(query); + when(query.getSingleResult()).thenReturn(emailHistory); + + EmailHistory result = emailDao.getEmailHistory(1L); + assertNotNull(result); + verify(query).setParameter(EMAIL_HISTORY_ID, 1L); + verify(query).getSingleResult(); + } + + public void testGetPendingSendEmails() { + @SuppressWarnings("unchecked") + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT e FROM Email e WHERE e.sent = FALSE", Email.class)).thenReturn(query); + + Email email1 = new Email(); + Email email2 = new Email(); + when(query.getResultList()).thenReturn(Arrays.asList(email1, email2)); + + List result = emailDao.getPendingSendEmails(); + assertEquals(2, result.size()); + verify(query).getResultList(); + } + + public void testGetEmailsOlderOneMonth() { + @SuppressWarnings("unchecked") + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT e FROM Email e WHERE e.createdDate <= :nowMinus30Days", Email.class)).thenReturn(query); + + Timestamp timestamp = new Timestamp(System.currentTimeMillis() - 86400000L * 30); + when(query.setParameter("nowMinus30Days", timestamp)).thenReturn(query); + + Email email1 = new Email(); + Email email2 = new Email(); + when(query.getResultList()).thenReturn(Arrays.asList(email1, email2)); + + List result = emailDao.getEmailsOlderOneMonth(); + assertEquals(2, result.size()); + verify(query).getResultList(); + } + + public void testMarkAllEmailsAsSent() { + Query query = mock(Query.class); + when(em.createQuery("UPDATE Email SET sent = TRUE WHERE sent = FALSE")).thenReturn(query); + + emailDao.markAllEmailsAsSent(); + + verify(query).executeUpdate(); + } + + @SuppressWarnings("unchecked") + public void testGetElectionEmails() { + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT e FROM Email e WHERE e.election.electionId = :electionId", Email.class)).thenReturn(query); + + Email email1 = new Email(); + Email email2 = new Email(); + when(query.setParameter(ELECTION_ID, 100L)).thenReturn(query); + when(query.getResultList()).thenReturn(Arrays.asList(email1, email2)); + + List result = emailDao.getElectionEmails(100L); + assertEquals(2, result.size()); + verify(query).setParameter(ELECTION_ID, 100L); + verify(query).getResultList(); + } + + public void testGetElectionPendingSendEmails() { + @SuppressWarnings("unchecked") + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT e FROM Email e WHERE e.election.electionId = :electionId AND e.sent = FALSE", Email.class)).thenReturn(query); + + Email email1 = new Email(); + Email email2 = new Email(); + when(query.setParameter(ELECTION_ID, 100L)).thenReturn(query); + when(query.getResultList()).thenReturn(Arrays.asList(email1, email2)); + + List result = emailDao.getElectionPendingSendEmails(100L); + assertEquals(2, result.size()); + verify(query).setParameter(ELECTION_ID, 100L); + verify(query).getResultList(); + } + + public void testGetEmailsAllIdAndDescription() { + Query query = mock(Query.class); + when(em.createQuery("SELECT e.emailId, e.subject FROM Email e ORDER BY e.emailId")).thenReturn(query); + + Object[] result1 = new Object[] { 1L, "Subject 1" }; + Object[] result2 = new Object[] { 2L, "Subject 2" }; + when(query.setMaxResults(10)).thenReturn(query); + when(query.setFirstResult(0)).thenReturn(query); + when(query.getResultList()).thenReturn(Arrays.asList(result1, result2)); + + List result = emailDao.getEmailsAllIdAndDescription(10, 0); + assertEquals(2, result.size()); + verify(query).setMaxResults(10); + verify(query).setFirstResult(0); + verify(query).getResultList(); + } + + public void testGetEmailsHistoryAllIdAndDescription() { + Query query = mock(Query.class); + when(em.createQuery("SELECT eh.emailHistoryId, eh.subject FROM EmailHistory eh ORDER BY eh.emailHistoryId")).thenReturn(query); + + Object[] result1 = new Object[] { 1L, "Subject 1" }; + Object[] result2 = new Object[] { 2L, "Subject 2" }; + when(query.setMaxResults(10)).thenReturn(query); + when(query.setFirstResult(0)).thenReturn(query); + when(query.getResultList()).thenReturn(Arrays.asList(result1, result2)); + + List result = emailDao.getEmailsHistoryAllIdAndDescription(10, 0); + assertEquals(2, result.size()); + verify(query).setMaxResults(10); + verify(query).setFirstResult(0); + verify(query).getResultList(); + } +} diff --git a/elections-ejb/src/test/java/net/lacnic/elections/dao/IpAccessDaoTest.java b/elections-ejb/src/test/java/net/lacnic/elections/dao/IpAccessDaoTest.java new file mode 100644 index 0000000..eb75a5a --- /dev/null +++ b/elections-ejb/src/test/java/net/lacnic/elections/dao/IpAccessDaoTest.java @@ -0,0 +1,104 @@ +package net.lacnic.elections.dao; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.util.Arrays; +import java.util.List; + +import javax.persistence.EntityManager; +import javax.persistence.Query; +import javax.persistence.TypedQuery; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; +import net.lacnic.elections.domain.IpAccess; + +public class IpAccessDaoTest extends TestCase { + + private EntityManager em; + private IpAccessDao ipAccessDao; + + public static Test suite() { + return new TestSuite(IpAccessDaoTest.class); + } + + @Override + public void setUp() { + em = mock(EntityManager.class); + ipAccessDao = new IpAccessDao(em); + } + + public void testGetIP() { + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT i FROM IpAccess i WHERE i.ip =:ip", IpAccess.class)).thenReturn(query); + + IpAccess ipAccess = new IpAccess(); + when(query.setParameter("ip", "192.168.1.1")).thenReturn(query); + when(query.getSingleResult()).thenReturn(ipAccess); + + IpAccess result = ipAccessDao.getIP("192.168.1.1"); + assertNotNull(result); + verify(query).setParameter("ip", "192.168.1.1"); + verify(query).getSingleResult(); + } + + public void testGetIP_Exception() { + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT i FROM IpAccess i WHERE i.ip =:ip", IpAccess.class)).thenReturn(query); + when(query.setParameter("ip", "192.168.1.1")).thenReturn(query); + when(query.getSingleResult()).thenThrow(new RuntimeException("Database error")); + + IpAccess result = ipAccessDao.getIP("192.168.1.1"); + assertNull(result); + verify(query).setParameter("ip", "192.168.1.1"); + verify(query).getSingleResult(); + } + + public void testGetAllDisabledIPs() { + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT i FROM IpAccess i", IpAccess.class)).thenReturn(query); + + IpAccess ipAccess1 = new IpAccess(); + IpAccess ipAccess2 = new IpAccess(); + when(query.getResultList()).thenReturn(Arrays.asList(ipAccess1, ipAccess2)); + + List result = ipAccessDao.getAllDisabledIPs(); + assertEquals(2, result.size()); + verify(query).getResultList(); + } + + public void testGetIpAccessesAllIdAndDescription() { + Query query = mock(Query.class); + when(em.createQuery("SELECT i.ipAccessId, i.ip FROM IpAccess i ORDER BY i.ipAccessId")).thenReturn(query); + + Object[] result1 = new Object[] { 1L, "192.168.1.1" }; + Object[] result2 = new Object[] { 2L, "192.168.1.2" }; + when(query.setMaxResults(10)).thenReturn(query); + when(query.setFirstResult(0)).thenReturn(query); + when(query.getResultList()).thenReturn(Arrays.asList(result1, result2)); + + List result = ipAccessDao.getIpAccessesAllIdAndDescription(10, 0); + assertEquals(2, result.size()); + verify(query).setMaxResults(10); + verify(query).setFirstResult(0); + verify(query).getResultList(); + } + + public void testGetIpAccess() { + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT i FROM IpAccess i WHERE i.ipAccessId = :ipAccessId", IpAccess.class)).thenReturn(query); + + IpAccess ipAccess = new IpAccess(); + when(query.setParameter("ipAccessId", 1L)).thenReturn(query); + when(query.getSingleResult()).thenReturn(ipAccess); + + IpAccess result = ipAccessDao.getIpAccess(1L); + assertNotNull(result); + verify(query).setParameter("ipAccessId", 1L); + verify(query).getSingleResult(); + } + +} diff --git a/elections-ejb/src/test/java/net/lacnic/elections/dao/JointElectionDaoTest.java b/elections-ejb/src/test/java/net/lacnic/elections/dao/JointElectionDaoTest.java new file mode 100644 index 0000000..7ce53e9 --- /dev/null +++ b/elections-ejb/src/test/java/net/lacnic/elections/dao/JointElectionDaoTest.java @@ -0,0 +1,65 @@ +package net.lacnic.elections.dao; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.util.Arrays; +import java.util.List; + +import javax.persistence.EntityManager; +import javax.persistence.TypedQuery; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; +import net.lacnic.elections.domain.JointElection; + +public class JointElectionDaoTest extends TestCase { + + private EntityManager em; + private JointElectionDao jointElectionDao; + + public static Test suite() { + return new TestSuite(JointElectionDaoTest.class); + } + + @Override + public void setUp() { + em = mock(EntityManager.class); + jointElectionDao = new JointElectionDao(em); + } + + public void testGetJointElectionsIds() { + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT j.jointElectionId FROM JointElection j ORDER BY j.jointElectionId", Long.class)).thenReturn(query); + + Long jointElectionId1 = 1L; + Long jointElectionId2 = 2L; + when(query.getResultList()).thenReturn(Arrays.asList(jointElectionId1, jointElectionId2)); + + List result = jointElectionDao.getJointElectionsIds(2, 0); + + assertEquals(2, result.size()); + assertTrue(result.contains(jointElectionId1)); + assertTrue(result.contains(jointElectionId2)); + verify(query).setMaxResults(2); + verify(query).setFirstResult(0); + verify(query).getResultList(); + } + + public void testGetJointElection() { + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT j FROM JointElection j WHERE j.jointElectionId = :jointElectionId", JointElection.class)).thenReturn(query); + + JointElection jointElection = new JointElection(); + when(query.setParameter("jointElectionId", 1L)).thenReturn(query); + when(query.getSingleResult()).thenReturn(jointElection); + + JointElection result = jointElectionDao.getJointElection(1L); + + assertNotNull(result); + verify(query).setParameter("jointElectionId", 1L); + verify(query).getSingleResult(); + } +} diff --git a/elections-ejb/src/test/java/net/lacnic/elections/dao/ParameterDaoTest.java b/elections-ejb/src/test/java/net/lacnic/elections/dao/ParameterDaoTest.java new file mode 100644 index 0000000..74bf214 --- /dev/null +++ b/elections-ejb/src/test/java/net/lacnic/elections/dao/ParameterDaoTest.java @@ -0,0 +1,72 @@ +package net.lacnic.elections.dao; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.util.Arrays; +import java.util.List; + +import javax.persistence.EntityManager; +import javax.persistence.TypedQuery; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; +import net.lacnic.elections.domain.Parameter; + +public class ParameterDaoTest extends TestCase { + + private EntityManager em; + private ParameterDao parameterDao; + + public static Test suite() { + return new TestSuite(ParameterDaoTest.class); + } + + @Override + public void setUp() { + em = mock(EntityManager.class); + parameterDao = new ParameterDao(em); + } + + public void testGetParameter() { + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT p FROM Parameter p WHERE p.key = :key", Parameter.class)).thenReturn(query); + + Parameter parameter = new Parameter(); + when(query.setParameter("key", "someKey")).thenReturn(query); + when(query.getSingleResult()).thenReturn(parameter); + + Parameter result = parameterDao.getParameter("someKey"); + assertNotNull(result); + verify(query).setParameter("key", "someKey"); + verify(query).getSingleResult(); + } + + public void testGetParameter_ExceptionHandling() { + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT p FROM Parameter p WHERE p.key = :key", Parameter.class)).thenReturn(query); + when(query.setParameter("key", "someKey")).thenReturn(query); + when(query.getSingleResult()).thenThrow(new RuntimeException("Database error")); + + Parameter result = parameterDao.getParameter("someKey"); + + assertNull(result); + verify(query).setParameter("key", "someKey"); + verify(query).getSingleResult(); + } + + public void testGetParametersAll() { + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT p FROM Parameter p", Parameter.class)).thenReturn(query); + + Parameter parameter1 = new Parameter(); + Parameter parameter2 = new Parameter(); + when(query.getResultList()).thenReturn(Arrays.asList(parameter1, parameter2)); + + List result = parameterDao.getParametersAll(); + assertEquals(2, result.size()); + verify(query).getResultList(); + } +} diff --git a/elections-ejb/src/test/java/net/lacnic/elections/dao/ReportDaoTest.java b/elections-ejb/src/test/java/net/lacnic/elections/dao/ReportDaoTest.java new file mode 100644 index 0000000..8e12017 --- /dev/null +++ b/elections-ejb/src/test/java/net/lacnic/elections/dao/ReportDaoTest.java @@ -0,0 +1,180 @@ +package net.lacnic.elections.dao; + +import static net.lacnic.elections.utils.Constants.ELECTION_ID; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.util.Arrays; +import java.util.List; + +import javax.persistence.EntityManager; +import javax.persistence.Query; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +public class ReportDaoTest extends TestCase { + + private EntityManager em; + private ReportDao reportDao; + + public static Test suite() { + return new TestSuite(ReportDaoTest.class); + } + + @Override + public void setUp() { + em = mock(EntityManager.class); + reportDao = new ReportDao(em); + } + + public void testGetEmailsAmount() { + Query query = mock(Query.class); + when(em.createQuery("SELECT COUNT(e.emailId) FROM Email e")).thenReturn(query); + when(query.getSingleResult()).thenReturn(100L); + + long result = reportDao.getEmailsAmount(); + assertEquals(100L, result); + verify(query).getSingleResult(); + } + + public void testGetPendingSendEmailsAmount() { + Query query = mock(Query.class); + when(em.createQuery("SELECT COUNT(e.emailId) FROM Email e WHERE e.sent = FALSE")).thenReturn(query); + when(query.getSingleResult()).thenReturn(50L); + + long result = reportDao.getPendingSendEmailsAmount(); + assertEquals(50L, result); + verify(query).getSingleResult(); + } + + public void testGetSentEmailsAmount() { + Query query = mock(Query.class); + when(em.createQuery("SELECT COUNT(e.emailId) FROM Email e WHERE e.sent = TRUE")).thenReturn(query); + when(query.getSingleResult()).thenReturn(150L); + + long result = reportDao.getSentEmailsAmount(); + assertEquals(150L, result); + verify(query).getSingleResult(); + } + + public void testGetFailedIpAccessesAmount() { + Query query = mock(Query.class); + when(em.createQuery("SELECT COUNT(a.ipAccessId) FROM IpAccess a")).thenReturn(query); + when(query.getSingleResult()).thenReturn(5L); + + long result = reportDao.getFailedIpAccesesAmount(); + assertEquals(5L, result); + verify(query).getSingleResult(); + } + + public void testGetFailedIpAccessesSum() { + Query query = mock(Query.class); + when(em.createQuery("SELECT COALESCE(SUM (a.attemptCount), 0) FROM IpAccess a")).thenReturn(query); + when(query.getSingleResult()).thenReturn(200L); + + long result = reportDao.getFailedIpAccesesSum(); + assertEquals(200L, result); + verify(query).getSingleResult(); + } + + public void testGetElectionPendingSendEmailsAmount() { + Query query = mock(Query.class); + when(em.createQuery("SELECT COUNT(e.emailId) FROM Email e WHERE e.election.electionId = :electionId AND e.sent = FALSE")).thenReturn(query); + when(query.setParameter(ELECTION_ID, 1L)).thenReturn(query); + when(query.getSingleResult()).thenReturn(30L); + + long result = reportDao.getElectionPendingSendEmailsAmount(1L); + assertEquals(30L, result); + verify(query).setParameter(ELECTION_ID, 1L); + verify(query).getSingleResult(); + } + + public void testGetElectionAlreadyVotedAmount() { + Query query = mock(Query.class); + when(em.createQuery("SELECT COUNT(u.userVoterId) FROM UserVoter u WHERE u.election.electionId =: electionId AND u.voted = TRUE")).thenReturn(query); + when(query.setParameter(ELECTION_ID, 1L)).thenReturn(query); + when(query.getSingleResult()).thenReturn(10L); + + long result = reportDao.getElectionAlreadyVotedAmount(1L); + assertEquals(10L, result); + verify(query).setParameter(ELECTION_ID, 1L); + verify(query).getSingleResult(); + } + + public void testGetElectionNotVotedYetAmount() { + Query query = mock(Query.class); + when(em.createQuery("SELECT COUNT(u.userVoterId) FROM UserVoter u WHERE u.election.electionId =: electionId AND u.voted = FALSE")).thenReturn(query); + when(query.setParameter(ELECTION_ID, 1L)).thenReturn(query); + when(query.getSingleResult()).thenReturn(20L); + + long result = reportDao.getElectionNotVotedYetAmount(1L); + assertEquals(20L, result); + verify(query).setParameter(ELECTION_ID, 1L); + verify(query).getSingleResult(); + } + + public void testGetElectionCensusSize() { + Query query = mock(Query.class); + when(em.createQuery("SELECT COUNT(u.userVoterId) FROM UserVoter u WHERE u.election.electionId =: electionId")).thenReturn(query); + when(query.setParameter(ELECTION_ID, 1L)).thenReturn(query); + when(query.getSingleResult()).thenReturn(100L); + + long result = reportDao.getElectionCensusSize(1L); + assertEquals(100L, result); + verify(query).setParameter(ELECTION_ID, 1L); + verify(query).getSingleResult(); + } + + public void testGetParameterExceptionHandling() { + Query query = mock(Query.class); + when(em.createQuery("SELECT COUNT(e.emailId) FROM Email e")).thenReturn(query); + + when(query.getSingleResult()).thenThrow(new RuntimeException("Database error")); + + try { + reportDao.getEmailsAmount(); + fail("Expected exception not thrown"); + } catch (Exception e) { + assertTrue(e instanceof RuntimeException); + } + + verify(query).getSingleResult(); + } + + public void testGetElectionsAllIdName() { + Query query = mock(Query.class); + when(em.createQuery("SELECT e.electionId, e.titleSpanish FROM Election e")).thenReturn(query); + + Object[] election1 = new Object[] { 1L, "Election 1" }; + Object[] election2 = new Object[] { 2L, "Election 2" }; + when(query.getResultList()).thenReturn(Arrays.asList(election1, election2)); + + List result = reportDao.getElectionsAllIdName(); + assertEquals(2, result.size()); // Ensure the correct number of results + verify(query).getResultList(); // Verify that getResultList() was called once + } + + public void testGetFailedIpAccesesAmount() { + Query query = mock(Query.class); + when(em.createQuery("SELECT COUNT(a.ipAccessId) FROM IpAccess a")).thenReturn(query); + when(query.getSingleResult()).thenReturn(3L); + + long result = reportDao.getFailedIpAccesesAmount(); + assertEquals(3L, result); + verify(query).getSingleResult(); + } + + public void testGetFailedIpAccesesSum() { + Query query = mock(Query.class); + when(em.createQuery("SELECT COALESCE(SUM (a.attemptCount), 0) FROM IpAccess a")).thenReturn(query); + when(query.getSingleResult()).thenReturn(15L); + + long result = reportDao.getFailedIpAccesesSum(); + assertEquals(15L, result); + verify(query).getSingleResult(); + } + +} diff --git a/elections-ejb/src/test/java/net/lacnic/elections/dao/UserAdminDaoTest.java b/elections-ejb/src/test/java/net/lacnic/elections/dao/UserAdminDaoTest.java new file mode 100644 index 0000000..b870f13 --- /dev/null +++ b/elections-ejb/src/test/java/net/lacnic/elections/dao/UserAdminDaoTest.java @@ -0,0 +1,233 @@ +package net.lacnic.elections.dao; + +import static net.lacnic.elections.utils.Constants.ELECTION_ID; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import javax.persistence.EntityManager; +import javax.persistence.Query; +import javax.persistence.TypedQuery; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; +import net.lacnic.elections.domain.UserAdmin; + +public class UserAdminDaoTest extends TestCase { + + private EntityManager em; + private UserAdminDao userAdminDao; + + public static Test suite() { + return new TestSuite(UserAdminDaoTest.class); + } + + @Override + public void setUp() { + em = mock(EntityManager.class); + userAdminDao = new UserAdminDao(em); + } + + public void testVerifyUserLogin() { + // Mocking the query and setting the expected behavior + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT a FROM UserAdmin a WHERE a.userAdminId = :userAdminId and a.password = :password", UserAdmin.class)).thenReturn(query); + + UserAdmin userAdmin = new UserAdmin(); + when(query.setParameter("userAdminId", "admin123")).thenReturn(query); + when(query.setParameter("password", "SECUREPASSWORD")).thenReturn(query); + when(query.getSingleResult()).thenReturn(userAdmin); + + UserAdmin result = userAdminDao.verifyUserLogin("admin123", "SECUREPASSWORD"); + assertNotNull(result); + verify(query).setParameter("userAdminId", "admin123"); + verify(query).setParameter("password", "SECUREPASSWORD"); + verify(query).getSingleResult(); + } + + public void testVerifyUserLogin_Exception() { + // Mocking exception handling scenario + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT a FROM UserAdmin a WHERE a.userAdminId = :userAdminId and a.password = :password", UserAdmin.class)).thenReturn(query); + + when(query.setParameter("userAdminId", "admin123")).thenReturn(query); + when(query.setParameter("password", "SECUREPASSWORD")).thenReturn(query); + when(query.getSingleResult()).thenThrow(new RuntimeException("Database error")); + + UserAdmin result = userAdminDao.verifyUserLogin("admin123", "SECUREPASSWORD"); + assertNull(result); + verify(query).setParameter("userAdminId", "admin123"); + verify(query).setParameter("password", "SECUREPASSWORD"); + verify(query).getSingleResult(); + } + + public void testGetUserAdminsAll() { + // Mocking the query to return a list of UserAdmins + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT a FROM UserAdmin a", UserAdmin.class)).thenReturn(query); + + UserAdmin userAdmin1 = new UserAdmin(); + UserAdmin userAdmin2 = new UserAdmin(); + List userAdminList = new ArrayList<>(); + userAdminList.add(userAdmin1); + userAdminList.add(userAdmin2); + when(query.getResultList()).thenReturn(userAdminList); + + List result = userAdminDao.getUserAdminsAll(); + assertEquals(2, result.size()); + verify(query).getResultList(); + } + + public void testGetUserAdminsAll_Exception() { + // Mocking exception handling scenario for getUserAdminsAll method + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT a FROM UserAdmin a", UserAdmin.class)).thenReturn(query); + + when(query.getResultList()).thenThrow(new RuntimeException("Database error")); + + List result = userAdminDao.getUserAdminsAll(); + assertEquals(0, result.size()); // Should return an empty list on exception + verify(query).getResultList(); + } + + public void testGetUserAdmin() { + // Mocking the query and setting the expected behavior + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT a FROM UserAdmin a WHERE UPPER(a.userAdminId) = :userAdminId", UserAdmin.class)).thenReturn(query); + + UserAdmin userAdmin = new UserAdmin(); + when(query.setParameter("userAdminId", "ADMIN123")).thenReturn(query); + when(query.getSingleResult()).thenReturn(userAdmin); + + UserAdmin result = userAdminDao.getUserAdmin("ADMIN123"); + assertNotNull(result); + verify(query).setParameter("userAdminId", "ADMIN123"); + verify(query).getSingleResult(); + } + + public void testGetUserAdmin_Exception() { + // Mocking exception handling scenario for getUserAdmin method + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT a FROM UserAdmin a WHERE UPPER(a.userAdminId) = :userAdminId", UserAdmin.class)).thenReturn(query); + + when(query.setParameter("userAdminId", "ADMIN123")).thenReturn(query); + when(query.getSingleResult()).thenThrow(new RuntimeException("Database error")); + + UserAdmin result = userAdminDao.getUserAdmin("ADMIN123"); + assertNull(result); // Should return null on exception + verify(query).setParameter("userAdminId", "ADMIN123"); + verify(query).getSingleResult(); + } + + public void testGetElectionUserAdmins() { + // Mocking the query and setting the expected behavior + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT a FROM UserAdmin a WHERE a.authorizedElectionId = :electionId", UserAdmin.class)).thenReturn(query); + + UserAdmin userAdmin1 = new UserAdmin(); + UserAdmin userAdmin2 = new UserAdmin(); + when(query.setParameter(ELECTION_ID, 100L)).thenReturn(query); + List userAdminList = new ArrayList<>(); + userAdminList.add(userAdmin1); + userAdminList.add(userAdmin2); + when(query.getResultList()).thenReturn(userAdminList); + + List result = userAdminDao.getElectionUserAdmins(100L); + assertEquals(2, result.size()); + verify(query).setParameter(ELECTION_ID, 100L); + verify(query).getResultList(); + } + + public void testGetUserAuthorizedElectionId() { + // Mocking the query and setting the expected behavior + Query query = mock(Query.class); + when(em.createQuery("SELECT a.authorizedElectionId FROM UserAdmin a WHERE UPPER(a.userAdminId) = :userAdminId")).thenReturn(query); + + when(query.setParameter("userAdminId", "ADMIN123")).thenReturn(query); + when(query.getSingleResult()).thenReturn(100L); + + long result = userAdminDao.getUserAuthorizedElectionId("ADMIN123"); + assertEquals(100L, result); + verify(query).setParameter("userAdminId", "ADMIN123"); + verify(query).getSingleResult(); + } + + public void testGetUserAuthorizedElectionId_Exception() { + // Mocking exception handling scenario for getUserAuthorizedElectionId method + Query query = mock(Query.class); + when(em.createQuery("SELECT a.authorizedElectionId FROM UserAdmin a WHERE UPPER(a.userAdminId) = :userAdminId")).thenReturn(query); + + when(query.setParameter("userAdminId", "ADMIN123")).thenReturn(query); + when(query.getSingleResult()).thenThrow(new RuntimeException("Database error")); + + long result = userAdminDao.getUserAuthorizedElectionId("ADMIN123"); + assertEquals(0L, result); // Should return 0L on exception + verify(query).setParameter("userAdminId", "ADMIN123"); + verify(query).getSingleResult(); + } + + public void testGetElectionUserAdmins_Exception() { + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT a FROM UserAdmin a WHERE a.authorizedElectionId = :electionId", UserAdmin.class)).thenReturn(query); + when(query.setParameter(ELECTION_ID, 100L)).thenReturn(query); + when(query.getResultList()).thenThrow(new RuntimeException("Database error")); + + List result = userAdminDao.getElectionUserAdmins(100L); + assertTrue(result.isEmpty()); + verify(query).setParameter(ELECTION_ID, 100L); + verify(query).getResultList(); + } + + public void testGetUserAdminsAllIdAndName() { + Query query = mock(Query.class); + when(em.createQuery("SELECT a.userAdminId, a.email FROM UserAdmin a ORDER BY a.userAdminId")).thenReturn(query); + + Object[] userAdmin1 = new Object[] { 1L, "admin1@example.com" }; + Object[] userAdmin2 = new Object[] { 2L, "admin2@example.com" }; + when(query.getResultList()).thenReturn(Arrays.asList(userAdmin1, userAdmin2)); + + List result = userAdminDao.getUserAdminsAllIdAndName(10, 0); + + assertEquals(2, result.size()); + verify(query).setMaxResults(10); + verify(query).setFirstResult(0); + verify(query).getResultList(); + } + + public void testGetUserAdminByEmail_Exception() { + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT a FROM UserAdmin a WHERE a.email = :userAdminEmail", UserAdmin.class)).thenReturn(query); + + when(query.setParameter("userAdminEmail", "admin@example.com")).thenReturn(query); + when(query.getSingleResult()).thenThrow(new RuntimeException("Database error")); + + UserAdmin result = userAdminDao.getUserAdminByEmail("admin@example.com"); + + assertNull(result); + verify(query).setParameter("userAdminEmail", "admin@example.com"); + verify(query).getSingleResult(); + } + + public void testGetUserAdminByEmail_Success() { + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT a FROM UserAdmin a WHERE a.email = :userAdminEmail", UserAdmin.class)).thenReturn(query); + + UserAdmin userAdmin = new UserAdmin(); + userAdmin.setUserAdminId("admin123"); + when(query.setParameter("userAdminEmail", "admin@example.com")).thenReturn(query); + when(query.getSingleResult()).thenReturn(userAdmin); + + UserAdmin result = userAdminDao.getUserAdminByEmail("admin@example.com"); + + assertNotNull(result); + assertEquals("admin123", result.getUserAdminId()); + verify(query).setParameter("userAdminEmail", "admin@example.com"); + verify(query).getSingleResult(); + } + +} diff --git a/elections-ejb/src/test/java/net/lacnic/elections/dao/UserVoterDaoTest.java b/elections-ejb/src/test/java/net/lacnic/elections/dao/UserVoterDaoTest.java new file mode 100644 index 0000000..34a8973 --- /dev/null +++ b/elections-ejb/src/test/java/net/lacnic/elections/dao/UserVoterDaoTest.java @@ -0,0 +1,458 @@ +package net.lacnic.elections.dao; + +import static net.lacnic.elections.utils.Constants.ELECTION_ID; +import static net.lacnic.elections.utils.Constants.ORG_ID; +import static net.lacnic.elections.utils.Constants.USER_VOTER_ID; +import static net.lacnic.elections.utils.Constants.VOTE_AMOUNT; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import javax.persistence.EntityManager; +import javax.persistence.Query; +import javax.persistence.TypedQuery; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; +import net.lacnic.elections.domain.UserVoter; + +public class UserVoterDaoTest extends TestCase { + + private EntityManager em; + private UserVoterDao userVoterDao; + + public static Test suite() { + return new TestSuite(UserVoterDaoTest.class); + } + + @Override + public void setUp() { + em = mock(EntityManager.class); + userVoterDao = new UserVoterDao(em); + } + + public void testGetUserVoter() { + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT u FROM UserVoter u WHERE u.userVoterId = :userVoterId", UserVoter.class)).thenReturn(query); + + UserVoter userVoter = new UserVoter(); + when(query.setParameter(USER_VOTER_ID, 1L)).thenReturn(query); + when(query.getSingleResult()).thenReturn(userVoter); + + UserVoter result = userVoterDao.getUserVoter(1L); + assertNotNull(result); + verify(query).setParameter(USER_VOTER_ID, 1L); + verify(query).getSingleResult(); + } + + public void testGetUserVoterByEmail() { + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT u FROM UserVoter u WHERE u.mail = :email ORDER BY u.userVoterId", UserVoter.class)).thenReturn(query); + + UserVoter userVoter = new UserVoter(); + when(query.setParameter("email", "user@example.com")).thenReturn(query); + when(query.getResultList()).thenReturn(Collections.singletonList(userVoter)); + + List result = userVoterDao.getUserVotersByEmail("user@example.com", 10, 0); + assertEquals(1, result.size()); + verify(query).getResultList(); + } + + public void testGetElectionUserVoters() { + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT u FROM UserVoter u WHERE u.election.electionId = :electionId ORDER BY u.userVoterId", UserVoter.class)).thenReturn(query); + + UserVoter userVoter1 = new UserVoter(); + UserVoter userVoter2 = new UserVoter(); + when(query.setParameter(ELECTION_ID, 100L)).thenReturn(query); + when(query.getResultList()).thenReturn(Arrays.asList(userVoter1, userVoter2)); + + List result = userVoterDao.getElectionUserVoters(100L); + assertEquals(2, result.size()); + verify(query).getResultList(); + } + + public void testGetElectionUserVoterByMail() { + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT u FROM UserVoter u WHERE u.election.electionId = :electionId AND u.mail = :mail", UserVoter.class)).thenReturn(query); + + UserVoter userVoter = new UserVoter(); + when(query.setParameter(ELECTION_ID, 100L)).thenReturn(query); + when(query.setParameter("mail", "user@example.com")).thenReturn(query); + when(query.getResultList()).thenReturn(Collections.singletonList(userVoter)); + + UserVoter result = userVoterDao.getElectionUserVoterByMail(100L, "user@example.com"); + assertNotNull(result); + verify(query).getResultList(); + } + + public void testGetElectionUserVoterByMail_EmptyList() { + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT u FROM UserVoter u WHERE u.election.electionId = :electionId AND u.mail = :mail", UserVoter.class)).thenReturn(query); + + when(query.setParameter(ELECTION_ID, 100L)).thenReturn(query); + when(query.setParameter("mail", "user@example.com")).thenReturn(query); + when(query.getResultList()).thenReturn(Collections.emptyList()); + + UserVoter result = userVoterDao.getElectionUserVoterByMail(100L, "user@example.com"); + assertNull(result); + verify(query).getResultList(); + } + + public void testGetElectionUserVoterByMail_NullList() { + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT u FROM UserVoter u WHERE u.election.electionId = :electionId AND u.mail = :mail", UserVoter.class)).thenReturn(query); + + when(query.setParameter(ELECTION_ID, 100L)).thenReturn(query); + when(query.setParameter("mail", "user@example.com")).thenReturn(query); + + when(query.getResultList()).thenReturn(null); + + UserVoter result = userVoterDao.getElectionUserVoterByMail(100L, "user@example.com"); + assertNull(result); + verify(query).getResultList(); + } + + public void testGetElectionCensusByCountry() { + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT u FROM UserVoter u WHERE u.election.electionId =:electionId AND u.country = :country ORDER BY u.userVoterId", UserVoter.class)).thenReturn(query); + + UserVoter userVoter = new UserVoter(); + when(query.setParameter(ELECTION_ID, 100L)).thenReturn(query); + when(query.setParameter("country", "Uruguay")).thenReturn(query); + when(query.getResultList()).thenReturn(Collections.singletonList(userVoter)); + + List result = userVoterDao.getElectionCensusByCountry(100L, "Uruguay"); + assertEquals(1, result.size()); + verify(query).getResultList(); + } + + public void testGetElectionsUserVotersNotVotedYet() { + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT u FROM UserVoter u WHERE u.election.electionId =:electionId AND u.voted = FALSE ORDER BY u.userVoterId DESC", UserVoter.class)).thenReturn(query); + + UserVoter userVoter = new UserVoter(); + when(query.setParameter(ELECTION_ID, 100L)).thenReturn(query); + when(query.getResultList()).thenReturn(Collections.singletonList(userVoter)); + + List result = userVoterDao.getElectionsUserVotersNotVotedYet(100L); + assertEquals(1, result.size()); + verify(query).getResultList(); + } + + public void testGetElectionsUserVotersNotVotedYetByCountry() { + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT u FROM UserVoter u WHERE u.election.electionId =:electionId AND u.voted = FALSE AND u.country = :country ORDER BY u.userVoterId DESC", UserVoter.class)).thenReturn(query); + + UserVoter userVoter = new UserVoter(); + when(query.setParameter(ELECTION_ID, 100L)).thenReturn(query); + when(query.setParameter("country", "Uruguay")).thenReturn(query); + when(query.getResultList()).thenReturn(Collections.singletonList(userVoter)); + + List result = userVoterDao.getElectionsUserVotersNotVotedYetByCountry(100L, "Uruguay"); + assertEquals(1, result.size()); + verify(query).getResultList(); + } + + public void testGetElectionsUserVotersVoted() { + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT u FROM UserVoter u WHERE u.election.electionId =:electionId AND u.voted = TRUE ORDER BY u.voteDate ASC", UserVoter.class)).thenReturn(query); + + UserVoter userVoter = new UserVoter(); + when(query.setParameter(ELECTION_ID, 100L)).thenReturn(query); + when(query.getResultList()).thenReturn(Collections.singletonList(userVoter)); + + List result = userVoterDao.getElectionsUserVotersVoted(100L); + assertEquals(1, result.size()); + verify(query).getResultList(); + } + + public void testGetElectionsUserVotersVotedByCountry() { + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT u FROM UserVoter u WHERE u.election.electionId =:electionId AND u.voted = TRUE AND u.country = :country ORDER BY u.voteDate ASC", UserVoter.class)).thenReturn(query); + + UserVoter userVoter = new UserVoter(); + when(query.setParameter(ELECTION_ID, 100L)).thenReturn(query); + when(query.setParameter("country", "Uruguay")).thenReturn(query); + when(query.getResultList()).thenReturn(Collections.singletonList(userVoter)); + + List result = userVoterDao.getElectionsUserVotersVotedByCountry(100L, "Uruguay"); + assertEquals(1, result.size()); + verify(query).getResultList(); + } + + public void testGetUserVoterByToken() { + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT u FROM UserVoter u WHERE u.voteToken = :voteToken", UserVoter.class)).thenReturn(query); + + UserVoter userVoter = new UserVoter(); + when(query.setParameter("voteToken", "token123")).thenReturn(query); + when(query.getSingleResult()).thenReturn(userVoter); + + UserVoter result = userVoterDao.getUserVoterByToken("token123"); + assertNotNull(result); + verify(query).getSingleResult(); + } + + public void testGetElectionCensusSize() { + Query query = mock(Query.class); + when(em.createQuery("SELECT COUNT(u.userVoterId) FROM UserVoter u WHERE u.election.electionId =: electionId")).thenReturn(query); + + when(query.setParameter(ELECTION_ID, 100L)).thenReturn(query); + when(query.getSingleResult()).thenReturn(50L); + + long result = userVoterDao.getElectionCensusSize(100L); + assertEquals(50L, result); + verify(query).getSingleResult(); + } + + public void testElectionsCensusEqual() { + // Mock the TypedQuery for both queries + TypedQuery query1 = mock(TypedQuery.class); + TypedQuery query2 = mock(TypedQuery.class); + + // Mock the EntityManager to return different queries for each call to createQuery + when(em.createQuery("SELECT u.mail FROM UserVoter u WHERE u.election.electionId = :electionId", String.class)).thenReturn(query1) // First call returns query1 + .thenReturn(query2); // Second call returns query2 + + // Set the parameters for both queries + when(query1.setParameter(ELECTION_ID, 100L)).thenReturn(query1); + when(query2.setParameter(ELECTION_ID, 100L)).thenReturn(query2); + + // Mock the result for both queries + when(query1.getResultList()).thenReturn(Arrays.asList("user1@example.com", "user2@example.com")); + when(query2.getResultList()).thenReturn(Arrays.asList("user1@example.com", "user2@example.com")); + + boolean result = userVoterDao.electionsCensusEqual(100L, 100L); + + // Assert the result + assertTrue(result); + + // Verify that getResultList() was called on both queries + verify(query1).getResultList(); + verify(query2).getResultList(); + } + + public void testGetJointElectionUserVotersNotVotedYet() { + TypedQuery query = mock(TypedQuery.class); + + when(em.createQuery("SELECT u FROM UserVoter u, JointElection p WHERE (p.idElectionA = :electionId OR p.idElectionB = :electionId)" + " AND (u.election.electionId = p.idElectionA OR u.election.electionId = p.idElectionB)" + " AND u.voted = FALSE ORDER BY u.voteDate ASC", UserVoter.class)).thenReturn(query); + + when(query.setParameter(ELECTION_ID, 100L)).thenReturn(query); + + // Mock the result list + UserVoter userVoter1 = new UserVoter(); + UserVoter userVoter2 = new UserVoter(); + when(query.getResultList()).thenReturn(Arrays.asList(userVoter1, userVoter2)); + + List result = userVoterDao.getJointElectionUserVotersNotVotedYet(100L); + + assertEquals(2, result.size()); + + // Verify that the correct query and parameters were used + verify(query).setParameter(ELECTION_ID, 100L); + verify(query).getResultList(); + } + + public void testGetJointElectionUserVotersNotVotedYetByCountry() { + TypedQuery query = mock(TypedQuery.class); + + when(em.createQuery("SELECT u FROM UserVoter u, JointElection p WHERE (p.idElectionA = :electionId OR p.idElectionB = :electionId)" + " AND (u.election.electionId = p.idElectionA OR u.election.electionId = p.idElectionB)" + " AND u.voted = FALSE AND u.country = :country ORDER BY u.voteDate ASC", UserVoter.class)).thenReturn(query); + + when(query.setParameter(ELECTION_ID, 100L)).thenReturn(query); + when(query.setParameter("country", "URUGUAY")).thenReturn(query); + + // Mock the result list + UserVoter userVoter1 = new UserVoter(); + UserVoter userVoter2 = new UserVoter(); + when(query.getResultList()).thenReturn(Arrays.asList(userVoter1, userVoter2)); + + List result = userVoterDao.getJointElectionUserVotersNotVotedYetByCountry(100L, "Uruguay"); + + assertEquals(2, result.size()); + + // Verify that the correct query and parameters were used + verify(query).setParameter(ELECTION_ID, 100L); + verify(query).setParameter("country", "URUGUAY"); + verify(query).getResultList(); + } + + public void testGetElectionUserVotersVotedAmount() { + Query query = mock(Query.class); + when(em.createQuery("SELECT COUNT(u.userVoterId) FROM UserVoter u WHERE u.election.electionId =:electionId AND u.voted = TRUE")).thenReturn(query); + + when(query.setParameter(ELECTION_ID, 100L)).thenReturn(query); + when(query.getSingleResult()).thenReturn(5L); + + Long result = userVoterDao.getElectionUserVotersVotedAmount(100L); + + assertEquals(Long.valueOf(5), result); + + // Verify that the query was executed correctly + verify(query).setParameter(ELECTION_ID, 100L); + verify(query).getSingleResult(); + } + + public void testGetElectionUserVotersDistinctVoteAmounts() { + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT DISTINCT(u.voteAmount) FROM UserVoter u WHERE u.election.electionId = :electionId ORDER BY u.voteAmount", Integer.class)).thenReturn(query); + + when(query.setParameter(ELECTION_ID, 100L)).thenReturn(query); + when(query.getResultList()).thenReturn(Arrays.asList(1, 2, 3)); + + List result = userVoterDao.getElectionUserVotersDistinctVoteAmounts(100L); + + // Assert that the result is correct + assertEquals(3, result.size()); + assertTrue(result.contains(1)); + assertTrue(result.contains(2)); + assertTrue(result.contains(3)); + + // Verify that the query was executed correctly + verify(query).setParameter(ELECTION_ID, 100L); + verify(query).getResultList(); + } + + public void testGetElectionUserVotersAmountByVoteAmount() { + Query query = mock(Query.class); + when(em.createQuery("SELECT COUNT(u.userVoterId) FROM UserVoter u WHERE u.election.electionId = :electionId AND u.voteAmount = :voteAmount")).thenReturn(query); + + when(query.setParameter(ELECTION_ID, 100L)).thenReturn(query); + when(query.setParameter(VOTE_AMOUNT, 3)).thenReturn(query); + when(query.getSingleResult()).thenReturn(10L); + + Long result = userVoterDao.getElectionUserVotersAmountByVoteAmount(100L, 3); + + // Assert that the result is correct + assertEquals(Long.valueOf(10), result); + + // Verify that the query was executed correctly + verify(query).setParameter(ELECTION_ID, 100L); + verify(query).setParameter(VOTE_AMOUNT, 3); + verify(query).getSingleResult(); + } + + public void testGetElectionUserVotersAmountByVoteAmountAndVoted() { + // Mock the Query object + Query query = mock(Query.class); + when(em.createQuery("SELECT COUNT(u.userVoterId) FROM UserVoter u WHERE u.election.electionId = :electionId AND u.voteAmount = :voteAmount AND u.voted = TRUE")).thenReturn(query); + + // Mock the result of the query + when(query.setParameter(ELECTION_ID, 100L)).thenReturn(query); + when(query.setParameter(VOTE_AMOUNT, 3)).thenReturn(query); + when(query.getSingleResult()).thenReturn(5L); + + Long result = userVoterDao.getElectionUserVotersAmountByVoteAmountAndVoted(100L, 3); + + // Assert that the result is correct + assertEquals(Long.valueOf(5), result); + + // Verify that the query was executed correctly + verify(query).setParameter(ELECTION_ID, 100L); + verify(query).setParameter(VOTE_AMOUNT, 3); + verify(query).getSingleResult(); + } + + public void testDeleteElectionCensus() { + // Mock the Query object + Query query = mock(Query.class); + when(em.createQuery("DELETE FROM UserVoter u WHERE u.election.electionId = :electionId")).thenReturn(query); + + userVoterDao.deleteElectionCensus(100L); + + // Verify that the query was executed + verify(query).setParameter(ELECTION_ID, 100L); + verify(query).executeUpdate(); + } + + public void testGetElectionUserVoterByOrganization() { + // Mock the TypedQuery object + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT u FROM UserVoter u WHERE u.orgID = :orgID AND u.election.electionId = :electionId", UserVoter.class)).thenReturn(query); + + // Mock the result of the query + when(query.setParameter(ELECTION_ID, 100L)).thenReturn(query); + when(query.setParameter(ORG_ID, "ORG123")).thenReturn(query); + + UserVoter userVoter = new UserVoter(); + when(query.getResultList()).thenReturn(Collections.singletonList(userVoter)); + + UserVoter result = userVoterDao.getElectionUserVoterByOrganization("ORG123", 100L); + + // Assert that the result is correct + assertNotNull(result); + + // Verify that the query was executed correctly + verify(query).setParameter(ELECTION_ID, 100L); + verify(query).setParameter(ORG_ID, "ORG123"); + verify(query).getResultList(); + } + + public void testGetUserVotersAllIdAndName() { + // Mock the Query object + Query query = mock(Query.class); + when(em.createQuery("SELECT u.userVoterId, u.name FROM UserVoter u ORDER BY u.userVoterId")).thenReturn(query); + + // Mock the result of the query + Object[] userVoter1 = new Object[] { 1L, "John Doe" }; + Object[] userVoter2 = new Object[] { 2L, "Jane Doe" }; + when(query.getResultList()).thenReturn(Arrays.asList(userVoter1, userVoter2)); + + List result = userVoterDao.getUserVotersAllIdAndName(10, 0); + + // Assert that the result is correct + assertEquals(2, result.size()); + assertEquals(1L, result.get(0)[0]); + assertEquals("John Doe", result.get(0)[1]); + + // Verify that the query was executed correctly + verify(query).getResultList(); + } + + public void testGetUserVotersByOrganization() { + // Mock the TypedQuery object + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT u FROM UserVoter u WHERE u.orgID = :orgID ORDER BY u.orgID, u.userVoterId", UserVoter.class)).thenReturn(query); + + // Mock the result of the query + when(query.setParameter(ORG_ID, "ORG123")).thenReturn(query); + + UserVoter userVoter1 = new UserVoter(); + UserVoter userVoter2 = new UserVoter(); + when(query.getResultList()).thenReturn(Arrays.asList(userVoter1, userVoter2)); + + List result = userVoterDao.getUserVotersByOrganization("ORG123", 10, 0); + + assertEquals(2, result.size()); + + // Verify that the query was executed correctly + verify(query).setParameter(ORG_ID, "ORG123"); + verify(query).getResultList(); + } + + public void testGetElectionUserVoterByOrganization_NoUsersFound() { + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT u FROM UserVoter u WHERE u.orgID = :orgID AND u.election.electionId = :electionId", UserVoter.class)).thenReturn(query); + + // Mock the result of the query to return an empty list (no users found) + when(query.setParameter(ELECTION_ID, 100L)).thenReturn(query); + when(query.setParameter(ORG_ID, "ORG123")).thenReturn(query); + when(query.getResultList()).thenReturn(Collections.emptyList()); + + UserVoter result = userVoterDao.getElectionUserVoterByOrganization("ORG123", 100L); + + // Assert that the result is null since no users were found + assertNull(result); + + // Verify that the query was executed correctly + verify(query).setParameter(ELECTION_ID, 100L); + verify(query).setParameter(ORG_ID, "ORG123"); + verify(query).getResultList(); + } + +} \ No newline at end of file diff --git a/elections-ejb/src/test/java/net/lacnic/elections/dao/VoteDaoTest.java b/elections-ejb/src/test/java/net/lacnic/elections/dao/VoteDaoTest.java new file mode 100644 index 0000000..1a990c3 --- /dev/null +++ b/elections-ejb/src/test/java/net/lacnic/elections/dao/VoteDaoTest.java @@ -0,0 +1,177 @@ +package net.lacnic.elections.dao; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; +import net.lacnic.elections.domain.Vote; + +import javax.persistence.EntityManager; +import javax.persistence.Query; +import javax.persistence.TypedQuery; +import java.util.Arrays; +import java.util.List; + +import static net.lacnic.elections.utils.Constants.*; +import static org.mockito.Mockito.*; + +public class VoteDaoTest extends TestCase { + + private EntityManager em; + private VoteDao voteDao; + + /** + * @return the suite of tests being tested + */ + public static Test suite() { + return new TestSuite(VoteDaoTest.class); + } + + @Override + public void setUp() { + em = mock(EntityManager.class); + voteDao = new VoteDao(em); + } + + public void testGetElectionVotes() { + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT v FROM Vote v WHERE v.election.electionId = :electionId ORDER BY v.voteId DESC", Vote.class)) + .thenReturn(query); + + Vote vote1 = new Vote(); + Vote vote2 = new Vote(); + when(query.setParameter(ELECTION_ID, 100L)).thenReturn(query); + when(query.getResultList()).thenReturn(Arrays.asList(vote1, vote2)); + + List result = voteDao.getElectionVotes(100L); + assertEquals(2, result.size()); + verify(query).getResultList(); + } + + public void testGetElectionUserVoterVotes() { + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT v FROM Vote v WHERE v.userVoter.userVoterId = :userVoterId AND v.election.electionId = :electionId", Vote.class)) + .thenReturn(query); + + Vote vote1 = new Vote(); + Vote vote2 = new Vote(); + when(query.setParameter("userVoterId", 1L)).thenReturn(query); + when(query.setParameter(ELECTION_ID, 100L)).thenReturn(query); + when(query.getResultList()).thenReturn(Arrays.asList(vote1, vote2)); + + List result = voteDao.getElectionUserVoterVotes(1L, 100L); + assertEquals(2, result.size()); + verify(query).getResultList(); + } + + public void testGetElectionVotesAmount() { + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT v FROM Vote v WHERE v.election.electionId = :electionId ORDER BY v.voteId DESC", Vote.class)) + .thenReturn(query); + + Vote vote1 = new Vote(); + Vote vote2 = new Vote(); + when(query.setParameter(ELECTION_ID, 100L)).thenReturn(query); + when(query.getResultList()).thenReturn(Arrays.asList(vote1, vote2)); + + long result = voteDao.getElectionVotesAmount(100L); + + assertEquals(2, result); + verify(query).setParameter(ELECTION_ID, 100L); + verify(query).getResultList(); + } + + + public void testGetElectionVotesCandidateAndCode() { + Query query = mock(Query.class); + when(em.createQuery("SELECT v.candidate.name, v.code FROM Vote v WHERE v.election.electionId = :electionId ORDER BY v.code")) + .thenReturn(query); + + Object[] voteData1 = new Object[] { "Candidate1", "Code1" }; + Object[] voteData2 = new Object[] { "Candidate2", "Code2" }; + when(query.setParameter(ELECTION_ID, 100L)).thenReturn(query); + when(query.getResultList()).thenReturn(Arrays.asList(voteData1, voteData2)); + + List result = voteDao.getElectionVotesCandidateAndCode(100L); + assertEquals(2, result.size()); + verify(query).getResultList(); + } + + public void testDeleteElectionVotes() { + Query query = mock(Query.class); + when(em.createQuery("DELETE FROM Vote v WHERE v.election.electionId = :electionId")).thenReturn(query); + + voteDao.deleteElectionVotes(100L); + + verify(query).setParameter(ELECTION_ID, 100L); + verify(query).executeUpdate(); + } + + public void testGetCandidateVotes() { + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT v FROM Vote v WHERE v.candidate.candidateId = :candidateId ORDER BY v.voteId DESC", Vote.class)) + .thenReturn(query); + + Vote vote1 = new Vote(); + Vote vote2 = new Vote(); + when(query.setParameter(CANDIDATE_ID, 1L)).thenReturn(query); + when(query.getResultList()).thenReturn(Arrays.asList(vote1, vote2)); + + List result = voteDao.getCandidateVotes(1L); + assertEquals(2, result.size()); + verify(query).setParameter(CANDIDATE_ID, 1L); + verify(query).getResultList(); + } + + public void testGetElectionVotesCandidateForUserVoter() { + Query query = mock(Query.class); + when(em.createQuery("SELECT v.candidate.name, v.code, v.candidate.pictureInfo FROM Vote v WHERE v.userVoter.userVoterId = :userVoterId AND v.election.electionId = :electionId")) + .thenReturn(query); + + Object[] result1 = new Object[] {"Candidate 1", "Code1", "Picture1"}; + Object[] result2 = new Object[] {"Candidate 2", "Code2", "Picture2"}; + when(query.setParameter("userVoterId", 1L)).thenReturn(query); + when(query.setParameter(ELECTION_ID, 100L)).thenReturn(query); + when(query.getResultList()).thenReturn(Arrays.asList(result1, result2)); + + List result = voteDao.getElectionVotesCandidateForUserVoter(1L, 100L); + assertEquals(2, result.size()); + verify(query).setParameter("userVoterId", 1L); + verify(query).setParameter(ELECTION_ID, 100L); + verify(query).getResultList(); + } + + public void testGetVotesAllIdAndDate() { + Query query = mock(Query.class); + when(em.createQuery("SELECT v.voteId, v.candidate.candidateId FROM Vote v ORDER BY v.voteId")) + .thenReturn(query); + + Object[] result1 = new Object[] {1L, 10L}; + Object[] result2 = new Object[] {2L, 20L}; + when(query.setMaxResults(10)).thenReturn(query); + when(query.setFirstResult(0)).thenReturn(query); + when(query.getResultList()).thenReturn(Arrays.asList(result1, result2)); + + List result = voteDao.getVotesAllIdAndDate(10, 0); + assertEquals(2, result.size()); + verify(query).setMaxResults(10); + verify(query).setFirstResult(0); + verify(query).getResultList(); + } + + public void testGetVote() { + TypedQuery query = mock(TypedQuery.class); + when(em.createQuery("SELECT v FROM Vote v WHERE v.voteId = :voteId", Vote.class)) + .thenReturn(query); + + Vote vote = new Vote(); + when(query.setParameter(VOTE_ID, 1L)).thenReturn(query); + when(query.getSingleResult()).thenReturn(vote); + + Vote result = voteDao.getVote(1L); + assertNotNull(result); + verify(query).setParameter(VOTE_ID, 1L); + verify(query).getSingleResult(); + } + +} + diff --git a/elections-ejb/src/test/java/net/lacnic/elections/domain/ElectionLightTest.java b/elections-ejb/src/test/java/net/lacnic/elections/domain/ElectionLightTest.java index ee037dd..f81786b 100644 --- a/elections-ejb/src/test/java/net/lacnic/elections/domain/ElectionLightTest.java +++ b/elections-ejb/src/test/java/net/lacnic/elections/domain/ElectionLightTest.java @@ -21,11 +21,6 @@ public class ElectionLightTest extends TestCase { private ElectionLight election; - @Override - public void setUp() { - election = new ElectionLight(); - } - /** * @return the suite of tests being tested */ @@ -33,6 +28,11 @@ public static Test suite() { return new TestSuite(ElectionLightTest.class); } + @Override + public void setUp() { + election = new ElectionLight(); + } + public void testElectionLightAnnotations() { AssertAnnotations.assertType(ElectionLight.class, Entity.class, Table.class); @@ -154,7 +154,6 @@ public void testElectionLight() { } } - // Assertions to verify initialization assertEquals(electionId, election.getElectionId()); assertEquals(migrated, election.isMigrated()); assertEquals(startDate, election.getStartDate()); @@ -187,9 +186,9 @@ public void testElectionLight() { assertEquals(electionId, election.getElectionId()); assertEquals(migrated, election.isMigrated()); assertEquals(category, election.getCategory()); - assertFalse(election.isFinished()); + assertTrue(election.isFinished()); assertTrue(election.isStarted()); - assertTrue(election.isEnabledToVote()); + assertFalse(election.isEnabledToVote()); assertEquals(resultLink, election.getResultLink()); } @@ -388,6 +387,19 @@ public void testCopyLanguageURLs() { assertEquals(linkSpanish, election.getLinkPortuguese()); } + public void testGetStartDateString() { + Date startDate = new Date(); + int diffUTC = 3; // Ex. different timezone + election.setStartDate(startDate); + election.setDiffUTC(diffUTC); + + DateTime expectedDateTime = new DateTime(startDate).plusHours(diffUTC); + String expectedStartDateString = expectedDateTime.toString("dd/MM/yyyy HH:mm") + " (UTC)"; + + String result = election.getStartDateString(); + assertEquals(expectedStartDateString, result); + } + public void testIsEnabledToVote_DateBetween_StartDate_EndDate() { // Scenario: Current date is between startDate and endDate election = new ElectionLight(); @@ -438,4 +450,36 @@ public void testIsEnabledToVote_AllConditionsFalse() { assertFalse(election.isEnabledToVote()); } + public void testGetEndDateString() { + Date endDate = new Date(); + int diffUTC = 3; + election.setEndDate(endDate); + election.setDiffUTC(diffUTC); + + DateTime expectedDateTime = new DateTime(endDate).plusHours(diffUTC); + String expectedEndDateString = expectedDateTime.toString("dd/MM/yyyy HH:mm") + " (UTC)"; + + String result = election.getEndDateString(); + assertEquals(expectedEndDateString, result); + } + + public void testGetClosedDateString_withClosedDate() { + Date closedDate = new Date(); + int diffUTC = 3; + election.setClosedDate(closedDate); + election.setDiffUTC(diffUTC); + + DateTime expectedDateTime = new DateTime(closedDate).plusHours(diffUTC); + String expectedClosedDateString = expectedDateTime.toString("dd/MM/yyyy HH:mm") + " (UTC)"; + + String result = election.getClosedDateString(); + assertEquals(expectedClosedDateString, result); + } + + public void testGetClosedDateString_withNullClosedDate() { + election.setClosedDate(null); + String result = election.getClosedDateString(); + assertEquals("", result); // If the close date is null, it should return an empty chain + } + } diff --git a/elections-ejb/src/test/java/net/lacnic/elections/domain/ElectionTest.java b/elections-ejb/src/test/java/net/lacnic/elections/domain/ElectionTest.java index f8dc09d..c993ca6 100644 --- a/elections-ejb/src/test/java/net/lacnic/elections/domain/ElectionTest.java +++ b/elections-ejb/src/test/java/net/lacnic/elections/domain/ElectionTest.java @@ -6,6 +6,9 @@ import static net.lacnic.elections.utils.ObjectsFactory.createUserVoters; import static net.lacnic.elections.utils.ObjectsFactory.createVotesList; +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.Date; import java.util.List; @@ -32,12 +35,8 @@ public class ElectionTest extends TestCase { private Election election; - - @Override - public void setUp() { - long electionId = 76447L; - election = ObjectsFactory.createElection(electionId); - } + private static final String ELECTION_DATE_TIME_FORMAT = "dd/MM/yyyy HH:mm"; + private static final String UTC = " (UTC)"; /** * @return the suite of tests being tested @@ -46,6 +45,12 @@ public static Test suite() { return new TestSuite(ElectionTest.class); } + @Override + public void setUp() { + long electionId = 76447L; + election = ObjectsFactory.createElection(electionId); + } + public void testElectionAnnotations() { AssertAnnotations.assertType(Election.class, Entity.class); @@ -215,7 +220,6 @@ public void testElection() { assertNotNull(election); - // Assertions to verify initialization assertEquals(electionId, election.getElectionId()); assertEquals(migrationId, election.getMigrationId()); assertEquals(startDate, election.getStartDate()); @@ -240,9 +244,9 @@ public void testElection() { assertEquals(auxEndHour, election.getAuxEndHour()); assertEquals(electionId, election.getElectionId()); assertEquals(category, election.getCategory()); - assertFalse(election.isFinished()); + assertTrue(election.isFinished()); assertTrue(election.isStarted()); - assertTrue(election.isEnabledToVote()); + assertFalse(election.isEnabledToVote()); assertEquals(1, election.getCandidates().size()); assertEquals(1, election.getUserVoters().size()); assertEquals(1, election.getAuditors().size()); @@ -287,7 +291,6 @@ public void testElectionDefaultConstructor() { assertNotNull(election); - // Assertions to verify initialization assertEquals(electionId, election.getElectionId()); assertEquals(migrated, election.isMigrated()); assertEquals(creationDate, election.getCreationDate()); @@ -306,6 +309,98 @@ public void testElectionDefaultConstructor() { assertEquals(resultLink, election.getResultLink()); } + public void testInitStringsStartEndDatesWithValidDates() { + election = new Election(); + DateTime startDate = new DateTime(2024, 12, 25, 10, 0); + DateTime endDate = new DateTime(2024, 12, 26, 18, 30); + + election.setStartDate(startDate.toDate()); + election.setEndDate(endDate.toDate()); + election.setDiffUTC(3); + + election.initStringsStartEndDates(); + + assertEquals("25/12/2024", election.getAuxStartDate()); + assertEquals("13:00", election.getAuxStartHour()); + assertEquals("26/12/2024", election.getAuxEndDate()); + assertEquals("21:30", election.getAuxEndHour()); + } + + public void testInitStringsStartEndDatesWithNullStartDate() { + election = new Election(); + DateTime endDate = new DateTime(2024, 12, 26, 18, 30); + + election.setAuxStartDate(null); + election.setAuxStartHour(null); + election.setAuxEndHour("21:30"); + election.setStartDate(null); + election.setAuxEndDate("26/12/2024"); + election.setEndDate(endDate.toDate()); + election.setDiffUTC(3); + + election.initStringsStartEndDates(); + + assertNull(election.getAuxStartDate()); + assertNull(election.getAuxStartHour()); + assertEquals("26/12/2024", election.getAuxEndDate()); + assertEquals("21:30", election.getAuxEndHour()); + } + + public void testInitStringsStartEndDatesWithNullEndDate() { + election = new Election(); + DateTime startDate = new DateTime(2024, 12, 25, 10, 0); + + election.setStartDate(startDate.toDate()); + election.setAuxStartDate("25/12/2024"); + election.setAuxStartHour("13:00"); + election.setAuxEndDate(null); + election.setEndDate(null); + election.setAuxEndHour(null); + election.setDiffUTC(3); + + election.initStringsStartEndDates(); + + assertEquals("25/12/2024", election.getAuxStartDate()); + assertEquals("13:00", election.getAuxStartHour()); + assertNull(election.getAuxEndDate()); + assertNull(election.getAuxEndHour()); + } + + public void testInitStringsStartEndDatesWithNullDates() { + election = new Election(); + election.setAuxStartDate(null); + election.setAuxStartHour(null); + election.setStartDate(null); + election.setEndDate(null); + election.setAuxEndDate(null); + election.setAuxEndHour(null); + election.setDiffUTC(3); + + election.initStringsStartEndDates(); + + assertNull(election.getAuxStartDate()); + assertNull(election.getAuxStartHour()); + assertNull(election.getAuxEndDate()); + assertNull(election.getAuxEndHour()); + } + + public void testInitStringsStartEndDatesWithZeroDiffUTC() { + election = new Election(); + DateTime startDate = new DateTime(2024, 12, 25, 10, 0); + DateTime endDate = new DateTime(2024, 12, 26, 18, 30); + + election.setStartDate(startDate.toDate()); + election.setEndDate(endDate.toDate()); + election.setDiffUTC(0); + + election.initStringsStartEndDates(); + + assertEquals("25/12/2024", election.getAuxStartDate()); + assertEquals("10:00", election.getAuxStartHour()); + assertEquals("26/12/2024", election.getAuxEndDate()); + assertEquals("18:30", election.getAuxEndHour()); + } + public void testCopyLanguageDescriptions() { election = new Election(12345L); @@ -595,4 +690,253 @@ public void testGetTitles() { assertEquals(titleSpanish, election.getTitle("other")); } + public void testInitDatesStartEndDates_ValidValues() { + Date expectedStartDate = new DateTime("2024-12-25T10:00:00").minusHours(3).toDate(); + Date expectedEndDate = new DateTime("2024-12-31T18:00:00").minusHours(3).toDate(); + + election = new Election(); + election.setStartDate(expectedStartDate); + election.setEndDate(expectedEndDate); + election.setAuxStartDate("25/12/2024"); + election.setAuxEndDate("31/12/2024"); + election.setAuxStartHour("10:00"); + election.setAuxEndHour("18:00"); + election.setDiffUTC(3); + + election.initDatesStartEndDates(); + + assertEquals(expectedStartDate, election.getStartDate()); + assertEquals(expectedEndDate, election.getEndDate()); + } + + public void testInitDatesStartEndDates_MissingValues() { + election = new Election(); + election.setAuxStartDate(""); + election.setAuxEndDate("2024-12-31"); + election.setAuxStartHour("10:00"); + election.setAuxEndHour(""); + + election.initDatesStartEndDates(); + + assertNull(election.getStartDate()); + assertNull(election.getEndDate()); + } + + public void testInitDatesStartEndDates_NegativeDiffUTC() { + Date expectedStartDate = new DateTime("2024-12-25T10:00:00").minusHours(-3).toDate(); + Date expectedEndDate = new DateTime("2024-12-31T18:00:00").minusHours(-3).toDate(); + + election = new Election(); + election.setStartDate(expectedStartDate); + election.setEndDate(expectedEndDate); + election.setAuxStartDate("2024-12-25"); + election.setAuxEndDate("2024-12-31"); + election.setAuxStartHour("10:00"); + election.setAuxEndHour("18:00"); + election.setDiffUTC(-3); + + election.initDatesStartEndDates(); + + assertEquals(expectedStartDate, election.getStartDate()); + assertEquals(expectedEndDate, election.getEndDate()); + } + + public void testInitDatesStartEndDates_EdgeDates() { + Date expectedStartDate = new DateTime("2024-12-31T23:59:00").minusHours(2).toDate(); + Date expectedEndDate = new DateTime("2025-01-01T00:01:00").minusHours(2).toDate(); + + election = new Election(); + election.setStartDate(expectedStartDate); + election.setEndDate(expectedEndDate); + election.setAuxStartDate("2024-12-31"); + election.setAuxEndDate("2025-01-01"); + election.setAuxStartHour("23:59"); + election.setAuxEndHour("00:01"); + election.setDiffUTC(2); + + election.initDatesStartEndDates(); + + assertEquals(expectedStartDate, election.getStartDate()); + assertEquals(expectedEndDate, election.getEndDate()); + } + + public void testInitDatesStartEndDates_Success() { + Date startDate = new DateTime(2024, 12, 29, 16, 52).toDate(); + Date endDate = new DateTime(2025, 1, 2, 15, 0).toDate(); + + election = new Election(); + election.setStartDate(startDate); + election.setEndDate(endDate); + election.setAuxStartDate("2025-01-01"); + election.setAuxEndDate("2025-01-02"); + election.setAuxStartHour("09:00"); + election.setAuxEndHour("18:00"); + election.setDiffUTC(3); + + election.initDatesStartEndDates(); + + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"); + + LocalDateTime formattedStartDate = election.getStartDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime(); + + LocalDateTime formattedEndDate = election.getEndDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime(); + + assertEquals("2024-12-29 16:52", formattedStartDate.format(formatter)); + assertEquals("2025-01-02 15:00", formattedEndDate.format(formatter)); + } + + public void testInitDatesStartEndDates_MissingAuxStartDate() { + election = new Election(); + election.setAuxStartDate(""); + election.setAuxEndDate("2025-01-02"); + election.setAuxStartHour("09:00"); + election.setAuxEndHour("18:00"); + election.setDiffUTC(3); + + election.initDatesStartEndDates(); + + assertNull(election.getStartDate()); + assertNull(election.getEndDate()); + } + + public void testInitDatesStartEndDates_InvalidFormat() { + election = new Election(); + election.setAuxStartDate("25/05/2005"); + election.setAuxEndDate("2025-01-02"); + election.setAuxStartHour("09:00"); + election.setAuxEndHour("18:00"); + election.setDiffUTC(3); + + election.initDatesStartEndDates(); + + assertNull(election.getStartDate()); + assertNull(election.getEndDate()); + } + + public void testInitDatesStartEndDates_MissingAuxStartHour() { + election = new Election(); + election.setAuxStartDate("2025-01-01"); + election.setAuxEndDate("2025-01-02"); + election.setAuxStartHour(""); + election.setAuxEndHour("18:00"); + election.setDiffUTC(3); + + election.initDatesStartEndDates(); + + assertNull(election.getStartDate()); + assertNull(election.getEndDate()); + } + + public void testInitDatesStartEndDates_EmptyAuxEndDate() { + election = new Election(); + election.setAuxEndDate(""); + election.setAuxStartDate("2025-01-01"); + election.setAuxStartHour("09:00"); + election.setAuxEndHour("18:00"); + election.setDiffUTC(3); + + election.initDatesStartEndDates(); + + assertNull(election.getStartDate()); + assertNull(election.getEndDate()); + } + + public void testInitDatesStartEndDates_EmptyAuxStartDate() { + election = new Election(); + election.setAuxEndDate("2025-01-02"); + election.setAuxStartDate(""); + election.setAuxStartHour("09:00"); + election.setAuxEndHour("18:00"); + election.setDiffUTC(3); + + election.initDatesStartEndDates(); + + assertNull(election.getStartDate()); + assertNull(election.getEndDate()); + } + + public void testInitDatesStartEndDates_EmptyAuxStartHour() { + election = new Election(); + election.setAuxEndDate("2025-01-02"); + election.setAuxStartDate("2025-01-01"); + election.setAuxStartHour(""); + election.setAuxEndHour("18:00"); + election.setDiffUTC(3); + + election.initDatesStartEndDates(); + + assertNull(election.getStartDate()); + assertNull(election.getEndDate()); + } + + public void testInitDatesStartEndDates_EmptyAuxEndHour() { + election = new Election(); + election.setAuxEndDate("2025-01-02"); + election.setAuxStartDate("2025-01-01"); + election.setAuxStartHour("09:00"); + election.setAuxEndHour(""); + election.setDiffUTC(3); + + election.initDatesStartEndDates(); + + assertNull(election.getStartDate()); + assertNull(election.getEndDate()); + } + + public void testGetClosedDateString_withClosedDate() { + Date closedDate = new Date(); + int diffUTC = 3; + election.setClosedDate(closedDate); + election.setDiffUTC(diffUTC); + + DateTime expectedDateTime = new DateTime(closedDate).plusHours(diffUTC); + String expectedClosedDateString = expectedDateTime.toString("dd/MM/yyyy HH:mm") + " (UTC)"; + + String result = election.getClosedDateString(); + assertEquals(expectedClosedDateString, result); + } + + public void testGetClosedDateString_withNullClosedDate() { + election.setClosedDate(null); + + String result = election.getClosedDateString(); + assertEquals("", result); // If the close date is null, it should return an empty chain + } + + public void testGetStartDateString_Success() { + election = new Election(); + election.setStartDate(new DateTime(2024, 12, 31, 18, 0).toDate()); // 6:00 PM + election.setDiffUTC(3); // UTC+3 + + LocalDateTime expectedDateTime = LocalDateTime.of(2024, 12, 31, 21, 0); // 6:00 PM + 3 hours = 9:00 PM + DateTimeFormatter formatter = DateTimeFormatter.ofPattern(ELECTION_DATE_TIME_FORMAT); + String expected = expectedDateTime.format(formatter) + UTC; + + assertEquals(expected, election.getStartDateString()); + } + + public void testGetEndDateString_Success() { + election = new Election(); + election.setEndDate(new DateTime(2025, 1, 1, 15, 30).toDate()); // 3:30 PM + election.setDiffUTC(2); // UTC+2 + + LocalDateTime expectedDateTime = LocalDateTime.of(2025, 1, 1, 17, 30); // 3:30 PM + 2 hours = 5:30 PM + DateTimeFormatter formatter = DateTimeFormatter.ofPattern(ELECTION_DATE_TIME_FORMAT); + String expected = expectedDateTime.format(formatter) + UTC; + + assertEquals(expected, election.getEndDateString()); + } + + public void testGetCreationDateString_Success() { + election = new Election(); + election.setCreationDate(new DateTime(2024, 12, 25, 8, 45).toDate()); // 8:45 AM + election.setDiffUTC(-1); // UTC-1 + + LocalDateTime expectedDateTime = LocalDateTime.of(2024, 12, 25, 7, 45); // 8:45 AM - 1 hour = 7:45 AM + DateTimeFormatter formatter = DateTimeFormatter.ofPattern(ELECTION_DATE_TIME_FORMAT); + String expected = expectedDateTime.format(formatter) + UTC; + + assertEquals(expected, election.getCreationDateString()); + } + } diff --git a/elections-ejb/src/test/java/net/lacnic/elections/domain/services/dbtables/ActivityTableReportTest.java b/elections-ejb/src/test/java/net/lacnic/elections/domain/services/dbtables/ActivityTableReportTest.java new file mode 100644 index 0000000..1555391 --- /dev/null +++ b/elections-ejb/src/test/java/net/lacnic/elections/domain/services/dbtables/ActivityTableReportTest.java @@ -0,0 +1,61 @@ +package net.lacnic.elections.domain.services.dbtables; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; +import net.lacnic.elections.domain.Activity; +import net.lacnic.elections.utils.DateTimeUtils; +import net.lacnic.elections.utils.ObjectsFactory; + +public class ActivityTableReportTest extends TestCase { + + public static Test suite() { + return new TestSuite(ActivityTableReportTest.class); + } + + public void testActivityTableReport() { + Activity activity = ObjectsFactory.createActivity(); + String timestampString = DateTimeUtils.getTableServicesDateTimeString(activity.getTimestamp()); + + ActivityTableReport report = new ActivityTableReport(activity); + assertNotNull(activity); + + assertEquals(activity.getActivityId(), report.getActivityId().longValue()); + assertEquals(activity.getDescription(), report.getDescription()); + assertEquals(activity.getUserName(), report.getUserName()); + assertEquals(timestampString, report.getTimestamp()); + assertEquals(activity.getActivityType().toString(), report.getActivityType()); + assertEquals(activity.getElectionId(), report.getElectionId()); + assertEquals(activity.getIp(), report.getIp()); + } + + public void testActivityTableReportEmptyConstructor() { + Long activityId = 2L; + String description = "User auditor activity"; + String userName = "testUser2"; + String timestamp = "2024-12-31 12:00:00"; + String activityType = "ADD_AUDITOR"; + Long electionId = 456L; + String ip = "192.168.0.2"; + + ActivityTableReport activityTableReport = new ActivityTableReport(); + assertNotNull(activityTableReport); + + activityTableReport.setActivityId(activityId); + activityTableReport.setDescription(description); + activityTableReport.setUserName(userName); + activityTableReport.setTimestamp(timestamp); + activityTableReport.setActivityType(activityType); + activityTableReport.setElectionId(electionId); + activityTableReport.setIp(ip); + + assertEquals(activityId, activityTableReport.getActivityId()); + assertEquals(description, activityTableReport.getDescription()); + assertEquals(userName, activityTableReport.getUserName()); + assertEquals(timestamp, activityTableReport.getTimestamp()); + assertEquals(activityType, activityTableReport.getActivityType()); + assertEquals(electionId, activityTableReport.getElectionId()); + assertEquals(ip, activityTableReport.getIp()); + } + +} diff --git a/elections-ejb/src/test/java/net/lacnic/elections/domain/services/dbtables/AuditorTableReportTest.java b/elections-ejb/src/test/java/net/lacnic/elections/domain/services/dbtables/AuditorTableReportTest.java new file mode 100644 index 0000000..e044f0d --- /dev/null +++ b/elections-ejb/src/test/java/net/lacnic/elections/domain/services/dbtables/AuditorTableReportTest.java @@ -0,0 +1,59 @@ +package net.lacnic.elections.domain.services.dbtables; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; +import net.lacnic.elections.domain.Auditor; +import net.lacnic.elections.utils.ObjectsFactory; + +public class AuditorTableReportTest extends TestCase { + + public static Test suite() { + return new TestSuite(AuditorTableReportTest.class); + } + + public void testAuditorTableReport() { + Auditor auditor = ObjectsFactory.createAuditor(); + AuditorTableReport report = new AuditorTableReport(auditor); + + assertNotNull(report); + + assertEquals(auditor.getAuditorId(), report.getAuditorId().longValue()); + assertTrue(report.getCommissioner()); + assertTrue(report.getAgreedConformity()); + assertEquals(auditor.getMail(), report.getMail()); + assertEquals(auditor.getName(), report.getName()); + assertEquals(auditor.getElection().getElectionId(), report.getElectionId().longValue()); + assertTrue(report.getRevisionAvailable()); + assertEquals(auditor.getMigrationId(), report.getMigrationId()); + } + + public void testAuditorTableReportEmptyConstructor() { + + Long auditorId = 1L; + String mail = "auditor1@example.com"; + String name = "Auditor 1"; + Long electionId = 4567L; + Long migrationId = 123L; + + AuditorTableReport report = new AuditorTableReport(); + report.setAuditorId(auditorId); + report.setCommissioner(true); + report.setAgreedConformity(true); + report.setMail(mail); + report.setName(name); + report.setElectionId(4567L); + report.setRevisionAvailable(true); + report.setMigrationId(123L); + + assertEquals(auditorId, report.getAuditorId()); + assertTrue(report.getCommissioner()); + assertTrue(report.getAgreedConformity()); + assertEquals(mail, report.getMail()); + assertEquals(name, report.getName()); + assertEquals(electionId, report.getElectionId()); + assertTrue(report.getRevisionAvailable()); + assertEquals(migrationId, report.getMigrationId()); + } + +} diff --git a/elections-ejb/src/test/java/net/lacnic/elections/domain/services/dbtables/CandidateTableReportTest.java b/elections-ejb/src/test/java/net/lacnic/elections/domain/services/dbtables/CandidateTableReportTest.java new file mode 100644 index 0000000..41947c7 --- /dev/null +++ b/elections-ejb/src/test/java/net/lacnic/elections/domain/services/dbtables/CandidateTableReportTest.java @@ -0,0 +1,95 @@ +package net.lacnic.elections.domain.services.dbtables; + +import java.util.Base64; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; +import net.lacnic.elections.domain.Candidate; +import net.lacnic.elections.utils.ObjectsFactory; + +public class CandidateTableReportTest extends TestCase { + + public static Test suite() { + return new TestSuite(CandidateTableReportTest.class); + } + + public void testCandidateTableReport() { + Candidate candidate = ObjectsFactory.createCandidate(); + CandidateTableReport report = new CandidateTableReport(candidate); + + assertNotNull(report); + + assertEquals(candidate.getCandidateId(), report.getCandidateId().longValue()); + assertEquals(candidate.getMigrationId(), report.getMigrationId()); + assertEquals(candidate.getName(), report.getName()); + assertEquals(candidate.getMail(), report.getMail()); + assertEquals(candidate.getElection().getElectionId(), report.getElectionId().longValue()); + assertEquals(Base64.getEncoder().encodeToString(candidate.getPictureInfo()), report.getPictureInfo()); + assertEquals(candidate.getPictureName(), report.getPictureName()); + assertEquals(candidate.getBioSpanish(), report.getBioSpanish()); + assertEquals(candidate.getBioEnglish(), report.getBioEnglish()); + assertEquals(candidate.getBioPortuguese(), report.getBioPortuguese()); + assertEquals(candidate.getPictureExtension(), report.getPictureExtension()); + assertEquals(candidate.getCandidateOrder(), report.getCandidateOrder().longValue()); + assertFalse(report.isOnlySp()); + assertEquals(candidate.getLinkSpanish(), report.getLinkSpanish()); + assertEquals(candidate.getLinkEnglish(), report.getLinkEnglish()); + assertEquals(candidate.getLinkPortuguese(), report.getLinkPortuguese()); + } + + public void testCandidateTableReportEmptyConstructor() { + + Long candidateId = 1L; + Long migrationId = 123L; + String name = "Juan Perez"; + String mail = "juan.perez@example.com"; + Long electionId = 456L; + String pictureInfo = "pictureInfo"; + String pictureName = "profile.jpg"; + String bioSpanish = "Biografía en español"; + String bioEnglish = "Biography in English"; + String bioPortuguese = "Biografia em português"; + String pictureExtension = "jpg"; + Integer candidateOrder = 1; + String linkSpanish = "linkES"; + String linkEnglish = "linkEN"; + String linkPortuguese = "linkPT"; + + CandidateTableReport report = new CandidateTableReport(); + + report.setCandidateId(candidateId); + report.setMigrationId(migrationId); + report.setName(name); + report.setMail(mail); + report.setElectionId(electionId); + report.setPictureInfo(pictureInfo); + report.setPictureName(pictureName); + report.setBioSpanish(bioSpanish); + report.setBioEnglish(bioEnglish); + report.setBioPortuguese(bioPortuguese); + report.setPictureExtension(pictureExtension); + report.setCandidateOrder(candidateOrder); + report.setOnlySp(true); + report.setLinkSpanish(linkSpanish); + report.setLinkEnglish(linkEnglish); + report.setLinkPortuguese(linkPortuguese); + + assertEquals(candidateId, report.getCandidateId()); + assertEquals(migrationId, report.getMigrationId()); + assertEquals(name, report.getName()); + assertEquals(mail, report.getMail()); + assertEquals(electionId, report.getElectionId()); + assertEquals(pictureInfo, report.getPictureInfo()); + assertEquals(pictureName, report.getPictureName()); + assertEquals(bioSpanish, report.getBioSpanish()); + assertEquals(bioEnglish, report.getBioEnglish()); + assertEquals(bioPortuguese, report.getBioPortuguese()); + assertEquals(pictureExtension, report.getPictureExtension()); + assertEquals(candidateOrder, report.getCandidateOrder()); + assertTrue(report.isOnlySp()); + assertEquals(linkSpanish, report.getLinkSpanish()); + assertEquals(linkEnglish, report.getLinkEnglish()); + assertEquals(linkPortuguese, report.getLinkPortuguese()); + } +} diff --git a/elections-ejb/src/test/java/net/lacnic/elections/domain/services/dbtables/CommissionerTableReportTest.java b/elections-ejb/src/test/java/net/lacnic/elections/domain/services/dbtables/CommissionerTableReportTest.java new file mode 100644 index 0000000..afac9f7 --- /dev/null +++ b/elections-ejb/src/test/java/net/lacnic/elections/domain/services/dbtables/CommissionerTableReportTest.java @@ -0,0 +1,41 @@ +package net.lacnic.elections.domain.services.dbtables; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; +import net.lacnic.elections.domain.Commissioner; +import net.lacnic.elections.utils.ObjectsFactory; + +public class CommissionerTableReportTest extends TestCase { + + public static Test suite() { + return new TestSuite(CommissionerTableReportTest.class); + } + + public void testCommissionerTableReport() { + Commissioner commissioner = ObjectsFactory.createCommissioner(); + CommissionerTableReport report = new CommissionerTableReport(commissioner); + + assertNotNull(report); + + assertEquals(commissioner.getCommissionerId(), report.getCommissionerId().longValue()); + assertEquals(commissioner.getName(), report.getName()); + assertEquals(commissioner.getMail(), report.getMail()); + } + + public void testCommissionerTableReportEmptyConstructor() { + Long commissionerId = 1L; + String name = "Juan Perez"; + String mail = "juan1@example.com"; + + CommissionerTableReport report = new CommissionerTableReport(); + + report.setCommissionerId(commissionerId); + report.setName(name); + report.setMail(mail); + + assertEquals(commissionerId, report.getCommissionerId()); + assertEquals(name, report.getName()); + assertEquals(mail, report.getMail()); + } +} diff --git a/elections-ejb/src/test/java/net/lacnic/elections/domain/services/dbtables/CustomizationTableReportTest.java b/elections-ejb/src/test/java/net/lacnic/elections/domain/services/dbtables/CustomizationTableReportTest.java new file mode 100644 index 0000000..3ffa4f8 --- /dev/null +++ b/elections-ejb/src/test/java/net/lacnic/elections/domain/services/dbtables/CustomizationTableReportTest.java @@ -0,0 +1,90 @@ +package net.lacnic.elections.domain.services.dbtables; + +import java.util.Base64; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; +import net.lacnic.elections.domain.Customization; +import net.lacnic.elections.utils.ObjectsFactory; + +public class CustomizationTableReportTest extends TestCase { + + public static Test suite() { + return new TestSuite(CustomizationTableReportTest.class); + } + + public void testCustomizationTableReport() { + Customization customization = ObjectsFactory.createCustomization(); + CustomizationTableReport report = new CustomizationTableReport(customization); + + assertNotNull(report); + + assertEquals(customization.getCustomizationId(), report.getCustomizationId().longValue()); + assertEquals(customization.getPicSmallLogo(), report.getPicSmallLogo()); + assertEquals(customization.getPicBigLogo(), report.getPicBigLogo()); + assertEquals(customization.getPicSymbol(), report.getPicSymbol()); + assertEquals(Base64.getEncoder().encodeToString(customization.getContPicSmallLogo()), report.getContPicSmallLogo()); + assertEquals(Base64.getEncoder().encodeToString(customization.getContPicBigLogo()), report.getContPicBigLogo()); + assertEquals(Base64.getEncoder().encodeToString(customization.getContPicSymbol()), report.getContPicSymbol()); + + assertEquals(customization.getContPicSmallLogo() != null ? Base64.getEncoder().encodeToString(customization.getContPicSmallLogo()) : "", report.getContPicSmallLogo()); + assertEquals(customization.getContPicBigLogo() != null ? Base64.getEncoder().encodeToString(customization.getContPicBigLogo()) : "", report.getContPicBigLogo()); + assertEquals(customization.getContPicSymbol() != null ? Base64.getEncoder().encodeToString(customization.getContPicSymbol()) : "", report.getContPicSymbol()); + + // prueba para estos valores en null + customization.setContPicSmallLogo(null); + customization.setContPicBigLogo(null); + customization.setContPicSymbol(null); + + report = new CustomizationTableReport(customization); + + assertEquals(customization.getContPicSmallLogo() != null ? Base64.getEncoder().encodeToString(customization.getContPicSmallLogo()) : "", report.getContPicSmallLogo()); + assertEquals(customization.getContPicBigLogo() != null ? Base64.getEncoder().encodeToString(customization.getContPicBigLogo()) : "", report.getContPicBigLogo()); + assertEquals(customization.getContPicSymbol() != null ? Base64.getEncoder().encodeToString(customization.getContPicSymbol()) : "", report.getContPicSymbol()); + + assertEquals(customization.getSiteTitle(), report.getSiteTitle()); + assertEquals(customization.getLoginTitle(), report.getLoginTitle()); + assertTrue(report.isShowHome()); + assertEquals(customization.getHomeHtml(), report.getHomeHtml()); + } + + public void testCustomizationTableReportEmptyConstructor() { + Long customizationId = 1L; + String picSmallLogo = "smallLogo.png"; + String picBigLogo = "bigLogo.png"; + String picSymbol = "symbol.png"; + String contPicSmallLogo = "smallLogoContent"; + String contPicBigLogo = "bigLogoContent"; + String contPicSymbol = "symbolContent"; + String siteTitle = "My Site"; + String loginTitle = "Login to My Site"; + String homeHtml = "Home content"; + + CustomizationTableReport report = new CustomizationTableReport(); + + report.setCustomizationId(customizationId); + report.setPicSmallLogo(picSmallLogo); + report.setPicBigLogo(picBigLogo); + report.setPicSymbol(picSymbol); + report.setContPicSmallLogo(contPicSmallLogo); + report.setContPicBigLogo(contPicBigLogo); + report.setContPicSymbol(contPicSymbol); + report.setSiteTitle(siteTitle); + report.setLoginTitle(loginTitle); + report.setShowHome(true); + report.setHomeHtml(homeHtml); + + assertEquals(customizationId, report.getCustomizationId()); + assertEquals(picSmallLogo, report.getPicSmallLogo()); + assertEquals(picBigLogo, report.getPicBigLogo()); + assertEquals(picSymbol, report.getPicSymbol()); + assertEquals(contPicSmallLogo, report.getContPicSmallLogo()); + assertEquals(contPicBigLogo, report.getContPicBigLogo()); + assertEquals(contPicSymbol, report.getContPicSymbol()); + assertEquals(siteTitle, report.getSiteTitle()); + assertEquals(loginTitle, report.getLoginTitle()); + assertTrue(report.isShowHome()); + assertEquals(homeHtml, report.getHomeHtml()); + } +} diff --git a/elections-ejb/src/test/java/net/lacnic/elections/domain/services/dbtables/ElectionEmailTemplateTableReportTest.java b/elections-ejb/src/test/java/net/lacnic/elections/domain/services/dbtables/ElectionEmailTemplateTableReportTest.java new file mode 100644 index 0000000..77ccdd2 --- /dev/null +++ b/elections-ejb/src/test/java/net/lacnic/elections/domain/services/dbtables/ElectionEmailTemplateTableReportTest.java @@ -0,0 +1,79 @@ +package net.lacnic.elections.domain.services.dbtables; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; +import net.lacnic.elections.domain.ElectionEmailTemplate; +import net.lacnic.elections.utils.ObjectsFactory; + +public class ElectionEmailTemplateTableReportTest extends TestCase { + + public static Test suite() { + return new TestSuite(ElectionEmailTemplateTableReportTest.class); + } + + public void testElectionEmailTemplateTableReport() { + ElectionEmailTemplate electionEmailTemplate = ObjectsFactory.createElectionEmailTemplate(); + ElectionEmailTemplateTableReport report = new ElectionEmailTemplateTableReport(electionEmailTemplate); + + assertNotNull(report); + + assertEquals(electionEmailTemplate.getElectionEmailTemplateId(), report.getElectionEmailTemplateId()); + assertEquals(electionEmailTemplate.getTemplateType(), report.getTemplateType()); + assertEquals(electionEmailTemplate.getSubjectSP(), report.getSubjectSP()); + assertEquals(electionEmailTemplate.getSubjectPT(), report.getSubjectPT()); + assertEquals(electionEmailTemplate.getSubjectEN(), report.getSubjectEN()); + assertEquals(electionEmailTemplate.getBodySP(), report.getBodySP()); + assertEquals(electionEmailTemplate.getBodyPT(), report.getBodyPT()); + assertEquals(electionEmailTemplate.getBodyEN(), report.getBodyEN()); + assertEquals(electionEmailTemplate.getRecipientType() != null ? electionEmailTemplate.getRecipientType().getDescription() : "", report.getRecipientType()); + assertEquals(electionEmailTemplate.getElection() != null ? electionEmailTemplate.getElection().getElectionId() : 0L, report.getElectionId().longValue()); + + // prueba para estos valores en null + electionEmailTemplate.setRecipientType(null); + electionEmailTemplate.setElection(null); + + report = new ElectionEmailTemplateTableReport(electionEmailTemplate); + + assertEquals(electionEmailTemplate.getRecipientType() != null ? electionEmailTemplate.getRecipientType().getDescription() : "", report.getRecipientType()); + assertEquals(electionEmailTemplate.getElection() != null ? electionEmailTemplate.getElection().getElectionId() : 0L, report.getElectionId().longValue()); + } + + public void testElectionEmailTemplateTableReportEmptyConstructor() { + long electionEmailTemplateId = 1L; + String templateType = "REMINDER"; + String subjectSP = "Recordatorio de elección"; + String subjectPT = "Lembrete de eleição"; + String subjectEN = "Election Reminder"; + String bodySP = "¡No olvides votar!"; + String bodyPT = "Não se esqueça de votar!"; + String bodyEN = "Don't forget to vote!"; + String recipientType = "VOTER"; + Long electionId = 123L; + + ElectionEmailTemplateTableReport report = new ElectionEmailTemplateTableReport(); + + report.setElectionEmailTemplateId(electionEmailTemplateId); + report.setTemplateType(templateType); + report.setSubjectSP(subjectSP); + report.setSubjectPT(subjectPT); + report.setSubjectEN(subjectEN); + report.setBodySP(bodySP); + report.setBodyPT(bodyPT); + report.setBodyEN(bodyEN); + report.setRecipientType(recipientType); + report.setElectionId(electionId); + + assertEquals(electionEmailTemplateId, report.getElectionEmailTemplateId()); + assertEquals(templateType, report.getTemplateType()); + assertEquals(subjectSP, report.getSubjectSP()); + assertEquals(subjectPT, report.getSubjectPT()); + assertEquals(subjectEN, report.getSubjectEN()); + assertEquals(bodySP, report.getBodySP()); + assertEquals(bodyPT, report.getBodyPT()); + assertEquals(bodyEN, report.getBodyEN()); + assertEquals(recipientType, report.getRecipientType()); + assertEquals(electionId, report.getElectionId()); + } + +} diff --git a/elections-ejb/src/test/java/net/lacnic/elections/domain/services/dbtables/ElectionTableReportTest.java b/elections-ejb/src/test/java/net/lacnic/elections/domain/services/dbtables/ElectionTableReportTest.java new file mode 100644 index 0000000..7027070 --- /dev/null +++ b/elections-ejb/src/test/java/net/lacnic/elections/domain/services/dbtables/ElectionTableReportTest.java @@ -0,0 +1,140 @@ +package net.lacnic.elections.domain.services.dbtables; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; +import net.lacnic.elections.domain.Election; +import net.lacnic.elections.utils.ObjectsFactory; + +public class ElectionTableReportTest extends TestCase { + + public static Test suite() { + return new TestSuite(ElectionTableReportTest.class); + } + + public void testElectionTableReport() { + Election election = ObjectsFactory.createElection(123L); + ElectionTableReport report = new ElectionTableReport(election); + + assertNotNull(report); + + assertEquals(election.getElectionId(), report.getElectionId().longValue()); + assertEquals(election.getDescriptionSpanish(), report.getDescriptionSpanish()); + assertEquals(election.getDescriptionEnglish(), report.getDescriptionEnglish()); + assertEquals(election.getDescriptionPortuguese(), report.getDescriptionPortuguese()); + assertEquals(election.getCreationDateString(), report.getCreationDate()); + assertEquals(election.getEndDateString(), report.getEndDate()); + assertEquals(election.getStartDateString(), report.getStartDate()); + assertTrue(report.getResultLinkAvailable()); + assertTrue(report.getVotingLinkAvailable()); + assertEquals(election.getLinkSpanish(), report.getLinkSpanish()); + assertEquals(election.getLinkEnglish(), report.getLinkEnglish()); + assertEquals(election.getLinkPortuguese(), report.getLinkPortuguese()); + assertEquals(election.getMaxCandidates(), report.getMaxCandidates().intValue()); + assertEquals(election.getTitleSpanish(), report.getTitleSpanish()); + assertEquals(election.getTitleEnglish(), report.getTitleEnglish()); + assertEquals(election.getTitlePortuguese(), report.getTitlePortuguese()); + assertFalse(report.getAuditorsSet()); + assertTrue(report.getCandidatesSet()); + assertTrue(report.getElectorsSet()); + assertFalse(report.getAuditorLinkAvailable()); + assertFalse(report.getOnlySp()); + assertEquals(election.getDefaultSender(), report.getDefaultSender()); + assertTrue(report.getRandomOrderCandidates()); + assertEquals(election.getDiffUTC(), report.getDiffUTC().intValue()); + assertTrue(report.getRevisionRequest()); + assertEquals(election.getMigrationId(), report.getMigrationId()); + assertTrue(report.getMigrated()); + assertEquals(election.getCategory().toString(), report.getCategory()); + assertFalse(report.getClosed()); + assertEquals(election.getClosedDateString(), report.getClosedDate()); + } + + public void testElectionTableReportEmptyConstructor() { + + Long electionId = 123L; + String descriptionSpanish = "Descripción en español"; + String descriptionEnglish = "Description in English"; + String descriptionPortuguese = "Descrição em português"; + String creationDate = "01/12/2024 09:00"; + String endDate = "02/12/2024 18:30"; + String startDate = "01/12/2024 10:00"; + String linkSpanish = "http://example.com/es"; + String linkEnglish = "http://example.com/en"; + String linkPortuguese = "http://example.com/pt"; + Integer maxCandidates = 5; + String titleSpanish = "Elección en español"; + String titleEnglish = "Election in English"; + String titlePortuguese = "Eleição em português"; + String defaultSender = "noreply@example.com"; + Integer diffUTC = 3; + Long migrationId = 54321L; + String category = "STATUTORY"; + String closedDate = "01/01/2025"; + + ElectionTableReport report = new ElectionTableReport(); + + report.setElectionId(electionId); + report.setDescriptionSpanish(descriptionSpanish); + report.setDescriptionEnglish(descriptionEnglish); + report.setDescriptionPortuguese(descriptionPortuguese); + report.setCreationDate(creationDate); + report.setEndDate(endDate); + report.setStartDate(startDate); + report.setResultLinkAvailable(true); + report.setVotingLinkAvailable(true); + report.setLinkSpanish(linkSpanish); + report.setLinkEnglish(linkEnglish); + report.setLinkPortuguese(linkPortuguese); + report.setMaxCandidates(maxCandidates); + report.setTitleSpanish(titleSpanish); + report.setTitleEnglish(titleEnglish); + report.setTitlePortuguese(titlePortuguese); + report.setAuditorsSet(true); + report.setCandidatesSet(true); + report.setElectorsSet(true); + report.setAuditorLinkAvailable(true); + report.setOnlySp(false); + report.setDefaultSender(defaultSender); + report.setRandomOrderCandidates(true); + report.setDiffUTC(diffUTC); + report.setRevisionRequest(true); + report.setMigrationId(migrationId); + report.setMigrated(true); + report.setCategory(category); + report.setClosed(false); + report.setClosedDate(closedDate); + + assertEquals(electionId, report.getElectionId()); + assertEquals(descriptionSpanish, report.getDescriptionSpanish()); + assertEquals(descriptionEnglish, report.getDescriptionEnglish()); + assertEquals(descriptionPortuguese, report.getDescriptionPortuguese()); + assertEquals(creationDate, report.getCreationDate()); + assertEquals(endDate, report.getEndDate()); + assertEquals(startDate, report.getStartDate()); + assertTrue(report.getResultLinkAvailable()); + assertTrue(report.getVotingLinkAvailable()); + assertEquals(linkSpanish, report.getLinkSpanish()); + assertEquals(linkEnglish, report.getLinkEnglish()); + assertEquals(linkPortuguese, report.getLinkPortuguese()); + assertEquals(maxCandidates, report.getMaxCandidates()); + assertEquals(titleSpanish, report.getTitleSpanish()); + assertEquals(titleEnglish, report.getTitleEnglish()); + assertEquals(titlePortuguese, report.getTitlePortuguese()); + assertTrue(report.getAuditorsSet()); + assertTrue(report.getCandidatesSet()); + assertTrue(report.getElectorsSet()); + assertTrue(report.getAuditorLinkAvailable()); + assertFalse(report.getOnlySp()); + assertEquals(defaultSender, report.getDefaultSender()); + assertTrue(report.getRandomOrderCandidates()); + assertEquals(diffUTC, report.getDiffUTC()); + assertTrue(report.getRevisionRequest()); + assertEquals(migrationId, report.getMigrationId()); + assertTrue(report.getMigrated()); + assertEquals(category, report.getCategory()); + assertFalse(report.getClosed()); + assertEquals(closedDate, report.getClosedDate()); + } + +} diff --git a/elections-ejb/src/test/java/net/lacnic/elections/domain/services/dbtables/EmailTableReportTest.java b/elections-ejb/src/test/java/net/lacnic/elections/domain/services/dbtables/EmailTableReportTest.java new file mode 100644 index 0000000..8e2f942 --- /dev/null +++ b/elections-ejb/src/test/java/net/lacnic/elections/domain/services/dbtables/EmailTableReportTest.java @@ -0,0 +1,88 @@ +package net.lacnic.elections.domain.services.dbtables; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; +import net.lacnic.elections.domain.Email; +import net.lacnic.elections.domain.EmailHistory; +import net.lacnic.elections.utils.DateTimeUtils; +import net.lacnic.elections.utils.ObjectsFactory; + +public class EmailTableReportTest extends TestCase { + + public static Test suite() { + return new TestSuite(EmailTableReportTest.class); + } + + public void testEmailTableReport() { + Email email = ObjectsFactory.createEmail(); + EmailTableReport report = new EmailTableReport(email); + + assertNotNull(report); + + assertEquals(email.getEmailId(), report.getEmailId()); + assertEquals(email.getRecipients(), report.getRecipients()); + assertEquals(email.getSender(), report.getSender()); + assertEquals(email.getCc(), report.getCc()); + assertEquals(email.getBcc(), report.getBcc()); + assertEquals(email.getSubject(), report.getSubject()); + assertEquals(email.getSent(), report.getSent()); + assertEquals(DateTimeUtils.getTableServicesDateTimeString(email.getCreatedDate()), report.getCreatedDate()); + assertEquals(email.getTemplateType(), report.getTemplateType()); + assertEquals(email.getElection().getElectionId(), report.getElectionId().longValue()); + } + + public void testEmailTableReportSecondConstructor() { + EmailHistory emailHistory = ObjectsFactory.createEmailHistory(); + EmailTableReport report = new EmailTableReport(emailHistory); + + assertNotNull(report); + + assertEquals(emailHistory.getEmailHistoryId(), report.getEmailId()); + assertEquals(emailHistory.getRecipients(), report.getRecipients()); + assertEquals(emailHistory.getSender(), report.getSender()); + assertEquals(emailHistory.getCc(), report.getCc()); + assertEquals(emailHistory.getBcc(), report.getBcc()); + assertEquals(emailHistory.getSubject(), report.getSubject()); + assertEquals(emailHistory.getSent(), report.getSent()); + assertEquals(DateTimeUtils.getTableServicesDateTimeString(emailHistory.getCreatedDate()), report.getCreatedDate()); + assertEquals(emailHistory.getTemplateType(), report.getTemplateType()); + assertEquals(emailHistory.getElectionId(), report.getElectionId().longValue()); + } + + public void testEmailTableReportEmptyConstructor() { + Long emailId = 1L; + String recipients = "example@example.com, other@example.com"; + String sender = "sender@example.com"; + String cc = "cc@example.com"; + String bcc = "bcc@example.com"; + String subject = "Asunto del correo"; + String createdDate = "2024-12-01 09:00"; + String templateType = "NOTIFICATION"; + Long electionId = 76447L; + + EmailTableReport report = new EmailTableReport(); + + report.setEmailId(emailId); + report.setRecipients(recipients); + report.setSender(sender); + report.setCc(cc); + report.setBcc(bcc); + report.setSubject(subject); + report.setSent(false); + report.setCreatedDate(createdDate); + report.setTemplateType(templateType); + report.setElectionId(electionId); + + assertEquals(emailId, report.getEmailId()); + assertEquals(recipients, report.getRecipients()); + assertEquals(sender, report.getSender()); + assertEquals(cc, report.getCc()); + assertEquals(bcc, report.getBcc()); + assertEquals(subject, report.getSubject()); + assertFalse(report.getSent()); + assertEquals(createdDate, report.getCreatedDate()); + assertEquals(templateType, report.getTemplateType()); + assertEquals(electionId, report.getElectionId()); + } +} diff --git a/elections-ejb/src/test/java/net/lacnic/elections/domain/services/dbtables/UserAdminTableReportTest.java b/elections-ejb/src/test/java/net/lacnic/elections/domain/services/dbtables/UserAdminTableReportTest.java new file mode 100644 index 0000000..79357e9 --- /dev/null +++ b/elections-ejb/src/test/java/net/lacnic/elections/domain/services/dbtables/UserAdminTableReportTest.java @@ -0,0 +1,44 @@ +package net.lacnic.elections.domain.services.dbtables; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; +import net.lacnic.elections.domain.UserAdmin; +import net.lacnic.elections.utils.ObjectsFactory; + +public class UserAdminTableReportTest extends TestCase { + + public static Test suite() { + return new TestSuite(UserAdminTableReportTest.class); + } + + public void testUserAdminTableReport() { + UserAdmin userAdmin = ObjectsFactory.createUserAdmin(); + UserAdminTableReport report = new UserAdminTableReport(userAdmin); + + assertNotNull(report); + + assertEquals(userAdmin.getUserAdminId(), report.getUserAdminId()); + assertEquals(userAdmin.getEmail(), report.getEmail()); + assertEquals(userAdmin.getAuthorizedElectionId(), report.getAuthorizedElectionId()); + } + + public void testUserAdminTableReportEmptyConstructor() { + String userAdminId = "admin123"; + String email = "admin@example.com"; + String password = "SECUREPASSWORD"; + Long authorizedElectionId = 1001L; + + UserAdminTableReport report = new UserAdminTableReport(); + + report.setUserAdminId(userAdminId); + report.setEmail(email); + report.setPassword(password); + report.setAuthorizedElectionId(authorizedElectionId); + + assertEquals(userAdminId, report.getUserAdminId()); + assertEquals(email, report.getEmail()); + assertEquals(password, report.getPassword()); + assertEquals(authorizedElectionId, report.getAuthorizedElectionId()); + } +} diff --git a/elections-ejb/src/test/java/net/lacnic/elections/domain/services/dbtables/UserVoterTableReportTest.java b/elections-ejb/src/test/java/net/lacnic/elections/domain/services/dbtables/UserVoterTableReportTest.java new file mode 100644 index 0000000..9f85b3a --- /dev/null +++ b/elections-ejb/src/test/java/net/lacnic/elections/domain/services/dbtables/UserVoterTableReportTest.java @@ -0,0 +1,73 @@ +package net.lacnic.elections.domain.services.dbtables; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; +import net.lacnic.elections.domain.UserVoter; +import net.lacnic.elections.utils.DateTimeUtils; +import net.lacnic.elections.utils.ObjectsFactory; + +public class UserVoterTableReportTest extends TestCase { + + public static Test suite() { + return new TestSuite(UserVoterTableReportTest.class); + } + + public void testUserVoterTableReport() { + UserVoter userVoter = ObjectsFactory.createUserVoter(); + UserVoterTableReport report = new UserVoterTableReport(userVoter); + + assertNotNull(report); + + assertEquals(userVoter.getUserVoterId(), report.getUserVoterId().longValue()); + assertEquals(userVoter.getMigrationId(), report.getMigrationId()); + assertEquals(userVoter.getElection().getElectionId(), report.getElectionId().longValue()); + assertEquals(userVoter.getCountry(), report.getCountry()); + assertEquals(userVoter.getLanguage(), report.getLanguage()); + assertEquals(userVoter.getMail(), report.getMail()); + assertEquals(userVoter.getName(), report.getName()); + assertEquals(userVoter.getOrgID(), report.getOrgID()); + assertEquals(userVoter.getVoteAmount(), report.getVoteAmount()); + assertTrue(report.getVoted()); + assertEquals(DateTimeUtils.getTableServicesDateTimeString(userVoter.getVoteDate()), report.getVoteDate()); + } + + public void testUserVoterTableReportEmptyConstructor() { + Long userVoterId = 1L; + Long migrationId = 100L; + Long electionId = 123L; + String country = "Uruguay"; + String language = "Spanish"; + String mail = "john.doe@example.com"; + String name = "John Doe"; + String orgID = "ORG001"; + Integer voteAmount = 3; + String voteDate = "2024-12-01 09:00"; + + UserVoterTableReport report = new UserVoterTableReport(); + + report.setUserVoterId(userVoterId); + report.setMigrationId(migrationId); + report.setElectionId(electionId); + report.setCountry(country); + report.setLanguage(language); + report.setMail(mail); + report.setName(name); + report.setOrgID(orgID); + report.setVoteAmount(voteAmount); + report.setVoted(true); + report.setVoteDate(voteDate); + + assertEquals(userVoterId, report.getUserVoterId()); + assertEquals(migrationId, report.getMigrationId()); + assertEquals(electionId, report.getElectionId()); + assertEquals(country, report.getCountry()); + assertEquals(language, report.getLanguage()); + assertEquals(mail, report.getMail()); + assertEquals(name, report.getName()); + assertEquals(orgID, report.getOrgID()); + assertEquals(voteAmount, report.getVoteAmount()); + assertTrue(report.getVoted()); + assertEquals(voteDate, report.getVoteDate()); + } +} diff --git a/elections-ejb/src/test/java/net/lacnic/elections/domain/services/dbtables/VoteTableReportTest.java b/elections-ejb/src/test/java/net/lacnic/elections/domain/services/dbtables/VoteTableReportTest.java new file mode 100644 index 0000000..ccc9c68 --- /dev/null +++ b/elections-ejb/src/test/java/net/lacnic/elections/domain/services/dbtables/VoteTableReportTest.java @@ -0,0 +1,51 @@ +package net.lacnic.elections.domain.services.dbtables; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; +import net.lacnic.elections.domain.Vote; +import net.lacnic.elections.utils.DateTimeUtils; +import net.lacnic.elections.utils.ObjectsFactory; + +public class VoteTableReportTest extends TestCase { + + public static Test suite() { + return new TestSuite(VoteTableReportTest.class); + } + + public void testVoteTableReport() { + Vote vote = ObjectsFactory.createVote(); + VoteTableReport report = new VoteTableReport(vote); + + assertNotNull(report); + + assertEquals(vote.getVoteId(), report.getVoteId().longValue()); + assertEquals(vote.getIp(), report.getIp()); + assertEquals(DateTimeUtils.getTableServicesDateTimeString(vote.getVoteDate()), report.getVoteDate()); + assertEquals(vote.getCandidate().getCandidateId(), report.getCandidateId().longValue()); + assertEquals(vote.getElection().getElectionId(), report.getElectionId().longValue()); + } + + public void testVoteTableReportEmptyConstructor() { + Long voteId = 1L; + String ip = "192.168.0.1"; + String voteDate = "2024-12-01 09:00"; + Long candidateId = 10L; + Long electionId = 1001L; + + VoteTableReport report = new VoteTableReport(); + + report.setVoteId(voteId); + report.setIp(ip); + report.setVoteDate(voteDate); + report.setCandidateId(candidateId); + report.setElectionId(electionId); + + assertEquals(voteId, report.getVoteId()); + assertEquals(ip, report.getIp()); + assertEquals(voteDate, report.getVoteDate()); + assertEquals(candidateId, report.getCandidateId()); + assertEquals(electionId, report.getElectionId()); + } + +} diff --git a/elections-ejb/src/test/java/net/lacnic/elections/domain/services/detail/AuditorDetailReportTest.java b/elections-ejb/src/test/java/net/lacnic/elections/domain/services/detail/AuditorDetailReportTest.java new file mode 100644 index 0000000..beb9b4e --- /dev/null +++ b/elections-ejb/src/test/java/net/lacnic/elections/domain/services/detail/AuditorDetailReportTest.java @@ -0,0 +1,62 @@ +package net.lacnic.elections.domain.services.detail; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; +import net.lacnic.elections.domain.Auditor; +import net.lacnic.elections.utils.ObjectsFactory; + +public class AuditorDetailReportTest extends TestCase { + + public static Test suite() { + return new TestSuite(AuditorDetailReportTest.class); + } + + public void testAuditorTableReport() { + Auditor auditor = ObjectsFactory.createAuditor(); + AuditorDetailReport report = new AuditorDetailReport(auditor); + + assertNotNull(report); + + assertEquals(auditor.getAuditorId(), report.getAuditorId().longValue()); + assertTrue(report.getCommissioner()); + assertTrue(report.getAgreedConformity()); + assertEquals(auditor.getMail(), report.getMail()); + assertEquals(auditor.getName(), report.getName()); + assertEquals(auditor.getElection().getElectionId(), report.getElectionId().longValue()); + assertTrue(report.getRevisionAvailable()); + assertEquals(auditor.getMigrationId(), report.getMigrationId()); + } + + public void testAuditorTableReportEmptyConstructor() { + Long auditorId = 1L; + boolean commissioner = true; + boolean agreedConformity = true; + String mail = "auditor1@example.com"; + String name = "Auditor 1"; + Long electionId = 4567L; + boolean revisionAvailable = true; + Long migrationId = 123L; + + AuditorDetailReport report = new AuditorDetailReport(); + + report.setAuditorId(auditorId); + report.setCommissioner(commissioner); + report.setAgreedConformity(agreedConformity); + report.setMail(mail); + report.setName(name); + report.setElectionId(electionId); + report.setRevisionAvailable(revisionAvailable); + report.setMigrationId(migrationId); + + assertEquals(auditorId, report.getAuditorId()); + assertTrue(report.getCommissioner()); + assertTrue(report.getAgreedConformity()); + assertEquals(mail, report.getMail()); + assertEquals(name, report.getName()); + assertEquals(electionId, report.getElectionId()); + assertTrue(report.getRevisionAvailable()); + assertEquals(migrationId, report.getMigrationId()); + } + +} diff --git a/elections-ejb/src/test/java/net/lacnic/elections/domain/services/detail/CandidateDetailReportTest.java b/elections-ejb/src/test/java/net/lacnic/elections/domain/services/detail/CandidateDetailReportTest.java new file mode 100644 index 0000000..dfdcaeb --- /dev/null +++ b/elections-ejb/src/test/java/net/lacnic/elections/domain/services/detail/CandidateDetailReportTest.java @@ -0,0 +1,95 @@ +package net.lacnic.elections.domain.services.detail; + +import java.util.Base64; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; +import net.lacnic.elections.domain.Candidate; +import net.lacnic.elections.utils.ObjectsFactory; + +public class CandidateDetailReportTest extends TestCase { + + public static Test suite() { + return new TestSuite(CandidateDetailReportTest.class); + } + + public void testCandidateTableReport() { + Candidate candidate = ObjectsFactory.createCandidate(); + CandidateDetailReport report = new CandidateDetailReport(candidate); + + assertNotNull(report); + + assertEquals(candidate.getCandidateId(), report.getCandidateId().longValue()); + assertEquals(candidate.getMigrationId(), report.getMigrationId()); + assertEquals(candidate.getName(), report.getName()); + assertEquals(candidate.getMail(), report.getMail()); + assertEquals(candidate.getElection().getElectionId(), report.getElectionId().longValue()); + assertEquals(Base64.getEncoder().encodeToString(candidate.getPictureInfo()), report.getPictureInfo()); + assertEquals(candidate.getPictureName(), report.getPictureName()); + assertEquals(candidate.getBioSpanish(), report.getBioSpanish()); + assertEquals(candidate.getBioEnglish(), report.getBioEnglish()); + assertEquals(candidate.getBioPortuguese(), report.getBioPortuguese()); + assertEquals(candidate.getPictureExtension(), report.getPictureExtension()); + assertEquals(candidate.getCandidateOrder(), report.getCandidateOrder().longValue()); + assertFalse(report.isOnlySp()); + assertEquals(candidate.getLinkSpanish(), report.getLinkSpanish()); + assertEquals(candidate.getLinkEnglish(), report.getLinkEnglish()); + assertEquals(candidate.getLinkPortuguese(), report.getLinkPortuguese()); + } + + public void testCandidateTableReportEmptyConstructor() { + Long candidateId = 1L; + Long migrationId = 123L; + String name = "Juan Perez"; + String mail = "juan.perez@example.com"; + Long electionId = 456L; + String pictureInfo = "pictureInfo"; + String pictureName = "profile.jpg"; + String bioSpanish = "Biografía en español"; + String bioEnglish = "Biography in English"; + String bioPortuguese = "Biografia em português"; + String pictureExtension = "jpg"; + Integer candidateOrder = 1; + String linkSpanish = "linkES"; + String linkEnglish = "linkEN"; + String linkPortuguese = "linkPT"; + + CandidateDetailReport report = new CandidateDetailReport(); + + report.setCandidateId(candidateId); + report.setMigrationId(migrationId); + report.setName(name); + report.setMail(mail); + report.setElectionId(electionId); + report.setPictureInfo(pictureInfo); + report.setPictureName(pictureName); + report.setBioSpanish(bioSpanish); + report.setBioEnglish(bioEnglish); + report.setBioPortuguese(bioPortuguese); + report.setPictureExtension(pictureExtension); + report.setCandidateOrder(candidateOrder); + report.setOnlySp(true); + report.setLinkSpanish(linkSpanish); + report.setLinkEnglish(linkEnglish); + report.setLinkPortuguese(linkPortuguese); + + assertEquals(candidateId, report.getCandidateId()); + assertEquals(migrationId, report.getMigrationId()); + assertEquals(name, report.getName()); + assertEquals(mail, report.getMail()); + assertEquals(electionId, report.getElectionId()); + assertEquals(pictureInfo, report.getPictureInfo()); + assertEquals(pictureName, report.getPictureName()); + assertEquals(bioSpanish, report.getBioSpanish()); + assertEquals(bioEnglish, report.getBioEnglish()); + assertEquals(bioPortuguese, report.getBioPortuguese()); + assertEquals(pictureExtension, report.getPictureExtension()); + assertEquals(candidateOrder, report.getCandidateOrder()); + assertTrue(report.isOnlySp()); + assertEquals(linkSpanish, report.getLinkSpanish()); + assertEquals(linkEnglish, report.getLinkEnglish()); + assertEquals(linkPortuguese, report.getLinkPortuguese()); + } + +} diff --git a/elections-ejb/src/test/java/net/lacnic/elections/domain/services/detail/CommissionerDetailReportTest.java b/elections-ejb/src/test/java/net/lacnic/elections/domain/services/detail/CommissionerDetailReportTest.java new file mode 100644 index 0000000..ae37d82 --- /dev/null +++ b/elections-ejb/src/test/java/net/lacnic/elections/domain/services/detail/CommissionerDetailReportTest.java @@ -0,0 +1,42 @@ +package net.lacnic.elections.domain.services.detail; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; +import net.lacnic.elections.domain.Commissioner; +import net.lacnic.elections.utils.ObjectsFactory; + +public class CommissionerDetailReportTest extends TestCase { + + public static Test suite() { + return new TestSuite(CommissionerDetailReportTest.class); + } + + public void testCommissionerTableReport() { + Commissioner commissioner = ObjectsFactory.createCommissioner(); + CommissionerDetailReport report = new CommissionerDetailReport(commissioner); + + assertNotNull(report); + + assertEquals(commissioner.getCommissionerId(), report.getCommissionerId().longValue()); + assertEquals(commissioner.getName(), report.getName()); + assertEquals(commissioner.getMail(), report.getMail()); + } + + public void testCommissionerTableReportEmptyConstructor() { + Long commissionerId = 1L; + String name = "Juan Perez"; + String mail = "juan1@example.com"; + + CommissionerDetailReport report = new CommissionerDetailReport(); + + report.setCommissionerId(commissionerId); + report.setName(name); + report.setMail(mail); + + assertEquals(commissionerId, report.getCommissionerId()); + assertEquals(name, report.getName()); + assertEquals(mail, report.getMail()); + } + +} diff --git a/elections-ejb/src/test/java/net/lacnic/elections/domain/services/detail/ElectionDetailReportTest.java b/elections-ejb/src/test/java/net/lacnic/elections/domain/services/detail/ElectionDetailReportTest.java new file mode 100644 index 0000000..2d1518e --- /dev/null +++ b/elections-ejb/src/test/java/net/lacnic/elections/domain/services/detail/ElectionDetailReportTest.java @@ -0,0 +1,167 @@ +package net.lacnic.elections.domain.services.detail; + +import java.util.ArrayList; +import java.util.List; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; +import net.lacnic.elections.domain.Auditor; +import net.lacnic.elections.domain.Election; +import net.lacnic.elections.utils.ObjectsFactory; + +public class ElectionDetailReportTest extends TestCase { + + public static Test suite() { + return new TestSuite(ElectionDetailReportTest.class); + } + + public void testElectionTableReport() { + Election election = ObjectsFactory.createElection(123L); + Auditor auditor1 = ObjectsFactory.createAuditor(); + auditor1.setCommissioner(true); + + Auditor auditor2 = ObjectsFactory.createAuditor(); + auditor2.setCommissioner(false); + + List auditors = new ArrayList<>(); + auditors.add(auditor1); + auditors.add(auditor2); + election.setAuditors(auditors); + + List auditorDetailReportList = new ArrayList<>(); + List candidates = new ArrayList<>(); + List commissioners = new ArrayList<>(); + List userVoters = new ArrayList<>(); + + ElectionDetailReport report = new ElectionDetailReport(election); + assertNotNull(report); + + report.setAuditors(auditorDetailReportList); + report.setCandidates(candidates); + report.setCommissioners(commissioners); + report.setUserVoters(userVoters); + + assertEquals(election.getElectionId(), report.getElectionId().longValue()); + assertEquals(election.getDescriptionSpanish(), report.getDescriptionSpanish()); + assertEquals(election.getDescriptionEnglish(), report.getDescriptionEnglish()); + assertEquals(election.getDescriptionPortuguese(), report.getDescriptionPortuguese()); + assertEquals(election.getCreationDateString(), report.getCreationDate()); + assertEquals(election.getEndDateString(), report.getEndDate()); + assertEquals(election.getStartDateString(), report.getStartDate()); + assertTrue(report.getResultLinkAvailable()); + assertTrue(report.getVotingLinkAvailable()); + assertEquals(election.getLinkSpanish(), report.getLinkSpanish()); + assertEquals(election.getLinkEnglish(), report.getLinkEnglish()); + assertEquals(election.getLinkPortuguese(), report.getLinkPortuguese()); + assertEquals(election.getMaxCandidates(), report.getMaxCandidates().intValue()); + assertEquals(election.getTitleSpanish(), report.getTitleSpanish()); + assertEquals(election.getTitleEnglish(), report.getTitleEnglish()); + assertEquals(election.getTitlePortuguese(), report.getTitlePortuguese()); + assertFalse(report.getAuditorsSet()); + assertTrue(report.getCandidatesSet()); + assertTrue(report.getElectorsSet()); + assertFalse(report.getAuditorLinkAvailable()); + assertFalse(report.getOnlySp()); + assertEquals(election.getDefaultSender(), report.getDefaultSender()); + assertTrue(report.getRandomOrderCandidates()); + assertEquals(election.getDiffUTC(), report.getDiffUTC().intValue()); + assertTrue(report.getRevisionRequest()); + assertEquals(election.getMigrationId(), report.getMigrationId()); + assertTrue(report.getMigrated()); + assertEquals(election.getCategory().toString(), report.getCategory()); + assertFalse(report.getClosed()); + assertEquals(election.getClosedDateString(), report.getClosedDate()); + assertEquals(auditorDetailReportList, report.getAuditors()); + assertEquals(candidates, report.getCandidates()); + assertEquals(commissioners, report.getCommissioners()); + assertEquals(userVoters, report.getUserVoters()); + } + + public void testElectionTableReportEmptyConstructor() { + Long electionId = 123L; + String descriptionSpanish = "Descripción en español"; + String descriptionEnglish = "Description in English"; + String descriptionPortuguese = "Descrição em português"; + String creationDate = "01/12/2024 09:00"; + String endDate = "02/12/2024 18:30"; + String startDate = "01/12/2024 10:00"; + String linkSpanish = "http://example.com/es"; + String linkEnglish = "http://example.com/en"; + String linkPortuguese = "http://example.com/pt"; + Integer maxCandidates = 5; + String titleSpanish = "Elección en español"; + String titleEnglish = "Election in English"; + String titlePortuguese = "Eleição em português"; + String defaultSender = "noreply@example.com"; + Integer diffUTC = 3; + Long migrationId = 54321L; + String category = "STATUTORY"; + String closedDate = "01/01/2025"; + + ElectionDetailReport report = new ElectionDetailReport(); + + report.setElectionId(electionId); + report.setDescriptionSpanish(descriptionSpanish); + report.setDescriptionEnglish(descriptionEnglish); + report.setDescriptionPortuguese(descriptionPortuguese); + report.setCreationDate(creationDate); + report.setEndDate(endDate); + report.setStartDate(startDate); + report.setResultLinkAvailable(true); + report.setVotingLinkAvailable(true); + report.setLinkSpanish(linkSpanish); + report.setLinkEnglish(linkEnglish); + report.setLinkPortuguese(linkPortuguese); + report.setMaxCandidates(maxCandidates); + report.setTitleSpanish(titleSpanish); + report.setTitleEnglish(titleEnglish); + report.setTitlePortuguese(titlePortuguese); + report.setAuditorsSet(true); + report.setCandidatesSet(true); + report.setElectorsSet(true); + report.setAuditorLinkAvailable(true); + report.setOnlySp(false); + report.setDefaultSender(defaultSender); + report.setRandomOrderCandidates(true); + report.setDiffUTC(diffUTC); + report.setRevisionRequest(true); + report.setMigrationId(migrationId); + report.setMigrated(true); + report.setCategory(category); + report.setClosed(false); + report.setClosedDate(closedDate); + + assertEquals(electionId, report.getElectionId()); + assertEquals(descriptionSpanish, report.getDescriptionSpanish()); + assertEquals(descriptionEnglish, report.getDescriptionEnglish()); + assertEquals(descriptionPortuguese, report.getDescriptionPortuguese()); + assertEquals(creationDate, report.getCreationDate()); + assertEquals(endDate, report.getEndDate()); + assertEquals(startDate, report.getStartDate()); + assertTrue(report.getResultLinkAvailable()); + assertTrue(report.getVotingLinkAvailable()); + assertEquals(linkSpanish, report.getLinkSpanish()); + assertEquals(linkEnglish, report.getLinkEnglish()); + assertEquals(linkPortuguese, report.getLinkPortuguese()); + assertEquals(maxCandidates, report.getMaxCandidates()); + assertEquals(titleSpanish, report.getTitleSpanish()); + assertEquals(titleEnglish, report.getTitleEnglish()); + assertEquals(titlePortuguese, report.getTitlePortuguese()); + assertTrue(report.getAuditorsSet()); + assertTrue(report.getCandidatesSet()); + assertTrue(report.getElectorsSet()); + assertTrue(report.getAuditorLinkAvailable()); + assertFalse(report.getOnlySp()); + assertEquals(defaultSender, report.getDefaultSender()); + assertTrue(report.getRandomOrderCandidates()); + assertEquals(diffUTC, report.getDiffUTC()); + assertTrue(report.getRevisionRequest()); + assertEquals(migrationId, report.getMigrationId()); + assertTrue(report.getMigrated()); + assertEquals(category, report.getCategory()); + assertFalse(report.getClosed()); + assertEquals(closedDate, report.getClosedDate()); + } + +} diff --git a/elections-ejb/src/test/java/net/lacnic/elections/domain/services/detail/ElectionParticipationDetailReportTest.java b/elections-ejb/src/test/java/net/lacnic/elections/domain/services/detail/ElectionParticipationDetailReportTest.java new file mode 100644 index 0000000..d4ae272 --- /dev/null +++ b/elections-ejb/src/test/java/net/lacnic/elections/domain/services/detail/ElectionParticipationDetailReportTest.java @@ -0,0 +1,151 @@ +package net.lacnic.elections.domain.services.detail; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; +import net.lacnic.elections.domain.Auditor; +import net.lacnic.elections.domain.Candidate; +import net.lacnic.elections.domain.UserVoter; +import net.lacnic.elections.utils.DateTimeUtils; +import net.lacnic.elections.utils.ObjectsFactory; + +public class ElectionParticipationDetailReportTest extends TestCase { + + public static Test suite() { + return new TestSuite(ElectionParticipationDetailReportTest.class); + } + + public void testElectionParticipationDetailReport_Auditor() { + Auditor auditor = ObjectsFactory.createAuditor(); + ElectionParticipationDetailReport report = new ElectionParticipationDetailReport(auditor); + + assertEquals(auditor.getElection().getElectionId(), report.getElectionId().longValue()); + assertEquals(auditor.getElection().getTitleEnglish(), report.getElectionTitleEN()); + assertEquals(auditor.getElection().getTitleSpanish(), report.getElectionTitleES()); + assertEquals(auditor.getElection().getTitlePortuguese(), report.getElectionTitleSP()); + assertEquals(auditor.getElection().getCategory().toString(), report.getCategory()); + assertEquals(auditor.getElection().getDescriptionEnglish(), report.getDescriptionEnglish()); + assertEquals(auditor.getElection().getDescriptionPortuguese(), report.getDescriptionPortuguese()); + assertEquals(auditor.getElection().getDescriptionSpanish(), report.getDescriptionSpanish()); + assertEquals("Commissioner", report.getRole()); + + auditor.setCommissioner(false); + report = new ElectionParticipationDetailReport(auditor); + assertEquals("Auditor", report.getRole()); + + assertEquals(DateTimeUtils.getTableServicesDateTimeString(auditor.getElection().getStartDate()), report.getStartDate()); + assertEquals(DateTimeUtils.getTableServicesDateTimeString(auditor.getElection().getEndDate()), report.getEndDate()); + } + + public void testElectionParticipationDetailReport_Candidate() { + Candidate candidate = ObjectsFactory.createCandidate(); + ElectionParticipationDetailReport report = new ElectionParticipationDetailReport(candidate); + + assertEquals(candidate.getElection().getElectionId(), report.getElectionId().longValue()); + assertEquals(candidate.getElection().getTitleEnglish(), report.getElectionTitleEN()); + assertEquals(candidate.getElection().getTitleSpanish(), report.getElectionTitleES()); + assertEquals(candidate.getElection().getTitlePortuguese(), report.getElectionTitleSP()); + assertEquals(candidate.getElection().getCategory().toString(), report.getCategory()); + assertEquals(candidate.getElection().getDescriptionEnglish(), report.getDescriptionEnglish()); + assertEquals(candidate.getElection().getDescriptionPortuguese(), report.getDescriptionPortuguese()); + assertEquals(candidate.getElection().getDescriptionSpanish(), report.getDescriptionSpanish()); + assertEquals("Candidate", report.getRole()); + assertEquals(DateTimeUtils.getTableServicesDateTimeString(candidate.getElection().getStartDate()), report.getStartDate()); + assertEquals(DateTimeUtils.getTableServicesDateTimeString(candidate.getElection().getEndDate()), report.getEndDate()); + + assertEquals(candidate.getBioSpanish(), report.getCandidateBio()); + assertEquals(candidate.getLinkSpanish(), report.getCandidateLink()); + } + + public void testElectionParticipationDetailReport_UserVoter() { + UserVoter userVoter = ObjectsFactory.createUserVoter(); + ElectionParticipationDetailReport report = new ElectionParticipationDetailReport(userVoter); + + assertEquals(userVoter.getElection().getElectionId(), report.getElectionId().longValue()); + assertEquals(userVoter.getElection().getTitleEnglish(), report.getElectionTitleEN()); + assertEquals(userVoter.getElection().getTitleSpanish(), report.getElectionTitleES()); + assertEquals(userVoter.getElection().getTitlePortuguese(), report.getElectionTitleSP()); + assertEquals(userVoter.getElection().getCategory().toString(), report.getCategory()); + assertEquals(userVoter.getElection().getDescriptionEnglish(), report.getDescriptionEnglish()); + assertEquals(userVoter.getElection().getDescriptionPortuguese(), report.getDescriptionPortuguese()); + assertEquals(userVoter.getElection().getDescriptionSpanish(), report.getDescriptionSpanish()); + assertEquals("Voter", report.getRole()); + assertEquals(DateTimeUtils.getTableServicesDateTimeString(userVoter.getElection().getStartDate()), report.getStartDate()); + assertEquals(DateTimeUtils.getTableServicesDateTimeString(userVoter.getElection().getEndDate()), report.getEndDate()); + + assertEquals(userVoter.getOrgID(), report.getVoterOrgID()); + assertEquals(userVoter.getCountry(), report.getVoterCountry()); + assertEquals(userVoter.getLanguage(), report.getVoterLanguage()); + assertEquals(userVoter.getVoteAmount().intValue(), report.getVoterVoteAmount()); + assertEquals(userVoter.isVoted(), report.isVoterVoted()); + assertEquals(DateTimeUtils.getTableServicesDateTimeString(userVoter.getVoteDate()), report.getVoterVoteDate()); + } + + public void testElectionParticipationDetailReport_Setters() { + Long electionId = 4567L; + String electionTitleEN = "Test Election in English"; + String electionTitleES = "Elección de prueba en español"; + String electionTitleSP = "Eleições de teste em português"; + String category = "Statutory"; + String descriptionEnglish = "Description in English"; + String descriptionPortuguese = "Descrição em português"; + String descriptionSpanish = "Descripción en español"; + String role = "Voter"; + String startDate = "2024-12-01 10:00"; + String endDate = "2024-12-02 18:00"; + String voterOrgID = "ORG001"; + String voterCountry = "Uruguay"; + String voterLanguage = "Spanish"; + int voterVoteAmount = 3; + String voterVoteDate = "2024-12-01 09:00"; + String candidateBio = "Biografía en español"; + String candidateLink = "linkES"; + + ElectionParticipationDetailReport report = new ElectionParticipationDetailReport(); + + report.setElectionId(electionId); + report.setElectionTitleEN(electionTitleEN); + report.setElectionTitleES(electionTitleES); + report.setElectionTitleSP(electionTitleSP); + report.setCategory(category); + report.setDescriptionEnglish(descriptionEnglish); + report.setDescriptionPortuguese(descriptionPortuguese); + report.setDescriptionSpanish(descriptionSpanish); + report.setRole(role); + report.setStartDate(startDate); + report.setEndDate(endDate); + + report.setVoterOrgID(voterOrgID); + report.setVoterCountry(voterCountry); + report.setVoterLanguage(voterLanguage); + report.setVoterVoteAmount(voterVoteAmount); + report.setVoterVoted(true); + report.setVoterVoteDate(voterVoteDate); + + report.setCandidateBio(candidateBio); + report.setCandidateLink(candidateLink); + + assertEquals(electionId, report.getElectionId()); + assertEquals(electionTitleEN, report.getElectionTitleEN()); + assertEquals(electionTitleES, report.getElectionTitleES()); + assertEquals(electionTitleSP, report.getElectionTitleSP()); + assertEquals(category, report.getCategory()); + assertEquals(descriptionEnglish, report.getDescriptionEnglish()); + assertEquals(descriptionPortuguese, report.getDescriptionPortuguese()); + assertEquals(descriptionSpanish, report.getDescriptionSpanish()); + assertEquals(role, report.getRole()); + assertEquals(startDate, report.getStartDate()); + assertEquals(endDate, report.getEndDate()); + + assertEquals(voterOrgID, report.getVoterOrgID()); + assertEquals(voterCountry, report.getVoterCountry()); + assertEquals(voterLanguage, report.getVoterLanguage()); + assertEquals(voterVoteAmount, report.getVoterVoteAmount()); + assertTrue(report.isVoterVoted()); + assertEquals(voterVoteDate, report.getVoterVoteDate()); + + assertEquals(candidateBio, report.getCandidateBio()); + assertEquals(candidateLink, report.getCandidateLink()); + } + +} diff --git a/elections-ejb/src/test/java/net/lacnic/elections/domain/services/detail/OrganizationElectionDetailReportTest.java b/elections-ejb/src/test/java/net/lacnic/elections/domain/services/detail/OrganizationElectionDetailReportTest.java new file mode 100644 index 0000000..db5760e --- /dev/null +++ b/elections-ejb/src/test/java/net/lacnic/elections/domain/services/detail/OrganizationElectionDetailReportTest.java @@ -0,0 +1,54 @@ +package net.lacnic.elections.domain.services.detail; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; +import net.lacnic.elections.domain.Election; +import net.lacnic.elections.utils.DateTimeUtils; +import net.lacnic.elections.utils.ObjectsFactory; + +public class OrganizationElectionDetailReportTest extends TestCase { + + public static Test suite() { + return new TestSuite(OrganizationElectionDetailReportTest.class); + } + + public void testOrganizationElectionDetailReport() { + Election election = ObjectsFactory.createElection(200L); + + OrganizationElectionDetailReport report = new OrganizationElectionDetailReport(election); + + assertEquals(election.getElectionId(), report.getElectionId().longValue()); + assertEquals(election.getTitleEnglish(), report.getElectionTitleEN()); + assertEquals(election.getTitleSpanish(), report.getElectionTitleES()); + assertEquals(election.getTitlePortuguese(), report.getElectionTitleSP()); + assertEquals(DateTimeUtils.getTableServicesDateTimeString(election.getStartDate()), report.getStartDate()); + assertEquals(DateTimeUtils.getTableServicesDateTimeString(election.getEndDate()), report.getEndDate()); + } + + public void testOrganizationElectionDetailReportEmptyConstructor() { + Long electionId = 200L; + String electionTitleEN = "Test Election in English"; + String electionTitleES = "Elección de prueba en español"; + String electionTitleSP = "Eleições de teste em português"; + String startDate = "2024-12-01 10:00"; + String endDate = "2024-12-02 18:00"; + + OrganizationElectionDetailReport report = new OrganizationElectionDetailReport(); + + report.setElectionId(electionId); + report.setElectionTitleEN(electionTitleEN); + report.setElectionTitleES(electionTitleES); + report.setElectionTitleSP(electionTitleSP); + report.setStartDate(startDate); + report.setEndDate(endDate); + + assertEquals(electionId, report.getElectionId()); + assertEquals(electionTitleEN, report.getElectionTitleEN()); + assertEquals(electionTitleES, report.getElectionTitleES()); + assertEquals(electionTitleSP, report.getElectionTitleSP()); + assertEquals(startDate, report.getStartDate()); + assertEquals(endDate, report.getEndDate()); + } + +} diff --git a/elections-ejb/src/test/java/net/lacnic/elections/domain/services/detail/OrganizationVoterDetailReportTest.java b/elections-ejb/src/test/java/net/lacnic/elections/domain/services/detail/OrganizationVoterDetailReportTest.java new file mode 100644 index 0000000..305be35 --- /dev/null +++ b/elections-ejb/src/test/java/net/lacnic/elections/domain/services/detail/OrganizationVoterDetailReportTest.java @@ -0,0 +1,81 @@ +package net.lacnic.elections.domain.services.detail; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; +import net.lacnic.elections.domain.UserVoter; +import net.lacnic.elections.utils.DateTimeUtils; +import net.lacnic.elections.utils.ObjectsFactory; + +public class OrganizationVoterDetailReportTest extends TestCase { + + public static Test suite() { + return new TestSuite(OrganizationVoterDetailReportTest.class); + } + + public void testOrganizationVoterDetailReport() { + UserVoter userVoter = ObjectsFactory.createUserVoter(); + OrganizationVoterDetailReport report = new OrganizationVoterDetailReport(userVoter); + + assertEquals(userVoter.getUserVoterId(), report.getUserVoterId().longValue()); + assertEquals(userVoter.isVoted(), report.getVoted().booleanValue()); + assertEquals(userVoter.getName(), report.getName()); + assertEquals(userVoter.getMail(), report.getMail()); + assertEquals(userVoter.getOrgID(), report.getOrgID()); + assertEquals(DateTimeUtils.getTableServicesDateTimeString(userVoter.getVoteDate()), report.getVoteDate()); + assertNotNull(report.getElection()); + assertEquals(userVoter.getElection().getElectionId(), report.getElection().getElectionId().longValue()); + assertEquals(userVoter.getElection().getTitleEnglish(), report.getElection().getElectionTitleEN()); + assertEquals(userVoter.getElection().getTitleSpanish(), report.getElection().getElectionTitleES()); + assertEquals(userVoter.getElection().getTitlePortuguese(), report.getElection().getElectionTitleSP()); + } + + public void testOrganizationVoterDetailReportEmptyConstructor() { + Long userVoterId = 1L; + String name = "John Doe"; + String mail = "john.doe@example.com"; + String orgID = "ORG001"; + String voteDate = "2024-12-01 09:00"; + + Long electionId = 200L; + String electionTitleEN = "Test Election in English"; + String electionTitleES = "Elección de prueba en español"; + String electionTitleSP = "Eleições de teste em português"; + String startDate = "2024-12-01 10:00"; + String endDate = "2024-12-02 18:00"; + + OrganizationVoterDetailReport report = new OrganizationVoterDetailReport(); + + report.setUserVoterId(userVoterId); + report.setVoted(true); + report.setName(name); + report.setMail(mail); + report.setOrgID(orgID); + report.setVoteDate(voteDate); + + OrganizationElectionDetailReport electionDetailReport = new OrganizationElectionDetailReport(); + electionDetailReport.setElectionId(electionId); + electionDetailReport.setElectionTitleEN(electionTitleEN); + electionDetailReport.setElectionTitleES(electionTitleES); + electionDetailReport.setElectionTitleSP(electionTitleSP); + electionDetailReport.setStartDate(startDate); + electionDetailReport.setEndDate(endDate); + + report.setElection(electionDetailReport); + + assertEquals(userVoterId, report.getUserVoterId()); + assertTrue(report.getVoted()); + assertEquals(name, report.getName()); + assertEquals(mail, report.getMail()); + assertEquals(orgID, report.getOrgID()); + assertEquals(voteDate, report.getVoteDate()); + + assertEquals(electionId, report.getElection().getElectionId()); + assertEquals(electionTitleEN, report.getElection().getElectionTitleEN()); + assertEquals(electionTitleES, report.getElection().getElectionTitleES()); + assertEquals(electionTitleSP, report.getElection().getElectionTitleSP()); + assertEquals(startDate, report.getElection().getStartDate()); + assertEquals(endDate, report.getElection().getEndDate()); + } + +} diff --git a/elections-ejb/src/test/java/net/lacnic/elections/domain/services/detail/UserVoterDetailReportTest.java b/elections-ejb/src/test/java/net/lacnic/elections/domain/services/detail/UserVoterDetailReportTest.java new file mode 100644 index 0000000..8be71cf --- /dev/null +++ b/elections-ejb/src/test/java/net/lacnic/elections/domain/services/detail/UserVoterDetailReportTest.java @@ -0,0 +1,74 @@ +package net.lacnic.elections.domain.services.detail; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; +import net.lacnic.elections.domain.UserVoter; +import net.lacnic.elections.utils.DateTimeUtils; +import net.lacnic.elections.utils.ObjectsFactory; + +public class UserVoterDetailReportTest extends TestCase { + + public static Test suite() { + return new TestSuite(UserVoterDetailReportTest.class); + } + + public void testUserVoterTableReport() { + UserVoter userVoter = ObjectsFactory.createUserVoter(); + UserVoterDetailReport report = new UserVoterDetailReport(userVoter); + + assertNotNull(report); + + assertEquals(userVoter.getUserVoterId(), report.getUserVoterId().longValue()); + assertEquals(userVoter.getMigrationId(), report.getMigrationId()); + assertEquals(userVoter.getElection().getElectionId(), report.getElectionId().longValue()); + assertEquals(userVoter.getCountry(), report.getCountry()); + assertEquals(userVoter.getLanguage(), report.getLanguage()); + assertEquals(userVoter.getMail(), report.getMail()); + assertEquals(userVoter.getName(), report.getName()); + assertEquals(userVoter.getOrgID(), report.getOrgID()); + assertEquals(userVoter.getVoteAmount(), report.getVoteAmount()); + assertTrue(report.getVoted()); + assertEquals(DateTimeUtils.getTableServicesDateTimeString(userVoter.getVoteDate()), report.getVoteDate()); + } + + public void testUserVoterTableReportEmptyConstructor() { + Long userVoterId = 1L; + Long migrationId = 100L; + Long electionId = 123L; + String country = "Uruguay"; + String language = "Spanish"; + String mail = "john.doe@example.com"; + String name = "John Doe"; + String orgID = "ORG001"; + Integer voteAmount = 3; + String voteDate = "2024-12-01 09:00"; + + UserVoterDetailReport report = new UserVoterDetailReport(); + + report.setUserVoterId(userVoterId); + report.setMigrationId(migrationId); + report.setElectionId(electionId); + report.setCountry(country); + report.setLanguage(language); + report.setMail(mail); + report.setName(name); + report.setOrgID(orgID); + report.setVoteAmount(voteAmount); + report.setVoted(true); + report.setVoteDate(voteDate); + + assertEquals(userVoterId, report.getUserVoterId()); + assertEquals(migrationId, report.getMigrationId()); + assertEquals(electionId, report.getElectionId()); + assertEquals(country, report.getCountry()); + assertEquals(language, report.getLanguage()); + assertEquals(mail, report.getMail()); + assertEquals(name, report.getName()); + assertEquals(orgID, report.getOrgID()); + assertEquals(voteAmount, report.getVoteAmount()); + assertTrue(report.getVoted()); + assertEquals(voteDate, report.getVoteDate()); + } + +} diff --git a/elections-ejb/src/test/java/net/lacnic/elections/utils/ObjectsFactory.java b/elections-ejb/src/test/java/net/lacnic/elections/utils/ObjectsFactory.java index ec4d37d..16d0a1e 100644 --- a/elections-ejb/src/test/java/net/lacnic/elections/utils/ObjectsFactory.java +++ b/elections-ejb/src/test/java/net/lacnic/elections/utils/ObjectsFactory.java @@ -4,19 +4,9 @@ import java.util.Date; import java.util.List; +import net.lacnic.elections.domain.*; import org.joda.time.DateTime; -import net.lacnic.elections.domain.Auditor; -import net.lacnic.elections.domain.Candidate; -import net.lacnic.elections.domain.Commissioner; -import net.lacnic.elections.domain.Election; -import net.lacnic.elections.domain.ElectionCategory; -import net.lacnic.elections.domain.ElectionEmailTemplate; -import net.lacnic.elections.domain.ElectionLight; -import net.lacnic.elections.domain.Email; -import net.lacnic.elections.domain.UserVoter; -import net.lacnic.elections.domain.Vote; - public class ObjectsFactory { public static Email createEmail() { @@ -39,69 +29,38 @@ public static Email createEmail() { } public static Election createElection(long electionId) { - Long migrationId = 54321L; - boolean migrated = true; - Date creationDate = new DateTime(2024, 12, 1, 9, 0).toDate(); - String titleSpanish = "Elección de prueba en español"; - String titleEnglish = "Test Election in English"; - String titlePortuguese = "Eleições de teste em português"; - String linkSpanish = "http://example.com/es"; - String linkEnglish = "http://example.com/en"; - String linkPortuguese = "http://example.com/pt"; - String descriptionSpanish = "Descripción en español"; - String descriptionEnglish = "Description in English"; - String descriptionPortuguese = "Descrição em português"; - int maxCandidates = 5; - boolean votingLinkAvailable = true; - boolean resultLinkAvailable = true; - boolean auditorLinkAvailable = false; - boolean revisionRequest = true; - boolean onlySp = false; - String resultToken = "result-token-123"; - String defaultSender = "noreply@example.com"; - boolean electorsSet = true; - boolean candidatesSet = true; - boolean auditorsSet = false; - boolean randomOrderCandidates = true; - int diffUTC = 3; - boolean closed = false; - String auxStartDate = "25/12/2024"; - String auxStartHour = "10:00"; - String auxEndDate = "26/12/2024"; - String auxEndHour = "18:30"; - Election election = new Election(electionId); - election.setMigrationId(migrationId); + election.setMigrationId(54321L); election.setCategory(ElectionCategory.STATUTORY); - election.setMigrated(migrated); - election.setCreationDate(creationDate); - election.setTitleSpanish(titleSpanish); - election.setTitleEnglish(titleEnglish); - election.setTitlePortuguese(titlePortuguese); - election.setLinkSpanish(linkSpanish); - election.setLinkEnglish(linkEnglish); - election.setLinkPortuguese(linkPortuguese); - election.setDescriptionSpanish(descriptionSpanish); - election.setDescriptionEnglish(descriptionEnglish); - election.setDescriptionPortuguese(descriptionPortuguese); - election.setMaxCandidates(maxCandidates); - election.setVotingLinkAvailable(votingLinkAvailable); - election.setResultLinkAvailable(resultLinkAvailable); - election.setAuditorLinkAvailable(auditorLinkAvailable); - election.setRevisionRequest(revisionRequest); - election.setOnlySp(onlySp); - election.setResultToken(resultToken); - election.setDefaultSender(defaultSender); - election.setElectorsSet(electorsSet); - election.setCandidatesSet(candidatesSet); - election.setAuditorsSet(auditorsSet); - election.setRandomOrderCandidates(randomOrderCandidates); - election.setDiffUTC(diffUTC); - election.setClosed(closed); - election.setAuxStartDate(auxStartDate); - election.setAuxStartHour(auxStartHour); - election.setAuxEndDate(auxEndDate); - election.setAuxEndHour(auxEndHour); + election.setMigrated(true); + election.setCreationDate(new DateTime(2024, 12, 1, 9, 0).toDate()); + election.setTitleSpanish("Elección de prueba en español"); + election.setTitleEnglish("Test Election in English"); + election.setTitlePortuguese("Eleições de teste em português"); + election.setLinkSpanish("http://example.com/es"); + election.setLinkEnglish("http://example.com/en"); + election.setLinkPortuguese("http://example.com/pt"); + election.setDescriptionSpanish("Descripción en español"); + election.setDescriptionEnglish("Description in English"); + election.setDescriptionPortuguese("Descrição em português"); + election.setMaxCandidates(5); + election.setVotingLinkAvailable(true); + election.setResultLinkAvailable(true); + election.setAuditorLinkAvailable(false); + election.setRevisionRequest(true); + election.setOnlySp(false); + election.setResultToken("result-token-123"); + election.setDefaultSender("noreply@example.com"); + election.setElectorsSet(true); + election.setCandidatesSet(true); + election.setAuditorsSet(false); + election.setRandomOrderCandidates(true); + election.setDiffUTC(3); + election.setClosed(false); + election.setAuxStartDate("25/12/2024"); + election.setAuxStartHour("10:00"); + election.setAuxEndDate("26/12/2024"); + election.setAuxEndHour("18:30"); election.setCandidates(createListCandidates()); election.setUserVoters(createUserVoters()); election.setAuditors(createAuditors()); @@ -115,6 +74,7 @@ public static Election createElection(long electionId) { return election; } + public static List createVotesList() { List votes = new ArrayList<>(); @@ -123,15 +83,30 @@ public static List createVotesList() { return votes; } - private static Vote createVote() { - + public static Vote createVote() { Vote vote = new Vote(); vote.setVoteId(1L); vote.setCode("123456"); + vote.setIp("192.168.0.1"); + vote.setVoteDate(new Date()); + + Candidate candidate = new Candidate(); + candidate.setCandidateId(10L); + vote.setCandidate(candidate); + + Election election = new Election(); + election.setElectionId(1001L); + vote.setElection(election); + + UserVoter userVoter = new UserVoter(); + userVoter.setUserVoterId(1L); + vote.setUserVoter(userVoter); + return vote; } + public static List createElectionEmailTemplatesList() { List emailTemplates = new ArrayList<>(); @@ -149,38 +124,28 @@ public static List createListCandidates() { } public static UserVoter createUserVoter() { - - long userVoterId = 1L; - Long migrationId = 100L; - boolean voted = true; - String voteToken = "TOKEN123"; - int voteAmount = 3; - String name = "John Doe"; - String mail = "john.doe@example.com"; - String country = "Uruguay"; - String language = "Spanish"; - String orgID = "ORG001"; - Date voteDate = new Date(); - int version = 1; - List votes = new ArrayList<>(); - String codesSummary = "Code1, Code2"; + Date voteDate = new DateTime(2024, 12, 1, 9, 0).toDate(); UserVoter userVoter = new UserVoter(); - userVoter.setUserVoterId(userVoterId); - userVoter.setMigrationId(migrationId); - userVoter.setElection(new Election()); - userVoter.setVoted(voted); - userVoter.setVoteToken(voteToken); - userVoter.setVoteAmount(voteAmount); - userVoter.setName(name); - userVoter.setMail(mail); - userVoter.setCountry(country); - userVoter.setLanguage(language); - userVoter.setOrgID(orgID); + userVoter.setUserVoterId(1L); + userVoter.setMigrationId(100L); + + Election election = new Election(200); + election.setCategory(ElectionCategory.TEST); + userVoter.setElection(election); + + userVoter.setVoted(true); + userVoter.setVoteToken("TOKEN123"); + userVoter.setVoteAmount(3); + userVoter.setName("John Doe"); + userVoter.setMail("john.doe@example.com"); + userVoter.setCountry("Uruguay"); + userVoter.setLanguage("Spanish"); + userVoter.setOrgID("ORG001"); userVoter.setVoteDate(voteDate); - userVoter.setVersion(version); - userVoter.setVotes(votes); - userVoter.setCodesSummary(codesSummary); + userVoter.setVersion(1); + userVoter.setVotes(new ArrayList<>()); + userVoter.setCodesSummary("Code1, Code2"); return userVoter; } @@ -199,6 +164,16 @@ public static Auditor createAuditor() { auditor.setAuditorId(1L); auditor.setName("Auditor 1"); auditor.setMail("auditor1@example.com"); + auditor.setMigrationId(123L); + auditor.setCommissioner(true); + auditor.setAgreedConformity(true); + auditor.setRevisionAvailable(true); + auditor.setResultToken("resultToken123"); + + Election election = new Election(200); + election.setElectionId(4567L); + election.setCategory(ElectionCategory.TEST); + auditor.setElection(election); return auditor; } @@ -217,6 +192,16 @@ public static ElectionEmailTemplate createElectionEmailTemplate() { emailTemplate.setTemplateType("REMINDER"); emailTemplate.setSubjectEN("Election Reminder"); emailTemplate.setBodyEN("Don't forget to vote!"); + emailTemplate.setSubjectSP("Recordatorio de elección"); + emailTemplate.setBodySP("¡No olvides votar!"); + emailTemplate.setSubjectPT("Lembrete de eleição"); + emailTemplate.setBodyPT("Não se esqueça de votar!"); + + Election election = new Election(); + election.setElectionId(123L); + emailTemplate.setElection(election); + + emailTemplate.setRecipientType(RecipientType.VOTERS); return emailTemplate; } @@ -299,46 +284,95 @@ public static ElectionLight createElectionLight() { } public static Candidate createCandidate() { + Candidate candidate = new Candidate(); - long candidateId = 1L; - String name = "Juan Perez"; - String mail = "juan.perez@mail.com"; - String pictureName = "profile.jpg"; - byte[] pictureInfo = new byte[] { 1, 2, 3 }; - String bioSpanish = "Biografía en español"; - String bioEnglish = "Biography in English"; - String bioPortuguese = "Biografia em português"; - String pictureExtension = "jpg"; - int candidateOrder = 1; - boolean onlySp = false; - String linkSpanish = "linkES"; - String linkEnglish = "linkEN"; - String linkPortuguese = "linkPT"; - Long migrationId = 123L; - List votes = new ArrayList<>(); + candidate.setCandidateId(1L); + candidate.setName("Juan Perez"); + candidate.setMail("juan.perez@mail.com"); + candidate.setPictureName("profile.jpg"); + candidate.setPictureInfo(new byte[] { 1, 2, 3 }); + candidate.setBioSpanish("Biografía en español"); + candidate.setBioEnglish("Biography in English"); + candidate.setBioPortuguese("Biografia em português"); + candidate.setPictureExtension("jpg"); + candidate.setCandidateOrder(1); + candidate.setOnlySp(false); + candidate.setLinkSpanish("linkES"); + candidate.setLinkEnglish("linkEN"); + candidate.setLinkPortuguese("linkPT"); Election election = new Election(200); - Candidate candidate = new Candidate(); - - // Set attributes - candidate.setCandidateId(candidateId); - candidate.setName(name); - candidate.setMail(mail); - candidate.setPictureName(pictureName); - candidate.setPictureInfo(pictureInfo); - candidate.setBioSpanish(bioSpanish); - candidate.setBioEnglish(bioEnglish); - candidate.setBioPortuguese(bioPortuguese); - candidate.setPictureExtension(pictureExtension); - candidate.setCandidateOrder(candidateOrder); - candidate.setOnlySp(onlySp); - candidate.setLinkSpanish(linkSpanish); - candidate.setLinkEnglish(linkEnglish); - candidate.setLinkPortuguese(linkPortuguese); + election.setCategory(ElectionCategory.TEST); candidate.setElection(election); - candidate.setMigrationId(migrationId); - candidate.setVotes(votes); + candidate.setMigrationId(123L); + candidate.setVotes(new ArrayList<>()); return candidate; } + + public static Activity createActivity() { + + Date timestamp = new DateTime(2024, 12, 1, 9, 0).toDate(); + Activity activity = new Activity(); + activity.setActivityId(14567L); + activity.setDescription("User auditor activity"); + activity.setUserName("testUser"); + activity.setTimestamp(timestamp); + activity.setActivityType(ActivityType.ADD_AUDITOR); + activity.setElectionId(456L); + activity.setIp("192.168.0.2"); + + return activity; + } + + public static Customization createCustomization() { + Customization customization = new Customization(); + + customization.setCustomizationId(1L); + customization.setPicSmallLogo("smallLogo.png"); + customization.setPicBigLogo("bigLogo.png"); + customization.setPicSymbol("symbol.png"); + customization.setContPicSmallLogo("smallLogoContent".getBytes()); + customization.setContPicBigLogo("bigLogoContent".getBytes()); + customization.setContPicSymbol("symbolContent".getBytes()); + customization.setSiteTitle("My Site"); + customization.setLoginTitle("Login to My Site"); + customization.setShowHome(true); + customization.setHomeHtml("Home content"); + + return customization; + } + + public static EmailHistory createEmailHistory() { + + Date createdDate = new DateTime(2024, 12, 1, 9, 0).toDate(); + EmailHistory emailHistory = new EmailHistory(); + + emailHistory.setEmailHistoryId(1L); + emailHistory.setRecipients("recipient@example.com"); + emailHistory.setSender("sender@example.com"); + emailHistory.setCc("cc@example.com"); + emailHistory.setBcc("bcc@example.com"); + emailHistory.setSubject("Test Subject"); + emailHistory.setBody("This is the email body."); + emailHistory.setSent(true); + emailHistory.setCreatedDate(createdDate); + emailHistory.setTemplateType("TestTemplate"); + emailHistory.setElectionId(12345L); + + return emailHistory; + } + + public static UserAdmin createUserAdmin() { + + UserAdmin userAdmin = new UserAdmin(); + userAdmin.setUserAdminId("admin123"); + userAdmin.setPassword("SECUREPASSWORD"); + userAdmin.setEmail("admin@example.com"); + userAdmin.setAuthorizedElectionId(1001L); + + return userAdmin; + } + + } diff --git a/pom.xml b/pom.xml index 0c15782..6b078d7 100644 --- a/pom.xml +++ b/pom.xml @@ -67,7 +67,8 @@ net/lacnic/elections/adminweb/app/ElectionsManagerApp.class - + sun/util/resources/cldr/provider/CLDRLocaleDataMetaInfo + @@ -99,6 +100,13 @@ pai-auth-ws-client 1.1.0 - + + + org.mockito + mockito-core + 3.12.4 + test + +