Search in sources :

Example 11 with NamedViewDetail

use of uk.gov.gchq.gaffer.data.elementdefinition.view.NamedViewDetail in project Gaffer by gchq.

the class NamedViewResolverTest method shouldResolveMultipleNestedNamedViews.

@Test
public void shouldResolveMultipleNestedNamedViews() throws CacheOperationFailedException {
    // Given
    final NamedView nestedNamedView = new NamedView.Builder().name(NESTED_NAMED_VIEW_NAME).entity(TestGroups.ENTITY_2).build();
    final NamedViewDetail nestedNamedViewDetail = new NamedViewDetail.Builder().name(nestedNamedView.getName()).view(nestedNamedView).build();
    final NamedView nestedNamedView1 = new NamedView.Builder().name(NESTED_NAMED_VIEW_NAME + 1).edge(TestGroups.EDGE).merge(nestedNamedView).build();
    final NamedViewDetail nestedNamedView1Detail = new NamedViewDetail.Builder().name(nestedNamedView1.getName()).view(nestedNamedView1).build();
    final NamedView nestedNamedView2 = new NamedView.Builder().name(NESTED_NAMED_VIEW_NAME + 2).edge(TestGroups.EDGE_2).merge(nestedNamedView1).build();
    final NamedViewDetail nestedNamedView2Detail = new NamedViewDetail.Builder().name(nestedNamedView2.getName()).view(nestedNamedView2).build();
    final NamedView nestedNamedView3 = new NamedView.Builder().name(NESTED_NAMED_VIEW_NAME + 3).edge(TestGroups.EDGE_3).merge(nestedNamedView2).build();
    final NamedViewDetail nestedNamedView3Detail = new NamedViewDetail.Builder().name(nestedNamedView3.getName()).view(nestedNamedView3).build();
    assertEquals(3, nestedNamedView3.getMergedNamedViewNames().size());
    assertTrue(nestedNamedView3.getMergedNamedViewNames().contains(NESTED_NAMED_VIEW_NAME));
    assertTrue(nestedNamedView3.getMergedNamedViewNames().contains(NESTED_NAMED_VIEW_NAME + 1));
    assertTrue(nestedNamedView3.getMergedNamedViewNames().contains(NESTED_NAMED_VIEW_NAME + 2));
    given(CACHE.getNamedView(NESTED_NAMED_VIEW_NAME, CONTEXT.getUser())).willReturn(nestedNamedViewDetail);
    given(CACHE.getNamedView(NESTED_NAMED_VIEW_NAME + 1, CONTEXT.getUser())).willReturn(nestedNamedView1Detail);
    given(CACHE.getNamedView(NESTED_NAMED_VIEW_NAME + 2, CONTEXT.getUser())).willReturn(nestedNamedView2Detail);
    given(CACHE.getNamedView(NESTED_NAMED_VIEW_NAME + 3, CONTEXT.getUser())).willReturn(nestedNamedView3Detail);
    final OperationChain<?> opChain = new OperationChain.Builder().first(new GetElements.Builder().view(new NamedView.Builder().name(nestedNamedView3.getName()).build()).build()).build();
    // When
    RESOLVER.preExecute(opChain, CONTEXT);
    GetElements getElements = (GetElements) opChain.getOperations().get(0);
    nestedNamedView3.setName(null);
    final View mergedView = new View.Builder().merge(nestedNamedView3).build();
    // Then
    JsonAssert.assertEquals(mergedView.toCompactJson(), getElements.getView().toCompactJson());
}
Also used : NamedView(uk.gov.gchq.gaffer.data.elementdefinition.view.NamedView) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) NamedViewDetail(uk.gov.gchq.gaffer.data.elementdefinition.view.NamedViewDetail) OperationView(uk.gov.gchq.gaffer.operation.graph.OperationView) NamedView(uk.gov.gchq.gaffer.data.elementdefinition.view.NamedView) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) Test(org.junit.jupiter.api.Test)

Example 12 with NamedViewDetail

use of uk.gov.gchq.gaffer.data.elementdefinition.view.NamedViewDetail in project Gaffer by gchq.

the class AddNamedViewHandlerTest method shouldAddNamedViewCorrectly.

