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.dbObjects.User;
14 import club.wpia.gigi.testUtils.BusinessTest;
15 import club.wpia.gigi.util.DayDate;
16 import club.wpia.gigi.util.Notary;
18 public class TestVerification extends BusinessTest {
20 private final Timestamp yesterday = new Timestamp(System.currentTimeMillis() - DayDate.MILLI_DAY);
22 private final Timestamp tomorrow = new Timestamp(System.currentTimeMillis() + DayDate.MILLI_DAY);
25 * at least 39 months ago, so is outside the window of
26 * {@link User#VERIFICATION_MONTHS}
28 private final Timestamp min39month = new Timestamp(System.currentTimeMillis() - DayDate.MILLI_DAY * 39 * 31);
31 * at least 24 months ago (but less than 39), so is inside the window of
32 * {@link User#VERIFICATION_MONTHS}
34 private final Timestamp min24month = new Timestamp(System.currentTimeMillis() - DayDate.MILLI_DAY * 24 * 31);
40 private int applicantID;
42 private int applicantNameID;
44 private User applicant;
46 private int applicantMultID;
48 public TestVerification() throws GigiApiException {
52 // test for verification in 39 month period
53 private void enterVerification(int agentID, int applicantID) {
54 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?")) {
55 ps.setInt(1, agentID);
56 ps.setInt(2, applicantID);
58 ps.setString(4, "test-location");
59 ps.setString(5, "2010-01-01");
65 private void enterVerificationExpired(int agentID, int applicantID, Timestamp expired) {
66 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?, `expire`=? ")) {
67 ps.setInt(1, agentID);
68 ps.setInt(2, applicantID);
70 ps.setString(4, "test-location");
71 ps.setString(5, "2010-01-01");
72 ps.setTimestamp(6, expired);
77 private void enterVerificationWhen(int agentID, int applicantID, Timestamp when) {
78 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?, `when`=? ")) {
79 ps.setInt(1, agentID);
80 ps.setInt(2, applicantID);
82 ps.setString(4, "test-location");
83 ps.setString(5, "2010-01-01");
84 ps.setTimestamp(6, when);
89 private void enterVericationWhen(int agentID, int applicantID, Timestamp when, int points) {
90 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?, `when`=? ")) {
91 ps.setInt(1, agentID);
92 ps.setInt(2, applicantID);
94 ps.setString(4, "test-location");
95 ps.setString(5, "2010-01-01");
96 ps.setTimestamp(6, when);
101 private void enterVerificationDeleted(int agentID, int applicantID, Timestamp deleted) {
102 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?, `deleted`=? ")) {
103 ps.setInt(1, agentID);
104 ps.setInt(2, applicantID);
106 ps.setString(4, "test-location");
107 ps.setString(5, "2010-01-01");
108 ps.setTimestamp(6, deleted);
114 public void initTest() throws GigiApiException {
115 agentID = createVerificationUser("a", "b", createUniqueName() + "@example.com", TEST_PASSWORD);
116 agent2ID = createVerificationUser("a", "d", createUniqueName() + "@example.com", TEST_PASSWORD);
117 applicantID = createVerifiedUser("a", "c", createUniqueName() + "@example.com", TEST_PASSWORD);
118 applicant = User.getById(applicantID);
119 applicantNameID = User.getById(applicantID).getPreferredName().getId();
120 applicantMultID = createVerifiedUser("a", "e", createUniqueName() + "@example.com", TEST_PASSWORD);
124 public void testVerificationYesterday() throws IOException {
125 enterVerificationWhen(agentID, applicantNameID, yesterday);
126 assertTrue(applicant.isInVerificationLimit());
130 public void testApprox24MonthAgo() throws IOException {
131 enterVerificationWhen(agentID, applicantNameID, min24month);
132 assertTrue(applicant.isInVerificationLimit());
136 public void testApprox39MonthAgo() throws IOException {
137 enterVerificationWhen(agentID, applicantNameID, min39month);
138 assertFalse(applicant.isInVerificationLimit());
142 public void testTomorrowExpired() throws IOException {
143 enterVerificationExpired(agentID, applicantNameID, tomorrow);
144 assertTrue(applicant.isInVerificationLimit());
148 public void testYesterdayExpired() throws IOException {
149 enterVerificationExpired(agentID, applicantNameID, yesterday);
150 assertFalse(applicant.isInVerificationLimit());
154 public void testNormal() throws IOException {
155 enterVerification(agentID, applicantNameID);
156 assertTrue(applicant.isInVerificationLimit());
160 public void testDeletedYesterday() throws IOException {
161 enterVerificationDeleted(agentID, applicantNameID, yesterday);
162 assertFalse(applicant.isInVerificationLimit());
166 public void testMultipleVerificationPossible() throws IOException {
167 User agent = User.getById(agentID);
168 User applicantMult = User.getById(applicantMultID);
170 enterVerificationWhen(agentID, applicantMult.getPreferredName().getId(), min39month);
172 // test that new entry would be possible
173 assertTrue(Notary.checkVerificationIsPossible(agent, applicantMult.getPreferredName()));
176 enterVerificationWhen(agentID, applicantMult.getPreferredName().getId(), yesterday);
178 // test that new entry is not possible
179 assertFalse(Notary.checkVerificationIsPossible(agent, applicantMult.getPreferredName()));
184 public void testMultipleVerificationPointsCalculation() throws IOException {
186 User agent = User.getById(agentID);
187 User applicantMult = User.getById(applicantMultID);
189 enterVerificationWhen(agentID, applicantMult.getPreferredName().getId(), min39month);
190 int xPoints = agent.getExperiencePoints();
192 // test that VP after first entry
194 assertEquals(applicantMult.getVerificationPoints(), 10);
196 // enter second entry to check correct calculation with larger points
197 enterVericationWhen(agentID, applicantMult.getPreferredName().getId(), min24month, 20);
198 assertEquals(applicantMult.getVerificationPoints(), 20);
200 // test correct XP calculation
201 assertEquals(agent.getExperiencePoints(), xPoints);
203 // enter third entry to check correct calculation with less points
204 enterVericationWhen(agentID, applicantMult.getPreferredName().getId(), yesterday, 15);
205 assertEquals(applicantMult.getVerificationPoints(), 15);
207 // test correct XP calculation
208 assertEquals(agent.getExperiencePoints(), xPoints);
210 // enter expired entry
211 enterVerificationExpired(agentID, applicantMult.getPreferredName().getId(), yesterday);
212 assertEquals(applicantMult.getVerificationPoints(), 15);
214 // enter deleted entry same agent
215 enterVerificationDeleted(agentID, applicantMult.getPreferredName().getId(), yesterday);
216 assertEquals(applicantMult.getVerificationPoints(), 15);
218 // enter expired entry future
219 enterVerificationExpired(agentID, applicantMult.getPreferredName().getId(), tomorrow);
220 assertEquals(applicantMult.getVerificationPoints(), 10);
222 // test correct XP calculation
223 assertEquals(agent.getExperiencePoints(), xPoints);
225 // enter entry from different agent
226 enterVerificationWhen(agent2ID, applicantMult.getPreferredName().getId(), yesterday);
227 assertEquals(applicantMult.getVerificationPoints(), 20);
229 // enter entry for second applicant
230 enterVerificationWhen(agentID, applicant.getPreferredName().getId(), yesterday);
232 assertEquals(agent.getExperiencePoints(), xPoints + User.EXPERIENCE_POINTS);