Search in sources :

Example 76 with ValidationResult

use of uk.gov.gchq.koryphe.ValidationResult in project Gaffer by gchq.

the class FederatedViewValidator method validate.

@Override
public ValidationResult validate(final View view, final Schema schema, final Set<StoreTrait> storeTraits) {
    final ValidationResult rtn = new ValidationResult();
    final boolean isStoreOrdered = storeTraits.contains(StoreTrait.ORDERED);
    if (null != view) {
        final ValidationResult entitiesResult = getEntityResult(view, schema, storeTraits, isStoreOrdered);
        final ValidationResult edgeResult = getEdgeResult(view, schema, storeTraits, isStoreOrdered);
        final boolean isEntityViewInvalid = !entitiesResult.isValid();
        final boolean isEdgeViewInvalid = !edgeResult.isValid();
        final boolean isEntityViewInvalidAndTheOnlyViewRequested = isEntityViewInvalid && (isNull(view.getEdges()) || view.getEdges().isEmpty());
        final boolean isEdgeViewInvalidAndTheOnlyViewRequested = isEdgeViewInvalid && (isNull(view.getEntities()) || view.getEntities().isEmpty());
        if (isEntityViewInvalid && isEdgeViewInvalid) {
            rtn.add(entitiesResult);
            rtn.add(edgeResult);
        } else if (isEntityViewInvalidAndTheOnlyViewRequested) {
            rtn.add(entitiesResult);
        } else if (isEdgeViewInvalidAndTheOnlyViewRequested) {
            rtn.add(edgeResult);
        }
    }
    return rtn;
}
Also used : ValidationResult(uk.gov.gchq.koryphe.ValidationResult)

Example 77 with ValidationResult

use of uk.gov.gchq.koryphe.ValidationResult in project Gaffer by gchq.

the class OperationChainHandlerTest method shouldHandleOperationChain.

@Test
public void shouldHandleOperationChain() throws OperationException {
    // Given
    final OperationChainValidator opChainValidator = mock(OperationChainValidator.class);
    final List<OperationChainOptimiser> opChainOptimisers = Collections.emptyList();
    final OperationChainHandler opChainHandler = new OperationChainHandler(opChainValidator, opChainOptimisers);
    final Context context = mock(Context.class);
    final Store store = mock(Store.class);
    final User user = mock(User.class);
    final StoreProperties storeProperties = new StoreProperties();
    final GetAdjacentIds op1 = mock(GetAdjacentIds.class);
    final GetElements op2 = mock(GetElements.class);
    final OperationChain opChain = new OperationChain(Arrays.asList(op1, op2));
    final Entity expectedResult = new Entity(TestGroups.ENTITY);
    given(context.getUser()).willReturn(user);
    given(store.getProperties()).willReturn(storeProperties);
    given(opChainValidator.validate(any(), any(), any())).willReturn(new ValidationResult());
    given(store.handleOperation(op1, context)).willReturn(new WrappedCloseableIterable<>(Collections.singletonList(new EntitySeed())));
    given(store.handleOperation(op2, context)).willReturn(expectedResult);
    // When
    final Object result = opChainHandler.doOperation(opChain, context, store);
    // Then
    assertSame(expectedResult, result);
}
Also used : Context(uk.gov.gchq.gaffer.store.Context) Entity(uk.gov.gchq.gaffer.data.element.Entity) User(uk.gov.gchq.gaffer.user.User) GetAdjacentIds(uk.gov.gchq.gaffer.operation.impl.get.GetAdjacentIds) Store(uk.gov.gchq.gaffer.store.Store) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) OperationChainValidator(uk.gov.gchq.gaffer.store.operation.OperationChainValidator) ValidationResult(uk.gov.gchq.koryphe.ValidationResult) OperationChainOptimiser(uk.gov.gchq.gaffer.store.optimiser.OperationChainOptimiser) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed) StoreProperties(uk.gov.gchq.gaffer.store.StoreProperties) Test(org.junit.jupiter.api.Test)

Example 78 with ValidationResult

use of uk.gov.gchq.koryphe.ValidationResult in project Gaffer by gchq.

the class OperationChainHandlerTest method shouldHandleNonInputOperation.

