Search in sources :

Example 1 with CreateMutationChangeLog

use of nl.knaw.huygens.timbuctoo.v5.graphql.mutations.dto.CreateMutationChangeLog in project timbuctoo by HuygensING.

the class CreateMutationChangeLogTest method getAdditionsReturnsCreations.

@Test
public void getAdditionsReturnsCreations() throws Exception {
    String addedValue = "newValue";
    Map<Object, Object> creations = Maps.newHashMap();
    creations.put(NAMES_FIELD, createPropertyInput(addedValue));
    Map<Object, Object> entity = Maps.newHashMap();
    entity.put("creations", creations);
    CreateMutationChangeLog instance = new CreateMutationChangeLog(new Graph(GRAPH), SUBJECT, TYPE_URI, entity);
    List<Change> adds = instance.getAdditions(dataSet).collect(toList());
    assertThat(adds.size(), is(2));
    assertThat(adds.get(0), is(likeChange().withValues(new Value(TYPE_URI, null)).oldValuesIsEmpty()));
    assertThat(adds.get(1), is(likeChange().withValues(new Value(addedValue, STRING)).oldValuesIsEmpty()));
}
Also used : Graph(nl.knaw.huygens.timbuctoo.v5.util.Graph) Value(nl.knaw.huygens.timbuctoo.v5.graphql.mutations.Change.Value) ChangeMatcher.likeChange(nl.knaw.huygens.timbuctoo.v5.graphql.mutations.ChangeMatcher.likeChange) CreateMutationChangeLog(nl.knaw.huygens.timbuctoo.v5.graphql.mutations.dto.CreateMutationChangeLog) Test(org.junit.Test)

Example 2 with CreateMutationChangeLog

use of nl.knaw.huygens.timbuctoo.v5.graphql.mutations.dto.CreateMutationChangeLog in project timbuctoo by HuygensING.

the class CreateMutation method executeAction.

@Override
public Object executeAction(DataFetchingEnvironment environment) {
    final Graph graph = new Graph(environment.getArgument("graph"));
    final String uri = environment.getArgument("uri");
    final Map entity = environment.getArgument("entity");
    ImmutableContextData contextData = environment.getContext();
    Optional<User> userOpt = contextData.getUser();
    if (!userOpt.isPresent()) {
        throw new RuntimeException("User should be logged in.");
    }
    User user = userOpt.get();
    Optional<DataSet> dataSetOpt = dataSetRepository.getDataSet(user, ownerId, dataSetName);
    if (!dataSetOpt.isPresent()) {
        throw new RuntimeException("Data set is not available.");
    }
    DataSet dataSet = dataSetOpt.get();
    if (!contextData.getUserPermissionCheck().hasPermission(dataSet.getMetadata(), Permission.CREATE)) {
        throw new RuntimeException("User should have permissions to create entities of the data set.");
    }
    try (Stream<CursorQuad> quads = dataSet.getQuadStore().getQuadsInGraph(uri, Optional.of(graph))) {
        if (quads.findAny().isPresent()) {
            if (graph.isDefaultGraph()) {
                throw new RuntimeException("Subject with uri '" + uri + "' already exists in the default graph");
            } else {
                throw new RuntimeException("Subject with uri '" + uri + "' already exists in graph '" + graph + "'");
            }
        }
    }
    try {
        dataSet.getImportManager().generateLog(dataSet.getMetadata().getBaseUri(), null, new GraphQlToRdfPatch(graph.getUri(), uri, userUriCreator.create(user), new CreateMutationChangeLog(graph, uri, typeUri, entity))).get();
    } catch (LogStorageFailedException | JsonProcessingException | InterruptedException | ExecutionException e) {
        throw new RuntimeException(e);
    }
    return subjectFetcher.getItemInGraph(uri, Optional.of(graph), dataSet);
}
Also used : User(nl.knaw.huygens.timbuctoo.v5.security.dto.User) ImmutableContextData(nl.knaw.huygens.timbuctoo.v5.graphql.datafetchers.dto.ImmutableContextData) DataSet(nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSet) LogStorageFailedException(nl.knaw.huygens.timbuctoo.v5.filestorage.exceptions.LogStorageFailedException) Graph(nl.knaw.huygens.timbuctoo.v5.util.Graph) CursorQuad(nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.dto.CursorQuad) ExecutionException(java.util.concurrent.ExecutionException) Map(java.util.Map) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) CreateMutationChangeLog(nl.knaw.huygens.timbuctoo.v5.graphql.mutations.dto.CreateMutationChangeLog)

Example 3 with CreateMutationChangeLog

use of nl.knaw.huygens.timbuctoo.v5.graphql.mutations.dto.CreateMutationChangeLog in project timbuctoo by HuygensING.

the class CreateMutationChangeLogTest method getReplacementsReturnsNothing.

