KsqlServiceTest.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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.times;
  5. import static org.mockito.Mockito.verify;
  6. import static org.mockito.Mockito.when;
  7. import com.provectus.kafka.ui.client.KsqlClient;
  8. import com.provectus.kafka.ui.exception.ClusterNotFoundException;
  9. import com.provectus.kafka.ui.exception.KsqlDbNotFoundException;
  10. import com.provectus.kafka.ui.exception.UnprocessableEntityException;
  11. import com.provectus.kafka.ui.model.KafkaCluster;
  12. import com.provectus.kafka.ui.model.KsqlCommand;
  13. import com.provectus.kafka.ui.model.KsqlCommandResponse;
  14. import com.provectus.kafka.ui.strategy.ksql.statement.BaseStrategy;
  15. import com.provectus.kafka.ui.strategy.ksql.statement.ShowStrategy;
  16. import java.util.List;
  17. import java.util.Optional;
  18. import org.junit.jupiter.api.BeforeEach;
  19. import org.junit.jupiter.api.Test;
  20. import org.junit.jupiter.api.extension.ExtendWith;
  21. import org.mockito.Mock;
  22. import org.mockito.Mockito;
  23. import org.mockito.junit.jupiter.MockitoExtension;
  24. import reactor.core.publisher.Mono;
  25. import reactor.test.StepVerifier;
  26. @ExtendWith(MockitoExtension.class)
  27. class KsqlServiceTest {
  28. private KsqlService ksqlService;
  29. private BaseStrategy baseStrategy;
  30. @Mock
  31. private ClustersStorage clustersStorage;
  32. @Mock
  33. private KsqlClient ksqlClient;
  34. @BeforeEach
  35. public void setUp() {
  36. this.baseStrategy = new ShowStrategy();
  37. this.ksqlService = new KsqlService(
  38. this.ksqlClient,
  39. this.clustersStorage,
  40. List.of(baseStrategy)
  41. );
  42. }
  43. @Test
  44. void shouldThrowClusterNotFoundExceptionOnExecuteKsqlCommand() {
  45. String clusterName = "test";
  46. KsqlCommand command = (new KsqlCommand()).ksql("show streams;");
  47. when(clustersStorage.getClusterByName(clusterName)).thenReturn(Optional.ofNullable(null));
  48. StepVerifier.create(ksqlService.executeKsqlCommand(clusterName, Mono.just(command)))
  49. .verifyError(ClusterNotFoundException.class);
  50. }
  51. @Test
  52. void shouldThrowKsqlDbNotFoundExceptionOnExecuteKsqlCommand() {
  53. String clusterName = "test";
  54. KsqlCommand command = (new KsqlCommand()).ksql("show streams;");
  55. KafkaCluster kafkaCluster = Mockito.mock(KafkaCluster.class);
  56. when(clustersStorage.getClusterByName(clusterName))
  57. .thenReturn(Optional.ofNullable(kafkaCluster));
  58. when(kafkaCluster.getKsqldbServer()).thenReturn(null);
  59. StepVerifier.create(ksqlService.executeKsqlCommand(clusterName, Mono.just(command)))
  60. .verifyError(KsqlDbNotFoundException.class);
  61. }
  62. @Test
  63. void shouldThrowUnprocessableEntityExceptionOnExecuteKsqlCommand() {
  64. String clusterName = "test";
  65. KsqlCommand command =
  66. (new KsqlCommand()).ksql("CREATE STREAM users WITH (KAFKA_TOPIC='users');");
  67. KafkaCluster kafkaCluster = Mockito.mock(KafkaCluster.class);
  68. when(clustersStorage.getClusterByName(clusterName))
  69. .thenReturn(Optional.ofNullable(kafkaCluster));
  70. when(kafkaCluster.getKsqldbServer()).thenReturn("localhost:8088");
  71. StepVerifier.create(ksqlService.executeKsqlCommand(clusterName, Mono.just(command)))
  72. .verifyError(UnprocessableEntityException.class);
  73. StepVerifier.create(ksqlService.executeKsqlCommand(clusterName, Mono.just(command)))
  74. .verifyErrorMessage("Invalid sql");
  75. }
  76. @Test
  77. void shouldSetHostToStrategy() {
  78. String clusterName = "test";
  79. String host = "localhost:8088";
  80. KsqlCommand command = (new KsqlCommand()).ksql("show streams;");
  81. KafkaCluster kafkaCluster = Mockito.mock(KafkaCluster.class);
  82. when(clustersStorage.getClusterByName(clusterName))
  83. .thenReturn(Optional.ofNullable(kafkaCluster));
  84. when(kafkaCluster.getKsqldbServer()).thenReturn(host);
  85. when(ksqlClient.execute(any())).thenReturn(Mono.just(new KsqlCommandResponse()));
  86. ksqlService.executeKsqlCommand(clusterName, Mono.just(command)).block();
  87. assertThat(baseStrategy.getUri()).isEqualTo(host + "/ksql");
  88. }
  89. @Test
  90. void shouldCallClientAndReturnResponse() {
  91. String clusterName = "test";
  92. KsqlCommand command = (new KsqlCommand()).ksql("show streams;");
  93. KafkaCluster kafkaCluster = Mockito.mock(KafkaCluster.class);
  94. KsqlCommandResponse response = new KsqlCommandResponse().message("success");
  95. when(clustersStorage.getClusterByName(clusterName))
  96. .thenReturn(Optional.ofNullable(kafkaCluster));
  97. when(kafkaCluster.getKsqldbServer()).thenReturn("host");
  98. when(ksqlClient.execute(any())).thenReturn(Mono.just(response));
  99. KsqlCommandResponse receivedResponse =
  100. ksqlService.executeKsqlCommand(clusterName, Mono.just(command)).block();
  101. verify(ksqlClient, times(1)).execute(baseStrategy);
  102. assertThat(receivedResponse).isEqualTo(response);
  103. }
  104. }