Skip to content

Commit 318bcc9

Browse files
wernerdvHaarolean
andauthored
BE: Chore: Cleanup api tests (#698)
Co-authored-by: Roman Zabaluev <[email protected]>
1 parent 45324dd commit 318bcc9

27 files changed

+108
-102
lines changed

api/src/test/java/io/kafbat/ui/KafkaConnectServiceTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ public void shouldReturnNotFoundForNonExistingConnectName() {
166166

167167
@Test
168168
public void shouldRetrieveConnector() {
169-
ConnectorDTO expected = (ConnectorDTO) new ConnectorDTO()
169+
ConnectorDTO expected = new ConnectorDTO()
170170
.connect(connectName)
171171
.status(new ConnectorStatusDTO()
172172
.state(ConnectorStateDTO.RUNNING)
@@ -383,7 +383,7 @@ public void shouldValidateAndReturnErrorsOfConnectorPluginConfiguration() {
383383
.map(ConnectorPluginConfigDTO::getValue)
384384
.map(ConnectorPluginConfigValueDTO::getErrors)
385385
.filter(not(List::isEmpty))
386-
.findFirst().get();
386+
.findFirst().orElseThrow();
387387
assertEquals(
388388
"Invalid value 0 for configuration tasks.max: Value must be at least 1",
389389
error.get(0)

api/src/test/java/io/kafbat/ui/KafkaConsumerTests.java

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import io.kafbat.ui.producer.KafkaTestProducer;
1414
import java.util.List;
1515
import java.util.Map;
16+
import java.util.Objects;
1617
import java.util.UUID;
1718
import java.util.stream.Stream;
1819
import lombok.extern.slf4j.Slf4j;
@@ -55,15 +56,17 @@ public void shouldDeleteRecords() {
5556
throw new RuntimeException(e);
5657
}
5758

58-
long count = webTestClient.get()
59-
.uri("/api/clusters/{clusterName}/topics/{topicName}/messages/v2?mode=EARLIEST", LOCAL, topicName)
60-
.accept(TEXT_EVENT_STREAM)
61-
.exchange()
62-
.expectStatus()
63-
.isOk()
64-
.expectBodyList(TopicMessageEventDTO.class)
65-
.returnResult()
66-
.getResponseBody()
59+
long count = Objects.requireNonNull(
60+
webTestClient.get()
61+
.uri("/api/clusters/{clusterName}/topics/{topicName}/messages/v2?mode=EARLIEST", LOCAL, topicName)
62+
.accept(TEXT_EVENT_STREAM)
63+
.exchange()
64+
.expectStatus()
65+
.isOk()
66+
.expectBodyList(TopicMessageEventDTO.class)
67+
.returnResult()
68+
.getResponseBody()
69+
)
6770
.stream()
6871
.filter(e -> e.getType().equals(TopicMessageEventDTO.TypeEnum.MESSAGE))
6972
.count();
@@ -76,14 +79,16 @@ public void shouldDeleteRecords() {
7679
.expectStatus()
7780
.isOk();
7881

79-
count = webTestClient.get()
80-
.uri("/api/clusters/{clusterName}/topics/{topicName}/messages/v2?mode=EARLIEST", LOCAL, topicName)
81-
.exchange()
82-
.expectStatus()
83-
.isOk()
84-
.expectBodyList(TopicMessageEventDTO.class)
85-
.returnResult()
86-
.getResponseBody()
82+
count = Objects.requireNonNull(
83+
webTestClient.get()
84+
.uri("/api/clusters/{clusterName}/topics/{topicName}/messages/v2?mode=EARLIEST", LOCAL, topicName)
85+
.exchange()
86+
.expectStatus()
87+
.isOk()
88+
.expectBodyList(TopicMessageEventDTO.class)
89+
.returnResult()
90+
.getResponseBody()
91+
)
8792
.stream()
8893
.filter(e -> e.getType().equals(TopicMessageEventDTO.TypeEnum.MESSAGE))
8994
.count();
@@ -120,7 +125,7 @@ public void shouldIncreasePartitionsUpTo10() {
120125
.returnResult()
121126
.getResponseBody();
122127

123-
assert response != null;
128+
Assertions.assertNotNull(response);
124129
Assertions.assertEquals(10, response.getTotalPartitionsCount());
125130

126131
TopicDetailsDTO topicDetails = webTestClient.get()
@@ -134,7 +139,7 @@ public void shouldIncreasePartitionsUpTo10() {
134139
.returnResult()
135140
.getResponseBody();
136141

137-
assert topicDetails != null;
142+
Assertions.assertNotNull(topicDetails);
138143
Assertions.assertEquals(10, topicDetails.getPartitionCount());
139144
}
140145

@@ -157,8 +162,6 @@ public void shouldReturn404ForNonExistingTopic() {
157162

158163
@Test
159164
public void shouldReturnConfigsForBroker() {
160-
var topicName = UUID.randomUUID().toString();
161-
162165
List<BrokerConfigDTO> configs = webTestClient.get()
163166
.uri("/api/clusters/{clusterName}/brokers/{id}/configs",
164167
LOCAL,
@@ -171,7 +174,7 @@ public void shouldReturnConfigsForBroker() {
171174
.getResponseBody();
172175

173176
Assertions.assertNotNull(configs);
174-
assert !configs.isEmpty();
177+
Assertions.assertFalse(configs.isEmpty());
175178
Assertions.assertNotNull(configs.get(0).getName());
176179
Assertions.assertNotNull(configs.get(0).getIsReadOnly());
177180
Assertions.assertNotNull(configs.get(0).getIsSensitive());
@@ -216,7 +219,7 @@ public void shouldRetrieveTopicConfig() {
216219
.getResponseBody();
217220

218221
Assertions.assertNotNull(configs);
219-
assert !configs.isEmpty();
222+
Assertions.assertFalse(configs.isEmpty());
220223
Assertions.assertNotNull(configs.get(0).getName());
221224
Assertions.assertNotNull(configs.get(0).getIsReadOnly());
222225
Assertions.assertNotNull(configs.get(0).getIsSensitive());

api/src/test/java/io/kafbat/ui/config/auth/azure/AzureEntraLoginCallbackHandlerTest.java

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import com.azure.core.credential.AccessToken;
1818
import com.azure.core.credential.TokenCredential;
1919
import com.azure.core.credential.TokenRequestContext;
20-
import java.util.HashMap;
2120
import java.util.List;
2221
import java.util.Map;
2322
import javax.security.auth.callback.Callback;
@@ -59,9 +58,6 @@ public class AzureEntraLoginCallbackHandlerTest {
5958
@Mock
6059
private OAuthBearerTokenCallback oauthBearerTokenCallBack;
6160

62-
@Mock
63-
private OAuthBearerToken oauthBearerToken;
64-
6561
@Mock
6662
private TokenCredential tokenCredential;
6763

@@ -77,12 +73,8 @@ public void beforeEach() {
7773
}
7874

7975
@Test
80-
public void shouldProvideTokenToCallbackWithSuccessfulTokenRequest()
81-
throws UnsupportedCallbackException {
82-
final Map<String, Object> configs = new HashMap<>();
83-
configs.put(
84-
"bootstrap.servers",
85-
List.of("test-eh.servicebus.windows.net:9093"));
76+
public void shouldProvideTokenToCallbackWithSuccessfulTokenRequest() throws UnsupportedCallbackException {
77+
Map<String, Object> configs = Map.of("bootstrap.servers", List.of("test-eh.servicebus.windows.net:9093"));
8678

8779
when(tokenCredential.getToken(any(TokenRequestContext.class))).thenReturn(Mono.just(accessToken));
8880
when(accessToken.getToken()).thenReturn(VALID_SAMPLE_TOKEN);
@@ -114,10 +106,7 @@ public void shouldProvideTokenToCallbackWithSuccessfulTokenRequest()
114106

115107
@Test
116108
public void shouldProvideErrorToCallbackWithTokenError() throws UnsupportedCallbackException {
117-
final Map<String, Object> configs = new HashMap<>();
118-
configs.put(
119-
"bootstrap.servers",
120-
List.of("test-eh.servicebus.windows.net:9093"));
109+
Map<String, Object> configs = Map.of("bootstrap.servers", List.of("test-eh.servicebus.windows.net:9093"));
121110

122111
when(tokenCredential.getToken(any(TokenRequestContext.class)))
123112
.thenThrow(new RuntimeException("failed to acquire token"));
@@ -136,16 +125,13 @@ public void shouldProvideErrorToCallbackWithTokenError() throws UnsupportedCallb
136125

137126
@Test
138127
public void shouldThrowExceptionWithNullBootstrapServers() {
139-
final Map<String, Object> configs = new HashMap<>();
140-
141128
assertThrows(IllegalArgumentException.class, () -> azureEntraLoginCallbackHandler.configure(
142-
configs, null, null));
129+
Map.of(), null, null));
143130
}
144131

145132
@Test
146133
public void shouldThrowExceptionWithMultipleBootstrapServers() {
147-
final Map<String, Object> configs = new HashMap<>();
148-
configs.put("bootstrap.servers", List.of("server1", "server2"));
134+
Map<String, Object> configs = Map.of("bootstrap.servers", List.of("server1", "server2"));
149135

150136
assertThrows(IllegalArgumentException.class, () -> azureEntraLoginCallbackHandler.configure(
151137
configs, null, null));

api/src/test/java/io/kafbat/ui/config/auth/azure/AzureEntraOAuthBearerTokenTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ public class AzureEntraOAuthBearerTokenTest {
3939
void constructorShouldParseToken() {
4040
final AccessToken accessToken = new AccessToken(VALID_SAMPLE_TOKEN, OffsetDateTime.MIN);
4141

42-
final AzureEntraOAuthBearerToken azureOAuthBearerToken =
43-
new AzureEntraOAuthBearerToken(accessToken);
42+
final AzureEntraOAuthBearerToken azureOAuthBearerToken = new AzureEntraOAuthBearerToken(accessToken);
4443

4544
assertThat(azureOAuthBearerToken, is(notNullValue()));
4645
assertThat(azureOAuthBearerToken.value(), is(VALID_SAMPLE_TOKEN));

api/src/test/java/io/kafbat/ui/container/KafkaConnectContainer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,6 @@ public KafkaConnectContainer withKafka(Network network, String bootstrapServers)
4747
}
4848

4949
public String getTarget() {
50-
return "http://" + getContainerIpAddress() + ":" + getMappedPort(CONNECT_PORT);
50+
return "http://" + getHost() + ":" + getMappedPort(CONNECT_PORT);
5151
}
5252
}

api/src/test/java/io/kafbat/ui/container/KsqlDbContainer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@ private KsqlDbContainer withKafka(Network network, String bootstrapServers) {
3434
}
3535

3636
public String url() {
37-
return "http://" + getContainerIpAddress() + ":" + getMappedPort(PORT);
37+
return "http://" + getHost() + ":" + getMappedPort(PORT);
3838
}
3939
}

api/src/test/java/io/kafbat/ui/container/SchemaRegistryContainer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public SchemaRegistryContainer withKafka(Network network, String bootstrapServer
2828
}
2929

3030
public String getUrl() {
31-
return "http://" + getContainerIpAddress() + ":" + getMappedPort(SCHEMA_PORT);
31+
return "http://" + getHost() + ":" + getMappedPort(SCHEMA_PORT);
3232
}
3333

3434
public SchemaRegistryClient schemaRegistryClient() {

api/src/test/java/io/kafbat/ui/emitter/CursorTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ private Cursor assertCursor(PollingModeDTO expectedMode,
9999

100100
var cursorPosition = registeredCursor.consumerPosition();
101101
assertThat(cursorPosition).isNotNull();
102+
assertThat(cursorPosition.offsets()).isNotNull();
102103
assertThat(cursorPosition.topic()).isEqualTo(TOPIC);
103104
assertThat(cursorPosition.partitions()).isEqualTo(List.of());
104105
assertThat(cursorPosition.pollingMode()).isEqualTo(expectedMode);
@@ -111,6 +112,7 @@ private void waitMgsgEmitted(AbstractEmitter emitter, int expectedMsgsCnt) {
111112
List<TopicMessageEventDTO> events = Flux.create(emitter)
112113
.collectList()
113114
.block();
115+
assertThat(events).isNotNull();
114116
assertThat(events.stream().filter(m -> m.getType() == TopicMessageEventDTO.TypeEnum.MESSAGE).count())
115117
.isEqualTo(expectedMsgsCnt);
116118
}

api/src/test/java/io/kafbat/ui/emitter/TailingEmitterTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ private Flux<TopicMessageEventDTO> createTailingFlux(
106106
String query) {
107107
var cluster = applicationContext.getBean(ClustersStorage.class)
108108
.getClusterByName(LOCAL)
109-
.get();
109+
.orElseThrow();
110110

111111
return applicationContext.getBean(MessagesService.class)
112112
.loadMessages(cluster, topicName,

api/src/test/java/io/kafbat/ui/serdes/SerdesInitializerTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import static org.mockito.Mockito.any;
66
import static org.mockito.Mockito.anyString;
77
import static org.mockito.Mockito.eq;
8-
import static org.mockito.Mockito.mock;
98
import static org.mockito.Mockito.verify;
109
import static org.mockito.Mockito.when;
1110

@@ -145,6 +144,8 @@ private void verifyAutoConfigured(SerdeInstance serde) {
145144
}
146145

147146
private void verifyPatternsMatch(ClustersProperties.SerdeConfig config, SerdeInstance serde) {
147+
assertThat(serde.topicKeyPattern).isNotNull();
148+
assertThat(serde.topicValuePattern).isNotNull();
148149
assertThat(serde.topicKeyPattern.pattern()).isEqualTo(config.getTopicKeysPattern());
149150
assertThat(serde.topicValuePattern.pattern()).isEqualTo(config.getTopicValuesPattern());
150151
}

api/src/test/java/io/kafbat/ui/serdes/builtin/ConsumerOffsetsSerdeTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ private boolean isGroupMetadataMessage(DeserializeResult key, DeserializeResult
168168
}
169169

170170
@SneakyThrows
171+
@SuppressWarnings("unchecked")
171172
private Map<String, Object> toMapFromJsom(DeserializeResult result) {
172173
return new JsonMapper().readValue(result.getResult(), Map.class);
173174
}

api/src/test/java/io/kafbat/ui/serdes/builtin/ProtobufFileSerdeTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ void createConfigureFillsDescriptorMappingsWhenProtoFilesListProvided() throws E
226226
}
227227

228228
@Test
229-
void createConfigureFillsDescriptorMappingsWhenProtoFileDirProvided() throws Exception {
229+
void createConfigureFillsDescriptorMappingsWhenProtoFileDirProvided() {
230230
PropertyResolver resolver = mock(PropertyResolver.class);
231231
when(resolver.getProperty("protobufFilesDir", String.class))
232232
.thenReturn(Optional.of(protoFilesDir()));

api/src/test/java/io/kafbat/ui/service/BrokerServiceTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class BrokerServiceTest extends AbstractIntegrationTest {
1515

1616
@Test
1717
void getBrokersReturnsFilledBrokerDto() {
18-
var localCluster = clustersStorage.getClusterByName(LOCAL).get();
18+
var localCluster = clustersStorage.getClusterByName(LOCAL).orElseThrow();
1919
StepVerifier.create(brokerService.getBrokers(localCluster))
2020
.expectNextMatches(b -> b.getId().equals(1)
2121
&& b.getHost().equals(kafka.getHost())

api/src/test/java/io/kafbat/ui/service/ConfigTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ private Optional<BrokerConfigDTO> getConfig(String name) {
8989
.returnResult()
9090
.getResponseBody();
9191

92+
assertThat(configs).isNotNull();
93+
9294
return configs.stream()
9395
.filter(c -> c.getName().equals(name))
9496
.findAny();

api/src/test/java/io/kafbat/ui/service/LogDirsTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public void testAllBrokers() {
3838

3939
BrokerTopicLogdirsDTO topic = dir.getTopics().stream()
4040
.filter(t -> t.getName().equals("__consumer_offsets"))
41-
.findAny().get();
41+
.findAny().orElseThrow();
4242

4343
assertThat(topic.getPartitions()).hasSize(1);
4444
assertThat(topic.getPartitions().get(0).getBroker()).isEqualTo(1);
@@ -63,7 +63,7 @@ public void testOneBrokers() {
6363

6464
BrokerTopicLogdirsDTO topic = dir.getTopics().stream()
6565
.filter(t -> t.getName().equals("__consumer_offsets"))
66-
.findAny().get();
66+
.findAny().orElseThrow();
6767

6868
assertThat(topic.getPartitions()).hasSize(1);
6969
assertThat(topic.getPartitions().get(0).getBroker()).isEqualTo(1);
@@ -99,6 +99,7 @@ public void testChangeDirToWrongDir() {
9999
.returnResult()
100100
.getResponseBody();
101101

102+
assertThat(dirs).isNotNull();
102103
assertThat(dirs.getMessage())
103104
.isEqualTo(new LogDirNotFoundApiException().getMessage());
104105

@@ -116,6 +117,7 @@ public void testChangeDirToWrongDir() {
116117
.returnResult()
117118
.getResponseBody();
118119

120+
assertThat(dirs).isNotNull();
119121
assertThat(dirs.getMessage())
120122
.isEqualTo(new TopicOrPartitionNotFoundException().getMessage());
121123
}

api/src/test/java/io/kafbat/ui/service/MessagesServiceTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import io.kafbat.ui.model.KafkaCluster;
1111
import io.kafbat.ui.model.PollingModeDTO;
1212
import io.kafbat.ui.model.SmartFilterTestExecutionDTO;
13-
import io.kafbat.ui.model.SmartFilterTestExecutionResultDTO;
1413
import io.kafbat.ui.model.TopicMessageDTO;
1514
import io.kafbat.ui.model.TopicMessageEventDTO;
1615
import io.kafbat.ui.producer.KafkaTestProducer;
@@ -48,7 +47,7 @@ void init() {
4847
cluster = applicationContext
4948
.getBean(ClustersStorage.class)
5049
.getClusterByName(LOCAL)
51-
.get();
50+
.orElseThrow();
5251
}
5352

5453
@AfterEach

api/src/test/java/io/kafbat/ui/service/OffsetsResetServiceTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public class OffsetsResetServiceTest extends AbstractIntegrationTest {
4545

4646
@BeforeEach
4747
void init() {
48-
cluster = applicationContext.getBean(ClustersStorage.class).getClusterByName(LOCAL).get();
48+
cluster = applicationContext.getBean(ClustersStorage.class).getClusterByName(LOCAL).orElseThrow();
4949
offsetsResetService = new OffsetsResetService(applicationContext.getBean(AdminClientService.class));
5050
createTopic(new NewTopic(topic, PARTITIONS, (short) 1));
5151
createConsumerGroup();

0 commit comments

Comments
 (0)