1818
1919package org .comixedproject .service .comicbooks ;
2020
21- import static junit .framework .TestCase .assertEquals ;
22- import static junit .framework .TestCase .assertFalse ;
23- import static junit .framework .TestCase .assertNotNull ;
24- import static junit .framework .TestCase .assertSame ;
25- import static junit .framework .TestCase .assertTrue ;
21+ import static junit .framework .TestCase .*;
2622
2723import java .util .*;
2824import org .apache .commons .lang .math .RandomUtils ;
2925import org .comixedproject .model .collections .CollectionEntry ;
30- import org .comixedproject .model .comicbooks .*;
26+ import org .comixedproject .model .comicbooks .ComicDetail ;
27+ import org .comixedproject .model .comicbooks .ComicTagType ;
3128import org .comixedproject .repositories .comicbooks .ComicDetailRepository ;
3229import org .junit .Before ;
3330import org .junit .Test ;
3431import org .junit .runner .RunWith ;
35- import org .mockito .ArgumentCaptor ;
36- import org .mockito .Captor ;
37- import org .mockito .InjectMocks ;
38- import org .mockito .Mock ;
39- import org .mockito .Mockito ;
32+ import org .mockito .*;
4033import org .mockito .junit .MockitoJUnitRunner ;
4134import org .springframework .data .domain .Example ;
4235import org .springframework .data .domain .Pageable ;
36+ import org .springframework .util .StringUtils ;
4337
4438@ RunWith (MockitoJUnitRunner .class )
4539public class ComicDetailServiceTest {
@@ -56,9 +50,11 @@ public class ComicDetailServiceTest {
5650 private static final int TEST_PAGE_SIZE = 10 ;
5751 private static final int TEST_PAGE_INDEX = RandomUtils .nextInt (100 );
5852 private static final long TEST_TOTAL_COMIC_COUNT = RandomUtils .nextLong () * 30000L ;
59- private static final String TEST_SORT_BY = "comic-count" ;
6053 private static final String TEST_SORT_DIRECTION = "asc" ;
6154 private static final String TEST_COMIC_FILENAME = "src/test/resources/example.cbz" ;
55+ private final Set <Date > weeksList = new HashSet <>();
56+ private final List <String > sortFieldNames = new ArrayList <>();
57+ private final List <ComicDetail > comicDetailList = new ArrayList <>();
6258
6359 @ InjectMocks private ComicDetailService service ;
6460 @ Mock private ComicDetailRepository comicDetailRepository ;
@@ -70,14 +66,9 @@ public class ComicDetailServiceTest {
7066 @ Mock private Set <Long > comicBookIdSet ;
7167 @ Mock private Example <ComicDetail > example ;
7268 @ Mock private CollectionEntry collectionEntry ;
73-
7469 @ Captor private ArgumentCaptor <Pageable > pageableArgumentCaptor ;
7570 @ Captor private ArgumentCaptor <Date > startDateArgumentCaptor ;
7671 @ Captor private ArgumentCaptor <Date > endDateArgumentCaptor ;
77-
78- private final Set <Date > weeksList = new HashSet <>();
79- private final List <String > sortFieldNames = new ArrayList <>();
80- private final List <ComicDetail > comicDetailList = new ArrayList <>();
8172 private List <CollectionEntry > collectionEntryList = new ArrayList <>();
8273
8374 @ Before
@@ -512,32 +503,76 @@ public void testLoadComicDetailsById() {
512503 }
513504
514505 @ Test
515- public void testLoadCollectionEntries () {
506+ public void testLoadCollectionEntries_unsorted () {
507+ collectionEntryList .add (collectionEntry );
508+
509+ Mockito .when (
510+ comicDetailRepository .loadCollectionEntries (
511+ Mockito .any (ComicTagType .class ), pageableArgumentCaptor .capture ()))
512+ .thenReturn (collectionEntryList );
513+
514+ doCollectionEntriesTest ("" );
515+ }
516+
517+ @ Test
518+ public void testLoadCollectionEntries_tagValueSort () {
519+ collectionEntryList .add (collectionEntry );
520+
521+ Mockito .when (
522+ comicDetailRepository .loadCollectionEntries (
523+ Mockito .any (ComicTagType .class ), pageableArgumentCaptor .capture ()))
524+ .thenReturn (collectionEntryList );
525+
526+ doCollectionEntriesTest ("tag-value" );
527+ }
528+
529+ @ Test
530+ public void testLoadCollectionEntries_comicCountSort () {
516531 collectionEntryList .add (collectionEntry );
517532
518533 Mockito .when (
519534 comicDetailRepository .loadCollectionEntries (
520535 Mockito .any (ComicTagType .class ), pageableArgumentCaptor .capture ()))
521536 .thenReturn (collectionEntryList );
522537
538+ doCollectionEntriesTest ("comic-count" );
539+ }
540+
541+ private void doCollectionEntriesTest (final String sortBy ) {
523542 final List <CollectionEntry > result =
524543 service .loadCollectionEntries (
525- TEST_TAG_TYPE , "" , TEST_PAGE_SIZE , TEST_PAGE_INDEX , TEST_SORT_BY , TEST_SORT_DIRECTION );
544+ TEST_TAG_TYPE , "" , TEST_PAGE_SIZE , TEST_PAGE_INDEX , sortBy , TEST_SORT_DIRECTION );
526545
527546 assertNotNull (result );
528547 assertFalse (result .isEmpty ());
529548
530549 final Pageable pageable = pageableArgumentCaptor .getValue ();
531550 assertEquals (TEST_PAGE_SIZE , pageable .getPageSize ());
532551 assertEquals (TEST_PAGE_INDEX , pageable .getPageNumber ());
533- assertFalse (pageable .getSort ().stream ().toList ().isEmpty ());
552+ if (StringUtils .hasLength (sortBy )) {
553+ assertFalse (pageable .getSort ().stream ().toList ().isEmpty ());
554+ }
534555
535556 Mockito .verify (comicDetailRepository , Mockito .times (1 ))
536557 .loadCollectionEntries (TEST_TAG_TYPE , pageable );
537558 }
538559
539560 @ Test
540- public void testLoadCollectionEntries_withFiltering () {
561+ public void testLoadCollectionEntries_unsorted_withFiltering () {
562+ collectionEntryList .add (collectionEntry );
563+
564+ Mockito .when (
565+ comicDetailRepository .loadCollectionEntriesWithFiltering (
566+ Mockito .any (ComicTagType .class ),
567+ Mockito .anyString (),
568+ pageableArgumentCaptor .capture ()))
569+ .thenReturn (collectionEntryList );
570+
571+ doCollectionEntriesTestWithFiltering ("" );
572+ }
573+
574+ @ Test
575+ public void testLoadCollectionEntries_tagValueSort_withFiltering () {
541576 collectionEntryList .add (collectionEntry );
542577
543578 Mockito .when (
@@ -547,13 +582,17 @@ public void testLoadCollectionEntries_withFiltering() {
547582 pageableArgumentCaptor .capture ()))
548583 .thenReturn (collectionEntryList );
549584
585+ doCollectionEntriesTestWithFiltering ("tag-value" );
586+ }
587+
588+ private void doCollectionEntriesTestWithFiltering (final String sortBy ) {
550589 final List <CollectionEntry > result =
551590 service .loadCollectionEntries (
552591 TEST_TAG_TYPE ,
553592 TEST_FILTER_TEXT ,
554593 TEST_PAGE_SIZE ,
555594 TEST_PAGE_INDEX ,
556- TEST_SORT_BY ,
595+ sortBy ,
557596 TEST_SORT_DIRECTION );
558597
559598 assertNotNull (result );
@@ -562,12 +601,28 @@ public void testLoadCollectionEntries_withFiltering() {
562601 final Pageable pageable = pageableArgumentCaptor .getValue ();
563602 assertEquals (TEST_PAGE_SIZE , pageable .getPageSize ());
564603 assertEquals (TEST_PAGE_INDEX , pageable .getPageNumber ());
565- assertFalse (pageable .getSort ().stream ().toList ().isEmpty ());
604+ if (StringUtils .hasLength (sortBy )) {
605+ assertFalse (pageable .getSort ().stream ().toList ().isEmpty ());
606+ }
566607
567608 Mockito .verify (comicDetailRepository , Mockito .times (1 ))
568609 .loadCollectionEntriesWithFiltering (TEST_TAG_TYPE , "%" + TEST_FILTER_TEXT + "%" , pageable );
569610 }
570611
612+ @ Test
613+ public void testLoadCollectionEntries_comicCountSort_withFiltering () {
614+ collectionEntryList .add (collectionEntry );
615+
616+ Mockito .when (
617+ comicDetailRepository .loadCollectionEntriesWithFiltering (
618+ Mockito .any (ComicTagType .class ),
619+ Mockito .anyString (),
620+ pageableArgumentCaptor .capture ()))
621+ .thenReturn (collectionEntryList );
622+
623+ doCollectionEntriesTestWithFiltering ("comic-count" );
624+ }
625+
571626 @ Test
572627 public void testLoadCollectionTotalEntries () {
573628 Mockito .when (comicDetailRepository .getFilterCount (Mockito .any (ComicTagType .class )))
0 commit comments