Skip to content

Commit

Permalink
gh-2580 dynamic schema, modification and tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
GCHQDev404 committed Feb 21, 2023
1 parent ed2c56a commit f9f12b0
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2020 Crown Copyright
* Copyright 2016-2023 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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2022 Crown Copyright
* Copyright 2016-2023 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 @@ -16,14 +16,19 @@

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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import uk.gov.gchq.gaffer.commonutil.pair.Pair;
import uk.gov.gchq.gaffer.data.elementdefinition.view.View;
import uk.gov.gchq.gaffer.operation.Operation;
import uk.gov.gchq.gaffer.operation.OperationChain;
import uk.gov.gchq.gaffer.operation.OperationException;
import uk.gov.gchq.gaffer.operation.graph.GraphFilters;
import uk.gov.gchq.gaffer.operation.impl.compare.ElementComparison;
import uk.gov.gchq.gaffer.operation.io.Input;
import uk.gov.gchq.gaffer.operation.io.Output;
import uk.gov.gchq.gaffer.store.Context;
import uk.gov.gchq.gaffer.store.Store;
import uk.gov.gchq.gaffer.store.StoreTrait;
import uk.gov.gchq.gaffer.store.schema.Schema;
Expand All @@ -38,6 +43,8 @@
* Validation class for validating {@link OperationChain}s against {@link ViewValidator}s.
*/
public class OperationChainValidator {
private static final Logger LOGGER = LoggerFactory.getLogger(OperationChainValidator.class);

private final ViewValidator viewValidator;

public OperationChainValidator(final ViewValidator viewValidator) {
Expand Down Expand Up @@ -114,7 +121,15 @@ protected Operation getFirstOperation(final Operation operation) {

protected void validateComparables(final Operation op, final User user, final Store store, final ValidationResult validationResult) {
if (op instanceof ElementComparison) {
final Schema schema = getSchema(op, user, store);
final Schema schema;
try {
schema = getSchema(op, user, store);
} catch (final Exception e) {
final String message = "Unable to getSchema to validate groups";
LOGGER.info(message, e);
validationResult.addError(message);
return;
}
for (final Pair<String, String> pair : ((ElementComparison) op).getComparableGroupPropertyPairs()) {
final SchemaElementDefinition elementDef = schema.getElement(pair.getFirst());
if (null == elementDef) {
Expand All @@ -136,7 +151,15 @@ protected void validateComparables(final Operation op, final User user, final St

protected void validateViews(final Operation op, final User user, final Store store, final ValidationResult validationResult) {
if (shouldValidate(op)) {
final Schema schema = getSchema(op, user, store);
final Schema schema;
try {
schema = getSchema(op, user, store);
} catch (final Exception e) {
final String message = "Unable to getSchema to validate groups";
LOGGER.info(message, e);
validationResult.addError(message);
return;
}
final ValidationResult viewValidationResult = viewValidator.validate(getView(op), schema, getStoreTraits(store));
if (!viewValidationResult.isValid()) {
validationResult.addError("View for operation "
Expand All @@ -155,8 +178,8 @@ protected boolean shouldValidate(final Operation op) {
return op instanceof GraphFilters;
}

protected Schema getSchema(final Operation operation, final User user, final Store store) {
return store.getSchema();
protected Schema getSchema(final Operation operation, final User user, final Store store) throws OperationException {
return store.execute(new GetSchema(), new Context(user));
}

protected Set<StoreTrait> getStoreTraits(final Store store) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2022 Crown Copyright
* Copyright 2017-2023 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 @@ -53,8 +53,7 @@ public OUT doOperation(final OperationChain<OUT> operationChain, final Context c
}

public <O> OperationChain<O> prepareOperationChain(final OperationChain<O> operationChain, final Context context, final Store store) {
final ValidationResult validationResult = opChainValidator.validate(operationChain, context
.getUser(), store);
final ValidationResult validationResult = opChainValidator.validate(operationChain, context.getUser(), store);
if (!validationResult.isValid()) {
throw new IllegalArgumentException("Operation chain is invalid. " + validationResult
.getErrorString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import uk.gov.gchq.gaffer.operation.OperationException;
import uk.gov.gchq.gaffer.store.Context;
import uk.gov.gchq.gaffer.store.Store;
import uk.gov.gchq.gaffer.store.StoreTrait;
import uk.gov.gchq.gaffer.store.operation.GetSchema;
import uk.gov.gchq.gaffer.store.operation.OperationChainValidator;
import uk.gov.gchq.gaffer.store.schema.Schema;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
import uk.gov.gchq.gaffer.operation.io.InputOutput;
import uk.gov.gchq.gaffer.operation.io.Output;
import uk.gov.gchq.gaffer.store.Context;
import uk.gov.gchq.gaffer.store.StoreTrait;
import uk.gov.gchq.gaffer.store.operation.GetSchema;
import uk.gov.gchq.gaffer.store.schema.Schema;
import uk.gov.gchq.gaffer.user.User;
Expand Down Expand Up @@ -149,14 +148,18 @@ public static <OP extends Operation> OP updateOperationForGraph(final OP operati
// then clone the operation and add the new view.
if (validView.hasGroups()) {
((OperationView) resultOp).setView(validView);
} else {
// The view has no groups so the operation would return
// nothing, so we shouldn't execute the operation.
resultOp = null;
}
}
}
} else if (resultOp instanceof AddElements) {
final AddElements addElements = ((AddElements) resultOp);
if (null == addElements.getInput()) {
if (!addElements.isValidate() || !addElements.isSkipInvalidElements()) {
LOGGER.debug("Invalid elements will be skipped when added to {}", graph.getGraphId());
LOGGER.debug("Invalid elements will be skipped");
resultOp = (OP) addElements.shallowClone();
((AddElements) resultOp).setValidate(true);
((AddElements) resultOp).setSkipInvalidElements(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,14 @@ public void shouldErrorIfViewIsInvalid() throws OperationException {
.build(), new User());

// When
assertThatExceptionOfType(OperationException.class)
assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> federatedStoreGraph.execute(
new GetAllElements.Builder()
.view(new View.Builder()
.edge(GROUP_BASIC_EDGE)
.build())
.build(), new User()))
.withStackTraceContaining("View is not valid for graphIds:[mapStore]");
.withStackTraceContaining("View is not valid for graphIds:[RestProxy]");
}

@Test
Expand Down Expand Up @@ -165,8 +165,7 @@ public void shouldMaintainView() throws OperationException {

// Then
assertThat(results)
.hasSize(1)
.first().isEqualTo(entity);
.containsExactly(entity);
}

@Test
Expand Down

0 comments on commit f9f12b0

Please sign in to comment.