@Test
public void shouldAddNamedViewCorrectly() throws OperationException, CacheOperationFailedException {
    handler.doOperation(addNamedView, context, store);
    final NamedViewDetail result = namedViewCache.getNamedView(testNamedViewName);
    assertTrue(cacheContains(testNamedViewName));
    assertEquals(addNamedView.getName(), result.getName());
    assertEquals(new String(addNamedView.getView().toCompactJson()), result.getView());
    assertEquals(context.getUser().getUserId(), result.getCreatorId());
    assertEquals(new UnrestrictedAccessPredicate(), result.getOrDefaultReadAccessPredicate());
    final AccessPredicate expectedWriteAccessPredicate = new NamedViewWriteAccessPredicate(context.getUser(), Arrays.asList(writeAccessRoles));
    assertEquals(expectedWriteAccessPredicate, result.getOrDefaultWriteAccessPredicate());
}
Also used : UnrestrictedAccessPredicate(uk.gov.gchq.gaffer.access.predicate.UnrestrictedAccessPredicate) 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) NamedViewDetail(uk.gov.gchq.gaffer.data.elementdefinition.view.NamedViewDetail) NamedViewWriteAccessPredicate(uk.gov.gchq.gaffer.data.elementdefinition.view.access.predicate.NamedViewWriteAccessPredicate) Test(org.junit.jupiter.api.Test)

Example 13 with NamedViewDetail

use of uk.gov.gchq.gaffer.data.elementdefinition.view.NamedViewDetail in project Gaffer by gchq.

the class AddNamedViewHandlerTest method shouldAddNamedViewContainingCustomAccessPredicatesCorrectly.

@Test
public void shouldAddNamedViewContainingCustomAccessPredicatesCorrectly() throws OperationException, CacheOperationFailedException {
    final AccessPredicate readAccessPredicate = new AccessPredicate(new CustomUserPredicate());
    final AccessPredicate writeAccessPredicate = new AccessPredicate(new CustomUserPredicate());
    addNamedView.setReadAccessPredicate(readAccessPredicate);
    addNamedView.setWriteAccessRoles(null);
    addNamedView.setWriteAccessPredicate(writeAccessPredicate);
    handler.doOperation(addNamedView, context, store);
    final NamedViewDetail result = namedViewCache.getNamedView(testNamedViewName);
    assertTrue(cacheContains(testNamedViewName));
    assertEquals(addNamedView.getName(), result.getName());
    assertEquals(new String(addNamedView.getView().toCompactJson()), result.getView());
    assertEquals(context.getUser().getUserId(), result.getCreatorId());
    assertEquals(readAccessPredicate, result.getOrDefaultReadAccessPredicate());
    assertEquals(writeAccessPredicate, result.getOrDefaultWriteAccessPredicate());
}
Also used : CustomUserPredicate(uk.gov.gchq.gaffer.access.predicate.user.CustomUserPredicate) 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) NamedViewDetail(uk.gov.gchq.gaffer.data.elementdefinition.view.NamedViewDetail) Test(org.junit.jupiter.api.Test)

Example 14 with NamedViewDetail

use of uk.gov.gchq.gaffer.data.elementdefinition.view.NamedViewDetail in project Gaffer by gchq.

the class NamedViewResolver method resolveView.

private View resolveView(final String namedViewName, final Map<String, Object> parameters, final Context context) {
    final NamedViewDetail cachedNamedView;
    try {
        cachedNamedView = cache.getNamedView(namedViewName, context.getUser());
    } catch (final CacheOperationFailedException e) {
        throw new RuntimeException(e);
    }
    View resolvedView;
    if (null == cachedNamedView) {
        resolvedView = new View();
    } else {
        resolvedView = cachedNamedView.getView(parameters);
        if (resolvedView instanceof NamedView) {
            ((NamedView) resolvedView).setName(null);
            if (CollectionUtils.isNotEmpty(((NamedView) resolvedView).getMergedNamedViewNames())) {
                final View.Builder viewBuilder = new View.Builder();
                viewBuilder.merge(resolvedView);
                for (final String name : ((NamedView) resolvedView).getMergedNamedViewNames()) {
                    viewBuilder.merge(resolveView(name, parameters, context));
                }
                resolvedView = viewBuilder.build();
            }
        }
    }
    return resolvedView;
}
Also used : NamedView(uk.gov.gchq.gaffer.data.elementdefinition.view.NamedView) CacheOperationFailedException(uk.gov.gchq.gaffer.named.operation.cache.exception.CacheOperationFailedException) NamedViewDetail(uk.gov.gchq.gaffer.data.elementdefinition.view.NamedViewDetail) NamedView(uk.gov.gchq.gaffer.data.elementdefinition.view.NamedView) OperationView(uk.gov.gchq.gaffer.operation.graph.OperationView) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View)

