11package io .kafbat .ui .service ;
22
33import static org .assertj .core .api .Assertions .assertThat ;
4+ import static org .mockito .ArgumentMatchers .anyInt ;
45import static org .mockito .ArgumentMatchers .anyList ;
56import static org .mockito .ArgumentMatchers .isA ;
67import static org .mockito .Mockito .mock ;
910import io .kafbat .ui .config .ClustersProperties ;
1011import io .kafbat .ui .controller .SchemasController ;
1112import io .kafbat .ui .model .KafkaCluster ;
13+ import io .kafbat .ui .model .SchemaColumnsToSortDTO ;
1214import io .kafbat .ui .model .SchemaSubjectDTO ;
15+ import io .kafbat .ui .model .SortOrderDTO ;
16+ import io .kafbat .ui .service .SchemaRegistryService .SubjectWithCompatibilityLevel ;
1317import io .kafbat .ui .service .audit .AuditService ;
1418import io .kafbat .ui .sr .model .Compatibility ;
1519import io .kafbat .ui .sr .model .SchemaSubject ;
20+ import io .kafbat .ui .sr .model .SchemaType ;
1621import io .kafbat .ui .util .AccessControlServiceMock ;
1722import io .kafbat .ui .util .ReactiveFailover ;
1823import java .util .Comparator ;
1924import java .util .List ;
25+ import java .util .Map ;
2026import java .util .Optional ;
27+ import java .util .function .Function ;
28+ import java .util .stream .Collectors ;
2129import java .util .stream .IntStream ;
2230import org .junit .jupiter .api .Test ;
2331import org .mockito .Mockito ;
@@ -30,19 +38,31 @@ class SchemaRegistryPaginationTest {
3038 private SchemasController controller ;
3139
3240 private void init (List <String > subjects ) {
41+ initWithData (subjects .stream ().map (s ->
42+ new SubjectWithCompatibilityLevel (
43+ new SchemaSubject ().subject (s ),
44+ Compatibility .FULL
45+ )
46+ ).toList ());
47+ }
48+
49+ private void initWithData (List <SubjectWithCompatibilityLevel > subjects ) {
3350 ClustersStorage clustersStorage = Mockito .mock (ClustersStorage .class );
3451 when (clustersStorage .getClusterByName (isA (String .class )))
3552 .thenReturn (Optional .of (buildKafkaCluster (LOCAL_KAFKA_CLUSTER_NAME )));
3653
54+ Map <String , SubjectWithCompatibilityLevel > subjectsMap = subjects .stream ().collect (Collectors .toMap (
55+ SubjectWithCompatibilityLevel ::getSubject ,
56+ Function .identity ()
57+ ));
58+
3759 SchemaRegistryService schemaRegistryService = Mockito .mock (SchemaRegistryService .class );
3860 when (schemaRegistryService .getAllSubjectNames (isA (KafkaCluster .class )))
39- .thenReturn (Mono .just (subjects ));
61+ .thenReturn (Mono .just (subjects . stream (). map ( SubjectWithCompatibilityLevel :: getSubject ). toList () ));
4062 when (schemaRegistryService
41- .getAllLatestVersionSchemas (isA (KafkaCluster .class ), anyList ())).thenCallRealMethod ();
63+ .getAllLatestVersionSchemas (isA (KafkaCluster .class ), anyList (), anyInt ())).thenCallRealMethod ();
4264 when (schemaRegistryService .getLatestSchemaVersionBySubject (isA (KafkaCluster .class ), isA (String .class )))
43- .thenAnswer (a -> Mono .just (
44- new SchemaRegistryService .SubjectWithCompatibilityLevel (
45- new SchemaSubject ().subject (a .getArgument (1 )), Compatibility .FULL )));
65+ .thenAnswer (a -> Mono .just (subjectsMap .get (a .getArgument (1 ))));
4666
4767 this .controller = new SchemasController (schemaRegistryService , new ClustersProperties ());
4868 this .controller .setAccessControlService (new AccessControlServiceMock ().getMock ());
@@ -59,7 +79,7 @@ void shouldListFirst25andThen10Schemas() {
5979 .toList ()
6080 );
6181 var schemasFirst25 = controller .getSchemas (LOCAL_KAFKA_CLUSTER_NAME ,
62- null , null , null , null ).block ();
82+ null , null , null , null , null , null ).block ();
6383 assertThat (schemasFirst25 ).isNotNull ();
6484 assertThat (schemasFirst25 .getBody ()).isNotNull ();
6585 assertThat (schemasFirst25 .getBody ().getPageCount ()).isEqualTo (4 );
@@ -68,7 +88,7 @@ void shouldListFirst25andThen10Schemas() {
6888 .isSortedAccordingTo (Comparator .comparing (SchemaSubjectDTO ::getSubject ));
6989
7090 var schemasFirst10 = controller .getSchemas (LOCAL_KAFKA_CLUSTER_NAME ,
71- null , 10 , null , null ).block ();
91+ null , 10 , null , null , null , null ).block ();
7292
7393 assertThat (schemasFirst10 ).isNotNull ();
7494 assertThat (schemasFirst10 .getBody ()).isNotNull ();
@@ -87,7 +107,7 @@ void shouldListSchemasContaining_1() {
87107 .toList ()
88108 );
89109 var schemasSearch7 = controller .getSchemas (LOCAL_KAFKA_CLUSTER_NAME ,
90- null , null , "1" , null ).block ();
110+ null , null , "1" , null , null , null ).block ();
91111 assertThat (schemasSearch7 ).isNotNull ();
92112 assertThat (schemasSearch7 .getBody ()).isNotNull ();
93113 assertThat (schemasSearch7 .getBody ().getPageCount ()).isEqualTo (1 );
@@ -103,7 +123,7 @@ void shouldCorrectlyHandleNonPositivePageNumberAndPageSize() {
103123 .toList ()
104124 );
105125 var schemas = controller .getSchemas (LOCAL_KAFKA_CLUSTER_NAME ,
106- 0 , -1 , null , null ).block ();
126+ 0 , -1 , null , null , null , null ).block ();
107127
108128 assertThat (schemas ).isNotNull ();
109129 assertThat (schemas .getBody ()).isNotNull ();
@@ -122,7 +142,7 @@ void shouldCalculateCorrectPageCountForNonDivisiblePageSize() {
122142 );
123143
124144 var schemas = controller .getSchemas (LOCAL_KAFKA_CLUSTER_NAME ,
125- 4 , 33 , null , null ).block ();
145+ 4 , 33 , null , null , null , null ).block ();
126146
127147 assertThat (schemas ).isNotNull ();
128148 assertThat (schemas .getBody ()).isNotNull ();
@@ -138,4 +158,39 @@ private KafkaCluster buildKafkaCluster(String clusterName) {
138158 .schemaRegistryClient (mock (ReactiveFailover .class ))
139159 .build ();
140160 }
161+
162+ @ Test
163+ void shouldOrderByAndPaginate () {
164+ List <SubjectWithCompatibilityLevel > schemas = IntStream .rangeClosed (1 , 100 )
165+ .boxed ()
166+ .map (num -> new
167+ SubjectWithCompatibilityLevel (
168+ new SchemaSubject ()
169+ .subject ("subject" + num )
170+ .schemaType (SchemaType .AVRO )
171+ .id (num ),
172+ Compatibility .FULL
173+ )
174+ ).toList ();
175+
176+ initWithData (schemas );
177+
178+ var schemasFirst25 = controller .getSchemas (LOCAL_KAFKA_CLUSTER_NAME ,
179+ null , null , null ,
180+ SchemaColumnsToSortDTO .ID , SortOrderDTO .DESC , null
181+ ).block ();
182+
183+ List <String > last25OrderedById = schemas .stream ()
184+ .sorted (Comparator .comparing (SubjectWithCompatibilityLevel ::getId ).reversed ())
185+ .map (SubjectWithCompatibilityLevel ::getSubject )
186+ .limit (25 )
187+ .toList ();
188+
189+ assertThat (schemasFirst25 ).isNotNull ();
190+ assertThat (schemasFirst25 .getBody ()).isNotNull ();
191+ assertThat (schemasFirst25 .getBody ().getPageCount ()).isEqualTo (4 );
192+ assertThat (schemasFirst25 .getBody ().getSchemas ()).hasSize (25 );
193+ assertThat (schemasFirst25 .getBody ().getSchemas ().stream ().map (SchemaSubjectDTO ::getSubject ).toList ())
194+ .isEqualTo (last25OrderedById );
195+ }
141196}
0 commit comments