]> WPIA git - gigi.git/blob - tests/org/cacert/gigi/dbObjects/TestAssurance.java
fix: ResultSet.getDate is often wrong as it fetches day-precision times
[gigi.git] / tests / org / cacert / gigi / dbObjects / TestAssurance.java
1 package org.cacert.gigi.dbObjects;
2
3 import static org.junit.Assert.*;
4
5 import java.io.IOException;
6 import java.sql.Timestamp;
7
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;
15
16 public class TestAssurance extends BusinessTest {
17
18     private final Timestamp yesterday = new Timestamp(System.currentTimeMillis() - DayDate.MILLI_DAY);
19
20     private final Timestamp tomorrow = new Timestamp(System.currentTimeMillis() + DayDate.MILLI_DAY);
21
22     /**
23      * at least 39 months ago, so is outside the window of
24      * {@link User#VERIFICATION_MONTHS}
25      */
26     private final Timestamp min39month = new Timestamp(System.currentTimeMillis() - DayDate.MILLI_DAY * 39 * 31);
27
28     /**
29      * at least 24 months ago (but less than 39), so is inside the window of
30      * {@link User#VERIFICATION_MONTHS}
31      */
32     private final Timestamp min24month = new Timestamp(System.currentTimeMillis() - DayDate.MILLI_DAY * 24 * 31);
33
34     private int agentID;
35
36     private int agent2ID;
37
38     private int applicantID;
39
40     private int applicantNameID;
41
42     private User applicant;
43
44     private int applicantMultID;
45
46     public TestAssurance() throws GigiApiException {
47
48     }
49
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);
55             ps.setInt(3, 10);
56             ps.setString(4, "test-location");
57             ps.setString(5, "2010-01-01");
58
59             ps.execute();
60         }
61     }
62
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);
67             ps.setInt(3, 10);
68             ps.setString(4, "test-location");
69             ps.setString(5, "2010-01-01");
70             ps.setTimestamp(6, expired);
71             ps.execute();
72         }
73     }
74
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);
79             ps.setInt(3, 10);
80             ps.setString(4, "test-location");
81             ps.setString(5, "2010-01-01");
82             ps.setTimestamp(6, when);
83             ps.execute();
84         }
85     }
86
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);
91             ps.setInt(3, points);
92             ps.setString(4, "test-location");
93             ps.setString(5, "2010-01-01");
94             ps.setTimestamp(6, when);
95             ps.execute();
96         }
97     }
98
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);
103             ps.setInt(3, 10);
104             ps.setString(4, "test-location");
105             ps.setString(5, "2010-01-01");
106             ps.setTimestamp(6, deleted);
107             ps.execute();
108         }
109     }
110
111     @Before
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);
119     }
120
121     @Test
122     public void testVerificationYesterday() throws IOException {
123         enterAssuranceWhen(agentID, applicantNameID, yesterday);
124         assertTrue(applicant.isInVerificationLimit());
125     }
126
127     @Test
128     public void testApprox24MonthAgo() throws IOException {
129         enterAssuranceWhen(agentID, applicantNameID, min24month);
130         assertTrue(applicant.isInVerificationLimit());
131     }
132
133     @Test
134     public void testApprox39MonthAgo() throws IOException {
135         enterAssuranceWhen(agentID, applicantNameID, min39month);
136         assertFalse(applicant.isInVerificationLimit());
137     }
138
139     @Test
140     public void testTomorrowExpired() throws IOException {
141         enterAssuranceExpired(agentID, applicantNameID, tomorrow);
142         assertTrue(applicant.isInVerificationLimit());
143     }
144
145     @Test
146     public void testYesterdayExpired() throws IOException {
147         enterAssuranceExpired(agentID, applicantNameID, yesterday);
148         assertFalse(applicant.isInVerificationLimit());
149     }
150
151     @Test
152     public void testNormal() throws IOException {
153         enterAssurance(agentID, applicantNameID);
154         assertTrue(applicant.isInVerificationLimit());
155     }
156
157     @Test
158     public void testDeletedYesterday() throws IOException {
159         enterAssuranceDeleted(agentID, applicantNameID, yesterday);
160         assertFalse(applicant.isInVerificationLimit());
161     }
162
163     @Test
164     public void testMultipleAssurancePossible() throws IOException {
165         User agent = User.getById(agentID);
166         User applicantMult = User.getById(applicantMultID);
167
168         enterAssuranceWhen(agentID, applicantMult.getPreferredName().getId(), min39month);
169
170         // test that new entry would be possible
171         assertTrue(Notary.checkAssuranceIsPossible(agent, applicantMult.getPreferredName()));
172
173         // enter new entry
174         enterAssuranceWhen(agentID, applicantMult.getPreferredName().getId(), yesterday);
175
176         // test that new entry is not possible
177         assertFalse(Notary.checkAssuranceIsPossible(agent, applicantMult.getPreferredName()));
178
179     }
180
181     @Test
182     public void testMultipleAssurancePointsCalculation() throws IOException {
183
184         User agent = User.getById(agentID);
185         User applicantMult = User.getById(applicantMultID);
186
187         enterAssuranceWhen(agentID, applicantMult.getPreferredName().getId(), min39month);
188         int xPoints = agent.getExperiencePoints();
189
190         // test that VP after first entry
191
192         assertEquals(applicantMult.getAssurancePoints(), 10);
193
194         // enter second entry to check correct calculation with larger points
195         enterAssuranceWhen(agentID, applicantMult.getPreferredName().getId(), min24month, 20);
196         assertEquals(applicantMult.getAssurancePoints(), 20);
197
198         // test correct XP calculation
199         assertEquals(agent.getExperiencePoints(), xPoints);
200
201         // enter third entry to check correct calculation with less points
202         enterAssuranceWhen(agentID, applicantMult.getPreferredName().getId(), yesterday, 15);
203         assertEquals(applicantMult.getAssurancePoints(), 15);
204
205         // test correct XP calculation
206         assertEquals(agent.getExperiencePoints(), xPoints);
207
208         // enter expired entry
209         enterAssuranceExpired(agentID, applicantMult.getPreferredName().getId(), yesterday);
210         assertEquals(applicantMult.getAssurancePoints(), 15);
211
212         // enter deleted entry same agent
213         enterAssuranceDeleted(agentID, applicantMult.getPreferredName().getId(), yesterday);
214         assertEquals(applicantMult.getAssurancePoints(), 15);
215
216         // enter expired entry future
217         enterAssuranceExpired(agentID, applicantMult.getPreferredName().getId(), tomorrow);
218         assertEquals(applicantMult.getAssurancePoints(), 10);
219
220         // test correct XP calculation
221         assertEquals(agent.getExperiencePoints(), xPoints);
222
223         // enter entry from different agent
224         enterAssuranceWhen(agent2ID, applicantMult.getPreferredName().getId(), yesterday);
225         assertEquals(applicantMult.getAssurancePoints(), 20);
226
227         // enter entry for second applicant
228         enterAssuranceWhen(agentID, applicant.getPreferredName().getId(), yesterday);
229
230         assertEquals(agent.getExperiencePoints(), xPoints + User.EXPERIENCE_POINTS);
231     }
232 }