1 package org.cacert.gigi.dbObjects;
3 import static org.junit.Assert.*;
5 import java.io.IOException;
6 import java.sql.Timestamp;
8 import org.cacert.gigi.GigiApiException;
9 import org.cacert.gigi.database.GigiPreparedStatement;
10 import org.cacert.gigi.testUtils.BusinessTest;
11 import org.cacert.gigi.util.DayDate;
12 import org.cacert.gigi.util.Notary;
13 import org.junit.Before;
14 import org.junit.Test;
16 public class TestAssurance extends BusinessTest {
18 private final Timestamp yesterday = new Timestamp(System.currentTimeMillis() - DayDate.MILLI_DAY);
20 private final Timestamp tomorrow = new Timestamp(System.currentTimeMillis() + DayDate.MILLI_DAY);
23 * at least 39 months ago, so is outside the window of
24 * {@link User#VERIFICATION_MONTHS}
26 private final Timestamp min39month = new Timestamp(System.currentTimeMillis() - DayDate.MILLI_DAY * 39 * 31);
29 * at least 24 months ago (but less than 39), so is inside the window of
30 * {@link User#VERIFICATION_MONTHS}
32 private final Timestamp min24month = new Timestamp(System.currentTimeMillis() - DayDate.MILLI_DAY * 24 * 31);
38 private int applicantID;
40 private int applicantNameID;
42 private User applicant;
44 private int applicantMultID;
46 public TestAssurance() throws GigiApiException {
50 // test for verification in 39 month period
51 private void enterAssurance(int agentID, int applicantID) {
52 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?")) {
53 ps.setInt(1, agentID);
54 ps.setInt(2, applicantID);
56 ps.setString(4, "test-location");
57 ps.setString(5, "2010-01-01");
63 private void enterAssuranceExpired(int agentID, int applicantID, Timestamp expired) {
64 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?, `expire`=? ")) {
65 ps.setInt(1, agentID);
66 ps.setInt(2, applicantID);
68 ps.setString(4, "test-location");
69 ps.setString(5, "2010-01-01");
70 ps.setTimestamp(6, expired);
75 private void enterAssuranceWhen(int agentID, int applicantID, Timestamp when) {
76 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?, `when`=? ")) {
77 ps.setInt(1, agentID);
78 ps.setInt(2, applicantID);
80 ps.setString(4, "test-location");
81 ps.setString(5, "2010-01-01");
82 ps.setTimestamp(6, when);
87 private void enterAssuranceWhen(int agentID, int applicantID, Timestamp when, int points) {
88 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?, `when`=? ")) {
89 ps.setInt(1, agentID);
90 ps.setInt(2, applicantID);
92 ps.setString(4, "test-location");
93 ps.setString(5, "2010-01-01");
94 ps.setTimestamp(6, when);
99 private void enterAssuranceDeleted(int agentID, int applicantID, Timestamp deleted) {
100 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?, `deleted`=? ")) {
101 ps.setInt(1, agentID);
102 ps.setInt(2, applicantID);
104 ps.setString(4, "test-location");
105 ps.setString(5, "2010-01-01");
106 ps.setTimestamp(6, deleted);
112 public void initTest() throws GigiApiException {
113 agentID = createAssuranceUser("a", "b", createUniqueName() + "@example.com", TEST_PASSWORD);
114 agent2ID = createAssuranceUser("a", "d", createUniqueName() + "@example.com", TEST_PASSWORD);
115 applicantID = createVerifiedUser("a", "c", createUniqueName() + "@example.com", TEST_PASSWORD);
116 applicant = User.getById(applicantID);
117 applicantNameID = User.getById(applicantID).getPreferredName().getId();
118 applicantMultID = createVerifiedUser("a", "e", createUniqueName() + "@example.com", TEST_PASSWORD);
122 public void testVerificationYesterday() throws IOException {
123 enterAssuranceWhen(agentID, applicantNameID, yesterday);
124 assertTrue(applicant.isInVerificationLimit());
128 public void testApprox24MonthAgo() throws IOException {
129 enterAssuranceWhen(agentID, applicantNameID, min24month);
130 assertTrue(applicant.isInVerificationLimit());
134 public void testApprox39MonthAgo() throws IOException {
135 enterAssuranceWhen(agentID, applicantNameID, min39month);
136 assertFalse(applicant.isInVerificationLimit());
140 public void testTomorrowExpired() throws IOException {
141 enterAssuranceExpired(agentID, applicantNameID, tomorrow);
142 assertTrue(applicant.isInVerificationLimit());
146 public void testYesterdayExpired() throws IOException {
147 enterAssuranceExpired(agentID, applicantNameID, yesterday);
148 assertFalse(applicant.isInVerificationLimit());
152 public void testNormal() throws IOException {
153 enterAssurance(agentID, applicantNameID);
154 assertTrue(applicant.isInVerificationLimit());
158 public void testDeletedYesterday() throws IOException {
159 enterAssuranceDeleted(agentID, applicantNameID, yesterday);
160 assertFalse(applicant.isInVerificationLimit());
164 public void testMultipleAssurancePossible() throws IOException {
165 User agent = User.getById(agentID);
166 User applicantMult = User.getById(applicantMultID);
168 enterAssuranceWhen(agentID, applicantMult.getPreferredName().getId(), min39month);
170 // test that new entry would be possible
171 assertTrue(Notary.checkAssuranceIsPossible(agent, applicantMult.getPreferredName()));
174 enterAssuranceWhen(agentID, applicantMult.getPreferredName().getId(), yesterday);
176 // test that new entry is not possible
177 assertFalse(Notary.checkAssuranceIsPossible(agent, applicantMult.getPreferredName()));
182 public void testMultipleAssurancePointsCalculation() throws IOException {
184 User agent = User.getById(agentID);
185 User applicantMult = User.getById(applicantMultID);
187 enterAssuranceWhen(agentID, applicantMult.getPreferredName().getId(), min39month);
188 int xPoints = agent.getExperiencePoints();
190 // test that VP after first entry
192 assertEquals(applicantMult.getAssurancePoints(), 10);
194 // enter second entry to check correct calculation with larger points
195 enterAssuranceWhen(agentID, applicantMult.getPreferredName().getId(), min24month, 20);
196 assertEquals(applicantMult.getAssurancePoints(), 20);
198 // test correct XP calculation
199 assertEquals(agent.getExperiencePoints(), xPoints);
201 // enter third entry to check correct calculation with less points
202 enterAssuranceWhen(agentID, applicantMult.getPreferredName().getId(), yesterday, 15);
203 assertEquals(applicantMult.getAssurancePoints(), 15);
205 // test correct XP calculation
206 assertEquals(agent.getExperiencePoints(), xPoints);
208 // enter expired entry
209 enterAssuranceExpired(agentID, applicantMult.getPreferredName().getId(), yesterday);
210 assertEquals(applicantMult.getAssurancePoints(), 15);
212 // enter deleted entry same agent
213 enterAssuranceDeleted(agentID, applicantMult.getPreferredName().getId(), yesterday);
214 assertEquals(applicantMult.getAssurancePoints(), 15);
216 // enter expired entry future
217 enterAssuranceExpired(agentID, applicantMult.getPreferredName().getId(), tomorrow);
218 assertEquals(applicantMult.getAssurancePoints(), 10);
220 // test correct XP calculation
221 assertEquals(agent.getExperiencePoints(), xPoints);
223 // enter entry from different agent
224 enterAssuranceWhen(agent2ID, applicantMult.getPreferredName().getId(), yesterday);
225 assertEquals(applicantMult.getAssurancePoints(), 20);
227 // enter entry for second applicant
228 enterAssuranceWhen(agentID, applicant.getPreferredName().getId(), yesterday);
230 assertEquals(agent.getExperiencePoints(), xPoints + User.EXPERIENCE_POINTS);