Add more tests for like page
[Sone.git] / src / test / java / net / pterodactylus / sone / core / SoneModificationDetectorTest.java
1 package net.pterodactylus.sone.core;
2
3 import static java.util.concurrent.TimeUnit.SECONDS;
4 import static org.hamcrest.MatcherAssert.assertThat;
5 import static org.hamcrest.Matchers.is;
6 import static org.mockito.Mockito.mock;
7 import static org.mockito.Mockito.when;
8
9 import java.util.concurrent.atomic.AtomicInteger;
10
11 import net.pterodactylus.sone.core.SoneModificationDetector.LockableFingerprintProvider;
12
13 import com.google.common.base.Ticker;
14 import org.junit.Before;
15 import org.junit.Test;
16
17 /**
18  * Unit test for {@link SoneModificationDetector}.
19  *
20  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
21  */
22 public class SoneModificationDetectorTest {
23
24         private final Ticker ticker = mock(Ticker.class);
25         private final AtomicInteger insertionDelay = new AtomicInteger(60);
26         private final SoneModificationDetector soneModificationDetector;
27         private final LockableFingerprintProvider lockableFingerprintProvider = mock(LockableFingerprintProvider.class);
28
29         public SoneModificationDetectorTest() {
30                 when(lockableFingerprintProvider.getFingerprint()).thenReturn("original");
31                 when(lockableFingerprintProvider.isLocked()).thenReturn(false);
32                 soneModificationDetector = new SoneModificationDetector(ticker, lockableFingerprintProvider, insertionDelay);
33         }
34
35         private void modifySone() {
36                 modifySone("");
37         }
38
39         private void modifySone(String uniqueValue) {
40                 when(lockableFingerprintProvider.getFingerprint()).thenReturn("modified" + uniqueValue);
41         }
42
43         private void passTime(int seconds) {
44                 when(ticker.read()).thenReturn(SECONDS.toNanos(seconds));
45         }
46
47         private void lockSone() {
48                 when(lockableFingerprintProvider.isLocked()).thenReturn(true);
49         }
50
51         private void unlockSone() {
52                 when(lockableFingerprintProvider.isLocked()).thenReturn(false);
53         }
54
55         @Before
56         public void setupOriginalFingerprint() {
57             soneModificationDetector.setFingerprint("original");
58         }
59
60         @Test
61         public void normalConstructorCanBeCalled() {
62                 new SoneModificationDetector(lockableFingerprintProvider, insertionDelay);
63         }
64
65         @Test
66         public void sonesStartOutAsNotEligible() {
67                 assertThat(soneModificationDetector.isModified(), is(false));
68                 assertThat(soneModificationDetector.isEligibleForInsert(), is(false));
69         }
70
71         @Test
72         public void originalFingerprintIsRetained() {
73                 assertThat(soneModificationDetector.getLastInsertFingerprint(), is("original"));
74         }
75
76         @Test
77         public void modifiedSoneIsEligibleAfter60Seconds() {
78                 modifySone();
79                 assertThat(soneModificationDetector.isModified(), is(true));
80                 assertThat(soneModificationDetector.isEligibleForInsert(), is(false));
81                 passTime(100);
82                 assertThat(soneModificationDetector.isModified(), is(true));
83                 assertThat(soneModificationDetector.isEligibleForInsert(), is(true));
84         }
85
86         @Test
87         public void modifiedAndRemodifiedSoneIsEligibleAfter90Seconds() {
88                 modifySone();
89                 assertThat(soneModificationDetector.isModified(), is(true));
90                 assertThat(soneModificationDetector.isEligibleForInsert(), is(false));
91                 passTime(30);
92                 modifySone("2");
93                 assertThat(soneModificationDetector.isModified(), is(true));
94                 assertThat(soneModificationDetector.isEligibleForInsert(), is(false));
95                 passTime(61);
96                 assertThat(soneModificationDetector.isModified(), is(true));
97                 assertThat(soneModificationDetector.isEligibleForInsert(), is(false));
98                 passTime(91);
99                 assertThat(soneModificationDetector.isModified(), is(true));
100                 assertThat(soneModificationDetector.isEligibleForInsert(), is(true));
101         }
102
103         @Test
104         public void modifiedSoneIsNotEligibleAfter30Seconds() {
105                 modifySone();
106                 passTime(30);
107                 assertThat(soneModificationDetector.isEligibleForInsert(), is(false));
108         }
109
110         @Test
111         public void lockedAndModifiedSoneIsNotEligibleAfter60Seconds() {
112                 lockSone();
113                 assertThat(soneModificationDetector.isEligibleForInsert(), is(false));
114                 modifySone();
115                 assertThat(soneModificationDetector.isEligibleForInsert(), is(false));
116                 passTime(100);
117                 assertThat(soneModificationDetector.isEligibleForInsert(), is(false));
118         }
119
120         @Test
121         public void lockingAndUnlockingASoneRestartsTheWaitPeriod() {
122                 modifySone();
123                 lockSone();
124                 passTime(30);
125                 assertThat(soneModificationDetector.isEligibleForInsert(), is(false));
126                 unlockSone();
127                 assertThat(soneModificationDetector.isEligibleForInsert(), is(false));
128                 passTime(60);
129                 assertThat(soneModificationDetector.isEligibleForInsert(), is(false));
130                 passTime(90);
131                 assertThat(soneModificationDetector.isEligibleForInsert(), is(true));
132         }
133
134         @Test
135         public void settingFingerprintWillResetTheEligibility() {
136                 modifySone();
137                 assertThat(soneModificationDetector.isEligibleForInsert(), is(false));
138                 passTime(100);
139                 assertThat(soneModificationDetector.isEligibleForInsert(), is(true));
140                 soneModificationDetector.setFingerprint("modified");
141                 assertThat(soneModificationDetector.isEligibleForInsert(), is(false));
142         }
143
144         @Test
145         public void changingInsertionDelayWillInfluenceEligibility() {
146                 modifySone();
147                 assertThat(soneModificationDetector.isEligibleForInsert(), is(false));
148                 passTime(100);
149                 assertThat(soneModificationDetector.isEligibleForInsert(), is(true));
150                 insertionDelay.set(120);
151                 assertThat(soneModificationDetector.isEligibleForInsert(), is(false));
152         }
153
154         @Test
155         public void soneWithoutOriginalFingerprintIsNotEligibleAfter59Seconds() {
156                 SoneModificationDetector soneModificationDetector = createDetectorWithoutOriginalFingerprint();
157                 assertThat(soneModificationDetector.isEligibleForInsert(), is(false));
158                 passTime(59);
159                 assertThat(soneModificationDetector.isEligibleForInsert(), is(false));
160         }
161
162         private SoneModificationDetector createDetectorWithoutOriginalFingerprint() {
163                 return new SoneModificationDetector(ticker, new LockableFingerprintProvider() {
164                         @Override
165                         public boolean isLocked() {
166                                 return false;
167                         }
168
169                         @Override
170                         public String getFingerprint() {
171                                 return "changed";
172                         }
173                 }, insertionDelay);
174         }
175
176         @Test
177         public void soneWithoutOriginalFingerprintIsEligibleAfter60Seconds() {
178                 SoneModificationDetector soneModificationDetector = createDetectorWithoutOriginalFingerprint();
179                 assertThat(soneModificationDetector.isEligibleForInsert(), is(false));
180                 passTime(60);
181                 assertThat(soneModificationDetector.isEligibleForInsert(), is(true));
182         }
183
184 }