Search in sources :

Example 1 with CallMethod

use of uk.gov.gchq.koryphe.impl.function.CallMethod in project Gaffer by gchq.

the class FederatedAddGraphHandlerTest method shouldAddGraphWithCustomReadAccessPredicate.

@Test
public void shouldAddGraphWithCustomReadAccessPredicate() throws Exception {
    store.initialise(FEDERATEDSTORE_GRAPH_ID, null, federatedStoreProperties);
    final Schema expectedSchema = new Schema.Builder().build();
    assertEquals(0, store.getGraphs(testUser, null, ignore).size());
    final AccessPredicate allowBlankUserAndTestUserReadAccess = new AccessPredicate(new AdaptedPredicate(new CallMethod("getUserId"), new Or<>(new IsEqual(testUser.getUserId()), new IsEqual(blankUser.getUserId()))));
    new FederatedAddGraphHandler().doOperation(new AddGraph.Builder().graphId(EXPECTED_GRAPH_ID).schema(expectedSchema).storeProperties(PROPERTIES).readAccessPredicate(allowBlankUserAndTestUserReadAccess).build(), new Context(testUser), store);
    assertEquals(1, store.getGraphs(blankUser, null, ignore).size());
    assertEquals(1, store.getGraphs(testUser, null, ignore).size());
}
Also used : Context(uk.gov.gchq.gaffer.store.Context) Or(uk.gov.gchq.koryphe.impl.predicate.Or) Schema(uk.gov.gchq.gaffer.store.schema.Schema) AdaptedPredicate(uk.gov.gchq.koryphe.predicate.AdaptedPredicate) AccessPredicate(uk.gov.gchq.gaffer.access.predicate.AccessPredicate) CallMethod(uk.gov.gchq.koryphe.impl.function.CallMethod) IsEqual(uk.gov.gchq.koryphe.impl.predicate.IsEqual) Test(org.junit.jupiter.api.Test)

Example 2 with CallMethod

use of uk.gov.gchq.koryphe.impl.function.CallMethod in project Gaffer by gchq.

the class KeyFunctionMatchTest method shouldThrowExceptionFromFunctionIfInputIsInvalid.

@Test
public void shouldThrowExceptionFromFunctionIfInputIsInvalid() {
    // given
    // Performing a FirstItem on null should throw IllegalArgumentException
    List<Long> testList = Lists.newArrayList(100L, 200L, 300L, null);
    // when
    KeyFunctionMatch match = new KeyFunctionMatch.Builder().firstKeyFunction(new FunctionComposite(Lists.newArrayList(new CallMethod("getValue"), new ToInteger()))).secondKeyFunction(new FunctionComposite(Lists.newArrayList(new ToInteger(), new DivideBy(10), new FirstItem<>()))).build();
    // then
    try {
        match.init(testList);
    } catch (final IllegalArgumentException e) {
        // copied from docs of FirstItem
        assertEquals("Input cannot be null", e.getMessage());
    }
}
Also used : DivideBy(uk.gov.gchq.koryphe.impl.function.DivideBy) ToInteger(uk.gov.gchq.koryphe.impl.function.ToInteger) ToLong(uk.gov.gchq.koryphe.impl.function.ToLong) FirstItem(uk.gov.gchq.koryphe.impl.function.FirstItem) FunctionComposite(uk.gov.gchq.koryphe.function.FunctionComposite) CallMethod(uk.gov.gchq.koryphe.impl.function.CallMethod) Test(org.junit.jupiter.api.Test)

Example 3 with CallMethod

use of uk.gov.gchq.koryphe.impl.function.CallMethod in project Gaffer by gchq.

the class NamedViewDetailTest method shouldTestAccessUsingCustomAccessPredicatesWhenConfigured.

