Search in sources :

Example 1 with Person

use of org.springframework.data.neo4j.integration.shared.common.Person in project spring-data-neo4j by spring-projects.

the class DynamicRelationshipsIT method shouldWriteDynamicRelationships.

// DATAGRAPH-1447
@Test
void shouldWriteDynamicRelationships(@Autowired PersonWithRelativesRepository repository) {
    PersonWithRelatives newPerson = new PersonWithRelatives("Test");
    Map<TypeOfRelative, Person> relatives = newPerson.getRelatives();
    Person d = new Person();
    ReflectionTestUtils.setField(d, "firstName", "R1");
    relatives.put(TypeOfRelative.RELATIVE_1, d);
    d = new Person();
    ReflectionTestUtils.setField(d, "firstName", "R2");
    relatives.put(TypeOfRelative.RELATIVE_2, d);
    Map<TypeOfClub, ClubRelationship> clubs = newPerson.getClubs();
    ClubRelationship clubRelationship = new ClubRelationship("Brunswick");
    Club club1 = new Club();
    club1.setName("BTSV");
    clubRelationship.setClub(club1);
    clubs.put(TypeOfClub.FOOTBALL, clubRelationship);
    clubRelationship = new ClubRelationship("Boston");
    Club club2 = new Club();
    club2.setName("Red Sox");
    clubRelationship.setClub(club2);
    clubs.put(TypeOfClub.BASEBALL, clubRelationship);
    newPerson = repository.findById(repository.save(newPerson).getId()).get();
    assertThat(newPerson.getRelatives()).containsOnlyKeys(TypeOfRelative.RELATIVE_1, TypeOfRelative.RELATIVE_2);
    assertThat(newPerson.getClubs()).containsOnlyKeys(TypeOfClub.BASEBALL, TypeOfClub.FOOTBALL);
    try (Transaction transaction = driver.session().beginTransaction()) {
        long numberOfRelations = transaction.run(("MATCH (t:%s) WHERE id(t) = $id RETURN size((t)-->(:Person)) as numberOfRelations").formatted(labelOfTestSubject), Values.parameters("id", newPerson.getId())).single().get("numberOfRelations").asLong();
        assertThat(numberOfRelations).isEqualTo(2L);
        numberOfRelations = transaction.run(("MATCH (t:%s) WHERE id(t) = $id RETURN size((t)-->(:Club)) as numberOfRelations").formatted(labelOfTestSubject), Values.parameters("id", newPerson.getId())).single().get("numberOfRelations").asLong();
        assertThat(numberOfRelations).isEqualTo(2L);
    }
}
Also used : TypeOfClub(org.springframework.data.neo4j.integration.shared.common.PersonWithRelatives.TypeOfClub) Transaction(org.neo4j.driver.Transaction) Club(org.springframework.data.neo4j.integration.shared.common.Club) TypeOfClub(org.springframework.data.neo4j.integration.shared.common.PersonWithRelatives.TypeOfClub) PersonWithRelatives(org.springframework.data.neo4j.integration.shared.common.PersonWithRelatives) Person(org.springframework.data.neo4j.integration.shared.common.Person) ClubRelationship(org.springframework.data.neo4j.integration.shared.common.ClubRelationship) TypeOfRelative(org.springframework.data.neo4j.integration.shared.common.PersonWithRelatives.TypeOfRelative) Test(org.junit.jupiter.api.Test)

Example 2 with Person

use of org.springframework.data.neo4j.integration.shared.common.Person in project spring-data-neo4j by spring-projects.

the class DynamicRelationshipsIT method shouldReadDynamicRelationshipsWithCustomQuery.

