Search in sources :

Example 1 with NotFoundException

use of nl.knaw.huygens.timbuctoo.core.NotFoundException 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 NotFoundException

use of nl.knaw.huygens.timbuctoo.core.NotFoundException 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 NotFoundException

use of nl.knaw.huygens.timbuctoo.core.NotFoundException in project timbuctoo by HuygensING.

the class D3GraphGeneratorServiceTest method getShouldGenerateAD3Graph.

@Test
public void getShouldGenerateAD3Graph() throws NotFoundException, IOException {
    Graph graph = newGraph().withVertex("relationCollection", v -> v.withProperty(Collection.ENTITY_TYPE_NAME_PROPERTY_NAME, "wwrelation").withProperty(Collection.IS_RELATION_COLLECTION_PROPERTY_NAME, true)).withVertex("vreNode", v -> v.withProperty(Vre.VRE_NAME_PROPERTY_NAME, "WomenWriters").withOutgoingRelation(Vre.HAS_COLLECTION_RELATION_NAME, "relationCollection")).withVertex(v -> v.withLabel(Collection.DATABASE_LABEL).withProperty(Collection.ENTITY_TYPE_NAME_PROPERTY_NAME, "wwperson").withIncomingRelation(Vre.HAS_COLLECTION_RELATION_NAME, "vreNode")).withVertex("v1", v -> v.withProperty("isLatest", true).withTimId(THE_UUID).withProperty("types", TYPES).withProperty("wwperson_tempName", "name1")).withVertex("v2", v -> v.withTimId("2").withProperty("types", TYPES).withOutgoingRelation(RELATION_NAME, "v1", r -> r.withAccepted("wwrelation", true).withIsLatest(true)).withProperty("wwperson_tempName", "name2")).withVertex("v3", v -> v.withTimId("3").withProperty("types", TYPES).withOutgoingRelation(RELATION_NAME_2, "v1", r -> r.withAccepted("wwrelation", true).withIsLatest(true)).withProperty("wwperson_tempName", "name3")).withVertex("v4", v -> v.withTimId("4").withProperty("types", TYPES).withOutgoingRelation(RELATION_NAME, "v2", r -> r.withAccepted("wwrelation", true).withIsLatest(true)).withProperty("wwperson_tempName", "name4")).withVertex("v5", v -> v.withTimId("5").withProperty("types", TYPES).withOutgoingRelation(UNREQUESTED_RELATION, "v1", r -> r.withAccepted("wwrelation_accepted", true).withIsLatest(true)).withProperty("wwperson_tempName", "name5")).withVertex("v6", v -> v.withTimId("6").withProperty("types", TYPES).withOutgoingRelation(RELATION_NAME, "v4", r -> r.withAccepted("wwrelation", true).withIsLatest(true)).withProperty("wwperson_tempName", "name6")).build();
    List<Vertex> vertices = graph.traversal().V().has("wwperson_tempName").asAdmin().clone().toList();
    Collections.sort(vertices, Comparator.comparing(vertexA -> ((String) vertexA.property("wwperson_tempName").value())));
    GraphWrapper graphWrapper = createGraphWrapper(graph);
    D3GraphGeneratorService underTest = new D3GraphGeneratorService(graphWrapper, HuygensIng.mappings);
    D3Graph result = underTest.get("wwperson", UUID.fromString(THE_UUID), Lists.newArrayList(RELATION_NAME, RELATION_NAME_2), 2);
    assertThat(result.getNodes().get(0), is(new Node(vertices.get(0), "wwperson")));
    assertThat(result.getNodes(), containsInAnyOrder(new Node(vertices.get(0), "wwperson"), new Node(vertices.get(1), "wwperson"), new Node(vertices.get(2), "wwperson"), new Node(vertices.get(3), "wwperson")));
    int indexOfRoot = getIndex(result.getNodes(), "077bf0b5-6b7d-45aa-89ff-6ecf2cfc549c");
    int indexOfPerson1 = getIndex(result.getNodes(), "2");
    int indexOfPerson2 = getIndex(result.getNodes(), "3");
    int indexOfPerson3 = getIndex(result.getNodes(), "4");
    assertThat(result.getLinks(), containsInAnyOrder(new Link(mockEdge(RELATION_NAME), indexOfRoot, indexOfPerson1), new Link(mockEdge(RELATION_NAME_2), indexOfRoot, indexOfPerson2), new Link(mockEdge(RELATION_NAME), indexOfPerson1, indexOfPerson3)));
}
Also used : HuygensIng(nl.knaw.huygens.timbuctoo.server.HuygensIng) Graph(org.apache.tinkerpop.gremlin.structure.Graph) Test(org.junit.Test) IOException(java.io.IOException) NotFoundException(nl.knaw.huygens.timbuctoo.core.NotFoundException) Collection(nl.knaw.huygens.timbuctoo.core.dto.dataset.Collection) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) UUID(java.util.UUID) GraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal) List(java.util.List) Lists(com.google.common.collect.Lists) GraphWrapper(nl.knaw.huygens.timbuctoo.server.GraphWrapper) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) BDDMockito.given(org.mockito.BDDMockito.given) Is.is(org.hamcrest.core.Is.is) Vre(nl.knaw.huygens.timbuctoo.model.vre.Vre) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Comparator(java.util.Comparator) Collections(java.util.Collections) TestGraphBuilder.newGraph(nl.knaw.huygens.timbuctoo.util.TestGraphBuilder.newGraph) GraphTraversalSource(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource) Edge(org.apache.tinkerpop.gremlin.structure.Edge) Mockito.mock(org.mockito.Mockito.mock) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) Graph(org.apache.tinkerpop.gremlin.structure.Graph) TestGraphBuilder.newGraph(nl.knaw.huygens.timbuctoo.util.TestGraphBuilder.newGraph) GraphWrapper(nl.knaw.huygens.timbuctoo.server.GraphWrapper) Test(org.junit.Test)

