Search in sources :

Example 1 with InvalidCollectionException

use of nl.knaw.huygens.timbuctoo.crud.InvalidCollectionException in project timbuctoo by HuygensING.

the class WomenWritersJsonCrudServiceTest method getReturnsAJsonNodeWithARelationsPropertyWithTheGenderOfTheAuthors.

@Test
public void getReturnsAJsonNodeWithARelationsPropertyWithTheGenderOfTheAuthors() throws InvalidCollectionException, NotFoundException {
    UUID workId = UUID.randomUUID();
    TinkerPopGraphManager graphManager = newGraph().withVertex("work1", v -> {
        v.withOutgoingRelation("isCreatedBy", "pers1").withOutgoingRelation("isCreatedBy", "pers2").withVre("ww").withVre("").withType("document").isLatest(true).withTimId(workId.toString());
    }).withVertex("pers1", v -> v.withVre("ww").withVre("").withType("person").withProperty("displayName", "author1").withProperty("wwperson_gender", "FEMALE").isLatest(true).withTimId(UUID.randomUUID().toString())).withVertex("pers2", v -> v.withVre("ww").withVre("").withType("person").withProperty("displayName", "author2").withProperty("wwperson_gender", "FEMALE").isLatest(true).withTimId(UUID.randomUUID().toString())).withVertex("relationType", v -> v.withType("relationType").withVre("").withProperty("relationtype_regularName", "isCreatedBy").withProperty("relationtype_inverseName", "isCreatorOf")).wrap();
    WomenWritersJsonCrudService instance = createInstance(graphManager);
    JsonNode result = instance.get("wwdocuments", workId);
    assertThat(result.toString(), sameJSONAs(jsnO("@relations", jsnO("isCreatedBy", jsnA(jsnO("displayName", jsn("author1"), "gender", jsn("FEMALE")), jsnO("displayName", jsn("author2"), "gender", jsn("FEMALE"))))).toString()).allowingAnyArrayOrdering().allowingExtraUnexpectedFields());
}
Also used : UserValidator(nl.knaw.huygens.timbuctoo.v5.security.UserValidator) JsonBuilder.jsnO(nl.knaw.huygens.timbuctoo.util.JsonBuilder.jsnO) PropertyTypes.localProperty(nl.knaw.huygens.timbuctoo.model.properties.PropertyTypes.localProperty) TinkerPopGraphManager(nl.knaw.huygens.timbuctoo.server.TinkerPopGraphManager) TimbuctooActionsStubs(nl.knaw.huygens.timbuctoo.core.TimbuctooActionsStubs) CollectionBuilder(nl.knaw.huygens.timbuctoo.core.dto.dataset.CollectionBuilder) Converters.personNames(nl.knaw.huygens.timbuctoo.model.properties.converters.Converters.personNames) Test(org.junit.Test) NotFoundException(nl.knaw.huygens.timbuctoo.core.NotFoundException) UUID(java.util.UUID) Mockito(org.mockito.Mockito) InvalidCollectionException(nl.knaw.huygens.timbuctoo.crud.InvalidCollectionException) VresBuilder(nl.knaw.huygens.timbuctoo.model.vre.vres.VresBuilder) JsonBuilder.jsnA(nl.knaw.huygens.timbuctoo.util.JsonBuilder.jsnA) Optional(java.util.Optional) JsonNode(com.fasterxml.jackson.databind.JsonNode) SameJSONAs.sameJSONAs(uk.co.datumedge.hamcrest.json.SameJSONAs.sameJSONAs) URI(java.net.URI) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) TestGraphBuilder.newGraph(nl.knaw.huygens.timbuctoo.util.TestGraphBuilder.newGraph) JsonBuilder.jsn(nl.knaw.huygens.timbuctoo.util.JsonBuilder.jsn) UserValidationException(nl.knaw.huygens.timbuctoo.v5.security.exceptions.UserValidationException) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) TinkerPopGraphManager(nl.knaw.huygens.timbuctoo.server.TinkerPopGraphManager) JsonNode(com.fasterxml.jackson.databind.JsonNode) UUID(java.util.UUID) Test(org.junit.Test)

Example 2 with InvalidCollectionException

use of nl.knaw.huygens.timbuctoo.crud.InvalidCollectionException in project timbuctoo by HuygensING.