Example 15 with NamedViewDetail

use of uk.gov.gchq.gaffer.data.elementdefinition.view.NamedViewDetail in project gaffer-doc by gchq.

the class NamedViews method run.

@Override
public CloseableIterable<? extends Element> run() throws OperationException, IOException {
    // / [graph] create a graph using our schema and store properties
    // ---------------------------------------------------------
    final Graph graph = new Graph.Builder().config(getDefaultGraphConfig()).addSchemas(StreamUtil.openStreams(getClass(), schemaPath)).storeProperties(getDefaultStoreProperties()).build();
    // ---------------------------------------------------------
    // [user] Create a user
    // ---------------------------------------------------------
    final User user = new User("user01");
    // ---------------------------------------------------------
    // [add] Create a data generator and add the edges to the graph using an operation chain consisting of:
    // generateElements - generating edges from the data (note these are directed edges)
    // addElements - add the edges to the graph
    // ---------------------------------------------------------
    final OperationChain<Void> addOpChain = new OperationChain.Builder().first(new GenerateElements.Builder<String>().generator(new RoadAndRoadUseWithTimesAndCardinalitiesElementGenerator()).input(IOUtils.readLines(StreamUtil.openStream(getClass(), "RoadAndRoadUseWithTimesAndCardinalities/data.txt"))).build()).then(new AddElements()).build();
    graph.execute(addOpChain, user);
    // ---------------------------------------------------------
    // [add named view] add a NamedView to be reused
    // ---------------------------------------------------------
    final AddNamedView addNamedView = new AddNamedView.Builder().name("RoadUse edges").description("NamedView to get only RoadUse edges").view(new View.Builder().edge("RoadUse").build()).overwrite(false).build();
    // ---------------------------------------------------------
    printJsonAndPython("ADD_NAMED_VIEW", addNamedView);
    graph.execute(addNamedView, user);
    // [get all named views] Get all named views
    // ---------------------------------------------------------
    final GetAllNamedViews getAllNamedViews = new GetAllNamedViews();
    // ---------------------------------------------------------
    printJsonAndPython("GET_ALL_NAMED_VIEWS", new GetAllNamedViews());
    final CloseableIterable<NamedViewDetail> namedViewDetails = graph.execute(getAllNamedViews, user);
    for (final NamedViewDetail detail : namedViewDetails) {
        print("ALL_NAMED_VIEW", detail.toString());
    }
    // [get elements with named view] create the operation using the NamedView
    // ---------------------------------------------------------
    final GetElements operation = new GetElements.Builder().view(new NamedView.Builder().name("RoadUse edges").build()).input(new EntitySeed("10")).build();
    // ---------------------------------------------------------
    printJsonAndPython("GET_ELEMENTS_WITH_NAMED_VIEW", operation);
    final CloseableIterable<? extends Element> namedViewResults = graph.execute(operation, user);
    for (final Object result : namedViewResults) {
        print("GET_ELEMENTS_WITH_NAMED_VIEW_RESULTS", result.toString());
    }
    // [add named view with parameters] create an operation chain to be executed as a named operation
    // with parameters
    // ---------------------------------------------------------
    final String viewJson = "{" + "      \"edges\" : {" + "        \"RoadUse\" : {  " + "           \"preAggregationFilterFunctions\" : [ {" + "               \"predicate\" : {" + "                   \"class\" : \"uk.gov.gchq.koryphe.impl.predicate.IsMoreThan\"," + "                   \"orEqualTo\" : false," + "                     \"value\": {" + "                         \"java.lang.Long\" : \"${isMoreThan}\"" + "                       }" + "                 }," + "             \"selection\" : [ \"${property}\" ]" + "         } ] }" + "      }," + "      \"entities\" : { }" + "}";
    final ViewParameterDetail propertyParam = new ViewParameterDetail.Builder().description("Property to select").valueClass(String.class).required(true).build();
    final ViewParameterDetail valueParam = new ViewParameterDetail.Builder().description("Value for the is more than predicate").defaultValue(0L).valueClass(Long.class).build();
    final Map<String, ViewParameterDetail> paramDetailMap = Maps.newHashMap();
    paramDetailMap.put("property", propertyParam);
    paramDetailMap.put("isMoreThan", valueParam);
    final AddNamedView addNamedViewWithParams = new AddNamedView.Builder().name("customCountView").description("named View with count param").view(viewJson).parameters(paramDetailMap).overwrite(true).build();
    // ---------------------------------------------------------
    printJsonAndPython("ADD_NAMED_VIEW_WITH_PARAMETERS", addNamedViewWithParams);
    graph.execute(addNamedViewWithParams, user);
    // [get elements with named view with parameters] create the named operation with a parameter
    // ---------------------------------------------------------
    final Map<String, Object> paramMap = Maps.newHashMap();
    paramMap.put("property", "count");
    paramMap.put("isMoreThan", 1L);
    final GetElements operationUsingParams = new GetElements.Builder().view(new NamedView.Builder().name("customCountView").parameters(paramMap).build()).input(new EntitySeed("10")).build();
    // ---------------------------------------------------------
    printJsonAndPython("GET_ELEMENTS_WITH_NAMED_VIEW_WITH_PARAMETERS", operationUsingParams);
    final CloseableIterable<? extends Element> namedViewResultsWithParams = graph.execute(operationUsingParams, user);
    for (final Object result : namedViewResultsWithParams) {
        print("GET_ELEMENTS_WITH_NAMED_VIEW_WITH_PARAMETERS_RESULTS", result.toString());
    }
    return namedViewResultsWithParams;
}
Also used : AddElements(uk.gov.gchq.gaffer.operation.impl.add.AddElements) User(uk.gov.gchq.gaffer.user.User) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) NamedViewDetail(uk.gov.gchq.gaffer.data.elementdefinition.view.NamedViewDetail) RoadAndRoadUseWithTimesAndCardinalitiesElementGenerator(uk.gov.gchq.gaffer.doc.user.generator.RoadAndRoadUseWithTimesAndCardinalitiesElementGenerator) ViewParameterDetail(uk.gov.gchq.gaffer.data.elementdefinition.view.ViewParameterDetail) NamedView(uk.gov.gchq.gaffer.data.elementdefinition.view.NamedView) AddNamedView(uk.gov.gchq.gaffer.named.view.AddNamedView) AddNamedView(uk.gov.gchq.gaffer.named.view.AddNamedView) GetAllNamedViews(uk.gov.gchq.gaffer.named.view.GetAllNamedViews) NamedView(uk.gov.gchq.gaffer.data.elementdefinition.view.NamedView) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) AddNamedView(uk.gov.gchq.gaffer.named.view.AddNamedView) Graph(uk.gov.gchq.gaffer.graph.Graph) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed)

