use of nl.knaw.huygens.timbuctoo.v5.graphql.customschema.MergeSchemas in project timbuctoo by HuygensING.
the class MergeSchemasTest method mergeSchemaReturnsMergedSchemaWithAllPredicatesForSingleType.
@Test
public void mergeSchemaReturnsMergedSchemaWithAllPredicatesForSingleType() throws Exception {
MergeSchemas mergeSchemas = new MergeSchemas();
Map<String, Type> generatedSchema = new HashMap<>();
generatedSchema.put("Type", createTypeWithPredicate("generated", Direction.OUT));
Map<String, Type> customSchema = new HashMap<>();
customSchema.put("Type", createTypeWithPredicate("custom", Direction.IN));
Map<String, Type> mergedSchema = mergeSchemas.mergeSchema(generatedSchema, customSchema);
assertThat(mergedSchema, hasEntry(is("Type"), hasProperty("predicates", containsInAnyOrder(predicateMatcher().withName("generated").withDirection(Direction.OUT), predicateMatcher().withName("custom").withDirection(Direction.IN)))));
}
use of nl.knaw.huygens.timbuctoo.v5.graphql.customschema.MergeSchemas in project timbuctoo by HuygensING.
the class ExtendSchemaMutation method get.
@Override
public Object get(DataFetchingEnvironment env) {
DataSet dataSet = MutationHelpers.getDataSet(env, dataSetRepository::getDataSet);
MutationHelpers.checkAdminPermissions(env, dataSet.getMetadata());
final SchemaStore generatedSchema = dataSet.getSchemaStore();
Map<String, Type> customTypes = new HashMap<>();
List<ExplicitType> customSchema;
Map<String, List<ExplicitField>> newCustomSchema = new HashMap<>();
try {
String customSchemaString = OBJECT_MAPPER.writeValueAsString(env.getArgument("customSchema"));
customSchema = OBJECT_MAPPER.readValue(customSchemaString, new TypeReference<List<ExplicitType>>() {
});
} catch (IOException e) {
throw new RuntimeException("Could not parse the schema input");
}
for (ExplicitType explicitType : customSchema) {
customTypes.put(explicitType.getCollectionId(), explicitType.convertToType());
newCustomSchema.put(explicitType.getCollectionId(), explicitType.getFields());
}
Map<String, List<ExplicitField>> existingCustomSchema = dataSet.getCustomSchema();
Map<String, Type> existingCustomSchemaTypes = new HashMap<>();
for (Map.Entry<String, List<ExplicitField>> entry : existingCustomSchema.entrySet()) {
ExplicitType tempExplicitType = new ExplicitType(entry.getKey(), entry.getValue());
existingCustomSchemaTypes.put(entry.getKey(), tempExplicitType.convertToType());
}
MergeSchemas mergeSchemas = new MergeSchemas();
customTypes = mergeSchemas.mergeSchema(existingCustomSchemaTypes, customTypes);
MergeExplicitSchemas mergeExplicitSchemas = new MergeExplicitSchemas();
Map<String, List<ExplicitField>> mergedExplicitSchema = mergeExplicitSchemas.mergeExplicitSchemas(existingCustomSchema, newCustomSchema);
try {
dataSet.saveCustomSchema(mergedExplicitSchema);
} catch (IOException e) {
LOG.error("Saving the custom schema failed", e);
throw new RuntimeException(e);
}
Map<String, Type> mergedSchema = mergeSchemas.mergeSchema(generatedSchema.getStableTypes(), customTypes);
for (Map.Entry<String, Type> customType : customTypes.entrySet()) {
if (!mergedSchema.containsKey(customType.getKey())) {
return ImmutableMap.of("message", "Schema extension was unsuccessful.");
}
}
return ImmutableMap.of("message", "Schema extended successfully.");
}
use of nl.knaw.huygens.timbuctoo.v5.graphql.customschema.MergeSchemas in project timbuctoo by HuygensING.
the class MergeSchemasTest method mergeSchemaMergesMatchingPredicates.
@Test
public void mergeSchemaMergesMatchingPredicates() throws Exception {
final MergeSchemas mergeSchemas = new MergeSchemas();
Map<String, Type> generatedSchema = new HashMap<>();
Type predType1 = createTypeWithPredicate("generated", Direction.IN);
predType1.getPredicate("generated", Direction.IN).setHasBeenList(true);
predType1.getPredicate("generated", Direction.IN).setOwner(new Type("testOwner"));
generatedSchema.put("Type", predType1);
Map<String, Type> customSchema = new HashMap<>();
Type predType2 = createTypeWithPredicate("generated", Direction.IN);
predType2.getPredicate("generated", Direction.IN).setHasBeenList(false);
predType2.getPredicate("generated", Direction.IN).setOwner(new Type("testOwner"));
customSchema.put("Type", predType2);
Map<String, Type> mergedSchema = mergeSchemas.mergeSchema(generatedSchema, customSchema);
assertThat(mergedSchema, hasEntry(is("Type"), hasProperty("predicates", contains(predicateMatcher().withName("generated").withDirection(Direction.IN).withWasList(true)))));
assertThat(mergedSchema, hasEntry(is("Type"), hasProperty("predicates", not(hasItem(predicateMatcher().withName("generated").withDirection(Direction.IN).withWasList(false))))));
}
Aggregations