@Test
public void shouldTestAccessUsingCustomAccessPredicatesWhenConfigured() {
    // Given
    final User testUser = new User.Builder().userId("testUserId").build();
    final User differentUser = new User.Builder().userId("differentUserId").opAuth("different").build();
    final String adminAuth = "adminAuth";
    final AccessPredicate readAccessPredicate = new AccessPredicate(new AdaptedPredicate(new CallMethod("getUserId"), new IsEqual("testUserId")));
    final AccessPredicate writeAccessPredicate = new AccessPredicate(new AdaptedPredicate(new CallMethod("getOpAuths"), new CollectionContains("different")));
    // When
    final NamedViewDetail namedViewDetail = createNamedViewDetailBuilder().readAccessPredicate(readAccessPredicate).writeAccessPredicate(writeAccessPredicate).build();
    // Then
    assertTrue(namedViewDetail.hasReadAccess(testUser, adminAuth));
    assertFalse(namedViewDetail.hasReadAccess(differentUser, adminAuth));
    assertFalse(namedViewDetail.hasWriteAccess(testUser, adminAuth));
    assertTrue(namedViewDetail.hasWriteAccess(differentUser, adminAuth));
}
Also used : CollectionContains(uk.gov.gchq.koryphe.impl.predicate.CollectionContains) User(uk.gov.gchq.gaffer.user.User) AdaptedPredicate(uk.gov.gchq.koryphe.predicate.AdaptedPredicate) NamedViewWriteAccessPredicate(uk.gov.gchq.gaffer.data.elementdefinition.view.access.predicate.NamedViewWriteAccessPredicate) AccessPredicate(uk.gov.gchq.gaffer.access.predicate.AccessPredicate) UnrestrictedAccessPredicate(uk.gov.gchq.gaffer.access.predicate.UnrestrictedAccessPredicate) CallMethod(uk.gov.gchq.koryphe.impl.function.CallMethod) IsEqual(uk.gov.gchq.koryphe.impl.predicate.IsEqual) Test(org.junit.jupiter.api.Test)

Example 4 with CallMethod

use of uk.gov.gchq.koryphe.impl.function.CallMethod in project Gaffer by gchq.

the class RoadTrafficCsvElementGenerator2Test method shouldParseSampleDataWithGenericFunctions.

