From 72193c4394d0bdf097d214bbe37314648d5ddc35 Mon Sep 17 00:00:00 2001 From: thcai Date: Thu, 18 Jul 2024 09:53:43 +0200 Subject: [PATCH 01/10] initial commit --- .../model/query/RolebindingFilterParams.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 src/main/java/com/michelin/ns4kafka/model/query/RolebindingFilterParams.java diff --git a/src/main/java/com/michelin/ns4kafka/model/query/RolebindingFilterParams.java b/src/main/java/com/michelin/ns4kafka/model/query/RolebindingFilterParams.java new file mode 100644 index 00000000..302113ae --- /dev/null +++ b/src/main/java/com/michelin/ns4kafka/model/query/RolebindingFilterParams.java @@ -0,0 +1,13 @@ +package com.michelin.ns4kafka.model.query; + +import java.util.List; +import lombok.Builder; + + /** + * Role binding filter parameters in endpoints. + * + * @param name role binding name filter + */ +@Builder +public record RolebindingFilterParams(List name) { +} From 9f53dd617d6cb94053f61ab4f5fc417c78866af1 Mon Sep 17 00:00:00 2001 From: thcai Date: Thu, 18 Jul 2024 12:20:04 +0200 Subject: [PATCH 02/10] Feature + controller TU --- .../controller/RoleBindingController.java | 8 +++-- ...rams.java => RoleBindingFilterParams.java} | 4 +-- .../repository/RoleBindingRepository.java | 9 +++++ .../kafka/KafkaRoleBindingRepository.java | 21 +++++++++++ .../ns4kafka/service/RoleBindingService.java | 13 ++++++- .../controller/RoleBindingControllerTest.java | 36 +++++++++++++++++++ 6 files changed, 86 insertions(+), 5 deletions(-) rename src/main/java/com/michelin/ns4kafka/model/query/{RolebindingFilterParams.java => RoleBindingFilterParams.java} (75%) diff --git a/src/main/java/com/michelin/ns4kafka/controller/RoleBindingController.java b/src/main/java/com/michelin/ns4kafka/controller/RoleBindingController.java index 75866519..05e21aad 100644 --- a/src/main/java/com/michelin/ns4kafka/controller/RoleBindingController.java +++ b/src/main/java/com/michelin/ns4kafka/controller/RoleBindingController.java @@ -3,6 +3,7 @@ import com.michelin.ns4kafka.controller.generic.NamespacedResourceController; import com.michelin.ns4kafka.model.Namespace; import com.michelin.ns4kafka.model.RoleBinding; +import com.michelin.ns4kafka.model.query.RoleBindingFilterParams; import com.michelin.ns4kafka.service.RoleBindingService; import com.michelin.ns4kafka.util.enumation.ApplyStatus; import io.micronaut.http.HttpResponse; @@ -41,8 +42,11 @@ public class RoleBindingController extends NamespacedResourceController { * @return A list of role bindings */ @Get - public List list(String namespace) { - return roleBindingService.list(namespace); + public List list(String namespace, @QueryValue Optional> name) { + RoleBindingFilterParams params = RoleBindingFilterParams.builder() + .name(name.orElse(List.of("*"))) + .build(); + return roleBindingService.list(namespace, params); } /** diff --git a/src/main/java/com/michelin/ns4kafka/model/query/RolebindingFilterParams.java b/src/main/java/com/michelin/ns4kafka/model/query/RoleBindingFilterParams.java similarity index 75% rename from src/main/java/com/michelin/ns4kafka/model/query/RolebindingFilterParams.java rename to src/main/java/com/michelin/ns4kafka/model/query/RoleBindingFilterParams.java index 302113ae..48ebe83f 100644 --- a/src/main/java/com/michelin/ns4kafka/model/query/RolebindingFilterParams.java +++ b/src/main/java/com/michelin/ns4kafka/model/query/RoleBindingFilterParams.java @@ -3,11 +3,11 @@ import java.util.List; import lombok.Builder; - /** +/** * Role binding filter parameters in endpoints. * * @param name role binding name filter */ @Builder -public record RolebindingFilterParams(List name) { +public record RoleBindingFilterParams(List name) { } diff --git a/src/main/java/com/michelin/ns4kafka/repository/RoleBindingRepository.java b/src/main/java/com/michelin/ns4kafka/repository/RoleBindingRepository.java index 8aa087fd..d20a1601 100644 --- a/src/main/java/com/michelin/ns4kafka/repository/RoleBindingRepository.java +++ b/src/main/java/com/michelin/ns4kafka/repository/RoleBindingRepository.java @@ -1,6 +1,7 @@ package com.michelin.ns4kafka.repository; import com.michelin.ns4kafka.model.RoleBinding; +import com.michelin.ns4kafka.model.query.RoleBindingFilterParams; import java.util.Collection; import java.util.List; @@ -24,6 +25,14 @@ public interface RoleBindingRepository { */ List findAllForNamespace(String namespace); + /** + * List role bindings of a given namespace, filtered by given parameters. + * + * @param namespace The namespace used to research + * @return The list of associated role bindings + */ + List findAllForNamespace(String namespace, RoleBindingFilterParams params); + /** * Create a role binding. * diff --git a/src/main/java/com/michelin/ns4kafka/repository/kafka/KafkaRoleBindingRepository.java b/src/main/java/com/michelin/ns4kafka/repository/kafka/KafkaRoleBindingRepository.java index 880e0fd8..79c120e5 100644 --- a/src/main/java/com/michelin/ns4kafka/repository/kafka/KafkaRoleBindingRepository.java +++ b/src/main/java/com/michelin/ns4kafka/repository/kafka/KafkaRoleBindingRepository.java @@ -1,7 +1,9 @@ package com.michelin.ns4kafka.repository.kafka; import com.michelin.ns4kafka.model.RoleBinding; +import com.michelin.ns4kafka.model.query.RoleBindingFilterParams; import com.michelin.ns4kafka.repository.RoleBindingRepository; +import com.michelin.ns4kafka.util.RegexUtils; import io.micronaut.configuration.kafka.annotation.KafkaClient; import io.micronaut.configuration.kafka.annotation.KafkaListener; import io.micronaut.configuration.kafka.annotation.OffsetReset; @@ -11,6 +13,7 @@ import jakarta.inject.Singleton; import java.util.Collection; import java.util.List; +import java.util.regex.Pattern; import org.apache.kafka.clients.consumer.ConsumerRecord; import org.apache.kafka.clients.producer.Producer; @@ -109,4 +112,22 @@ public List findAllForNamespace(String namespace) { .filter(roleBinding -> roleBinding.getMetadata().getNamespace().equals(namespace)) .toList(); } + + /** + * List role bindings of a given namespace, filtered by given parameters. + * + * @param namespace The namespace used to research + * @param params The filter parameters + * @return The list of associated role bindings + */ + @Override + public List findAllForNamespace(String namespace, RoleBindingFilterParams params) { + List nameFilterPatterns = RegexUtils.wildcardStringsToRegexPatterns(params.name()); + return getKafkaStore().values() + .stream() + .filter(roleBinding -> roleBinding.getMetadata().getNamespace().equals(namespace) + && nameFilterPatterns.stream().anyMatch(pattern -> + Pattern.compile(pattern).matcher(roleBinding.getMetadata().getName()).matches())) + .toList(); + } } diff --git a/src/main/java/com/michelin/ns4kafka/service/RoleBindingService.java b/src/main/java/com/michelin/ns4kafka/service/RoleBindingService.java index ffec1887..b596e7c0 100644 --- a/src/main/java/com/michelin/ns4kafka/service/RoleBindingService.java +++ b/src/main/java/com/michelin/ns4kafka/service/RoleBindingService.java @@ -1,6 +1,7 @@ package com.michelin.ns4kafka.service; import com.michelin.ns4kafka.model.RoleBinding; +import com.michelin.ns4kafka.model.query.RoleBindingFilterParams; import com.michelin.ns4kafka.repository.RoleBindingRepository; import jakarta.inject.Inject; import jakarta.inject.Singleton; @@ -35,7 +36,7 @@ public void create(RoleBinding roleBinding) { } /** - * List role bindings by namespace. + * List role bindings of a given namespace. * * @param namespace The namespace used to research * @return The list of associated role bindings @@ -44,6 +45,16 @@ public List list(String namespace) { return roleBindingRepository.findAllForNamespace(namespace); } + /** + * List role bindings of a given namespace, filtered by given parameters. + * + * @param namespace The namespace used to research + * @return The list of associated role bindings + */ + public List list(String namespace, RoleBindingFilterParams params) { + return roleBindingRepository.findAllForNamespace(namespace, params); + } + /** * List role bindings by groups. * diff --git a/src/test/java/com/michelin/ns4kafka/controller/RoleBindingControllerTest.java b/src/test/java/com/michelin/ns4kafka/controller/RoleBindingControllerTest.java index 110023b3..e6ea19f9 100644 --- a/src/test/java/com/michelin/ns4kafka/controller/RoleBindingControllerTest.java +++ b/src/test/java/com/michelin/ns4kafka/controller/RoleBindingControllerTest.java @@ -12,11 +12,13 @@ import com.michelin.ns4kafka.model.Metadata; import com.michelin.ns4kafka.model.Namespace; import com.michelin.ns4kafka.model.RoleBinding; +import com.michelin.ns4kafka.model.query.RoleBindingFilterParams; import com.michelin.ns4kafka.security.ResourceBasedSecurityRule; import com.michelin.ns4kafka.service.NamespaceService; import com.michelin.ns4kafka.service.RoleBindingService; import io.micronaut.context.event.ApplicationEventPublisher; import io.micronaut.security.utils.SecurityService; +import java.util.List; import java.util.Map; import java.util.Optional; import org.junit.jupiter.api.Test; @@ -187,4 +189,38 @@ void deleteSuccessDryRun() { roleBindingController.delete("test", "test.rolebinding", true); verify(roleBindingService, never()).delete(any()); } + + @Test + void listRoleBindingWithNameParameters() { + RoleBinding rb1 = RoleBinding.builder().metadata(Metadata.builder().name("namespace-rb1").build()).build(); + RoleBinding rb2 = RoleBinding.builder().metadata(Metadata.builder().name("namespace-rb2").build()).build(); + + List nameParamsList = List.of("namespace-rb1", "namespace-rb2"); + Optional> nameParams = Optional.of(nameParamsList); + RoleBindingFilterParams searchParams = RoleBindingFilterParams.builder().name(nameParamsList).build(); + + when(roleBindingService.list("test", searchParams)) + .thenReturn(List.of(rb1, rb2)); + + List actual = roleBindingController.list("test", nameParams); + + assertEquals(2, actual.size()); + assertEquals("namespace-rb1", actual.get(0).getMetadata().getName()); + assertEquals("namespace-rb2", actual.get(1).getMetadata().getName()); + } + + @Test + void listRoleBindingWithEmptyNameParameter() { + RoleBinding rb1 = RoleBinding.builder().metadata(Metadata.builder().name("namespace-rb1").build()).build(); + RoleBinding rb2 = RoleBinding.builder().metadata(Metadata.builder().name("namespace-rb2").build()).build(); + + when(roleBindingService.list("test", RoleBindingFilterParams.builder().name(List.of("")).build())) + .thenReturn(List.of(rb1, rb2)); + + List actual = roleBindingController.list("test", Optional.of(List.of(""))); + + assertEquals(2, actual.size()); + assertEquals("namespace-rb1", actual.get(0).getMetadata().getName()); + assertEquals("namespace-rb2", actual.get(1).getMetadata().getName()); + } } From 63b31866b0d48c36541f109568082014f8e09c55 Mon Sep 17 00:00:00 2001 From: thcai Date: Thu, 18 Jul 2024 17:40:32 +0200 Subject: [PATCH 03/10] Service TU --- .../repository/RoleBindingRepository.java | 9 -- .../kafka/KafkaRoleBindingRepository.java | 21 --- .../ns4kafka/service/RoleBindingService.java | 10 +- .../service/RoleBindingServiceTest.java | 123 ++++++++++++++++++ 4 files changed, 132 insertions(+), 31 deletions(-) diff --git a/src/main/java/com/michelin/ns4kafka/repository/RoleBindingRepository.java b/src/main/java/com/michelin/ns4kafka/repository/RoleBindingRepository.java index d20a1601..8aa087fd 100644 --- a/src/main/java/com/michelin/ns4kafka/repository/RoleBindingRepository.java +++ b/src/main/java/com/michelin/ns4kafka/repository/RoleBindingRepository.java @@ -1,7 +1,6 @@ package com.michelin.ns4kafka.repository; import com.michelin.ns4kafka.model.RoleBinding; -import com.michelin.ns4kafka.model.query.RoleBindingFilterParams; import java.util.Collection; import java.util.List; @@ -25,14 +24,6 @@ public interface RoleBindingRepository { */ List findAllForNamespace(String namespace); - /** - * List role bindings of a given namespace, filtered by given parameters. - * - * @param namespace The namespace used to research - * @return The list of associated role bindings - */ - List findAllForNamespace(String namespace, RoleBindingFilterParams params); - /** * Create a role binding. * diff --git a/src/main/java/com/michelin/ns4kafka/repository/kafka/KafkaRoleBindingRepository.java b/src/main/java/com/michelin/ns4kafka/repository/kafka/KafkaRoleBindingRepository.java index 79c120e5..880e0fd8 100644 --- a/src/main/java/com/michelin/ns4kafka/repository/kafka/KafkaRoleBindingRepository.java +++ b/src/main/java/com/michelin/ns4kafka/repository/kafka/KafkaRoleBindingRepository.java @@ -1,9 +1,7 @@ package com.michelin.ns4kafka.repository.kafka; import com.michelin.ns4kafka.model.RoleBinding; -import com.michelin.ns4kafka.model.query.RoleBindingFilterParams; import com.michelin.ns4kafka.repository.RoleBindingRepository; -import com.michelin.ns4kafka.util.RegexUtils; import io.micronaut.configuration.kafka.annotation.KafkaClient; import io.micronaut.configuration.kafka.annotation.KafkaListener; import io.micronaut.configuration.kafka.annotation.OffsetReset; @@ -13,7 +11,6 @@ import jakarta.inject.Singleton; import java.util.Collection; import java.util.List; -import java.util.regex.Pattern; import org.apache.kafka.clients.consumer.ConsumerRecord; import org.apache.kafka.clients.producer.Producer; @@ -112,22 +109,4 @@ public List findAllForNamespace(String namespace) { .filter(roleBinding -> roleBinding.getMetadata().getNamespace().equals(namespace)) .toList(); } - - /** - * List role bindings of a given namespace, filtered by given parameters. - * - * @param namespace The namespace used to research - * @param params The filter parameters - * @return The list of associated role bindings - */ - @Override - public List findAllForNamespace(String namespace, RoleBindingFilterParams params) { - List nameFilterPatterns = RegexUtils.wildcardStringsToRegexPatterns(params.name()); - return getKafkaStore().values() - .stream() - .filter(roleBinding -> roleBinding.getMetadata().getNamespace().equals(namespace) - && nameFilterPatterns.stream().anyMatch(pattern -> - Pattern.compile(pattern).matcher(roleBinding.getMetadata().getName()).matches())) - .toList(); - } } diff --git a/src/main/java/com/michelin/ns4kafka/service/RoleBindingService.java b/src/main/java/com/michelin/ns4kafka/service/RoleBindingService.java index b596e7c0..290e3564 100644 --- a/src/main/java/com/michelin/ns4kafka/service/RoleBindingService.java +++ b/src/main/java/com/michelin/ns4kafka/service/RoleBindingService.java @@ -3,11 +3,13 @@ import com.michelin.ns4kafka.model.RoleBinding; import com.michelin.ns4kafka.model.query.RoleBindingFilterParams; import com.michelin.ns4kafka.repository.RoleBindingRepository; +import com.michelin.ns4kafka.util.RegexUtils; import jakarta.inject.Inject; import jakarta.inject.Singleton; import java.util.Collection; import java.util.List; import java.util.Optional; +import java.util.regex.Pattern; /** * Service to manage role bindings. @@ -49,10 +51,16 @@ public List list(String namespace) { * List role bindings of a given namespace, filtered by given parameters. * * @param namespace The namespace used to research + * @param params The filter parameters * @return The list of associated role bindings */ public List list(String namespace, RoleBindingFilterParams params) { - return roleBindingRepository.findAllForNamespace(namespace, params); + List nameFilterPatterns = RegexUtils.wildcardStringsToRegexPatterns(params.name()); + return roleBindingRepository.findAllForNamespace(namespace) + .stream() + .filter(roleBinding -> nameFilterPatterns.stream() + .anyMatch(pattern -> Pattern.compile(pattern).matcher(roleBinding.getMetadata().getName()).matches())) + .toList(); } /** diff --git a/src/test/java/com/michelin/ns4kafka/service/RoleBindingServiceTest.java b/src/test/java/com/michelin/ns4kafka/service/RoleBindingServiceTest.java index d0b9791e..4d69d3c6 100644 --- a/src/test/java/com/michelin/ns4kafka/service/RoleBindingServiceTest.java +++ b/src/test/java/com/michelin/ns4kafka/service/RoleBindingServiceTest.java @@ -5,6 +5,7 @@ import com.michelin.ns4kafka.model.Metadata; import com.michelin.ns4kafka.model.RoleBinding; +import com.michelin.ns4kafka.model.query.RoleBindingFilterParams; import com.michelin.ns4kafka.repository.RoleBindingRepository; import java.util.List; import org.junit.jupiter.api.Test; @@ -47,4 +48,126 @@ void findByName() { var result = roleBindingService.findByName("namespace", "namespace-rb2"); assertEquals(rb2, result.get()); } + + @Test + void listRoleBindingWithoutParameter() { + RoleBinding rb1 = RoleBinding.builder() + .metadata(Metadata.builder() + .name("namespace-rb1") + .cluster("local") + .build()) + .build(); + RoleBinding rb2 = RoleBinding.builder() + .metadata(Metadata.builder() + .name("namespace-rb2") + .cluster("local") + .build()) + .build(); + RoleBinding rb3 = RoleBinding.builder() + .metadata(Metadata.builder() + .name("namespace-rb3") + .cluster("local") + .build()) + .build(); + + when(roleBindingRepository.findAllForNamespace("namespace")).thenReturn(List.of(rb1, rb2, rb3)); + + assertEquals(List.of(rb1, rb2, rb3), roleBindingService.list("namespace")); + } + + @Test + void listRoleBindingWithNameParameter() { + RoleBinding rb1 = RoleBinding.builder().metadata(Metadata.builder().name("namespace-rb1").build()).build(); + RoleBinding rb2 = RoleBinding.builder().metadata(Metadata.builder().name("namespace-rb2").build()).build(); + RoleBinding rb3 = RoleBinding.builder().metadata(Metadata.builder().name("namespace-rb3").build()).build(); + + when(roleBindingRepository.findAllForNamespace("namespace")).thenReturn(List.of(rb1, rb2, rb3)); + + RoleBindingFilterParams params1 = RoleBindingFilterParams.builder().name(List.of("namespace-rb1")).build(); + assertEquals(List.of(rb1), roleBindingService.list("namespace", params1)); + + RoleBindingFilterParams params2 = RoleBindingFilterParams.builder().name(List.of("")).build(); + assertEquals(List.of(rb1, rb2, rb3), roleBindingService.list("namespace", params2)); + + RoleBindingFilterParams params3 = RoleBindingFilterParams.builder().name(List.of("namespace-rb5")).build(); + assertEquals(List.of(), roleBindingService.list("namespace", params3)); + } + + @Test + void listRoleBindingWithWildcardNameParameter() { + RoleBinding rb1 = RoleBinding.builder().metadata(Metadata.builder().name("namespace-rb1").build()).build(); + RoleBinding rb2 = RoleBinding.builder().metadata(Metadata.builder().name("namespace-rb2").build()).build(); + RoleBinding rb3 = RoleBinding.builder().metadata(Metadata.builder().name("namespace-rb3").build()).build(); + RoleBinding rb4 = RoleBinding.builder().metadata(Metadata.builder().name("rb4").build()).build(); + + when(roleBindingRepository.findAllForNamespace("namespace")).thenReturn(List.of(rb1, rb2, rb3, rb4)); + + RoleBindingFilterParams params1 = RoleBindingFilterParams.builder().name(List.of("namespace-*")).build(); + assertEquals(List.of(rb1, rb2, rb3), roleBindingService.list("namespace", params1)); + + RoleBindingFilterParams params2 = RoleBindingFilterParams.builder().name(List.of("*rb?")).build(); + assertEquals(List.of(rb1, rb2, rb3, rb4), roleBindingService.list("namespace", params2)); + + RoleBindingFilterParams params3 = RoleBindingFilterParams.builder().name(List.of("rb?")).build(); + assertEquals(List.of(rb4), roleBindingService.list("namespace", params3)); + + RoleBindingFilterParams params4 = RoleBindingFilterParams.builder().name(List.of("role_binding*")).build(); + assertEquals(List.of(), roleBindingService.list("namespace", params4)); + } + + @Test + void listRoleBindingWithMultipleNameParameters() { + RoleBinding rb1 = RoleBinding.builder().metadata(Metadata.builder().name("namespace-rb1").build()).build(); + RoleBinding rb2 = RoleBinding.builder().metadata(Metadata.builder().name("namespace-rb2").build()).build(); + RoleBinding rb3 = RoleBinding.builder().metadata(Metadata.builder().name("ns-rb1").build()).build(); + RoleBinding rb4 = RoleBinding.builder().metadata(Metadata.builder().name("ns-rb2").build()).build(); + RoleBinding rb5 = RoleBinding.builder().metadata(Metadata.builder().name("ns-rb4").build()).build(); + RoleBinding rb6 = RoleBinding.builder().metadata(Metadata.builder().name("rb4").build()).build(); + + when(roleBindingRepository.findAllForNamespace("namespace")).thenReturn(List.of(rb1, rb2, rb3, rb4, rb5, rb6)); + + RoleBindingFilterParams params1 = RoleBindingFilterParams.builder() + .name(List.of("namespace-rb1", "namespace-rb2")).build(); + assertEquals(List.of(rb1, rb2), roleBindingService.list("namespace", params1)); + + RoleBindingFilterParams params2 = RoleBindingFilterParams.builder() + .name(List.of("namespace-rb1", "namespace-rb4")).build(); + assertEquals(List.of(rb1), roleBindingService.list("namespace", params2)); + + RoleBindingFilterParams params3 = RoleBindingFilterParams.builder() + .name(List.of("namespace-rb4", "namespace-rb5")).build(); + assertEquals(List.of(), roleBindingService.list("namespace", params3)); + + RoleBindingFilterParams params4 = RoleBindingFilterParams.builder() + .name(List.of("namespace-rb1", "namespace-rb1")).build(); + assertEquals(List.of(rb1), roleBindingService.list("namespace", params4)); + + RoleBindingFilterParams params5 = RoleBindingFilterParams.builder() + .name(List.of("namespace-rb?", "*-rb1")).build(); + assertEquals(List.of(rb1, rb2, rb3), roleBindingService.list("namespace", params5)); + + RoleBindingFilterParams params6 = RoleBindingFilterParams.builder() + .name(List.of("namespace-rb?", "*-rb6")).build(); + assertEquals(List.of(rb1, rb2), roleBindingService.list("namespace", params6)); + + RoleBindingFilterParams params7 = RoleBindingFilterParams.builder() + .name(List.of("namespace-role-binding*", "*-rb6")).build(); + assertEquals(List.of(), roleBindingService.list("namespace", params7)); + + RoleBindingFilterParams params8 = RoleBindingFilterParams.builder() + .name(List.of("*-rb1", "*-rb1")).build(); + assertEquals(List.of(rb1, rb3), roleBindingService.list("namespace", params8)); + + RoleBindingFilterParams params9 = RoleBindingFilterParams.builder() + .name(List.of("ns-rb1", "*-rb2")).build(); + assertEquals(List.of(rb2, rb3, rb4), roleBindingService.list("namespace", params9)); + + RoleBindingFilterParams params10 = RoleBindingFilterParams.builder() + .name(List.of("ns-rb5", "*rb4")).build(); + assertEquals(List.of(rb5, rb6), roleBindingService.list("namespace", params10)); + + RoleBindingFilterParams params11 = RoleBindingFilterParams.builder() + .name(List.of("ns-rb5", "ns-?")).build(); + assertEquals(List.of(), roleBindingService.list("namespace", params11)); + } } From d5d0e60a5d5a6ae2a232bc843478c81eca7054ba Mon Sep 17 00:00:00 2001 From: thcai Date: Thu, 18 Jul 2024 17:41:14 +0200 Subject: [PATCH 04/10] fix warning --- .../com/michelin/ns4kafka/service/RoleBindingServiceTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/michelin/ns4kafka/service/RoleBindingServiceTest.java b/src/test/java/com/michelin/ns4kafka/service/RoleBindingServiceTest.java index 4d69d3c6..d71abe26 100644 --- a/src/test/java/com/michelin/ns4kafka/service/RoleBindingServiceTest.java +++ b/src/test/java/com/michelin/ns4kafka/service/RoleBindingServiceTest.java @@ -46,7 +46,7 @@ void findByName() { when(roleBindingRepository.findAllForNamespace("namespace")).thenReturn(List.of(rb1, rb2, rb3)); var result = roleBindingService.findByName("namespace", "namespace-rb2"); - assertEquals(rb2, result.get()); + assertEquals(rb2, result.orElse(null)); } @Test From 782e29cce22b938aa9b38604364c5201fb611099 Mon Sep 17 00:00:00 2001 From: thcai Date: Wed, 24 Jul 2024 14:48:58 +0200 Subject: [PATCH 05/10] rework --- .../controller/RoleBindingController.java | 9 +- .../model/query/RoleBindingFilterParams.java | 13 --- .../ns4kafka/service/RoleBindingService.java | 11 +- .../controller/RoleBindingControllerTest.java | 28 +---- .../service/RoleBindingServiceTest.java | 104 ++---------------- 5 files changed, 23 insertions(+), 142 deletions(-) delete mode 100644 src/main/java/com/michelin/ns4kafka/model/query/RoleBindingFilterParams.java diff --git a/src/main/java/com/michelin/ns4kafka/controller/RoleBindingController.java b/src/main/java/com/michelin/ns4kafka/controller/RoleBindingController.java index 05e21aad..a7dda035 100644 --- a/src/main/java/com/michelin/ns4kafka/controller/RoleBindingController.java +++ b/src/main/java/com/michelin/ns4kafka/controller/RoleBindingController.java @@ -3,7 +3,6 @@ import com.michelin.ns4kafka.controller.generic.NamespacedResourceController; import com.michelin.ns4kafka.model.Namespace; import com.michelin.ns4kafka.model.RoleBinding; -import com.michelin.ns4kafka.model.query.RoleBindingFilterParams; import com.michelin.ns4kafka.service.RoleBindingService; import com.michelin.ns4kafka.util.enumation.ApplyStatus; import io.micronaut.http.HttpResponse; @@ -39,14 +38,12 @@ public class RoleBindingController extends NamespacedResourceController { * List role bindings by namespace. * * @param namespace The namespace + * @param name The name filter * @return A list of role bindings */ @Get - public List list(String namespace, @QueryValue Optional> name) { - RoleBindingFilterParams params = RoleBindingFilterParams.builder() - .name(name.orElse(List.of("*"))) - .build(); - return roleBindingService.list(namespace, params); + public List list(String namespace, @QueryValue(defaultValue = "*") String name) { + return roleBindingService.list(namespace, name); } /** diff --git a/src/main/java/com/michelin/ns4kafka/model/query/RoleBindingFilterParams.java b/src/main/java/com/michelin/ns4kafka/model/query/RoleBindingFilterParams.java deleted file mode 100644 index 48ebe83f..00000000 --- a/src/main/java/com/michelin/ns4kafka/model/query/RoleBindingFilterParams.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.michelin.ns4kafka.model.query; - -import java.util.List; -import lombok.Builder; - -/** - * Role binding filter parameters in endpoints. - * - * @param name role binding name filter - */ -@Builder -public record RoleBindingFilterParams(List name) { -} diff --git a/src/main/java/com/michelin/ns4kafka/service/RoleBindingService.java b/src/main/java/com/michelin/ns4kafka/service/RoleBindingService.java index 290e3564..e02d6a9e 100644 --- a/src/main/java/com/michelin/ns4kafka/service/RoleBindingService.java +++ b/src/main/java/com/michelin/ns4kafka/service/RoleBindingService.java @@ -1,7 +1,6 @@ package com.michelin.ns4kafka.service; import com.michelin.ns4kafka.model.RoleBinding; -import com.michelin.ns4kafka.model.query.RoleBindingFilterParams; import com.michelin.ns4kafka.repository.RoleBindingRepository; import com.michelin.ns4kafka.util.RegexUtils; import jakarta.inject.Inject; @@ -9,7 +8,6 @@ import java.util.Collection; import java.util.List; import java.util.Optional; -import java.util.regex.Pattern; /** * Service to manage role bindings. @@ -51,15 +49,14 @@ public List list(String namespace) { * List role bindings of a given namespace, filtered by given parameters. * * @param namespace The namespace used to research - * @param params The filter parameters + * @param name The name parameter * @return The list of associated role bindings */ - public List list(String namespace, RoleBindingFilterParams params) { - List nameFilterPatterns = RegexUtils.wildcardStringsToRegexPatterns(params.name()); + public List list(String namespace, String name) { + List nameFilterPatterns = RegexUtils.wildcardStringsToRegexPatterns(List.of(name)); return roleBindingRepository.findAllForNamespace(namespace) .stream() - .filter(roleBinding -> nameFilterPatterns.stream() - .anyMatch(pattern -> Pattern.compile(pattern).matcher(roleBinding.getMetadata().getName()).matches())) + .filter(rb -> RegexUtils.filterByPattern(rb.getMetadata().getName(), nameFilterPatterns)) .toList(); } diff --git a/src/test/java/com/michelin/ns4kafka/controller/RoleBindingControllerTest.java b/src/test/java/com/michelin/ns4kafka/controller/RoleBindingControllerTest.java index e6ea19f9..ff2405c3 100644 --- a/src/test/java/com/michelin/ns4kafka/controller/RoleBindingControllerTest.java +++ b/src/test/java/com/michelin/ns4kafka/controller/RoleBindingControllerTest.java @@ -12,7 +12,6 @@ import com.michelin.ns4kafka.model.Metadata; import com.michelin.ns4kafka.model.Namespace; import com.michelin.ns4kafka.model.RoleBinding; -import com.michelin.ns4kafka.model.query.RoleBindingFilterParams; import com.michelin.ns4kafka.security.ResourceBasedSecurityRule; import com.michelin.ns4kafka.service.NamespaceService; import com.michelin.ns4kafka.service.RoleBindingService; @@ -152,7 +151,6 @@ void createDryRun() { @Test void deleteSucess() { - Namespace ns = Namespace.builder() .metadata(Metadata.builder() .name("test") @@ -165,7 +163,6 @@ void deleteSucess() { .build()) .build(); - //when(namespaceService.findByName(any())).thenReturn(Optional.of(ns)); when(roleBindingService.findByName(any(), any())).thenReturn(Optional.of(rolebinding)); when(securityService.username()).thenReturn(Optional.of("test-user")); when(securityService.hasRole(ResourceBasedSecurityRule.IS_ADMIN)).thenReturn(false); @@ -193,20 +190,10 @@ void deleteSuccessDryRun() { @Test void listRoleBindingWithNameParameters() { RoleBinding rb1 = RoleBinding.builder().metadata(Metadata.builder().name("namespace-rb1").build()).build(); - RoleBinding rb2 = RoleBinding.builder().metadata(Metadata.builder().name("namespace-rb2").build()).build(); - - List nameParamsList = List.of("namespace-rb1", "namespace-rb2"); - Optional> nameParams = Optional.of(nameParamsList); - RoleBindingFilterParams searchParams = RoleBindingFilterParams.builder().name(nameParamsList).build(); - when(roleBindingService.list("test", searchParams)) - .thenReturn(List.of(rb1, rb2)); + when(roleBindingService.list("test", "namespace-rb1")).thenReturn(List.of(rb1)); - List actual = roleBindingController.list("test", nameParams); - - assertEquals(2, actual.size()); - assertEquals("namespace-rb1", actual.get(0).getMetadata().getName()); - assertEquals("namespace-rb2", actual.get(1).getMetadata().getName()); + assertEquals(List.of(rb1), roleBindingController.list("test", "namespace-rb1")); } @Test @@ -214,13 +201,10 @@ void listRoleBindingWithEmptyNameParameter() { RoleBinding rb1 = RoleBinding.builder().metadata(Metadata.builder().name("namespace-rb1").build()).build(); RoleBinding rb2 = RoleBinding.builder().metadata(Metadata.builder().name("namespace-rb2").build()).build(); - when(roleBindingService.list("test", RoleBindingFilterParams.builder().name(List.of("")).build())) - .thenReturn(List.of(rb1, rb2)); - - List actual = roleBindingController.list("test", Optional.of(List.of(""))); + when(roleBindingService.list("test", "*")).thenReturn(List.of(rb1, rb2)); + when(roleBindingService.list("test", "")).thenReturn(List.of(rb1, rb2)); - assertEquals(2, actual.size()); - assertEquals("namespace-rb1", actual.get(0).getMetadata().getName()); - assertEquals("namespace-rb2", actual.get(1).getMetadata().getName()); + assertEquals(List.of(rb1, rb2), roleBindingController.list("test", "*")); + assertEquals(List.of(rb1, rb2), roleBindingController.list("test", "")); } } diff --git a/src/test/java/com/michelin/ns4kafka/service/RoleBindingServiceTest.java b/src/test/java/com/michelin/ns4kafka/service/RoleBindingServiceTest.java index d71abe26..b041d194 100644 --- a/src/test/java/com/michelin/ns4kafka/service/RoleBindingServiceTest.java +++ b/src/test/java/com/michelin/ns4kafka/service/RoleBindingServiceTest.java @@ -5,7 +5,6 @@ import com.michelin.ns4kafka.model.Metadata; import com.michelin.ns4kafka.model.RoleBinding; -import com.michelin.ns4kafka.model.query.RoleBindingFilterParams; import com.michelin.ns4kafka.repository.RoleBindingRepository; import java.util.List; import org.junit.jupiter.api.Test; @@ -51,24 +50,9 @@ void findByName() { @Test void listRoleBindingWithoutParameter() { - RoleBinding rb1 = RoleBinding.builder() - .metadata(Metadata.builder() - .name("namespace-rb1") - .cluster("local") - .build()) - .build(); - RoleBinding rb2 = RoleBinding.builder() - .metadata(Metadata.builder() - .name("namespace-rb2") - .cluster("local") - .build()) - .build(); - RoleBinding rb3 = RoleBinding.builder() - .metadata(Metadata.builder() - .name("namespace-rb3") - .cluster("local") - .build()) - .build(); + RoleBinding rb1 = RoleBinding.builder().metadata(Metadata.builder().name("namespace-rb1").build()).build(); + RoleBinding rb2 = RoleBinding.builder().metadata(Metadata.builder().name("namespace-rb2").build()).build(); + RoleBinding rb3 = RoleBinding.builder().metadata(Metadata.builder().name("namespace-rb3").build()).build(); when(roleBindingRepository.findAllForNamespace("namespace")).thenReturn(List.of(rb1, rb2, rb3)); @@ -83,14 +67,9 @@ void listRoleBindingWithNameParameter() { when(roleBindingRepository.findAllForNamespace("namespace")).thenReturn(List.of(rb1, rb2, rb3)); - RoleBindingFilterParams params1 = RoleBindingFilterParams.builder().name(List.of("namespace-rb1")).build(); - assertEquals(List.of(rb1), roleBindingService.list("namespace", params1)); - - RoleBindingFilterParams params2 = RoleBindingFilterParams.builder().name(List.of("")).build(); - assertEquals(List.of(rb1, rb2, rb3), roleBindingService.list("namespace", params2)); - - RoleBindingFilterParams params3 = RoleBindingFilterParams.builder().name(List.of("namespace-rb5")).build(); - assertEquals(List.of(), roleBindingService.list("namespace", params3)); + assertEquals(List.of(rb1), roleBindingService.list("namespace", "namespace-rb1")); + assertEquals(List.of(rb1, rb2, rb3), roleBindingService.list("namespace", "")); + assertEquals(List.of(), roleBindingService.list("namespace", "namespace-rb5")); } @Test @@ -102,72 +81,9 @@ void listRoleBindingWithWildcardNameParameter() { when(roleBindingRepository.findAllForNamespace("namespace")).thenReturn(List.of(rb1, rb2, rb3, rb4)); - RoleBindingFilterParams params1 = RoleBindingFilterParams.builder().name(List.of("namespace-*")).build(); - assertEquals(List.of(rb1, rb2, rb3), roleBindingService.list("namespace", params1)); - - RoleBindingFilterParams params2 = RoleBindingFilterParams.builder().name(List.of("*rb?")).build(); - assertEquals(List.of(rb1, rb2, rb3, rb4), roleBindingService.list("namespace", params2)); - - RoleBindingFilterParams params3 = RoleBindingFilterParams.builder().name(List.of("rb?")).build(); - assertEquals(List.of(rb4), roleBindingService.list("namespace", params3)); - - RoleBindingFilterParams params4 = RoleBindingFilterParams.builder().name(List.of("role_binding*")).build(); - assertEquals(List.of(), roleBindingService.list("namespace", params4)); - } - - @Test - void listRoleBindingWithMultipleNameParameters() { - RoleBinding rb1 = RoleBinding.builder().metadata(Metadata.builder().name("namespace-rb1").build()).build(); - RoleBinding rb2 = RoleBinding.builder().metadata(Metadata.builder().name("namespace-rb2").build()).build(); - RoleBinding rb3 = RoleBinding.builder().metadata(Metadata.builder().name("ns-rb1").build()).build(); - RoleBinding rb4 = RoleBinding.builder().metadata(Metadata.builder().name("ns-rb2").build()).build(); - RoleBinding rb5 = RoleBinding.builder().metadata(Metadata.builder().name("ns-rb4").build()).build(); - RoleBinding rb6 = RoleBinding.builder().metadata(Metadata.builder().name("rb4").build()).build(); - - when(roleBindingRepository.findAllForNamespace("namespace")).thenReturn(List.of(rb1, rb2, rb3, rb4, rb5, rb6)); - - RoleBindingFilterParams params1 = RoleBindingFilterParams.builder() - .name(List.of("namespace-rb1", "namespace-rb2")).build(); - assertEquals(List.of(rb1, rb2), roleBindingService.list("namespace", params1)); - - RoleBindingFilterParams params2 = RoleBindingFilterParams.builder() - .name(List.of("namespace-rb1", "namespace-rb4")).build(); - assertEquals(List.of(rb1), roleBindingService.list("namespace", params2)); - - RoleBindingFilterParams params3 = RoleBindingFilterParams.builder() - .name(List.of("namespace-rb4", "namespace-rb5")).build(); - assertEquals(List.of(), roleBindingService.list("namespace", params3)); - - RoleBindingFilterParams params4 = RoleBindingFilterParams.builder() - .name(List.of("namespace-rb1", "namespace-rb1")).build(); - assertEquals(List.of(rb1), roleBindingService.list("namespace", params4)); - - RoleBindingFilterParams params5 = RoleBindingFilterParams.builder() - .name(List.of("namespace-rb?", "*-rb1")).build(); - assertEquals(List.of(rb1, rb2, rb3), roleBindingService.list("namespace", params5)); - - RoleBindingFilterParams params6 = RoleBindingFilterParams.builder() - .name(List.of("namespace-rb?", "*-rb6")).build(); - assertEquals(List.of(rb1, rb2), roleBindingService.list("namespace", params6)); - - RoleBindingFilterParams params7 = RoleBindingFilterParams.builder() - .name(List.of("namespace-role-binding*", "*-rb6")).build(); - assertEquals(List.of(), roleBindingService.list("namespace", params7)); - - RoleBindingFilterParams params8 = RoleBindingFilterParams.builder() - .name(List.of("*-rb1", "*-rb1")).build(); - assertEquals(List.of(rb1, rb3), roleBindingService.list("namespace", params8)); - - RoleBindingFilterParams params9 = RoleBindingFilterParams.builder() - .name(List.of("ns-rb1", "*-rb2")).build(); - assertEquals(List.of(rb2, rb3, rb4), roleBindingService.list("namespace", params9)); - - RoleBindingFilterParams params10 = RoleBindingFilterParams.builder() - .name(List.of("ns-rb5", "*rb4")).build(); - assertEquals(List.of(rb5, rb6), roleBindingService.list("namespace", params10)); - - RoleBindingFilterParams params11 = RoleBindingFilterParams.builder() - .name(List.of("ns-rb5", "ns-?")).build(); - assertEquals(List.of(), roleBindingService.list("namespace", params11)); + assertEquals(List.of(rb1, rb2, rb3), roleBindingService.list("namespace", "namespace-*")); + assertEquals(List.of(rb1, rb2, rb3, rb4), roleBindingService.list("namespace", "*rb?")); + assertEquals(List.of(rb4), roleBindingService.list("namespace", "rb?")); + assertEquals(List.of(), roleBindingService.list("namespace", "role_binding*")); } } From eca1a2255cd67aa6a426a53d5b2c3d66bdadc2ce Mon Sep 17 00:00:00 2001 From: thcai Date: Mon, 29 Jul 2024 10:29:04 +0200 Subject: [PATCH 06/10] javadoc --- .../michelin/ns4kafka/controller/RoleBindingController.java | 4 ++-- .../com/michelin/ns4kafka/service/RoleBindingService.java | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/michelin/ns4kafka/controller/RoleBindingController.java b/src/main/java/com/michelin/ns4kafka/controller/RoleBindingController.java index a7dda035..ca74d9de 100644 --- a/src/main/java/com/michelin/ns4kafka/controller/RoleBindingController.java +++ b/src/main/java/com/michelin/ns4kafka/controller/RoleBindingController.java @@ -35,10 +35,10 @@ public class RoleBindingController extends NamespacedResourceController { RoleBindingService roleBindingService; /** - * List role bindings by namespace. + * List role bindings by namespace, filtered by name parameter. * * @param namespace The namespace - * @param name The name filter + * @param name The name parameter * @return A list of role bindings */ @Get diff --git a/src/main/java/com/michelin/ns4kafka/service/RoleBindingService.java b/src/main/java/com/michelin/ns4kafka/service/RoleBindingService.java index e02d6a9e..0ad4f9ef 100644 --- a/src/main/java/com/michelin/ns4kafka/service/RoleBindingService.java +++ b/src/main/java/com/michelin/ns4kafka/service/RoleBindingService.java @@ -46,10 +46,10 @@ public List list(String namespace) { } /** - * List role bindings of a given namespace, filtered by given parameters. + * List role bindings of a given namespace, filtered by name parameter. * * @param namespace The namespace used to research - * @param name The name parameter + * @param name The name filter * @return The list of associated role bindings */ public List list(String namespace, String name) { From ab71c2fed01a364e16ba523444eed13b7ac39919 Mon Sep 17 00:00:00 2001 From: thcai Date: Wed, 31 Jul 2024 14:34:02 +0200 Subject: [PATCH 07/10] rename + reuse of method for better maintenance + deprecated annotation --- .../ns4kafka/controller/RoleBindingController.java | 4 +++- .../ns4kafka/service/RoleBindingService.java | 4 ++-- .../controller/RoleBindingControllerTest.java | 6 +++--- .../ns4kafka/service/RoleBindingServiceTest.java | 14 +++++++------- 4 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/michelin/ns4kafka/controller/RoleBindingController.java b/src/main/java/com/michelin/ns4kafka/controller/RoleBindingController.java index ca74d9de..50d5daeb 100644 --- a/src/main/java/com/michelin/ns4kafka/controller/RoleBindingController.java +++ b/src/main/java/com/michelin/ns4kafka/controller/RoleBindingController.java @@ -43,7 +43,7 @@ public class RoleBindingController extends NamespacedResourceController { */ @Get public List list(String namespace, @QueryValue(defaultValue = "*") String name) { - return roleBindingService.list(namespace, name); + return roleBindingService.findByWildcardName(namespace, name); } /** @@ -52,8 +52,10 @@ public List list(String namespace, @QueryValue(defaultValue = "*") * @param namespace The namespace * @param name The role binding name * @return A role binding + * @deprecated use list(String, String name) instead. */ @Get("/{name}") + @Deprecated(since = "1.11.13") public Optional get(String namespace, String name) { return roleBindingService.findByName(namespace, name); } diff --git a/src/main/java/com/michelin/ns4kafka/service/RoleBindingService.java b/src/main/java/com/michelin/ns4kafka/service/RoleBindingService.java index 0ad4f9ef..5fef1a3a 100644 --- a/src/main/java/com/michelin/ns4kafka/service/RoleBindingService.java +++ b/src/main/java/com/michelin/ns4kafka/service/RoleBindingService.java @@ -52,9 +52,9 @@ public List list(String namespace) { * @param name The name filter * @return The list of associated role bindings */ - public List list(String namespace, String name) { + public List findByWildcardName(String namespace, String name) { List nameFilterPatterns = RegexUtils.wildcardStringsToRegexPatterns(List.of(name)); - return roleBindingRepository.findAllForNamespace(namespace) + return list(namespace) .stream() .filter(rb -> RegexUtils.filterByPattern(rb.getMetadata().getName(), nameFilterPatterns)) .toList(); diff --git a/src/test/java/com/michelin/ns4kafka/controller/RoleBindingControllerTest.java b/src/test/java/com/michelin/ns4kafka/controller/RoleBindingControllerTest.java index ff2405c3..fc48bf66 100644 --- a/src/test/java/com/michelin/ns4kafka/controller/RoleBindingControllerTest.java +++ b/src/test/java/com/michelin/ns4kafka/controller/RoleBindingControllerTest.java @@ -191,7 +191,7 @@ void deleteSuccessDryRun() { void listRoleBindingWithNameParameters() { RoleBinding rb1 = RoleBinding.builder().metadata(Metadata.builder().name("namespace-rb1").build()).build(); - when(roleBindingService.list("test", "namespace-rb1")).thenReturn(List.of(rb1)); + when(roleBindingService.findByWildcardName("test", "namespace-rb1")).thenReturn(List.of(rb1)); assertEquals(List.of(rb1), roleBindingController.list("test", "namespace-rb1")); } @@ -201,8 +201,8 @@ void listRoleBindingWithEmptyNameParameter() { RoleBinding rb1 = RoleBinding.builder().metadata(Metadata.builder().name("namespace-rb1").build()).build(); RoleBinding rb2 = RoleBinding.builder().metadata(Metadata.builder().name("namespace-rb2").build()).build(); - when(roleBindingService.list("test", "*")).thenReturn(List.of(rb1, rb2)); - when(roleBindingService.list("test", "")).thenReturn(List.of(rb1, rb2)); + when(roleBindingService.findByWildcardName("test", "*")).thenReturn(List.of(rb1, rb2)); + when(roleBindingService.findByWildcardName("test", "")).thenReturn(List.of(rb1, rb2)); assertEquals(List.of(rb1, rb2), roleBindingController.list("test", "*")); assertEquals(List.of(rb1, rb2), roleBindingController.list("test", "")); diff --git a/src/test/java/com/michelin/ns4kafka/service/RoleBindingServiceTest.java b/src/test/java/com/michelin/ns4kafka/service/RoleBindingServiceTest.java index b041d194..1724acaa 100644 --- a/src/test/java/com/michelin/ns4kafka/service/RoleBindingServiceTest.java +++ b/src/test/java/com/michelin/ns4kafka/service/RoleBindingServiceTest.java @@ -67,9 +67,9 @@ void listRoleBindingWithNameParameter() { when(roleBindingRepository.findAllForNamespace("namespace")).thenReturn(List.of(rb1, rb2, rb3)); - assertEquals(List.of(rb1), roleBindingService.list("namespace", "namespace-rb1")); - assertEquals(List.of(rb1, rb2, rb3), roleBindingService.list("namespace", "")); - assertEquals(List.of(), roleBindingService.list("namespace", "namespace-rb5")); + assertEquals(List.of(rb1), roleBindingService.findByWildcardName("namespace", "namespace-rb1")); + assertEquals(List.of(rb1, rb2, rb3), roleBindingService.findByWildcardName("namespace", "")); + assertEquals(List.of(), roleBindingService.findByWildcardName("namespace", "namespace-rb5")); } @Test @@ -81,9 +81,9 @@ void listRoleBindingWithWildcardNameParameter() { when(roleBindingRepository.findAllForNamespace("namespace")).thenReturn(List.of(rb1, rb2, rb3, rb4)); - assertEquals(List.of(rb1, rb2, rb3), roleBindingService.list("namespace", "namespace-*")); - assertEquals(List.of(rb1, rb2, rb3, rb4), roleBindingService.list("namespace", "*rb?")); - assertEquals(List.of(rb4), roleBindingService.list("namespace", "rb?")); - assertEquals(List.of(), roleBindingService.list("namespace", "role_binding*")); + assertEquals(List.of(rb1, rb2, rb3), roleBindingService.findByWildcardName("namespace", "namespace-*")); + assertEquals(List.of(rb1, rb2, rb3, rb4), roleBindingService.findByWildcardName("namespace", "*rb?")); + assertEquals(List.of(rb4), roleBindingService.findByWildcardName("namespace", "rb?")); + assertEquals(List.of(), roleBindingService.findByWildcardName("namespace", "role_binding*")); } } From 5dccbca5c4207f926883df7c301b9da6046b39aa Mon Sep 17 00:00:00 2001 From: thcai Date: Wed, 31 Jul 2024 14:35:10 +0200 Subject: [PATCH 08/10] warning --- .../ns4kafka/controller/RoleBindingControllerTest.java | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/src/test/java/com/michelin/ns4kafka/controller/RoleBindingControllerTest.java b/src/test/java/com/michelin/ns4kafka/controller/RoleBindingControllerTest.java index fc48bf66..1aad2b33 100644 --- a/src/test/java/com/michelin/ns4kafka/controller/RoleBindingControllerTest.java +++ b/src/test/java/com/michelin/ns4kafka/controller/RoleBindingControllerTest.java @@ -144,19 +144,12 @@ void createDryRun() { when(namespaceService.findByName(any())).thenReturn(Optional.of(ns)); var response = roleBindingController.apply("test", rolebinding, true); - RoleBinding actual = response.body(); assertEquals("created", response.header("X-Ns4kafka-Result")); verify(roleBindingService, never()).create(rolebinding); } @Test - void deleteSucess() { - Namespace ns = Namespace.builder() - .metadata(Metadata.builder() - .name("test") - .cluster("local") - .build()) - .build(); + void deleteSuccess() { RoleBinding rolebinding = RoleBinding.builder() .metadata(Metadata.builder() .name("test.rolebinding") From bf6d1f78d0501757fd49f8c3910e318bd1b3cf19 Mon Sep 17 00:00:00 2001 From: thcai Date: Wed, 31 Jul 2024 15:23:29 +0200 Subject: [PATCH 09/10] deprecated version --- .../com/michelin/ns4kafka/controller/RoleBindingController.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/michelin/ns4kafka/controller/RoleBindingController.java b/src/main/java/com/michelin/ns4kafka/controller/RoleBindingController.java index 50d5daeb..38abe5bb 100644 --- a/src/main/java/com/michelin/ns4kafka/controller/RoleBindingController.java +++ b/src/main/java/com/michelin/ns4kafka/controller/RoleBindingController.java @@ -55,7 +55,7 @@ public List list(String namespace, @QueryValue(defaultValue = "*") * @deprecated use list(String, String name) instead. */ @Get("/{name}") - @Deprecated(since = "1.11.13") + @Deprecated(since = "1.12.0") public Optional get(String namespace, String name) { return roleBindingService.findByName(namespace, name); } From a36f157995d8596a9efb31a9ffb6f0c1f76dbc4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Greffier?= Date: Wed, 31 Jul 2024 21:55:47 +0200 Subject: [PATCH 10/10] Update --- .../security/auth/AuthenticationService.java | 2 +- .../ns4kafka/service/NamespaceService.java | 2 +- .../ns4kafka/service/RoleBindingService.java | 46 +++++----- .../controller/RoleBindingControllerTest.java | 85 +++++++++++------ .../auth/AuthenticationServiceTest.java | 8 +- .../service/NamespaceServiceTest.java | 14 +-- .../service/RoleBindingServiceTest.java | 91 +++++++++++++++---- 7 files changed, 167 insertions(+), 81 deletions(-) diff --git a/src/main/java/com/michelin/ns4kafka/security/auth/AuthenticationService.java b/src/main/java/com/michelin/ns4kafka/security/auth/AuthenticationService.java index 4fb6e512..4417d6e6 100644 --- a/src/main/java/com/michelin/ns4kafka/security/auth/AuthenticationService.java +++ b/src/main/java/com/michelin/ns4kafka/security/auth/AuthenticationService.java @@ -41,7 +41,7 @@ public class AuthenticationService { * @return An authentication response with the user details */ public AuthenticationResponse buildAuthJwtGroups(String username, List groups) { - List roleBindings = roleBindingService.listByGroups(groups); + List roleBindings = roleBindingService.findAllByGroups(groups); if (roleBindings.isEmpty() && !groups.contains(securityProperties.getAdminGroup())) { log.debug("Error during authentication: user groups not found in any namespace"); throw new AuthenticationException(new AuthenticationFailed("No namespace matches your groups")); diff --git a/src/main/java/com/michelin/ns4kafka/service/NamespaceService.java b/src/main/java/com/michelin/ns4kafka/service/NamespaceService.java index eb0cf592..76b3a8bd 100644 --- a/src/main/java/com/michelin/ns4kafka/service/NamespaceService.java +++ b/src/main/java/com/michelin/ns4kafka/service/NamespaceService.java @@ -170,7 +170,7 @@ public List listAllNamespaceResources(Namespace namespace) { .map(ace -> ACCESS_CONTROL_ENTRY + "/" + ace.getMetadata().getName()), resourceQuotaService.findByNamespace(namespace.getMetadata().getName()).stream() .map(resourceQuota -> RESOURCE_QUOTA + "/" + resourceQuota.getMetadata().getName()), - roleBindingService.list(namespace.getMetadata().getName()).stream() + roleBindingService.findAllForNamespace(namespace.getMetadata().getName()).stream() .map(roleBinding -> ROLE_BINDING + "/" + roleBinding.getMetadata().getName()) ) .reduce(Stream::concat) diff --git a/src/main/java/com/michelin/ns4kafka/service/RoleBindingService.java b/src/main/java/com/michelin/ns4kafka/service/RoleBindingService.java index 5fef1a3a..7e3423fe 100644 --- a/src/main/java/com/michelin/ns4kafka/service/RoleBindingService.java +++ b/src/main/java/com/michelin/ns4kafka/service/RoleBindingService.java @@ -17,31 +17,13 @@ public class RoleBindingService { @Inject RoleBindingRepository roleBindingRepository; - /** - * Delete a role binding. - * - * @param roleBinding The role binding to delete - */ - public void delete(RoleBinding roleBinding) { - roleBindingRepository.delete(roleBinding); - } - - /** - * Create a role binding. - * - * @param roleBinding The role binding to create - */ - public void create(RoleBinding roleBinding) { - roleBindingRepository.create(roleBinding); - } - /** * List role bindings of a given namespace. * * @param namespace The namespace used to research * @return The list of associated role bindings */ - public List list(String namespace) { + public List findAllForNamespace(String namespace) { return roleBindingRepository.findAllForNamespace(namespace); } @@ -49,12 +31,12 @@ public List list(String namespace) { * List role bindings of a given namespace, filtered by name parameter. * * @param namespace The namespace used to research - * @param name The name filter + * @param name The name filter * @return The list of associated role bindings */ public List findByWildcardName(String namespace, String name) { List nameFilterPatterns = RegexUtils.wildcardStringsToRegexPatterns(List.of(name)); - return list(namespace) + return findAllForNamespace(namespace) .stream() .filter(rb -> RegexUtils.filterByPattern(rb.getMetadata().getName(), nameFilterPatterns)) .toList(); @@ -66,7 +48,7 @@ public List findByWildcardName(String namespace, String name) { * @param groups The groups used to research * @return The list of associated role bindings */ - public List listByGroups(Collection groups) { + public List findAllByGroups(Collection groups) { return roleBindingRepository.findAllForGroups(groups); } @@ -78,9 +60,27 @@ public List listByGroups(Collection groups) { * @return The researched role binding */ public Optional findByName(String namespace, String name) { - return list(namespace) + return findAllForNamespace(namespace) .stream() .filter(t -> t.getMetadata().getName().equals(name)) .findFirst(); } + + /** + * Delete a role binding. + * + * @param roleBinding The role binding to delete + */ + public void delete(RoleBinding roleBinding) { + roleBindingRepository.delete(roleBinding); + } + + /** + * Create a role binding. + * + * @param roleBinding The role binding to create + */ + public void create(RoleBinding roleBinding) { + roleBindingRepository.create(roleBinding); + } } diff --git a/src/test/java/com/michelin/ns4kafka/controller/RoleBindingControllerTest.java b/src/test/java/com/michelin/ns4kafka/controller/RoleBindingControllerTest.java index 1aad2b33..d74449db 100644 --- a/src/test/java/com/michelin/ns4kafka/controller/RoleBindingControllerTest.java +++ b/src/test/java/com/michelin/ns4kafka/controller/RoleBindingControllerTest.java @@ -45,22 +45,26 @@ class RoleBindingControllerTest { RoleBindingController roleBindingController; @Test - void applySuccess() { + void shouldCreateRoleBinding() { Namespace ns = Namespace.builder() .metadata(Metadata.builder() .name("test") .cluster("local") .build()) .build(); + RoleBinding rolebinding = RoleBinding.builder() .metadata(Metadata.builder() .name("test.rolebinding") .build()) .build(); - when(namespaceService.findByName(any())).thenReturn(Optional.of(ns)); - when(securityService.username()).thenReturn(Optional.of("test-user")); - when(securityService.hasRole(ResourceBasedSecurityRule.IS_ADMIN)).thenReturn(false); + when(namespaceService.findByName(any())) + .thenReturn(Optional.of(ns)); + when(securityService.username()) + .thenReturn(Optional.of("test-user")); + when(securityService.hasRole(ResourceBasedSecurityRule.IS_ADMIN)) + .thenReturn(false); doNothing().when(applicationEventPublisher).publishEvent(any()); var response = roleBindingController.apply("test", rolebinding, false); @@ -70,20 +74,22 @@ void applySuccess() { } @Test - void applySuccess_AlreadyExists() { + void shouldNotCreateRoleBindingWhenAlreadyExists() { Namespace ns = Namespace.builder() .metadata(Metadata.builder() .name("test") .cluster("local") .build()) .build(); + RoleBinding rolebinding = RoleBinding.builder() .metadata(Metadata.builder() .name("test.rolebinding") .build()) .build(); - when(namespaceService.findByName(any())).thenReturn(Optional.of(ns)); + when(namespaceService.findByName(any())) + .thenReturn(Optional.of(ns)); when(roleBindingService.findByName("test", "test.rolebinding")) .thenReturn(Optional.of(rolebinding)); @@ -95,18 +101,20 @@ void applySuccess_AlreadyExists() { } @Test - void applySuccess_Changed() { + void shouldChangeRoleBinding() { Namespace ns = Namespace.builder() .metadata(Metadata.builder() .name("test") .cluster("local") .build()) .build(); + RoleBinding rolebinding = RoleBinding.builder() .metadata(Metadata.builder() .name("test.rolebinding") .build()) .build(); + RoleBinding rolebindingOld = RoleBinding.builder() .metadata(Metadata.builder() .name("test.rolebinding") @@ -114,11 +122,14 @@ void applySuccess_Changed() { .build()) .build(); - when(namespaceService.findByName(any())).thenReturn(Optional.of(ns)); + when(namespaceService.findByName(any())) + .thenReturn(Optional.of(ns)); when(roleBindingService.findByName("test", "test.rolebinding")) .thenReturn(Optional.of(rolebindingOld)); - when(securityService.username()).thenReturn(Optional.of("test-user")); - when(securityService.hasRole(ResourceBasedSecurityRule.IS_ADMIN)).thenReturn(false); + when(securityService.username()) + .thenReturn(Optional.of("test-user")); + when(securityService.hasRole(ResourceBasedSecurityRule.IS_ADMIN)) + .thenReturn(false); doNothing().when(applicationEventPublisher).publishEvent(any()); var response = roleBindingController.apply("test", rolebinding, false); @@ -128,20 +139,22 @@ void applySuccess_Changed() { } @Test - void createDryRun() { + void shouldCreateRoleBindingInDryRunMode() { Namespace ns = Namespace.builder() .metadata(Metadata.builder() .name("test") .cluster("local") .build()) .build(); + RoleBinding rolebinding = RoleBinding.builder() .metadata(Metadata.builder() .name("test.rolebinding") .build()) .build(); - when(namespaceService.findByName(any())).thenReturn(Optional.of(ns)); + when(namespaceService.findByName(any())) + .thenReturn(Optional.of(ns)); var response = roleBindingController.apply("test", rolebinding, true); assertEquals("created", response.header("X-Ns4kafka-Result")); @@ -149,16 +162,19 @@ void createDryRun() { } @Test - void deleteSuccess() { + void shouldDeleteRoleBinding() { RoleBinding rolebinding = RoleBinding.builder() .metadata(Metadata.builder() .name("test.rolebinding") .build()) .build(); - when(roleBindingService.findByName(any(), any())).thenReturn(Optional.of(rolebinding)); - when(securityService.username()).thenReturn(Optional.of("test-user")); - when(securityService.hasRole(ResourceBasedSecurityRule.IS_ADMIN)).thenReturn(false); + when(roleBindingService.findByName(any(), any())) + .thenReturn(Optional.of(rolebinding)); + when(securityService.username()) + .thenReturn(Optional.of("test-user")); + when(securityService.hasRole(ResourceBasedSecurityRule.IS_ADMIN)) + .thenReturn(false); doNothing().when(applicationEventPublisher).publishEvent(any()); assertDoesNotThrow( @@ -167,35 +183,52 @@ void deleteSuccess() { } @Test - void deleteSuccessDryRun() { + void shouldDeleteRoleBindingInDryRunMode() { RoleBinding rolebinding = RoleBinding.builder() .metadata(Metadata.builder() .name("test.rolebinding") .build()) .build(); - when(roleBindingService.findByName(any(), any())).thenReturn(Optional.of(rolebinding)); + when(roleBindingService.findByName(any(), any())) + .thenReturn(Optional.of(rolebinding)); roleBindingController.delete("test", "test.rolebinding", true); verify(roleBindingService, never()).delete(any()); } @Test - void listRoleBindingWithNameParameters() { - RoleBinding rb1 = RoleBinding.builder().metadata(Metadata.builder().name("namespace-rb1").build()).build(); + void shouldListRoleBindingsWithNameParameter() { + RoleBinding rb1 = RoleBinding.builder() + .metadata(Metadata.builder() + .name("namespace-rb1") + .build()) + .build(); - when(roleBindingService.findByWildcardName("test", "namespace-rb1")).thenReturn(List.of(rb1)); + when(roleBindingService.findByWildcardName("test", "namespace-rb1")) + .thenReturn(List.of(rb1)); assertEquals(List.of(rb1), roleBindingController.list("test", "namespace-rb1")); } @Test - void listRoleBindingWithEmptyNameParameter() { - RoleBinding rb1 = RoleBinding.builder().metadata(Metadata.builder().name("namespace-rb1").build()).build(); - RoleBinding rb2 = RoleBinding.builder().metadata(Metadata.builder().name("namespace-rb2").build()).build(); + void shouldListRoleBindingsWithEmptyNameParameter() { + RoleBinding rb1 = RoleBinding.builder() + .metadata(Metadata.builder() + .name("namespace-rb1") + .build()) + .build(); + + RoleBinding rb2 = RoleBinding.builder() + .metadata(Metadata.builder() + .name("namespace-rb2") + .build()) + .build(); - when(roleBindingService.findByWildcardName("test", "*")).thenReturn(List.of(rb1, rb2)); - when(roleBindingService.findByWildcardName("test", "")).thenReturn(List.of(rb1, rb2)); + when(roleBindingService.findByWildcardName("test", "*")) + .thenReturn(List.of(rb1, rb2)); + when(roleBindingService.findByWildcardName("test", "")) + .thenReturn(List.of(rb1, rb2)); assertEquals(List.of(rb1, rb2), roleBindingController.list("test", "*")); assertEquals(List.of(rb1, rb2), roleBindingController.list("test", "")); diff --git a/src/test/java/com/michelin/ns4kafka/security/auth/AuthenticationServiceTest.java b/src/test/java/com/michelin/ns4kafka/security/auth/AuthenticationServiceTest.java index 1dd24e1f..82cd04ad 100644 --- a/src/test/java/com/michelin/ns4kafka/security/auth/AuthenticationServiceTest.java +++ b/src/test/java/com/michelin/ns4kafka/security/auth/AuthenticationServiceTest.java @@ -41,7 +41,7 @@ class AuthenticationServiceTest { @Test void shouldThrowErrorWhenNoRoleBindingAndNotAdmin() { - when(roleBindingService.listByGroups(any())) + when(roleBindingService.findAllByGroups(any())) .thenReturn(Collections.emptyList()); when(securityProperties.getAdminGroup()) @@ -57,7 +57,7 @@ void shouldThrowErrorWhenNoRoleBindingAndNotAdmin() { @Test void shouldReturnAuthenticationSuccessWhenAdminNoGroup() { - when(roleBindingService.listByGroups(any())) + when(roleBindingService.findAllByGroups(any())) .thenReturn(Collections.emptyList()); when(securityProperties.getAdminGroup()) @@ -96,7 +96,7 @@ void shouldReturnAuthenticationSuccessWhenAdminWithGroups() { .build()) .build(); - when(roleBindingService.listByGroups(any())) + when(roleBindingService.findAllByGroups(any())) .thenReturn(List.of(roleBinding)); when(resourceBasedSecurityRule.computeRolesFromGroups(any())) @@ -144,7 +144,7 @@ void shouldReturnAuthenticationSuccessWhenUserWithGroups() { .build()) .build(); - when(roleBindingService.listByGroups(any())) + when(roleBindingService.findAllByGroups(any())) .thenReturn(List.of(roleBinding)); when(resourceBasedSecurityRule.computeRolesFromGroups(any())) diff --git a/src/test/java/com/michelin/ns4kafka/service/NamespaceServiceTest.java b/src/test/java/com/michelin/ns4kafka/service/NamespaceServiceTest.java index 195a4dfe..f493ed0f 100644 --- a/src/test/java/com/michelin/ns4kafka/service/NamespaceServiceTest.java +++ b/src/test/java/com/michelin/ns4kafka/service/NamespaceServiceTest.java @@ -372,7 +372,7 @@ void listAllNamespaceResourcesEmpty() { .thenReturn(List.of()); when(connectorService.findAllForNamespace(ns)) .thenReturn(List.of()); - when(roleBindingService.list("namespace")) + when(roleBindingService.findAllForNamespace("namespace")) .thenReturn(List.of()); when(aclService.findAllForNamespace(ns)) .thenReturn(List.of()); @@ -409,7 +409,7 @@ void listAllNamespaceResourcesTopic() { .thenReturn(List.of(topic)); when(connectorService.findAllForNamespace(ns)) .thenReturn(List.of()); - when(roleBindingService.list("namespace")) + when(roleBindingService.findAllForNamespace("namespace")) .thenReturn(List.of()); when(aclService.findAllForNamespace(ns)) .thenReturn(List.of()); @@ -447,7 +447,7 @@ void listAllNamespaceResourcesConnect() { .thenReturn(List.of()); when(connectorService.findAllForNamespace(ns)) .thenReturn(List.of(connector)); - when(roleBindingService.list("namespace")) + when(roleBindingService.findAllForNamespace("namespace")) .thenReturn(List.of()); when(aclService.findAllForNamespace(ns)) .thenReturn(List.of()); @@ -485,7 +485,7 @@ void listAllNamespaceResourcesRoleBinding() { .thenReturn(List.of()); when(connectorService.findAllForNamespace(ns)) .thenReturn(List.of()); - when(roleBindingService.list("namespace")) + when(roleBindingService.findAllForNamespace("namespace")) .thenReturn(List.of(rb)); when(aclService.findAllForNamespace(ns)) .thenReturn(List.of()); @@ -523,7 +523,7 @@ void listAllNamespaceResourcesAccessControlEntry() { .thenReturn(List.of()); when(connectorService.findAllForNamespace(ns)) .thenReturn(List.of()); - when(roleBindingService.list("namespace")) + when(roleBindingService.findAllForNamespace("namespace")) .thenReturn(List.of()); when(aclService.findAllForNamespace(ns)) .thenReturn(List.of(ace)); @@ -561,7 +561,7 @@ void listAllNamespaceResourcesConnectCluster() { .thenReturn(List.of()); when(connectorService.findAllForNamespace(ns)) .thenReturn(List.of()); - when(roleBindingService.list("namespace")) + when(roleBindingService.findAllForNamespace("namespace")) .thenReturn(List.of()); when(aclService.findAllForNamespace(ns)) .thenReturn(List.of()); @@ -599,7 +599,7 @@ void listAllNamespaceResourcesQuota() { .thenReturn(List.of()); when(connectorService.findAllForNamespace(ns)) .thenReturn(List.of()); - when(roleBindingService.list("namespace")) + when(roleBindingService.findAllForNamespace("namespace")) .thenReturn(List.of()); when(aclService.findAllForNamespace(ns)) .thenReturn(List.of()); diff --git a/src/test/java/com/michelin/ns4kafka/service/RoleBindingServiceTest.java b/src/test/java/com/michelin/ns4kafka/service/RoleBindingServiceTest.java index 1724acaa..83938c9e 100644 --- a/src/test/java/com/michelin/ns4kafka/service/RoleBindingServiceTest.java +++ b/src/test/java/com/michelin/ns4kafka/service/RoleBindingServiceTest.java @@ -22,19 +22,21 @@ class RoleBindingServiceTest { RoleBindingService roleBindingService; @Test - void findByName() { + void shouldFindByName() { RoleBinding rb1 = RoleBinding.builder() .metadata(Metadata.builder() .name("namespace-rb1") .cluster("local") .build()) .build(); + RoleBinding rb2 = RoleBinding.builder() .metadata(Metadata.builder() .name("namespace-rb2") .cluster("local") .build()) .build(); + RoleBinding rb3 = RoleBinding.builder() .metadata(Metadata.builder() .name("namespace-rb3") @@ -42,30 +44,61 @@ void findByName() { .build()) .build(); - when(roleBindingRepository.findAllForNamespace("namespace")).thenReturn(List.of(rb1, rb2, rb3)); + when(roleBindingRepository.findAllForNamespace("namespace")) + .thenReturn(List.of(rb1, rb2, rb3)); var result = roleBindingService.findByName("namespace", "namespace-rb2"); assertEquals(rb2, result.orElse(null)); } @Test - void listRoleBindingWithoutParameter() { - RoleBinding rb1 = RoleBinding.builder().metadata(Metadata.builder().name("namespace-rb1").build()).build(); - RoleBinding rb2 = RoleBinding.builder().metadata(Metadata.builder().name("namespace-rb2").build()).build(); - RoleBinding rb3 = RoleBinding.builder().metadata(Metadata.builder().name("namespace-rb3").build()).build(); + void shouldListRoleBindingsWithoutParameter() { + RoleBinding rb1 = RoleBinding.builder() + .metadata(Metadata.builder() + .name("namespace-rb1") + .build()) + .build(); - when(roleBindingRepository.findAllForNamespace("namespace")).thenReturn(List.of(rb1, rb2, rb3)); + RoleBinding rb2 = RoleBinding.builder() + .metadata(Metadata.builder() + .name("namespace-rb2") + .build()) + .build(); - assertEquals(List.of(rb1, rb2, rb3), roleBindingService.list("namespace")); + RoleBinding rb3 = RoleBinding.builder() + .metadata(Metadata.builder() + .name("namespace-rb3") + .build()) + .build(); + + when(roleBindingRepository.findAllForNamespace("namespace")) + .thenReturn(List.of(rb1, rb2, rb3)); + + assertEquals(List.of(rb1, rb2, rb3), roleBindingService.findAllForNamespace("namespace")); } @Test - void listRoleBindingWithNameParameter() { - RoleBinding rb1 = RoleBinding.builder().metadata(Metadata.builder().name("namespace-rb1").build()).build(); - RoleBinding rb2 = RoleBinding.builder().metadata(Metadata.builder().name("namespace-rb2").build()).build(); - RoleBinding rb3 = RoleBinding.builder().metadata(Metadata.builder().name("namespace-rb3").build()).build(); + void shouldListRoleBindingsWithNameParameter() { + RoleBinding rb1 = RoleBinding.builder() + .metadata(Metadata.builder() + .name("namespace-rb1") + .build()) + .build(); - when(roleBindingRepository.findAllForNamespace("namespace")).thenReturn(List.of(rb1, rb2, rb3)); + RoleBinding rb2 = RoleBinding.builder() + .metadata(Metadata.builder() + .name("namespace-rb2") + .build()) + .build(); + + RoleBinding rb3 = RoleBinding.builder() + .metadata(Metadata.builder() + .name("namespace-rb3") + .build()) + .build(); + + when(roleBindingRepository.findAllForNamespace("namespace")) + .thenReturn(List.of(rb1, rb2, rb3)); assertEquals(List.of(rb1), roleBindingService.findByWildcardName("namespace", "namespace-rb1")); assertEquals(List.of(rb1, rb2, rb3), roleBindingService.findByWildcardName("namespace", "")); @@ -73,13 +106,33 @@ void listRoleBindingWithNameParameter() { } @Test - void listRoleBindingWithWildcardNameParameter() { - RoleBinding rb1 = RoleBinding.builder().metadata(Metadata.builder().name("namespace-rb1").build()).build(); - RoleBinding rb2 = RoleBinding.builder().metadata(Metadata.builder().name("namespace-rb2").build()).build(); - RoleBinding rb3 = RoleBinding.builder().metadata(Metadata.builder().name("namespace-rb3").build()).build(); - RoleBinding rb4 = RoleBinding.builder().metadata(Metadata.builder().name("rb4").build()).build(); + void shouldListRoleBindingsWithWildcardNameParameter() { + RoleBinding rb1 = RoleBinding.builder() + .metadata(Metadata.builder() + .name("namespace-rb1") + .build()) + .build(); + + RoleBinding rb2 = RoleBinding.builder() + .metadata(Metadata.builder() + .name("namespace-rb2") + .build()) + .build(); + + RoleBinding rb3 = RoleBinding.builder() + .metadata(Metadata.builder() + .name("namespace-rb3") + .build()) + .build(); + + RoleBinding rb4 = RoleBinding.builder() + .metadata(Metadata.builder() + .name("rb4") + .build()) + .build(); - when(roleBindingRepository.findAllForNamespace("namespace")).thenReturn(List.of(rb1, rb2, rb3, rb4)); + when(roleBindingRepository.findAllForNamespace("namespace")) + .thenReturn(List.of(rb1, rb2, rb3, rb4)); assertEquals(List.of(rb1, rb2, rb3), roleBindingService.findByWildcardName("namespace", "namespace-*")); assertEquals(List.of(rb1, rb2, rb3, rb4), roleBindingService.findByWildcardName("namespace", "*rb?"));