Skip to content

Commit 3eeed88

Browse files
Make filtering parameters dynamic for LIST namespace API (#505)
* Namespace controller search param * Javadoc
1 parent 8bbec0d commit 3eeed88

File tree

3 files changed

+40
-19
lines changed

3 files changed

+40
-19
lines changed

src/main/java/com/michelin/ns4kafka/controller/NamespaceController.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import java.util.Collections;
4747
import java.util.Date;
4848
import java.util.List;
49+
import java.util.Map;
4950
import java.util.Optional;
5051

5152
/**
@@ -59,21 +60,21 @@ public class NamespaceController extends NonNamespacedResourceController {
5960
NamespaceService namespaceService;
6061

6162
/**
62-
* List namespaces, filtered by namespace name and topic name parameters.
63+
* List namespaces, which can be filtered based on optional search query parameters.
6364
*
64-
* @param name The namespace name parameter
65-
* @param topic The topic name parameter
65+
* @param search The map of optional query parameters used to filter namespaces. Supported parameters are:
66+
* - name: matches the namespace name. Wildcard supported.
67+
* - topic: find namespace owner of the given topic name.
6668
* @return A list of namespaces
6769
*/
68-
@Get
69-
public List<Namespace> list(@QueryValue(defaultValue = "*") String name,
70-
@QueryValue(defaultValue = "") String topic) {
71-
List<Namespace> namespaces = namespaceService.findByWildcardName(name);
70+
@Get("{?search*}")
71+
public List<Namespace> list(@QueryValue Map<String, String> search) {
72+
List<Namespace> namespaces = namespaceService.findByWildcardName(search.getOrDefault("name", "*"));
7273

73-
return topic.isEmpty() ? namespaces :
74-
namespaceService.findByTopicName(namespaces, topic)
74+
return search.containsKey("topic")
75+
? namespaceService.findByTopicName(namespaces, search.get("topic"))
7576
.map(Collections::singletonList)
76-
.orElse(List.of());
77+
.orElse(List.of()) : namespaces;
7778
}
7879

7980
/**

src/main/java/com/michelin/ns4kafka/service/NamespaceService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public List<Namespace> findByWildcardName(String name) {
101101
}
102102

103103
/**
104-
* Find the namespace which are owner of the given topic name, out of the given list.
104+
* Find the namespace which is owner of the given topic name, out of the given list.
105105
*
106106
* @param namespaces The namespaces list
107107
* @param topic The topic name to search

src/test/java/com/michelin/ns4kafka/controller/NamespaceControllerTest.java

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class NamespaceControllerTest {
6363
NamespaceController namespaceController;
6464

6565
@Test
66-
void shouldListNamespacesWithWildcardParameter() {
66+
void shouldListNamespaces() {
6767
Namespace ns1 = Namespace.builder()
6868
.metadata(Metadata.builder()
6969
.name("ns1")
@@ -79,7 +79,27 @@ void shouldListNamespacesWithWildcardParameter() {
7979
when(namespaceService.findByWildcardName("*"))
8080
.thenReturn(List.of(ns1, ns2));
8181

82-
assertEquals(List.of(ns1, ns2), namespaceController.list("*", ""));
82+
assertEquals(List.of(ns1, ns2), namespaceController.list(Map.of()));
83+
}
84+
85+
@Test
86+
void shouldListNamespacesWithWildcardNameParameter() {
87+
Namespace ns1 = Namespace.builder()
88+
.metadata(Metadata.builder()
89+
.name("ns1")
90+
.build())
91+
.build();
92+
93+
Namespace ns2 = Namespace.builder()
94+
.metadata(Metadata.builder()
95+
.name("ns2")
96+
.build())
97+
.build();
98+
99+
when(namespaceService.findByWildcardName("*"))
100+
.thenReturn(List.of(ns1, ns2));
101+
102+
assertEquals(List.of(ns1, ns2), namespaceController.list(Map.of("name", "*")));
83103
}
84104

85105
@Test
@@ -93,7 +113,7 @@ void shouldListNamespacesWithNameParameter() {
93113
when(namespaceService.findByWildcardName("ns"))
94114
.thenReturn(List.of(ns));
95115

96-
assertEquals(List.of(ns), namespaceController.list("ns", ""));
116+
assertEquals(List.of(ns), namespaceController.list(Map.of("name", "ns")));
97117
}
98118

99119
@Test
@@ -106,10 +126,10 @@ void shouldListNamespaceFilteredByTopic() {
106126

107127
when(namespaceService.findByWildcardName("*"))
108128
.thenReturn(List.of(ns));
109-
when(namespaceService.findByTopicName(List.of(ns), "topic"))
129+
when(namespaceService.findByTopicName(List.of(ns), "myTopicName"))
110130
.thenReturn(Optional.of(ns));
111131

112-
assertEquals(List.of(ns), namespaceController.list("*", "topic"));
132+
assertEquals(List.of(ns), namespaceController.list(Map.of("topic", "myTopicName")));
113133
}
114134

115135
@Test
@@ -122,16 +142,16 @@ void shouldListNoNamespaceFilteredByTopic() {
122142

123143
when(namespaceService.findByWildcardName("*"))
124144
.thenReturn(List.of(ns));
125-
when(namespaceService.findByTopicName(List.of(ns), "topic"))
145+
when(namespaceService.findByTopicName(List.of(ns), "myTopicName"))
126146
.thenReturn(Optional.empty());
127147

128-
assertEquals(List.of(), namespaceController.list("*", "topic"));
148+
assertEquals(List.of(), namespaceController.list(Map.of("topic", "myTopicName")));
129149
}
130150

131151
@Test
132152
void shouldListNamespacesWhenEmpty() {
133153
when(namespaceService.findByWildcardName("*")).thenReturn(List.of());
134-
assertEquals(List.of(), namespaceController.list("*", ""));
154+
assertEquals(List.of(), namespaceController.list(Map.of()));
135155
}
136156

137157
@Test

0 commit comments

Comments
 (0)