// DATAGRAPH-1411
@Test
void shouldReadDynamicRelationshipsWithCustomQuery(@Autowired PersonWithRelativesRepository repository) {
    PersonWithRelatives person = repository.byCustomQuery(idOfExistingPerson);
    assertThat(person).isNotNull();
    assertThat(person.getName()).isEqualTo("A");
    Map<TypeOfRelative, Person> relatives = person.getRelatives();
    assertThat(relatives).containsOnlyKeys(TypeOfRelative.HAS_WIFE, TypeOfRelative.HAS_DAUGHTER);
    assertThat(relatives.get(TypeOfRelative.HAS_WIFE).getFirstName()).isEqualTo("B");
    assertThat(relatives.get(TypeOfRelative.HAS_DAUGHTER).getFirstName()).isEqualTo("C");
    Map<TypeOfClub, ClubRelationship> clubs = person.getClubs();
    assertThat(clubs).containsOnlyKeys(TypeOfClub.FOOTBALL);
    assertThat(clubs.get(TypeOfClub.FOOTBALL).getPlace()).isEqualTo("Brunswick");
    assertThat(clubs.get(TypeOfClub.FOOTBALL).getClub().getName()).isEqualTo("BTSV");
}
Also used : TypeOfClub(org.springframework.data.neo4j.integration.shared.common.PersonWithRelatives.TypeOfClub) PersonWithRelatives(org.springframework.data.neo4j.integration.shared.common.PersonWithRelatives) Person(org.springframework.data.neo4j.integration.shared.common.Person) ClubRelationship(org.springframework.data.neo4j.integration.shared.common.ClubRelationship) TypeOfRelative(org.springframework.data.neo4j.integration.shared.common.PersonWithRelatives.TypeOfRelative) Test(org.junit.jupiter.api.Test)

Example 3 with Person

use of org.springframework.data.neo4j.integration.shared.common.Person in project spring-data-neo4j by spring-projects.

the class Neo4jTemplateIT method saveAllAsWithDynamicProjectionOnSecondLevelShouldWork.