@Test
public void getReplacementsReturnsNothing() throws Exception {
    String addedValue1 = "newValue1";
    String addedValue2 = "newValue2";
    Map<Object, Object> creations = Maps.newHashMap();
    creations.put(NAMES_FIELD, newArrayList(createPropertyInput(addedValue1), createPropertyInput(addedValue2)));
    Map<Object, Object> entity = Maps.newHashMap();
    entity.put("creations", creations);
    CreateMutationChangeLog instance = new CreateMutationChangeLog(new Graph(GRAPH), SUBJECT, TYPE_URI, entity);
    Stream<Change> replacements = instance.getReplacements(dataSet);
    assertThat(replacements, StreamMatchers.empty());
}
Also used : Graph(nl.knaw.huygens.timbuctoo.v5.util.Graph) ChangeMatcher.likeChange(nl.knaw.huygens.timbuctoo.v5.graphql.mutations.ChangeMatcher.likeChange) CreateMutationChangeLog(nl.knaw.huygens.timbuctoo.v5.graphql.mutations.dto.CreateMutationChangeLog) Test(org.junit.Test)

Example 4 with CreateMutationChangeLog

use of nl.knaw.huygens.timbuctoo.v5.graphql.mutations.dto.CreateMutationChangeLog in project timbuctoo by HuygensING.

the class CreateMutationChangeLogTest method getDeletionsReturnsNothing.

@Test
public void getDeletionsReturnsNothing() throws Exception {
    String addedValue1 = "newValue1";
    String addedValue2 = "newValue2";
    Map<Object, Object> creations = Maps.newHashMap();
    creations.put(NAMES_FIELD, newArrayList(createPropertyInput(addedValue1), createPropertyInput(addedValue2)));
    Map<Object, Object> entity = Maps.newHashMap();
    entity.put("creations", creations);
    CreateMutationChangeLog instance = new CreateMutationChangeLog(new Graph(GRAPH), SUBJECT, TYPE_URI, entity);
    Stream<Change> deletes = instance.getDeletions(dataSet);
    assertThat(deletes, StreamMatchers.empty());
}
Also used : Graph(nl.knaw.huygens.timbuctoo.v5.util.Graph) ChangeMatcher.likeChange(nl.knaw.huygens.timbuctoo.v5.graphql.mutations.ChangeMatcher.likeChange) CreateMutationChangeLog(nl.knaw.huygens.timbuctoo.v5.graphql.mutations.dto.CreateMutationChangeLog) Test(org.junit.Test)

Example 5 with CreateMutationChangeLog

use of nl.knaw.huygens.timbuctoo.v5.graphql.mutations.dto.CreateMutationChangeLog in project timbuctoo by HuygensING.

the class CreateMutationChangeLogTest method getAdditionsForListReturnsCreations.

@Test
public void getAdditionsForListReturnsCreations() throws Exception {
    String addedValue1 = "newValue1";
    String addedValue2 = "newValue2";
    Map<Object, Object> creations = Maps.newHashMap();
    creations.put(NAMES_FIELD, newArrayList(createPropertyInput(addedValue1), createPropertyInput(addedValue2)));
    Map<Object, Object> entity = Maps.newHashMap();
    entity.put("creations", creations);
    CreateMutationChangeLog instance = new CreateMutationChangeLog(new Graph(GRAPH), SUBJECT, TYPE_URI, entity);
    List<Change> adds = instance.getAdditions(dataSet).collect(toList());
    assertThat(adds.size(), is(2));
    assertThat(adds.get(0), is(likeChange().withValues(new Value(TYPE_URI, null)).oldValuesIsEmpty()));
    assertThat(adds.get(1), is(likeChange().withValues(new Value(addedValue1, STRING), new Value(addedValue2, STRING)).oldValuesIsEmpty()));
}
Also used : Graph(nl.knaw.huygens.timbuctoo.v5.util.Graph) Value(nl.knaw.huygens.timbuctoo.v5.graphql.mutations.Change.Value) ChangeMatcher.likeChange(nl.knaw.huygens.timbuctoo.v5.graphql.mutations.ChangeMatcher.likeChange) CreateMutationChangeLog(nl.knaw.huygens.timbuctoo.v5.graphql.mutations.dto.CreateMutationChangeLog) Test(org.junit.Test)

Aggregations

CreateMutationChangeLog (nl.knaw.huygens.timbuctoo.v5.graphql.mutations.dto.CreateMutationChangeLog)5 Graph (nl.knaw.huygens.timbuctoo.v5.util.Graph)5 ChangeMatcher.likeChange (nl.knaw.huygens.timbuctoo.v5.graphql.mutations.ChangeMatcher.likeChange)4 Test (org.junit.Test)4 Value (nl.knaw.huygens.timbuctoo.v5.graphql.mutations.Change.Value)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 Map (java.util.Map)1 ExecutionException (java.util.concurrent.ExecutionException)1 DataSet (nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSet)1 CursorQuad (nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.dto.CursorQuad)1 LogStorageFailedException (nl.knaw.huygens.timbuctoo.v5.filestorage.exceptions.LogStorageFailedException)1 ImmutableContextData (nl.knaw.huygens.timbuctoo.v5.graphql.datafetchers.dto.ImmutableContextData)1 User (nl.knaw.huygens.timbuctoo.v5.security.dto.User)1