Skip to content

Commit

Permalink
Move the param class in model.query
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasCAI-mlv committed Jul 18, 2024
1 parent 9d811ab commit c4a79b5
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import com.michelin.ns4kafka.controller.generic.NamespacedResourceController;
import com.michelin.ns4kafka.model.KafkaStream;
import com.michelin.ns4kafka.model.Namespace;
import com.michelin.ns4kafka.model.query.KafkaStreamFilterParams;
import com.michelin.ns4kafka.service.StreamService;
import com.michelin.ns4kafka.service.client.connect.entities.KafkaStreamSearchParams;
import com.michelin.ns4kafka.util.enumation.ApplyStatus;
import com.michelin.ns4kafka.util.exception.ResourceValidationException;
import io.micronaut.http.HttpResponse;
Expand Down Expand Up @@ -44,7 +44,7 @@ public class StreamController extends NamespacedResourceController {
*/
@Get
List<KafkaStream> list(String namespace, @QueryValue Optional<List<String>> name) {
KafkaStreamSearchParams params = KafkaStreamSearchParams.builder()
KafkaStreamFilterParams params = KafkaStreamFilterParams.builder()
.name(name.orElse(List.of("*")))
.build();
return streamService.findAllForNamespace(getNamespace(namespace), params);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.michelin.ns4kafka.model.query;

import java.util.List;
import lombok.Builder;

/**
* Kafka stream filter parameters in endpoints.
*
* @param name kafka stream name filter
*/
@Builder
public record KafkaStreamFilterParams(List<String> name) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import com.michelin.ns4kafka.model.AccessControlEntry;
import com.michelin.ns4kafka.model.KafkaStream;
import com.michelin.ns4kafka.model.Namespace;
import com.michelin.ns4kafka.model.query.KafkaStreamFilterParams;
import com.michelin.ns4kafka.repository.StreamRepository;
import com.michelin.ns4kafka.service.client.connect.entities.KafkaStreamSearchParams;
import com.michelin.ns4kafka.service.executor.AccessControlEntryAsyncExecutor;
import com.michelin.ns4kafka.util.RegexUtils;
import io.micronaut.context.ApplicationContext;
Expand Down Expand Up @@ -48,7 +48,7 @@ public List<KafkaStream> findAllForNamespace(Namespace namespace) {
* @param namespace The namespace
* @return A list of Kafka Streams
*/
public List<KafkaStream> findAllForNamespace(Namespace namespace, KafkaStreamSearchParams params) {
public List<KafkaStream> findAllForNamespace(Namespace namespace, KafkaStreamFilterParams params) {
List<String> nameFilterPatterns = RegexUtils.wildcardStringsToRegexPatterns(params.name());
return streamRepository.findAllForCluster(namespace.getMetadata().getCluster()).stream()
.filter(stream -> stream.getMetadata().getNamespace().equals(namespace.getMetadata().getName())
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
import com.michelin.ns4kafka.model.KafkaStream;
import com.michelin.ns4kafka.model.Metadata;
import com.michelin.ns4kafka.model.Namespace;
import com.michelin.ns4kafka.model.query.KafkaStreamFilterParams;
import com.michelin.ns4kafka.security.ResourceBasedSecurityRule;
import com.michelin.ns4kafka.service.NamespaceService;
import com.michelin.ns4kafka.service.StreamService;
import com.michelin.ns4kafka.service.client.connect.entities.KafkaStreamSearchParams;
import com.michelin.ns4kafka.util.exception.ResourceValidationException;
import io.micronaut.context.event.ApplicationEventPublisher;
import io.micronaut.http.HttpStatus;
Expand Down Expand Up @@ -56,7 +56,7 @@ void listEmptyStreams() {
.build();
when(namespaceService.findByName("test"))
.thenReturn(Optional.of(ns));
when(streamService.findAllForNamespace(ns, KafkaStreamSearchParams.builder().name(List.of("*")).build()))
when(streamService.findAllForNamespace(ns, KafkaStreamFilterParams.builder().name(List.of("*")).build()))
.thenReturn(List.of());

List<KafkaStream> actual = streamController.list("test", Optional.empty());
Expand Down Expand Up @@ -86,7 +86,7 @@ void listStreamsWithoutParameter() {

when(namespaceService.findByName("test"))
.thenReturn(Optional.of(ns));
when(streamService.findAllForNamespace(ns, KafkaStreamSearchParams.builder().name(List.of("*")).build()))
when(streamService.findAllForNamespace(ns, KafkaStreamFilterParams.builder().name(List.of("*")).build()))
.thenReturn(List.of(stream1, stream2));

List<KafkaStream> actual = streamController.list("test", Optional.empty());
Expand All @@ -109,7 +109,7 @@ void listStreamsWithNameParameters() {

List<String> nameParamsList = List.of("prefix.s1", "prefix.s2");
Optional<List<String>> nameParams = Optional.of(nameParamsList);
KafkaStreamSearchParams searchParams = KafkaStreamSearchParams.builder().name(nameParamsList).build();
KafkaStreamFilterParams searchParams = KafkaStreamFilterParams.builder().name(nameParamsList).build();

when(namespaceService.findByName("test"))
.thenReturn(Optional.of(ns));
Expand Down Expand Up @@ -137,7 +137,7 @@ void listStreamsWithEmptyNameParameter() {

when(namespaceService.findByName("test"))
.thenReturn(Optional.of(ns));
when(streamService.findAllForNamespace(ns, KafkaStreamSearchParams.builder().name(List.of("")).build()))
when(streamService.findAllForNamespace(ns, KafkaStreamFilterParams.builder().name(List.of("")).build()))
.thenReturn(List.of(stream1, stream2));

List<KafkaStream> actual = streamController.list("test", Optional.of(List.of("")));
Expand Down
50 changes: 25 additions & 25 deletions src/test/java/com/michelin/ns4kafka/service/StreamServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import com.michelin.ns4kafka.model.KafkaStream;
import com.michelin.ns4kafka.model.Metadata;
import com.michelin.ns4kafka.model.Namespace;
import com.michelin.ns4kafka.model.query.KafkaStreamFilterParams;
import com.michelin.ns4kafka.repository.StreamRepository;
import com.michelin.ns4kafka.service.client.connect.entities.KafkaStreamSearchParams;
import java.util.List;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -116,15 +116,15 @@ void findKafkaStreamWithNameParameter() {

when(streamRepository.findAllForCluster("local")).thenReturn(List.of(stream1, stream2, stream3));

KafkaStreamSearchParams params1 = KafkaStreamSearchParams.builder().name(List.of("test_stream3")).build();
KafkaStreamFilterParams params1 = KafkaStreamFilterParams.builder().name(List.of("test_stream3")).build();
List<KafkaStream> list1 = streamService.findAllForNamespace(ns, params1);
assertEquals(List.of(stream3), list1);

KafkaStreamSearchParams params2 = KafkaStreamSearchParams.builder().name(List.of("test_stream5")).build();
KafkaStreamFilterParams params2 = KafkaStreamFilterParams.builder().name(List.of("test_stream5")).build();
List<KafkaStream> list2 = streamService.findAllForNamespace(ns, params2);
assertTrue(list2.isEmpty());

KafkaStreamSearchParams params3 = KafkaStreamSearchParams.builder().name(List.of("")).build();
KafkaStreamFilterParams params3 = KafkaStreamFilterParams.builder().name(List.of("")).build();
List<KafkaStream> list3 = streamService.findAllForNamespace(ns, params3);
assertEquals(List.of(stream1, stream2, stream3), list3);
}
Expand Down Expand Up @@ -183,31 +183,31 @@ void findKafkaStreamWithWildcardNameParameter() {
when(streamRepository.findAllForCluster("local"))
.thenReturn(List.of(stream1, stream2, stream3, stream4, stream5, stream6));

KafkaStreamSearchParams params1 = KafkaStreamSearchParams.builder().name(List.of("test_*")).build();
KafkaStreamFilterParams params1 = KafkaStreamFilterParams.builder().name(List.of("test_*")).build();
assertEquals(List.of(stream1, stream2, stream3), streamService.findAllForNamespace(ns, params1));

KafkaStreamSearchParams params2 = KafkaStreamSearchParams.builder().name(List.of("test_stream?")).build();
KafkaStreamFilterParams params2 = KafkaStreamFilterParams.builder().name(List.of("test_stream?")).build();
assertEquals(List.of(stream1, stream2, stream3), streamService.findAllForNamespace(ns, params2));

KafkaStreamSearchParams params3 = KafkaStreamSearchParams.builder().name(List.of("*_*")).build();
KafkaStreamFilterParams params3 = KafkaStreamFilterParams.builder().name(List.of("*_*")).build();
assertEquals(List.of(stream1, stream2, stream3, stream5), streamService.findAllForNamespace(ns, params3));

KafkaStreamSearchParams params4 = KafkaStreamSearchParams.builder().name(List.of("test?stream1")).build();
KafkaStreamFilterParams params4 = KafkaStreamFilterParams.builder().name(List.of("test?stream1")).build();
assertEquals(List.of(stream1, stream4), streamService.findAllForNamespace(ns, params4));

KafkaStreamSearchParams params5 = KafkaStreamSearchParams.builder().name(List.of("*stream2*")).build();
KafkaStreamFilterParams params5 = KafkaStreamFilterParams.builder().name(List.of("*stream2*")).build();
assertEquals(List.of(stream2, stream5), streamService.findAllForNamespace(ns, params5));

KafkaStreamSearchParams params6 = KafkaStreamSearchParams.builder().name(List.of("*stream5")).build();
KafkaStreamFilterParams params6 = KafkaStreamFilterParams.builder().name(List.of("*stream5")).build();
assertTrue(streamService.findAllForNamespace(ns, params6).isEmpty());

KafkaStreamSearchParams params7 = KafkaStreamSearchParams.builder().name(List.of("test??stream1")).build();
KafkaStreamFilterParams params7 = KafkaStreamFilterParams.builder().name(List.of("test??stream1")).build();
assertTrue(streamService.findAllForNamespace(ns, params7).isEmpty());

KafkaStreamSearchParams params8 = KafkaStreamSearchParams.builder().name(List.of(".*")).build();
KafkaStreamFilterParams params8 = KafkaStreamFilterParams.builder().name(List.of(".*")).build();
assertTrue(streamService.findAllForNamespace(ns, params8).isEmpty());

KafkaStreamSearchParams params9 = KafkaStreamSearchParams.builder().name(List.of("*")).build();
KafkaStreamFilterParams params9 = KafkaStreamFilterParams.builder().name(List.of("*")).build();
assertEquals(List.of(stream1, stream2, stream3, stream4, stream5),
streamService.findAllForNamespace(ns, params9));
}
Expand Down Expand Up @@ -259,51 +259,51 @@ void findKafkaStreamWithMultipleNameParameters() {
when(streamRepository.findAllForCluster("local"))
.thenReturn(List.of(stream1, stream2, stream3, stream4, stream5));

KafkaStreamSearchParams params1 = KafkaStreamSearchParams.builder()
KafkaStreamFilterParams params1 = KafkaStreamFilterParams.builder()
.name(List.of("test_stream1", "test_stream2")).build();
assertEquals(List.of(stream1, stream2), streamService.findAllForNamespace(ns, params1));

KafkaStreamSearchParams params2 = KafkaStreamSearchParams.builder()
KafkaStreamFilterParams params2 = KafkaStreamFilterParams.builder()
.name(List.of("stream2_test", "test.stream10")).build();
assertEquals(List.of(stream5), streamService.findAllForNamespace(ns, params2));

KafkaStreamSearchParams params3 = KafkaStreamSearchParams.builder()
KafkaStreamFilterParams params3 = KafkaStreamFilterParams.builder()
.name(List.of("streamTEST", "TEST-stream3")).build();
assertTrue(streamService.findAllForNamespace(ns, params3).isEmpty());

KafkaStreamSearchParams params4 = KafkaStreamSearchParams.builder()
KafkaStreamFilterParams params4 = KafkaStreamFilterParams.builder()
.name(List.of("test_stream1", "test_stream1", "test_stream1")).build();
assertEquals(List.of(stream1), streamService.findAllForNamespace(ns, params4));

KafkaStreamSearchParams params5 = KafkaStreamSearchParams.builder()
KafkaStreamFilterParams params5 = KafkaStreamFilterParams.builder()
.name(List.of("test_*", "stream?_test")).build();
assertEquals(List.of(stream1, stream2, stream3, stream5), streamService.findAllForNamespace(ns, params5));

KafkaStreamSearchParams params6 = KafkaStreamSearchParams.builder()
KafkaStreamFilterParams params6 = KafkaStreamFilterParams.builder()
.name(List.of("test*", "stream??_test")).build();
assertEquals(List.of(stream1, stream2, stream3, stream4), streamService.findAllForNamespace(ns, params6));

KafkaStreamSearchParams params7 = KafkaStreamSearchParams.builder()
KafkaStreamFilterParams params7 = KafkaStreamFilterParams.builder()
.name(List.of("stream??_test", "kafka*stream*")).build();
assertTrue(streamService.findAllForNamespace(ns, params7).isEmpty());

KafkaStreamSearchParams params8 = KafkaStreamSearchParams.builder()
KafkaStreamFilterParams params8 = KafkaStreamFilterParams.builder()
.name(List.of("*_test", "*_test")).build();
assertEquals(List.of(stream5), streamService.findAllForNamespace(ns, params8));

KafkaStreamSearchParams params9 = KafkaStreamSearchParams.builder()
KafkaStreamFilterParams params9 = KafkaStreamFilterParams.builder()
.name(List.of("test_stream1", "test_stream3", "*_test")).build();
assertEquals(List.of(stream1, stream3, stream5), streamService.findAllForNamespace(ns, params9));

KafkaStreamSearchParams params10 = KafkaStreamSearchParams.builder()
KafkaStreamFilterParams params10 = KafkaStreamFilterParams.builder()
.name(List.of("test_stream9", "*_test")).build();
assertEquals(List.of(stream5), streamService.findAllForNamespace(ns, params10));

KafkaStreamSearchParams params11 = KafkaStreamSearchParams.builder()
KafkaStreamFilterParams params11 = KafkaStreamFilterParams.builder()
.name(List.of("*-dev", "stream_topic")).build();
assertTrue(streamService.findAllForNamespace(ns, params11).isEmpty());

KafkaStreamSearchParams params12 = KafkaStreamSearchParams.builder()
KafkaStreamFilterParams params12 = KafkaStreamFilterParams.builder()
.name(List.of("test_stream?", "test_stream1")).build();
assertEquals(List.of(stream1, stream2, stream3), streamService.findAllForNamespace(ns, params12));
}
Expand Down

0 comments on commit c4a79b5

Please sign in to comment.