Skip to content

Commit

Permalink
integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tb06904 committed Jan 10, 2025
1 parent ba6796e commit a77f960
Show file tree
Hide file tree
Showing 2 changed files with 207 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2024 Crown Copyright
* Copyright 2024-2025 Crown Copyright
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -24,12 +24,15 @@
import uk.gov.gchq.gaffer.data.element.Element;
import uk.gov.gchq.gaffer.data.element.Entity;
import uk.gov.gchq.gaffer.data.element.Properties;
import uk.gov.gchq.gaffer.data.elementdefinition.view.View;
import uk.gov.gchq.gaffer.data.graph.Walk;
import uk.gov.gchq.gaffer.federated.simple.access.GraphAccess;
import uk.gov.gchq.gaffer.federated.simple.operation.AddGraph;
import uk.gov.gchq.gaffer.federated.simple.operation.GetAllGraphIds;
import uk.gov.gchq.gaffer.federated.simple.operation.GetAllGraphInfo;
import uk.gov.gchq.gaffer.federated.simple.operation.handler.FederatedOperationHandler;
import uk.gov.gchq.gaffer.federated.simple.operation.handler.get.GetAllGraphInfoHandler;
import uk.gov.gchq.gaffer.federated.simple.util.FederatedModernTestUtils;
import uk.gov.gchq.gaffer.federated.simple.util.FederatedTestUtils;
import uk.gov.gchq.gaffer.graph.Graph;
import uk.gov.gchq.gaffer.graph.GraphConfig;
Expand All @@ -38,8 +41,12 @@
import uk.gov.gchq.gaffer.named.operation.NamedOperation;
import uk.gov.gchq.gaffer.operation.OperationChain;
import uk.gov.gchq.gaffer.operation.OperationException;
import uk.gov.gchq.gaffer.operation.data.EntitySeed;
import uk.gov.gchq.gaffer.operation.graph.SeededGraphFilters;
import uk.gov.gchq.gaffer.operation.impl.GetWalks;
import uk.gov.gchq.gaffer.operation.impl.add.AddElements;
import uk.gov.gchq.gaffer.operation.impl.get.GetAllElements;
import uk.gov.gchq.gaffer.operation.impl.get.GetElements;
import uk.gov.gchq.gaffer.operation.impl.get.GetGraphCreatedTime;
import uk.gov.gchq.gaffer.store.Context;
import uk.gov.gchq.gaffer.store.StoreException;
Expand All @@ -52,6 +59,7 @@
import static uk.gov.gchq.gaffer.federated.simple.util.FederatedTestUtils.StoreType;

import java.util.AbstractMap.SimpleEntry;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -451,4 +459,67 @@ void shouldAddAndExecuteNamedOperationsFromSubGraphCache() throws OperationExcep
.withMessageContaining(namedOpName);
}

@Test
void shouldGetWalksAcrossFederatedGraphs() throws OperationException {
// Given
final Graph federatedGraph = FederatedModernTestUtils.setUpSimpleFederatedGraph(FederatedStoreIT.class, new MapStoreProperties());

final GetWalks getWalks = new GetWalks.Builder()
.operations(new GetElements.Builder()
.inOutType(SeededGraphFilters.IncludeIncomingOutgoingType.OUTGOING)
.view(new View.Builder().allEdges(true).build())
.build(),
new GetElements.Builder()
.inOutType(SeededGraphFilters.IncludeIncomingOutgoingType.OUTGOING)
.view(new View.Builder().allEdges(true).build())
.build())
.input(new EntitySeed("1"))
.build();

// When
OperationChain<Iterable<Walk>> getAllElements = new OperationChain.Builder()
.first(getWalks)
.option(FederatedOperationHandler.OPT_GRAPH_IDS, "createdGraph,knowsGraph")
.build();

// Then
final Iterable<Walk> res = federatedGraph.execute(getAllElements, new Context());
final ArrayList<String> walkList = new ArrayList<>();
for (final Walk walk : res) {
walkList.add(walk.getVerticesOrdered().stream().map(Object::toString).collect(Collectors.joining("")));
}
assertThat(walkList).containsExactly("143", "145");
}

