Skip to content

Commit

Permalink
tests with multiple common friends
Browse files Browse the repository at this point in the history
  • Loading branch information
danthe1st committed Jul 29, 2024
1 parent 2f5fbfb commit 3d3b823
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.danthe1st.arebac.data.graph_pattern;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand All @@ -22,8 +23,8 @@ public record GPGraph(Map<String, GPNode> nodes,
public GPGraph(List<GPNode> nodes, List<GPEdge> edges) {
this(
nodes.stream().collect(Collectors.toMap(GPNode::id, Function.identity())),
edges.stream().collect(Collectors.groupingBy(GPEdge::source)),
edges.stream().collect(Collectors.groupingBy(GPEdge::target))
edges.stream().collect(Collectors.groupingBy(GPEdge::source, LinkedHashMap::new, Collectors.toList())),
edges.stream().collect(Collectors.groupingBy(GPEdge::target, LinkedHashMap::new, Collectors.toList()))
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package io.github.danthe1st.arebac.tests.weaving;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.List;
import java.util.Map;

import io.github.danthe1st.arebac.data.graph_pattern.GPEdge;
import io.github.danthe1st.arebac.data.graph_pattern.GPGraph;
import io.github.danthe1st.arebac.data.graph_pattern.GPNode;
import io.github.danthe1st.arebac.data.graph_pattern.GraphPattern;
import io.github.danthe1st.arebac.data.graph_pattern.constraints.MutualExclusionConstraint;
import io.github.danthe1st.arebac.weaving.Weaving;
import org.junit.jupiter.api.Test;

class MultipleCommonFriendsWeavingTest {
private static final String FRIEND_EDGE_TYPE = "friend";
private static final String USER_NODE_TYPE = "user";

@Test
void testCombineFriendsOfFriendsWithNotSelf() {
GraphPattern combined = Weaving.combinePatterns(List.of(createCommonFriendsPattern(), createRequestorNotTargetPattern()));
assertEquals(createExpectedCombinedFriendsOfFriendsButNotSelfPattern(), combined);
}

private GraphPattern createExpectedCombinedFriendsOfFriendsButNotSelfPattern() {
GPNode requestor = new GPNode("p0,requestor", USER_NODE_TYPE);
GPNode target = new GPNode("p0,target", USER_NODE_TYPE);
GPNode firstFriend = new GPNode("p0,f1", USER_NODE_TYPE);
GPNode secondFriend = new GPNode("p0,f2", USER_NODE_TYPE);

return new GraphPattern(
new GPGraph(
List.of(requestor, target, firstFriend, secondFriend),
List.of(
new GPEdge(requestor, firstFriend, "p0,req->f1", FRIEND_EDGE_TYPE),
new GPEdge(requestor, secondFriend, "p0,req->f2", FRIEND_EDGE_TYPE),
new GPEdge(firstFriend, target, "p0,f1->target", FRIEND_EDGE_TYPE),
new GPEdge(secondFriend, target, "p0,f2->target", FRIEND_EDGE_TYPE)
)
),
List.of(
new MutualExclusionConstraint(firstFriend, secondFriend),
new MutualExclusionConstraint(requestor, target)
),
Map.of(),
Map.of(),
List.of(target),
Map.of("requestor", requestor, "target", target)
);
}

private GraphPattern createRequestorNotTargetPattern() {
GPNode requestor = new GPNode("requestor", USER_NODE_TYPE);
GPNode target = new GPNode("target", USER_NODE_TYPE);

return new GraphPattern(
new GPGraph(List.of(requestor, target), List.of()),
List.of(new MutualExclusionConstraint(requestor, target)),
Map.of(),
Map.of(),
List.of(),
Map.of("requestor", requestor, "target", target)
);
}

private GraphPattern createCommonFriendsPattern() {
GPNode requestor = new GPNode("requestor", USER_NODE_TYPE);
GPNode target = new GPNode("target", USER_NODE_TYPE);
GPNode firstFriend = new GPNode("f1", USER_NODE_TYPE);
GPNode secondFriend = new GPNode("f2", USER_NODE_TYPE);

return new GraphPattern(
new GPGraph(
List.of(requestor, target, firstFriend, secondFriend),
List.of(
new GPEdge(requestor, firstFriend, "req->f1", FRIEND_EDGE_TYPE),
new GPEdge(requestor, secondFriend, "req->f2", FRIEND_EDGE_TYPE),
new GPEdge(firstFriend, target, "f1->target", FRIEND_EDGE_TYPE),
new GPEdge(secondFriend, target, "f2->target", FRIEND_EDGE_TYPE)
)
), List.of(new MutualExclusionConstraint(firstFriend, secondFriend)),
Map.of(),
Map.of(),
List.of(target),
Map.of("requestor", requestor, "target", target)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package io.github.danthe1st.arebac.tests.weaving;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.List;
import java.util.Map;

import io.github.danthe1st.arebac.data.graph_pattern.GPGraph;
import io.github.danthe1st.arebac.data.graph_pattern.GPNode;
import io.github.danthe1st.arebac.data.graph_pattern.GraphPattern;
import io.github.danthe1st.arebac.weaving.Weaving;
import org.junit.jupiter.api.Test;

class MultiplePatternsReturnSameNodeTest {
@Test
void testMeregPatternsReturnSameResult() {
GPNode singleNode = new GPNode("someNode", "user");
GraphPattern simplePattern = new GraphPattern(
new GPGraph(List.of(singleNode), List.of()),
List.of(), Map.of(), Map.of(),
List.of(singleNode),
Map.of("relevantNode", singleNode)
);
GraphPattern combinedWithItself = Weaving.combinePatterns(List.of(simplePattern, simplePattern));

GPNode nodeInResult = new GPNode("p0,someNode", "user");
GraphPattern expectedPattern = new GraphPattern(
new GPGraph(List.of(nodeInResult), List.of()),
List.of(), Map.of(), Map.of(),
List.of(nodeInResult, nodeInResult),
Map.of("relevantNode", nodeInResult)
);
assertEquals(expectedPattern, combinedWithItself);
}
}

0 comments on commit 3d3b823

Please sign in to comment.