Skip to content

Commit

Permalink
feat(GraphQL): Add GraphQL filter ability for node name from set (#1518)
Browse files Browse the repository at this point in the history
  • Loading branch information
aali309 authored Jun 27, 2023
1 parent b3ac76a commit 3352d27
Show file tree
Hide file tree
Showing 11 changed files with 572 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,14 @@ public Active getAuthenticated(DataFetchingEnvironment environment) throws Excep
.filter(r -> Objects.equals(r.getName(), recordingName))
.collect(Collectors.toList());
}
if (filter.contains(FilterInput.Key.NAMES)) {
List<String> recordingNames = filter.get(FilterInput.Key.NAMES);
recordings =
recordings.stream()
.filter(r -> recordingNames.contains(r.getName()))
.collect(Collectors.toList());
}

if (filter.contains(FilterInput.Key.LABELS)) {
List<String> labels = filter.get(FilterInput.Key.LABELS);
for (String label : labels) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,14 @@ public Archived getAuthenticated(DataFetchingEnvironment environment) throws Exc
.filter(r -> Objects.equals(r.getName(), recordingName))
.collect(Collectors.toList());
}
if (filter.contains(FilterInput.Key.NAMES)) {
List<String> names = filter.get(FilterInput.Key.NAMES);
recordings =
recordings.stream()
.filter(r -> names.contains(r.getName()))
.collect(Collectors.toList());
}