@Test
public void shouldParseSampleDataWithGenericFunctions() throws IOException {
    // Given
    CsvLinesToMaps parseCsv = new CsvLinesToMaps().header("Region Name (GO)", "ONS LACode", "ONS LA Name", "CP", "S Ref E", "S Ref N", "Road", "A-Junction", "A Ref E", "A Ref N", "B-Junction", "B Ref E", "B Ref N", "RCat", "iDir", "Year", "dCount", "Hour", "PC", "2WMV", "CAR", "BUS", "LGV", "HGVR2", "HGVR3", "HGVR4", "HGVA3", "HGVA5", "HGVA6", "HGV", "AMV").firstRow(1);
    IterableFunction<Map<String, Object>, Tuple<String>> toTuples = new IterableFunction<>(new MapToTuple<String>());
    IterableFunction<Tuple<String>, Tuple<String>> transformTuples = new IterableFunction(new FunctionChain.Builder<>().execute(new String[] { "Road", "A-Junction" }, new Concat(":"), new String[] { "A-Junction" }).execute(new String[] { "Road", "B-Junction" }, new Concat(":"), new String[] { "B-Junction" }).execute(new String[] { "A Ref E", "A Ref N" }, new Concat(), new String[] { "A-Location" }).execute(new String[] { "B Ref E", "B Ref N" }, new Concat(), new String[] { "B-Location" }).execute(new String[] { "THIS" }, new CreateRoadTrafficFreqMap(), new String[] { "countByVehicleType" }).execute(new String[] { "countByVehicleType" }, new CallMethod("getTotal"), new String[] { "total-count" }).execute(new String[] { "dCount" }, new ParseTime().timeZone("UTC"), new String[] { "timestamp" }).execute(new String[] { "Hour" }, new FunctionChain(new ToInteger(), new MultiplyBy(60 * 60 * 1000), new ToLong()), new String[] { "hoursInMilliseconds" }).execute(new String[] { "timestamp", "hoursInMilliseconds" }, new FunctionChain(new ApplyBiFunction<>(new Sum()), new ToString(), new ParseDate()), new String[] { "startDate" }).execute(new String[] { "startDate" }, new DateToTimeBucketEnd(TimeBucket.HOUR), new String[] { "endDate" }).build());
    TuplesToElements toElements = new TuplesToElements().element(new ElementTupleDefinition("RegionContainsLocation").source("Region Name (GO)").destination("ONS LA Name")).element(new ElementTupleDefinition("LocationContainsRoad").source("ONS LA Name").destination("Road")).element(new ElementTupleDefinition("RoadHasJunction").source("Road").destination("A-Junction")).element(new ElementTupleDefinition("RoadHasJunction").source("Road").destination("B-Junction")).element(new ElementTupleDefinition("JunctionLocatedAt").source("A-Junction").destination("A-Location")).element(new ElementTupleDefinition("JunctionLocatedAt").source("B-Junction").destination("B-Location")).element(new ElementTupleDefinition("RoadUse").source("A-Junction").destination("B-Junction").property("startDate").property("endDate").property("countByVehicleType").property("count", "total-count")).element(new ElementTupleDefinition("JunctionUse").vertex("A-Junction").property("startDate").property("endDate").property("countByVehicleType").property("count", "total-count")).element(new ElementTupleDefinition("JunctionUse").vertex("B-Junction").property("startDate").property("endDate").property("countByVehicleType").property("count", "total-count"));
    HyperLogLogPlusEntityGenerator addCardinalities = new HyperLogLogPlusEntityGenerator().countProperty("count").edgeGroupProperty("edgeGroup").cardinalityPropertyName("hllp");
    // Apply functions
    final FunctionChain<List<String>, Iterable<Element>> generator2 = new FunctionChain.Builder<List<String>, Iterable<Element>>().execute(parseCsv).execute(toTuples).execute(transformTuples).execute(toElements).execute(addCardinalities).build();
    // Uncomment the following for debugging
    // System.out.println(new String(JSONSerialiser.serialise(generator2, true)));
    final List<String> lines = IOUtils.readLines(createInputStream());
    final List<Element> elements2 = Lists.newArrayList(generator2.apply(lines));
    // Then - the results should be the same as those generated using the original element generator
    final RoadTrafficStringElementGenerator generator1 = new RoadTrafficStringElementGenerator();
    final List<Element> elements1;
    try (final InputStream inputStream = createInputStream()) {
        elements1 = Lists.newArrayList(generator1.apply(() -> new LineIterator(new InputStreamReader(inputStream))));
    }
    JSONSerialiser.getMapper();
    SimpleClassNameCache.setUseFullNameForSerialisation(false);
    elements1.forEach(e -> e.removeProperty("hllp"));
    elements2.forEach(e -> e.removeProperty("hllp"));
    ElementUtil.assertElementEquals(elements1, elements2);
}
Also used : MultiplyBy(uk.gov.gchq.koryphe.impl.function.MultiplyBy) DateToTimeBucketEnd(uk.gov.gchq.gaffer.time.function.DateToTimeBucketEnd) Element(uk.gov.gchq.gaffer.data.element.Element) ParseDate(uk.gov.gchq.koryphe.impl.function.ParseDate) ToString(uk.gov.gchq.koryphe.impl.function.ToString) LineIterator(org.apache.commons.io.LineIterator) CallMethod(uk.gov.gchq.koryphe.impl.function.CallMethod) Concat(uk.gov.gchq.koryphe.impl.function.Concat) IterableFunction(uk.gov.gchq.koryphe.impl.function.IterableFunction) CsvLinesToMaps(uk.gov.gchq.koryphe.impl.function.CsvLinesToMaps) List(java.util.List) TuplesToElements(uk.gov.gchq.gaffer.data.element.function.TuplesToElements) FunctionChain(uk.gov.gchq.koryphe.impl.function.FunctionChain) ToLong(uk.gov.gchq.koryphe.impl.function.ToLong) HyperLogLogPlusEntityGenerator(uk.gov.gchq.gaffer.sketches.clearspring.cardinality.HyperLogLogPlusEntityGenerator) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) ToInteger(uk.gov.gchq.koryphe.impl.function.ToInteger) Sum(uk.gov.gchq.koryphe.impl.binaryoperator.Sum) ElementTupleDefinition(uk.gov.gchq.gaffer.data.element.function.ElementTupleDefinition) ParseTime(uk.gov.gchq.koryphe.impl.function.ParseTime) ToString(uk.gov.gchq.koryphe.impl.function.ToString) FreqMap(uk.gov.gchq.gaffer.types.FreqMap) Map(java.util.Map) MapToTuple(uk.gov.gchq.koryphe.impl.function.MapToTuple) Tuple(uk.gov.gchq.koryphe.tuple.Tuple) Test(org.junit.Test)