Aggregations

NamedViewDetail (uk.gov.gchq.gaffer.data.elementdefinition.view.NamedViewDetail)22 Test (org.junit.jupiter.api.Test)16 View (uk.gov.gchq.gaffer.data.elementdefinition.view.View)10 NamedView (uk.gov.gchq.gaffer.data.elementdefinition.view.NamedView)9 OperationView (uk.gov.gchq.gaffer.operation.graph.OperationView)7 GetElements (uk.gov.gchq.gaffer.operation.impl.get.GetElements)7 CacheOperationFailedException (uk.gov.gchq.gaffer.named.operation.cache.exception.CacheOperationFailedException)5 ViewParameterDetail (uk.gov.gchq.gaffer.data.elementdefinition.view.ViewParameterDetail)4 ElementFilter (uk.gov.gchq.gaffer.data.element.function.ElementFilter)3 AccessPredicate (uk.gov.gchq.gaffer.access.predicate.AccessPredicate)2 UnrestrictedAccessPredicate (uk.gov.gchq.gaffer.access.predicate.UnrestrictedAccessPredicate)2 NamedViewWriteAccessPredicate (uk.gov.gchq.gaffer.data.elementdefinition.view.access.predicate.NamedViewWriteAccessPredicate)2 ExampleFilterFunction (uk.gov.gchq.gaffer.function.ExampleFilterFunction)2 GetAllNamedViews (uk.gov.gchq.gaffer.named.view.GetAllNamedViews)2 HashSet (java.util.HashSet)1 CustomUserPredicate (uk.gov.gchq.gaffer.access.predicate.user.CustomUserPredicate)1 WrappedCloseableIterable (uk.gov.gchq.gaffer.commonutil.iterable.WrappedCloseableIterable)1 RoadAndRoadUseWithTimesAndCardinalitiesElementGenerator (uk.gov.gchq.gaffer.doc.user.generator.RoadAndRoadUseWithTimesAndCardinalitiesElementGenerator)1 Graph (uk.gov.gchq.gaffer.graph.Graph)1 AddNamedView (uk.gov.gchq.gaffer.named.view.AddNamedView)1