if (filter.contains(FilterInput.Key.LABELS)) {
List<String> labels = filter.get(FilterInput.Key.LABELS);
for (String label : labels) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ public List<EnvironmentNode> getAuthenticated(DataFetchingEnvironment environmen
nodes = filter(nodes, n -> Objects.equals(n.getName(), nodeName));
}

if (filter.contains(FilterInput.Key.NAMES)) {
List<String> names = filter.get(FilterInput.Key.NAMES);
nodes = filter(nodes, n -> names.contains(n.getName()));
}

if (filter.contains(FilterInput.Key.LABELS)) {
List<String> labels = filter.get(FilterInput.Key.LABELS);
for (String label : labels) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ <T> T get(Key key) {
enum Key {
ID("id"),
NAME("name"),
NAMES("names"),
LABELS("labels"),
ANNOTATIONS("annotations"),
SOURCE_TARGET("sourceTarget"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ public List<TargetNode> getAuthenticated(DataFetchingEnvironment environment) th
.filter(n -> Objects.equals(n.getName(), nodeName))
.collect(Collectors.toList());
}
if (filter.contains(FilterInput.Key.NAMES)) {
List<String> names = filter.get(FilterInput.Key.NAMES);
result =
result.stream()
.filter(n -> names.contains(n.getName()))
.collect(Collectors.toList());
}
if (filter.contains(FilterInput.Key.LABELS)) {
List<String> labels = filter.get(FilterInput.Key.LABELS);
for (String label : labels) {
Expand Down
5 changes: 5 additions & 0 deletions src/main/resources/types.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,30 @@ scalar Float
input EnvironmentNodeFilterInput {
id: Int
name: String
names: [String]
nodeType: String
labels: [String]
}

input TargetNodesFilterInput {
id: Int
name: String
names: [String]
labels: [String]
annotations: [String]
}

input DescendantTargetsFilterInput {
id: Int
name: String
names: [String]
labels: [String]
annotations: [String]
}

input ActiveRecordingFilterInput {
name: String
names: [String]
state: String
continuous: Boolean
toDisk: Boolean
Expand All @@ -42,6 +46,7 @@ input ActiveRecordingFilterInput {

input ArchivedRecordingFilterInput {
name: String
names: [String]
labels: [String]
sourceTarget: String
sizeBytesGreaterThanEqual: Long
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,4 +284,36 @@ void shouldReturnRecordingsMultipleFilters() throws Exception {
MatcherAssert.assertThat(active.aggregate.count, Matchers.equalTo(1L));
}
}

@Test
void shouldReturnRecordingsFilteredByNames() throws Exception {
try (MockedStatic<FilterInput> staticFilter = Mockito.mockStatic(FilterInput.class)) {
staticFilter.when(() -> FilterInput.from(env)).thenReturn(filter);
when(env.getGraphQlContext()).thenReturn(graphCtx);
when(auth.validateHttpHeader(Mockito.any(), Mockito.any()))
.thenReturn(CompletableFuture.completedFuture(true));

GraphRecordingDescriptor recording1 = Mockito.mock(GraphRecordingDescriptor.class);
GraphRecordingDescriptor recording2 = Mockito.mock(GraphRecordingDescriptor.class);
GraphRecordingDescriptor recording3 = Mockito.mock(GraphRecordingDescriptor.class);
when(recording1.getName()).thenReturn("Recording1");
when(recording2.getName()).thenReturn("Recording2");
when(recording3.getName()).thenReturn("Recording3");

when(filter.contains(Mockito.any())).thenReturn(false);
when(filter.contains(FilterInput.Key.NAMES)).thenReturn(true);
when(filter.get(FilterInput.Key.NAMES)).thenReturn(List.of("Recording1", "Recording3"));

Recordings source = Mockito.mock(Recordings.class);
source.active = List.of(recording1, recording2, recording3);

when(env.getSource()).thenReturn(source);

Active active = fetcher.get(env);

MatcherAssert.assertThat(active, Matchers.notNullValue());
MatcherAssert.assertThat(active.data, Matchers.contains(recording1, recording3));
MatcherAssert.assertThat(active.aggregate.count, Matchers.equalTo(2L));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -245,4 +245,37 @@ void shouldReturnRecordingsMultipleFilters() throws Exception {
Matchers.equalTo(recording3.getSize() + recording5.getSize()));
}
}

@Test
void shouldReturnArchivedRecordingsFilteredByNames() throws Exception {
try (MockedStatic<FilterInput> staticFilter = Mockito.mockStatic(FilterInput.class)) {
staticFilter.when(() -> FilterInput.from(env)).thenReturn(filter);
when(env.getGraphQlContext()).thenReturn(graphCtx);
when(auth.validateHttpHeader(Mockito.any(), Mockito.any()))
.thenReturn(CompletableFuture.completedFuture(true));

ArchivedRecordingInfo recording1 = Mockito.mock(ArchivedRecordingInfo.class);
ArchivedRecordingInfo recording2 = Mockito.mock(ArchivedRecordingInfo.class);
ArchivedRecordingInfo recording3 = Mockito.mock(ArchivedRecordingInfo.class);
when(recording1.getName()).thenReturn("foo");
when(recording2.getName()).thenReturn("bar");
when(recording3.getName()).thenReturn("baz");

when(filter.contains(Mockito.any())).thenReturn(false);
when(filter.contains(FilterInput.Key.NAMES)).thenReturn(true);
when(filter.get(FilterInput.Key.NAMES)).thenReturn(List.of("foo", "baz"));

Recordings source = Mockito.mock(Recordings.class);
source.archived = List.of(recording1, recording2, recording3);

when(env.getSource()).thenReturn(source);

Archived archived = fetcher.get(env);

MatcherAssert.assertThat(archived, Matchers.notNullValue());
MatcherAssert.assertThat(
archived.data, Matchers.containsInAnyOrder(recording1, recording3));
MatcherAssert.assertThat(archived.aggregate.count, Matchers.equalTo(2L));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

import static org.mockito.Mockito.when;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -175,4 +176,37 @@ void shouldReturnFilteredEnvironmentNodes() throws Exception {
Matchers.containsInAnyOrder(leftChildNode, rightChildNode, leftGrandchildNode));
}
}

@Test
void shouldReturnFilteredEnvironmentNodesByNames() throws Exception {
try (MockedStatic<FilterInput> staticFilter = Mockito.mockStatic(FilterInput.class)) {
staticFilter.when(() -> FilterInput.from(env)).thenReturn(filter);
when(env.getGraphQlContext()).thenReturn(graphCtx);
when(auth.validateHttpHeader(Mockito.any(), Mockito.any()))
.thenReturn(CompletableFuture.completedFuture(true));

EnvironmentNode northAmericaNode =
new EnvironmentNode("North America", BaseNodeType.REALM);
EnvironmentNode earthNode = new EnvironmentNode("Earth", BaseNodeType.REALM);
EnvironmentNode marsNode = new EnvironmentNode("Mars", BaseNodeType.REALM);

EnvironmentNode universe =
new EnvironmentNode(
"Universe",
BaseNodeType.UNIVERSE,
Collections.emptyMap(),
Set.of(northAmericaNode, earthNode, marsNode));

when(filter.contains(Mockito.any())).thenReturn(false);
when(filter.contains(FilterInput.Key.NAMES)).thenReturn(true);
when(filter.get(FilterInput.Key.NAMES)).thenReturn(Arrays.asList("Earth", "Mars"));

when(rootNodeFetcher.get(env)).thenReturn(universe);

List<EnvironmentNode> nodes = fetcher.get(env);

MatcherAssert.assertThat(nodes, Matchers.notNullValue());
MatcherAssert.assertThat(nodes, Matchers.containsInAnyOrder(earthNode, marsNode));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -151,24 +151,28 @@ void shouldReturnTargetsFiltered() throws Exception {
.thenReturn(CompletableFuture.completedFuture(true));

when(filter.contains(Mockito.any())).thenReturn(false);
when(filter.contains(FilterInput.Key.NAME)).thenReturn(true);
when(filter.get(FilterInput.Key.NAME)).thenReturn("foo");
when(filter.contains(FilterInput.Key.NAMES)).thenReturn(true);
when(filter.get(FilterInput.Key.NAMES)).thenReturn(List.of("foo", "bar", "baz"));

TargetNode target1 = Mockito.mock(TargetNode.class);
TargetNode target2 = Mockito.mock(TargetNode.class);
TargetNode target3 = Mockito.mock(TargetNode.class);
TargetNode target4 = Mockito.mock(TargetNode.class);
TargetNode target5 = Mockito.mock(TargetNode.class);

when(target1.getName()).thenReturn("foo");
when(target2.getName()).thenReturn("foobar");
when(target3.getName()).thenReturn("foo");
when(target4.getName()).thenReturn("bar");
when(target5.getName()).thenReturn("baz");

when(recurseFetcher.get(Mockito.any()))
.thenReturn(List.of(target1, target2, target3));
.thenReturn(List.of(target1, target2, target3, target4, target5));

List<TargetNode> nodes = fetcher.get(env);

MatcherAssert.assertThat(nodes, Matchers.notNullValue());
MatcherAssert.assertThat(nodes, Matchers.containsInAnyOrder(target1, target3));
MatcherAssert.assertThat(nodes, Matchers.hasItems(target1, target4, target5));
}
}
}
Expand Down
Loading

0 comments on commit 3352d27

Please sign in to comment.