Skip to content

Commit

Permalink
Add unit/integration tests
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Edgar <[email protected]>
  • Loading branch information
MikeEdgar committed Nov 1, 2024
1 parent ec9683b commit 7ce7881
Show file tree
Hide file tree
Showing 9 changed files with 520 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -335,4 +335,8 @@ public void cruiseControlEnabled(boolean cruiseControlEnabled) {
public Metrics metrics() {
return attributes.metrics;
}

public void metrics(Metrics metrics) {
attributes.metrics = metrics;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ CompletionStage<KafkaCluster> addMetrics(KafkaCluster cluster, List<String> fiel

if (kafkaContext.prometheus() == null) {
logger.warnf("Kafka cluster metrics were requested, but Prometheus URL is not configured");
cluster.metrics(null);
return CompletableFuture.completedStage(cluster);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.function.BiFunction;
Expand Down Expand Up @@ -61,6 +62,12 @@ public class MetricsService {
@Inject
KafkaContext kafkaContext;

Optional<ClientRequestFilter> additionalFilter = Optional.empty();

public /* test */ void setAdditionalFilter(Optional<ClientRequestFilter> additionalFilter) {
this.additionalFilter = additionalFilter;
}

ClientRequestFilter createAuthenticationFilter(PrometheusConfig config) {
return new ClientRequestFilter() {
@Override
Expand Down Expand Up @@ -99,11 +106,14 @@ public PrometheusAPI createClient(ConsoleConfig consoleConfig, KafkaClusterConfi

var trustStore = certificates.getDefault().map(TlsConfiguration::getTrustStore).orElse(null);

return RestClientBuilder.newBuilder()
RestClientBuilder builder = RestClientBuilder.newBuilder()
.baseUri(URI.create(prometheusConfig.getUrl()))
.trustStore(trustStore)
.register(createAuthenticationFilter(prometheusConfig))
.build(PrometheusAPI.class);
.register(createAuthenticationFilter(prometheusConfig));

additionalFilter.ifPresent(builder::register);

return builder.build(PrometheusAPI.class);
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.junit.jupiter.params.provider.CsvSource;
import org.mockito.Mockito;

import com.github.streamshub.console.api.model.KafkaCluster;
import com.github.streamshub.console.api.model.ListFetchParams;
import com.github.streamshub.console.api.service.KafkaClusterService;
import com.github.streamshub.console.api.support.ErrorCategory;
Expand Down Expand Up @@ -739,6 +740,20 @@ void testDescribeClusterWithScram(boolean tls, String expectedProtocol) {
containsString(ScramLoginModule.class.getName()));
}

@Test
/*
* Tests with metrics enabled are in KafkaClustersResourceMetricsIT
*/
void testDescribeClusterWithMetricsNotEnabled() {
whenRequesting(req -> req
.param("fields[" + KafkaCluster.API_TYPE + "]", "name,metrics")
.get("{clusterId}", clusterId1))
.assertThat()
.statusCode(is(Status.OK.getStatusCode()))
.body("data.attributes.name", equalTo("test-kafka1"))
.body("data.attributes", hasEntry(is("metrics"), nullValue()));
}

@ParameterizedTest
@CsvSource({
"true",
Expand Down
Loading

0 comments on commit 7ce7881

Please sign in to comment.