1
1
package io .kafbat .ui .service ;
2
2
3
3
import static org .assertj .core .api .Assertions .assertThat ;
4
+ import static org .mockito .ArgumentMatchers .anyInt ;
4
5
import static org .mockito .ArgumentMatchers .anyList ;
5
6
import static org .mockito .ArgumentMatchers .isA ;
6
7
import static org .mockito .Mockito .mock ;
9
10
import io .kafbat .ui .config .ClustersProperties ;
10
11
import io .kafbat .ui .controller .SchemasController ;
11
12
import io .kafbat .ui .model .KafkaCluster ;
13
+ import io .kafbat .ui .model .SchemaColumnsToSortDTO ;
12
14
import io .kafbat .ui .model .SchemaSubjectDTO ;
15
+ import io .kafbat .ui .model .SortOrderDTO ;
16
+ import io .kafbat .ui .service .SchemaRegistryService .SubjectWithCompatibilityLevel ;
13
17
import io .kafbat .ui .service .audit .AuditService ;
14
18
import io .kafbat .ui .sr .model .Compatibility ;
15
19
import io .kafbat .ui .sr .model .SchemaSubject ;
20
+ import io .kafbat .ui .sr .model .SchemaType ;
16
21
import io .kafbat .ui .util .AccessControlServiceMock ;
17
22
import io .kafbat .ui .util .ReactiveFailover ;
18
23
import java .util .Comparator ;
19
24
import java .util .List ;
25
+ import java .util .Map ;
20
26
import java .util .Optional ;
27
+ import java .util .function .Function ;
28
+ import java .util .stream .Collectors ;
21
29
import java .util .stream .IntStream ;
22
30
import org .junit .jupiter .api .Test ;
23
31
import org .mockito .Mockito ;
@@ -30,19 +38,31 @@ class SchemaRegistryPaginationTest {
30
38
private SchemasController controller ;
31
39
32
40
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 ) {
33
50
ClustersStorage clustersStorage = Mockito .mock (ClustersStorage .class );
34
51
when (clustersStorage .getClusterByName (isA (String .class )))
35
52
.thenReturn (Optional .of (buildKafkaCluster (LOCAL_KAFKA_CLUSTER_NAME )));
36
53
54
+ Map <String , SubjectWithCompatibilityLevel > subjectsMap = subjects .stream ().collect (Collectors .toMap (
55
+ SubjectWithCompatibilityLevel ::getSubject ,
56
+ Function .identity ()
57
+ ));
58
+
37
59
SchemaRegistryService schemaRegistryService = Mockito .mock (SchemaRegistryService .class );
38
60
when (schemaRegistryService .getAllSubjectNames (isA (KafkaCluster .class )))
39
- .thenReturn (Mono .just (subjects ));
61
+ .thenReturn (Mono .just (subjects . stream (). map ( SubjectWithCompatibilityLevel :: getSubject ). toList () ));
40
62
when (schemaRegistryService
41
- .getAllLatestVersionSchemas (isA (KafkaCluster .class ), anyList ())).thenCallRealMethod ();
63
+ .getAllLatestVersionSchemas (isA (KafkaCluster .class ), anyList (), anyInt ())).thenCallRealMethod ();
42
64
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 ))));
46
66
47
67
this .controller = new SchemasController (schemaRegistryService , new ClustersProperties ());
48
68
this .controller .setAccessControlService (new AccessControlServiceMock ().getMock ());
@@ -59,7 +79,7 @@ void shouldListFirst25andThen10Schemas() {
59
79
.toList ()
60
80
);
61
81
var schemasFirst25 = controller .getSchemas (LOCAL_KAFKA_CLUSTER_NAME ,
62
- null , null , null , null ).block ();
82
+ null , null , null , null , null , null ).block ();
63
83
assertThat (schemasFirst25 ).isNotNull ();
64
84
assertThat (schemasFirst25 .getBody ()).isNotNull ();
65
85
assertThat (schemasFirst25 .getBody ().getPageCount ()).isEqualTo (4 );
@@ -68,7 +88,7 @@ void shouldListFirst25andThen10Schemas() {
68
88
.isSortedAccordingTo (Comparator .comparing (SchemaSubjectDTO ::getSubject ));
69
89
70
90
var schemasFirst10 = controller .getSchemas (LOCAL_KAFKA_CLUSTER_NAME ,
71
- null , 10 , null , null ).block ();
91
+ null , 10 , null , null , null , null ).block ();
72
92
73
93
assertThat (schemasFirst10 ).isNotNull ();
74
94
assertThat (schemasFirst10 .getBody ()).isNotNull ();
@@ -87,7 +107,7 @@ void shouldListSchemasContaining_1() {
87
107
.toList ()
88
108
);
89
109
var schemasSearch7 = controller .getSchemas (LOCAL_KAFKA_CLUSTER_NAME ,
90
- null , null , "1" , null ).block ();
110
+ null , null , "1" , null , null , null ).block ();
91
111
assertThat (schemasSearch7 ).isNotNull ();
92
112
assertThat (schemasSearch7 .getBody ()).isNotNull ();
93
113
assertThat (schemasSearch7 .getBody ().getPageCount ()).isEqualTo (1 );
@@ -103,7 +123,7 @@ void shouldCorrectlyHandleNonPositivePageNumberAndPageSize() {
103
123
.toList ()
104
124
);
105
125
var schemas = controller .getSchemas (LOCAL_KAFKA_CLUSTER_NAME ,
106
- 0 , -1 , null , null ).block ();
126
+ 0 , -1 , null , null , null , null ).block ();
107
127
108
128
assertThat (schemas ).isNotNull ();
109
129
assertThat (schemas .getBody ()).isNotNull ();
@@ -122,7 +142,7 @@ void shouldCalculateCorrectPageCountForNonDivisiblePageSize() {
122
142
);
123
143
124
144
var schemas = controller .getSchemas (LOCAL_KAFKA_CLUSTER_NAME ,
125
- 4 , 33 , null , null ).block ();
145
+ 4 , 33 , null , null , null , null ).block ();
126
146
127
147
assertThat (schemas ).isNotNull ();
128
148
assertThat (schemas .getBody ()).isNotNull ();
@@ -138,4 +158,39 @@ private KafkaCluster buildKafkaCluster(String clusterName) {
138
158
.schemaRegistryClient (mock (ReactiveFailover .class ))
139
159
.build ();
140
160
}
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
+ }
141
196
}
0 commit comments