@Test
public void shouldHandleNonInputOperation() throws OperationException {
    // Given
    final OperationChainValidator opChainValidator = mock(OperationChainValidator.class);
    final List<OperationChainOptimiser> opChainOptimisers = Collections.emptyList();
    final OperationChainHandler opChainHandler = new OperationChainHandler(opChainValidator, opChainOptimisers);
    final Context context = mock(Context.class);
    final Store store = mock(Store.class);
    final User user = mock(User.class);
    final StoreProperties storeProperties = new StoreProperties();
    final GetAllElements op = mock(GetAllElements.class);
    final OperationChain opChain = new OperationChain(Collections.singletonList(op));
    final Entity expectedResult = new Entity(TestGroups.ENTITY);
    given(context.getUser()).willReturn(user);
    given(store.getProperties()).willReturn(storeProperties);
    given(opChainValidator.validate(any(), any(), any())).willReturn(new ValidationResult());
    given(store.handleOperation(op, context)).willReturn(expectedResult);
    // When
    final Object result = opChainHandler.doOperation(opChain, context, store);
    // Then
    assertSame(expectedResult, result);
}
Also used : Context(uk.gov.gchq.gaffer.store.Context) Entity(uk.gov.gchq.gaffer.data.element.Entity) User(uk.gov.gchq.gaffer.user.User) Store(uk.gov.gchq.gaffer.store.Store) OperationChainValidator(uk.gov.gchq.gaffer.store.operation.OperationChainValidator) ValidationResult(uk.gov.gchq.koryphe.ValidationResult) OperationChainOptimiser(uk.gov.gchq.gaffer.store.optimiser.OperationChainOptimiser) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) GetAllElements(uk.gov.gchq.gaffer.operation.impl.get.GetAllElements) StoreProperties(uk.gov.gchq.gaffer.store.StoreProperties) Test(org.junit.jupiter.api.Test)

Example 79 with ValidationResult

use of uk.gov.gchq.koryphe.ValidationResult in project Gaffer by gchq.

the class ValidateOperationChainHandlerTest method shouldValidateOperationChain.

@Test
public void shouldValidateOperationChain() throws OperationException {
    // Given
    final AddElements addElements = new AddElements();
    final GetAdjacentIds getAdj = new GetAdjacentIds();
    final GetElements getElements = new GetElements();
    final DiscardOutput discardOutput = new DiscardOutput();
    OperationChain chain = new OperationChain.Builder().first(addElements).then(getAdj).then(getElements).then(discardOutput).build();
    ValidateOperationChain validateOperationChain = new ValidateOperationChain.Builder().operationChain(chain).build();
    given(store.getOperationChainValidator()).willReturn(new OperationChainValidator(new ViewValidator()));
    ValidateOperationChainHandler handler = new ValidateOperationChainHandler();
    // When
    ValidationResult result = handler.doOperation(validateOperationChain, context, store);
    // Then
    assertTrue(result.isValid());
}
Also used : AddElements(uk.gov.gchq.gaffer.operation.impl.add.AddElements) GetAdjacentIds(uk.gov.gchq.gaffer.operation.impl.get.GetAdjacentIds) ViewValidator(uk.gov.gchq.gaffer.store.schema.ViewValidator) ValidateOperationChain(uk.gov.gchq.gaffer.operation.impl.ValidateOperationChain) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) DiscardOutput(uk.gov.gchq.gaffer.operation.impl.DiscardOutput) ValidateOperationChain(uk.gov.gchq.gaffer.operation.impl.ValidateOperationChain) OperationChainValidator(uk.gov.gchq.gaffer.store.operation.OperationChainValidator) ValidationResult(uk.gov.gchq.koryphe.ValidationResult) Test(org.junit.jupiter.api.Test)

Example 80 with ValidationResult

use of uk.gov.gchq.koryphe.ValidationResult in project Gaffer by gchq.

the class AuthorisedGraphForExportDelegate method validateGraph.