the class WomenWritersJsonCrudServiceTest method getReturnsTheLanguagesOfTheDocumentsWrittenByAPerson.

@Test
public void getReturnsTheLanguagesOfTheDocumentsWrittenByAPerson() throws InvalidCollectionException, NotFoundException {
    UUID pers1Id = UUID.randomUUID();
    TinkerPopGraphManager graphManager = newGraph().withVertex("work1", v -> v.withOutgoingRelation("isCreatedBy", "pers1", r -> r.withIsLatest(true).withAccepted("wwrelation", true)).withOutgoingRelation("hasWorkLanguage", "lang1", r -> r.withIsLatest(true).withAccepted("wwrelation", true)).withVre("ww").withVre("").withType("document").isLatest(true).withTimId(UUID.randomUUID().toString())).withVertex("work2", v -> v.withOutgoingRelation("isCreatedBy", "pers1", r -> r.withIsLatest(true).withAccepted("wwrelation", true)).withOutgoingRelation("hasWorkLanguage", "lang2", r -> r.withIsLatest(true).withAccepted("wwrelation", true)).withVre("ww").withVre("").withType("document").isLatest(true).withTimId(UUID.randomUUID().toString())).withVertex("pers1", v -> v.withVre("ww").withVre("").withType("person").withProperty("displayName", "author1").withProperty("wwperson_gender", "FEMALE").isLatest(true).withTimId(pers1Id.toString())).withVertex("lang1", v -> v.withVre("ww").withVre("").withType("language").withProperty("wwlanguage_name", "French").isLatest(true).withTimId(UUID.randomUUID().toString())).withVertex("lang2", v -> v.withVre("ww").withVre("").withType("language").withProperty("wwlanguage_name", "Dutch").isLatest(true).withTimId(UUID.randomUUID().toString())).withVertex("relationType1", v -> v.withType("relationType").withVre("").withProperty("relationtype_regularName", "isCreatedBy").withProperty("relationtype_inverseName", "isCreatorOf")).withVertex("relationType2", v -> v.withType("relationType").withVre("").withProperty("relationtype_regularName", "hasWorkLanguage").withProperty("relationtype_inverseName", "isWorkLanguageOf")).wrap();
    WomenWritersJsonCrudService instance = createInstance(graphManager);
    JsonNode result = instance.get("wwpersons", pers1Id);
    assertThat(result.toString(), sameJSONAs(jsnO("@authorLanguages", jsnA(jsn("French"), jsn("Dutch"))).toString()).allowingAnyArrayOrdering().allowingExtraUnexpectedFields());
}
Also used : UserValidator(nl.knaw.huygens.timbuctoo.v5.security.UserValidator) JsonBuilder.jsnO(nl.knaw.huygens.timbuctoo.util.JsonBuilder.jsnO) PropertyTypes.localProperty(nl.knaw.huygens.timbuctoo.model.properties.PropertyTypes.localProperty) TinkerPopGraphManager(nl.knaw.huygens.timbuctoo.server.TinkerPopGraphManager) TimbuctooActionsStubs(nl.knaw.huygens.timbuctoo.core.TimbuctooActionsStubs) CollectionBuilder(nl.knaw.huygens.timbuctoo.core.dto.dataset.CollectionBuilder) Converters.personNames(nl.knaw.huygens.timbuctoo.model.properties.converters.Converters.personNames) Test(org.junit.Test) NotFoundException(nl.knaw.huygens.timbuctoo.core.NotFoundException) UUID(java.util.UUID) Mockito(org.mockito.Mockito) InvalidCollectionException(nl.knaw.huygens.timbuctoo.crud.InvalidCollectionException) VresBuilder(nl.knaw.huygens.timbuctoo.model.vre.vres.VresBuilder) JsonBuilder.jsnA(nl.knaw.huygens.timbuctoo.util.JsonBuilder.jsnA) Optional(java.util.Optional) JsonNode(com.fasterxml.jackson.databind.JsonNode) SameJSONAs.sameJSONAs(uk.co.datumedge.hamcrest.json.SameJSONAs.sameJSONAs) URI(java.net.URI) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) TestGraphBuilder.newGraph(nl.knaw.huygens.timbuctoo.util.TestGraphBuilder.newGraph) JsonBuilder.jsn(nl.knaw.huygens.timbuctoo.util.JsonBuilder.jsn) UserValidationException(nl.knaw.huygens.timbuctoo.v5.security.exceptions.UserValidationException) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) TinkerPopGraphManager(nl.knaw.huygens.timbuctoo.server.TinkerPopGraphManager) JsonNode(com.fasterxml.jackson.databind.JsonNode) UUID(java.util.UUID) Test(org.junit.Test)

