@@ -10,35 +10,28 @@ import org.junit.Before
10
10
import org.junit.Test
11
11
import org.mockito.ArgumentMatchers.any
12
12
import org.mockito.ArgumentMatchers.argThat
13
- import org.mockito.ArgumentMatchers.eq
14
13
import org.mockito.Mock
15
- import org.mockito.Mockito.mockStatic
16
14
import org.mockito.Mockito.verify
17
15
import org.mockito.Mockito.`when`
18
16
import org.mockito.MockitoAnnotations
19
- import java.util.concurrent.CountDownLatch
20
- import java.util.concurrent.atomic.AtomicReference
21
17
22
18
23
19
class EncryptionMigrationTaskTest {
24
20
25
21
private val mApiKey: String = " abcdedfghijklmnopqrstuvwxyz"
22
+
26
23
@Mock
27
24
private lateinit var mSplitDatabase: SplitRoomDatabase
25
+
28
26
@Mock
29
27
private lateinit var mGeneralInfoDao: GeneralInfoDao
30
- @Mock
31
- private lateinit var mSplitCipherReference: AtomicReference <SplitCipher >
32
- @Mock
33
- private lateinit var mFromCipher: SplitCipher
28
+
34
29
@Mock
35
30
private lateinit var mToCipher: SplitCipher
36
- private lateinit var mCountDownLatch: CountDownLatch
37
31
38
32
@Before
39
33
fun setUp () {
40
34
MockitoAnnotations .openMocks(this )
41
- mCountDownLatch = CountDownLatch (1 )
42
35
`when `(mSplitDatabase.generalInfoDao()).thenReturn(mGeneralInfoDao)
43
36
}
44
37
@@ -47,9 +40,8 @@ class EncryptionMigrationTaskTest {
47
40
val encryptionMigrationTask = EncryptionMigrationTask (
48
41
mApiKey,
49
42
mSplitDatabase,
50
- mSplitCipherReference,
51
- mCountDownLatch,
52
- false
43
+ false ,
44
+ mToCipher
53
45
)
54
46
`when `(mGeneralInfoDao.getByName(" databaseEncryptionMode" )).thenReturn(
55
47
GeneralInfoEntity (
@@ -68,106 +60,30 @@ class EncryptionMigrationTaskTest {
68
60
val encryptionMigrationTask = EncryptionMigrationTask (
69
61
mApiKey,
70
62
mSplitDatabase,
71
- mSplitCipherReference,
72
- mCountDownLatch,
73
- true
63
+ true ,
64
+ mToCipher
74
65
)
75
66
76
- mockStatic(SplitCipherFactory ::class .java).use { mock ->
77
- mock.`when `<Any > {
78
- SplitCipherFactory .create(
79
- any(), eq(SplitEncryptionLevel .AES_128_CBC )
80
- )
81
- }.thenReturn(mToCipher)
82
-
83
- encryptionMigrationTask.execute()
67
+ encryptionMigrationTask.execute()
84
68
85
- mock.verify { SplitCipherFactory .create(mApiKey, SplitEncryptionLevel .AES_128_CBC ) }
86
- }
69
+ verify(mGeneralInfoDao).update(argThat { entity ->
70
+ entity.name == " databaseEncryptionMode" && entity.stringValue == " AES_128_CBC"
71
+ })
87
72
}
88
73
89
74
@Test
90
75
fun targetEncryptionLevelIsDeterminedWithEncryptionDisabledProperty () {
91
76
val encryptionMigrationTask = EncryptionMigrationTask (
92
77
mApiKey,
93
78
mSplitDatabase,
94
- mSplitCipherReference,
95
- mCountDownLatch,
96
- false
97
- )
98
-
99
- mockStatic(SplitCipherFactory ::class .java).use { mock ->
100
- mock.`when `<Any > {
101
- SplitCipherFactory .create(
102
- any(), eq(SplitEncryptionLevel .NONE )
103
- )
104
- }.thenReturn(mToCipher)
105
-
106
- encryptionMigrationTask.execute()
107
-
108
- mock.verify { SplitCipherFactory .create(mApiKey, SplitEncryptionLevel .NONE ) }
109
- }
110
- }
111
-
112
- @Test
113
- fun toCipherIsSetToCipherReference () {
114
- val encryptionMigrationTask = EncryptionMigrationTask (
115
- mApiKey,
116
- mSplitDatabase,
117
- mSplitCipherReference,
118
- mCountDownLatch,
119
- false
79
+ false ,
80
+ mToCipher
120
81
)
121
82
122
- mockStatic(SplitCipherFactory ::class .java).use { mock ->
123
- mock.`when `<Any > {
124
- SplitCipherFactory .create(
125
- any(), eq(SplitEncryptionLevel .NONE )
126
- )
127
- }.thenReturn(mToCipher)
128
-
129
- encryptionMigrationTask.execute()
130
-
131
- verify(mSplitCipherReference).set(mToCipher)
132
- }
133
- }
134
-
135
- @Test
136
- fun countDownLatchIsCountedDownAfterReferenceIsSet () {
137
- val encryptionMigrationTask = EncryptionMigrationTask (
138
- mApiKey,
139
- mSplitDatabase,
140
- mSplitCipherReference,
141
- mCountDownLatch,
142
- true
143
- )
144
-
145
- mockStatic(SplitCipherFactory ::class .java).use { mock ->
146
- mock.`when `<Any > {
147
- SplitCipherFactory .create(
148
- any(), eq(SplitEncryptionLevel .AES_128_CBC )
149
- )
150
- }.thenReturn(mToCipher)
151
-
152
- encryptionMigrationTask.execute()
153
-
154
- verify(mSplitCipherReference).set(mToCipher)
155
- assertEquals(0L , mCountDownLatch.count)
156
- }
157
- }
158
-
159
- @Test
160
- fun levelIsUpdatedInGeneralInfo () {
161
- EncryptionMigrationTask (
162
- mApiKey,
163
- mSplitDatabase,
164
- mSplitCipherReference,
165
- mCountDownLatch,
166
- true
167
- ).execute()
83
+ encryptionMigrationTask.execute()
168
84
169
- verify(mGeneralInfoDao).update(argThat {
170
- it .name == " databaseEncryptionMode" && it .stringValue == " AES_128_CBC "
85
+ verify(mGeneralInfoDao).update(argThat { entity ->
86
+ entity .name == " databaseEncryptionMode" && entity .stringValue == " NONE "
171
87
})
172
88
}
173
89
@@ -176,9 +92,8 @@ class EncryptionMigrationTaskTest {
176
92
val encryptionMigrationTask = EncryptionMigrationTask (
177
93
mApiKey,
178
94
mSplitDatabase,
179
- mSplitCipherReference,
180
- mCountDownLatch,
181
- true
95
+ true ,
96
+ mToCipher
182
97
)
183
98
184
99
val taskInfo = encryptionMigrationTask.execute()
@@ -192,22 +107,14 @@ class EncryptionMigrationTaskTest {
192
107
val encryptionMigrationTask = EncryptionMigrationTask (
193
108
mApiKey,
194
109
mSplitDatabase,
195
- mSplitCipherReference,
196
- mCountDownLatch,
197
- true
110
+ true ,
111
+ mToCipher
198
112
)
113
+ `when `(mGeneralInfoDao.update(any())).thenThrow(RuntimeException ())
199
114
200
- mockStatic(SplitCipherFactory ::class .java).use { mock ->
201
- mock.`when `<Any > {
202
- SplitCipherFactory .create(
203
- any(), eq(SplitEncryptionLevel .AES_128_CBC )
204
- )
205
- }.thenThrow(RuntimeException (" Test exception" ))
206
-
207
- val taskInfo = encryptionMigrationTask.execute()
115
+ val taskInfo = encryptionMigrationTask.execute()
208
116
209
- assertEquals(SplitTaskExecutionStatus .ERROR , taskInfo.status)
210
- assertEquals(SplitTaskType .GENERIC_TASK , taskInfo.taskType)
211
- }
117
+ assertEquals(SplitTaskExecutionStatus .ERROR , taskInfo.status)
118
+ assertEquals(SplitTaskType .GENERIC_TASK , taskInfo.taskType)
212
119
}
213
120
}
0 commit comments