@Override
public void validateGraph(final Store store, final String graphId, final Schema schema, final StoreProperties storeProperties, final List<String> parentSchemaIds, final String parentStorePropertiesId, final Pair<Schema, StoreProperties> existingGraphPair) {
    ValidationResult result = super.validate(store, graphId, schema, storeProperties, parentSchemaIds, parentStorePropertiesId, existingGraphPair, new ValidationResult());
    Set<String> errors = result.getErrors();
    result.getErrors().removeIf(s -> s.equals(String.format(S_CANNOT_BE_USED_WITHOUT_A_GRAPH_LIBRARY, PARENT_SCHEMA_IDS)) || s.equals(String.format(GRAPH_ID_S_CANNOT_BE_CREATED_WITHOUT_DEFINED_KNOWN_S, graphId, SCHEMA_STRING)) || s.equals(String.format(GRAPH_ID_S_CANNOT_BE_CREATED_WITHOUT_DEFINED_KNOWN_S, graphId, STORE_PROPERTIES_STRING)));
    result = new ValidationResult();
    for (final String error : errors) {
        result.addError(error);
    }
    if (null == store.getGraphLibrary()) {
        // GraphLibrary is required as only a graphId, a parentStorePropertiesId or a parentSchemaId can be given
        result.addError(String.format(STORE_GRAPH_LIBRARY_IS_NULL));
    } else {
        // Check all the auths before anything else
        if (!isAuthorised(user, idAuths.get(graphId))) {
            result.addError(String.format(USER_IS_NOT_AUTHORISED_TO_EXPORT_USING_S_S, GRAPH_ID, graphId));
        }
        if (null != parentSchemaIds) {
            for (final String parentSchemaId : parentSchemaIds) {
                if (!isAuthorised(user, idAuths.get(parentSchemaId))) {
                    result.addError(String.format(USER_IS_NOT_AUTHORISED_TO_EXPORT_USING_S_S, SCHEMA_ID, parentSchemaId));
                }
            }
        }
        if (null != parentStorePropertiesId) {
            if (!isAuthorised(user, idAuths.get(parentStorePropertiesId))) {
                result.addError(String.format(USER_IS_NOT_AUTHORISED_TO_EXPORT_USING_S_S, STORE_PROPERTIES_ID, parentStorePropertiesId));
            }
        }
        if (store.getGraphLibrary().exists(graphId)) {
            if (null != parentSchemaIds) {
                result.addError(String.format(GRAPH_S_ALREADY_EXISTS_SO_YOU_CANNOT_USE_A_DIFFERENT_S_DO_NOT_SET_THE_S_FIELD, graphId, SCHEMA_STRING, PARENT_SCHEMA_IDS));
            }
            if (null != parentStorePropertiesId) {
                result.addError(String.format(GRAPH_S_ALREADY_EXISTS_SO_YOU_CANNOT_USE_A_DIFFERENT_S_DO_NOT_SET_THE_S_FIELD, graphId, STORE_PROPERTIES_STRING, PARENT_STORE_PROPERTIES_ID));
            }
        } else if (!store.getGraphLibrary().exists(graphId) && null == parentSchemaIds && null == parentStorePropertiesId) {
            result.addError(String.format(GRAPH_LIBRARY_CANNOT_BE_FOUND_WITH_GRAPHID_S, graphId));
        }
        if (null != parentSchemaIds && null == parentStorePropertiesId) {
            result.addError(String.format(S_MUST_BE_SPECIFIED_WITH_S, PARENT_STORE_PROPERTIES_ID, PARENT_SCHEMA_IDS));
        }
        if (null == parentSchemaIds && null != parentStorePropertiesId) {
            result.addError(String.format(S_MUST_BE_SPECIFIED_WITH_S, PARENT_SCHEMA_IDS, PARENT_STORE_PROPERTIES_ID));
        }
        if (graphId.equals(store.getGraphId())) {
            result.addError(String.format(CANNOT_EXPORT_TO_THE_SAME_GRAPH_S, graphId));
        }
    }
    if (!result.isValid()) {
        throw new IllegalArgumentException(result.getErrorString());
    }
}
Also used : ValidationResult(uk.gov.gchq.koryphe.ValidationResult)

Aggregations

ValidationResult (uk.gov.gchq.koryphe.ValidationResult)132 Test (org.junit.jupiter.api.Test)86 View (uk.gov.gchq.gaffer.data.elementdefinition.view.View)32 ElementFilter (uk.gov.gchq.gaffer.data.element.function.ElementFilter)27 HashMap (java.util.HashMap)13 OperationTest (uk.gov.gchq.gaffer.operation.OperationTest)12 ElementAggregator (uk.gov.gchq.gaffer.data.element.function.ElementAggregator)11 OperationChain (uk.gov.gchq.gaffer.operation.OperationChain)11 ViewElementDefinition (uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition)9 ExampleTransformFunction (uk.gov.gchq.gaffer.function.ExampleTransformFunction)9 OperationChainValidator (uk.gov.gchq.gaffer.store.operation.OperationChainValidator)9 GetElements (uk.gov.gchq.gaffer.operation.impl.get.GetElements)8 Schema (uk.gov.gchq.gaffer.store.schema.Schema)8 Signature (uk.gov.gchq.koryphe.signature.Signature)8 Map (java.util.Map)7 OperationChainOptimiser (uk.gov.gchq.gaffer.store.optimiser.OperationChainOptimiser)7 Set (java.util.Set)6 Operation (uk.gov.gchq.gaffer.operation.Operation)6 Element (uk.gov.gchq.gaffer.data.element.Element)5 ElementTransformer (uk.gov.gchq.gaffer.data.element.function.ElementTransformer)5