Skip to content

Commit

Permalink
Deprecated annotation + rename of method + reuse of method for better…
Browse files Browse the repository at this point in the history
… maintenance
  • Loading branch information
ThomasCAI-mlv committed Aug 1, 2024
1 parent 25a3126 commit c644c87
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class ConnectorController extends NamespacedResourceController {
*/
@Get
public List<Connector> list(String namespace, @QueryValue(defaultValue = "*") String name) {
return connectorService.findAllForNamespace(getNamespace(namespace), name);
return connectorService.findByWildcardName(getNamespace(namespace), name);
}

/**
Expand All @@ -66,7 +66,8 @@ public List<Connector> list(String namespace, @QueryValue(defaultValue = "*") St
* @return A connector
*/
@Get("/{connector}")
public Optional<Connector> getConnector(String namespace, String connector) {
@Deprecated(since = "1.12.0")
public Optional<Connector> get(String namespace, String connector) {
return connectorService.findByName(getNamespace(namespace), connector);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,14 @@ public List<Connector> 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<Connector> findAllForNamespace(Namespace namespace, String name) {
List<AccessControlEntry> acls = aclService
.findResourceOwnerGrantedToNamespace(namespace, AccessControlEntry.ResourceType.CONNECT);
public List<Connector> findByWildcardName(Namespace namespace, String name) {
List<String> 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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand All @@ -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", "*"));
}
Expand All @@ -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()
Expand All @@ -123,11 +124,12 @@ void getConnectorEmpty() {
when(connectorService.findByName(ns, "missing"))
.thenReturn(Optional.empty());

Optional<Connector> actual = connectorController.getConnector("test", "missing");
Optional<Connector> actual = connectorController.get("test", "missing");
assertTrue(actual.isEmpty());
}

@Test
@SuppressWarnings("deprecation")
void getConnector() {
Namespace ns = Namespace.builder()
.metadata(Metadata.builder()
Expand All @@ -142,7 +144,7 @@ void getConnector() {
.thenReturn(Optional.of(
Connector.builder().metadata(Metadata.builder().name("connect1").build()).build()));

Optional<Connector> actual = connectorController.getConnector("test", "connect1");
Optional<Connector> actual = connectorController.get("test", "connect1");
assertTrue(actual.isPresent());
assertEquals("connect1", actual.get().getMetadata().getName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit c644c87

Please sign in to comment.