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 ;
78import static org .mockito .Mockito .when ;
89
910import io .kafbat .ui .controller .SchemasController ;
1011import io .kafbat .ui .model .KafkaCluster ;
12+ import io .kafbat .ui .model .SchemaColumnsToSortDTO ;
1113import io .kafbat .ui .model .SchemaSubjectDTO ;
14+ import io .kafbat .ui .model .SortOrderDTO ;
15+ import io .kafbat .ui .service .SchemaRegistryService .SubjectWithCompatibilityLevel ;
1216import io .kafbat .ui .service .audit .AuditService ;
1317import io .kafbat .ui .sr .model .Compatibility ;
1418import io .kafbat .ui .sr .model .SchemaSubject ;
19+ import io .kafbat .ui .sr .model .SchemaType ;
1520import io .kafbat .ui .util .AccessControlServiceMock ;
1621import io .kafbat .ui .util .ReactiveFailover ;
1722import java .util .Comparator ;
1823import java .util .List ;
24+ import java .util .Map ;
1925import java .util .Optional ;
26+ import java .util .function .Function ;
27+ import java .util .stream .Collectors ;
2028import java .util .stream .IntStream ;
2129import org .junit .jupiter .api .Test ;
2230import org .mockito .Mockito ;
@@ -29,19 +37,31 @@ class SchemaRegistryPaginationTest {
2937 private SchemasController controller ;
3038
3139 private void init (List <String > subjects ) {
40+ initWithData (subjects .stream ().map (s ->
41+ new SubjectWithCompatibilityLevel (
42+ new SchemaSubject ().subject (s ),
43+ Compatibility .FULL
44+ )
45+ ).toList ());
46+ }
47+
48+ private void initWithData (List <SubjectWithCompatibilityLevel > subjects ) {
3249 ClustersStorage clustersStorage = Mockito .mock (ClustersStorage .class );
3350 when (clustersStorage .getClusterByName (isA (String .class )))
3451 .thenReturn (Optional .of (buildKafkaCluster (LOCAL_KAFKA_CLUSTER_NAME )));
3552
53+ Map <String , SubjectWithCompatibilityLevel > subjectsMap = subjects .stream ().collect (Collectors .toMap (
54+ SubjectWithCompatibilityLevel ::getSubject ,
55+ Function .identity ()
56+ ));
57+
3658 SchemaRegistryService schemaRegistryService = Mockito .mock (SchemaRegistryService .class );
3759 when (schemaRegistryService .getAllSubjectNames (isA (KafkaCluster .class )))
38- .thenReturn (Mono .just (subjects ));
60+ .thenReturn (Mono .just (subjects . stream (). map ( SubjectWithCompatibilityLevel :: getSubject ). toList () ));
3961 when (schemaRegistryService
40- .getAllLatestVersionSchemas (isA (KafkaCluster .class ), anyList ())).thenCallRealMethod ();
62+ .getAllLatestVersionSchemas (isA (KafkaCluster .class ), anyList (), anyInt ())).thenCallRealMethod ();
4163 when (schemaRegistryService .getLatestSchemaVersionBySubject (isA (KafkaCluster .class ), isA (String .class )))
42- .thenAnswer (a -> Mono .just (
43- new SchemaRegistryService .SubjectWithCompatibilityLevel (
44- new SchemaSubject ().subject (a .getArgument (1 )), Compatibility .FULL )));
64+ .thenAnswer (a -> Mono .just (subjectsMap .get (a .getArgument (1 ))));
4565
4666 this .controller = new SchemasController (schemaRegistryService );
4767 this .controller .setAccessControlService (new AccessControlServiceMock ().getMock ());
@@ -58,7 +78,7 @@ void shouldListFirst25andThen10Schemas() {
5878 .toList ()
5979 );
6080 var schemasFirst25 = controller .getSchemas (LOCAL_KAFKA_CLUSTER_NAME ,
61- null , null , null , null ).block ();
81+ null , null , null , null , null , null ).block ();
6282 assertThat (schemasFirst25 ).isNotNull ();
6383 assertThat (schemasFirst25 .getBody ()).isNotNull ();
6484 assertThat (schemasFirst25 .getBody ().getPageCount ()).isEqualTo (4 );
@@ -67,7 +87,7 @@ void shouldListFirst25andThen10Schemas() {
6787 .isSortedAccordingTo (Comparator .comparing (SchemaSubjectDTO ::getSubject ));
6888
6989 var schemasFirst10 = controller .getSchemas (LOCAL_KAFKA_CLUSTER_NAME ,
70- null , 10 , null , null ).block ();
90+ null , 10 , null , null , null , null ).block ();
7191
7292 assertThat (schemasFirst10 ).isNotNull ();
7393 assertThat (schemasFirst10 .getBody ()).isNotNull ();
@@ -86,7 +106,7 @@ void shouldListSchemasContaining_1() {
86106 .toList ()
87107 );
88108 var schemasSearch7 = controller .getSchemas (LOCAL_KAFKA_CLUSTER_NAME ,
89- null , null , "1" , null ).block ();
109+ null , null , "1" , null , null , null ).block ();
90110 assertThat (schemasSearch7 ).isNotNull ();
91111 assertThat (schemasSearch7 .getBody ()).isNotNull ();
92112 assertThat (schemasSearch7 .getBody ().getPageCount ()).isEqualTo (1 );
@@ -102,7 +122,7 @@ void shouldCorrectlyHandleNonPositivePageNumberAndPageSize() {
102122 .toList ()
103123 );
104124 var schemas = controller .getSchemas (LOCAL_KAFKA_CLUSTER_NAME ,
105- 0 , -1 , null , null ).block ();
125+ 0 , -1 , null , null , null , null ).block ();
106126
107127 assertThat (schemas ).isNotNull ();
108128 assertThat (schemas .getBody ()).isNotNull ();
@@ -121,7 +141,7 @@ void shouldCalculateCorrectPageCountForNonDivisiblePageSize() {
121141 );
122142
123143 var schemas = controller .getSchemas (LOCAL_KAFKA_CLUSTER_NAME ,
124- 4 , 33 , null , null ).block ();
144+ 4 , 33 , null , null , null , null ).block ();
125145
126146 assertThat (schemas ).isNotNull ();
127147 assertThat (schemas .getBody ()).isNotNull ();
@@ -137,4 +157,39 @@ private KafkaCluster buildKafkaCluster(String clusterName) {
137157 .schemaRegistryClient (mock (ReactiveFailover .class ))
138158 .build ();
139159 }
160+
161+ @ Test
162+ void shouldOrderByAndPaginate () {
163+ List <SubjectWithCompatibilityLevel > schemas = IntStream .rangeClosed (1 , 100 )
164+ .boxed ()
165+ .map (num -> new
166+ SubjectWithCompatibilityLevel (
167+ new SchemaSubject ()
168+ .subject ("subject" + num )
169+ .schemaType (SchemaType .AVRO )
170+ .id (num ),
171+ Compatibility .FULL
172+ )
173+ ).toList ();
174+
175+ initWithData (schemas );
176+
177+ var schemasFirst25 = controller .getSchemas (LOCAL_KAFKA_CLUSTER_NAME ,
178+ null , null , null ,
179+ SchemaColumnsToSortDTO .ID , SortOrderDTO .DESC , null
180+ ).block ();
181+
182+ List <String > last25OrderedById = schemas .stream ()
183+ .sorted (Comparator .comparing (SubjectWithCompatibilityLevel ::getId ).reversed ())
184+ .map (SubjectWithCompatibilityLevel ::getSubject )
185+ .limit (25 )
186+ .toList ();
187+
188+ assertThat (schemasFirst25 ).isNotNull ();
189+ assertThat (schemasFirst25 .getBody ()).isNotNull ();
190+ assertThat (schemasFirst25 .getBody ().getPageCount ()).isEqualTo (4 );
191+ assertThat (schemasFirst25 .getBody ().getSchemas ()).hasSize (25 );
192+ assertThat (schemasFirst25 .getBody ().getSchemas ().stream ().map (SchemaSubjectDTO ::getSubject ).toList ())
193+ .isEqualTo (last25OrderedById );
194+ }
140195}
0 commit comments