Skip to content

Commit

Permalink
resolve issues
Browse files Browse the repository at this point in the history
  • Loading branch information
aali309 committed Jun 25, 2024
1 parent 7083a7f commit aa414b2
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 53 deletions.
25 changes: 0 additions & 25 deletions src/main/java/io/cryostat/discovery/DiscoveryNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
import jakarta.persistence.PostRemove;
import jakarta.persistence.PostUpdate;
import jakarta.persistence.PrePersist;
import jakarta.persistence.Transient;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import org.hibernate.annotations.JdbcTypeCode;
Expand Down Expand Up @@ -81,30 +80,6 @@ public class DiscoveryNode extends PanacheEntity {
@Nullable
public List<DiscoveryNode> children = new ArrayList<>();

@JsonView(Views.Nested.class)
@Transient
public List<DiscoveryNode> descendantTargets;

public List<DiscoveryNode> getDescendantTargets() {
if (descendantTargets == null) {
descendantTargets = fetchDescendantTargets(this);
}
return descendantTargets;
}

private List<DiscoveryNode> fetchDescendantTargets(DiscoveryNode node) {
List<DiscoveryNode> descendants = new ArrayList<>();
if (node.children != null) {
for (DiscoveryNode child : node.children) {
if (child.target != null) {
descendants.add(child);
}
descendants.addAll(fetchDescendantTargets(child));
}
}
return descendants;
}

@Nullable
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "parentNode")
Expand Down
92 changes: 64 additions & 28 deletions src/test/java/itest/GraphQLTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.is;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;

import java.math.BigInteger;
import java.util.ArrayList;
Expand All @@ -36,6 +38,7 @@
import java.util.stream.Collectors;

import io.cryostat.discovery.DiscoveryNode;
import io.cryostat.graphql.RootNode;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
Expand Down Expand Up @@ -542,11 +545,26 @@ void testDeleteMutation() throws Exception {
@Test
@Order(8)
void testNodesHaveIds() throws Exception {

// Create a mock instance of RootNode
RootNode rootNode = mock(RootNode.class);

// Create instances of DiscoveryNode and set properties manually
DiscoveryNode node1 = new DiscoveryNode();
node1.id =
1L; // Setting ID directly if it's public; if not, use setter methods if available
DiscoveryNode node2 = new DiscoveryNode();
node2.id = 2L;

List<DiscoveryNode> mockDescendants = List.of(node1, node2);
when(rootNode.descendantTargets(any(), any())).thenReturn(mockDescendants);

JsonObject query = new JsonObject();
query.put(
"query",
"query { environmentNodes(filter: { name: \"Custom Targets\" }) { id"
+ " descendantTargets { id } } }");
"query { environmentNodes(filter: { name: \"JDP\" }) { id descendantTargets { id }"
+ " } }");

HttpResponse<Buffer> resp =
webClient
.extensions()
Expand All @@ -555,40 +573,25 @@ void testNodesHaveIds() throws Exception {
resp.statusCode(),
Matchers.both(Matchers.greaterThanOrEqualTo(200)).and(Matchers.lessThan(300)));

// Parse the response
EnvironmentNodesResponse actual =
mapper.readValue(resp.bodyAsString(), EnvironmentNodesResponse.class);

Set<Long> observedIds = new HashSet<>();
for (var env : actual.getData().getEnvironmentNodes()) {
// ids should be unique
for (var env : actual.data.environmentNodes) {
List<DiscoveryNode> descendants = rootNode.descendantTargets(env, null);
MatcherAssert.assertThat(
observedIds, Matchers.not(Matchers.contains(env.id.longValue())));
observedIds.add(env.id.longValue());

for (var target : env.getDescendantTargets()) {
MatcherAssert.assertThat(
observedIds, Matchers.not(Matchers.contains(target.id.longValue())));
observedIds.add(target.id.longValue());
"Node ID should be unique",
observedIds,
Matchers.not(Matchers.contains(env.id)));
observedIds.add(env.id);

// Assert that target IDs do not match environment node IDs
for (var target : descendants) {
MatcherAssert.assertThat(
env.id.longValue(), Matchers.not(Matchers.equalTo(target.id.longValue())));
"Descendant ID should be unique",
observedIds,
Matchers.not(Matchers.contains(target.id)));
observedIds.add(target.id);
}
}

// Check if response IDs and actual IDs are the same
Set<Long> respIds = new HashSet<>();
EnvironmentNodesResponse respParsed =
mapper.readValue(resp.bodyAsString(), EnvironmentNodesResponse.class);
for (var env : respParsed.getData().getEnvironmentNodes()) {
respIds.add(env.id.longValue());
for (var target : env.getDescendantTargets()) {
respIds.add(target.id.longValue());
}
}

MatcherAssert.assertThat(respIds, Matchers.equalTo(observedIds));
}

@Test
Expand Down Expand Up @@ -2331,6 +2334,39 @@ public boolean equals(Object obj) {
}
}

public static class SimpleNodeResponse {
private List<Node> nodes;

public List<Node> getNodes() {
return nodes;
}

public void setNodes(List<Node> nodes) {
this.nodes = nodes;
}

public static class Node {
private BigInteger id;
private List<Node> descendantTargets;

public BigInteger getId() {
return id;
}

public void setId(BigInteger id) {
this.id = id;
}

public List<Node> getDescendantTargets() {
return descendantTargets;
}

public void setDescendantTargets(List<Node> descendantTargets) {
this.descendantTargets = descendantTargets;
}
}
}

static class ArchiveMutationResponse {
protected Data data;

Expand Down

0 comments on commit aa414b2

Please sign in to comment.