use of uk.gov.gchq.gaffer.data.elementdefinition.exception.SchemaException in project Gaffer by gchq.
the class Store method validateSchemas.
public void validateSchemas() {
final ValidationResult validationResult = new ValidationResult();
if (null == schema) {
validationResult.addError("Schema is missing");
} else {
validationResult.add(schema.validate());
getSchemaElements().forEach((key, value) -> value.getProperties().forEach(propertyName -> {
final Class propertyClass = value.getPropertyClass(propertyName);
final Serialiser serialisation = value.getPropertyTypeDef(propertyName).getSerialiser();
if (null == serialisation) {
validationResult.addError(String.format("Could not find a serialiser for property '%s' in the group '%s'.", propertyName, key));
} else if (!serialisation.canHandle(propertyClass)) {
validationResult.addError(String.format("Schema serialiser (%s) for property '%s' in the group '%s' cannot handle property found in the schema", serialisation.getClass().getName(), propertyName, key));
}
}));
validateSchema(validationResult, getSchema().getVertexSerialiser());
getSchema().getTypes().forEach((k, v) -> validateSchema(validationResult, v.getSerialiser()));
}
if (!validationResult.isValid()) {
throw new SchemaException("Schema is not valid. " + validationResult.getErrorString());
}
}
use of uk.gov.gchq.gaffer.data.elementdefinition.exception.SchemaException in project Gaffer by gchq.
the class FederatedStoreTest method shouldFailGetAllElementsFromSelectedGraphsWithViewOfMissingEntityGroupWhileHasConflictingSchemasDueToDiffVertexSerialiser.
@Test
public void shouldFailGetAllElementsFromSelectedGraphsWithViewOfMissingEntityGroupWhileHasConflictingSchemasDueToDiffVertexSerialiser() throws OperationException {
// given
final Entity A = getEntityA();
final Entity B = getEntityB();
addElementsToNewGraph(A, "graphA", PATH_ENTITY_A_SCHEMA_JSON);
addElementsToNewGraph(B, "graphB", PATH_ENTITY_B_SCHEMA_JSON);
try {
// when
store.execute(new GetSchema.Builder().build(), userContext);
fail("exception expected");
} catch (final SchemaException e) {
// then
assertTrue(Pattern.compile("Unable to merge the schemas for all of your federated graphs: \\[graph., graph.\\]\\. You can limit which graphs to query for using the operation option: gaffer\\.federatedstore\\.operation\\.graphIds").matcher(e.getMessage()).matches(), e.getMessage());
}
try {
// when
CloseableIterable<? extends Element> responseGraphAWithBView = store.execute(new GetAllElements.Builder().option(FederatedStoreConstants.KEY_OPERATION_OPTIONS_GRAPH_IDS, "graphA").view(new View.Builder().entity("entityB").build()).build(), userContext);
fail("exception expected");
} catch (Exception e) {
// then
assertEquals("Operation chain is invalid. Validation errors: \n" + "View is not valid for graphIds:[graphA]\n" + "(graphId: graphA) View for operation uk.gov.gchq.gaffer.operation.impl.get.GetAllElements is not valid. \n" + "(graphId: graphA) Entity group entityB does not exist in the schema", e.getMessage());
}
try {
// when
final CloseableIterable<? extends Element> responseGraphBWithAView = store.execute(new GetAllElements.Builder().option(FederatedStoreConstants.KEY_OPERATION_OPTIONS_GRAPH_IDS, "graphB").view(new View.Builder().entity("entityA").build()).build(), userContext);
fail("exception expected");
} catch (Exception e) {
// then
assertEquals("Operation chain is invalid. Validation errors: \n" + "View is not valid for graphIds:[graphB]\n" + "(graphId: graphB) View for operation uk.gov.gchq.gaffer.operation.impl.get.GetAllElements is not valid. \n" + "(graphId: graphB) Entity group entityA does not exist in the schema", e.getMessage());
}
addGraphWithPaths("graphC", PROPERTIES_1, PATH_ENTITY_B_SCHEMA_JSON);
try {
// when
final CloseableIterable<? extends Element> responseGraphBWithAView = store.execute(new GetAllElements.Builder().option(FederatedStoreConstants.KEY_OPERATION_OPTIONS_GRAPH_IDS, "graphB,graphC").view(new View.Builder().entity("entityA").build()).build(), userContext);
fail("exception expected");
} catch (Exception e) {
// then
assertEquals("Operation chain is invalid. Validation errors: \n" + "View is not valid for graphIds:[graphB,graphC]\n" + "(graphId: graphB) View for operation uk.gov.gchq.gaffer.operation.impl.get.GetAllElements is not valid. \n" + "(graphId: graphB) Entity group entityA does not exist in the schema\n" + "(graphId: graphC) View for operation uk.gov.gchq.gaffer.operation.impl.get.GetAllElements is not valid. \n" + "(graphId: graphC) Entity group entityA does not exist in the schema", e.getMessage());
}
}
use of uk.gov.gchq.gaffer.data.elementdefinition.exception.SchemaException in project Gaffer by gchq.
the class FederatedStoreTest method shouldGetAllElementsFromSelectedGraphsWithViewOfExistingEntityGroupWhileHasConflictingSchemasDueToDiffVertexSerialiser.
@Test
public void shouldGetAllElementsFromSelectedGraphsWithViewOfExistingEntityGroupWhileHasConflictingSchemasDueToDiffVertexSerialiser() throws OperationException {
// given
final Entity A = getEntityA();
final Entity B = getEntityB();
final ArrayList<Entity> expectedA = Lists.newArrayList(A);
final ArrayList<Entity> expectedB = Lists.newArrayList(B);
addElementsToNewGraph(A, "graphA", PATH_ENTITY_A_SCHEMA_JSON);
addElementsToNewGraph(B, "graphB", PATH_ENTITY_B_SCHEMA_JSON);
try {
// when
store.execute(new GetSchema.Builder().build(), userContext);
fail("exception expected");
} catch (final SchemaException e) {
// then
assertTrue(Pattern.compile("Unable to merge the schemas for all of your federated graphs: \\[graph., graph.\\]\\. You can limit which graphs to query for using the operation option: gaffer\\.federatedstore\\.operation\\.graphIds").matcher(e.getMessage()).matches(), e.getMessage());
}
// when
final CloseableIterable<? extends Element> responseGraphAWithAView = store.execute(new GetAllElements.Builder().option(FederatedStoreConstants.KEY_OPERATION_OPTIONS_GRAPH_IDS, "graphA").view(new View.Builder().entity("entityA").build()).build(), userContext);
final CloseableIterable<? extends Element> responseGraphBWithBView = store.execute(new GetAllElements.Builder().option(FederatedStoreConstants.KEY_OPERATION_OPTIONS_GRAPH_IDS, "graphB").view(new View.Builder().entity("entityB").build()).build(), userContext);
final CloseableIterable<? extends Element> responseAllGraphsWithAView = store.execute(new GetAllElements.Builder().option(FederatedStoreConstants.KEY_OPERATION_OPTIONS_GRAPH_IDS, "graphA,graphB").view(new View.Builder().entity("entityA").build()).build(), userContext);
final CloseableIterable<? extends Element> responseAllGraphsWithBView = store.execute(new GetAllElements.Builder().option(FederatedStoreConstants.KEY_OPERATION_OPTIONS_GRAPH_IDS, "graphA,graphB").view(new View.Builder().entity("entityB").build()).build(), userContext);
// then
ElementUtil.assertElementEquals(expectedA, responseGraphAWithAView);
ElementUtil.assertElementEquals(expectedB, responseGraphBWithBView);
ElementUtil.assertElementEquals(expectedA, responseAllGraphsWithAView);
ElementUtil.assertElementEquals(expectedB, responseAllGraphsWithBView);
}
use of uk.gov.gchq.gaffer.data.elementdefinition.exception.SchemaException in project Gaffer by gchq.
the class FederatedGraphStorage method getSchema.
public Schema getSchema(final Map<String, String> config, final User user) {
if (null == user) {
// no user then return an empty schema
return new Schema();
}
final List<String> graphIds = FederatedStoreUtil.getGraphIds(config);
final Stream<Graph> graphs = getStream(user, graphIds);
final Builder schemaBuilder = new Builder();
try {
graphs.forEach(g -> schemaBuilder.merge(g.getSchema()));
} catch (final SchemaException e) {
final List<String> resultGraphIds = getStream(user, graphIds).map(Graph::getGraphId).collect(Collectors.toList());
throw new SchemaException(String.format(UNABLE_TO_MERGE_THE_SCHEMAS_FOR_ALL_OF_YOUR_FEDERATED_GRAPHS, resultGraphIds, KEY_OPERATION_OPTIONS_GRAPH_IDS), e);
}
return schemaBuilder.build();
}
use of uk.gov.gchq.gaffer.data.elementdefinition.exception.SchemaException in project Gaffer by gchq.
the class FederatedGraphStorage method getSchema.
public Schema getSchema(final GetSchema operation, final Context context) {
if (null == context || null == context.getUser()) {
// no user then return an empty schema
return new Schema();
}
if (null == operation) {
return getSchema((Map<String, String>) null, context);
}
final List<String> graphIds = FederatedStoreUtil.getGraphIds(operation.getOptions());
final Stream<Graph> graphs = getStream(context.getUser(), graphIds);
final Builder schemaBuilder = new Builder();
try {
if (operation.isCompact()) {
final GetSchema getSchema = new GetSchema.Builder().compact(true).build();
graphs.forEach(g -> {
try {
schemaBuilder.merge(g.execute(getSchema, context));
} catch (final OperationException e) {
throw new RuntimeException("Unable to fetch schema from graph " + g.getGraphId(), e);
}
});
} else {
graphs.forEach(g -> schemaBuilder.merge(g.getSchema()));
}
} catch (final SchemaException e) {
final List<String> resultGraphIds = getStream(context.getUser(), graphIds).map(Graph::getGraphId).collect(Collectors.toList());
throw new SchemaException("Unable to merge the schemas for all of your federated graphs: " + resultGraphIds + ". You can limit which graphs to query for using the operation option: " + KEY_OPERATION_OPTIONS_GRAPH_IDS, e);
}
return schemaBuilder.build();
}
Aggregations