This repository has been archived by the owner on Dec 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 304
/
StoreNetworkBeforeStaleTest.java
132 lines (108 loc) · 4.64 KB
/
StoreNetworkBeforeStaleTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package com.nytimes.android.external.fs3;
import com.nytimes.android.external.store3.base.Fetcher;
import com.nytimes.android.external.store3.base.RecordState;
import com.nytimes.android.external.store3.base.impl.BarCode;
import com.nytimes.android.external.store3.base.impl.Store;
import com.nytimes.android.external.store3.base.impl.StoreBuilder;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InOrder;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.runners.MockitoJUnitRunner;
import io.reactivex.Maybe;
import io.reactivex.Single;
import okio.BufferedSource;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public class StoreNetworkBeforeStaleTest {
Exception sorry = new Exception("sorry");
@Mock
Fetcher<BufferedSource, BarCode> fetcher;
@Mock
RecordPersister persister;
@Mock
BufferedSource network1;
@Mock
BufferedSource network2;
@Mock
BufferedSource disk1;
@Mock
BufferedSource disk2;
private final BarCode barCode = new BarCode("key", "value");
private Store<BufferedSource, BarCode> store;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
store = StoreBuilder.<BufferedSource>barcode()
.fetcher(fetcher)
.persister(persister)
.networkBeforeStale()
.open();
}
@Test
public void networkBeforeDiskWhenStale() {
when(fetcher.fetch(barCode))
.thenReturn(Single.<BufferedSource>error(new Exception()));
when(persister.read(barCode))
.thenReturn(Maybe.just(disk1)); //get should return from disk
when(persister.getRecordState(barCode)).thenReturn(RecordState.STALE);
when(persister.write(barCode, network1))
.thenReturn(Single.just(true));
store.get(barCode).test().awaitTerminalEvent();
InOrder inOrder = inOrder(fetcher, persister);
inOrder.verify(fetcher, times(1)).fetch(barCode);
inOrder.verify(persister, times(1)).read(barCode);
verify(persister, never()).write(barCode, network1);
}
@Test
public void noNetworkBeforeStaleWhenMissingRecord() {
when(fetcher.fetch(barCode))
.thenReturn(Single.just(network1));
when(persister.read(barCode))
.thenReturn(Maybe.<BufferedSource>empty(), Maybe.just(disk1)); //first call should return
// empty, second call after network should return the network value
when(persister.getRecordState(barCode)).thenReturn(RecordState.MISSING);
when(persister.write(barCode, network1))
.thenReturn(Single.just(true));
store.get(barCode).test().awaitTerminalEvent();
InOrder inOrder = inOrder(fetcher, persister);
inOrder.verify(persister, times(1)).read(barCode);
inOrder.verify(fetcher, times(1)).fetch(barCode);
inOrder.verify(persister, times(1)).write(barCode, network1);
inOrder.verify(persister, times(1)).read(barCode);
}
@Test
public void noNetworkBeforeStaleWhenFreshRecord() {
when(persister.read(barCode))
.thenReturn(Maybe.just(disk1)); //get should return from disk
when(persister.getRecordState(barCode)).thenReturn(RecordState.FRESH);
store.get(barCode).test().awaitTerminalEvent();
verify(fetcher, never()).fetch(barCode);
verify(persister, never()).write(barCode, network1);
verify(persister, times(1)).read(barCode);
}
@Test
public void networkBeforeStaleNoNetworkResponse() {
Single<BufferedSource> singleError = Single.error(sorry);
Maybe<BufferedSource> maybeError = Maybe.error(sorry);
when(fetcher.fetch(barCode))
.thenReturn(singleError);
when(persister.read(barCode))
.thenReturn(maybeError, maybeError); //first call should return
// empty, second call after network should return the network value
when(persister.getRecordState(barCode)).thenReturn(RecordState.MISSING);
when(persister.write(barCode, network1))
.thenReturn(Single.just(true));
store.get(barCode).test().assertError(sorry);
InOrder inOrder = inOrder(fetcher, persister);
inOrder.verify(persister, times(1)).read(barCode);
inOrder.verify(fetcher, times(1)).fetch(barCode);
inOrder.verify(persister, times(1)).read(barCode);
}
}