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 User applicant;
42 private int applicantMultID;
44 public TestAssurance() throws GigiApiException {
48 // test for verification in 39 month period
49 private void enterAssurance(int agentID, int applicantID) {
50 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?")) {
51 ps.setInt(1, agentID);
52 ps.setInt(2, applicantID);
54 ps.setString(4, "test-location");
55 ps.setString(5, "2010-01-01");
61 private void enterAssuranceExpired(int agentID, int applicantID, Timestamp expired) {
62 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?, `expire`=? ")) {
63 ps.setInt(1, agentID);
64 ps.setInt(2, applicantID);
66 ps.setString(4, "test-location");
67 ps.setString(5, "2010-01-01");
68 ps.setTimestamp(6, expired);
73 private void enterAssuranceWhen(int agentID, int applicantID, Timestamp when) {
74 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?, `when`=? ")) {
75 ps.setInt(1, agentID);
76 ps.setInt(2, applicantID);
78 ps.setString(4, "test-location");
79 ps.setString(5, "2010-01-01");
80 ps.setTimestamp(6, when);
85 private void enterAssuranceWhen(int agentID, int applicantID, Timestamp when, int points) {
86 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?, `when`=? ")) {
87 ps.setInt(1, agentID);
88 ps.setInt(2, applicantID);
90 ps.setString(4, "test-location");
91 ps.setString(5, "2010-01-01");
92 ps.setTimestamp(6, when);
97 private void enterAssuranceDeleted(int agentID, int applicantID, Timestamp deleted) {
98 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?, `deleted`=? ")) {
99 ps.setInt(1, agentID);
100 ps.setInt(2, applicantID);
102 ps.setString(4, "test-location");
103 ps.setString(5, "2010-01-01");
104 ps.setTimestamp(6, deleted);
110 public void initTest() throws GigiApiException {
111 agentID = createAssuranceUser("a", "b", createUniqueName() + "@example.com", TEST_PASSWORD);
112 agent2ID = createAssuranceUser("a", "d", createUniqueName() + "@example.com", TEST_PASSWORD);
113 applicantID = createVerifiedUser("a", "c", createUniqueName() + "@example.com", TEST_PASSWORD);
114 applicant = User.getById(applicantID);
115 applicantMultID = createVerifiedUser("a", "e", createUniqueName() + "@example.com", TEST_PASSWORD);
119 public void testVerificationYesterday() throws IOException {
120 enterAssuranceWhen(agentID, applicantID, yesterday);
121 assertTrue(applicant.isInVerificationLimit());
125 public void testApprox24MonthAgo() throws IOException {
126 enterAssuranceWhen(agentID, applicantID, min24month);
127 assertTrue(applicant.isInVerificationLimit());
131 public void testApprox39MonthAgo() throws IOException {
132 enterAssuranceWhen(agentID, applicantID, min39month);
133 assertFalse(applicant.isInVerificationLimit());
137 public void testTomorrowExpired() throws IOException {
138 enterAssuranceExpired(agentID, applicantID, tomorrow);
139 assertTrue(applicant.isInVerificationLimit());
143 public void testYesterdayExpired() throws IOException {
144 enterAssuranceExpired(agentID, applicantID, yesterday);
145 assertFalse(applicant.isInVerificationLimit());
149 public void testNormal() throws IOException {
150 enterAssurance(agentID, applicantID);
151 assertTrue(applicant.isInVerificationLimit());
155 public void testDeletedYesterday() throws IOException {
156 enterAssuranceDeleted(agentID, applicantID, yesterday);
157 assertFalse(applicant.isInVerificationLimit());
161 public void testMultipleAssurancePossible() throws IOException {
163 User agent = User.getById(agentID);
164 User applicantMult = User.getById(applicantMultID);
166 enterAssuranceWhen(agentID, applicantMultID, min39month);
168 // test that new entry would be possible
170 Notary.checkAssuranceIsPossible(agent, applicantMult);
171 } catch (GigiApiException e) {
176 enterAssuranceWhen(agentID, applicantMultID, yesterday);
178 // test that new entry is not possible
180 Notary.checkAssuranceIsPossible(agent, applicantMult);
181 } catch (GigiApiException e) {
188 public void testMultipleAssurancePointsCalculation() throws IOException {
190 User agent = User.getById(agentID);
191 User applicantMult = User.getById(applicantMultID);
193 enterAssuranceWhen(agentID, applicantMultID, min39month);
195 int xPoints = agent.getExperiencePoints();
197 // test that VP after first entry
199 assertEquals(applicantMult.getAssurancePoints(), 10);
201 // enter second entry to check correct calculation with larger points
202 enterAssuranceWhen(agentID, applicantMultID, min24month, 20);
203 assertEquals(applicantMult.getAssurancePoints(), 20);
205 // test correct XP calculation
206 assertEquals(agent.getExperiencePoints(), xPoints);
208 // enter third entry to check correct calculation with less points
209 enterAssuranceWhen(agentID, applicantMultID, yesterday, 15);
210 assertEquals(applicantMult.getAssurancePoints(), 15);
212 // test correct XP calculation
213 assertEquals(agent.getExperiencePoints(), xPoints);
215 // enter expired entry
216 enterAssuranceExpired(agentID, applicantMultID, yesterday);
217 assertEquals(applicantMult.getAssurancePoints(), 15);
219 // enter deleted entry same agent
220 enterAssuranceDeleted(agentID, applicantMultID, yesterday);
221 assertEquals(applicantMult.getAssurancePoints(), 15);
223 // enter expired entry future
224 enterAssuranceExpired(agentID, applicantMultID, tomorrow);
225 assertEquals(applicantMult.getAssurancePoints(), 10);
227 // test correct XP calculation
228 assertEquals(agent.getExperiencePoints(), xPoints);
230 // enter entry from different agent
231 enterAssuranceWhen(agent2ID, applicantMultID, yesterday);
232 assertEquals(applicantMult.getAssurancePoints(), 20);
234 // enter entry for second applicant
235 enterAssuranceWhen(agentID, applicantID, yesterday);
236 assertEquals(agent.getExperiencePoints(), xPoints + 2);