Example 5 with CallMethod

use of uk.gov.gchq.koryphe.impl.function.CallMethod in project Gaffer by gchq.

the class KeyFunctionMatchTest method shouldMatchObjectsBasedOnKeyFunctions.

@Test
public void shouldMatchObjectsBasedOnKeyFunctions() {
    // given
    TypeSubTypeValue testValue = new TypeSubTypeValue("myType", "mySubType", "30");
    List<Long> testList = Lists.newArrayList(100L, 200L, 300L, 400L);
    // when
    KeyFunctionMatch match = new KeyFunctionMatch.Builder().firstKeyFunction(new FunctionComposite(Lists.newArrayList(new CallMethod("getValue"), new ToInteger()))).secondKeyFunction(new FunctionComposite(Lists.newArrayList(new ToInteger(), new DivideBy(10), new FirstItem<>()))).build();
    match.init(testList);
    // then
    List<Long> expected = Lists.newArrayList(300L);
    assertEquals(expected, match.matching(testValue));
}
Also used : DivideBy(uk.gov.gchq.koryphe.impl.function.DivideBy) TypeSubTypeValue(uk.gov.gchq.gaffer.types.TypeSubTypeValue) ToInteger(uk.gov.gchq.koryphe.impl.function.ToInteger) ToLong(uk.gov.gchq.koryphe.impl.function.ToLong) FirstItem(uk.gov.gchq.koryphe.impl.function.FirstItem) FunctionComposite(uk.gov.gchq.koryphe.function.FunctionComposite) CallMethod(uk.gov.gchq.koryphe.impl.function.CallMethod) Test(org.junit.jupiter.api.Test)

Aggregations

CallMethod (uk.gov.gchq.koryphe.impl.function.CallMethod)7 Test (org.junit.jupiter.api.Test)6 AccessPredicate (uk.gov.gchq.gaffer.access.predicate.AccessPredicate)4 AdaptedPredicate (uk.gov.gchq.koryphe.predicate.AdaptedPredicate)4 ToInteger (uk.gov.gchq.koryphe.impl.function.ToInteger)3 ToLong (uk.gov.gchq.koryphe.impl.function.ToLong)3 IsEqual (uk.gov.gchq.koryphe.impl.predicate.IsEqual)3 Context (uk.gov.gchq.gaffer.store.Context)2 Schema (uk.gov.gchq.gaffer.store.schema.Schema)2 FunctionComposite (uk.gov.gchq.koryphe.function.FunctionComposite)2 DivideBy (uk.gov.gchq.koryphe.impl.function.DivideBy)2 FirstItem (uk.gov.gchq.koryphe.impl.function.FirstItem)2 CollectionContains (uk.gov.gchq.koryphe.impl.predicate.CollectionContains)2 Or (uk.gov.gchq.koryphe.impl.predicate.Or)2 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 List (java.util.List)1 Map (java.util.Map)1 LineIterator (org.apache.commons.io.LineIterator)1 Test (org.junit.Test)1