Skip to content

Commit

Permalink
Gh-3120: Improved testing of matchedVertex (#3131)
Browse files Browse the repository at this point in the history
Co-authored-by: GCHQDeveloper314 <[email protected]>
  • Loading branch information
t92549 and GCHQDeveloper314 authored Jan 2, 2024
1 parent ffd66d7 commit b2bca5e
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 77 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2023 Crown Copyright
* Copyright 2016-2024 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 Down Expand Up @@ -43,4 +43,21 @@ public static void assertElementEquals(final Iterable<? extends ElementId> expec
assertThat(resultCache).containsExactlyInAnyOrderElementsOf(expectedCache);
}
}

public static void assertElementEqualsIncludingMatchedVertex(final Iterable<? extends ElementId> expected, final Iterable<? extends ElementId> result) {
assertElementEqualsIncludingMatchedVertex(expected, result, false);
}

@SuppressWarnings("unchecked")
public static void assertElementEqualsIncludingMatchedVertex(final Iterable<? extends ElementId> expected, final Iterable<? extends ElementId> result, final boolean ignoreDuplicates) {
if (ignoreDuplicates) {
assertThat((Iterable<ElementId>) result)
.usingRecursiveFieldByFieldElementComparator()
.hasSameElementsAs((Iterable<ElementId>) expected);
} else {
assertThat((Iterable<ElementId>) result)
.usingRecursiveFieldByFieldElementComparator()
.containsExactlyInAnyOrderElementsOf((Iterable<ElementId>) expected);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2021 Crown Copyright
* Copyright 2017-2024 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 @@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package uk.gov.gchq.gaffer.store.operation.handler.function;

import com.google.common.collect.Lists;
Expand Down Expand Up @@ -707,7 +708,7 @@ public void shouldFilterBasedOnMatchedVertex() throws OperationException {
final Iterable<? extends Element> results = handler.doOperation(filter, context, store);

// Then
ElementUtil.assertElementEquals(
ElementUtil.assertElementEqualsIncludingMatchedVertex(
Arrays.asList(new Edge.Builder()
.group(TestGroups.EDGE)
.source("srcVal1")
Expand All @@ -718,7 +719,7 @@ public void shouldFilterBasedOnMatchedVertex() throws OperationException {
.group(TestGroups.EDGE)
.source("srcVal3")
.dest("destVal3")
.matchedVertex(EdgeId.MatchedVertex.SOURCE)
.matchedVertex(EdgeId.MatchedVertex.DESTINATION)
.build()),
results);
}
Expand Down Expand Up @@ -774,7 +775,7 @@ public void shouldFilterBasedOnAdjacentMatchedVertex() throws OperationException
final Iterable<? extends Element> results = handler.doOperation(filter, context, store);

// Then
ElementUtil.assertElementEquals(
ElementUtil.assertElementEqualsIncludingMatchedVertex(
Arrays.asList(new Edge.Builder()
.group(TestGroups.EDGE)
.source("srcVal1")
Expand All @@ -785,7 +786,7 @@ public void shouldFilterBasedOnAdjacentMatchedVertex() throws OperationException
.group(TestGroups.EDGE)
.source("srcVal3")
.dest("destVal3")
.matchedVertex(EdgeId.MatchedVertex.SOURCE)
.matchedVertex(EdgeId.MatchedVertex.DESTINATION)
.build()),
results);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2021 Crown Copyright
* Copyright 2017-2024 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 @@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package uk.gov.gchq.gaffer.store.operation.handler.function;

import com.google.common.collect.Lists;
Expand Down Expand Up @@ -470,7 +471,7 @@ public void shouldSelectMatchedVertexForTransform() throws OperationException {
.matchedVertex(EdgeId.MatchedVertex.SOURCE)
.property(TestPropertyNames.PROP_3, "srcVal")
.build();
ElementUtil.assertElementEquals(Collections.singletonList(expectedEdge), results);
ElementUtil.assertElementEqualsIncludingMatchedVertex(Collections.singletonList(expectedEdge), results);
}

@Test
Expand Down Expand Up @@ -505,6 +506,6 @@ public void shouldSelectAdjacentMatchedVertexForTransform() throws OperationExce
.matchedVertex(EdgeId.MatchedVertex.SOURCE)
.property(TestPropertyNames.PROP_3, "destVal")
.build();
ElementUtil.assertElementEquals(Collections.singletonList(expectedEdge), results);
ElementUtil.assertElementEqualsIncludingMatchedVertex(Collections.singletonList(expectedEdge), results);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2021 Crown Copyright
* Copyright 2017-2024 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 Down Expand Up @@ -55,6 +55,8 @@
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static uk.gov.gchq.gaffer.data.util.ElementUtil.assertElementEquals;
import static uk.gov.gchq.gaffer.data.util.ElementUtil.assertElementEqualsIncludingMatchedVertex;


public class AggregatorUtilTest {
@Test
Expand Down Expand Up @@ -1007,7 +1009,7 @@ public void shouldQueryAggregateDirectedEdgesIncludingMatchedVertexGroupBy() {
final Iterable<Element> aggregatedElementsIncludingMatchedVertex = AggregatorUtil.queryAggregate(elements, schema, view, true);

// then
assertElementEquals(expectedIncludingMatchedVertex, aggregatedElementsIncludingMatchedVertex);
assertElementEqualsIncludingMatchedVertex(expectedIncludingMatchedVertex, aggregatedElementsIncludingMatchedVertex);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2023 Crown Copyright
* Copyright 2016-2024 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 Down Expand Up @@ -88,7 +88,7 @@ public void shouldConvertFromDomainObjects() throws OperationException {
.first(new GenerateElements.Builder<DomainObject>()
.generator(new BasicElementGenerator())
.input(new EntityDomainObject(NEW_VERTEX, "1", null),
new EdgeDomainObject(NEW_SOURCE, NEW_DEST, false, 1, 1L))
new EdgeDomainObject(NEW_SOURCE, NEW_DEST, true, 1, 1L))
.build())
.then(new AddElements())
.build();
Expand All @@ -98,22 +98,22 @@ public void shouldConvertFromDomainObjects() throws OperationException {

// Then - check they were added correctly
final List<Element> results = Lists.newArrayList(graph.execute(new GetElements.Builder()
.input(new EntitySeed(NEW_VERTEX), new EdgeSeed(NEW_SOURCE, NEW_DEST, false))
.input(new EntitySeed(NEW_VERTEX), new EdgeSeed(NEW_SOURCE, NEW_DEST, true))
.build(), getUser()));

final Edge expectedEdge = new Edge.Builder()
.group(TestGroups.EDGE)
.source(NEW_SOURCE)
.dest(NEW_DEST)
.directed(false)
.matchedVertex(EdgeId.MatchedVertex.DESTINATION)
.directed(true)
.matchedVertex(EdgeId.MatchedVertex.SOURCE)
.build();
expectedEdge.putProperty(TestPropertyNames.INT, 1);
expectedEdge.putProperty(TestPropertyNames.COUNT, 1L);

final Entity expectedEntity = new Entity(TestGroups.ENTITY, NEW_VERTEX);
expectedEntity.putProperty(TestPropertyNames.SET, CollectionUtil.treeSet("1"));

ElementUtil.assertElementEquals(Arrays.asList(expectedEntity, expectedEdge), results);
ElementUtil.assertElementEqualsIncludingMatchedVertex(Arrays.asList(expectedEntity, expectedEdge), results);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2023 Crown Copyright
* Copyright 2016-2024 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 Down Expand Up @@ -193,7 +193,7 @@ public void shouldGetAllEdgesWhenFlagSet() throws Exception {
final Iterable<? extends Element> resultsIncludingAllEdges = graph.execute(opIncludingAllEdges, user);

// Then
ElementUtil.assertElementEquals(Arrays.asList(
ElementUtil.assertElementEqualsIncludingMatchedVertex(Arrays.asList(
new Edge.Builder()
.group(TestGroups.EDGE)
.source(SOURCE_1)
Expand Down Expand Up @@ -238,7 +238,7 @@ public void shouldGetAllEntitiesWhenFlagSet() throws OperationException {
final Iterable<? extends Element> resultsExcludingAllEntities = graph.execute(opExcludingAllEntities, user);

// Then
ElementUtil.assertElementEquals(Arrays.asList(
ElementUtil.assertElementEqualsIncludingMatchedVertex(Arrays.asList(
new Edge.Builder()
.group(TestGroups.EDGE)
.source(SOURCE_1)
Expand Down Expand Up @@ -297,7 +297,7 @@ public void shouldGetElementsWithMatchedVertex() throws Exception {
final Iterable<? extends Element> results = graph.execute(op, user);

// Then
ElementUtil.assertElementEquals(Arrays.asList(
ElementUtil.assertElementEqualsIncludingMatchedVertex(Arrays.asList(
new Edge.Builder()
.group(TestGroups.EDGE)
.source(SOURCE_DIR_1)
Expand Down Expand Up @@ -350,7 +350,7 @@ public void shouldGetElementsWithMatchedVertexFilter() throws Exception {
final Iterable<? extends Element> results = graph.execute(op, user);

// Then
ElementUtil.assertElementEquals(Arrays.asList(
ElementUtil.assertElementEqualsIncludingMatchedVertex(Arrays.asList(
new Edge.Builder()
.group(TestGroups.EDGE)
.source(SOURCE_DIR_1)
Expand Down Expand Up @@ -652,8 +652,13 @@ private void shouldGetElements(final Collection<Element> expectedElements,
final Iterable<? extends Element> resultsElement = graph.execute(opElement, user);

// Then
ElementUtil.assertElementEquals(expectedElements, resultsSeed, true);
ElementUtil.assertElementEquals(expectedElements, resultsElement, true);
if (includeEdges && inOutType == IncludeIncomingOutgoingType.INCOMING) {
ElementUtil.assertElementEquals(expectedElements, resultsSeed, true);
ElementUtil.assertElementEquals(expectedElements, resultsElement, true);
} else {
ElementUtil.assertElementEqualsIncludingMatchedVertex(expectedElements, resultsSeed, true);
ElementUtil.assertElementEqualsIncludingMatchedVertex(expectedElements, resultsElement, true);
}
}

private static Collection<Element> getElements(final Collection<ElementId> seeds, final Boolean direction) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2018-2023 Crown Copyright
* Copyright 2018-2024 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 Down Expand Up @@ -517,7 +517,7 @@ public void shouldCorrectlyApplyPostAggFiltering() throws OperationException {
new User());

// Then
ElementUtil.assertElementEquals(
ElementUtil.assertElementEqualsIncludingMatchedVertex(
Arrays.asList(
EDGE_OLD_AGGREGATION_ALT_COUNT_MIGRATED_TO_NEW
),
Expand Down Expand Up @@ -557,7 +557,7 @@ public void shouldApplyPostOpAggregation() throws OperationException {
new User());

// Then
ElementUtil.assertElementEquals(
ElementUtil.assertElementEqualsIncludingMatchedVertex(
Arrays.asList(
EDGE_NEW_POST_OP_AGGREGATION_AGGREGATED
),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2020 Crown Copyright
* Copyright 2016-2024 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 @@ -18,7 +18,7 @@

import uk.gov.gchq.gaffer.accumulostore.AccumuloStore;
import uk.gov.gchq.gaffer.accumulostore.key.exception.IteratorSettingException;
import uk.gov.gchq.gaffer.data.element.id.EdgeId;
import uk.gov.gchq.gaffer.data.element.id.EntityId;
import uk.gov.gchq.gaffer.operation.impl.get.GetElements;
import uk.gov.gchq.gaffer.store.StoreException;
import uk.gov.gchq.gaffer.user.User;
Expand All @@ -31,8 +31,8 @@ public AccumuloElementsRetriever(final AccumuloStore store,
final User user)
throws IteratorSettingException, StoreException {
super(store, operation, user,
// includeMatchedVertex if input only contains EntityIds
StreamSupport.stream(operation.getInput().spliterator(), false).noneMatch(input -> EdgeId.class.isInstance(input)),
// includeMatchedVertex if any input are EntityIds
StreamSupport.stream(operation.getInput().spliterator(), false).anyMatch(EntityId.class::isInstance),
store.getKeyPackage().getIteratorFactory().getElementPreAggregationFilterIteratorSetting(operation.getView(), store),
store.getKeyPackage().getIteratorFactory().getElementPostAggregationFilterIteratorSetting(operation.getView(), store),
store.getKeyPackage().getIteratorFactory().getEdgeEntityDirectionFilterIteratorSetting(operation),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2021 Crown Copyright
* Copyright 2016-2024 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 @@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package uk.gov.gchq.gaffer.accumulostore.key;

import org.apache.accumulo.core.data.Key;
Expand Down Expand Up @@ -479,7 +480,7 @@ public void shouldThrowExceptionWhenGetPropertiesFromTimestampWhenGroupIsNotFoun

@Test
public void shouldSerialiseAndDeserialisePropertiesWhenAllAreEmpty() {
// Given
// Given
final Schema schema = new Schema.Builder()
.entity(TestGroups.ENTITY, new SchemaEntityDefinition.Builder()
.vertex("string")
Expand All @@ -503,7 +504,7 @@ public void shouldSerialiseAndDeserialisePropertiesWhenAllAreEmpty() {
.property(TestPropertyNames.PROP_2, new FreqMap())
.build();

// When 1
// When 1
final Value value = converter.getValueFromProperties(TestGroups.ENTITY, entity.getProperties());

// Then 1
Expand All @@ -519,7 +520,7 @@ public void shouldSerialiseAndDeserialisePropertiesWhenAllAreEmpty() {

@Test
public void shouldDeserialiseEntityId() {
// Given
// Given
final EntityId expectedElementId = new EntitySeed("vertex1");
final Entity entity = new Entity.Builder()
.vertex("vertex1")
Expand All @@ -538,7 +539,7 @@ public void shouldDeserialiseEntityId() {

@Test
public void shouldDeserialiseEdgeId() {
// Given
// Given
final EdgeId expectedElementId = new EdgeSeed("source1", "dest1", true);
final Edge edge = new Edge.Builder()
.source("source1")
Expand All @@ -559,7 +560,7 @@ public void shouldDeserialiseEdgeId() {

@Test
public void shouldDeserialiseEdgeIdWithQueriedDestVertex() {
// Given
// Given
final EdgeId expectedElementId = new EdgeSeed("vertex1", "vertex2", true, EdgeId.MatchedVertex.DESTINATION);
final Edge edge = new Edge.Builder()
.source("vertex1")
Expand All @@ -581,7 +582,7 @@ public void shouldDeserialiseEdgeIdWithQueriedDestVertex() {

@Test
public void shouldDeserialiseEdgeIdWithQueriedSourceVertex() {
// Given
// Given
final EdgeId expectedElementId = new EdgeSeed("source1", "dest1", true);
final Edge edge = new Edge.Builder()
.source("source1")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2020 Crown Copyright
* Copyright 2017-2024 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 @@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package uk.gov.gchq.gaffer.accumulostore.key.core;

import com.google.common.primitives.Bytes;
Expand Down Expand Up @@ -69,7 +70,7 @@ public void shouldReturnOverriddenSerialiseNull() throws Exception {

@Test
public void shouldDeserialiseSourceDestinationValuesCorrectWayRound() {
// Given
// Given
final Edge edge = new Edge.Builder()
.source("1")
.dest("2")
Expand Down Expand Up @@ -109,7 +110,7 @@ public void shouldDeserialiseSourceDestinationValuesIncorrectWayRound() {

@Test
public void shouldDeserialiseSourceDestinationValuesUndirected() {
// Given
// Given
final Edge edge = new Edge.Builder()
.source("1")
.dest("2")
Expand Down
Loading

0 comments on commit b2bca5e

Please sign in to comment.