Example 3 with InvalidCollectionException

use of nl.knaw.huygens.timbuctoo.crud.InvalidCollectionException in project timbuctoo by HuygensING.

the class TimbuctooActions method getCollectionMetadata.

public Collection getCollectionMetadata(String collectionName) throws InvalidCollectionException {
    Vres vres = loadVres();
    Optional<Collection> collection = vres.getCollection(collectionName);
    return collection.orElseThrow(() -> new InvalidCollectionException(collectionName));
}
Also used : Vres(nl.knaw.huygens.timbuctoo.model.vre.Vres) Collection(nl.knaw.huygens.timbuctoo.core.dto.dataset.Collection) InvalidCollectionException(nl.knaw.huygens.timbuctoo.crud.InvalidCollectionException)

Example 4 with InvalidCollectionException

use of nl.knaw.huygens.timbuctoo.crud.InvalidCollectionException in project timbuctoo by HuygensING.

the class Index method createNew.

@POST
public Response createNew(@PathParam("collection") String collectionName, @HeaderParam("Authorization") String authHeader, ObjectNode body) throws URISyntaxException {
    Optional<User> user;
    try {
        user = userValidator.getUserFromAccessToken(authHeader);
    } catch (UserValidationException e) {
        user = Optional.empty();
    }
    Optional<User> newUser = user;
    if (!user.isPresent()) {
        return Response.status(Response.Status.UNAUTHORIZED).build();
    } else {
        return transactionEnforcer.executeAndReturn(timbuctooActions -> {
            JsonCrudService crudService = crudServiceFactory.newJsonCrudService(timbuctooActions);
            try {
                UUID id = crudService.create(collectionName, body, newUser.get());
                return commitAndReturn(Response.created(SingleEntity.makeUrl(collectionName, id)).build());
            } catch (InvalidCollectionException e) {
                return rollbackAndReturn(Response.status(Response.Status.NOT_FOUND).entity(jsnO("message", jsn(e.getMessage()))).build());
            } catch (IOException e) {
                return rollbackAndReturn(Response.status(Response.Status.BAD_REQUEST).entity(jsnO("message", jsn(e.getMessage()))).build());
            } catch (PermissionFetchingException e) {
                return rollbackAndReturn(Response.status(Response.Status.FORBIDDEN).entity(jsnO("message", jsn(e.getMessage()))).build());
            }
        });
    }
}
Also used : UserValidationException(nl.knaw.huygens.timbuctoo.v5.security.exceptions.UserValidationException) User(nl.knaw.huygens.timbuctoo.v5.security.dto.User) PermissionFetchingException(nl.knaw.huygens.timbuctoo.v5.security.exceptions.PermissionFetchingException) JsonCrudService(nl.knaw.huygens.timbuctoo.crud.JsonCrudService) InvalidCollectionException(nl.knaw.huygens.timbuctoo.crud.InvalidCollectionException) IOException(java.io.IOException) UUID(java.util.UUID) POST(javax.ws.rs.POST)

Example 5 with InvalidCollectionException

use of nl.knaw.huygens.timbuctoo.crud.InvalidCollectionException in project timbuctoo by HuygensING.

the class SingleEntity method put.

