1
+ package fr .free .nrw .commons .bookmarks .pictures ;
2
+
3
+ import android .net .Uri ;
4
+
5
+ import org .junit .Before ;
6
+ import org .junit .Test ;
7
+ import org .mockito .InjectMocks ;
8
+ import org .mockito .Mock ;
9
+ import org .mockito .MockitoAnnotations ;
10
+
11
+ import java .util .ArrayList ;
12
+ import java .util .List ;
13
+
14
+ import fr .free .nrw .commons .Media ;
15
+ import fr .free .nrw .commons .bookmarks .Bookmark ;
16
+ import fr .free .nrw .commons .mwapi .OkHttpJsonApiClient ;
17
+ import io .reactivex .Single ;
18
+
19
+ import static org .junit .Assert .assertEquals ;
20
+ import static org .junit .Assert .assertFalse ;
21
+ import static org .junit .Assert .assertTrue ;
22
+ import static org .mockito .ArgumentMatchers .anyBoolean ;
23
+ import static org .mockito .ArgumentMatchers .anyString ;
24
+ import static org .mockito .Mockito .when ;
25
+
26
+ /**
27
+ * Tests for bookmark pictures controller
28
+ */
29
+ public class BookmarkPicturesControllerTest {
30
+
31
+ @ Mock
32
+ OkHttpJsonApiClient okHttpJsonApiClient ;
33
+ @ Mock
34
+ BookmarkPicturesDao bookmarkDao ;
35
+
36
+ @ InjectMocks
37
+ BookmarkPicturesController bookmarkPicturesController ;
38
+
39
+
40
+ /**
41
+ * Init mocks
42
+ */
43
+ @ Before
44
+ public void setup () {
45
+ MockitoAnnotations .initMocks (this );
46
+ Media mockMedia = getMockMedia ();
47
+ when (bookmarkDao .getAllBookmarks ())
48
+ .thenReturn (getMockBookmarkList ());
49
+ when (okHttpJsonApiClient .getMedia (anyString (), anyBoolean ()))
50
+ .thenReturn (Single .just (mockMedia ));
51
+ }
52
+
53
+ /**
54
+ * Get mock bookmark list
55
+ * @return
56
+ */
57
+ private List <Bookmark > getMockBookmarkList () {
58
+ ArrayList <Bookmark > list = new ArrayList <>();
59
+ list .add (new Bookmark ("File:Test1.jpg" , "Maskaravivek" , Uri .EMPTY ));
60
+ list .add (new Bookmark ("File:Test2.jpg" , "Maskaravivek" , Uri .EMPTY ));
61
+ return list ;
62
+ }
63
+
64
+ /**
65
+ * Test case where all bookmark pictures are fetched and media is found against it
66
+ */
67
+ @ Test
68
+ public void loadBookmarkedPictures () {
69
+ List <Media > bookmarkedPictures = bookmarkPicturesController .loadBookmarkedPictures ().blockingGet ();
70
+ assertEquals (2 , bookmarkedPictures .size ());
71
+ }
72
+
73
+ /**
74
+ * Test case where all bookmark pictures are fetched and only one media is found
75
+ */
76
+ @ Test
77
+ public void loadBookmarkedPicturesForNullMedia () {
78
+ when (okHttpJsonApiClient .getMedia ("File:Test1.jpg" , false ))
79
+ .thenReturn (Single .error (new NullPointerException ("Error occurred" )));
80
+ when (okHttpJsonApiClient .getMedia ("File:Test2.jpg" , false ))
81
+ .thenReturn (Single .just (getMockMedia ()));
82
+ List <Media > bookmarkedPictures = bookmarkPicturesController .loadBookmarkedPictures ().blockingGet ();
83
+ assertEquals (1 , bookmarkedPictures .size ());
84
+ }
85
+
86
+ private Media getMockMedia () {
87
+ return new Media ("File:Test.jpg" );
88
+ }
89
+
90
+ /**
91
+ * Test case where current bookmarks don't match the bookmarks in DB
92
+ */
93
+ @ Test
94
+ public void needRefreshBookmarkedPictures () {
95
+ boolean needRefreshBookmarkedPictures = bookmarkPicturesController .needRefreshBookmarkedPictures ();
96
+ assertTrue (needRefreshBookmarkedPictures );
97
+ }
98
+
99
+ /**
100
+ * Test case where the DB is up to date with the bookmarks loaded in the list
101
+ */
102
+ @ Test
103
+ public void doNotNeedRefreshBookmarkedPictures () {
104
+ List <Media > bookmarkedPictures = bookmarkPicturesController .loadBookmarkedPictures ().blockingGet ();
105
+ assertEquals (2 , bookmarkedPictures .size ());
106
+ boolean needRefreshBookmarkedPictures = bookmarkPicturesController .needRefreshBookmarkedPictures ();
107
+ assertFalse (needRefreshBookmarkedPictures );
108
+ }
109
+ }
0 commit comments