Example 4 with NotFoundException

use of nl.knaw.huygens.timbuctoo.core.NotFoundException 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)

Example 5 with NotFoundException

use of nl.knaw.huygens.timbuctoo.core.NotFoundException in project timbuctoo by HuygensING.

the class SingleEntityNTriple method get.

@GET
public Response get(@PathParam("collection") String collectionName, @PathParam("id") UUIDParam id, @QueryParam("rev") Integer rev) {
    return transactionEnforcer.executeAndReturn(timbuctooActions -> {
        try {
            Collection collection = timbuctooActions.getCollectionMetadata(collectionName);
            ReadEntity entity = timbuctooActions.getEntity(collection, id.get(), rev);
            URI rdfUri = entity.getRdfUri();
            String rdfString = rdfUri == null ? uriHelper.fromResourceUri(makeUrl(collectionName, id.get())).toString() : rdfUri.toString();
            StringBuilder sb = new StringBuilder();
            addRdfProp(rdfString, sb, "id", entity.getId() + "", "https://www.ietf.org/rfc/rfc4122.txt");
            entity.getRdfAlternatives().forEach(alt -> sb.append(asNtriples(new LinkTriple(rdfString, SAME_AS_PRED, alt))));
            TriplePropertyConverter converter = new TriplePropertyConverter(collection, rdfString);
            for (TimProperty<?> timProperty : entity.getProperties()) {
                try {
                    timProperty.convert(converter).getRight().forEach(triple -> sb.append(asNtriples(triple)));
                } catch (IOException e) {
                    LOG.error("Could not convert property with name '{}' and value '{}'", timProperty.getName(), timProperty.getValue());
                }
            }
            entity.getRelations().forEach(rel -> sb.append(asNtriples(new LinkTriple(rdfString, getRelationRdfUri(rel), getEntityRdfUri(rel)))));
            return commitAndReturn(Response.ok(sb.toString()).build());
        } catch (InvalidCollectionException e) {
            return commitAndReturn(Response.status(BAD_REQUEST).entity(e.getMessage()).build());
        } catch (NotFoundException e) {
            return commitAndReturn(Response.status(NOT_FOUND).build());
        }
    });
}
Also used : ReadEntity(nl.knaw.huygens.timbuctoo.core.dto.ReadEntity) LinkTriple(nl.knaw.huygens.timbuctoo.rdf.LinkTriple) Collection(nl.knaw.huygens.timbuctoo.core.dto.dataset.Collection) InvalidCollectionException(nl.knaw.huygens.timbuctoo.crud.InvalidCollectionException) NotFoundException(nl.knaw.huygens.timbuctoo.core.NotFoundException) IOException(java.io.IOException) TriplePropertyConverter(nl.knaw.huygens.timbuctoo.rdf.conversion.TriplePropertyConverter) URI(java.net.URI) GET(javax.ws.rs.GET)

Aggregations

NotFoundException (nl.knaw.huygens.timbuctoo.core.NotFoundException)14 IOException (java.io.IOException)6 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)6 JsonNode (com.fasterxml.jackson.databind.JsonNode)5 URI (java.net.URI)5 UUID (java.util.UUID)5 Collection (nl.knaw.huygens.timbuctoo.core.dto.dataset.Collection)4 InvalidCollectionException (nl.knaw.huygens.timbuctoo.crud.InvalidCollectionException)4 VertexDuplicator.duplicateVertex (nl.knaw.huygens.timbuctoo.database.tinkerpop.VertexDuplicator.duplicateVertex)4 Optional (java.util.Optional)3 CreateCollection (nl.knaw.huygens.timbuctoo.core.dto.CreateCollection)3 TestGraphBuilder.newGraph (nl.knaw.huygens.timbuctoo.util.TestGraphBuilder.newGraph)3 UserValidationException (nl.knaw.huygens.timbuctoo.v5.security.exceptions.UserValidationException)3 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)3 Test (org.junit.Test)3 Mockito.mock (org.mockito.Mockito.mock)3 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)2 Lists (com.google.common.collect.Lists)2 Instant (java.time.Instant)2 List (java.util.List)2