2222import static org .comixedproject .rest .comicbooks .ComicBookSelectionController .LIBRARY_SELECTIONS ;
2323
2424import jakarta .servlet .http .HttpSession ;
25+ import java .security .Principal ;
2526import java .util .ArrayList ;
2627import java .util .List ;
2728import org .comixedproject .model .comicbooks .ComicTagType ;
3031import org .comixedproject .service .comicbooks .ComicBookSelectionException ;
3132import org .comixedproject .service .comicbooks .ComicBookSelectionService ;
3233import org .comixedproject .service .comicbooks .ComicBookService ;
34+ import org .comixedproject .service .library .LastReadService ;
3335import org .junit .Before ;
3436import org .junit .Test ;
3537import org .junit .runner .RunWith ;
@@ -48,13 +50,17 @@ public class ComicBookSelectionControllerTest {
4850 private static final String TEST_PUBLISHER = "The Publisher" ;
4951 private static final String TEST_SERIES = "The Series" ;
5052 private static final String TEST_VOLUME = "2024" ;
53+ private static final String TEST_EMAIL =
"[email protected] " ;
5154
5255 @ InjectMocks private ComicBookSelectionController controller ;
5356 @ Mock private ComicBookSelectionService comicBookSelectionService ;
5457 @ Mock private ComicBookService comicBookService ;
58+ @ Mock private LastReadService lastReadService ;
5559 @ Mock private OPDSUtils opdsUtils ;
5660 @ Mock private HttpSession httpSession ;
5761 @ Mock private List selectionIdList ;
62+ @ Mock private Principal principal ;
63+ @ Mock private List <Long > comicDetailIdList ;
5864
5965 @ Before
6066 public void setUp () throws ComicBookSelectionException {
@@ -63,6 +69,9 @@ public void setUp() throws ComicBookSelectionException {
6369 .thenReturn (selectionIdList );
6470 Mockito .when (comicBookSelectionService .encodeSelections (Mockito .anyList ()))
6571 .thenReturn (TEST_REENCODED_SELECTIONS );
72+ Mockito .when (principal .getName ()).thenReturn (TEST_EMAIL );
73+ Mockito .when (lastReadService .getComicBookIdsForUser (Mockito .anyString (), Mockito .anyBoolean ()))
74+ .thenReturn (comicDetailIdList );
6675 }
6776
6877 @ Test
@@ -76,7 +85,7 @@ public void testGetAllSelections() throws ComicBookSelectionException {
7685 }
7786
7887 @ Test
79- public void testAddSingleSelection () throws ComicBookSelectionException {
88+ public void testAddSingleSelection_selecting () throws ComicBookSelectionException {
8089 controller .addSingleSelection (httpSession , TEST_COMIC_BOOK_ID );
8190
8291 Mockito .verify (comicBookSelectionService , Mockito .times (1 ))
@@ -87,7 +96,7 @@ public void testAddSingleSelection() throws ComicBookSelectionException {
8796 }
8897
8998 @ Test
90- public void testRemoveSingleSelection () throws ComicBookSelectionException {
99+ public void testAddSingleSelection_deselecting () throws ComicBookSelectionException {
91100 controller .deleteSingleSelection (httpSession , TEST_COMIC_BOOK_ID );
92101
93102 Mockito .verify (httpSession , Mockito .times (1 )).getAttribute (LIBRARY_SELECTIONS );
@@ -99,7 +108,7 @@ public void testRemoveSingleSelection() throws ComicBookSelectionException {
99108 }
100109
101110 @ Test
102- public void testSelectMultipleComicWithAdded () throws ComicBookSelectionException {
111+ public void testSelectComicBooksByFilter_selecting () throws ComicBookSelectionException {
103112 controller .selectComicBooksByFilter (
104113 httpSession ,
105114 new MultipleComicBooksSelectionRequest (null , null , null , null , null , false , null , true ));
@@ -112,7 +121,7 @@ public void testSelectMultipleComicWithAdded() throws ComicBookSelectionExceptio
112121 }
113122
114123 @ Test
115- public void testSelectMultipleComicWithRemoving () throws ComicBookSelectionException {
124+ public void testSelectComicBooksByFilter_deselecting () throws ComicBookSelectionException {
116125 controller .selectComicBooksByFilter (
117126 httpSession ,
118127 new MultipleComicBooksSelectionRequest (null , null , null , null , null , false , null , false ));
@@ -126,8 +135,7 @@ public void testSelectMultipleComicWithRemoving() throws ComicBookSelectionExcep
126135
127136 @ Test
128137 public void testAddComicBooksByTagTypeAndValue () throws ComicBookSelectionException {
129- Mockito .when (opdsUtils .urlDecodeString (TEST_TAG_VALUE .toString ()))
130- .thenReturn (TEST_TAG_VALUE .toString ());
138+ Mockito .when (opdsUtils .urlDecodeString (TEST_TAG_VALUE )).thenReturn (TEST_TAG_VALUE );
131139
132140 controller .addComicBooksByTagTypeAndValue (
133141 httpSession , TEST_TAG_TYPE .getValue (), TEST_TAG_VALUE );
@@ -142,8 +150,7 @@ public void testAddComicBooksByTagTypeAndValue() throws ComicBookSelectionExcept
142150
143151 @ Test
144152 public void testRemoveComicBooksByTagTypeAndValue () throws ComicBookSelectionException {
145- Mockito .when (opdsUtils .urlDecodeString (TEST_TAG_VALUE .toString ()))
146- .thenReturn (TEST_TAG_VALUE .toString ());
153+ Mockito .when (opdsUtils .urlDecodeString (TEST_TAG_VALUE )).thenReturn (TEST_TAG_VALUE );
147154
148155 controller .removeComicBooksByTagTypeAndValue (
149156 httpSession , TEST_TAG_TYPE .getValue (), TEST_TAG_VALUE );
@@ -157,7 +164,7 @@ public void testRemoveComicBooksByTagTypeAndValue() throws ComicBookSelectionExc
157164 }
158165
159166 @ Test
160- public void testAddSelectionsByIdForAddition () throws ComicBookSelectionException {
167+ public void testAddSelectionsById_selecting () throws ComicBookSelectionException {
161168 final List <Long > comicBookIdList = new ArrayList <Long >();
162169 for (long index = 1000L ; index < 2000L ; index ++) comicBookIdList .add (index );
163170
@@ -172,7 +179,7 @@ public void testAddSelectionsByIdForAddition() throws ComicBookSelectionExceptio
172179 }
173180
174181 @ Test
175- public void testAddSelectionsByIdForRemoval () throws ComicBookSelectionException {
182+ public void testAddSelectionsById_deselecting () throws ComicBookSelectionException {
176183 final List <Long > comicBookIdList = new ArrayList <Long >();
177184 for (long index = 1000L ; index < 2000L ; index ++) comicBookIdList .add (index );
178185
@@ -187,7 +194,8 @@ public void testAddSelectionsByIdForRemoval() throws ComicBookSelectionException
187194 }
188195
189196 @ Test
190- public void testAddSelectionsByPublisherForAddition () throws ComicBookSelectionException {
197+ public void testAddSelectionsByPublisherForAddition_selecting ()
198+ throws ComicBookSelectionException {
191199 Mockito .when (comicBookService .getIdsByPublisher (Mockito .anyString ()))
192200 .thenReturn (selectionIdList );
193201
@@ -203,7 +211,8 @@ public void testAddSelectionsByPublisherForAddition() throws ComicBookSelectionE
203211 }
204212
205213 @ Test
206- public void testAddSelectionsByPublisherForRemoval () throws ComicBookSelectionException {
214+ public void testAddSelectionsByPublisherForAddition_deselecting ()
215+ throws ComicBookSelectionException {
207216 Mockito .when (comicBookService .getIdsByPublisher (Mockito .anyString ()))
208217 .thenReturn (selectionIdList );
209218
@@ -219,7 +228,7 @@ public void testAddSelectionsByPublisherForRemoval() throws ComicBookSelectionEx
219228 }
220229
221230 @ Test
222- public void testAddSelectionsByPublisherSeriesVolumeForAddition ()
231+ public void testAddSelectionsByPublisherSeriesVolume_selecting ()
223232 throws ComicBookSelectionException {
224233 Mockito .when (
225234 comicBookService .getIdsByPublisherSeriesAndVolume (
@@ -241,7 +250,7 @@ public void testAddSelectionsByPublisherSeriesVolumeForAddition()
241250 }
242251
243252 @ Test
244- public void testAddSelectionsByPublisherSeriesVolumeForRemoval ()
253+ public void testAddSelectionsByPublisherSeriesVolume_deselecting ()
245254 throws ComicBookSelectionException {
246255 Mockito .when (
247256 comicBookService .getIdsByPublisherSeriesAndVolume (
@@ -292,6 +301,61 @@ public void testRemoveDuplicateComicBooksToSelections() throws ComicBookSelectio
292301 .setAttribute (LIBRARY_SELECTIONS , TEST_REENCODED_SELECTIONS );
293302 }
294303
304+ @ Test
305+ public void testAddUnreadComicBooksSelection_selectingRead () throws ComicBookSelectionException {
306+ controller .addUnreadComicBooksSelection (
307+ httpSession , principal , new UnreadComicBooksSelectionRequest (true , false ));
308+
309+ Mockito .verify (httpSession , Mockito .times (1 )).getAttribute (LIBRARY_SELECTIONS );
310+ Mockito .verify (lastReadService , Mockito .times (1 )).getComicBookIdsForUser (TEST_EMAIL , false );
311+ Mockito .verify (selectionIdList , Mockito .times (1 )).addAll (comicDetailIdList );
312+ Mockito .verify (comicBookSelectionService , Mockito .times (1 )).encodeSelections (selectionIdList );
313+ Mockito .verify (httpSession , Mockito .times (1 ))
314+ .setAttribute (LIBRARY_SELECTIONS , TEST_REENCODED_SELECTIONS );
315+ }
316+
317+ @ Test
318+ public void testAddUnreadComicBooksSelection_deselectingRead ()
319+ throws ComicBookSelectionException {
320+ controller .addUnreadComicBooksSelection (
321+ httpSession , principal , new UnreadComicBooksSelectionRequest (false , false ));
322+
323+ Mockito .verify (httpSession , Mockito .times (1 )).getAttribute (LIBRARY_SELECTIONS );
324+ Mockito .verify (lastReadService , Mockito .times (1 )).getComicBookIdsForUser (TEST_EMAIL , false );
325+ Mockito .verify (selectionIdList , Mockito .times (1 )).removeAll (comicDetailIdList );
326+ Mockito .verify (comicBookSelectionService , Mockito .times (1 )).encodeSelections (selectionIdList );
327+ Mockito .verify (httpSession , Mockito .times (1 ))
328+ .setAttribute (LIBRARY_SELECTIONS , TEST_REENCODED_SELECTIONS );
329+ }
330+
331+ @ Test
332+ public void testAddUnreadComicBooksSelection_selectingUnread ()
333+ throws ComicBookSelectionException {
334+ controller .addUnreadComicBooksSelection (
335+ httpSession , principal , new UnreadComicBooksSelectionRequest (true , true ));
336+
337+ Mockito .verify (httpSession , Mockito .times (1 )).getAttribute (LIBRARY_SELECTIONS );
338+ Mockito .verify (lastReadService , Mockito .times (1 )).getComicBookIdsForUser (TEST_EMAIL , true );
339+ Mockito .verify (selectionIdList , Mockito .times (1 )).addAll (comicDetailIdList );
340+ Mockito .verify (comicBookSelectionService , Mockito .times (1 )).encodeSelections (selectionIdList );
341+ Mockito .verify (httpSession , Mockito .times (1 ))
342+ .setAttribute (LIBRARY_SELECTIONS , TEST_REENCODED_SELECTIONS );
343+ }
344+
345+ @ Test
346+ public void testAddUnreadComicBooksSelection_deselectingUnread ()
347+ throws ComicBookSelectionException {
348+ controller .addUnreadComicBooksSelection (
349+ httpSession , principal , new UnreadComicBooksSelectionRequest (false , true ));
350+
351+ Mockito .verify (httpSession , Mockito .times (1 )).getAttribute (LIBRARY_SELECTIONS );
352+ Mockito .verify (lastReadService , Mockito .times (1 )).getComicBookIdsForUser (TEST_EMAIL , true );
353+ Mockito .verify (selectionIdList , Mockito .times (1 )).removeAll (comicDetailIdList );
354+ Mockito .verify (comicBookSelectionService , Mockito .times (1 )).encodeSelections (selectionIdList );
355+ Mockito .verify (httpSession , Mockito .times (1 ))
356+ .setAttribute (LIBRARY_SELECTIONS , TEST_REENCODED_SELECTIONS );
357+ }
358+
295359 @ Test
296360 public void testClearSelections () throws ComicBookSelectionException {
297361 controller .clearSelections (httpSession );
0 commit comments