From c644c87c78ff48fd998264cb97c13260cdc1cfe5 Mon Sep 17 00:00:00 2001 From: thcai Date: Thu, 1 Aug 2024 09:54:10 +0200 Subject: [PATCH] Deprecated annotation + rename of method + reuse of method for better maintenance --- .../controller/ConnectorController.java | 5 +++-- .../ns4kafka/service/ConnectorService.java | 11 ++++------ .../controller/ConnectorControllerTest.java | 12 ++++++----- .../service/ConnectorServiceTest.java | 20 +++++++++---------- 4 files changed, 24 insertions(+), 24 deletions(-) diff --git a/src/main/java/com/michelin/ns4kafka/controller/ConnectorController.java b/src/main/java/com/michelin/ns4kafka/controller/ConnectorController.java index 99c597da..d13368a9 100644 --- a/src/main/java/com/michelin/ns4kafka/controller/ConnectorController.java +++ b/src/main/java/com/michelin/ns4kafka/controller/ConnectorController.java @@ -55,7 +55,7 @@ public class ConnectorController extends NamespacedResourceController { */ @Get public List list(String namespace, @QueryValue(defaultValue = "*") String name) { - return connectorService.findAllForNamespace(getNamespace(namespace), name); + return connectorService.findByWildcardName(getNamespace(namespace), name); } /** @@ -66,7 +66,8 @@ public List list(String namespace, @QueryValue(defaultValue = "*") St * @return A connector */ @Get("/{connector}") - public Optional getConnector(String namespace, String connector) { + @Deprecated(since = "1.12.0") + public Optional get(String namespace, String connector) { return connectorService.findByName(getNamespace(namespace), connector); } diff --git a/src/main/java/com/michelin/ns4kafka/service/ConnectorService.java b/src/main/java/com/michelin/ns4kafka/service/ConnectorService.java index a96288cc..9ff74a98 100644 --- a/src/main/java/com/michelin/ns4kafka/service/ConnectorService.java +++ b/src/main/java/com/michelin/ns4kafka/service/ConnectorService.java @@ -70,17 +70,14 @@ public List findAllForNamespace(Namespace namespace) { * Find all connectors by given namespace, filtered by name parameter. * * @param namespace The namespace - * @param name The name filter + * @param name The name parameter * @return A list of connectors */ - public List findAllForNamespace(Namespace namespace, String name) { - List acls = aclService - .findResourceOwnerGrantedToNamespace(namespace, AccessControlEntry.ResourceType.CONNECT); + public List findByWildcardName(Namespace namespace, String name) { List nameFilterPatterns = RegexUtils.wildcardStringsToRegexPatterns(List.of(name)); - return connectorRepository.findAllForCluster(namespace.getMetadata().getCluster()) + return findAllForNamespace(namespace) .stream() - .filter(connector -> aclService.isAnyAclOfResource(acls, connector.getMetadata().getName()) - && RegexUtils.filterByPattern(connector.getMetadata().getName(), nameFilterPatterns)) + .filter(connector -> RegexUtils.filterByPattern(connector.getMetadata().getName(), nameFilterPatterns)) .toList(); } diff --git a/src/test/java/com/michelin/ns4kafka/controller/ConnectorControllerTest.java b/src/test/java/com/michelin/ns4kafka/controller/ConnectorControllerTest.java index ce9a0478..b1f2e21a 100644 --- a/src/test/java/com/michelin/ns4kafka/controller/ConnectorControllerTest.java +++ b/src/test/java/com/michelin/ns4kafka/controller/ConnectorControllerTest.java @@ -69,7 +69,7 @@ void listEmptyConnectors() { .build(); when(namespaceService.findByName("test")).thenReturn(Optional.of(ns)); - when(connectorService.findAllForNamespace(ns, "*")).thenReturn(List.of()); + when(connectorService.findByWildcardName(ns, "*")).thenReturn(List.of()); assertTrue(connectorController.list("test", "*").isEmpty()); } @@ -87,7 +87,7 @@ void listMultipleConnectors() { Connector connector2 = Connector.builder().metadata(Metadata.builder().name("connect2").build()).build(); when(namespaceService.findByName("test")).thenReturn(Optional.of(ns)); - when(connectorService.findAllForNamespace(ns, "*")).thenReturn(List.of(connector1, connector2)); + when(connectorService.findByWildcardName(ns, "*")).thenReturn(List.of(connector1, connector2)); assertEquals(List.of(connector1, connector2), connectorController.list("test", "*")); } @@ -104,12 +104,13 @@ void listConnectorWithNameParameter() { Connector connector = Connector.builder().metadata(Metadata.builder().name("connect1").build()).build(); when(namespaceService.findByName("test")).thenReturn(Optional.of(ns)); - when(connectorService.findAllForNamespace(ns, "connect1")).thenReturn(List.of(connector)); + when(connectorService.findByWildcardName(ns, "connect1")).thenReturn(List.of(connector)); assertEquals(List.of(connector), connectorController.list("test", "connect1")); } @Test + @SuppressWarnings("deprecation") void getConnectorEmpty() { Namespace ns = Namespace.builder() .metadata(Metadata.builder() @@ -123,11 +124,12 @@ void getConnectorEmpty() { when(connectorService.findByName(ns, "missing")) .thenReturn(Optional.empty()); - Optional actual = connectorController.getConnector("test", "missing"); + Optional actual = connectorController.get("test", "missing"); assertTrue(actual.isEmpty()); } @Test + @SuppressWarnings("deprecation") void getConnector() { Namespace ns = Namespace.builder() .metadata(Metadata.builder() @@ -142,7 +144,7 @@ void getConnector() { .thenReturn(Optional.of( Connector.builder().metadata(Metadata.builder().name("connect1").build()).build())); - Optional actual = connectorController.getConnector("test", "connect1"); + Optional actual = connectorController.get("test", "connect1"); assertTrue(actual.isPresent()); assertEquals("connect1", actual.get().getMetadata().getName()); } diff --git a/src/test/java/com/michelin/ns4kafka/service/ConnectorServiceTest.java b/src/test/java/com/michelin/ns4kafka/service/ConnectorServiceTest.java index c25c5414..1aeb9a16 100644 --- a/src/test/java/com/michelin/ns4kafka/service/ConnectorServiceTest.java +++ b/src/test/java/com/michelin/ns4kafka/service/ConnectorServiceTest.java @@ -171,7 +171,7 @@ void listConnectorWithoutParameter() { when(aclService.isAnyAclOfResource(acls, "other-connect2")).thenReturn(false); when(aclService.isAnyAclOfResource(acls, "ns2-connect1")).thenReturn(false); - assertEquals(List.of(c1, c2), connectorService.findAllForNamespace(ns, "*")); + assertEquals(List.of(c1, c2), connectorService.findByWildcardName(ns, "*")); } @Test @@ -218,10 +218,10 @@ void listConnectorWithNameParameter() { when(aclService.isAnyAclOfResource(acls, "other-connect2")).thenReturn(false); when(aclService.isAnyAclOfResource(acls, "ns2-connect1")).thenReturn(false); - assertEquals(List.of(c1), connectorService.findAllForNamespace(ns, "ns-connect1")); - assertEquals(List.of(c2), connectorService.findAllForNamespace(ns, "other-connect1")); - assertTrue(connectorService.findAllForNamespace(ns, "ns2-connect1").isEmpty()); - assertTrue(connectorService.findAllForNamespace(ns, "ns4-connect1").isEmpty()); + assertEquals(List.of(c1), connectorService.findByWildcardName(ns, "ns-connect1")); + assertEquals(List.of(c2), connectorService.findByWildcardName(ns, "other-connect1")); + assertTrue(connectorService.findByWildcardName(ns, "ns2-connect1").isEmpty()); + assertTrue(connectorService.findByWildcardName(ns, "ns4-connect1").isEmpty()); } @Test @@ -270,11 +270,11 @@ void listConnectorWithWildcardNameParameter() { when(aclService.isAnyAclOfResource(acls, "other-connect2")).thenReturn(true); when(aclService.isAnyAclOfResource(acls, "ns2-connect1")).thenReturn(false); - assertEquals(List.of(c1, c2), connectorService.findAllForNamespace(ns, "ns-connect?")); - assertEquals(List.of(c1, c3), connectorService.findAllForNamespace(ns, "*-connect1")); - assertEquals(List.of(c1, c2, c3, c4), connectorService.findAllForNamespace(ns, "*-connect?")); - assertTrue(connectorService.findAllForNamespace(ns, "ns2-*").isEmpty()); - assertTrue(connectorService.findAllForNamespace(ns, "ns*4-connect?").isEmpty()); + assertEquals(List.of(c1, c2), connectorService.findByWildcardName(ns, "ns-connect?")); + assertEquals(List.of(c1, c3), connectorService.findByWildcardName(ns, "*-connect1")); + assertEquals(List.of(c1, c2, c3, c4), connectorService.findByWildcardName(ns, "*-connect?")); + assertTrue(connectorService.findByWildcardName(ns, "ns2-*").isEmpty()); + assertTrue(connectorService.findByWildcardName(ns, "ns*4-connect?").isEmpty()); } @Test