1 package club.wpia.gigi.dbObjects;
3 import static org.junit.Assert.*;
5 import java.io.IOException;
6 import java.sql.Timestamp;
8 import org.junit.Before;
11 import club.wpia.gigi.GigiApiException;
12 import club.wpia.gigi.database.GigiPreparedStatement;
13 import club.wpia.gigi.testUtils.BusinessTest;
14 import club.wpia.gigi.util.DayDate;
15 import club.wpia.gigi.util.Notary;
17 public class TestVerification extends BusinessTest {
19 private final Timestamp yesterday = new Timestamp(System.currentTimeMillis() - DayDate.MILLI_DAY);
21 private final Timestamp tomorrow = new Timestamp(System.currentTimeMillis() + DayDate.MILLI_DAY);
24 * at least 27 months ago, so is outside the window of
25 * {@link User#VERIFICATION_MONTHS}
27 private final Timestamp min27month = new Timestamp(System.currentTimeMillis() - DayDate.MILLI_DAY * 27 * 31);
30 * at least 24 months ago (but less than 27), so is inside the window of
31 * {@link User#VERIFICATION_MONTHS}
33 private final Timestamp min24month = new Timestamp(System.currentTimeMillis() - DayDate.MILLI_DAY * 24 * 31);
39 private int applicantID;
41 private int applicantNameID;
43 private User applicant;
45 private int applicantMultID;
47 public TestVerification() throws GigiApiException {
51 // test for verification in 39 month period
52 private void enterVerification(int agentID, int applicantID) {
53 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?")) {
54 ps.setInt(1, agentID);
55 ps.setInt(2, applicantID);
57 ps.setString(4, "test-location");
58 ps.setString(5, "2010-01-01");
64 private void enterVerificationExpired(int agentID, int applicantID, Timestamp expired) {
65 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?, `expire`=? ")) {
66 ps.setInt(1, agentID);
67 ps.setInt(2, applicantID);
69 ps.setString(4, "test-location");
70 ps.setString(5, "2010-01-01");
71 ps.setTimestamp(6, expired);
76 private void enterVerificationWhen(int agentID, int applicantID, Timestamp when) {
77 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?, `when`=? ")) {
78 ps.setInt(1, agentID);
79 ps.setInt(2, applicantID);
81 ps.setString(4, "test-location");
82 ps.setString(5, "2010-01-01");
83 ps.setTimestamp(6, when);
88 private void enterVericationWhen(int agentID, int applicantID, Timestamp when, int points) {
89 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?, `when`=? ")) {
90 ps.setInt(1, agentID);
91 ps.setInt(2, applicantID);
93 ps.setString(4, "test-location");
94 ps.setString(5, "2010-01-01");
95 ps.setTimestamp(6, when);
100 private void enterVerificationDeleted(int agentID, int applicantID, Timestamp deleted) {
101 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?, `deleted`=? ")) {
102 ps.setInt(1, agentID);
103 ps.setInt(2, applicantID);
105 ps.setString(4, "test-location");
106 ps.setString(5, "2010-01-01");
107 ps.setTimestamp(6, deleted);
113 public void initTest() throws GigiApiException {
114 agentID = createVerificationUser("a", "b", createUniqueName() + "@example.com", TEST_PASSWORD);
115 agent2ID = createVerificationUser("a", "d", createUniqueName() + "@example.com", TEST_PASSWORD);
116 applicantID = createVerifiedUser("a", "c", createUniqueName() + "@example.com", TEST_PASSWORD);
117 applicant = User.getById(applicantID);
118 applicantNameID = User.getById(applicantID).getPreferredName().getId();
119 applicantMultID = createVerifiedUser("a", "e", createUniqueName() + "@example.com", TEST_PASSWORD);
123 public void testVerificationYesterday() throws IOException {
124 enterVerificationWhen(agentID, applicantNameID, yesterday);
125 assertTrue(applicant.isInVerificationLimit());
129 public void testApprox24MonthAgo() throws IOException {
130 enterVerificationWhen(agentID, applicantNameID, min24month);
131 assertTrue(applicant.isInVerificationLimit());
135 public void testApprox39MonthAgo() throws IOException {
136 enterVerificationWhen(agentID, applicantNameID, min27month);
137 assertFalse(applicant.isInVerificationLimit());
141 public void testTomorrowExpired() throws IOException {
142 enterVerificationExpired(agentID, applicantNameID, tomorrow);
143 assertTrue(applicant.isInVerificationLimit());
147 public void testYesterdayExpired() throws IOException {
148 enterVerificationExpired(agentID, applicantNameID, yesterday);
149 assertFalse(applicant.isInVerificationLimit());
153 public void testNormal() throws IOException {
154 enterVerification(agentID, applicantNameID);
155 assertTrue(applicant.isInVerificationLimit());
159 public void testDeletedYesterday() throws IOException {
160 enterVerificationDeleted(agentID, applicantNameID, yesterday);
161 assertFalse(applicant.isInVerificationLimit());
165 public void testMultipleVerificationPossible() throws IOException {
166 User agent = User.getById(agentID);
167 User applicantMult = User.getById(applicantMultID);
169 enterVerificationWhen(agentID, applicantMult.getPreferredName().getId(), min27month);
171 // test that new entry would be possible
172 assertTrue(Notary.checkVerificationIsPossible(agent, applicantMult.getPreferredName()));
175 enterVerificationWhen(agentID, applicantMult.getPreferredName().getId(), yesterday);
177 // test that new entry is not possible
178 assertFalse(Notary.checkVerificationIsPossible(agent, applicantMult.getPreferredName()));
183 public void testMultipleVerificationPointsCalculation() throws IOException {
185 User agent = User.getById(agentID);
186 User applicantMult = User.getById(applicantMultID);
188 enterVerificationWhen(agentID, applicantMult.getPreferredName().getId(), min27month);
189 int xPoints = agent.getExperiencePoints();
191 // test that VP after first entry
193 assertEquals(applicantMult.getVerificationPoints(), 10);
195 // enter second entry to check correct calculation with larger points
196 enterVericationWhen(agentID, applicantMult.getPreferredName().getId(), min24month, 20);
197 assertEquals(applicantMult.getVerificationPoints(), 20);
199 // test correct XP calculation
200 assertEquals(agent.getExperiencePoints(), xPoints);
202 // enter third entry to check correct calculation with less points
203 enterVericationWhen(agentID, applicantMult.getPreferredName().getId(), yesterday, 15);
204 assertEquals(applicantMult.getVerificationPoints(), 15);
206 // test correct XP calculation
207 assertEquals(agent.getExperiencePoints(), xPoints);
209 // enter expired entry
210 enterVerificationExpired(agentID, applicantMult.getPreferredName().getId(), yesterday);
211 assertEquals(applicantMult.getVerificationPoints(), 15);
213 // enter deleted entry same agent
214 enterVerificationDeleted(agentID, applicantMult.getPreferredName().getId(), yesterday);
215 assertEquals(applicantMult.getVerificationPoints(), 15);
217 // enter expired entry future
218 enterVerificationExpired(agentID, applicantMult.getPreferredName().getId(), tomorrow);
219 assertEquals(applicantMult.getVerificationPoints(), 10);
221 // test correct XP calculation
222 assertEquals(agent.getExperiencePoints(), xPoints);
224 // enter entry from different agent
225 enterVerificationWhen(agent2ID, applicantMult.getPreferredName().getId(), yesterday);
226 assertEquals(applicantMult.getVerificationPoints(), 20);
228 // enter entry for second applicant
229 enterVerificationWhen(agentID, applicant.getPreferredName().getId(), yesterday);
231 assertEquals(agent.getExperiencePoints(), xPoints + User.EXPERIENCE_POINTS);