@Test
void shouldGetWalksUsingDefaultGraphIds() throws OperationException {
// Given
final Graph federatedGraph = FederatedModernTestUtils.setUpSimpleFederatedGraph(FederatedStoreIT.class, new MapStoreProperties());

final GetWalks getWalks = new GetWalks.Builder()
.operations(new GetElements.Builder()
.inOutType(SeededGraphFilters.IncludeIncomingOutgoingType.OUTGOING)
.view(new View.Builder().allEdges(true).build())
.build(),
new GetElements.Builder()
.inOutType(SeededGraphFilters.IncludeIncomingOutgoingType.OUTGOING)
.view(new View.Builder().allEdges(true).build())
.build())
.input(new EntitySeed("1"))
.build();

// When
OperationChain<Iterable<Walk>> getAllElements = new OperationChain.Builder()
.first(getWalks)
.build();

// Then
final Iterable<Walk> res = federatedGraph.execute(getAllElements, new Context());
final ArrayList<String> walkList = new ArrayList<>();
for (final Walk walk : res) {
walkList.add(walk.getVerticesOrdered().stream().map(Object::toString).collect(Collectors.joining("")));
}
assertThat(walkList).containsExactly("143", "145");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*
* Copyright 2025 Crown Copyright
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package uk.gov.gchq.gaffer.federated.simple.util;

import uk.gov.gchq.gaffer.commonutil.StreamUtil;
import uk.gov.gchq.gaffer.data.element.Edge;
import uk.gov.gchq.gaffer.data.element.Element;
import uk.gov.gchq.gaffer.data.element.Entity;
import uk.gov.gchq.gaffer.federated.simple.FederatedStoreProperties;
import uk.gov.gchq.gaffer.federated.simple.operation.AddGraph;
import uk.gov.gchq.gaffer.federated.simple.operation.handler.FederatedOperationHandler;
import uk.gov.gchq.gaffer.graph.Graph;
import uk.gov.gchq.gaffer.graph.GraphConfig;
import uk.gov.gchq.gaffer.operation.OperationException;
import uk.gov.gchq.gaffer.operation.impl.add.AddElements;
import uk.gov.gchq.gaffer.store.Context;
import uk.gov.gchq.gaffer.store.StoreProperties;
import uk.gov.gchq.gaffer.store.schema.Schema;

import java.util.ArrayList;
import java.util.List;

import static uk.gov.gchq.gaffer.federated.simple.FederatedStoreProperties.PROP_DEFAULT_GRAPH_IDS;
import static uk.gov.gchq.gaffer.federated.simple.FederatedStoreProperties.PROP_DEFAULT_MERGE_ELEMENTS;

public final class FederatedModernTestUtils {
public static final String FEDERATED_GRAPH_ID = "simpleFederatedGraph";
public static final String CREATED_GRAPH_ID = "createdGraph";
public static final String KNOWS_GRAPH_ID = "knowsGraph";
private static final Context CONTEXT = new Context();

private FederatedModernTestUtils() {
// utility class
}

public static Graph setUpSimpleFederatedGraph(Class<?> clazz, StoreProperties properties)
throws OperationException {
FederatedStoreProperties fedProperties = new FederatedStoreProperties();
fedProperties.set(PROP_DEFAULT_GRAPH_IDS, CREATED_GRAPH_ID + "," + KNOWS_GRAPH_ID);
fedProperties.set(PROP_DEFAULT_MERGE_ELEMENTS, "true");
final Graph federatedGraph = new Graph.Builder()
.config(new GraphConfig.Builder()
.graphId(FEDERATED_GRAPH_ID)
.build())
.addStoreProperties(fedProperties)
.build();

federatedGraph.execute(
new AddGraph.Builder()
.graphConfig(new GraphConfig(KNOWS_GRAPH_ID))
.schema(Schema.fromJson(StreamUtil.openStreams(clazz, "/modern/schema")))
.properties(properties.getProperties())
.build(),
CONTEXT);

federatedGraph.execute(
new AddGraph.Builder()
.graphConfig(new GraphConfig(CREATED_GRAPH_ID))
.schema(Schema.fromJson(StreamUtil.openStreams(clazz, "/modern/schema")))
.properties(properties.getProperties())
.build(),
CONTEXT);

setupKnowsGraph(federatedGraph);
setupCreatedGraph(federatedGraph);

return federatedGraph;
}

private static void setupKnowsGraph(final Graph federatedGraph) throws OperationException {
List<Element> knowsGraphElements = new ArrayList<>();

knowsGraphElements.add(getEntity("1", "person", "marko"));
knowsGraphElements.add(getEntity("4", "person", "josh"));
knowsGraphElements.add(getEntity("2", "person", "vadas"));
knowsGraphElements.add(getEntity("6", "person", "peter"));
knowsGraphElements.add(getEdge("1", "4", "knows"));
knowsGraphElements.add(getEdge("1", "2", "knows"));

federatedGraph.execute(new AddElements.Builder()
.input(knowsGraphElements.toArray(new Element[0]))
.option(FederatedOperationHandler.OPT_GRAPH_IDS, KNOWS_GRAPH_ID)
.build(), CONTEXT);
}

private static void setupCreatedGraph(final Graph federatedGraph) throws OperationException {
List<Element> createdGraphElements = new ArrayList<>();

createdGraphElements.add(getEntity("3", "software", "lop"));
createdGraphElements.add(getEntity("5", "software", "ripple"));
createdGraphElements.add(getEntity("1", "person", "marko"));
createdGraphElements.add(getEntity("4", "person", "josh"));
createdGraphElements.add(getEntity("6", "person", "peter"));
createdGraphElements.add(getEdge("1", "3", "created"));
createdGraphElements.add(getEdge("4", "3", "created"));
createdGraphElements.add(getEdge("6", "3", "created"));
createdGraphElements.add(getEdge("4", "5", "created"));

federatedGraph.execute(new AddElements.Builder()
.input(createdGraphElements.toArray(new Element[0]))
.option(FederatedOperationHandler.OPT_GRAPH_IDS, CREATED_GRAPH_ID)
.build(), CONTEXT);
}

private static Entity getEntity(final String vertex, final String group, final String name) {
return new Entity.Builder()
.group(group)
.vertex(vertex)
.property("name", name)
.build();
}

private static Edge getEdge(final String source, final String dest, final String group) {
return new Edge.Builder()
.group(group)
.source(source)
.dest(dest)
.directed(true)
.build();
}
}

0 comments on commit a77f960

Please sign in to comment.