14
14
import org .powermock .modules .junit4 .rule .PowerMockRule ;
15
15
import org .robolectric .util .ReflectionHelpers ;
16
16
import org .smartregister .BaseUnitTest ;
17
- import org .smartregister .repository .mock .RepositoryMock ;
18
- import org .smartregister .util .Session ;
17
+ import org .smartregister .security .SecurityHelper ;
19
18
import org .smartregister .view .activity .DrishtiApplication ;
20
19
21
20
import java .io .File ;
@@ -29,38 +28,40 @@ public class RepositoryTest extends BaseUnitTest {
29
28
@ Rule
30
29
public PowerMockRule rule = new PowerMockRule ();
31
30
32
- private RepositoryMock repositoryMock ;
33
-
34
31
@ Mock
35
32
private DrishtiApplication drishtiApplication ;
36
33
37
34
private Repository repository ;
35
+
38
36
@ Mock
39
37
private android .content .Context context ;
38
+
40
39
@ Mock
41
- private Session session ;
42
- @ Mock
43
- private DrishtiRepository drishtiRepository ;
40
+ private SQLiteDatabase database ;
44
41
45
42
private String dbName ;
46
- private String password ;
43
+ private char [] password ;
47
44
48
45
@ Before
49
46
public void setUp () {
50
47
dbName = "drishti.db" ;
51
- password = "Android7832!" ;
48
+ password = "Android7832!" . toCharArray () ;
52
49
53
50
MockitoAnnotations .initMocks (this );
54
51
PowerMockito .mockStatic (DrishtiApplication .class );
55
52
PowerMockito .when (DrishtiApplication .getInstance ()).thenReturn (drishtiApplication );
56
53
PowerMockito .when (drishtiApplication .getApplicationContext ()).thenReturn (context );
57
- Mockito .doReturn (password ).when (drishtiApplication ).getPassword ();
54
+
55
+ ReflectionHelpers .setField (drishtiApplication , "password" , SecurityHelper .toBytes (password ));
58
56
PowerMockito .when (context .getDir ("opensrp" , android .content .Context .MODE_PRIVATE )).thenReturn (new File ("/" ));
59
57
60
58
61
- repository = Mockito .mock (Repository .class , Mockito .CALLS_REAL_METHODS );
59
+ repository = Mockito .spy ( Mockito . mock (Repository .class , Mockito .CALLS_REAL_METHODS ) );
62
60
ReflectionHelpers .setField (repository , "context" , context );
63
61
ReflectionHelpers .setField (repository , "dbName" , dbName );
62
+
63
+ Mockito .doReturn (true ).when (database ).isOpen ();
64
+ ReflectionHelpers .setField (repository , "mDatabase" , database );
64
65
}
65
66
66
67
@ Test
@@ -69,7 +70,7 @@ public void getReadableDatabaseShouldCallGetReadableDbAndPassword() {
69
70
70
71
repository .getReadableDatabase ();
71
72
72
- Mockito .verify (repository ).getReadableDatabase (password );
73
+ Mockito .verify (repository ).getReadableDatabase (SecurityHelper . toBytes ( password ) );
73
74
}
74
75
75
76
@ Test (expected = RuntimeException .class )
@@ -83,9 +84,10 @@ public void getReadableDatabaseShouldThrowRuntimeException() {
83
84
public void getWritableDatabaseShouldCallGetWritableDbAndPassword () {
84
85
Mockito .doReturn (null ).when (repository ).getWritableDatabase (password );
85
86
87
+
86
88
repository .getWritableDatabase ();
87
89
88
- Mockito .verify (repository ).getWritableDatabase (password );
90
+ Mockito .verify (repository ).getWritableDatabase (SecurityHelper . toBytes ( password ) );
89
91
}
90
92
91
93
@ Test (expected = RuntimeException .class )
0 commit comments