ClusterServiceTest.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. package com.provectus.kafka.ui.service;
  2. import static org.assertj.core.api.Assertions.assertThat;
  3. import static org.mockito.ArgumentMatchers.any;
  4. import static org.mockito.Mockito.when;
  5. import com.provectus.kafka.ui.mapper.ClusterMapper;
  6. import com.provectus.kafka.ui.model.InternalTopic;
  7. import com.provectus.kafka.ui.model.InternalTopicConfig;
  8. import com.provectus.kafka.ui.model.KafkaCluster;
  9. import com.provectus.kafka.ui.model.TopicColumnsToSortDTO;
  10. import com.provectus.kafka.ui.model.TopicDTO;
  11. import java.util.List;
  12. import java.util.Map;
  13. import java.util.Objects;
  14. import java.util.Optional;
  15. import java.util.UUID;
  16. import java.util.function.Function;
  17. import java.util.stream.Collectors;
  18. import java.util.stream.IntStream;
  19. import org.apache.kafka.clients.admin.ConfigEntry;
  20. import org.junit.jupiter.api.Test;
  21. import org.junit.jupiter.api.extension.ExtendWith;
  22. import org.mapstruct.factory.Mappers;
  23. import org.mockito.InjectMocks;
  24. import org.mockito.Mock;
  25. import org.mockito.Spy;
  26. import org.mockito.junit.jupiter.MockitoExtension;
  27. @ExtendWith(MockitoExtension.class)
  28. class ClusterServiceTest {
  29. @Spy
  30. private final ClusterMapper clusterMapper = Mappers.getMapper(ClusterMapper.class);
  31. @InjectMocks
  32. private ClusterService clusterService;
  33. @Mock
  34. private ClustersStorage clustersStorage;
  35. @Mock
  36. private KafkaService kafkaService;
  37. @Test
  38. public void shouldListFirst25Topics() {
  39. var topicName = UUID.randomUUID().toString();
  40. final KafkaCluster cluster = KafkaCluster.builder()
  41. .topics(
  42. IntStream.rangeClosed(1, 100).boxed()
  43. .map(Objects::toString)
  44. .collect(Collectors.toMap(Function.identity(), e -> InternalTopic.builder()
  45. .partitions(Map.of())
  46. .name(e)
  47. .build()))
  48. )
  49. .build();
  50. when(clustersStorage.getClusterByName(topicName))
  51. .thenReturn(Optional.of(cluster));
  52. when(
  53. kafkaService.getTopicPartitions(any(), any())
  54. ).thenReturn(
  55. Map.of()
  56. );
  57. var topics = clusterService.getTopics(topicName,
  58. Optional.empty(), Optional.empty(), Optional.empty(),
  59. Optional.empty(), Optional.empty());
  60. assertThat(topics.getPageCount()).isEqualTo(4);
  61. assertThat(topics.getTopics()).hasSize(25);
  62. assertThat(topics.getTopics()).map(TopicDTO::getName).isSorted();
  63. }
  64. @Test
  65. public void shouldCalculateCorrectPageCountForNonDivisiblePageSize() {
  66. var topicName = UUID.randomUUID().toString();
  67. when(clustersStorage.getClusterByName(topicName))
  68. .thenReturn(Optional.of(KafkaCluster.builder()
  69. .topics(
  70. IntStream.rangeClosed(1, 100).boxed()
  71. .map(Objects::toString)
  72. .collect(Collectors.toMap(Function.identity(), e -> InternalTopic.builder()
  73. .partitions(Map.of())
  74. .name(e)
  75. .build()))
  76. )
  77. .build()));
  78. when(
  79. kafkaService.getTopicPartitions(any(), any())
  80. ).thenReturn(
  81. Map.of()
  82. );
  83. var topics = clusterService.getTopics(topicName, Optional.of(4), Optional.of(33),
  84. Optional.empty(), Optional.empty(), Optional.empty());
  85. assertThat(topics.getPageCount()).isEqualTo(4);
  86. assertThat(topics.getTopics()).hasSize(1)
  87. .first().extracting(TopicDTO::getName).isEqualTo("99");
  88. }
  89. @Test
  90. public void shouldCorrectlyHandleNonPositivePageNumberAndPageSize() {
  91. var topicName = UUID.randomUUID().toString();
  92. when(clustersStorage.getClusterByName(topicName))
  93. .thenReturn(Optional.of(KafkaCluster.builder()
  94. .topics(
  95. IntStream.rangeClosed(1, 100).boxed()
  96. .map(Objects::toString)
  97. .collect(Collectors.toMap(Function.identity(), e -> InternalTopic.builder()
  98. .partitions(Map.of())
  99. .name(e)
  100. .build()))
  101. )
  102. .build()));
  103. when(
  104. kafkaService.getTopicPartitions(any(), any())
  105. ).thenReturn(
  106. Map.of()
  107. );
  108. var topics = clusterService.getTopics(topicName, Optional.of(0), Optional.of(-1),
  109. Optional.empty(), Optional.empty(), Optional.empty());
  110. assertThat(topics.getPageCount()).isEqualTo(4);
  111. assertThat(topics.getTopics()).hasSize(25);
  112. assertThat(topics.getTopics()).map(TopicDTO::getName).isSorted();
  113. }
  114. @Test
  115. public void shouldListBotInternalAndNonInternalTopics() {
  116. var topicName = UUID.randomUUID().toString();
  117. when(clustersStorage.getClusterByName(topicName))
  118. .thenReturn(Optional.of(KafkaCluster.builder()
  119. .topics(
  120. IntStream.rangeClosed(1, 100).boxed()
  121. .map(Objects::toString)
  122. .collect(Collectors.toMap(Function.identity(), e -> InternalTopic.builder()
  123. .partitions(Map.of())
  124. .name(e)
  125. .internal(Integer.parseInt(e) % 10 == 0)
  126. .build()))
  127. )
  128. .build()));
  129. when(
  130. kafkaService.getTopicPartitions(any(), any())
  131. ).thenReturn(
  132. Map.of()
  133. );
  134. var topics = clusterService.getTopics(topicName,
  135. Optional.empty(), Optional.empty(), Optional.of(true),
  136. Optional.empty(), Optional.empty());
  137. assertThat(topics.getPageCount()).isEqualTo(4);
  138. assertThat(topics.getTopics()).hasSize(25);
  139. assertThat(topics.getTopics()).map(TopicDTO::getName).isSorted();
  140. }
  141. @Test
  142. public void shouldListOnlyNonInternalTopics() {
  143. var topicName = UUID.randomUUID().toString();
  144. when(clustersStorage.getClusterByName(topicName))
  145. .thenReturn(Optional.of(KafkaCluster.builder()
  146. .topics(
  147. IntStream.rangeClosed(1, 100).boxed()
  148. .map(Objects::toString)
  149. .collect(Collectors.toMap(Function.identity(), e -> InternalTopic.builder()
  150. .partitions(Map.of())
  151. .name(e)
  152. .internal(Integer.parseInt(e) % 10 == 0)
  153. .build()))
  154. )
  155. .build()));
  156. when(
  157. kafkaService.getTopicPartitions(any(), any())
  158. ).thenReturn(
  159. Map.of()
  160. );
  161. var topics = clusterService.getTopics(topicName,
  162. Optional.empty(), Optional.empty(), Optional.of(true),
  163. Optional.empty(), Optional.empty());
  164. assertThat(topics.getPageCount()).isEqualTo(4);
  165. assertThat(topics.getTopics()).hasSize(25);
  166. assertThat(topics.getTopics()).map(TopicDTO::getName).isSorted();
  167. }
  168. @Test
  169. public void shouldListOnlyTopicsContainingOne() {
  170. var topicName = UUID.randomUUID().toString();
  171. when(clustersStorage.getClusterByName(topicName))
  172. .thenReturn(Optional.of(KafkaCluster.builder()
  173. .topics(
  174. IntStream.rangeClosed(1, 100).boxed()
  175. .map(Objects::toString)
  176. .collect(Collectors.toMap(Function.identity(), e -> InternalTopic.builder()
  177. .partitions(Map.of())
  178. .name(e)
  179. .build()))
  180. )
  181. .build()));
  182. when(
  183. kafkaService.getTopicPartitions(any(), any())
  184. ).thenReturn(
  185. Map.of()
  186. );
  187. var topics = clusterService.getTopics(topicName,
  188. Optional.empty(), Optional.empty(), Optional.empty(),
  189. Optional.of("1"), Optional.empty());
  190. assertThat(topics.getPageCount()).isEqualTo(1);
  191. assertThat(topics.getTopics()).hasSize(20);
  192. assertThat(topics.getTopics()).map(TopicDTO::getName).isSorted();
  193. }
  194. @Test
  195. public void shouldListTopicsOrderedByPartitionsCount() {
  196. var topicName = UUID.randomUUID().toString();
  197. when(clustersStorage.getClusterByName(topicName))
  198. .thenReturn(Optional.of(KafkaCluster.builder()
  199. .topics(
  200. IntStream.rangeClosed(1, 100).boxed()
  201. .map(Objects::toString)
  202. .collect(Collectors.toMap(Function.identity(), e -> InternalTopic.builder()
  203. .partitions(Map.of())
  204. .name(e)
  205. .partitionCount(100 - Integer.parseInt(e))
  206. .build()))
  207. )
  208. .build()));
  209. when(
  210. kafkaService.getTopicPartitions(any(), any())
  211. ).thenReturn(
  212. Map.of()
  213. );
  214. var topics = clusterService.getTopics(topicName,
  215. Optional.empty(), Optional.empty(), Optional.empty(),
  216. Optional.empty(), Optional.of(TopicColumnsToSortDTO.TOTAL_PARTITIONS));
  217. assertThat(topics.getPageCount()).isEqualTo(4);
  218. assertThat(topics.getTopics()).hasSize(25);
  219. assertThat(topics.getTopics()).map(TopicDTO::getPartitionCount).isSorted();
  220. }
  221. @Test
  222. public void shouldRetrieveTopicConfigs() {
  223. var topicName = UUID.randomUUID().toString();
  224. when(clustersStorage.getClusterByName(topicName))
  225. .thenReturn(Optional.of(KafkaCluster.builder()
  226. .topics(
  227. IntStream.rangeClosed(1, 100).boxed()
  228. .map(Objects::toString)
  229. .collect(Collectors.toMap(Function.identity(), e -> InternalTopic.builder()
  230. .name(e)
  231. .topicConfigs(
  232. List.of(InternalTopicConfig.builder()
  233. .name("testName")
  234. .value("testValue")
  235. .defaultValue("testDefaultValue")
  236. .source(ConfigEntry.ConfigSource.DEFAULT_CONFIG)
  237. .isReadOnly(true)
  238. .isSensitive(true)
  239. .synonyms(List.of())
  240. .build()
  241. )
  242. )
  243. .build()))
  244. )
  245. .build()));
  246. var configs = clusterService.getTopicConfigs(topicName, "1");
  247. var topicConfig = configs.isPresent() ? configs.get().get(0) : null;
  248. assertThat(configs.isPresent()).isTrue();
  249. assertThat(topicConfig.getName()).isEqualTo("testName");
  250. assertThat(topicConfig.getValue()).isEqualTo("testValue");
  251. assertThat(topicConfig.getDefaultValue()).isEqualTo("testDefaultValue");
  252. assertThat(topicConfig.getSource().getValue())
  253. .isEqualTo(ConfigEntry.ConfigSource.DEFAULT_CONFIG.name());
  254. assertThat(topicConfig.getSynonyms()).isNotNull();
  255. assertThat(topicConfig.getIsReadOnly()).isTrue();
  256. assertThat(topicConfig.getIsSensitive()).isTrue();
  257. }
  258. }