// GH-2420
@Test
void saveAllAsWithDynamicProjectionOnSecondLevelShouldWork() {
    Person p = neo4jTemplate.findOne("MATCH (p:Person {lastName: $lastName})-[r:LIVES_AT]-(a:Address) RETURN p, collect(r), collect(a)", Collections.singletonMap("lastName", "Siemons"), Person.class).get();
    p.setFirstName("Klaus");
    p.setLastName("Simons");
    p.getAddress().setCity("Braunschweig");
    p.getAddress().setStreet("Single Trail");
    Person.Address.Country country = new Person.Address.Country();
    country.setName("Foo");
    country.setCountryCode("DE");
    p.getAddress().setCountry(country);
    BiPredicate<PropertyPath, Neo4jPersistentProperty> predicate = create2LevelProjectingPredicate();
    List<Person> projections = neo4jTemplate.saveAllAs(Collections.singletonList(p), predicate);
    assertThat(projections).hasSize(1).first().satisfies(projection -> {
        assertThat(projection.getAddress().getStreet()).isEqualTo("Single Trail");
        assertThat(projection.getAddress().getCountry().getName()).isEqualTo("Foo");
    });
    p = neo4jTemplate.findById(p.getId(), Person.class).get();
    assertThat(p.getFirstName()).isEqualTo("Michael");
    assertThat(p.getLastName()).isEqualTo("Simons");
    assertThat(p.getAddress().getCity()).isEqualTo("Aachen");
    assertThat(p.getAddress().getStreet()).isEqualTo("Single Trail");
    assertThat(p.getAddress().getCountry().getName()).isEqualTo("Foo");
}
Also used : PropertyPath(org.springframework.data.mapping.PropertyPath) Person(org.springframework.data.neo4j.integration.shared.common.Person) Neo4jPersistentProperty(org.springframework.data.neo4j.core.mapping.Neo4jPersistentProperty) Neo4jIntegrationTest(org.springframework.data.neo4j.test.Neo4jIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 4 with Person

use of org.springframework.data.neo4j.integration.shared.common.Person in project spring-data-neo4j by spring-projects.

the class Neo4jTemplateIT method saveProjectionShouldWork.

// GH-2215
@Test
void saveProjectionShouldWork() {
    // Using a query on purpose so that the address is null
    DtoPersonProjection dtoPersonProjection = neo4jTemplate.find(Person.class).as(DtoPersonProjection.class).matching("MATCH (p:Person {lastName: $lastName}) RETURN p", Collections.singletonMap("lastName", "Siemons")).one().get();
    dtoPersonProjection.setFirstName("Micha");
    dtoPersonProjection.setLastName("Simons");
    DtoPersonProjection savedProjection = neo4jTemplate.save(Person.class).one(dtoPersonProjection);
    // Assert that we saved and returned the correct data
    assertThat(savedProjection.getFirstName()).isEqualTo("Micha");
    assertThat(savedProjection.getLastName()).isEqualTo("Simons");
    // Assert the result inside the database.
    Person person = neo4jTemplate.findById(savedProjection.getId(), Person.class).get();
    assertThat(person.getFirstName()).isEqualTo("Micha");
    assertThat(person.getLastName()).isEqualTo("Simons");
    assertThat(person.getAddress()).isNotNull();
}
Also used : Person(org.springframework.data.neo4j.integration.shared.common.Person) Neo4jIntegrationTest(org.springframework.data.neo4j.test.Neo4jIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 5 with Person

use of org.springframework.data.neo4j.integration.shared.common.Person in project spring-data-neo4j by spring-projects.

the class Neo4jTemplateIT method statementWithParamsShouldWork.

@Test
void statementWithParamsShouldWork() {
    Node person = Cypher.node("Person");
    List<Person> people = neo4jTemplate.find(Person.class).matching(Cypher.match(person).where(person.property("lastName").isEqualTo(Cypher.parameter("lastName", "Siemons"))).returning(person).build(), Collections.singletonMap("lastName", "Schnitzel")).all();
    assertThat(people).extracting(Person::getLastName).containsExactly("Schnitzel");
}
Also used : Node(org.neo4j.cypherdsl.core.Node) Person(org.springframework.data.neo4j.integration.shared.common.Person) Neo4jIntegrationTest(org.springframework.data.neo4j.test.Neo4jIntegrationTest) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)42 Person (org.springframework.data.neo4j.integration.shared.common.Person)42 Neo4jIntegrationTest (org.springframework.data.neo4j.test.Neo4jIntegrationTest)30 ClubRelationship (org.springframework.data.neo4j.integration.shared.common.ClubRelationship)12 Predicate (com.querydsl.core.types.Predicate)9 Transaction (org.neo4j.driver.Transaction)8 Club (org.springframework.data.neo4j.integration.shared.common.Club)8 StepVerifier (reactor.test.StepVerifier)8 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)6 Driver (org.neo4j.driver.Driver)6 Autowired (org.springframework.beans.factory.annotation.Autowired)6 Bean (org.springframework.context.annotation.Bean)6 Configuration (org.springframework.context.annotation.Configuration)6 ReactiveDatabaseSelectionProvider (org.springframework.data.neo4j.core.ReactiveDatabaseSelectionProvider)6 Neo4jBookmarkManager (org.springframework.data.neo4j.core.transaction.Neo4jBookmarkManager)6 ReactiveNeo4jTransactionManager (org.springframework.data.neo4j.core.transaction.ReactiveNeo4jTransactionManager)6 PersonWithRelatives (org.springframework.data.neo4j.integration.shared.common.PersonWithRelatives)6 TypeOfClub (org.springframework.data.neo4j.integration.shared.common.PersonWithRelatives.TypeOfClub)6 TypeOfRelative (org.springframework.data.neo4j.integration.shared.common.PersonWithRelatives.TypeOfRelative)6 ReactiveNeo4jRepository (org.springframework.data.neo4j.repository.ReactiveNeo4jRepository)6