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;
}
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);
}
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);
}
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());
}
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());
}
}
Aggregations