@PUT
public Response put(@PathParam("collection") String collectionName, @HeaderParam("Authorization") String authHeader, @PathParam("id") UUIDParam id, ObjectNode body) {
    Optional<User> user;
    try {
        user = userValidator.getUserFromAccessToken(authHeader);
    } catch (UserValidationException e) {
        user = Optional.empty();
    }
    Optional<User> newUser = user;
    if (!newUser.isPresent()) {
        return Response.status(Response.Status.UNAUTHORIZED).build();
    } else {
        UpdateMessage updateMessage = transactionEnforcer.executeAndReturn(timbuctooActions -> {
            JsonCrudService crudService = crudServiceFactory.newJsonCrudService(timbuctooActions);
            try {
                crudService.replace(collectionName, id.get(), body, newUser.get());
                return commitAndReturn(UpdateMessage.success());
            } catch (InvalidCollectionException e) {
                return rollbackAndReturn(UpdateMessage.failure(e.getMessage(), Response.Status.NOT_FOUND));
            } catch (NotFoundException e) {
                return rollbackAndReturn(UpdateMessage.failure("not found", Response.Status.NOT_FOUND));
            } catch (IOException e) {
                return rollbackAndReturn(UpdateMessage.failure(e.getMessage(), Response.Status.BAD_REQUEST));
            } catch (AlreadyUpdatedException e) {
                return rollbackAndReturn(UpdateMessage.failure("Entry was already updated", Response.Status.EXPECTATION_FAILED));
            } catch (PermissionFetchingException e) {
                return rollbackAndReturn(UpdateMessage.failure(e.getMessage(), Response.Status.FORBIDDEN));
            }
        });
        // committed in the database
        if (updateMessage.isSuccess()) {
            return transactionEnforcer.executeAndReturn(timbuctooActions -> {
                JsonCrudService crudService = crudServiceFactory.newJsonCrudService(timbuctooActions);
                try {
                    JsonNode jsonNode = crudService.get(collectionName, id.get());
                    return commitAndReturn(Response.ok(jsonNode).build());
                } catch (InvalidCollectionException e) {
                    return rollbackAndReturn(Response.status(Response.Status.NOT_FOUND).entity(jsnO("message", jsn("Collection '" + collectionName + "' was available a moment ago, but not anymore: " + e.getMessage()))).build());
                } catch (NotFoundException e) {
                    return rollbackAndReturn(Response.status(Response.Status.NOT_FOUND).entity(jsnO("message", jsn("not found"))).build());
                }
            });
        } else {
            return Response.status(updateMessage.getResponseStatus()).entity(jsnO("message", jsn(updateMessage.getException().get()))).build();
        }
    }
}
Also used : UserValidationException(nl.knaw.huygens.timbuctoo.v5.security.exceptions.UserValidationException) User(nl.knaw.huygens.timbuctoo.v5.security.dto.User) AlreadyUpdatedException(nl.knaw.huygens.timbuctoo.core.AlreadyUpdatedException) PermissionFetchingException(nl.knaw.huygens.timbuctoo.v5.security.exceptions.PermissionFetchingException) JsonCrudService(nl.knaw.huygens.timbuctoo.crud.JsonCrudService) InvalidCollectionException(nl.knaw.huygens.timbuctoo.crud.InvalidCollectionException) NotFoundException(nl.knaw.huygens.timbuctoo.core.NotFoundException) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) PUT(javax.ws.rs.PUT)

Aggregations

InvalidCollectionException (nl.knaw.huygens.timbuctoo.crud.InvalidCollectionException)10 JsonNode (com.fasterxml.jackson.databind.JsonNode)5 NotFoundException (nl.knaw.huygens.timbuctoo.core.NotFoundException)5 UserValidationException (nl.knaw.huygens.timbuctoo.v5.security.exceptions.UserValidationException)5 IOException (java.io.IOException)4 URI (java.net.URI)4 Optional (java.util.Optional)4 UUID (java.util.UUID)4 Collection (nl.knaw.huygens.timbuctoo.core.dto.dataset.Collection)4 JsonBuilder.jsn (nl.knaw.huygens.timbuctoo.util.JsonBuilder.jsn)4 JsonBuilder.jsnA (nl.knaw.huygens.timbuctoo.util.JsonBuilder.jsnA)4 JsonBuilder.jsnO (nl.knaw.huygens.timbuctoo.util.JsonBuilder.jsnO)4 Test (org.junit.Test)4 TimbuctooActions (nl.knaw.huygens.timbuctoo.core.TimbuctooActions)3 JsonCrudService (nl.knaw.huygens.timbuctoo.crud.JsonCrudService)3 User (nl.knaw.huygens.timbuctoo.v5.security.dto.User)3 PermissionFetchingException (nl.knaw.huygens.timbuctoo.v5.security.exceptions.PermissionFetchingException)3 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)3 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)3 Mockito.mock (org.mockito.Mockito.mock)3